note 78898 modified in function.echo by cataphract

From: Date: Thu, 05 Aug 2010 07:52:28 +0000
Subject: note 78898 modified in function.echo by cataphract
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171382@lists.php.net to get a copy of this message
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: <?php echo "outside func ()\n", func (); ?> effectively becomes: <?php 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: <?php echo "outside func ()\n" . func (); ?> effectively becomes: <?php //func () { echo "within func ()\n"; } echo "outside func ()\n" . ''; ?> --was-- 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" . ''; ?> http://php.net/manual/en/function.echo.php

« previous php.notes (#171382) next »