What's the difference between <?php ?> and <? ?> - php

I can't find the answer anywhere. Thanks!

Basically,
<? ?> are short tags. However, not every php installation has short tags enabled. Therefore, even though is faster to type than the normal tags (<?php ?>), it may not work if you move your code to another server.
Are PHP short tags acceptable to use?
EDIT:
Also, if you're using xml in your web page, you might run into conflicts, as writing <?xml version="1.0"?> will make you run into a PHP error, as xml version="1.0" isn't PHP!
If you're using XML and PHP you may need to <?php echo "<?xml version=\"1.0\""; ?>

In your php.ini, if you want <? and ?> to work, you need to turn on
"short tags".
However, it is better to write long-tag compliant code in the first place.
Acutally is no difference.

They both mean the same, with the difference that the short form <? ?> is not always supported/enabled.

There acutally is no difference between the two, the second one is bascially just a shorthand. I personally would recommend using the longer version, because on some systems, the second possibilty is disabled in the php.ini (see short_open_tags).

It is always better to use <?php ?> as on some installations of php <? ?> is not supported! If this happens your code will not work!

Related

Omitting the word php for opening tags

I mean:
<?php echo "hello"; ?>
against:
<? echo "hello"; ?>
(without the php).
It works right, but is there any consequence/bad programming practice?? There is no problem?? Is everything OK? What can happen?
Paraphrased from the PHP manual:
<?php should always work.
<? only works if short_open_tag = On in php.ini.
<?= (equivalent to <?php echo) should always work in PHP 5.4 and newer. In PHP 5.3 and older, it does require short_open_tag = On.
So if you are writing a PHP app for others to use, it is best to stick with <?php (and <?php echo if you decide to support PHP 5.3 or older). If you can control the server's configuration, you can use <? if you want to.
Use, for example, <?php echo '<?xml version="1.0"?>'; ?> if you have to output an XML prolog or processing instruction manually (as opposed to using PHP's XML functions to do it).
It works right, but is there any consequence/bad programming
practice?? there is no problem?? is everything ok? what can it happen?
Well, not every hosting allows short-open-tag. So <?php is a better option if your script target large audience. Or you often change your hosting.
if short_open_tag is open, the short form (<? ?>) will work.
anyway the long form is a standard.

php short_open_tag problem

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.

executing php script in console

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

<? 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

Difference between <?php echo $session_id ?> and <?= $session_id ?>

Is there any particular reason to use one over the other? I personally tend to use the latter, as it just seems to flow better to me.
They do the same thing, the <?= is just called the short tag and is shorthand for <?php echo. You have to make sure the short tags are enabled to use the <?= notation.
As far as I know, they are functionally equivalent except the second can be disabled in configurations so isn't as portable.
short_open_tag boolean
Tells 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 inline.
Otherwise, you can print it with PHP,
for example: . Also if disabled,
you must use the long form of the PHP
open tag ( ).
Note: This directive also affects the
shorthand
Source.
The shorthand is clearer. It says, with as few words possible:
"Here, an expressions is echoed and
nothing else is going on."
Short tags are disabled on a significant amount of php installation so I never use
<?=$my_var?> // Bad Portability
<?php echo $my_var; ?> // Good Portability!
I would assume that <?php= $session_id; ?> works fine, and does not have the issue of portability.
Just an addon question. I've read a few years ago that using <? and ?> is not recommended due to security issues. Is this correct?

Categories