


Introduction to the usage of echo, print, print_r, printf, sprintf, var_dump in php_PHP tutorial
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 | ||||||||
Example 1
Output:
Who's John Adam? | |||||||||
代码如下 | 复制代码 | ||||||||
$a="55nav"; |
Roses are =$color?>
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<🎜> ?> |
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 | ||||
?> |
%% - 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.
代码如下 | 复制代码 |
例子 输出: Hello world. Day number 123 例子 2 输出: 123.000000 例子 3 输出: With 2 decimals: 123.00 With no decimals: 123 |
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
|
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...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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,

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

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