PHP code explanation "<?=$variable?>

allanch08
allanch08 used Ask the Experts™
on
Hello experts,

I've seen this simple bit of code and I'm trying to understand it. It looks like PHP and it's requesting to print a variable. Can someone shed more light on this? thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
<?=$variable?>

all that is saying is:
<?  is the short php open tag
=$variable   short for "echo $variable"
?>  closing php tag

so <?=$variable?>  echoes out $variable inline to html.

i.e.

<?php
$page = 'index.php';
?>
<a href='http://example.com/<?=$page?>'>click here</a>
 
prints out:
<a href='http://example.com/index.php'>click here</a>

Open in new window

Author

Commented:
thanks for speed reply. i get it now, so this is just a shorthand way of doin the echo. is it purely down to individual preference as to which one to use?
Commented:
<?=$variable?>

is the same as:

<?php
echo $variable;
?>

It uses short tags instead of full tags. This can be a problem when a server has short tags turned off and you have no control over them.

I would always suggest using full tags.
Commented:
<?=$variable?>

typically has a similar effect as

<?php
echo $variable;
?>

However, it is not the same*.  If it were the same*, then there would be no concern or worry for using short tags or not.

Nevertheless, ncoo nailed it on the head - using full tags is the safest way to go about it, because not all servers have the same configurations in regards to short tags.

so for best coding practices and transportability, use
<?php

instead of
<?

Author

Commented:
thanks for the help! I hope I've devided the points in a fair way.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial