PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

explode> <crypt
Last updated: Sat, 24 Mar 2007

view this page in

echo

(PHP 4, PHP 5)

echo — Vytisknout jeden nebo více řetězců

Popis

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

Vytiskne všechny parametry.

echo() vlastně není funkce (je to jazykový konstrukt), takže u něj nemusíte používat závorky. Opravdu, pokud byste potřebovali vytisknout více než jeden parametr, nemohli byste dokonce závorky vůbec použít. Proto nelze použít echo() ani pro proměnnou funkci, ovšem místo toho můžete použít funkci print().

Příklad 2084. Ukázka echo()

<?php
echo "Nazdar světe";

echo
"Toto zabírá
několik řádků. Konce řádků se
vytisknou také"
;

echo
"Toto zabírá\nněkolik řádků. Konce řádků se\nvytisknou také.";

echo
"Speciální znaky předřazené zpětnými lomítky lze použít i v řetězci \"jako toto\".";

//Proměnné lze použít i uvnitř příkazu echo
$foo = "foobar";
$bar = "barbaz";

echo
"foo je $foo"; // foo je foobar

// Použitím jednoduchých uvozovek vypíšte jméno proměnné, nikoli její hodnotu
echo 'foo je $foo'; // foo je $foo

// Jestliže nepotřebujete vypisovat další znaky, můžete rovnou uvést jen názvy proměnných
echo $foo;          // foobar
echo $foo, $bar;     // foobarbarbaz

echo <<<END
Toto používá "dokumentovou" syntaxi pro víceřádkový výstup
s vložnými $prommenymi. Uvědomte si, že ukončovací řetězec
se středníkem musí být na začátku nového řádku (bez mezer či
tabulátorů).
END;

// Protože echo není funkce, následující kód je neplatný
($some_var) ? echo('true'): echo('false');

// Nicméně tento příklad fungovat bude
($some_var) ? print('true'): print('false'); // print je funkce
echo $some_var ? 'true': 'false'; // příkaz musíte uvést předtím
?>

echo() také má zkrácenou syntaxi, kdy je možné následně za otvíracím php tagem použít jen znak rovná se.

Mám <?=$foo?> foo.

Poznámka: Tato zkrácená syntaxe bude fungovat pouze jsou-li povoleny zkrácené otvírací php tagy; short_open_tag je nastaveno na "on".

Viz také: print(), printf() a flush().



explode> <crypt
Last updated: Sat, 24 Mar 2007
 
add a note add a note User Contributed Notes
echo
robertark at gmail dot com
21-Aug-2007 11:25
You mentioned:

<?php
// Because echo does not behave like a function, the following code is invalid.
($some_var) ? echo 'true' : echo 'false';
?>

However, if you still want to echo a string, or anything else, you can use:

<?php
// This ternary operation will work because you can echo a bool true/false and vice-vursa
echo ($some_var) ? 'true' : 'false';
?>

Hope this helps for someone who doesn't understand this very well :)
scott at truebluewc dot com
28-Jul-2007 10:58
<?

function yrsolder ($person=0)
{
$george = 29;
$bobby = 27;
if($person==0) // george
{$yrs = ($george-$bobby);}
if($person==1) // bobby
{$yrs = ($bobby-$george);}
}

echo "I am ".yrsolder(1)." years older than Bobby!";
?>

Will output:

3I am older than Bobby!

instead of

I am 3 years older than Bobby!

This is because php will call functions when they are referenced, before an echo statement is called. So if you have an echo statement inside the function, that echo statement is called first, then the echo statement that originally called the function.
Sean Middleditch
21-May-2007 04: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 11: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 11: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
13-Dec-2006 08: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 01: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.
Jason Carlson - SiteSanity
16-May-2005 10: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
28-Mar-2005 03: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 12: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 01: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 11: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, 24 Mar 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites