PHP中echo和print的区别,phpechoprint
一般来说,PHP中动态输出HTML内容,是通过print 和 echo 语句来实现的,在实际使用中, print 和 echo 两者的功能几乎是完全一样。可以这么说,凡是有一个可以使用的地方,另一个也可以使用。但是,两者之间也还是一个非常重要的区别:在 echo 函数中,可以同时输出多个字符串,而在 print 函数中则只可以同时输出一个字符串。同时,echo函数并不需要圆括号,所以echo函数更像是语句而不像是函数。
echo 和 print 都不是函数,而是语言结构,所以圆括号都不是必需的。
他们的区别在于:
(1) echo可以输出多个字符串,像下面这样:
echo 'a','b','c';
如果你非要加上圆括号,注意写成echo ('a','b','c');是错误的,应该写成:
echo ('a'),('b'),('c');
它没有像函数的行为,所以不能用于函数的上下文
(2) print只能输出一个字符串,它可以表现得像一个函数,比如你可以如下使用:
$ret = print 'Hello World';
所有它能用在更复杂的表达式中。
另外,echo的效率相对比较快!
看看如下代码:
<?php $a='hello ';$b='php world!';echo $a,$b.'<br />';//echo 可以用逗号分隔字符串变量来显示 print $a.$b.'<br />';//而print不能使用逗号,只能用点号分隔, //print $a,$b.'<br />';//这里使用逗号时报错。 ?>
分析总结:
echo 命令和print命令相同,没有区别
echo 函数和print函数有区别。
echo() 无返回值,与echo命令相同
print() 有返回值,成功,返1,false,返0.
printf() 和sprintf()类似,均为格式化输出,不同的是前者输出到标准输出,后者输出到变量
形如:
echo <<< EOT EOT; print <<< EOT EOT;
的书写格式,其含义如下:
EOT 自定义分界符,结束时必须位于行首
相信本文所述对大家更好的掌握PHP程序设计有一定的借鉴价值。
echo
可以一次输出多个值,多个值之间用逗号分隔。echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用。
print()
函数print()打印一个值(它的参数),如果字符串成功显示则返回true,否则返回false。
print_r()
可以把字符串和数字简单地打印出来,而数组则以括起来的键和值得列表形式显示,并以Array开头。但print_r()输出布尔值和NULL的结果没有意义,因为都是打印"\n"。因此用var_dump()函数更适合调试。
四种方法可以输出字符串。 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......余下全文>>

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft






