This is a discussion on Newbie question about <?= ?> within the PHP General forums, part of the PHP Programming Forums category; Good day, While I've been using php for more than a little while now, I've never understood why ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 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 > - From my understanding, there are multiple reasons. One is that depending on where your application is being hosted, the server it is on may have turned off the short tags option (and you can't get your hosting provider to change this). Thus, <?= ...?> would simply be written to the page as that. Additionally, through some of my training courses, another reason behind it is that if you use PHP to generate any XML documents, XML uses <? ?> syntax, and it's better to turn off the short tags in your PHP config so that PHP doesn't attempt to interpret those tags as PHP code. - -- Christopher Weldon President, Lead Systems Administrator Cerberus Interactive, Inc. cweldon@cerberusonline.com 979.739.5874 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFAsRnZxvk7JEXkbERAiW+AJ9POOf3U1K6TiRVhSFC6o k7VjDm2ACfel8U /6gFKYPPFYa5iyEtWdksZ0w= =C1fK -----END PGP SIGNATURE----- |
|
|||
|
All forms of short tags make your php file not XML compliant.
Notice that the <?php tag which starts every script is a valid XML construct, it is what is called a Processing Instruction (PI) and admits within it absolutely anything except for a ?> which ends the PI. A PI you will be familiar with is the <?xml PI. XML expects something to identify who is to process that PI, thus, there can be no 'anonymous' PI such as a plain <?. Actually, I have heard that it is possible (and have been done) to use more than one interpreter at once on the same document, using the proper processing tags for each language. How you configure that, don't ask me, I wouldn't know. Any short tag then, whether a <? or a <?=, is not valid XML. Whether that is important or not, I can't tell, but it is the only reason I know why short tags might be considered inapropriate and disabled. Satyam ----- Original Message ----- From: "Mike Borrelli" <mike@hitori.org> To: <php-general@lists.php.net> Sent: Saturday, September 09, 2006 3:19 PM Subject: [php] Newbie question about <?= ?> > 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 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > |
|
|||
|
On Saturday 09 September 2006 08:19, 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 As was said before, the major reasons for not using short tags are: 1) Not everyone has access to the ini file, and might not be able to use short tags. If you're releasing your code to the public, you want to work in as many places as possible, so then you should use <?php instead. 2) when using XML files, they use <?xml, and the <? is recognized as the start of a PHP block, but the following syntax will be correct and your code will fail. I have seen that someone suggested having PHP recognize <?xml as not being a PHP short tag, but I don't remember where off the top of my head. This does bring up an interesting idea though. It appears that <?php= is not valid. A lot of people use <?= as a sort of templating system instead of adding the complexity of smarty or other templating systems (which are fine). Is it possible, or will it be possible, to use <?php="blah" ?> -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 |
|
|||
|
Okay, after thinking about it, and looking at the replies, it seems as
though there is only one situation where the short tags break things, and that's in XML with the <? ?> pair being valid for XML. Yes, enabling the short tags is in the ini file, but is it on by default and require effort to change? That's the real question, I think. Not, why shouldn't I use them the way they are, but why is it that the <?= ?> tag isn't designed into the language to be acceptable? Honestly, the <? ?> pair just feels a little lazy, since all you save are the 4 characters "php ", and all the code inside is the same. The <?= ?> tag gives you 7 characters free and does something unique. The situation I think of using it the most in is something such as: <li><a href="<?= $post->permLink; ?>">...</a></li> Which looks cleaner, in my opinion, than: <li><a href="<?php print $post->permLink; ?>">...</a></li> Not a whole lot larger, no, and perhaps you might say, Well, the second way is more explicit! Yes. Also, shouldn't it be possible to check for <?= ?> and, based on the '=' there, pop out of the parser if it comes across <?xml...?> ? Cheers, Mike > As was said before, the major reasons for not using short tags are: > > 1) Not everyone has access to the ini file, and might not be able to use > short > tags. If you're releasing your code to the public, you want to work in as > many places as possible, so then you should use <?php instead. > > 2) when using XML files, they use <?xml, and the <? is recognized as the > start > of a PHP block, but the following syntax will be correct and your code > will > fail. I have seen that someone suggested having PHP recognize <?xml as > not > being a PHP short tag, but I don't remember where off the top of my head. > > This does bring up an interesting idea though. It appears that <?php= is > not > valid. A lot of people use <?= as a sort of templating system instead of > adding the complexity of smarty or other templating systems (which are > fine). > Is it possible, or will it be possible, to use <?php="blah" ?> > > -- > Ray Hauge > Programmer/Systems Administrator > American Student Loan Services > www.americanstudentloan.com > 1.800.575.1099 > |
|
|||
|
----- Original Message ----- From: "Mike Borrelli" <mike@hitori.org> To: "Ray Hauge" <ray.hauge@americanstudentloan.com> Cc: <php-general@lists.php.net>; "Mike Borrelli" <mike@hitori.org> Sent: Sunday, September 10, 2006 6:52 AM Subject: Re: [php] Newbie question about <?= ?> > Okay, after thinking about it, and looking at the replies, it seems as > though there is only one situation where the short tags break things, and > that's in XML with the <? ?> pair being valid for XML. > As I said <? ?> is not valid XML, it misses the code to identify the processor to deal with the contents. XML expects what it calls a PITarget (PI being Processing Instruction, which is what this <?PITarget ?> pair is) right after the first question mark. > Yes, enabling the short tags is in the ini file, but is it on by default > and require effort to change? > > That's the real question, I think. Not, why shouldn't I use them the way > they are, but why is it that the <?= ?> tag isn't designed into the > language to be acceptable? > Which language? If it only was for PHP, anything could be designed into it, but it does not have the environment all to itself and it might have to coexist with other standards. XML requires the PITarget, and that's it. Nevertheless, you are not required to be XML compatible in your setup and indeed, in most cases, this is irrelevant. > Honestly, the <? ?> pair just feels a little lazy, since all you save are > the 4 characters "php ", and all the code inside is the same. The <?= ?> > tag gives you 7 characters free and does something unique. > > The situation I think of using it the most in is something such as: > <li><a href="<?= $post->permLink; ?>">...</a></li> > > Which looks cleaner, in my opinion, than: > <li><a href="<?php print $post->permLink; ?>">...</a></li> > > Not a whole lot larger, no, and perhaps you might say, Well, the second > way is more explicit! Yes. > > Also, shouldn't it be possible to check for <?= ?> and, based on the '=' > there, pop out of the parser if it comes across <?xml...?> ? > xml is not the only valid PITarget out there, thus, you cannot parse everything but xml since the list of 'buts' is endless. Anyway, if you care for XML compatibility , it is important that the <?xml PI is in clear text at the beginning and that you don't use short tags, otherwise if you do not care for compatibility, then the <?xml can be echoed from within php as a normal string, which is a good thing since, after all, the <?xml in clear text at the beginning is announcing that the whole document is xml compatible which, with short tags enabled, it is not. The output of that script, which would include the echoed <?xml, would be XML compatible. I personally don't care much for XML compatibility in my PHP scripts, I care that the output of those scripts is XML compatible, if it has to. I still think the recomendation is valid though I am still to see any place which would deny itself the chance to run zillions of packages out there to achieve some theoretical compatibility at a point (PHP source code) where it is irrelevant. Satyam > Cheers, > Mike > >> As was said before, the major reasons for not using short tags are: >> >> 1) Not everyone has access to the ini file, and might not be able to use >> short >> tags. If you're releasing your code to the public, you want to work in >> as >> many places as possible, so then you should use <?php instead. >> >> 2) when using XML files, they use <?xml, and the <? is recognized as the >> start >> of a PHP block, but the following syntax will be correct and your code >> will >> fail. I have seen that someone suggested having PHP recognize <?xml as >> not >> being a PHP short tag, but I don't remember where off the top of my head. >> >> This does bring up an interesting idea though. It appears that <?php= is >> not >> valid. A lot of people use <?= as a sort of templating system instead of >> adding the complexity of smarty or other templating systems (which are >> fine). >> Is it possible, or will it be possible, to use <?php="blah" ?> >> >> -- >> Ray Hauge >> Programmer/Systems Administrator >> American Student Loan Services >> www.americanstudentloan.com >> 1.800.575.1099 >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > |
|
|||
|
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; |
|
|||
|
----- Original Message ----- From: "Al" <news@ridersite.org> To: <php-general@lists.php.net> Sent: Sunday, September 10, 2006 5:52 PM Subject: [php] 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 |
|
|||
|
Al wrote:
> 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; I thought I'd look into this, because I'm a bit of a performance nut - I like my code to run as fast as possible at all times. I wrote up a quick buffer v.s. direct "benchmark" for this, and the winner is clear: direct output is much faster. (If my example below isn't what you meant, please let me know. I'm always happy to hear new ways to improve my code.) Best of 3 runs with apache bench (concurrency 10, 1000 requests total): Direct output: 582 requests a second Buffer var: 286 requests a second I believe the margin would get wider with real-world usage, as the buffer variable would increase in size. My test code is copied below. jon --- "Direct output": testecho.php --- <html> <head> <style type="text/wastespacetosimulateastylesheet"> style1 { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; } style2 { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; } style3 { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; } style4 { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; } </style> </head> <body><table> <?php for ($x=0;$x<1000;$x++) { ?> <tr><td>X is <?= $x ?></td></tr> <?php } ?> </table></body> </html> --- "Buffered output": testbuffer.php --- <?php $buffer = ' <html> <head> <style type="text/wastespacetosimulateastylesheet"> style1 { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; } style2 { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; } style3 { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; } style4 { a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; } </style> </head> <body><table>'; for ($x=0;$x<1000;$x++) { $buffer .= "<tr><td>X is $x</td></tr>"; } $buffer .= '</table></body> </html>'; echo $buffer; ?> |
|
|||
|
----- Original Message ----- From: "Jon Anderson" <jon@gamingsolutions.ca> To: <php-general@lists.php.net> Cc: "Al" <news@ridersite.org> Sent: Sunday, September 10, 2006 9:16 PM Subject: Re: [php] Re: Newbie question about <?= ?> > Al wrote: >> 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; > I thought I'd look into this, because I'm a bit of a performance nut - I > like my code to run as fast as possible at all times. I wrote up a quick > buffer v.s. direct "benchmark" for this, and the winner is clear: direct > output is much faster. (If my example below isn't what you meant, please > let me know. I'm always happy to hear new ways to improve my code.) > > Best of 3 runs with apache bench (concurrency 10, 1000 requests total): > Direct output: 582 requests a second > Buffer var: 286 requests a second > > I believe the margin would get wider with real-world usage, as the buffer > variable would increase in size. My test code is copied below. > > jon > > --- "Direct output": testecho.php --- > > <html> > <head> > <style type="text/wastespacetosimulateastylesheet"> > style1 { > a = 1; > b = 2; > c = 3; > d = 4; > e = 5; > f = 6; > } > style2 { > a = 1; > b = 2; > c = 3; > d = 4; > e = 5; > f = 6; > } > style3 { > a = 1; > b = 2; > c = 3; > d = 4; > e = 5; > f = 6; > } > style4 { > a = 1; > b = 2; > c = 3; > d = 4; > e = 5; > f = 6; > } > </style> > </head> > <body><table> > > <?php for ($x=0;$x<1000;$x++) { ?> > <tr><td>X is <?= $x ?></td></tr> > <?php } ?> > > </table></body> > </html> > > --- "Buffered output": testbuffer.php --- > > <?php > > $buffer = ' > <html> > <head> > <style type="text/wastespacetosimulateastylesheet"> > style1 { > a = 1; > b = 2; > c = 3; > d = 4; > e = 5; > f = 6; > } > style2 { > a = 1; > b = 2; > c = 3; > d = 4; > e = 5; > f = 6; > } > style3 { > a = 1; > b = 2; > c = 3; > d = 4; > e = 5; > f = 6; > } > style4 { > a = 1; > b = 2; > c = 3; > d = 4; > e = 5; > f = 6; > } > </style> > </head> > <body><table>'; > > for ($x=0;$x<1000;$x++) { > $buffer .= "<tr><td>X is $x</td></tr>"; > } > > $buffer .= '</table></body> > </html>'; > > echo $buffer; > ?> > > -- In my message I was careful to mention that stepping in and out of PHP was good 'for the longer invariable parts of immutable HTML'. What could be considered 'longer' is certainly a matter discussion, your results prove that this is not long enough. Notice that when the parser finds the '<?=', it converts it into the equivalent of "<? echo", thus, though the echo is not explicitly there, from the parser on is as if it were. Then, since you have an echo, why not use it for all of the output? The equivalent to what I showed as the second best, which would be the first best with 'shorter' strings would be the following: for ($x=0;$x<1000;$x++) { echo ' <tr><td>X is ' , $x , '</td></tr>'; } Can you try and time that one so we have comparable results? This one should be second best: for ($x=0;$x<1000;$x++) { echo "<tr><td>X is $x</td></tr>"; } Back again to what would be 'longer', well, in your example, the whole header, up to the loop itself should be faster if sent out of PHP. Likewise, you could echo $buffer right after the loop, drop out of PHP and send the footer as plain HTML. This, of course, is harder to time since it happens only once. I admit though that I did time the options I listed and on the 'dropping in and out of PHP' I'm relying on the PHP manual ( see http://www.php.net/manual/en/language.basic-syntax.php, the first paragraph after the examples) and the source of the lexical scanner, which supports that, though your numbers do contradict it. Interesting. Satyam |