|
echo
Output one or more strings
(PHP 4, PHP 5)
Example 2406. echo() examples<?php Related Examples ( Source code ) » echo Examples ( Source code ) » React to Form action Examples ( Source code ) » Creating an HTML Form That Accepts Mail-Related Information Examples ( Source code ) » Send email with CC and BCC Examples ( Source code ) » Build query string based on form input Examples ( Source code ) » Form value validation Examples ( Source code ) » Form Data Validation With Error Count Examples ( Source code ) » Call MySelf PHP Examples ( Source code ) » Get Text Field value Examples ( Source code ) » Form Numeric value Examples ( Source code ) » Disable TextBox Examples ( Source code ) » Cookie based login form and get last login time Examples ( Source code ) » Login ID Options Examples ( Source code ) » Hard code login Form Examples ( Source code ) » Get uniqid Examples ( Source code ) » Use md5 function to encrypt text Code Examples / Notes » echozombie
[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. jason carlson - sitesanity
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); } } ?> nikolaas dot mennega
hemanman at gmail dot com, the problem is that func() doesn't actually return a value (string or otherwise), so the result of echoing func() is null. With the comma version, each argument is evaluated and echoed in turn: first the literal string (simple), then func(). Evaluating a function call obviously calls the function (and in this case executes its own internal echo), and the result (null) is then echoed accordingly. So we end up with "outside func() within func()" as we would expect. Thus: <? echo "outside func ()\n", func (); ?> effectively becomes: <? echo "outside func ()\n"; //func () { echo "within func ()\n"; } echo ''; ?> The dot version is different: there's only one argument here, and it has to be fully evaluated before it can be echoed as requested. So we start at the beginning again: a literal string, no problem, then a concatenator, then a function call. Obviously the function call has to be evaluated before the result can be concatenated with the literal string, and THAT has to happen BEFORE we can complete the echo command. But evaluating func() produces its own call to echo, which promptly gets executed. Thus: <? echo "outside func ()\n" . func (); ?> effectively becomes: <? //func () { echo "within func ()\n"; } echo "outside func ()\n" . ''; ?> ryan
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; } ?> |
Change Languageaddcslashes addslashes bin2hex chop chr chunk_split convert_cyr_string convert_uudecode convert_uuencode count_chars crc32 crypt echo explode fprintf get_html_translation_table hebrev hebrevc html_entity_decode htmlentities htmlspecialchars_decode htmlspecialchars implode join levenshtein localeconv ltrim md5_file md5 metaphone money_format nl_langinfo nl2br number_format ord parse_str printf quoted_printable_decode quotemeta rtrim setlocale sha1_file sha1 similar_text soundex sprintf sscanf str_getcsv str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split str_word_count strcasecmp strchr strcmp strcoll strcspn strip_tags stripcslashes stripos stripslashes stristr strlen strnatcasecmp strnatcmp strncasecmp strncmp strpbrk strpos strrchr strrev strripos strrpos strspn strstr strtok strtolower strtoupper strtr substr_compare substr_count substr_replace substr trim ucfirst ucwords vfprintf vprintf vsprintf wordwrap |