Question

I'm wondering if there is a shorter way of inserting text in PHP than

<?php
$city = "London";
?>
This website is a funky guide to <?php print $city; ?>!!!

For example, using ruby on rails, I could set

city = 'London'

somewhere in the code, and in my .erb file I could do

This website is a funky guide to <%= city %>!!!

I did read somewhere that {$city} could be used, but I tried it and it didn't. So is there a shorter form than <?php print $var; ?> ?

Was it helpful?

Solution

You could use short_open_tag, which have to be enabled in your configuration, but that's not considered as a good practice, as it only works if those are enabled -- and they are not always (maybe not even by default)

Using long tags and echo/print might be longer, yes... But I would recommend using those, and not short tags.


Also note that you might need to escape your data, when it comes from an un-trusted source and/or might contain HTML you don't want to get injected in the page, to avoid injections of HTML/JS (see htmlspecialchars) :


EDIT after the comments, to add couple of things about short_open_tag :

Why are short open tags considered (at least by me ^^ ) bad practice ?

First of all, after some checking, they are not enabled by default :

For PHP 5.3 :

squale@shark:~/temp/php/php-5.3.0
$ grep 'short_open_tag' php.ini-development
; short_open_tag
short_open_tag = Off
squale@shark:~/temp/php/php-5.3.0
$ grep 'short_open_tag' php.ini-production
; short_open_tag
short_open_tag = Off

Disabled by default in either "development" or "production" settings.

For PHP 5.2.10 (most recent version of PHP 5.2) :

squale@shark:~/temp/php/php-5.2.10
$ grep 'short_open_tag' php.ini-dist
short_open_tag = On
squale@shark:~/temp/php/php-5.2.10
$ grep 'short_open_tag' php.ini-recommended
; - short_open_tag = Off           [Portability]
short_open_tag = Off

Disabled by default in the "recommended" settings


Considering these default settings are sometimes (often ?) kept by hosting services, it is dangerous to rely on short_open_tag being activated.
(I have myself run into problem with those being disabled... And when you are not admin of the server and don't have required privilegies to modify that, it's not fun ^^ )

If you want some numbers, you can take a look at Quick survery: short_open_tag support on or off by default?
(Not a scientific proof -- but show it could be dangerous to use those for an application you'd release to the public)


Like you said, those, when activated, conflict with XML declaration -- means you have to use something like this :

<?php echo '<?xml version="1.0" encoding="UTF-8" ?>'; ?>

Considering short open tags exists, and might be activated on the server you'll use, you should probable not use <?xml ever, though ; too bad :-(


Actually, reading through the php.ini-recommended of PHP 5.2.10 :

; Allow the <? tag.  Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.

The one from PHP 6 is even more interesting :

; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php and ?> tag combination. With the wide spread use
; of XML and use of these tags by other languages, the server can become easily
; confused and end up parsing the wrong code in the wrong context. But because
; this short cut has been a feature for such a long time, it's currently still
; supported for backwards compatibility, but we recommend you don't use them.

(Might be the same in PHP 5.3 ; didn't check)


There have been rumors short open tags could be removed from PHP 6 ; considering the portion of php.ini I just posted, it probably won't... but, still...


To give an argument pointing to the other direction (I've gotta be honest, after all) : using short open tags for template files (only) is something that is often done in Zend Framework's examples that use template files :

In our examples and documentation, we make use of PHP short tags:

That said, many developers prefer to use full tags for purposes of validation or portability. For instance, short_open_tag is disabled in the php.ini.recommended file, and if you template XML in view scripts, short open tags will cause the templates to fail validation.

(source)

On the contrary, for .php files :

Short tags are never allowed. For files containing only PHP code, the closing tag must always be omitted

(source)


I hope those informations are useful, and bring some kind of answer to your comment :-)

OTHER TIPS

If you switch out of PHP mode, then the only way to print the variable is to switch back into it and print it like you have above. You could stay in PHP mode and use the {} construction you tried:

<?php
$city = "London";
echo "This website is a funky guide to {$city}!!!";
?>

I did read somewhere that {$city} could be used

If you go for a templating engine such as Smarty you can do this. That might be useful if you're developing in a team and some of them don't know PHP.

Otherwise, the shortest you will get is the following, assuming you have short tags enabled:

<?=$city?>

If you don't, or you're creating an app to redistribute to others, then use this:

<?php echo $city ?>

Just extend your PHP block and do this:

<?php
$city = 'London';
echo 'This website is a funky guide to ',$city,' !!!';
?>

Alternatively, you can use " " instead of ' ' and replace ',$city,' with $city, but it takes more CPU time to parse for variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top