Home > Backend Development > PHP Tutorial > Introduction to the usage of echo, print, print_r, printf, sprintf, var_dump in php_PHP tutorial

Introduction to the usage of echo, print, print_r, printf, sprintf, var_dump in php_PHP tutorial

WBOY
Release: 2016-07-13 17:13:44
Original
1038 people have browsed it

The article introduces echo, print, print_r, printf, sprintf, and var_dump. Friends who need to know more can refer to it.

1. echo
Definition and Usage
The PHP echo() function outputs one or more strings.

echo "" This method is also possible, no parentheses are needed

Grammar
echo(strings)
Parameter Description
strings required. One or more strings to send to the output.

Tips and Notes
Note: echo() is not actually a function, so you don't need to use parentheses with it. However, if you wish to pass one or more arguments to echo(), then using parentheses will cause a parsing error.

Tip: The echo() function is a little faster than the print() function.

Tip: The echo() function can use simplified syntax. See example 5.

The code is as follows Copy code
 代码如下 复制代码

例子
例子 1
$str = "Who's John Adams?";
echo $str;
echo "
";
echo $str."
I don't know!";
?>

  输出:

Who's John Adam?
Who's John Adam?
I don't know!

例子 2
echo "This text spans multiple lines.";
?>

  输出:

This text spans multiple lines.

例子 3
echo 'This ','string ','was ','made ','with multiple parameters';
?>

  输出:

This string was made with multiple parameters

例子 4
  单引号和双引号的不同之处。单引号仅输出变量名,而不是值:

$color = "red";
echo "Roses are $color";echo "
";
echo 'Roses are $color';?>

  输出:

Roses are red Roses are $color

例子 5
  简化语法:

$color = "red";
?>

Roses are

  

Example

Example 1
$str = "Who's John Adams?"; echo $str;
echo "
";
echo $str."
I don't know!";

?>
 代码如下 复制代码
$a = print("55nav"); // 这个是允许的
echo $a; // $a的值是1
?>

Output: Who's John Adam?
Who's John Adam?
I don't know!

Example 2

代码如下 复制代码

$a="55nav";
$c = print_r($a);
echo $c; // $c的值是TRUE
$c = print_r($a, ture);
echo $c; // $c的值是字符串55nav
?>

echo "This text spans multiple lines."; ?> Output: This text spans multiple lines. Example 3 echo 'This ','string ','was ','made ','with multiple parameters';<🎜> ?> Output: This string was made with multiple parameters Example 4 The difference between single quotes and double quotes. Single quotes only output the variable name, not the value: "; echo 'Roses are $color';?> Output: Roses are red Roses are $color Example 5 Simplified syntax: $color = "red";<🎜> ?>

Roses are

  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:
The code is as follows Copy code
$a = print("55nav"); // 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:
The code is as follows Copy code
$a="55nav";<🎜> $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 55nav <🎜> ?>

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:

The code is as follows Copy code
 代码如下 复制代码

printf("My name is %s %s。","55nav", "com"); // My name is 55nav com。
printf("My name is %1$s %1$s","55nav", "com"); // 在s前添加1$或2$.....表示后面的参数显示的位置,此行输出 My name is Ricky Ricky因为只显示第一个参数两次。
printf("My name is %2$s %1$s","55nav", "com"); // My name is com 55nav
?>

printf("My name is %s %s.","55nav", "com"); // My name is 55nav com. printf("My name is %1$s %1$s","55nav", "com"); // 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","55nav", "com"); // My name is com 55nav
?>

5. function/43020.htm target=_blank >sprintf function

The parameter format is the conversion format, starting with the percent sign ("%") and ending with the conversion character. Possible format values ​​below:

%% - Returns the percent symbol

 %b - binary number

%c - character

according to ASCII value

 %d - signed decimal number

 %e - scientific notation (e.g. 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 letters)

arg1, arg2, ++ and other parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2, and so on.

 代码如下 复制代码

例子
例子 1
$str = "Hello";
$number = 123;
$txt = sprintf("%s world. Day number %u",$str,$number);
echo $txt;
?>

