PHP print类似PHP echo语句,但是PHP print是一个语言结构(因为有返回值,可以认为它是函数),所以也不需要使用括号和参数列表。与echo不同,它总是返回1。
PHP print的语法如下:
int print(string $arg)
PHP print语句可以用来打印字符串,多行字符串,转义字符,变量,数组等。
PHP print:打印字符串
File:print1.php
<?php
print "Hello by PHP print ";
print ("Hello by PHP print()");
?>
执行上述代码输出以下结果:
Hello by PHP print Hello by PHP print()
PHP print:打印多行字符串
File:print2.php
<?php
Print " Hello by PHP print
this is multi line
text printed by
PHP print statement
";
?>
执行上述代码输出以下结果:
File:print3.php
Hello by PHP print this is multi line text printed by PHP print statement
PHP print:打印转义字符
File: print3.php
<?php
print "Hello escape \"sequence\" characters by PHP print";
?>
执行以上代码输出以下结果:
Hello escape "equence" characters by PHP print
PHP print :打印变量值
File:print4.php
<?php
$msg = "Hello print() in PHP";
print " Message is: $msg";
执行以上代码输出以下结果:
Message is: Hello print() in PHP