Using PHP Short Tags

PHP Short Tags refers to '<?' when a full tag is '<?php'. Most PHP installations has short tags support - but there are a few installations that have turned it off. So it is recommended that you don't use the short tags in your app if you plan to distribute it.

Short tags are considered bad because they are in conflict with XML's open tag - '<?xml'. If it is a PHP file, the interpreter will think everything after the '<?' is PHP code. As a result it will show a parsing error.

This problem can be solved easily - I use this code...

<?php echo '<?'; ?>xml

I use the full tag almost all the time. The only exception is that I use <?= $print_me ?>. And I am not prepared to give that up.

I follow the MVC pattern in my projects. My PHP framework, iFrame, is an MVC framework. In the template(view) part, there is a lot of usage of such tags. It is more concise and more readable than the alternative...

<?= $print_me ?>
<?php print $print_me; ?>

You tell me - which is better?

I hear that they will be introducing <php= > in PHP 6. Until then, I will continue to use <?= ?>.

Comments

Anonymous at 14 Apr, 2009 11:11
sed -i 's/<?= \$/<?php print \$/g' yourfile.php
Reply to this.
Anonymous at 23 Jun, 2009 07:17

<?php
echo "test";
?>
Reply to this.
Johan de Jong at 04 Aug, 2009 12:37
Personally, I hate people who use short tags, since it's just a case of bad coding.
And, as you said yourself, it's not recommended.

At my MVC framework, I use the 'Controller PUSH to View' method, instead of the 'View PULL from Controller'.
So instead of placing variables at my view which are requested by the view from the Controller, all data is replaced at the template engine by the Controller into the View.

Example (HTML - View):

<table>
<!-- ROW -->
<tr>
<td>#{id}</td>
<td>{name}</td>
<td>view</td>
</tr>
<!-- /ROW -->
</table>


Example (PHP - Controller):

<?php
$tpl->get('example.html');
$items = $db->findAll('items'); // get all rows from the 'items' table
foreach($items as $item) {
$tpl->parse('id', $item['id']); // parse the 'id' variable
$tpl->parse('name', $item['name']); // parse the 'name' variable
$tpl->block('ROW'); // render the 'ROW' block
}
$tpl->render(); // print the HTML
?>
Reply to this.
Dougle at 21 Oct, 2009 02:54
@Johan Don't lecture people on good php coding, when you are not using the language fully anyway! your a user of someone else's software.

Using your framework's template object compared to using straight php (which originally was a template language in itself) is totally irrelevant to this topic.
Reply to this.
doug at 18 Jan, 2010 09:25
Great code man. A good MVC framework is the way to go. I can't tell if these other comments are serious or not. It sounds like these people are trolling.
Reply to this.
Anonymous at 11 Sep, 2009 02:18
Actually I like short tags (it's faster) but
I would like to know why it is "a case of bad coding"...
Reply to this.
Anonymous at 11 Sep, 2009 03:10
It's not bad coding, it's a substitute for having a life ;-)
Reply to this.
malik at 15 Oct, 2009 02:45
better <?= $print_me ?>

use <?php to start page to solve xml problem

then use short echo <?=$print_me?> any where

Reply to this.
wowow at 18 Nov, 2009 05:39
@Dougle
His code actually made me chuckle. Hehe.
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed