I just upgraded to php5.3 and when I execute:
php myfile.php
I see the source code for myfile.php.
Any idea why this is happening? I've never seen anything like it before!
In 5.3
short_open_tag
is disabled by default. So if you use
<? ?>
instead of
<?php ?>
it can cause issues in the future (provided you just re enable the flag in php.ini). From what I have read they are still debating removal from 6.
Are you sure you didn't forget the opening PHP tag at the begining of the file ?
ie :
<?php
echo "hello, world!";
(Note I used "full" open tags ; ie "<?php", and not short open tags "<?" -- maybe you used short open tags, and those are disabled ? If I remember correctly, they are disabled by default, in PHP 5.3)
and not :
echo "hello, world!";
(If that doesn't help : can you post a short code sample that reproduces the problem ?)
Related
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
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.
hi i am working on a project of my college but my college server admin told me that we don’t use short_open_tag in our code what’s its mean? what is short open tag and what is alternate of it
thanks
Normally you write PHP like so: <?php PHP CODE HERE ?>. However if allow_short_tags directive is enabled you're able to use: <? PHP CODE HERE ?>. Also sort tags provides extra syntax: <?= $var ?> which is equal to <?php echo $var ?>.
Short tags might seem cool but they're not. They causes only more problems. Oh... and IIRC they'll be removed from PHP6.
You can try the tool at:
http://blog.eezgu.com/php-short-open-tag-sorunu-ve-cozumu.html
This is a PHP command line tool that converts short tags to normal PHP tags.
The page is not English but you can click the link in the post and download the tool.
i'm trying to execute a php in the console, but each time i run it:
php myscript.php
it only outputs the content of the file, it dowsn't run it.
output:
<?
echo 'test';
?>
instead of:
test
What's wrong? I have php installed under c:/program files/php and the environment variable is set.
Thanks,
Dave
Try
<?php
It might be short_open_tag is disabled in your php.ini
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 ?>).
Edit: You might also want to read Are Short Open Tags Acceptable To Use?
Try:
<?php
instead of
<?
(if that works you may need to configure your installation of PHP to enable short tags.)
Don't use short tags. Replace <? with <?php.
use <?php instead of <?
edit Try also the -n flags from the cmd line, it avoids PHP read the ini file where short tag could be disabled
this is my php code:
<html><body>Hey!: <?= "World";?></body></html>
It just prints "Hey!:" Whats wrong with my code?
Short tags (which you're using here) can be turned on or off depending on the server you're running the code on. If it's your server, look in php.ini
You need to set short_open_tag to 1
http://php.net/manual/en/ini.core.php
Does your file end in .php and will it execute as php on your webserver? add
<?php echo "yes I run php!<br>\n"; ?>
to your file to be sure. View source to see what happened to the php tags. Then maybe switch on short tags as the other answers told you to.