Log in

View Full Version : <?php and variations thereof



marain
12-21-2012, 02:03 AM
The code below works (at least when error_reporting is turned off). But I do not understand WHY it works. I understand the short tag notation (and that its use is discouraged). But when I add "php" to the short tag, or remove the equal sign (use of which I do not understand), or any combination thereof, the page no longer works. I am bewildered and befuddled.

Please help.



<?php
$lastUpdate = date( 'j F Y', filemtime( 'pageContent/test.txt' ) );
// error_reporting( -1 );
// ini_set( 'display_errors','On' );
?>
<br />
<br />
&copy; Copyright 2006-<?= date( 'Y' ) ?>, Allan Marain, New Brunswick, NJ, All rights reserved.
<br />
<br />
Page last updated <?= $lastUpdate ?>.<br />

bernie1227
12-21-2012, 02:15 AM
Basically, you can make up your own mind as to whether to use them or not ( you could take a look at these:
http://stackoverflow.com/questions/1386620/php-echo-vs-php-short-tags
http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use
http://stackoverflow.com/questions/234241/how-are-echo-and-print-different-in-php
and I have no doubt that traq will proffer his own opinion on them).

But as to your question, your code means exactly the same thing as:


<?php
$lastUpdate = date( 'j F Y', filemtime( 'pageContent/test.txt' ) );
// error_reporting( -1 );
// ini_set( 'display_errors','On' );
?>
<br />
<br />
&copy; Copyright 2006-<?php echo date( 'Y' ) ?>, Allan Marain, New Brunswick, NJ, All rights reserved.
<br />
<br />
Page last updated <?php echo $lastUpdate ?>.<br />


in other words, <?= is the same as <?php echo, although it is easier to write.

If you took out the equal sign of <?= $variable, that would be the same as just writing; <?php $variable. It doesn't not work (if that makes sense), but it just doesn't output anything.

TL;DR: <?= means <?php echo.