Table of Contents
PHP中echo和print的区别,phpechoprint
在php中echo() print() print-r()不同
PHP中echo(),print(),print_r()的不同是什?
Home php教程 php手册 PHP中echo和print的区别,phpechoprint

PHP中echo和print的区别,phpechoprint

Jun 13, 2016 am 09:26 AM
echo php print the difference

PHP中echo和print的区别,phpechoprint

一般来说,PHP中动态输出HTML内容,是通过print 和 echo 语句来实现的,在实际使用中, print 和 echo 两者的功能几乎是完全一样。可以这么说,凡是有一个可以使用的地方,另一个也可以使用。但是,两者之间也还是一个非常重要的区别:在 echo 函数中,可以同时输出多个字符串,而在 print 函数中则只可以同时输出一个字符串。同时,echo函数并不需要圆括号,所以echo函数更像是语句而不像是函数。
echo 和 print 都不是函数,而是语言结构,所以圆括号都不是必需的。
 
他们的区别在于:

(1) echo可以输出多个字符串,像下面这样:

echo 'a','b','c';
Copy after login

如果你非要加上圆括号,注意写成echo ('a','b','c');是错误的,应该写成:

echo ('a'),('b'),('c');
Copy after login

它没有像函数的行为,所以不能用于函数的上下文
(2) print只能输出一个字符串,它可以表现得像一个函数,比如你可以如下使用:

$ret = print 'Hello World';
Copy after login

所有它能用在更复杂的表达式中。
另外,echo的效率相对比较快

看看如下代码:

<&#63;php
$a='hello ';$b='php world!';echo $a,$b.'<br />';//echo 可以用逗号分隔字符串变量来显示
print $a.$b.'<br />';//而print不能使用逗号,只能用点号分隔,
//print $a,$b.'<br />';//这里使用逗号时报错。
&#63;>
Copy after login

分析总结:

echo 命令和print命令相同,没有区别
echo 函数和print函数有区别。
echo() 无返回值,与echo命令相同
print() 有返回值,成功,返1,false,返0.
printf() 和sprintf()类似,均为格式化输出,不同的是前者输出到标准输出,后者输出到变量

形如:

echo  <<< EOT 
EOT; 
print  <<< EOT 
EOT; 

Copy after login

的书写格式,其含义如下:

EOT    自定义分界符,结束时必须位于行首

相信本文所述对大家更好的掌握PHP程序设计有一定的借鉴价值。

在php中echo() print() print-r()不同

echo
可以一次输出多个值,多个值之间用逗号分隔。echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用。
print()
函数print()打印一个值(它的参数),如果字符串成功显示则返回true,否则返回false。
print_r()
可以把字符串和数字简单地打印出来,而数组则以括起来的键和值得列表形式显示,并以Array开头。但print_r()输出布尔值和NULL的结果没有意义,因为都是打印"\n"。因此用var_dump()函数更适合调试。
 

PHP中echo(),print(),print_r()的不同是什?

四种方法可以输出字符串。 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);

?>
#Results======
Hello world. Day number 123

如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入 % 符号之后,由数字和 "\$" 组成。请参见例子 3。
Example: ?php

$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()和var_dump()
print_r()可以把字符串和数字简单地打印出来,而数组则以括起来的键和值得列表形式显示,并以Array开头。如, $a = array('name' => 'Fred', 'age' => '15', 'wife' => 'Wilma');

print_r($a);
Output: Array

{

[name] => Fred

[age] => 15

[wife] => Wilma

}
对象也一样。如, class P {

var $name = 'nat';

// ...

}

$p = new P;

print_r($p);
Output: Object

{

[name] => nat

}
但print_r()输出布尔值和NULL的结果没有意义,因为都是打印&quo......余下全文>>
 

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

Video Face Swap

Video Face Swap

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

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)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles