Re: Re: Newbie question about <?= ?>

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




----- Original Message ----- From: "Al" <news@xxxxxxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Sunday, September 10, 2006 5:52 PM
Subject:  Re: Newbie question about <?= ?>


Mike Borrelli wrote:
Good day,

While I've been using php for more than a little while now, I've never
understood why the use of the "<?= ...?>" short tag is noted "to be
avoided".

Or rather, I understand that there's an option to disable it, and that's
why it's noted in this way, but I don't understand why it's disabled? What's gained by writing <?php echo some_function(); ?> over <?=
some_function(); ?>

Thanks in advance.

Cheers,
Mike
Structurally, there is a far better way to compile your html pages. This approach is easier to design and debug and it is faster since it sends one complete packet instead of one for every short tag. And, it saves using ob_start() and ob_flush().

Consider:

$report= '';

$report .= function() [or whatever]

..... repeat as necessary to assemble your complete page.

Then simply

echo $report;


Actually, in my experience, that is not the case, my e-mail from more than a year ago must be somewhere there in the archives, but what you sugest is not the fastest.

The fastest is to escape out of php (with a ?> ) for the longer invariable parts of immutable HTML. Stepping out from PHP and in again is handled by the lexical scanner, it doesn't even reach the parser level so, for all effects, the PHP interpreter is basically frozen at the point before the ?> was found. For the sake of completeness, the ?> is translated as a ; for the parser so it ends any statement that could have been left open, but it does not bother the parser at all for all the rest of the characters found until a <?php tag (or equivalent) is found. If the lexer didn't issue a ; for a ?>, the following code would be valid:

echo 'This ' , ?> is <?php 'not valid';

For the variable parts, the best is to issue as little echos as possible with its arguments separated by commas, not with dots. Most people don't realize that echo taks a list of arguments, a list separated by commas. Thus, in the ratings, from best to worst, it goes:

echo '<p>' , $something, '</p>';
echo "<p>$something</p>"
echo '<p>' . $something . '</p>';
echo '<p>'; echo $something; echo '</p>';

The reason for this is that generating a single string either from variable interpolation as in the second case or by concatenating the arguments, as in the third, requires a lot of memory handling for the strings and its intermediate and final results. Some of it might be delayed until the page is served so the time the garbage collector takes to clean it up might not be fully reflected in the processing time of a single page, but it does affect the overall throughput of the server. Notice also that I have used single quotes whenever possible, which is slightly faster since the parser has much less to look for within it.

Finally, the first option is the fastest just as a C++ iostream or a Java StringBuffer are faster than plain strings: since you know you will only append to the end of them, the characters echoed go into a much more efficient character buffer instead of a more complex string which has to be available for all sorts of string operations.

Satyam

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux