About PHP Printing functions We need to understand the types of PHP print functions, simple and functional differences, summarize the practical scenarios of each printing function and related best practices.
function Introduction
1. Echo
-
echo is used to output one or more strings without wrapping.
-
If the output object is not a string, an attempt is made to convert the object to a string, and the conversion fails to throw an exception
-
Echo belongs to the grammatical structure, not to the function
-
Use
<?phpclass CLS1 {}class Cls2 {public function __tostring () { return ' Cls2 '; }} echo "111"; ' 111 '//numeric type turns into Stringecho 111; ' 111 '//output bool type True is converted to ' 1 ', false turns to ' echo true; ' 1 ' echo false; "///Output object, attempt to invoke object __tostring method to serialize object Echo (new CLS1); Error:class CLS1 could not being converted to Stringecho (new CLS2); ' CLS2 '//Output multiple variables (non-string type will be tried to convert to string) echo, "One", new Cls2; ' 2211CLS2 '
2. Print
-
Print is a syntactic structure, not a function (same as ECHO)
-
Print can only accept one string at a time (distinguished from Echo)
-
Other effects with Echo
-
Use
<?phpprint ' Hello World ';
3. Print_r ()
-
Used to print easy-to-understand information about variables. (does not include variable type information)
-
For objects, print all properties (public, protected, private)
-
Allow only one variable at a time (different from Echo)
-
Use
<?phpclass Cls1 {public $var 1 = 111; Protected $var 2 = 222; Private $var 3 = 333; Public function func () {}} $s = "111"; $n = 1; $b = false; $f = 1.1222; $o = new Cls1 (); $arr </span> = [<span class= "HL Js-string "> ' 1 ' </span> => <span class= "Hljs-number" >1</SPAN> <span class= "hljs-string" > "3" </SPAN>, <span class= " Hljs-number ">5</span>, <span class=" hljs-variable "> $o];p rint_r ($s); ' 111 ' Print_r ($n); 1print_r ($b); "Print_r ($f); 1.1222print_r ($arr);/*array ( [1] = 1 [2] = 3 [3] = 5 [4] = = CLS1 Object ( [ VAR1] = 111 [var2:protected] = 222 [Var3:Cls1:private] = 333 )) */print_r ($o);/*CLS1 Object ( [var1] = 111 [var2:protected] = 222 [var3:Cls1:private] = 333) */
4. Var_dump
<?phpclass CLS1 {public $var 1 = 111; Protected $var 2 = 222; Private $var 3 = 333; Public function func () {}} $s = "111"; $n = 1; $b = false; $f = 1.1222; $o = new Cls1 (); $arr </span> = [<span class= "HL Js-string "> ' 1 ' </span> => <span class= "Hljs-number" >1</SPAN> <span class= "hljs-string" > "3" </SPAN>, <span class= " Hljs-number ">5</span>, <span class=" hljs-variable "> $o];var_dump ($s); String (3) "111" Var_dump ($n); Int (1) var_dump ($b); BOOL (FALSE) Var_dump ($f); Float (1.1222) var_dump ($arr);/*array (4) {[1]=> int (1) [2]=> string (1) "3" [3]=> int (5) [4]=> Object (CLS1) #1 (3) {["var1"]=> Int (111) ["VAR2":p rotected]=> Int (222) ["Var3": "CLS1":p rivate]=> Int ( 333)}}*/var_dump ($o),/*object (CLS1) #1 (3) {["var1"]=> Int (111) ["VAR2":p rotected]=> Int (222) ["Var3": "CLS1": private]=> Int (333)}*/var_dump ($s </span>, <span class= "hljs-variable" > $n, $b</span>, <span class= "hljs-variable" > $f);/*string (3) "111" INT (1) bool (false) float (1.1222) */
5. Other
-
printf (): A scene for formatting output
-
sprintf () is used to format a string scene, which differs from printf () in that the former is directly output to standard output, and the latter string is returned as a variable
-
Use
<?php$n = one, $s = "a", $f = "a";p rintf ("%d%s%f", $n </span>, <span class= "hljs-variable" > $s, $f); 112222.000000$s2</span> = <span class= "Hljs-keyword" >sprintf</span> (<span class= " Hljs-string ">" <span class= "hljs-variable" >%d</span><span class= "hljs-variable" >%s</span ><span class= "hljs-variable" >%f</span> "</span>, <span class=" hljs-variable "> $n, $s </ Span>, <span class= "hljs-variable" > $f); Echo $s 2; 112222.000000
Best Practices (Service debugging)
-
Do not use print, echo can be used instead
-
Because echo can only output simple strings, need to understand the variable details without echo, we recommend using Print_r (), Var_dump () (Personal preference Var_dump)
-
Debug information output to the browser, try to add echo ' <pre> '; Produce a more friendly display of information
4. Var_dump
<?phpclass CLS1 {public $var 1 = 111; Protected $var 2 = 222; Private $var 3 = 333; Public function func () {}} $s = "111"; $n = 1; $b = false; $f = 1.1222; $o = new Cls1 (); $arr </span> = [<span class= "HL Js-string "> ' 1 ' </span> => <span class= "Hljs-number" >1</SPAN> <span class= "hljs-string" > "3" </SPAN>, <span class= " Hljs-number ">5</span>, <span class=" hljs-variable "> $o];var_dump ($s); String (3) "111" Var_dump ($n); Int (1) var_dump ($b); BOOL (FALSE) Var_dump ($f); Float (1.1222) var_dump ($arr);/*array (4) {[1]=> int (1) [2]=> string (1) "3" [3]=> int (5) [4]=> Object (CLS1) #1 (3) {["var1"]=> Int (111) ["VAR2":p rotected]=> Int (222) ["Var3": "CLS1":p rivate]=> Int ( 333)}}*/var_dump ($o),/*object (CLS1) #1 (3) {["var1"]=> Int (111) ["VAR2":p rotected]=> Int (222) ["Var3": "CLS1": private]=> Int (333)}*/var_dump ($s </span>, <span class= "hljs-variable" > $n, $b</span>, <span class= "hljs-variable" > $f);/*string (3) "111" INT (1) bool (false) float (1.1222) */