Difference between <?php and <? - php

I am new to php and would like to know if there are any differences between these server tags :
<?php
?>
and
<?
?>

The first is a safe open and close tag variation, the second is the so called short-open tag. The second one is not always available, use the first option if it's possible.
You could check the availability of short open tags in php.ini, at the short_open_tag.

The problem with short open tags is that the following:
<?xml version="1.0" ?>
will cause problems if you're allowed to use short tags (i.e. <? and ?>). <?php is less open to misinterpretation.
Whether or not you're allowed to use short tags is defined by the ini directive short_open_tag.

Also I think shorttags are being removed in one of the upcomming releases.
Edit: I was wrong.
Farewell <% They will remove support
for the ASP style tags, but the PHP
short-code tag will remain - so
to those on php general who reckon the
short-tag is 'depreceated' - hah! ;)
http://phpmysqldev.blogspot.com/2007/05/php-6.html

Nothing AFAIK, however I have had servers (shared) where the settings do not support shorthand tags <? ?>, so I usually stick with the <?php ?> for good measure.

There is no difference.
The ability to use <? ?> is defined in your php.ini file - usually accessed only by the server host.
You can find more information here

Note short_open_tag = Off did not effect the <?= shorthand tag, which is equivalent to <?php echo

As #erenon explained here: https://stackoverflow.com/a/1808372/1961535
The difference is that short_open_tag in some cases isnt available. You can check the status by accessing the php.ini file but in case of shared hosting server, the host does not always allow edits to php.ini file.
You can easily print the phpinfo as explained here:
https://www.php.net/manual/en/function.phpinfo.php
phpinfo();
search for short_open_tag as shown below
It is always better to use full tag because that will always be supported in every version of PHP weather old file or new.

Related

What is the correct way to comment out a PHP variable being echoed using shorthand notation?

I have recently started using the PHP shorthand <?= ?> tags to echo variables etc in my PHP scripts. However I find if I want to then comment out the variable, e.g. <?= //$myVariable; ?> i get a syntax error.
Is it safe to just do this: <?//= $myVariable; ?>
Many Thanks!
The short tag
<?= ... ?>
is translated into
<?php echo ...; ?>
So to comment it out, you have to put something into ... that always shows up as empty. Here's the shortest I can come up with:
<?= false && ... ?>
This works because echo false echoes nothing.
There's no documentation supporting it, so it might be an old compatibility hack, but the following seem to work:
<?//= ... ?>
and
<?/*= ... */?>
Since they're undocumented, I wouldn't depend on them for anything important, but you could use them if you're just temporarily commenting something out while debugging.
So on the question of why <?/*=...*/?> and <?//=...?> work is because there's a PHP feature called short_open_tag, that lets you skip putting php after <? and just go with something like <? echo ...; ?>. It can be disabled in the INI file, and before v5.4 the short hand wouldn't work unless it was enabled. So as long as you're in control of your INI file you should be OK.
BUT, I just checked our 5.6.31 system (it's old yes), but when the short_open_tag is set to false the <? ... ?> get emitted directly to the client as if it were HTML text. So, it may be that you just aren't seeing the text because the browser isn't rendering it.

Unexpected end of file error with valid code

<?php if (arg(1) == 40): ?>
<?php
$block = module_invoke('views', 'block_view', 'home_rotator-block');
print render($block['content']);
?>
<? endif; ?>
For some reason this code is causing causing an unexpected end of file error pointing to the end of the file. I know that this code is valid because it works on my other server. Does anyone have any ideas why this might be throwing the error?
Note: I know it can be formatted differently(without the inside the if statement.
I get the same error when I comment out the $block line and the print line.
Short PHP tags may not be supported on the new server. Instead of using the short tags <? and ?>, use the full ones: <?php and ?>
Or, if you want to enable that on your new server, just change the directive in your php.ini file:
short_open_tag=On
But however, <?php is the official standard and I recommend you use it everywhere so you won't have to change it every time you switch between servers.
You're using short tags for the last line. This could very well not be enabled on your server. Check php.ini to be sure.
Maximus2012 was able to answer the question. I had to to change <? to <?php.
Thanks Maximus2012

php block start with <?php= instead of <?php

Hi All
I download a free chat script that in it's php files all of php code blocks start with <?php= instead of <?php or <? for print variables and constants that causes some problems and show error messages .
i want to know that how to solve this problem for php script work correctly
in your server's php config (php.ini file)
add
short_open_tag = On
At my understanding this <?php= is wrong..
Your currently problem is not the short open tags <?, but this code
<?=
means you are doing exactly this
<?php echo $someVar;
so all you have to do is change for the correct syntax.

xampp for Windows

Why my localhosted files appear like this?
http://109.236.82.36/upload/
Everything is undefined, the defined variables aren't working.
Am I supposed to enable a plugin in xampp to get it to work?
PHP short tags must be disabled. If you check the source of your generated page, you'll notice that the img tags have src values like
<?=$website?>/images/hitpoints.gif
You have two options here. Either change the code to
<?php echo $website ?>/images/hitpoints.gif
or enable short tags in php.ini. You can get help on the second option using google.
I strongly suggest the first one as short tags are going to be chucked in future versions of PHP incompatible with XML documents and make code less portable.

Tag <? Misbehaving in PHP

I'm working with a legacy code that someone had left, and it happens to be my task to re-deploy the code. I'm using Windows Server 2003, Apache 2.0.63 and PHP 5.2.10.
It doesn't work. At least, not in the way I had expected it to work. Call it bugs, if you will.
Upon inspecting, I had a suspicion that this code (which appears numerous times in the application) is the culprit.
&$this->
To illustrate the problem, I reproduce this code:
<?php
phpinfo();
//$variable = &$this->request;
?>
The code above executed beautifully and as expected. However, if I change the code to:
<?
phpinfo();
//$variable = &$this->request;
?>
The code misbehaves, and produce this result on the screen instead, which of course, totally unwanted and unexpected.
request; ?>
Now, the code is littered with the similar code as above, and as such, the application now produce output on the screen similar to this one:
request; $user = &$this->user; // This is comment return false; ?>
for a code that reads as:
<?
$request = &$this->request;
$user = &$this->user;
// This is comment
return false;
?>
I had tried to change every <? with <?php whenever &$this-> rears its ugly head, but most of the time, it introduces a new error instead.
I reinstalled PHP and Apache, even using another version of PHP (5.2.6) and it still won't work. I deployed the code in my localhost (Mac OS X, PHP 5.2.8 and Apache 2.0.63) and it worked without a hassle.
Please, anyone, any enlightenment will more than suffice.
In your php.ini, you need to set the following directive:
short_open_tag = On
From the manual:
Tells whether the short form (<? ?>)
of PHP's open tag should be allowed...
If you have time on your hands, you may want to consider replacing all those short tags '<?' with the full-form ones <?php, for better portability (see what just happened to you? :)
my recommendation is not to use open tags, because it can interfere with <?xml codes.
I also had that problem before, just go and replace all <?php to <? , and then again all <? to <?php.
In this way you won't get any
<? <-- one space after the '?'
and
<?php <-- one space after the 'p'
hope this will help...
If you want to use the short tags:
("<?")
they need to be enabled in you php.ini. See this link.

Categories