Strings print

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

print — 문자열 출력


설명

print(string $expression): int

expression을 출력합니다.

print는 함수가 아니라 언어 구조입니다. 그 인수는 print 키워드 뒤에 오는 표현식이며 괄호로 구분되지 않습니다.

echo의 주요 차이점은 print는 단일 인수만 허용하고 항상 1을 반환한다는 것입니다.


매개변수

expression
출력할 표현식입니다. strict_types directive이 활성화된 경우에도 문자열이 아닌 값은 문자열로 강제 변환됩니다.

반환 값

항상 1을 반환합니다.


Examples

예제 #1 print 예제

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

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

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

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

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

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

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

// Because print has a return value, it can be used in expressions
// The following outputs "hello world"
if ( print "hello" ) {
    echo " world";
}

// The following outputs "true"
( 1 === 1 ) ? print 'true' : print 'false';
?>
                  
                

메모

참고: 괄호와 함께 사용

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

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

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

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

if ( print("hello") && false ) {
    print " - inside if";
}
else {
    print " - inside else";
}
// outputs " - inside if"
// the expression ("hello") && false is first evaluated, giving false
// this is coerced to the empty string "" and printed
// the print construct then returns 1, so code in the if block is run
?>
                    
                  

더 큰 표현식에서 print를 사용할 때 키워드와 인수를 모두 괄호 안에 넣어야 의도한 결과를 얻을 수 있습니다.

                    
<?php
if ( (print "hello") && false ) {
    print " - inside if";
}
else {
    print " - inside else";
}
// outputs "hello - inside else"
// unlike the previous example, the expression (print "hello") is evaluated first
// after outputting "hello", print returns 1
// since 1 && false is false, code in the else block is run

print "hello " && print "world";
// outputs "world1"; print "world" is evaluated first,
// then the expression "hello " && 1 is passed to the left-hand print

(print "hello ") && (print "world");
// outputs "hello world"; the parentheses force the print expressions
// to be evaluated before the &&
?>
                    
                  

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


기타