Strings echo

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

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


설명

echo(string ...$expressions): void

추가 줄 바꿈이나 공백 없이 하나 이상의 표현식을 출력합니다.

echo는 함수가 아니라 언어 구조입니다. 인수는 쉼표로 구분되고 괄호로 구분되지 않는 echo 키워드 뒤에 오는 표현식 목록입니다. 다른 언어 구성과 달리 echo는 반환 값이 없으므로 표현식의 컨텍스트에서 사용할 수 없습니다.

echo는 또한 등호로 여는 태그 바로 뒤에 올 수 있는 바로 가기 구문을 가지고 있습니다. 이 구문은 short_open_tag 구성 설정이 비활성화된 경우에도 사용할 수 있습니다.

I have <?=$foo?> foo.

print와 주요 차이점은 echo가 여러 인수를 허용하고 반환 값이 없다는 것입니다.


매개변수

expressions
쉼표로 구분하여 출력할 하나 이상의 문자열 표현식입니다. strict_types directive이 활성화된 경우에도 문자열이 아닌 값은 문자열로 강제 변환됩니다.

반환 값

값이 반환되지 않습니다.


Examples

예제 #1 echo 예제

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

// Strings can either be passed individually as multiple arguments or
// concatenated together and passed as a single argument
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', "\n";
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

// No newline or space is added; the below outputs "helloworld" all on one line
echo "hello";
echo "world";

// Same as above
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.";

// The argument can be any expression which produces a string
$foo = "example";
echo "foo is $foo"; // foo is example

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

// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
echo 6 * 7; // 42

// Because echo does not behave as an expression, the following code is invalid.
($some_var) ? echo 'true' : echo 'false';

// However, the following examples will work:
($some_var) ? print 'true' : print 'false'; // print is also a construct, but
                                            // it is a valid expression, returning 1,
                                            // so it may be used in this context.

echo $some_var ? 'true': 'false'; // evaluating the expression first and passing it to echo
?>
                  
                

메모

참고: 이것은 함수가 아니라 언어 구조이기 때문에 변수 함수명명된 인수를 사용하여 호출할 수 없습니다.

참고: 괄호와 함께 사용

echo에 대한 단일 인수를 괄호로 둘러싸면 구문 오류가 발생하지 않으며 일반 함수 호출처럼 보이는 구문이 생성됩니다. 그러나 괄호는 실제로 에코 구문 자체의 일부가 아니라 출력되는 표현식의 일부이기 때문에 오해의 소지가 있습니다.

                    
  <?php
  echo "hello";
  // outputs "hello"

  echo("hello");
  // also outputs "hello", because ("hello") is a valid expression

  echo(1 + 2) * 3;
  // outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3
  // the echo statement sees the whole expression as one argument

  echo "hello", " world";
  // outputs "hello world"

  echo("hello"), (" world");
  // outputs "hello world"; the parentheses are part of each expression

  echo("hello", " world");
  // Throws a Parse Error because ("hello", " world") is not a valid expression
  ?>
                    
                  

echo에 여러 인수를 전달하면 PHP에서 연결 연산자의 우선 순위로 인해 발생하는 복잡한 문제를 피할 수 있습니다. 예를 들어 연결 연산자는 삼항 연산자보다 우선 순위가 높으며 PHP 8.0.0 이전에는 더하기 및 빼기와 동일한 우선 순위를 가졌습니다.

                    
<?php
// Below, the expression 'Hello ' . isset($name) is evaluated first,
// and is always true, so the argument to echo is always $name
echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';

// The intended behaviour requires additional parentheses
echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';

// In PHP prior to 8.0.0, the below outputs "2", rather than "Sum: 3"
echo 'Sum: ' . 1 + 2;

// Again, adding parentheses ensures the intended order of evaluation
echo 'Sum: ' . (1 + 2);
                    
                  

여러 인수가 전달되면 각 표현식이 분리되기 때문에 우선 순위를 적용하기 위해 괄호가 필요하지 않습니다.

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

echo "Sum: ", 1 + 2;
                    
                  

기타