I used to use echo like this because I thought it
neat and easier to read.
<?
echo
"<TABLE>",
"<TR>",
"<TD>COLUMN A</TD>",
"<TD>COLUMN BA</TD>",
"<TD>COLUMN C</TD>",
"<TD>COLUMN D</TD>",
"<TD>COLUMN E</TD>",
"</TR>",
"</TABLE";
?>
Until, one day, I noticed that the HTML was injected with misterious
8 characters in the middle of the HTML stream.
Using packet capture I discovered that the first 2 and last 2 bytes
were always OD OA (cr & lf) and the middle 4 were something
similar to '206e'. The very odd thing is that the first OD OA was
always at the end of a network packet and the remaining 6 were
at the beginning of the following network packet. It was happening
approximately every 5 to 10 running of the scripts and was random.
So, Instead, I tried using concatenation replacing large chunks
where echo was...
<?
$echo="<TABLE>";
$echo.="<TR>";
$echo.="<TD>COLUMN A</TD>";
$echo.="<TD>COLUMN BA</TD>";
$echo.="<TD>COLUMN C</TD>";
$echo.="<TD>COLUMN D</TD>";
$echo.="<TD>COLUMN E</TD>";
$echo.="</TR>";
$echo.="</TABLE";
echo $echo;
?>
The issue disappeared AND it works faster! :)
I hope this helps someone with similar issues
nando_f@nothingsimple.com
echo
(PHP 4, PHP 5)
echo — 1 つ以上の文字列を出力する
説明
void echo
( string $arg1
[, string $...
] )
すべてのパラメータを出力します。
echo() は実際には関数ではありません (言語構造です)。このため、使用する際に括弧は必要ありません。 (いくつかの他の言語構造と異なり) echo() は関数のように動作しません。そのため、 関数のコンテキスト中では常に使用することができません。 加えて、複数のパラメータを指定して echo() をコールしたい場合、括弧の中にパラメータを記述してはいけません。
echo() には、開始タグの直後に等号を付ける短縮構文もあります。 この短縮構文は、設定オプションshort_open_tag が有効な場合のみ使用可能です。
I have <?=$foo?> foo.
パラメータ
- arg1
-
出力したいパラメータ。
- ...
-
返り値
値を返しません。
例
Example#1 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\".";
// echo 命令の中で変数を使用することが可能です
$foo = "foobar";
$bar = "barbaz";
echo "foo is $foo"; // foo is foobar
// 配列を使用することもできます
$baz = array("value" => "foo");
echo "this is {$baz['value']} !"; // this is foo !
// 値ではなく変数名を出力するシングルクオートを使用します
echo 'foo is $foo'; // foo is $foo
// 他の文字を全く使用しない場合、echo 変数を使用可能です
echo $foo; // foobar
echo $foo,$bar; // foobarbarbaz
// 複数のパラメータを結合してechoに渡そうとする人もいます
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;
// echo は関数のように動作しないので、以下のコードは正しくありません
($some_var) ? echo 'true' : echo 'false';
// しかし、次の例は動作します
($some_var) ? print 'true' : print 'false'; // print も言語構造ですが、
// 関数のように動作します。なので、
// このコンテキスト中で使用できます
echo $some_var ? 'true': 'false'; // 命令を変更
?>
注意
print() と echo() の違いに関する簡単な議論については、FAQTs Knowledge Base Article: » http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40 を参照してください。
注意: これは、関数ではなく 言語構造のため、可変関数 を用いて コールすることはできません。
echo
nando_f at nothingsimple dot com
17-Mar-2008 06:13
17-Mar-2008 06:13
nikolaas dot mennega at links dot com dot au
01-Nov-2007 04:04
01-Nov-2007 04:04
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" . '';
?>
Jason Carlson - SiteSanity
17-May-2005 02:28
17-May-2005 02: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);
}
}
?>
ryan at wonko dot com
27-Feb-2005 05:56
27-Feb-2005 05: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;
}
?>
zombie)at(localm)dot(org)
26-Jan-2003 04:26
26-Jan-2003 04: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.