php-doc-en/reference/strings/functions/print.xml
2021-04-20 13:39:32 +08:00

217 lines
5.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://docbook.org/ns/docbook" xml:id="function.print">
<refnamediv>
<refname>print</refname>
<refpurpose>Output a string</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>print</methodname>
<methodparam><type>string</type><parameter>expression</parameter></methodparam>
</methodsynopsis>
<para>
Outputs <parameter>expression</parameter>.
</para>
<para>
<literal>print</literal> is not a function but a language construct.
Its argument is the expression following the <literal>print</literal> keyword,
and is not delimited by parentheses.
</para>
<para>
The major differences to <function>echo</function> are that
<literal>print</literal> only accepts a single argument and always returns
<literal>1</literal>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>expression</parameter></term>
<listitem>
<para>
The expression to be output. Non-string values will be coerced to strings,
even when <link linkend="language.types.declarations.strict">the
<literal>strict_types</literal> directive</link> is enabled.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns <literal>1</literal>, always.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><literal>print</literal> examples</title>
<programlisting role="php">
<![CDATA[
<?php
print "print does not require parentheses.";
// No newline or space is added; the below outputs "helloworld" all on one line
print "hello";
print "world";
print "This string spans
multiple lines. The newlines will be
output as well";
print "This string spans\nmultiple lines. The newlines will be\noutput as well.";
// The argument can be any expression which produces a string
$foo = "example";
print "foo is $foo"; // foo is example
$fruits = ["lemon", "orange", "banana"];
print implode(" and ", $fruits); // lemon and orange and banana
// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
print 6 * 7; // 42
// Because print has a return value, it can be used in expressions
// The following outputs "hello world"
if ( print "hello" ) {
echo " world";
}
// The following outputs "true"
( 1 === 1 ) ? print 'true' : print 'false';
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<title>Using with parentheses</title>
<para>
Surrounding the argument to <literal>print</literal> with parentheses will not
raise a syntax error, and produces syntax which looks like a normal
function call. However, this can be misleading, because the parentheses are actually
part of the expression being output, not part of the <literal>print</literal>
syntax itself.
</para>
<para>
<example>
<title/>
<programlisting role="php">
<![CDATA[
<?php
print "hello";
// outputs "hello"
print("hello");
// also outputs "hello", because ("hello") is a valid expression
print(1 + 2) * 3;
// outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3
// the print statement sees the whole expression as one argument
if ( print("hello") && false ) {
print " - inside if";
}
else {
print " - inside else";
}
// outputs " - inside if"
// the expression ("hello") && false is first evaluated, giving false
// this is coerced to the empty string "" and printed
// the print construct then returns 1, so code in the if block is run
?>
]]>
</programlisting>
</example>
</para>
<para>
When using <literal>print</literal> in a larger expression, placing both the
keyword and its argument in parentheses may be necessary to give the intended
result:
</para>
<para>
<example>
<title/>
<programlisting role="php">
<![CDATA[
<?php
if ( (print "hello") && false ) {
print " - inside if";
}
else {
print " - inside else";
}
// outputs "hello - inside else"
// unlike the previous example, the expression (print "hello") is evaluated first
// after outputting "hello", print returns 1
// since 1 && false is false, code in the else block is run
print "hello " && print "world";
// outputs "world1"; print "world" is evaluated first,
// then the expression "hello " && 1 is passed to the left-hand print
(print "hello ") && (print "world");
// outputs "hello world"; the parentheses force the print expressions
// to be evaluated before the &&
?>
]]>
</programlisting>
</example>
</para>
</note>
&note.language-construct;
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>echo</function></member>
<member><function>printf</function></member>
<member><function>flush</function></member>
<member><link linkend="language.types.string">Ways to specify literal strings</link></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->