How can I replace `<?php echo ...; ?>` expression with `<?= ?> `? - php

From php 5.4, <?= ...?> can be used for replacement of <?php echo ...;?>.
So I would like to replace all of them in .php files in my project. How can I replace them?
Or can't it be possibly convert automatically?
I have found the script that replaces array() with [] (thomasbachem/php-short-array-syntax-converter). Do anyone know something like this?
(This may be present anywhere, but it is hard for google to search by queries with many symbols...)

If you have short_open_tags enabled in php.ini, you will be able to use <?= ...?> instead of default open tags.
As for as converting all the program statements from
<?php
echo $var;
?>
to
<?=$var ?>
would have to be done manually. I do not know of any convertor script that automates it, although I found some scripts on github that does the reverse(converting short tags to long ones).

In PHP 5.4.0, The tag <?=.. is always available regardless of the short_open_tag ini setting. Source
php.ini
; 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.
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://php.net/short-open-tag
short_open_tag=Off
After that you can change your <?php echo ?> to <?= ?>

in your php.ini change the tag:
short_open_tag=Off to:
short_open_tag=On. that would solve your problem

Related

Difference in Rendering Yii2 php tags [duplicate]

Even the official documentation used to tell us that PHP "short tags" (<? /*...*/ ?>) are "bad". However, since PHP 5.4, the echo variety <?= /*...*/ ?> is permanently enabled regardless of the short_open_tag setting.
What's changed?
Even if they were previously discouraged solely due to the unpredictable nature of whether short_open_tag is enabled on a shared hosting platform, surely that argument doesn't go away just because some subset of hosts will be running PHP 5.4?
Arguably this change to the language doesn't inherently signify a change in the recommendation that we should nonetheless avoid "short tags", but if they've gone to the trouble it would certainly seem like the PHP devs no longer "hate" them so much. Right?
The only logical conclusion I can draw at this time is that there must be some objective rationale for the introduction of this change in PHP 5.4.
What is it?
Short open tags are not always enabled since PHP 5.4. The documentation talks about the short echo tags. Which is a different thing. (short open tags are <? style tags, short echo tags are <?= style tags, for echo-ing).
Then why are they enabled by default now? Well, there are a lot of scripts out there, where it benefits to use <?= $somevar ?> instead of <?php echo $somevar ?>. And because the short echo tags aren't as bad as the short open tags, they chose to always enable the short echo tags. Because now developers (of frameworks and CMS-es) can count on them (or rather, when PHP 5.4 becomes mainstream).
However, the short open tags are still influenced by the short_open_tag setting in your php.ini.
Only short echo tag (<?=) is enabled permanently, not short open tags (<?).
It's because short echo tag is really handy when you're creating HTML templates (or any other view templates) and without that you have to write a lot more (like <?php echo $var; ?> instead of just <?= $var ?>).
Note: Starting with PHP 5.4, short echo tag <?= is always recognized and valid, regardless of the short_open_tag setting.
All that this is saying, is that <?= is always valid, and not <?
The reason is that < ? is used in XML documents and enabling short_open_tags will generate errors in XML codes.
But, < ?=, just like < ?php is not XML open tag and is safe to use.

Why doesn't <?php= work? [duplicate]

This question already has answers here:
Are PHP short tags acceptable to use?
(28 answers)
Closed 9 years ago.
If
<?=$var?>
is used only if short tags are enabled, then does that mean the regular version of that is:
<?php=$var?>
The second one doesn't work tho.
<?php= is not valid syntax. Your choices are
Short tags <?=
Long version <?php echo (or print())
If the short tag is enabled you can use <?=$var?>
Which is the shorter version of <?php echo $var ?>
There is nothing called <?php=$var?> You have to replace the = with echo or print()
Because no one else has mentioned this, and after further searching using google... I wanted to post what I think is crucial info.
http://php.net/manual/en/function.echo.php
The following link states:
echo also has a shortcut syntax, where you can immediately follow the
opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax
only works with the short_open_tag configuration setting enabled.
Which is very important, because there isn't a need for <?php= because <?= will work if short tags are disabled or enabled for all future versions of PHP!
This is very important as the use of all other short tags is considered futile. Anyway the use of the short echo tag is encouraged from now on. It does provide for a smoother and tidier code-base - esp. in view files. So for PHP >= 5.4.0 <?= ?> can be used without setting short_open_tag.
Always check Stack, Google, etc first, and check the PHP.net and read about it.
It's all in the manual clear as day! :)
http://php.net/manual/en/language.basic-syntax.phptags.php
When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them.
PHP also allows for short tags <? and ?> (which are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option.
http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo. Use of this shortcut required short_open_tag to be on. Since PHP 5.4.0, <?= is always available.

What are these PHP tags called?

Instead of <?php print $somevar; ?> you can write <?= $somevar; ?>.
The reason I ask is that my php configuration does not seem to be evaluating these and I need to know the name so I can change php.ini.
They are called short-tags
Oh, and keep in mind, short tags have been said to be deprecated in 6 and will be removed later (I haven't been able to find a working link, sorry)... There's some distention among the community about this, so time will tell...
Short open tags
As others said, they're called short_open_tags . Relying on these is considered "bad" because it depends on a INI setting and affects your code's portability.
Open or short tags
; 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.
short_open_tag = On
short_open_tags = On or Off in php.ini

<? or <?php --- is there any difference? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Are PHP short tags acceptable to use?
What does “<?=” mean when seen in PHP
is there any difference in using <? ?> to signify a php block, or using <?php ?> ?
if there is not, why would anyone use <?php ?
figure the file extension of .php would give plenty of info about what type of code you are looking at.
The first is called short-open tags and second one is safe open and close tags.
You could enable/disable short open tags in php.ini using short_open_tag setting.
The short tags should be avoided, have a look at:
PHP Short Open Tag: Convenient Shortcut or Short Changing Security?
Servers must be configured to also use <?, so it is considered best practice to use <?php for portability reasons.
From the manual ( http://www.php.net/manual/en/language.basic-syntax.phpmode.php ):
There are four different pairs of
opening and closing tags which can be
used in PHP. Two of those,
and ,
are always available. The other two
are short tags and ASP style tags, and
can be turned on and off from the
php.ini configuration file. As such,
while some people find short tags and
ASP style tags convenient, they are
less portable, and generally not
recommended.
<?php can always be used. <? can only be used if the short_open_tag directive is turned on.
short_open_tag tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).
Note: This directive also affects the shorthand <?=, which is identical to <? echo. Use of this shortcut requires short_open_tag to be on.
-- Description of core php.ini directives
As others have mentioned, this directive is often turned off so for portability reasons I prefer using <?php ?>. If this is not an issue, there shouldn't be much difference other than that if the directive is turned on you can also use the <?= shorthand thingy.
I have never personally run into this issue, but support for <? ?> is spotty when moving to different servers. I prefer to just stick to <?php ?> for clarity and consistency.
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.
And also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards.
Always use <?php ?> because <? ?>:
will not work in coming PHP versions
could be mixed with XML definitions. (XML always starts with <?xml ...)
is not enabled on many shared hosting sites.
short tags <? ?> , only work in older php versions.
There is no difference language-wise, but many shop prefer the use of <?php because the simple <? opening tag can be found in XML files, which can lead to confusion for the interpreter.
Edit: I thought this was still an issue: http://terrychay.com/article/short_open_tag.shtml

What is the shortest way of inserting a variable into text with PHP?

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; ?> ?
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 :-)
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.

Categories