suche nach in der

explode> <crypt
Last updated: Sat, 07 Jan 2012

view this page in

echo

(PHP 4, PHP 5)

echoGibt einen oder mehrere Strings aus

Beschreibung

void echo ( string $arg1 [, string $... ] )

Gibt alle Parameter aus.

echo() ist nicht wirklich eine Funktion sondern ein Sprach-Konstrukt, daher brauchen Sie keine Klammern verwenden. echo() verhält sich im Gegensatz zu einigen anderen Sprach-Konstrukten nicht wie eine Funktion, deshalb kann es nicht immer in einem Funktionskontext verwendet werden. Hinzu kommt, dass bei der Angabe mehrerer Parameter für echo() diese nicht von Klammern umschlossen sein dürfen.

echo() besitzt zusätzlich eine Syntax-Kurzform, Sie können also ein öffnendes PHP-Tag von einem Gleichheitszeichen gefolgt notieren. Diese Syntax-Kurzform funktioniert nur, wenn short_open_tag in der php.ini eingeschaltet ist.

Ich habe <?=$foo?> foo.

Parameter-Liste

arg1

Der auszugebende Parameter.

...

Rückgabewerte

Es wird kein Wert zurückgegeben.

Beispiele

Beispiel #1 echo()-Beispiele

<?php
echo "Hallo Welt";

echo 
"Diese Ausgabe geht über
mehrere Zeilen. Die Zeilenumbrüche werden
ebenfalls ausgegeben."
;

echo 
"Diese Ausgabe geht über\nmehrere Zeilen. Die Zeilenumbrüche werden\nebenfalls ausgegeben.";

echo 
"Escape Zeichen werden \"so realisiert\".";

// Sie können Variablen innerhalb eines echo-Statements verwenden
$foo "foobar";
$bar "barbaz";

echo 
"foo ist $foo"// foo ist foobar

// Sie können auch Arrays nutzen
$bar = array("wert" => "foo");

echo 
"Das ist {$bar['wert']} !"// Das ist foo !

// Wenn Sie einfache Anführungszeichen verwenden, wird der Name der Variable
// anstelle ihres Inhalts ausgegeben
echo 'foo ist $foo'// foo ist $foo

// Sie können auch ausschließlich Variablen ausgeben,
// sofern Sie keine weiteren Zeichen ausgeben wollen
echo $foo;          // foobar
echo $foo,$bar;     // foobarbarbaz

// Einige Programmierer bevorzugen es, mehrere Parameter
// mithilfe von Stringverkettung auszugeben
echo 'Dieser ''String ''besteht ''aus ''mehreren Parametern.'chr(10);
echo 
'Dieser  ' 'String ' 'wurde ' 'mit ' 'Stringverkettung erzeugt.' "\n";

echo <<<END
Hier wird die "here document"-Syntax verwendet, um mehrere
Zeilen mit 
$variablen Interpolation auszugeben. Beachten Sie,
dass das sich das "here document"-Endzeichen in einer Zeile
mit nur einem Strichpunkt aber ohne Leerzeichen o.ä. stehen muss!
END;

// Da echo sich nicht wie eine Funktion verhält, ist der folgende Code ungültig.
($eine_variable) ? echo 'true' : echo 'false';

// Folgende Beispiele funktionieren hingegen:
($eine_variable) ? print 'true': print 'false' ;   // print ist ebenfalls ein
                         // Konstrukt, aber es verhält sich wie eine Funktion,
                         // so dass es in diesem Kontext verwendet werden kann
echo $eine_variable 'true''false';            // Das Statement herumgedreht
?>

Anmerkungen

Hinweis: Da dies ein Sprachkonstrukt und keine Funktion ist, können Sie dieses nicht mit Variablenfunktionen verwenden.

Siehe auch



add a note add a note User Contributed Notes
echo
michaelmcandrew at gmail dot com
08-Jul-2007 01:53
Sean Middleditch
22-May-2007 01:37
The reason is operator precedence.  The . and + operators apparently have equal precedence.  So the following line:

"begin" . $var + 1 . "end"

is parsed to the equivalent of:

((("begin" . $var) + 1) . "$end")

The + 1 is being applied to the string "begin".$var, which is treated as 0 because it doesn't start with any digits, so the end result should be "1end".

It's not a bug, just a minor misdesign (at worst).
superwebdeveloper at gmail dot com
25-Apr-2007 08:39
I noticed something new with echo:

echo "You are viewing #" . $itemValue + 1 . " of a total of $announcementsCount announcements. <br />";

causes "You are viewing #"  to not appear in the output.

putting parenthesis around the math resolves the issue.

echo "You are viewing #" . ($itemValue + 1) . " of a total of $announcementsCount announcements. <br />";

this may not be so much a bug as something that happens for a darn good reason. Im just not sure of the reason.
Jim Walker
13-Jan-2007 08:24
Putting the constant value __LINE__ in debugging messages can save time locating the message line in your source. You can use the same message text in more than one location and the message will be unique. For example,

     echo __LINE__.' My Array: '.$k.' = '.$v.'<br>';
Meercat9
14-Dec-2006 05:12
<table align=center>
<?
//Using Backslashes Does not Make the Quotation Marks Not Execute, Only Separates them From the Others.
 
echo "<tr align=center bgcolor=#1A1A1A onMouseOver=\"this.bgColor='#33333';\" onMouseOut=\"this.bgColor='#1A1A1A';\">"
?>
</table>

Which Can Work Similar if You Want to Store Some Java Incorporated HTML in a String Variable.

<?
  $table
= "<table align=\"center\">
  $td = "
<tr align=center bgcolor=#1A1A1A onMouseOver=\"this.bgColor='#33333';\" onMouseOut=\"this.bgColor='#1A1A1A';\">";
 
