1
0
mirror of https://github.com/php/doc-ja.git synced 2026-03-24 15:12:22 +01:00
Files
Yoshinari Takaoka 4773312462 [ext/strings] followed all updates made on or after November 1, 2024.
- WASM の example 修正
- number_format まわりの誤訳
- その他細かい修正全てに追随

https://github.com/php/doc-en/commits/master/reference/strings
2025-08-23 15:55:59 +09:00

224 lines
6.5 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 45042fef652f1b4e904e809fcbfcf31f6c60670b Maintainer: hirokawa Status: ready -->
<!-- CREDITS: shimooka,mumumu -->
<refentry xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://docbook.org/ns/docbook" xml:id="function.print">
<refnamediv>
<refname>print</refname>
<refpurpose>文字列を出力する</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>print</methodname>
<methodparam><type>string</type><parameter>expression</parameter></methodparam>
</methodsynopsis>
<para>
<parameter>expression</parameter> を出力します。
</para>
<para>
<literal>print</literal>は実際には関数ではなく、
言語構造です。
引数は、<literal>print</literal> の後に文字列として評価される式を指定し、括弧で括る必要はありません。
</para>
<para>
<literal>echo</literal> との主な違いは、
<literal>print</literal> が単一の引数のみ受け付け、常に <literal>1</literal> を返すことです。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>expression</parameter></term>
<listitem>
<para>
出力する文字列として評価される式。
文字列として評価できない値は、
強制的に文字列に変換されます。
これは、
<link linkend="language.types.declarations.strict">
<literal>strict_types</literal> ディレクティブ</link> が有効になっていても同じです。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
常に <literal>1</literal> を返します。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><literal>print</literal> の例</title>
<programlisting role="php">
<![CDATA[
<?php
print "print に括弧は不要です";
print PHP_EOL;
// 改行やスペースは付加されません; 以下は、"helloworld" を一行で出力します
print "hello";
print "world";
print PHP_EOL;
print "This string spans
multiple lines. The newlines will be
output as well";
print PHP_EOL;
print "This string spans\nmultiple lines. The newlines will be\noutput as well.";
print PHP_EOL;
// 引数は文字列を生成するあらゆる式を指定することができます。
$foo = "example";
print "foo is $foo"; // foo is example
print PHP_EOL;
$fruits = ["lemon", "orange", "banana"];
print implode(" and ", $fruits); // lemon and orange and banana
print PHP_EOL;
// 文字列でない値は、文字列に強制されます。たとえ declare(strict_types=1) が使われていても同じです。
print 6 * 7; // 42
print PHP_EOL;
// print は戻り値があるため、式の中で使うことができます。
// 以下は "hello world" を出力します。
if ( print "hello" ) {
echo " world";
}
print PHP_EOL;
// 以下は "true" を出力します。
( 1 === 1 ) ? print 'true' : print 'false';
print PHP_EOL;
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<title>括弧を使う</title>
<para>
<literal>print</literal>の引数を括弧で囲んで渡しても文法エラーにはなりませんが、
通常の関数コールのような文法に見えてしまいます。
しかし、これは誤解を招く恐れがあります。
なぜなら、括弧は実際には出力を構成する式の一部であり、
<literal>print</literal> の文法の一部ではないからです。
<informalexample>
<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>
</informalexample>
</para>
<para>
<literal>print</literal> を大きな式で使う場合、
意図した結果を得るためには、キーワードと引数を括弧で囲む必要があるかもしれません:
<informalexample>
<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>
</informalexample>
</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">文字列リテラルの指定方法</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
-->