I feel a bit stupid for asking, but when and why is <?=
used instead of <?php
?
Kirby-Builder makes use of <?=
for example.
I am just getting started working in php and could not find an answer to this. Would appreciate an explanation or some pointers to relevant literature.
Cheers & thanks
Julian
<?= ?>
is the same as <?php echo ?>
, only shorter. It is generally good practice to use the short code for echoing something (but you can use both).
Don’t use the short code <? ... ?>
without the =
for general php purposes (i.e. when not echoing something), though.
Example:
<?= $page->title() ?>
is the same as:
<?php echo $page->title() ?>
Don’t do this, however:
<?
foreach($pages as $p) {
....
}
?>
Always use the php tag:
<?php
foreach($pages as $p) {
...
}
?>
Ah nice. Thanks for the short explanation.
Are there other shorthands for things like <?php foreach
for example?
EDIT: Thanks for the further clarification with the examples.
No, there is no shorthand for foreach
, see my edited post above.
1 Like
http://php.net/manual/en/ini.core.php#ini.short-open-tag
Maybe this is interesting for you.
Not all developers / server-admins like the Short Open Tag (for various reasons), so I never use it (been there, done that… too much trouble with it).
1 Like
Just a little image that’s good to keep in mind:
2 Likes
The short open tag <? ?>
is different from <?= ?>
as I explained above and should not be used.
Kirby requires PHP 5.4+, so the short echo tag may safely be used. PHP 5.4 has reached its end of life, anyway: http://php.net/supported-versions.php
You should be using PHP 5.6+ these days.
See this discussion as well: To PHP Short Tag or not to PHP Short Tag?
Cool - thx! 
I know, but not all of my clients can migrate to PHP 5.6+ (for several reasons).
In the ideal world, everyone is using the most recent versions of PHP (or, to be honest - no PHP at all) - but I guess we’re not living in the ideal world 
I even have clients, that use PHP 5.2… can you imagine… :’(
Well, that may be so. But as I said, the current version of Kirby requires PHP 5.4+, so you have no other choice.
Even a cheap shared hosting plan let’s you choose your PHP version a a per-domain basis. I think that money is well invested in security.
I understand that clients might have old PHP versions running for old applications but that should not stop you from using current PHP version for current software.
I 100% agree with you - but I have a couple of clients in South-America, and the IT-level over there is not what we can except in Europe.
The highest bandwidth is about 2G (or they use TDMA which does not allow mobile internet at all), they sell server-space for maximum of 25mb… etc… (and they pay a lot for this).
I even have one client on a server that’s not updated since 1998 - can you imagine?
Off course they don’t use Kirby, I use Wordpress for those instances 
So, it’s a lot off-topic - but thanks again for your outstanding knowledge and support!