$td = "<td align=\"center\">";

echo
"<$table \n $tr \n $td \n Hello </td>\n</tr>\n</table>"
?>
rwruck
27-Jan-2006 10:15
It should be mentioned here that the script will be aborted if you output something in response to a HTTP HEAD request.
In the following example, the second line will NOT be written to the file. Any registered shutdown function will be called, though:

<?php
$hf
= fopen('head.log', 'ab');
fwrite($hf, "before output\n");
echo
"Test";
fwrite($hf, "after output\n");
fclose($hf);
?>

This is normal behaviour; see the description of $_SERVER.
lorenpr at gmail dot com
15-Sep-2005 06:12
I hope this ternary thing isn't overkill. Last thing - I would just like to make you aware of the side effect when you do this statement,
 
echo $some_var ? print 'true': print 'false';

The result would be either 'true1' or 'false1', depending on whether $some_var is true or false respectively. This happens because of what I mentioned earlier. Figure it out.
lorenpr at gmail dot com
15-Sep-2005 05:58
linus is correct.
The ternary relationship is evaluated first, then echo prints the result.

Also, keep in mind
(condition? a : b)
reads
if(condition) return a; else return b;

So what makes
A. echo ($somevar) ? 'true' : 'false';

different from

B. ($somevar) ? echo 'true' : echo 'false';

is the ternary relationship in A reads
if (condition) return 'true'; else return 'false';

while in B it reads
if(condition) return (echo 'true'); else return (echo 'false');

- an invalid statement, since echo evaluates to void.
linus.martensson a gmail
24-Aug-2005 11:03
Simple. it reads like echo (($somevar)?'true';'false'); and outputs the result, unless I'm mistaken.
shannonmoeller at gmail dot com
10-Aug-2005 01:31
In reply to lorenpr at gmail dot com:

If what you say is true, why does this work?

<?php
echo ($somevar) ? 'true' : 'false'; //works
?>
lorenpr at gmail dot com
28-Jul-2005 07:17
I just want to point out something to beginners. The documentation is misleading where it says:

// Because echo is not a function, following code is invalid.
($some_var) ? echo 'true' : echo 'false';

The code is invalid, but not because 'echo' is a language construct, but rather because 'echo' does not return a value.
So don't be mislead: the syntax used above is certainly not limited to functions.

You must keep in mind that the job of the ternary syntax used is not actually to display anything, but to test a boolean relationship. The 'print' statement would work because it always returns a 1, which in php, is interpreted to a boolean  'true'. Things that return 'void' cannot be expected to evaluate to a 'true' or 'false', and that is why using 'echo' in this particular case is invalid.
Jason Carlson - SiteSanity
16-May-2005 07:28
In response to Ryan's post with his echobig() function, using str_split wastes memory resources for what you are doing.

If all you want to do is echo smaller chunks of a large string, I found the following code to perform better and it will work in PHP versions 3+

<?php
function echobig($string, $bufferSize = 8192)
{
 
// suggest doing a test for Integer & positive bufferSize
 
for ($chars=strlen($string)-1,$start=0;$start <= $chars;$start += $bufferSize) {
    echo
substr($string,$start,$buffer_size);
  }
}
?>
renrutal at gmail dot com
29-Mar-2005 01:34
Note that:

<?php
echo "2 + 2 = " . 2+2; // This will print 4
echo "2 + 2 = " , 2+2; // This will print 2+2 = 4
?>

The commas will parse the result of the expressions correctly.
ryan at wonko dot com
27-Feb-2005 09:56
Due to the way TCP/IP packets are buffered, using echo to send large strings to the client may cause a severe performance hit. Sometimes it can add as much as an entire second to the processing time of the script. This even happens when output buffering is used.

If you need to echo a large string, break it into smaller chunks first and then echo each chunk. The following function will do the trick in PHP5:

<?php
function echobig($string, $bufferSize = 8192)
{
   
$splitString = str_split($string, $bufferSize);

    foreach(
$splitString as $chunk)
        echo
$chunk;
}
?>
Truffy
15-Jan-2005 10:02
You can use braces around variables as well as array items. This is useful to help recognition of your variables in your code, but most useful where the variable iteslf cannot be separated with spaces from the preceding/following code, for exmple in a file path:

If a path is assigned the variable $path, then this code will not work:

echo "$pathindex.php";

whereas this will

echo "{$path}index.php";
zombie)at(localm)dot(org)
25-Jan-2003 08:26
[Ed. Note: During normal execution, the buffer (where echo's arguments go) is not flushed (sent) after each write to the buffer. To do that you'd need to use the flush() function, and even that may not cause the data to be sent, depending on your web server.]

Echo is an i/o process and i/o processes are typically time consuming. For the longest time i have been outputting content by echoing as i get the data to output. Therefore i might have hundreds of echoes in my document. Recently, i have switched to concatenating all my string output together and then just doing one echo at the end. This organizes the code more, and i do believe cuts down on a bit of time. Likewise, i benchmark all my pages and echo seems to influence this as well. At the top of the page i get the micro time, and at the end i figure out how long the page took to process. With the old method of "echo as you go" the processing time seemed to be dependent on the user's net connection as well as the servers processing speed. This was probably due to how echo works and the sending of packets of info back and forth to the user. One an one script i was getting .0004 secs on a cable modem, and a friend of mine in on dialup was getting .2 secs. Finally, to test that echo is slow; I built strings of XML and XSLT and used the PHP sablotron functions to do a transformation and return a new string. I then echoed the string. Before the echo, the process time was around .025 seconds and .4 after the echo. So if you are big into getting the actual processing time of your scripts, don't include echoes since they seem to be user dependent. Note that this is just my experience and it could be a fluke.

explode> <crypt
Last updated: Sat, 07 Jan 2012