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

Jul 13, 2016 pm 05:13 PM
dump echo php print printf var introduce article usage

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles