0

could someone take a look at the code below and tell me why after transfering it from one server to another I suddenly get syntax error, unexpected '}' (in below example it would be in line 3)

<? if (isset ($_GET['ppp'])){ ?>    
<div style="text-align:center;">div1</div>
<?php } else {?>
<div style="text-align:center;">div2</div>
<?php } ?>

Could it be possibly related to some php.ini setting? Why simply transfering the template to another server would give such mix of php and html an error in that place? Works on one server but does not on another.

Greg Viv
  • 817
  • 1
  • 8
  • 22

2 Answers2

1

You might be moving from a server with very old PHP to newer version of PHP. In newer versions you always have to start with

<?php 
if (isset ($_GET['ppp'])){ 
?>    
<div style="text-align:center;">div1</div>
<?php 
} else {
?>
<div style="text-align:center;">div2</div>
<?php 
} 
?>
Nawed Khan
  • 4,393
  • 1
  • 10
  • 22
  • aaaa I've tottaly missed that. You're partially right - it's was not about php versions cause that was the same but essentially one server had short tags enabled and another did not - as in: https://stackoverflow.com/questions/2185320/how-to-enable-php-short-tags – Greg Viv Dec 10 '18 at 21:41
  • yeah I did not know the name of the feature in PHP. I had this issue once and just using full tag solved it. – Nawed Khan Dec 10 '18 at 21:46
1

My best guess given what you posted is this:

Make sure short tags are on on your new server, you can test it by changing the tags <? to the full PHP tags <?php. What happens is the first block of PHP starting with <? is ignored then the next block with the <?php starts with a } which is invalid (excluding the first block).

This is what the server sees:

 <? if (isset ($_GET['ppp'])){ ?> //------- this is just text
 <div style="text-align:center;">div1</div>
 <?php } else {?> //----------------------- PHP code starts here with the }
 <div style="text-align:center;">div2</div>
 <?php } ?>

The easy way to test it is this:

 <?php if (isset ($_GET['ppp'])){ ?>    
 <div style="text-align:center;">div1</div>
 <?php } else {?>
 <div style="text-align:center;">div2</div>
 <?php } ?>

If that solves the issue, then you know short tags are off on this server. You could also look at the php.ini (i suppose).

As a general rule I never use short tags, although the <?= is tempting sometimes .... lol

Cheers!

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • That is full and correct answer. It was all about short tag settings as explained in here: https://stackoverflow.com/questions/2185320/how-to-enable-php-short-tags once I turn them on on both servers it started working ok. Sorry for the duplicate but I woudn't know to search it under "short tag php" keyword untill the resolution hit me. – Greg Viv Dec 10 '18 at 21:44