print 文字列を出力する &reftitle.description; intprint stringexpression expression を出力します。 printは実際には関数ではなく、 言語構造です。 引数は、print の後に文字列として評価される式を指定し、括弧で括る必要はありません。 echo との主な違いは、 print が単一の引数のみ受け付け、常に 1 を返すことです。 &reftitle.parameters; expression 出力する文字列として評価される式。 文字列として評価できない値は、 強制的に文字列に変換されます。 これは、 strict_types ディレクティブ が有効になっていても同じです。 &reftitle.returnvalues; 常に 1 を返します。 &reftitle.examples; <literal>print</literal> の例 ]]> &reftitle.notes; 括弧を使う printの引数を括弧で囲んで渡しても文法エラーにはなりませんが、 通常の関数コールのような文法に見えてしまいます。 しかし、これは誤解を招く恐れがあります。 なぜなら、括弧は実際には出力を構成する式の一部であり、 print の文法の一部ではないからです。 <programlisting role="php"> <![CDATA[ <?php print "hello"; // "hello" を出力します。 print("hello"); // 同じく"hello" を出力します。なぜなら ("hello") は正しい式だからです。 print(1 + 2) * 3; // "9" を出力します; 括弧によって 1+2 が先に評価され、3*3がその次に評価されます。 // print 文は、式の評価結果全体をひとつの引数とみなします。 if ( print("hello") && false ) { print " - inside if"; } else { print " - inside else"; } // " - inside if" を出力します。 // ("hello") && false という式が最初に評価され、 // 評価された false が空文字列 "" に強制的に変換され、print に渡され、1を返します。 // よって、if ブロックの内部のコードが実行されます。 ?> ]]> </programlisting> </example> </para> <para> <literal>print</literal> を大きな式で使う場合、 意図した結果を得るためには、キーワードと引数を括弧で囲む必要があるかもしれません: </para> <para> <example> <title/> <programlisting role="php"> <![CDATA[ <?php if ( (print "hello") && false ) { print " - inside if"; } else { print " - inside else"; } // "hello - inside else" を出力します。 // 直前の例と異なり、(print "hello") が最初に評価され、 // "hello" を出力した後、print が1を返します。 // 1 && false は false なので、else ブロックの中身が実行されます。 print "hello " && print "world"; // "world1"; を出力します。print "world" が先に評価され、 // "hello " && 1 が次に評価され、左辺の print に渡されます。 (print "hello ") && (print "world"); // "hello world" を出力します; 括弧が print を && より前に評価させているからです。 ?> ]]> </programlisting> </example> </para> </note> ¬e.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">文字列リテラルの指定方法</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 -->