ifyr
ifyr

Reputation: 41

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

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; ?> ?

Upvotes: 3

Views: 156

Answers (3)

Martin.
Martin.

Reputation: 10539

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

Upvotes: 4

Shai Mishali
Shai Mishali

Reputation: 9402

There is no output differnce between <?=$x;?> and <? echo $x; ?> for that matter.

Even though i think this technique of including an active PHP file isn't really best practices in this specific condition.

Shai.

Upvotes: 2

macjohn
macjohn

Reputation: 1803

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.

Upvotes: 3

Related Questions