输出:

Hello world. Day number 123

例子 2
$number = 123;
$txt = sprintf("%f",$number);
echo $txt;
?>

输出:

123.000000

例子 3
$number = 123;
$txt = sprintf("With 2 decimals: %1$.2f
With no decimals: %1$u",$number);
echo $txt;
?>

输出:

With 2 decimals: 123.00 With no decimals: 123

Tips and Notes Note: If there are more % symbols than arg parameters, you must use placeholders. The placeholder is inserted after the % symbol and consists of a number and "$". See example 3. Tip: Related functions: fprintf(), printf(), vfprintf(), vprintf() and vsprintf().
The code is as follows Copy code
Example Example 1 $str = "Hello";<🎜> $number = 123;<🎜> $txt = sprintf("%s world. Day number %u",$str,$number);<🎜> echo $txt;<🎜> ?> Output: Hello world. Day number 123 Example 2 $number = 123;<🎜> $txt = sprintf("%f",$number);<🎜> echo $txt;<🎜> ?> Output: 123.000000 Example 3 With no decimals: %1$u",$number); echo $txt; ?> Output: With 2 decimals: 123.00 With no decimals: 123

PHP String Function


6. var_dump function
var_dump  (PHP 3 >= 3.0.5, PHP 4, PHP 5)  

var_dump -- Print information about variables

void var_dump ( mixed expression [, mixed expression [, ...]] )

This function displays structural information about one or more expressions, including the type and value of the expression. Arrays will expand values ​​recursively, showing their structure through indentation. ​

Tip: To prevent the program from outputting the results directly to the browser, you can use output-control functions to capture the output of this function and save them to a variable of type string, for example. ​


You can compare var_dump() and print_r().


Example

The code is as follows
 代码如下 复制代码


  

</p>
<p>  <?php</p>
<p>  $a = array (1, 2, array ("a", "b", "c"));</p>
<p>  var_dump ($a);</p>
<p>  /* 输出:</p>
<p>  array(3) {</p>
<p>  [0]=></p>
<p>  int(1)</p>
<p>  [1]=></p>
<p>  int(2)</p>
<p>  [2]=></p>
<p>  array(3) {</p>
<p>  [0]=></p>
<p>  string(1) "a"</p>
<p>  [1]=></p>
<p>  string(1) "b"</p>
<p>  [2]=></p>
<p>  string(1) "c"</p>
<p>  }</p>
<p>  }</p>
<p>  */</p>
<p>  $b = 3.1;</p>
<p>  $c = TRUE;</p>
<p>  var_dump($b,$c);</p>
<p>  /* 输出:</p>
<p>  float(3.1)</p>
<p>  bool(true)</p>
<p>  */</p>
<p>  ?></p>
<p>  
 

Copy code
<p align="left">
</p>
<div style="display:none;"> <?php<span id="url" itemprop="url">
</span> $a = array (1, 2, array ("a", "b", "c"));<span id="indexUrl" itemprop="indexUrl">
</span> var_dump ($a);<span id="isOriginal" itemprop="isOriginal">
</span> /* Output: <span id="isBasedOnUrl" itemprop="isBasedOnUrl">
</span> array(3) {<span id="genre" itemprop="genre">
</span> [0]=><span id="description" itemprop="description">
</span>int(1)</div>
 [1]=>
<div class="art_confoot">int(2)</div>
 [2]=>
 array(3) {
 [0]=>
 String(1) "a"
 [1]=>
 String(1) "b"
 [2]=>
 String(1) "c"
 }
 }
 */
 $b = 3.1;
 $c = TRUE;
 var_dump($b,$c);
 /* Output: 
 float(3.1)
bool(true)
 */
 ?>
 
http://www.bkjia.com/PHPjc/629092.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629092.htmlTechArticleThe article introduces echo, print, print_r, printf, sprintf, var_dump. Friends who need to know more can refer to it. 1. Echo definition and usage PHP echo() function outputs one or more strings...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template