This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am having a problem with putting partial bits of PHP code into an include file. Essentially, the situation is like in the following example:
<?php
$x=1;
if ($x==1) { echo 'test'; }
?>
This yields test.
I now replace this with
<?php
$x=1;
if ($x==1) { include('test_incl.php') ;
?>
with test_incl.php
<?php echo 'test'; } ?>
As the include file just adds the code back on that was taken out in the original code, I would expect it to work as well, but instead I am getting "Parse error: syntax error, unexpected end of file".
Any clues what is going on here?
A further explanation:
I need to put a conditional around a block of code in one language version of our shopping cart but not others. So my idea was to include the PHP files where the opening and closing part of the conditionals should be, which would keep the core code for all versions identical, with only the includes differing (this how the language specific features are dealt with anyway for this shopping cart).
From what I have read, the PHP engine parses the includes in the flow of the code, so I don't understand why I get the end of file error (which suggests that the include has actually not been parsed yet when the end of the parent code is reached.
Also, I have used this kind of method successfully before with Javascript/HTML includes.
You can't include opening and closing tags of the if statement like that in separate files, so when your main file gets to the end of the file the if statement is still open, hence the unexpected eof.
To fix this you need to move the closing brace of the if statement back into the original file. Your files should look like this:
<?php
$x=1;
if ($x==1) { include('test_incl.php'); }
?>
and test_incl.php:
<?php echo 'test'; ?>
Try this:
<?php
$x=1;
if ($x==1) {
include('test_incl.php');
}
?>
and
<?php echo 'test';?>
Related
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.
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
This question already has answers here:
Why is the semicolon optional in the last statement in php?
(3 answers)
Closed 6 years ago.
Today I have a little weird question.
Should I end my PHP script with exit or exit; and which is correct?.
Without ;
and
With ;
Thanks.
exit, exit;, exit(), and exit(); are exactly the same, but the fact is that you should not end your script with an exit() call, it's just bad practice (but it works).
EDIT
Clarifying why i said it's "bad practice", it's not about the functionality of "exit" itself, it's OK to halt the script execution if something bad happens, but the concept of "something bad" is really wide. In general, even if some unwanted condition occurs, the normal execution flow should reach to the end of the file. Just consider this example:
...some init stuff...
if (!user_is_authenticated) {
...print some nasty message...
exit();
}
...continue with normal stuff...
A better approach would be:
...some init stuff...
if (user_is_authenticated) {
...continue with normal stuff...
}
else {
...print some nasty message...
}
What's the difference? The difference is that in the second version you don't need to use exit(), which means if one day you need to do something after BOTH the "normal" and "unwanted" execution flows you can just add it at the end of the file.
It's more or less the same argument about why you should NOT use "return" in function bodies except at the end of the function..
It is allways a good practise to use semicolon
<?php
//exit program normally
exit;
exit();
exit(0);
//exit with an error code
exit(1);
exit(0376); //octal
?>
Like Johnny said, all those four forms of 'exit' statement work. Both exit and exit() work if there is no statements followed by any of these two language constructs. It doesn't work if there is any other PHP statements followed by these two constructs and semicolon's purpose is to end the statement to proceed to the next statement, therefore you need to use exit; or exit(); to avoid syntax error and also, in this case, without using semicolon is considered to be a bad practice in the coding conventions.
This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 6 years ago.
Should I close PHP tags after die or exit functions or it is not necessary?
You'd still need to make sure your PHP is syntactically valid, even if the script might exit mid-point somewhere.
e.g. this will not work:
<?php
if (true) {
die();
}
<html>
would not valid, because the PHP parser will barf on <html> - you're still in PHP mode, and <html> is not valid PHP code.
The script execution would never reach the tag, so theoretically it shouldn't matter that you don't have ?>, but the parser doesn't check if something is logically reachable, it just checks the raw syntax.
According to the docs, only omit the closing tag if it's at the end of the file:
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
-- http://php.net/manual/en/language.basic-syntax.phptags.php
This applies to exit and die calls as well. They would run through the same parser as the rest of your php code.
This question already has an answer here:
How can we include php tag in html with php echo [closed]
(1 answer)
Closed 10 years ago.
I already post this question before but people always ask unnecessary question. I'm gonna explain it in a simple way.
I HAVE 3 files :
a php file (contains only html, thats important) : We call it X file for the example.
a php file where there's some database query to insert database data on the screen : Y file
a php file (a script that will make some manipulations) : Z file
SO, i want to include Y into X with the script of Z.
In Z, i make a str_replace($text, $new, file_get_contents($file));
The ONLY THING is that i need to include PHP open and close TAGS in X because there's no php tags in it.
So, $new = "<?php include('Y.php'); ?>";.
If you try, the close tag wont be considered in the string, but that's what i want.
Hope this question is NOW clear. I can't be more clearer than that. :D
Thanks for you advice.
You need to replace the < and > characters with their HTML entities:
<?php echo "<?php include('this.php'); ?>"; ?>
If you want to include the file, there's no need for the above, running the code below is more than sufficient:
<?php
include('this.php');
?>
You don't have to use two lots of PHP tags...
I may as well put this as an answer.
If you don't want to manually replace < and >:
<?php echo htmlentities("<?php include('this.php'); ?>"); ?>