PHP Velho Oeste 2024

print

(PHP 4, PHP 5, PHP 7)

print문자열을 출력

설명

int print ( string $arg )

arg를 출력합니다.

print는 실제 함수가 아닙니다. (언어 구조입니다) 그러므로, 인수 목록에 괄호를 사용할 필요가 없습니다.

printecho의 차이에 관한 간단한 설명은 FAQT의 Knowledge Base Article에서 볼 수 있습니다:

인수

arg

입력 데이터.

반환값

항상 1을 반환합니다.

예제

Example #1 print 예제

<?php
print("Hello World");

print 
"print()는 괄호 없이도 작동합니다.";

print 
"여러 줄로
사용할 수 있습니다. 줄바꿈 문자도
제대로 출력합니다"
;

print 
"여러 줄로\n사용할 수 있습니다. 줄바꿈 문자도\n제대로 출력합니다.";

print 
"이스케이핑 문자는 \"이렇게\" 합니다.";

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

print 
"foo는 $foo"// foo는 foobar

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

print 
"이것은 {$bar['value']} !"// 이것은 foo !

// 변수값이 아닌, 변수명을 출력하려면 작은 따옴표를 사용하십시오.
print 'foo는 $foo'// foo는 $foo

// 다른 문자을 사용하지 않는다면, 변수만 출력할 수 있습니다.
print $foo;          // foobar

print <<<END
이는 $varialbe을 써넣으면서 여러 줄을 출력하는
"here document"(여기는 문서) 구문의 사용입니다.
문서 종료어는 아무런 공백도 없이 단지 세미콜론
만을 가지는 점에 주의하십시오.
END;
?>

주의

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

참고

  • echo - 하나 이상의 문자열을 출력
  • print_r() - 변수에 관한 정보를 사람이 읽기 편하게 출력
  • flush() - 출력 버퍼를 비웁니다

add a note add a note

User Contributed Notes 3 notes

up
31
user at example dot net
15 years ago
Be careful when using print. Since print is a language construct and not a function, the parentheses around the argument is not required.
In fact, using parentheses can cause confusion with the syntax of a function and SHOULD be omited.

Most would expect the following behavior:
<?php
   
if (print("foo") && print("bar")) {
       
// "foo" and "bar" had been printed
   
}
?>

But since the parenthesis around the argument are not required, they are interpretet as part of the argument.
This means that the argument of the first print is

    ("foo") && print("bar")

and the argument of the second print is just

    ("bar")

For the expected behavior of the first example, you need to write:
<?php
   
if ((print "foo") && (print "bar")) {
       
// "foo" and "bar" had been printed
   
}
?>
up
11
danielxmorris @ gmail dotcom
15 years ago
I wrote a println function that determines whether a \n or a <br /> should be appended to the line depending on whether it's being executed in a shell or a browser window.  People have probably thought of this before but I thought I'd post it anyway - it may help a couple of people.

<?php
function println ($string_message) {
   
$_SERVER['SERVER_PROTOCOL'] ? print "$string_message<br />" : print "$string_message\n";
}
?>

Examples:

Running in a browser:

<?php println ("Hello, world!"); ?>
Output: Hello, world!<br />

Running in a shell:

<?php println ("Hello, world!"); ?>
Output: Hello, world!\n
up
1
mark at manngo dot net
7 months ago
The other major difference with echo is that print returns a value, even it’s always 1.

That might not look like much, but you can use print in another expression. Here are some examples:

<?php
    rand
(0,1) ? print 'Hello' : print 'goodbye';
    print
PHP_EOL;
    print
'Hello ' and print 'goodbye';
    print
PHP_EOL;
   
rand(0,1) or print 'whatever';
?>

Here’s a more serious example:

<?php
   
function test() {
        return !!
rand(0,1);
    }
   
test() or print 'failed';   
?>
To Top