php中echo(),print(),print_r()用法
从我对echo(),print(),print_r()这个函数的理解是echo可输入字符串变量常量,print与echo差不多,但print_r可以打印数组而前两者是不可以的,下面我来详细介绍它们三个的用法与区别吧。
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)
print() 只能打印出简单类型变量的值(如int,string)
print_r() 可以打印出复杂类型变量的值(如数组,对象)
echo 输出一个或者多个字符串
echo — 输出一个或者多个字符串
Descrīption
void echo ( string arg1 [, string ...] ) //返回值为空
代码如下 | 复制代码 |
echo "你好","朋友"; |
print –输出一个字符串
Descrīption
int print ( string arg )//返回值为整形
代码如下 | 复制代码 |
print "你好朋友"; |
可以进行下面操作
代码如下 | 复制代码 |
$name=print "nihao n"; |
print_r — 打印关于变量的易于理解的信息。
bool print_r ( mixed expression [, bool return] ) //返回值是布尔型的,参数是mix类型的,可以是字符串,整形,数组,对象类print_r() 显示关于一个变量的易于理解的信息。如果给出的是 string、integer 或 float,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素。object 与数组类似。
print_r() 将把数组的指针移到最后边。
你可以
print_r(str);
print_r(int);
print_r(array);
print_r(obj);
下面我们一个个举例说明一下
四种方法可以输出字符串。 echo
print()
printf()
print_r()
echo
可以一次输出多个值,多个值之间用逗号分隔。echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用。
语法正确: echo "Hello", "World";
语法错误: echo ("Hello", "World");
print()
函数print()打印一个值(它的参数),如果字符串成功显示则返回true,否则返回false。如,
代码如下 | 复制代码 |
if (!print("Hello, World")){ die("you are not listening to me"); } |
printf()
printf()源于C语言中的printf()。该函数输出格式化的字符串。
语法: printf(format,arg1,arg2,arg++)
format 规定字符串以及如何格式化其中的变量;
arg1, arg2, ++ 等参数将插入到主字符串中的百分号 (%) 符号处。该函数是逐步执行的。在第一个 % 符号中,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。
Example: ?php
代码如下 | 复制代码 |
$str = "Hello"; $number = 123; printf("%s world. Day number %u",$str,$number); ?> |
如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入 % 符号之后,由数字和 "$" 组成。请参见例子 3。
Example: ?php
代码如下 | 复制代码 |
$number = 123; printf("With 2 decimals: %1$.2fbr />With no decimals: %1$u",$number); ?> |
print_r()和var_dump()
print_r()可以把字符串和数字简单地打印出来,而数组则以括起来的键和值得列表形式显示,并以Array开头。如,
代码如下 | 复制代码 |
$a = array('name' => 'Fred', 'age' => '15', 'wife' => 'Wilma'); print_r($a); { [name] => Fred [age] => 15 [wife] => Wilma } |
对象也一样。如,
代码如下 | 复制代码 |
class P { var $name = 'nat'; // ... } $p = new P; print_r($p); { [name] => nat } |
但print_r()输出布尔值和NULL的结果没有意义,因为都是打印"n"。因此用var_dump()函数更适合调试

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











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,

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.

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

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.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
