php outputs the difference comparison of echo print print_r printf sprintf var_dump

  • 2020-06-19 09:51:07
  • OfStack

I have been developing with.net for 5 years, and I suddenly wanted to touch 1. Beyond net, that's where php comes in. In learning php, first look at a few output functions.
1. echo
echo() is not actually a function, it's an php statement, so you don't need to use parentheses on it. However, if you want to pass more than one argument to echo(), a parsing error occurs when you use parentheses. Also, echo returns void and does not return a value, so it cannot be used for assignment.
Example:

<?php  
$a = echo("55nav"); //  Error! Cannot be used for assignment   
echo "55nav"; // 55nav   
echo ("55nav"); // 55nav   
echo ("55nav","com"); // An error occurred. Parentheses could not pass more than one argument    
echo "55nav"," com"," is", " web";  //  You can separate multiple values with commas when you don't have parentheses,   Will be output  55nav com is web   
echo "55nav is  8 good  9 web.";  //  Whether or not a line is wrapped, the final display is 1 line  55nav is good web.  
$fistname="55nav"; 
echo "$fistname com"; //  if  $firstname = "55nav",  Will be output  55nav com.  
echo '$firstname com'; //  Since you use single quotes, the output will not be output $firstname It's the output  $firstname com 
?> 

2. print
print() and echo() use 1, but echo will be 1 dot faster than print. It's not actually a function, so you don't need to put parentheses around it. However, if you want to pass more than one argument to print(), a parsing error occurs when you use parentheses. Note that print always returns 1, which is not the same as echo, meaning you can use print to assign values, but it doesn't make sense.
Example:

<?php 
$a = print("55nav"); //  This is allowed   
echo $a; // $a The value is 1 
?> 

3. print_r function
The print_r function prints easy-to-understand information about variables.
Grammar: mixed print_r (mixed $expression [, bool return])
If the variable is string, integer or float, it will directly output its value, if the variable is an array, it will output a formatted array, easy to read, that is, key and value corresponding format. Similar for object objects. print_r has two arguments, the first is a variable, the second can be set to true, if set to true, it returns a string, otherwise it returns the Boolean value TRUE.
Example:

<?php 
 $a="55nav"; 
 $c = print_r($a);  
 echo $c;  // $c The value is TRUE  
 $c = print_r($a,true);  
 echo $c; // $c The value of is a string 55nav  
 ?> 

4. printf function
The printf function returns 1 formatted string.
Grammar: printf (format, arg1 arg2, arg + +)
The parameter format is the format of the conversion, starting with a percentage symbol (" % ") and ending with the conversion character. Here are the possible format values:
* %% %? Returns percentage symbol
* %b? 2
* %c? Characters according to the ASCII value
* %d, with signed decimal Numbers
* %e?? (e.g. 1.5e+3)
* %u unsigned decimal Numbers
Floating point number (local settings aware)
Floating point number (not local settings aware)
The digits of 8 are in hexadecimal
* %s wmstring
* %x? 006 (in lower case)
* %X, digits of 6 (capital letters)
Parameters such as arg1, arg2, arg++ are inserted into the percent sign (%) symbol in the main string. This function is executed step by step, inserting arg1 in the first % symbol, arg2 in the second % symbol, and so on. If the % symbol is more than the arg parameter, you must use placeholders. After the % symbol is inserted, the placeholder consists of a number and "\$". You can use Numbers to specify the parameters to display. See the example for details.
Example:

<?php 
printf("My name is %s %s . ","55nav", "com"); // My name is 55nav com .  
printf("My name is %1\$s %1\$s","55nav", "com"); //  in s Before adding 1\$ or 2\$..... Represents the position displayed for the following parameters, this line of output  My name is 55nav 55nav Because only the values are displayed 1 Two parameters.  
printf("My name is %2\$s %1\$s","55nav", "com"); // My name is com 55nav  
?> 

5. sprintf function
This function is used in the same way as printf1, except that it writes the formatted string to a variable instead of output.
Example:

<?php 
sprintf("My name is %1\$s %1\$s","55nav", "com");  // You will find that there is nothing to output.   
$out = sprintf("My name is %1\$s %2\$s","55nav", "com");  
echo $out;  // The output  My name is 55nav com  
?> 

6. var_dump function
Function: Output variable content, type, or string content, type, length. Often used for debugging.

<?php 
$a=100; 
var_dump($a); //int(100) 
$a=100.356; 
var_dump($a); //float(100.356) 
?> 


Related articles: