PHP echo (), print (), Print_r () Usage _php tutorial

Source: Internet
Author: User
From my understanding of Echo (), print (), print_r () This function is echo can input string variable constants, print and Echo, but Print_r can print the array and the first two is not possible, let me explain in detail their usage and differences three.


ECHO is a PHP statement, print and Print_r are functions, the statement does not return a value, the function can have a return value (even if it is not used)
Print () prints only the values of a simple type variable (such as int,string)
Print_r () can print out values for complex type variables (such as arrays, objects)
echo outputs one or more strings

echo-output one or more strings
Descrīption
void Echo (String arg1 [, String ...])//return value is empty

The code is as follows Copy Code
echo "Hello", "friend";

print– output A string

Descrīption

int print (string arg)//return value is reshape

The code is as follows Copy Code

print "Hello friend";

You can do the following

The code is as follows Copy Code

$name =print "Nihao n";
$STR = ' Test print value is '. $name;
Eval ("$print =" $str ";");
Echo $print;

print_r-prints easy-to-understand information about variables.
BOOL Print_r (mixed expression [, bool return])//Return value is Boolean, parameter is mix type, can be string, shape, array, object class Print_r () display easy-to-understand information about a variable. If a string, integer, or float is given, the variable value itself is printed. If an array is given, the keys and elements are displayed in a certain format. object is similar to an array.
Print_r () Moves the pointer of the array to the last edge.
You can
Print_r (str);
Print_r (int);
Print_r (array);
Print_r (obj);

Let's take a look at the following example

There are four ways to output a string. Echo

Print ()

printf ()

Print_r ()
Echo
Multiple values can be output at one time, and multiple values are separated by commas. Echo is a language structure (language construct), not a real function and therefore cannot be used as part of an expression.

Grammatically correct: Echo "Hello", "World";
Syntax error: Echo ("Hello", "World");
Print ()
The function print () prints a value (its arguments) and returns True if the string is successfully displayed, otherwise false. Such as

The code is as follows Copy Code

if (!print ("Hello, World")) {

Die ("is not a listening to me");

}

printf ()
printf () originates from printf () in the C language. The function outputs a formatted string.
Syntax: printf (format,arg1,arg2,arg++)
format Specifies the string and how the variable is formatted;
Arg1, arg2, + + etc parameters will be inserted into the main string percent percent (%) Symbol. This function is executed step-by. In the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.
Example:? php

The code is as follows Copy Code

$str = "Hello";

$number = 123;

printf ("%s World". Day number%u ", $STR, $number);

?>
#Results ======
Hello World. Day Number 123

If the% symbol is more than the arg parameter, you must use a placeholder. The placeholder is inserted after the% symbol and consists of a number and a "$". See Example 3.
Example:? php

The code is as follows Copy Code

$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 simply print the string and the number, and the array is displayed with the enclosed key and the list of merit, beginning with an array. Such as

The code is as follows Copy Code

$a = Array (' name ' = ' Fred ', ' age ' = ' + ', ' wife ' = ' Wilma ');

Print_r ($a);
Output:array

{

[Name] + Fred

[Age] = 15

[Wife] = Wilma

}

objects are the same. Such as

The code is as follows Copy Code

Class P {

var $name = ' nat ';

// ...

}

$p = new P;

Print_r ($p);
Output:object

{

[Name] = NAT

}

However, the result of Print_r () output Boolean and null is meaningless because it is all printed "n". Therefore, using the Var_dump () function is more suitable for debugging

http://www.bkjia.com/PHPjc/631244.html www.bkjia.com true http://www.bkjia.com/PHPjc/631244.html techarticle from my understanding of Echo (), print (), print_r () This function is echo can input string variable constant, print is similar to echo, but Print_r can print array and the former two is not possible, below I ...

  • Related Article

    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.