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

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?

Related

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.

Is it now safe to use PHP echo short tags?

Is it now totally and completely safe to use
<?=$var ?>
instead of
<?php echo $var; ?>
I wouldn't use the words "totally" and "completely", but with PHP5.4 the "short-open-and-echo"-syntax is part of the core and thus always available. Remind, that I only talk about <?= ?> and not the "regular" short-open-tags <? ?>.
Yes. As of PHP 5.4.0 from 01-Mar-2012 you can use short tag. From php 5.4 change log,
<?= is now always available regardless of the short_open_tag setting.
This was a General improvement.
So if you have PHP 5.4 you can use <?= syntax.
Yes. There is no real issue with using <?=$var?>, but if you want to be totally prepared for a host that doesn't have this enabled, then you may want to write it using full statement.
Typically, you can enable this feature even if it's disabled.
It's better to always use the regular <?php tag. That way you are sure that your scripts are always compatible with any PHP installation, regardless of the PHP version or php.ini settings.
Even more important if you are developing code that's meant to be shared, such as a library.
ref: http://php.net/ChangeLog.php#5.4.32
"short open tag is now always available regardless of the short_open_tagsetting"
Yes. As of PHP 5.4, echo tags are always enabled unless your host disables them.

Apart from it being lazy is there a good reason not use <?= to echo PHP variables? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between the PHP open tags “<?=” and “<?php”/“<?”?
Rather than type:
<?php echo $foo; ?>
I have seen it written
<?= $foo; ?>
But I've often wondered what the risk/impracticalities are of doing it? Just curious. Thanks!
If you happen to move the code to an environment where short_open_tag isn't enabled, you'll be exposing a lot of internal variable names (security issue) and have a whole lot of damaged output.
The other downside is that the same setting that allows usage of <?= is the same that lets you open PHP tags with just <?, so having it disabled would not only expose those specific variables you were attempting to display, but also display any PHP code within short tags.
The second option increases the readability. The first ensures portability to other systems.
Other than that, there is no difference at all...
It's not portable. There's a pre-5.4 configuration setting to turn it off, so if you move your script to a site where it is disabled, it would break
Also, it's less explicit in my opinion. The difference between <?= func() ?> and <? func(); ?> is easy to miss, but important
You can only use <? and <?= if short tags are enabled when you are running PHP. The actual reason not to use it is because it's incompatible with an xml declaration. If you are trying to output xml with a php extension and you have short tags enabled, you have to do something like <<??>? .. I suppose you can just echo a string.
I don't understand all that portability talk.
There is ALWAYS a portability issue.
There can be no apache - so, don't use mod_rewrite.
There can be no PDO - so, don't use prepared statements.
There can be no mysql - so, don't use complex queries.
There can be no PHP - so, plain HTML is most compatible format, never use anything else because of portability issues!

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

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!

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

Categories