PHP 8.3.4 Released!

print

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

printMostrar una cadena

Descripción

print(string $arg): int

Muestra el parámetro arg.

print no es realmente una función (es un constructor de lenguaje) por lo tanto no es necesario usar paréntesis para indicar su lista de argumentos.

Parámetros

arg

Los datos de entrada.

Valores devueltos

Siempre devuelve 1.

Ejemplos

Ejemplo #1 Ejemplos de print

<?php
print("Hola mundo");

print
"print() también funciona sin paréntesis.";

print
"Esto separa
múltiples líneas. Los saltos de línea también
se mostrarán"
;

print
"Esto separa\nmúltiples líneas. Los salos de línea también\nse mostrarán.";

print
"para escapar caracteres se \"hace así\".";

// También se puede usar variables usando print
$foo = "foobar";
$bar = "barbaz";

print
"foo es $foo"; // foo es foobar

// También se pueden usar arrays
$bar = array("value" => "foo");

print
"Esto es {$bar['value']} !"; // Esto es foo !

// Al usar comillas simples se mostrará el nombre de la variable, no su valor
print 'foo is $foo'; // foo is $foo

// Si no se necesita mostrar otros caracteres, se puede simplemente mostrar variables
print $foo; // foobar

print <<<END
Este párrafo utiliza la sintaxis "here document" para mostrar
múltiples líneas con la interpolación de
$variable. Nótese
que el terminador de here document debe aparecer al final
de la línea con punto y coma y ¡ningún espacio en blanco extra!
END;
?>

Notas

Nota: Puesto que esto es una construcción del lenguaje y no una función, no puede ser llamada usando funciones variables.

Ver también

add a note

User Contributed Notes 3 notes

up
32
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
12
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
2
mark at manngo dot net
6 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