print

(PHP 4, PHP 5, PHP 7, PHP 8)

print輸出字符串

說明

print(string $arg): int

輸出 arg

print 實(shí)際上不是函數(shù)(而是語言結(jié)構(gòu)),所以可以不用圓括號(hào)包圍參數(shù)列表。

echo 最主要的區(qū)別: print 僅支持一個(gè)參數(shù),并總是返回 1。

參數(shù)

arg

輸入數(shù)據(jù)。

返回值

總是返回 1。

范例

示例 #1 print 范例

<?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\".";

// 可以在打印語句中使用變量
$foo "foobar";
$bar "barbaz";

print 
"foo is $foo"// foo is foobar

// 也可以使用數(shù)組
$bar = array("value" => "foo");

print 
"this is {$bar['value']} !"// this is foo !

// 使用單引號(hào)將打印變量名,而不是變量的值
print 'foo is $foo'// foo is $foo

// 如果沒有使用任何其他字符,可以僅打印變量
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;
?>

注釋

注意: 因?yàn)槭且粋€(gè)語言構(gòu)造器而不是一個(gè)函數(shù),不能被 可變函數(shù) 調(diào)用。

參見