echo

(PHP 4, PHP 5, PHP 7)

echo하나 이상의 문자열을 출력

설명

void echo ( string $arg1 [, string $... ] )

모든 인수를 출력합니다.

echo은 실제 함수가 아니기에 (언어 구조입니다) 괄호를 사용할 필요가 없습니다. echo는 (다른 언어 구조와 달리) 함수처럼 작동하지 않으므로, 함수 문맥으로 사용할 수 없습니다. 추가로, echo에 둘 이상의 인수를 사용할 때 괄호를 사용해서는 안됩니다.

echo는 열기 태그에 이어지는 등호를 사용한 짧은 구문을 가지고 있습니다. 이 짧은 구문은 short_open_tag 설정을 활성화 했을 때만 작동합니다.

I have <?=$foo?> foo.

인수

arg1

출력할 인수.

...

반환값

값을 반환하지 않습니다.

예제

Example #1 echo 예제

<?php
echo "Hello World";

echo 
"이것은 여러
줄을 표현합니다. 물론 줄바꿈도 
출력합니다."
;

echo 
"이것은 여러\n줄을 표현합니다. 물론 줄바꿈도\n출력합니다.";

echo 
"문자 이스케이프는 \"이렇게\" 합니다.";

// echo 구문 안에 변수를 사용할 수 있습니다.
$foo "foobar";
$bar "barbaz";

echo 
"foo는 $foo"// foo는 foobar

// 배열을 사용할 수도 있습니다.
$baz = array("value" => "foo");

echo 
"이것은 {$baz['value']} !"// 이것은 foo !

// 작은 따옴표는 변수값이 아닌, 변수명을 출력합니다.
echo 'foo는 $foo'// foo는 $foo

// 다른 문자를 사용하지 않는다면, 바로 변수를 echo할 수 있습니다.
echo $foo;          // foobar
echo $foo,$bar;     // foobarbarbaz

// 몇몇 사람들은 결합 echo보다 복수 인수 사용을 선호합니다.
echo 'This ''string ''was ''made ''with multiple parameters.'chr(10);
echo 
'This ' 'string ' 'was ' 'made ' 'with concatenation.' "\n";

echo <<<END
이는 $variable 삽입을 가지는 여러 줄을
출력하는 "here document" 구문을 사용합니다. here
document 종료어는 줄에 세미콜론만을 가지고 있어야
하며, 어떠한 공백도 없어야하는 점에 주의하십시오!
END;

// echo는 함수처럼 작동하지 않기에, 다음 코드는 유효하지 않습니다.
($some_var) ? echo 'true' : echo 'false';

// 그러나, 다음 예제는 작동합니다.
($some_var) ? print 'true' : print 'false'// print도 구조이지만, 함수처럼
                                            // 작동합니다. 그러므로
                                            // 이 문맥에서 사용할 수 있습니다.
echo $some_var 'true''false'// 구문을 변경하여 처리
?>

주의

printecho의 차이에 대해서는, FAQT의 Knowledge Base Article을 읽어보십시오:

Note: 이것은 함수가 아닌 언어 구조이기 때문에, 가변 함수 방식으로 호출할 수 없습니다.

참고

  • print - 문자열을 출력
  • printf() - 형식화한 문자열을 출력
  • flush() - 출력 버퍼를 비웁니다

add a note add a note

User Contributed Notes 3 notes

up
30
pemapmodder1970 at gmail dot com
7 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