0

I just narrowed down one of these strange errors that just occurs at certain environments.

The code below parses ok on my linux php 5.3.6 default install, on windows php 5.4.3 default install but fails on some of the windows machines we have with Windows php 5.3.6.

The message we get is:

Parse error: syntax error, unexpected '}' in C:\bogus.php on line 7

The line 7 is the "}".

I can only think this error is caused by a php.ini setting. Does anybody know what the problem is?

<?php
if(!empty($data['foo'])) {
    ?>
    <div>
        <?=$data['bar'];if(!empty($data['foo'])) {?> (Foo: <?=$data['foo'];?>) <?php }?></div>
    <?php
}
?>      
3
  • 2
    try remove short tags in environments where problem occurs Commented May 22, 2012 at 17:10
  • This works like a charm for me. Do you have short_open_tags enabled in php.ini? Also you are mixing multiple statements within the <?= Commented May 22, 2012 at 17:11
  • I did not have it enabled. Now it works if I enable it. Is it good practice (I am new to php) to have it enabled? Commented May 22, 2012 at 17:17

3 Answers 3

4

This looks strange:

<?=$data['bar'];if(!empty($data['foo'])) {?>

You are mixing <?= with multiple statements. I would try to avoid the short tag here if I were you.

But regardless, I think this means that the windows machines does NOT have short_open_tags enabled in php.ini. Enable it and your problems will probably go away.

Sign up to request clarification or add additional context in comments.

2 Comments

Probably because: 1) You have short_open_tags enabled on your Linux machines, and 2) this tag is always enabled in PHP 5.4, regardless of configuration.
Thank you very much! That was really it. Life savior :-)
2

I would change all those {...} to make your life a bit easier:

<?php if(!empty($data['foo'])):?>
    <div>
        <?php echo $data['bar'];
         if(!empty($data['foo'])):?> 
              (Foo: <?php echo $data['foo']?>) 
        <?php endif?>
    </div>
<?php endif?>      

Comments

1
<?php
if(!empty($data['foo'])) {
    echo '<div>'.$data['bar'];if(!empty($data['foo']))echo 'Foo:'.$data['foo']; 
echo '</div>';
}
?>

Use echo function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.