$values = array(0, 0.0, false, '');
var_dump($values);
print_r ($values);
With print_r you can't tell the difference between 0 and 0.0, or false and '':array(4) {
[0]=>
int(0)
[1]=>
float(0)
[2]=>
bool(false)
[3]=>
string(0) ""
}
Array
(
[0] => 0
[1] => 0
[2] =>
[3] =>
)
echo
- Outputs one or more strings separated by commas
- No return value
e.g.echo "String 1", "String 2"
- echo is a statement i.e used to display the output. it can be used with parentheses echo or without parentheses echo.
- echo can pass multiple string separated as ( , )
- echo doesn’t return any value
- echo is faster then print
- Outputs only a single string
- Returns
1, so it can be used in an expression
e.g.print "Hello"
or,if ($expr && print "foo")
- Print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print.
- using print can doesn’t pass multiple argument
- print always return 1
- it is slower than echo
- Outputs a human-readable representation of any one value
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Useful when debugging
- May return its output as a return value (instead of echoing) if the second optional argument is given
- Outputs a human-readable representation of one or more values separated by commas
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Uses a different output format to
print_r(), for example it also prints the type of values - Useful when debugging
- No return value
- Outputs a human-readable and PHP-executable representation of any one value
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Uses a different output format to both
print_r()andvar_dump()- resulting output is valid PHP code! - Useful when debugging
- May return its output as a return value (instead of echoing) if the second optional argument is given
- Even though
printcan be used in an expression, I recommend people avoid doing so, because it is bad for code readability (and because it's unlikely to ever be useful). The precedence rules when it interacts with other operators can also be confusing. Because of this, I personally don't ever have a reason to use it overecho. - Whereas
echoandprintare language constructs,print_r()andvar_dump()/var_export()are regular functions. You don't need parentheses to enclose the arguments toechoorprint(and if you do use them, they'll be treated as they would in an expression).
No comments:
Post a Comment