PHP if condition error syntax error in server [duplicate] - php

This question already has answers here:
is Short tag good practice in php? [duplicate]
(4 answers)
Are PHP short tags acceptable to use?
(28 answers)
How to set short tag(<?) in PHP?
(6 answers)
Closed 3 years ago.
I have used one if condition and i'm using opencart platform
code is working fine in my local but not in the server
<? if ($a == '1') : ?>
// if something
<? else: ?>
// else something
<? endif; ?>
this code giving me an error in my server
i'm using php version 7.2 in both my local and server

Make sure your code to be like this
<?php if ($a == '1') : ?>
// if something
<?php else: ?>
// else something
<?php endif; ?>

short tags are deprecated. short tags depend on an INI directive and as such are non-portable.
If you enable short tags on server then you will not see error.
It is recommended to use if else statemens without short tags.
for more detail about short tags https://wiki.php.net/rfc/deprecate_php_short_tags

Related

php interpretation stops at > sign [duplicate]

This question already has answers here:
<? ?> tags not working in php 5.3.1
(5 answers)
Closed 2 years ago.
I'm trying to install a site locally. Using XAMPP 3.2.3 and php 5.6.
I have an index2.php file which looks like this:
<?
if (2>1)
{
echo 'aa';
}
I open http://localhost/index2.php and see:
1) { echo 'aa'; }
somehow the ">" is interpreted as the end of the php code. How can I make it interpret '>" signs in condition expressions properly? I.e. so the output is
aa
You need to use <?php, not <?, as your script block start.
<? is an XML thing, not a PHP thing.
The <?= syntax is shorthand for <?php echo and is always legal.
But the <? syntax (without the =) is only supported when short_open_tag (in php.ini) is enabled, but this is an obsolete option that is no-longer supported.

php variable not found in other php block [duplicate]

This question already has answers here:
Are PHP short tags acceptable to use?
(28 answers)
Closed 5 years ago.
After migrate a webpage to a new server (with more updated php-server), I have problems in some pages. These pages uses some different php blocks:
<?
$result = 1;
?>
some html code...
<?php
$result2 = $result;
?>
This works before the migration, but now the $result in the second part is reported as undefined variable in the apache2 error log.
How can I make that variables still are defined in second part?
Make sure to use long tags for php: <? needs to be: <?php
Either that, or enable short_open_tag in php.ini

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

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.

PHP ignoring <?= $var ?> blocks [duplicate]

This question already has answers here:
<? ?> tags not working in php 5.3.1
(5 answers)
Closed 9 years ago.
Note: I'm using PHP 5.3.3 on a CentOS 6 server.
I'm testing out a new web host and I've discovered that it is simply ignoring the <?= $var ?> blocks of PHP code. For instance, if I have this in a PHP file:
<div id='<?=$page_id?>'>
Then it does not display the value of $page_id, it writes it just as you see there. On the other hand, if I write:
<div id='<?php echo $page_id; ?>'>
Then it displays the variable as it should. On its own, this isn't a big deal. Unfortunately I have a TONNE of inherited code that uses the <?= $var ?> syntax all over the place, so I'd like to avoid having to change it all.
Does anyone know what setting I have to change in order for PHP to recognize this syntax? Our old server was running PHP 5.3.14 and it worked fine.
You need to enable short tags.

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.

Categories