Unexpected closing bracket with split php code [duplicate] - php

This question already has answers here:
Are PHP short tags acceptable to use?
(28 answers)
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I'm receiving the following error:
Parse error: syntax error, unexpected '}', expecting end of file in /blah/blah/Edit.php on LINE_WITH_COMMENT
for this code inside my Edit.php view file.
<? $categories = $this->config->loader->getCategories();
foreach($categories as $category) { ?>
<? $selected = $this->obj->category==$category ? 'selected' : ''; ?>
<option value="<?=$category?>" <?=$selected?>><?=$category?></option>
<?php } ?> //ERROR IS HERE
But if I change <?php to the short open tag <?, the error goes away, and my script works as expected.
It works on my server with hostgator with <?php but on my local machine it's only working with <? and giving the fatal error if I have <?php.
From phpinfo() on the local machine: PHP Version 7.2.17-0ubuntu0.19.04.1
I've changed <?php to <? for now, but how can I fix this? I prefer not to use short open tags.

It actually turned out my short_open_tags were set to Off in my php.ini.
So I went to /etc/php/7.2/apache2/ did nano php.ini, found the setting by searching short_open and changed short_open_tags = Off to short_open_tags = On... then I decided to turn it back off because... I've read you're supposed to keep it off. Then I changed my other <?s to <?php.
The problem was that I WAS using short open tags <? for parts of the code, and it was not getting evaluated - rather it was output. And I didn't see it was output because I was looking at the rendered page, rather than the source for the page, and i was only seeing confusing problems in the rendered page

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.

Unexpected end of file on custom Woocommerce template [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I'm coding a custom Woocommerce theme. All works well in local, but when I deploy on my server, this error occurs when I go to a product details:
Parse error: syntax error, unexpected end of file in /wp-content/themes/customtheme/content-single-product.php on line 231
Here's the concerned file code (Pastebin since it's a big amount of code and it display the lines number) : https://pastebin.com/bzgLQTmD
I really don't know where is this error given that I don't have it in local development.
I tried code validators, but they didn't gave me any clues.
The problem is with short tags, which are not enabled by default. So, either enable them from php.ini or
Change all of these:
<? endif; ?>
To Proper full tags like:
<?php endif; ?>

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

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