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; ?>
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
How to read php
i'm learning php, I wrote this. (It is from a book)
<?php if(!empty($data)): ?>
<ul>
<?php foreach ($data as $dataprint): ?>
<li><?= $dataprint ?></li>
<?php endforeach ?>
</ul>
I am unable to understand some of the code in this section.
1. Why are there colons on line 1 and 3?
2. What does the '<?= $dataprint ?> ) and why does it not have the standard 'PHP' word?
3. why is there an equal mark in the next?
colons - this is a shorthand version of statements, you should not really learn them at such an early stage. First you need to learn the full versions.
<?= $variable ?> is a short version of <?php echo $variable ?> but means exactly the same.
Also, the shordhand versions from question one are actually not considered a good practice, because when you nest them they are hard to read.
Colons on lines 1 and 3 are an alternative way of doing the below code
if (!empty($data)) {
...
}
It makes it a lot cleaner when outputting HTML.
(See http://php.net/manual/en/control-structures.alternative-syntax.php)
<?= is a shorthand way of writing <?php echo, again it's just an alternative way of doing things.
(See http://php.net/manual/en/function.echo.php)
You can write loops in php many type...
1)eg: if(condition){...code...}else{...code...} or if(condition): means, in place of "{" you can use ":" and for ending the loop use "endif"
2) Your point no 2 is written in shortcode in php for that you have to enabled shortcode in php.ini
This question already has answers here:
Difference between <?php and <?
(7 answers)
Closed 6 years ago.
I've got some old legacy code written in PHP 5.3.
All PHP blocks in this code are like
<? some_php_code_here ?>
and my Apache just ignores them, while works good with
<?php some_php_code_here ?>
Why old code contains invalid blocks, and why this old code works good on old server ? It's really hard to google something with so much special symbols, sorry for stupid question
<? is called a php short tag
Here is what the manual has to say about it
PHP also allows for short open tag <? (which is discouraged since it
is only available if enabled using the short_open_tag php.ini
configuration file directive, or if PHP was configured with the
--enable-short-tags option).
You can enable them at runtime with <?php ini_set('short_open_tag',1) ?>
But if you do so you won't be able to use inline xml like so <?xml ?>
and instead you can use
<?php echo '<?xml version="1.0"?>'; ?>
As a side note I do often use:
<?= "Hello world" ?>
<?= 'foo' ?> is equivalent to <?php echo 'foo' ?> and looks much cleaner in template files in my opinion
Since php version 5.4, <?= is no longer tied to short_open_tag but prior to that it was, so care should be taken if you might be running your code on older php versions
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.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
Previous I bumped into codes like this:
<?php
$var="hello";
?>
<?=$var?>
It simply prints out the content of $var, so... is the syntax equivalent to echo $var?
I'll also appreciate an answer pointing to a related manual page. Since the syntax is not searchable.
Yes, <?=$var?> is the same as <?php echo $var; ?>
From PHP.net manual: 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.
You can read more here.
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.