PHP 8.3.4 Released!

echo

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

echoVisualizza una o più stringhe

Descrizione

echo(string $arg1, string $... = ?): void

Visualizza tutti i parametri.

echo in realtà non è una funzione (è un costrutto del linguaggio) pertanto non richiede l'uso di parentesi. echo (diversamente da altri costrutti del linguaggio)) non si comporta come una funzion, quindi non può essere sempre usata nel constesto di una funzione. Inoltre,se si vuole passare più di un parametro a echo, i parametri non devono essere racchiusi tra parentesi.

echo ha anche una sintassi abbreviata, nella quale si può immediatamente seguire il simbolo di apertura del tag con un simbolo di uguale. questa sintassi abbreviata funziona solo se la configurazione short_open_tag è abilitata.

Numero di foo: <?=$foo?>.

Elenco dei parametri

arg1

Il parametro da visualizzare.

...

Valori restituiti

Nessun valore viene restituito.

Esempi

Example #1 Esempi della funzione echo

<?php
echo "Hello World";

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

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

echo
"Escaping characters is done \"Like this\".";

// Si possono utilizzare variabili all'interno dei parametri di echo
$foo = "foobar";
$bar = "barbaz";

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

// Si possono utilizzare anche delle matrici
$baz = array("value" => "foo");

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

// Utilizzando gli apici singoli viene visualizzato il nome della variabile, non il valore
echo 'foo is $foo'; // foo is $foo

// Se non vi sono altri caratteri, si può visualizzare soltanto il contenuto delle variabili
echo $foo; // foobar
echo $foo,$bar; // foobarbarbaz

// Alcuni programmatori preferiscono passare i parametri come sequenza di stringhe concatenate.
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
echo
'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

echo <<<END
Questo esempio utilizza la sintassi "here document"
per visualizzare più linee oltre al contenuto di
$variable
Notare che il terminatore del testo richiede
anche il punto e virgola, senza alcun spazio aggiuntivo!
END;

// Poiché echo non è una funzione la seguente riga non è valida.
($some_var) ? echo 'true' : echo 'false';

// Tuttavia la seguente funziona
($some_var) ? print 'true' : print 'false'; // print è un costrutto, ma
// si comporta come una funzione, quindi
// può essere utilizzato in questo contesto.
echo $some_var ? 'true': 'false'; // altra versione dell'istruzione
?>

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
34
pemapmodder1970 at gmail dot com
6 years ago
Passing multiple parameters to echo using commas (',')is not exactly identical to using the concatenation operator ('.'). There are two notable differences.

First, concatenation operators have much higher precedence. Referring to http://php.net/operators.precedence, there are many operators with lower precedence than concatenation, so it is a good idea to use the multi-argument form instead of passing concatenated strings.

<?php
echo "The sum is " . 1 | 2; // output: "2". Parentheses needed.
echo "The sum is ", 1 | 2; // output: "The sum is 3". Fine.
?>

Second, a slightly confusing phenomenon is that unlike passing arguments to functions, the values are evaluated one by one.

<?php
function f($arg){
var_dump($arg);
return
$arg;
}
echo
"Foo" . f("bar") . "Foo";
echo
"\n\n";
echo
"Foo", f("bar"), "Foo";
?>

The output would be:
string(3) "bar"FoobarFoo

Foostring(3) "bar"
barFoo

It would become a confusing bug for a script that uses blocking functions like sleep() as parameters:

<?php
while(true){
echo
"Loop start!\n", sleep(1);
}
?>

vs

<?php
while(true){
echo
"Loop started!\n" . sleep(1);
}
?>

With ',' the cursor stops at the beginning every newline, while with '.' the cursor stops after the 0 in the beginning every line (because sleep() returns 0).
up
-7
t3tesla at gmail dot com
2 years ago
We can use the 'echo' shortcut syntax with the conditional operator (expr1) ? (expr2) : (expr3)

<?php
$some_var
= 10;
?>
Back to html :
<p class="<?=$some_var>5 ? "class1" : "class2"?>">Some text.</p>

Will give : <p class="class1">Some text.</p>

<?php
$some_var
= 4;
?>
<p class="<?=$some_var>5 ? "class1" : "class2"?>">Some text.</p>

Will give : <p class="class2">Some text.</p>
up
-10
retrobytespr at mail dot com
2 years ago
If you have a large block of text, say your blog or something includes code examples, you may use the <<< operator (?) to define the start and end of your block to be echoed out. For instance:

<?php
echo <<< JAVASCRIPT

function convertTroyOuncesToGrams(troyOunce) {
return troyOunce / 31.1034768;
}

JAVASCRIPT; # End of block

?>

You may also embed PHP strings and other simple scalars into your blocks of text, for example:

<?php
$troyOunceAsGrams
= 31.1034768;

echo <<< JAVASCRIPT

function convertTroyOuncesToGrams(troyOunce) {
return troyOunce /
{$troyOunceAsGrams};
}

JAVASCRIPT;
To Top