echo

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

echo1 つ以上の文字列を出力する

説明

echo(string ...$expressions): void

ひとつ以上の(文字列として評価される)式を出力します。末尾に改行やスペースを付加することはありません。

echo は実際には関数ではなく、言語構造です。 echo キーワードの後に続く引数は、 コンマで区切られた(文字列として評価される)式のリストであり、括弧で括る必要はありません。 他の言語構造と異なり echo には戻り値がありません。そのため、 式のコンテクストでは使うことができません。

echo には、開始タグの直後に等号を付ける短縮構文もあります。 この短縮構文は、設定オプションshort_open_tag が無効な場合でも使えます。

I have <?=$foo?> foo.

print との主な違いは、 echo がリスト形式の引数を受け付け、戻り値を持たないことです。

パラメータ

expressions

出力したいひとつ以上の文字列として評価される式。 コンマで区切ります。 文字列として評価されない値を渡した場合、文字列に強制的に変換されます。 これは、strict_types ディレクティブ が有効になっていても同じです。

戻り値

値を返しません。

例1 echo の例

<?php
echo "echo does not require parentheses.";

// 文字列は、複数の引数として個別に渡すこともできますし、
// 連結し、ひとつの引数として渡すこともできます。
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', "\n";
echo
'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

// 改行やスペースが付加されることはありません; 以下は、"helloworld" を一行で出力します。
echo "hello";
echo
"world";

// 上と同じです。
echo "hello", "world";

echo
"This string spans
multiple lines. The newlines will be
output as well"
;

echo
"This string spans\nmultiple lines. The newlines will be\noutput as well.";

// 引数は文字列を生成するあらゆる式を指定することができます。
$foo = "example";
echo
"foo is $foo"; // foo is example

$fruits = ["lemon", "orange", "banana"];
echo
implode(" and ", $fruits); // lemon and orange and banana

// 文字列でない値は、文字列に強制されます。たとえ declare(strict_types=1) が使われていても同じです。
echo 6 * 7; // 42

// echo は式として振る舞わないので、以下のコードは正しくありません。
($some_var) ? echo 'true' : echo 'false';

// しかし、以下の例は動作します:
($some_var) ? print 'true' : print 'false'; // print も言語構造ですが、
// これは正しい式であり、1を返します。
// よって、式のコンテクストで使うことが出来ます。

echo $some_var ? 'true': 'false'; // 式を最初に評価し、echo に渡します。
?>

注意

注意: これは、関数ではなく 言語構造のため、可変関数名前付き引数 を用いてコールすることはできません。

注意: 括弧を使う

echo に対して引数をひとつだけ括弧で囲んで渡しても文法エラーにはなりませんが、 通常の関数コールのような文法に見えてしまいます。 しかし、これは誤解を招く恐れがあります。 なぜなら、括弧は実際には出力を構成する式の一部であり、 echo の文法の一部ではないからです。

<?php
echo "hello";
// "hello" を出力します。

echo("hello");
// 同じく"hello" を出力します。なぜなら ("hello") は正しい式だからです。

echo(1 + 2) * 3;
// "9" を出力します; 括弧によって 1+2 が先に評価され、3*3がその次に評価されます。
// echo 文は、式の評価結果全体をひとつの引数とみなします。

echo "hello", " world";
// "hello world" を出力します。

echo("hello"), (" world");
// "hello world" を出力します; 括弧は個々の式の一部です。

echo("hello", " world");
// Parse Error をスローします。なぜなら、 ("hello", " world") は正しい式ではないからです。
?>

ヒント

複数の引数をecho に渡すことで、 PHP におけるピリオド演算子の優先順位が高いことによる複雑な挙動を避けることができます。 たとえば、ピリオド演算子は三項演算子より演算子が高くなっています。さらにPHP 8.0.0 より前のバージョンでは、加算および減算演算子と優先順位が同じでした:

<?php
// 以下の例では、'Hello ' . isset($name) という式が先に評価されます。
// そして、常にこれは true と評価されるので、echo への引数は常に $name になります。
echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';

// 上の例を意図した振る舞いにしようとするなら、以下のように追加の括弧が必要です。
echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';

// PHP 8.0.0 より前のバージョンでは、以下は "Sum: 3" ではなく、"2" を出力していました。
echo 'Sum: ' . 1 + 2;

// 繰り返しますが、括弧を追加することで、意図通りの評価順序を保証できます。
echo 'Sum: ' . (1 + 2);

複数の引数を渡した場合、 優先順位を強制するための括弧は不要です。 なぜなら、それぞれの文字列表現は別のものだからです:

<?php
echo "Hello ", isset($name) ? $name : "John Doe", "!";

echo
"Sum: ", 1 + 2;

参考

add a note add a note

User Contributed Notes 3 notes

up
30
pemapmodder1970 at gmail dot com
6 years ago
Passing multiple parameters to echo using commas (',')is not exactly identical to using the concatenation operator ('.'). There are two notable differences.

First, concatenation operators have much higher precedence. Referring to http://php.net/operators.precedence, there are many operators with lower precedence than concatenation, so it is a good idea to use the multi-argument form instead of passing concatenated strings.

<?php
echo "The sum is " . 1 | 2; // output: "2". Parentheses needed.
echo "The sum is ", 1 | 2; // output: "The sum is 3". Fine.
?>

Second, a slightly confusing phenomenon is that unlike passing arguments to functions, the values are evaluated one by one.

<?php
function f($arg){
 
var_dump($arg);
  return
$arg;
}
echo
"Foo" . f("bar") . "Foo";
echo
"\n\n";
echo
"Foo", f("bar"), "Foo";
?>

The output would be:
string(3) "bar"FoobarFoo

Foostring(3) "bar"
barFoo

It would become a confusing bug for a script that uses blocking functions like sleep() as parameters:

<?php
while(true){
  echo
"Loop start!\n", sleep(1);
}
?>

vs

<?php
while(true){
  echo
"Loop started!\n" . sleep(1);
}
?>

With ',' the cursor stops at the beginning every newline, while with '.' the cursor stops after the 0 in the beginning every line (because sleep() returns 0).
up
0
t3tesla at gmail dot com
2 years ago
We can use the 'echo' shortcut syntax with the conditional operator (expr1) ? (expr2) : (expr3)

<?php
$some_var
= 10;
?>
Back to html :
<p class="<?=$some_var>5 ? "class1" : "class2"?>">Some text.</p>

Will give :  <p class="class1">Some text.</p>

<?php
$some_var
= 4;
?>
<p class="<?=$some_var>5 ? "class1" : "class2"?>">Some text.</p>

Will give :  <p class="class2">Some text.</p>
up
-3
mparsa1372 at gmail dot com
3 years ago
The following example shows how to output text with the echo command (notice that the text can contain HTML markup):

<?php
echo "<h2>PHP is Fun!</h2>";
echo
"Hello world!<br>";
echo
"I'm about to learn PHP!<br>";
echo
"This ", "string ", "was ", "made ", "with multiple parameters.";
?>
To Top