PHP 8.1.20 Released!


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

printMostra uma string


print(string $arg): int

Mostra arg.

print na verdade não é uma função real (ela é uma construção da linguagem), dessa forma você não precisa usar parênteses com sua lista de argumentos.

As principais diferenças para echo são que print aceita apenas um único argumento e sempre retorna 1



O dado de entrada.

Valor Retornado

Retorna 1, sempre.


Exemplo #1 Exemplos print

print("Hello World");

"print() also works without parentheses.";

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

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

"escaping characters is done \"Like this\".";

// You can use variables inside a print statement
$foo = "foobar";
$bar = "barbaz";

"foo is $foo"; // foo is foobar

// You can also use arrays
$bar = array("value" => "foo");

"this is {$bar['value']} !"; // this is foo !

// Using single quotes will print the variable name, not the value
print 'foo is $foo'; // foo is $foo

// If you are not using any other characters, you can just print variables
print $foo; // foobar

print <<<END
This uses the "here document" syntax to output
multiple lines with
$variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!


Nota: Como esta é uma construção da linguagem e não uma função, ela não pode ser chamada usando funções variáveis ou argumentos nomeados.

Veja Também

add a note

User Contributed Notes 2 notes

user at example dot net
14 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:
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


For the expected behavior of the first example, you need to write:
if ((print "foo") && (print "bar")) {
// "foo" and "bar" had been printed
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.

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


Running in a browser:

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

Running in a shell:

<?php println ("Hello, world!"); ?>
Output: Hello, world!\n
To Top