PHP 8.3.4 Released!

print

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

printVisualizza una stringa

Descrizione

print(string $arg): int

Visualizza arg.

In realtà print non è una funzione (è un costrutto del linguaggio) pertanto non sono richieste le parentesi per la lista degli argomenti.

La sola differenza rispetto a echo è il fatto che print accetta un elemento singolo.

Elenco dei parametri

arg

I dati in ingresso.

Valori restituiti

Restituisce sempre 1.

Esempi

Example #1 Esempio di uso di print

<?php
print("Ciao Mondo");

print
"print() funziona anche senza parentesi.";

print
"Questo print() si
ripartisce su più linee. Anche i ritorni a capo
sono visualizzati"
;

print
"Questo print() si\nripartisce su più linee. Anche i ritorni a capo\nsono visualizzati.";

print
"l'escape dei caratteri si fa \"in questo modo\".";

// Si possono usare variabili all'interno di istruzioni print
$foo = "foobar";
$bar = "barbaz";

print
"foo è $foo"; // foo è foobar

// Si possono usare le matrici
$bar = array("chiave" => "foo");

print
"questo è {$bar['chiave']} !"; // questo è foo !

// Utilizzando l'apice singolo sarà visualizzato il nome della variabile e non il contenuto
print 'foo è $foo'; // foo è $foo

// Se non si usa altri caratteri, si può semplicemente visualizzare il contenuto della variabile
print $foo; // foobar

print <<<END
Questo esempio usa la sintassi "here document" per visualizzare
linee multiple con interpolazione delle
$variabili. Si noti
che il terminatore deve apparire su una
riga con solo un punto e virgola e senza spazi!
END;
?>

Note

Nota: Poiché questo è un costrutto del linguaggio e non una funzione, non può essere chiamato con le variabili funzione

Vedere anche:

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