What Does <?=?> (Question Mark Equals) Do In PHP?

Are you looking to learn the basics of WooCommerce development?
My book "Learning WooCommerce Development by Example" will teach you what you need in a fast and friendly way!

Some times when looking at WordPress or WooCommerce code you might see something like this

<?php
$value = "Hi there!";
?>

<div>
    <strong><?=$value?></strong>
</div>

The very simple example above would render the following to the screen

So what is the  <?=$value?> code doing? From the example above you might have worked out that <?=$value?> is shorthand for writing <?php echo $value;?> so we could change our code to this

<?php
$value = "Hi there!";
?>

<div>
    <strong><?php echo $value;?></strong>
</div>

and still get the same results.

Is it Always Safe to Use the <?=?> Syntax in PHP

The answer to this question is it’s safe if you’re using PHP version 5.4 or above.

PHP 5.4 was introduced in 2015 so it should be safe to assume that most hosts/environments will be using it, but it’s always wise to double check if you think your code maybe hosted somewhere that uses a lower version.

To give a bit of background, PHP also has a shortcut tag with the syntax <? this tag can cause issues as it shares a syntax with XML ,but the <?=  syntax does not clash with XML so it is safe to use.

Any warnings you see about the syntax are likely to be because  prior to version 5.4 PHP bundled both tags under the short_open_tag php.ini directive, this meant that to turn on the <?= syntax also meant enabling the <? syntax which could lead to issues if you used PHP in combination with XML.

From version 5.4 onwards PHP made the <?= tag always available and no longer linked it to the short_open_tag php.ini directive  meaning it could be used safely in all circumstances.

Further Reading

https://softwareengineering.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php – This Software Engineering Stack Exchange question has a numerous helpful answers that go into greater detail than this article. Note that most of the answers/comments that advise you against using the syntax were made prior to 2015.

Leave a Comment