PHP echo, print_r (expression), var_dump (expression) difference, print_rvar_dump

Source: Internet
Author: User

PHP echo, print_r (expression), var_dump (expression) difference, print_rvar_dump

All three are php statements with output functions, but print_r (expression), var_dump (expression) is a function, and echo is only a language structure, not a function, so it cannot be part of the expression.
For data types in php 8,


What data classes can be output by echo, print_r, and var_dump in PHP? What are their differences?

Echo 'output A string'; // only strings and numbers can be output.

Print_r-prints easy-to-understand information about variables. Generally, the output array structure is used.

Var_dump-prints information about a variable, which is generally used for printing arrays and objects.

Just use it.

What are the differences between echo (), print (), and print_r () in PHP?

Four methods can output strings. Echo

Print ()

Printf ()

Print_r ()
Echo
Multiple values can be output at a time. Multiple values are separated by commas. Echo is a language construct rather than a real function. Therefore, it cannot be used as part of an expression.

Correct syntax: echo "Hello", "World ";
Syntax error: echo ("Hello", "World ");
Print ()
The print () function prints a value (its parameter). If the string is successfully displayed, true is returned. Otherwise, false is returned. For example, if (! Print ("Hello, World ")){

Die ("you are not listening to me ");

}
Printf ()
Printf () is derived from printf () in C language (). This function outputs formatted strings.
Syntax: printf (format, arg1, arg2, arg ++)
Format specifies the string and how to format the variable;
Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on.
Example :? Php

$ Str = "Hello ";

$ Number = 123;

Printf ("% s world. Day number % u", $ str, $ number );

?>
# Results ======
Hello world. Day number 123

If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted with the % symbol, it consists of numbers and "\ $. See example 3.
Example :? Php

$ Number = 123;

Printf ("With 2 decimals: % 1 \ $. 2fbr/> With no decimals: % 1 \ $ u", $ number );

?>
# Result
With 2 decimals: 123.00
With no decimals: 123

Print_r () and var_dump ()
Print_r () can print strings and numbers, while the Array is displayed in the form of an enclosed key and a list of values, and starts with Array. For example, $ a = array ('name' => 'fred ', 'age' => '15', 'wife' => 'wilm ');

Print_r ($ );
Output: Array

{

[Name] => Fred

[Age] => 15

[Wife] => Wilma

}
The same is true for objects. For example, class P {

Var $ name = 'nat ';

//...

}

$ P = new P;

Print_r ($ p );
Output: Object

{

[Name] => nat

}
But print_r () outputs the Boolean value and the NULL result are meaningless, because both print & quo ...... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.