PHP Short Open Tags & Short Hand Syntax Cheatsheet

arp14 PHP Tags: , , , , , 0

PHP Short Open Tags & Short Hand Syntax Cheatsheet

PHP Short Tags

A PHP block is started by starting “<?php” tag and closed by “?>” tag. But you if you feel this is kinda long then you can use “<?” for starting and regular “?>” for ending a PHP block as alternative. But to achieve this benefit of PHP short tags, you must enable “short open tag” as this is not enabled by default.

To enable short open tag, open php.ini file and find “short_open_tag” and set its value to “On“.

short_open_tag = On

You can also use ASP like tags in PHP. ASP tags are starting with “<%” and closed with “%>” tag. ASP tags are also not enabled by default. To enable ASP tags, open php.ini file and look for “asp_tags” and set its value to “On“.

asp_tags = On

What PHP says about short tags?

This directive determines whether or not PHP will recognize code between<? and ?> tags as PHP source which should be processed as such. It’s beenrecommended for several years that you not use the short tag “short cut” andinstead to use the full <?php and ?> tag combination. With the wide spread useof XML and use of these tags by other languages, the server can become easilyconfused and end up parsing the wrong code in the wrong context. But becausethis short cut has been a feature for such a long time, it’s currently stillsupported for backwards compatibility, but we recommend you don’t use them.Default Value: OnDevelopment Value: OffProduction Value: Off

http://php.net/short-open-tag

PHP Short Syntax for Echoing/Printing Variables Inline

Printing a variable is done by

<?php echo $variable ?>
But, you can also use this short hand code
<?=$variable?>

This syntax is availabe from PHP 5.4 – pre installed and ready to use. Do not confuse this short syntax with short open tag. You dont need to enable short open tag to use this feature.

Alternative Control Structure – if, for, while, foreach, switch

Shorthand Syntax for “if” Statement

<?php 
	$x = 2;
	$y = 2;
?>

<? if( $x > $y ): ?>
<h1>x is greater than y</h1>
<? elseif( $x == $y ): ?>
<h1>x is equal to y</h1>
<? else: ?>
<h1>x is less than y</h1>
<? endif ?>

Shorthand Syntax for “for” Loop

<?php for ( $i=0; $i<10; $i++ ): ?>
<h1><?=$i?></h1>
<?php endfor ?>

Shorthand Syntax for “while” Loop

<?php $i = 0 ?>
<?php while ( $i < 10 ): ?>
<h1><?=$i?></h1>
<?php $i++ ?>
<?php endwhile ?>

Shorthand Syntax for “foeach” Loop

<?php $list = array(1, 2, 3, 4, 5) ?>
<?php foreach( $list as $item ): ?>
<h1><?=$item?></h1>
<?php endforeach ?>

Shorthand Syntax for “switch” Statement

<?php $i = 2 ?>

<?php switch( $i ):?>
<?php case 1:?>
<?php echo '<h1>i is one</h1>'; break; ?>
<?php case 2:?>
<?php echo '<h1>i is two</h1>'; break; ?>
<?php case 3:?>
<?php echo '<h1>i is three</h1>'; break; ?>
<?php default: ?>
<?php echo '<h1>i is undefined</h1>'; ?>
<?php endswitch ?>
Thanks for your time. If you have any problem regarding PHP short tag and short syntax, please comment below – I’ll be back to you.
Thank you!

Leave a Reply

Note: Your email address will not be published. Required fields are marked *