Are there different between <?php and <? in PHP? [duplicate] - php

This question already has answers here:
Difference between <?php and <?
(7 answers)
Closed 8 years ago.
Sometimes when I use 'php codes' working with <?php and sometimes working with <? as this code
<?php
if(isset($_GET['url']))
{
echo 'There is a url!';
}
?>
Doesn't work to remove php after and use <?, but sometimes working in another projects!
I want to know there are different between <?php and <? ?

They do the same thing. However, you should use <?php unless you're completely sure that you will have full control over the configuration of your server. I say this because short tags (<?) can be disabled on certain hosts and using short tags can make your code less portable.

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.

Related

How to write php without <?php> [duplicate]

This question already has answers here:
How to enable PHP short tags?
(21 answers)
Closed 5 years ago.
im trying to setup my php server to use
<?
echo 'ok';
?>
syntaxis without 'php' inside opening tag. Now my code works like this
<?php
echo 'ok';
?>
How can i achieve this?
I tried to install latest version of php, but it still not working.
ANSWER:
Set short_open_tag=On in your php.ini and restart the server.
CAVEAT:
Short open tags are not used according to PHP coding standard PSR-1 which states:
PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations.
The reason for this as mentioned by #Magnus Eriksson is that there may be situations where you do not have access to edit the php.ini and therefore code that uses short tags will be rendered unusable in those environments. Adhere to the standard for maximum portability!
You need to edit your php.ini file and turn short_open_tags on
short_open_tag = On

<?php, <?, <?=, whats the difference? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I don't know if this has been asked before, but here goes: What's the difference between <?php, <?=, and <??
I'm just curious, and people have been telling me to use <?php instead of <?, but I just use <? because it works for me and it's faster. I ain't seein' no diff'rence (redneck accent for extra pleasures...).
<?php and <? are equivalent, except it's generally preferred to use <?php since not every server configuration may have the short opening PHP tags option enabled.
<?= is a shortcut for echoing back the value of a variable/expression, i. e.
<?=$var ?>
is similar to
<?php echo $var; ?>

Better to write <?php or <? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are PHP short tags acceptable to use?
So I noticed that you can write PHP programs either like
<?php
// do something
?>
or
<?
// do something
?>
What's the difference and what's the best practice?
I always use
<?php
because in some hosts the short tags are not allowed. This avoids some headaches.
There's no difference at all. Except
<?= $var ?>
that acts like an "echo"
You should use <?php whenever possible, with short_open_tag=off (see php.ini) <? just won't work and code will be treated as html.

PHP without <?php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Are PHP short tags acceptable to use?
How to enable PHP short tags?
I came across some PHP code that is being used on a site. The odd thing is the php code is enclosed like this:
<? ?>
not
<?php ?>
How can I get it to run on my server without the 'php' in there?
take care,
lee
The opening tag <? is known as the short tag. It is not recommended because it requires a certain setting to function on servers. Specifically, you need to enable short_open_tag in your php.ini file.
Configure your server to allow short_open_tags; see the documentation for details.
You can use <? ?> with short_open_tag turned on in php.ini.
Are PHP short tags acceptable to use?

Difference in php syntax of <?php ?> and <? ?> [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
What is the difference between the PHP open tags “<?=” and “<?php”/“<?”?
Reference - What does this symbol mean in PHP?
Is there really any difference in writing the syntax like <?php ?> or as <? ?>.
I was under the impression that it really didn't matter as long as the file had an .php extension. But sometimes I seem to run into problems with the syntax.
so whats the lowdown?
The latter is called shorttags.
You can disable those tags in php's configuration, apart from that there is not really a difference.
Because you can disable them, you should not use them, as they can cause lots of unexpected problems when you happen to run your scripts on servers where you can't enable them. Another reason, as noted by Sjoerd in the comment is, that they can cause conflicts when you have an XML header for instance.
If you enable short tags though, you get a shorthand syntax for the echo function: <?= $string ?>
It's enabled using the short_open_tag setting in php.ini. You can read about it here.
Update
As of PHP 5.4.0 alpha1:
<?= is now always available regardless of the short_tags setting
(Rasmus)
The below will work on all servers.
<?php // some logic in here ?>
<?php echo "cat"; ?>
The below will work on servers that have their php.ini properly configured for short tags.
<? // some logic in here ?>
<?= "cat" ?>
one little dangerous thing about shorttags is that if the php.ini is set incorrectly your sourcecode is available on the internet. so

Categories