Hello... Friends today we are going to discus about String , functions & constants
creating a string & accessing : we can create & use a variable string as a new variable assignment we can use single quote ( ' ) as well double quote ( " ) marks both having there spacial meaning like
$myString = 'name';
$myString = "name";
when we are using a single quote then the variable string can be used directly as it is as it define while when we use a double quote then it gives us the value defined in the variable.
here in this same script i'll try to show you various things representation of string , with single quote ,double quotes & calling multiple values in a single variable & also trying to tell how we can find the error & resolve it in our script :p
now tester we want to test how long is this string value then we have a function here strlen () which is used to get the length of the string value
here marked values tells about the length of the strings
String support various function some are as :
some functions with Searching Functions are :
str_word_count() --- to count the words in a string
strstr() --- to search any txt in serched string
strpos() & strrpos() --- tells index positions of Ist & last occurence of the word in txt resp
substr_count() --- how many times the searched txt occurs
strpbrk() --- searches a string for any of a list characters
Some functions with Replacing functions are :
str_replace() --- replace all occurrences of the search text within the target string
substr_replace() --- replace a specific portion of target string with another string
strtr() --- replace certain characters in the target string with other characters
Some functions with formatting functions are :
printf() and sprintf() --- Simple formatting data
trim(), ltrim() & rtrim() --- remove white space from string
str_pad() --- add padding in String
wordwrap() --- wrapping Lines of Text
number_format() --- formatting numbers
String :
any string is a simply a sequence of characters. if we take an example about string then "Hello" , "What is your name ?" , "My roll no is 009" , "@ywat%!&#" these all are valid stringscreating a string & accessing : we can create & use a variable string as a new variable assignment we can use single quote ( ' ) as well double quote ( " ) marks both having there spacial meaning like
$myString = 'name';
$myString = "name";
when we are using a single quote then the variable string can be used directly as it is as it define while when we use a double quote then it gives us the value defined in the variable.
here in this same script i'll try to show you various things representation of string , with single quote ,double quotes & calling multiple values in a single variable & also trying to tell how we can find the error & resolve it in our script :p
now tester we want to test how long is this string value then we have a function here strlen () which is used to get the length of the string value
here marked values tells about the length of the strings
String support various function some are as :
- Searching
- Replacing
- Formatting
some functions with Searching Functions are :
str_word_count() --- to count the words in a string
strstr() --- to search any txt in serched string
strpos() & strrpos() --- tells index positions of Ist & last occurence of the word in txt resp
substr_count() --- how many times the searched txt occurs
strpbrk() --- searches a string for any of a list characters
Some functions with Replacing functions are :
str_replace() --- replace all occurrences of the search text within the target string
substr_replace() --- replace a specific portion of target string with another string
strtr() --- replace certain characters in the target string with other characters
Some functions with formatting functions are :
printf() and sprintf() --- Simple formatting data
trim(), ltrim() & rtrim() --- remove white space from string
str_pad() --- add padding in String
wordwrap() --- wrapping Lines of Text
number_format() --- formatting numbers
Functions :
Function is a block of code that performs a specific task , a function accepts one or more arguments any function read those arguments & works on those.
if we talk more about function then we can say any function is a black box .The code that calls the unction does not need to know what is inside the function same like in black box we only means with the job done
examples of functions as bindec() ,base_convert() , Str_pad() , str_word_count() , wordwrap () etc all are the functions
why we use the function
How to call a function
Function with variable
How to write your own function
How to create anonymous function
why we use the functions :
we use the functions various time in php because
To avoid the duplicate code
To reduce errors in code
To reduce the coding time etc
How to call a function :
As we studied in variables a function can be call simply just with function name example as
functionName() --- function calling without argument
functionName( arg1, arg2 ) --- function calling with arguments
Function with variable :
we also can store the function in a variable too if a function returns a value, then we can assign the value to a variable :
$returnvalue = functionName (arg );
but we have some other conditions too like we need to call any function various time in function then we need to store the function name in a variable & we can call it easily by using variable
How to write your own function :
here we also can create our own functions too & call the in our scripts at various places
& to write our own function we just need to call word "function" just calling before
Creating :
function OurFunctionName ( ) {
// write what you want from this function ;
}
Calling :
OurFunctionName ( ) ;
if we want to pass parameters then we can simple insert in it & also we can call any variable inside it too as :
function ourFunctionName ( $parameter1, $parameter2 ) {
// do action you want to perform from the function ;
}
How to create anonymous function :
anonymous function is a function without name. we mainly create anonymous functions mainly to make function dynamic & we can create an anonymous function just by using function () method call
here you can see it clearly we are just storing the value in variables, we store this anonymous function is variable & just calling variable in different part in our script so we can use this function in our code for security perspective without disclosing anything we are calling in our script :p
define ("salary", 500000);
print salary;
Remember constants are case sensitive that means "salary" is different than "Salary" but we can make it case insensitive by adding true in the end just try it as
define("salary" , 500000, true);
print Salary;
if we talk more about function then we can say any function is a black box .The code that calls the unction does not need to know what is inside the function same like in black box we only means with the job done
examples of functions as bindec() ,base_convert() , Str_pad() , str_word_count() , wordwrap () etc all are the functions
why we use the function
How to call a function
Function with variable
How to write your own function
How to create anonymous function
why we use the functions :
we use the functions various time in php because
To avoid the duplicate code
To reduce errors in code
To reduce the coding time etc
How to call a function :
As we studied in variables a function can be call simply just with function name example as
functionName() --- function calling without argument
functionName( arg1, arg2 ) --- function calling with arguments
Function with variable :
we also can store the function in a variable too if a function returns a value, then we can assign the value to a variable :
$returnvalue = functionName (arg );
but we have some other conditions too like we need to call any function various time in function then we need to store the function name in a variable & we can call it easily by using variable
How to write your own function :
here we also can create our own functions too & call the in our scripts at various places
& to write our own function we just need to call word "function" just calling before
Creating :
function OurFunctionName ( ) {
// write what you want from this function ;
}
Calling :
OurFunctionName ( ) ;
if we want to pass parameters then we can simple insert in it & also we can call any variable inside it too as :
function ourFunctionName ( $parameter1, $parameter2 ) {
// do action you want to perform from the function ;
}
How to create anonymous function :
anonymous function is a function without name. we mainly create anonymous functions mainly to make function dynamic & we can create an anonymous function just by using function () method call
here you can see it clearly we are just storing the value in variables, we store this anonymous function is variable & just calling variable in different part in our script so we can use this function in our code for security perspective without disclosing anything we are calling in our script :p
Constants :
Constant are same like variables except that once they define, means as we define them once they will not change they remains same. like variables constant do not have a $ sign in beginning and constants are automatically global across your entire script,unlike variables. we set a constant by using define () function. it takes two parameters one is name of the constant & second one is value of the constant like as :define ("salary", 500000);
print salary;
Remember constants are case sensitive that means "salary" is different than "Salary" but we can make it case insensitive by adding true in the end just try it as
define("salary" , 500000, true);
print Salary;
0 comments