PHP函数补完计划:print()函数
2010-08-25
本文的主角是print()函数,先介绍这个函数的一些基本情况,然后再挖掘其用法,以后有时间会补上它的C实现。
print()函数的作用
print() 函数输出一个或多个字符串。
语法:print(strings)。参数strings必需。作用是发送到输出的一个或多个字符串。
print() 函数实际上不是函数,所以您不必对它使用括号。print() 是一个语言结构。
print() 函数稍慢于 echo()。
Program List:打印一个字符串
<?php print "Welcome to NowaMagic!"; ?>
程序输出:
Welcome to NowaMagic!
Program List:打印变量
<?php $nm = "NowaMagic"; print "Welcome to $nm! "; ?>
程序输出:
Welcome to nowamagic!
Program List:打印数组变量
注意数组外面的大括号不要忘记。
<?php
$nm = array("value" => "nowamagic");
print "Welcome to {$nm['value']}! ";
?>
程序输出:
Welcome to nowamagic!
Program List:取得字符串中的某个字符
字符串可以视为一个字符数组,下面的程序就利用了这一点:
<?php $nm = "NowaMagic"; print $nm[4]; ?>
程序输出:
M
官方的示例程序
<?php
print("Hello World");
print "print() also works without parentheses.";
print "This spans
multiple lines. The newlines will be
output as well";
print "This spans\nmultiple lines. The newlines will be\noutput as well.";
print "escaping characters is done \"Like this\".";
// You can use variables inside of a print statement
$foo = "foobar";
$bar = "barbaz";
print "foo is $foo"; // foo is foobar
// You can also use arrays
$bar = array("value" => "foo");
print "this is {$bar['value']} !"; // this is foo !
// Using single quotes will print the variable name, not the value
print 'foo is $foo'; // foo is $foo
// If you are not using any other characters, you can just print variables
print $foo; // foobar
print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;
?>

