Echo function ( ) and Print ( )
This function is a function that is very frequently found in PHP , because this function is used to display a string or text to the browser . The syntax is as follows :
Echo (string )Surely you are familiar about the use of these functions , because in previous articles has been very often used as an example.
Print (string )
The function printf () and Sprint ( )
Both these functions is used to display output to the browser with a specific format . The syntax is as follows :
Printf (format [, argument ] )Parameter format is always marked with characters percent ( % ) , followed by certain characters that provide specifications to provide results with a specific format . Character giver specifications are:
Sprintf (format [, argument ] )
Character | Information |
b | The argument is treated as an integer , and displayed as a binary number. |
c | The argument is treated as an integer , and displayed as a character with a value ASCIInya . |
d | The argument is treated as an integer , and displayed as a decimal number . |
f | The argument is treated as a double , and displayed as floating point numbers . |
o | The argument is treated as an integer , and displayed as octal numbers . |
s | The argument is treated and displayed as a string . |
x | The argument is treated as an integer and displayed as a hexadecimal number ( with lowercase) . |
X | The argument is treated as an integer and displayed as hexadecimal numbers ( with capital letters ) . |
$ angka1 = 68.75 ;Among the characters % and character of the giver specifications can also be inserted figures. Figures in front of the character of the giver specification indicates the number of digits or the number of characters (minimum ) are shown. If there is a point number , meaning it shows the number of decimal places . For example "% 01.2f "indicates that the numbers are displayed as floating point and there should be two decimal places displayed , "% 02d "indicates that the number is displayed as an integer and there should be two digits are displayed , so if there are only eight digits , will be changed to 2008 .
$ angka2 = 54.35 ;
$ number = $ angka1 + $ angka2 ;
/ / echo $ number will produce " 123.1 ";
$ format = sprintf ( "% 01.2f " , $ number) ;
/ / echo $ format will result in " 123.10 "
Example :
$ year = 1982 ;
$ month = 2 ;
$ day = 27 ;
$ date = sprintf ( "% 04d -% 02d -% 02d " , $ year , $ month , $ day) ;
/ / echo $ date will result in " 1982-02-27 "
Htmlentities function ( )
This function is used to stop the process penerjemaahan HTML tags by the browser, so HTML tags will be allowed to appear as it is, without translation by the browser . The syntax is as follows :
Htmlentities (string )Example :
$ str = " Click Here < / a> " ;If the variable $ str directly subjected to function echo () , then what emerges is a link that reads " Click Here " , but if subjected to htmlentities ( ) first as shown by the variable $ result , then what emerges is " Click Here < / a > " .
$ result = htmlentities ( $ str ) ;
echo "$ str ";
echo "$ result ";
?>
Things like this is useful for example if we want to show the use of HTML tutorials in the form of HTML files as well. As examples, for instance we want to teach how to make a link . If you want to write " Click Here < / a > "without translated by the browser , then we should write it as follows :
Click Here < / a >Obviously a very difficult and time consuming is not it? Htmlentities () will facilitate our work.
Strstr function ( ) , Stristr ( ) , and Strchr ( )
The third function is used to find the existence of a string within another string. The syntax is as follows :
Strstr ( destination , search )Of the three functions, the only stristr () which is not case sensitive . Parameter is the goal sought by the search string . The results obtained are starting from the first search character is found until the end of the destination string . If the search string is not contained in string destination , then the function will produce a value of False.
Stristr ( destination , search )
Strchr ( destination , search )
Example :
$ Email = " user@domain.com ";
$ Domain = strstr ( $ email ,'@');
echo $ domain ;
will generate "@ domain.com "
Strlen function ( )
This function is used to measure the length of a string of characters . The syntax is as follows :
Strlen (string )
A space will be counted as a character .
Example :
$ str = "Test the number of variables ";Strrev function ( )
$ PJ = strlen ( $ str ) ;
/ / variables $ PJ worth 18
This function is used to reverse the order of the characters string compiler from front to rear to be from back to front . In other words, this function is used to read the string in reverse .
Strrev (string )Example :
strrev echo ( $ str ) ;Function str_replace ( )
/ / result is " PHP UTI hadum rajaleB " ? >
This function is used to replace a string with another string . The syntax is as follows :
Str_replace ( yang_diganti , substitutes , goal )Parameter yang_diganti indicates a string that will be replaced .
Parameter substitute indicates a string that will replace it.
Parameter goal is the overall string containing string in which yang_diganti. Can also refer to a particular variable that contains a string .
Example :
echo str_replace ( " difficult " , " easy " , $ str ) ;The function substr ( )
/ / result is " Learning PHP is easy " ? >
This function is used to retrieve or bypass a section of a string and displays it as a separate string . The syntax is as follows :
Substr (string , start [ , length ] )String will be taken or withheld from the character located at the number indicated by the parameters start with the number of digits indicated by the length parameter .
Example :
$ rest = substr ( " ABCDEF " , 1 ) ; / / produces " bcdef "Start parameters can also be filled with the negative . If negative , then the calculation starts from the very back of the character .
$ rest = substr ( " ABCDEF " , 1 , 3 ) ; / / produces " BCD "
Example :
$ rest = substr ( " ABCDEF " , -1 ) ; / / produces " f "
$ rest = substr ( " ABCDEF " , -2 ) ; / / produces " ef "
$ rest = substr ( " ABCDEF " , -3 , 1 ) ; / / produces " d "
Strtolower function ( ) and Strtoupper ( )
Both these functions are used to convert a string into all uppercase or all lowercase letters . The syntax is as follows :
Strtolower (string )Example :
Strtoupper (string )
$ str = "string consists of upper and lower case ";Strpos function ( )
echo strtolower ( $ str ) ;
/ / result is : " This string consists of upper and lower case "
echo strtoupper ( $ str ) ;
/ / result is : " STRING consists of CAPITALS AND SMALL "
This function is used to determine the position of a string within another string . The syntax is as follows :
Strpos ( destination , search )If the destination string , there are more than one character by character looking for a search , found the first characters will be used.
Example :
$ str = " bambang ";Note that it is very easy to avoid confusion between the characters found in the position to zero with a character not found. Therefore we need a way to distinguish , as follows :
$ search = strpos ( $ str , " b " ) ;
/ / echo $ search will yield 0
$ Pos = strpos ( $ str , " b " ) ;Perhaps you are confused , what does all these functions ? Indeed as explained by these functions seems to play only for strings only, but later if the discussion we have to go deeper and begin to build a web application , these functions will appear to its usefulness . But it is not possible to build a web application without learning first base , instead : -p ? , good luck .
if ( $ pos === false) { / / note the number of signs = no 3
echo " Not found ";
}
/ / in PHP prior to 4.0b3 :
$ Pos = strpos ( $ str , " b " ) ;
if ( is_string ( $ pos ) & & ! $ pos ) {
echo " Not found "; ? >
0 komentar:
Post a Comment