-2

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.

Thomas
  • 107
  • 2

2 Answers2

8

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'; ?>
Styphon
  • 10,304
  • 9
  • 52
  • 86
  • 4
    Exactly. And even if it did work, that would be horrible. – AbraCadaver Apr 21 '15 at 15:53
  • Not exactly a useful answer. Of course I had figured out already that your suggestion works. The question is WHY the other does not work. If there is a syntax error with including the opening and closing brackets of the conditional from different files, what is the official PHP syntax rule from which that follows? I try do understand the PHP logic here not to find a workaround. – Thomas Apr 21 '15 at 21:56
  • @thomas this isn't a work around, this is how the logic works. PHP simply won't allow you to split conditionals over different files. The reason for this is pretty clear I think, because if you do split conditionals it becomes extremely difficult to follow the code. It would also allow you to break your code easier if you accidentally included an extra close in an include file it would break your main file and be almost impossible to find. If you have a specific reason for needing this in your code then you seriously need to rethink how your application works.. – Styphon Apr 22 '15 at 06:56
-1

Try this:

<?php 
$x=1;
if ($x==1) {
   include('test_incl.php');
}
?>

and

<?php echo 'test';?>
Steph
  • 779
  • 1
  • 8
  • 18