Php print () and printf () function tutorial
Definition and usage
The print () function outputs one or more strings.
Syntax
Print (strings)
Parameter |
Description |
Strings |
Required. One or more strings to be sent to the output |
Tips and instructions
Note: The print () function does not have actual functions, so you do not need to use parentheses.
Tip: The print () function is slightly lower than Echo ().
--------------------------------------------------------------------------------
Example 1
<? Php $ str = "Who's Kai Jim? "; Print $ str; print" <br/> "; print $ str." <br/> I don't know! ";?>
Result.
Who's Kai Jim? Who's Kai Jim? I don't know!
Now let's take a look at the printf function.
Definition and usage
Output the formatted string in the printf function.
The arg1, arg2, ++ parameter inserts a major string showing signs of percentage (%. Works of this function are "one step ". In first % signs, arg1 inserts, signs in Second %, arg2 inserts, etc.
Syntax
Printf (format, arg1, arg2, arg ++)
Parameter |
Description |
Format |
Required. The specified string and how to format the variable. Possible format values: %-return percent number % number B-binary number % C group-character according to a SC II value % d-signature decimal number % specialized-scientific symbol (such as 1. 2 e + 2) % u-unsigned decimal number % female-floating point number (realized by local settings) % female-floating point number (not realized by local settings) % s-string % of % o-no octal characters-hexadecimal (lower case) % of devices-additional value of hexadecimal (upper case) format. The % and text (for example, 0.2%, female) between these locations: + (Power Capital + and-the front number. By default, only the negative number is marked) '(specifies what is used as the fill. Default space. Must use the same width. Example: % 'x20s (this uses "X" as the fill)-(left reason variable value) [0-9] (specified variable value with the lowest hold width ). [0-9] (specify the number of decimal places or maximum string length) Note: If there are multiple additional formats for value, they must be in the same order as above.
|
Arg1 |
Required. The argument to be inserted at the first %-sign in the format string |
Arg2 |
Optional. The argument to be inserted at the second %-sign in the format string |
Arg ++ |
Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string |
Tips and instructions
Note: If there are more signs % of the above arguments, you must use placeholders. Reserved location inserted % signed by argument, quantity and "$ ". See three examples.
Tip: Related functions: fprintf (), sprintf (), vfprintf (), vprintf (), and vsprintf ().
--------------------------------------------------------------------------------
Example 1
<? Php $ str = "Hello"; $ number = 123; printf ("% s world. Day number % u", $ str, $ number);?>
Output result
Hello world. Day number 123
Instance 2.
<? Php $ number = 123; printf ("% f", $ number);?> |
Result.