Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between <?=$value?> and <? echo $value; ?>

Tags:

php

Environment: Windows/7 + Apache/2.2.21 + PHP/5.3.8

File contents of test.php:

hello, <?=$test?>

File contents of index1.php:

<?php
$test = 'world';

require './test.php';
?>

File contents of index2.php:

<?php
global $test;
$test = 'world';

require './test.php';
?>

Output of index1.php is:

hello,

Output of index2.php is:

hello, world

When the contents of test.php is:

hello, <? echo $test; ?>

Output of index1.php and index2.php both are:

hello, world

So, my question is: Is there any difference between <?=$test?> and <? echo $test; ?> ?

like image 854
ifyr Avatar asked Sep 10 '26 00:09

ifyr


2 Answers

No, there is no difference. Only one I think about is that <? is considered as short tag and might not work.

like image 74
Martin. Avatar answered Sep 12 '26 14:09

Martin.


There is a little difference, that can be very, very annoying. If in php.ini you short_open_tag is set to false, you will receive a lot of errors. Otherwise, is exactly the same.

In every case, the last ; before ?> is optional.

like image 41
macjohn Avatar answered Sep 12 '26 16:09

macjohn