Home >Backend Development >PHP Tutorial >PHP output statement_PHP tutorial
1. echo
echo() is not actually a function, it is a php statement, so you don't need to use parentheses on it. However, if you wish to pass more than one argument to echo(), a parsing error will occur using parentheses. Moreover, echo returns void and does not return a value, so it cannot be used to assign values.
Example:
$a = echo("xshell"); // Error! Cannot be used for assignment
echo "xshell"; // xshell
echo ("xshell"); // xshell
echo ("xshell","net"); //An error occurred, multiple parameters cannot be passed due to parentheses
echo "xshell"," net"," is", " web"; // When brackets are not used, multiple values can be separated by commas, and xshell net is web
will be output.
echo "xshell is
good
web."; // Regardless of line breaks, the final display will be one line xshell is good web.
echo "$fistname net"; // If $firstname = "xshell", xshell net.
will be output
echo '$firstname net'; // Due to the use of single quotes, the value of $firstname will not be output, but $firstname net
?>
2. print
Print() has the same usage as echo(), but echo is slightly faster than print. It's actually not a function either, so you don't need to use parentheses on it. However, if you wish to pass more than one argument to print(), a parsing error will occur using parentheses. Note that print always returns 1, which is different from echo, that is, you can use print to assign values, but it has no practical meaning.
Example:
$a = print("xshell"); // This is allowed
echo $a; // The value of $a is 1
?>
3. print_r function
The print_r function prints easy-to-understand information about variables.
Syntax: mixed print_r ( mixed $expression [, bool return ] )
If the variable is string, integer or float, its value will be output directly. If the variable is an array, a formatted array will be output for easy reading, that is, the format corresponding to key and value. The same is true for object objects. print_r has two parameters, the first is a variable, and the second can be set to true. If set to true, a string will be returned, otherwise a Boolean value TRUE will be returned.
Example:
$a="xshell";
$c = print_r($a);
echo $c; // The value of $c is TRUE
$c = print_r($a, ture);
echo $c; // The value of $c is the string xshell
?>
4. printf function
The printf function returns a formatted string.
Syntax: printf(format,arg1,arg2,arg++)
The parameter format is the conversion format, starting with the percent sign ("%") and ending with the conversion character. The following are possible format values:
* %% – Returns the percent symbol
* %b – binary number
* %c – character
according to ASCII value
* %d – signed decimal number
* %e – Continuous counting method (such as 1.5e+3)
* %u – unsigned decimal number
* %f – floating point number (local settings aware)
* %F – floating point number (not local settings aware)
* %o – octal number
* %s – string
* %x – Hexadecimal number (lowercase letters)
* %X – hexadecimal number (uppercase letter)
Parameters such as arg1, arg2, arg++ will be inserted into the main string at the percent sign (%) symbol. The function is executed step by step, at the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, and so on. If there are more % symbols than arg arguments, you must use placeholders. The placeholder is inserted after the % sign and consists of a number followed by "$". You can use numbers to specify the displayed parameters. See the example for details.
Example:
printf("My name is %s %s.","xshell", "net"); // My name is xshell net.
printf("My name is %1$s %1$s","xshell", "net"); // Add 1$ or 2$ before s to indicate the position where the following parameters are displayed. This The line outputs My name is Ricky Ricky because only the first parameter is shown twice.
printf("My name is %2$s %1$s","xshell", "net"); // My name is net xshell
?>
5. sprintf function
This function is used in the same way as printf. The only difference is that this function writes the formatted string into a variable instead of outputting it.
Example:
sprintf("My name is %1$s %1$s","xshell", "net"); //You will find that nothing is output.
$out = sprintf("My name is %1$s %2$s","xshell", "net");
echo $out; //Output My name is xshell net
?>
6. var_dump function
Function: Output the content, type of the variable or the content, type, and length of the string. Often used for debugging.
$a=100;
var_dump($a); //int(100)
$a=100.356;
var_dump($a); //float(100.356)
?>