0

Now I am trying to set up a local mirror of a website. I have whole database and source code of this website. The website runs smoothly in the server, but when I try to visit login.php of the website locally, I got the parse error: syntax error, unexpected '}'. I checked source code of login.php, and there should be no problem with syntax. I also tried different versions of php, but got the same syntax error again and again. Can anybody help me find where the problem is?

The source code of php part in login.php:

<?php
if ($form->Value("email") != "")
    ?>
<script>document.getElementById('email').value = "<?php echo $form->Value("email"); ?>"</script>
<?
if ($form->Value("pass") != "")
    ?>
<script>document.getElementById('pass').value = "<?php echo $form->Value("pass"); ?>"</script>
<?
$fields = array("email", "pass");
for ($i=0 ; $i<count($fields) ; $i++) {
    if ($form->Error($fields[$i]) != "") {
        ?>
<script>DisplayMsg(document.getElementById("<?php echo $fields[$i]; ?>"), "<?php echo $form->Error($fields[$i]); ?>", false);</script>
<?php
    }//Parse error here: syntax error, unexpected '}' 
    else {
        ?>
<script>DisplayMsg(document.getElementById("<?php echo $fields[$i]; ?>"), "", true);</script>
<?php
    }
}

?>

I am using XAMPP to set up my local environment. I am new to php, let me know if I need to post more information

hpPlayer
  • 61
  • 6

3 Answers3

1

The way you put php code inside a html page is not so readable. You could try this format:

<?php for ($i=0 ; $i<count($fields); $i++): ?>
    <?php if ($form->Error($fields[$i]) != ""): ?>
        <script>DisplayMsg(document.getElementById("<?php echo $fields[$i]; ?>"), "<?php echo $form->Error($fields[$i]); ?>", false);</script>
    <?php endif; ?>
<?php endfor; ?>

This will also avoid having issues with curly braces.

Jake Opena
  • 1,475
  • 1
  • 11
  • 18
1

This is likely because short_open_tag is disabled when you test locally. This setting controls whether or not <? ?> among other tags are interpreted as PHP during parsing.

The result is that this block:

<?
$fields = array("email", "pass");
for ($i=0 ; $i<count($fields) ; $i++) {
    if ($form->Error($fields[$i]) != "") {
        ?>

is never executed as PHP.

Consequently, the closing brace closes nothing when used here:

<?php
    }//Parse error here: syntax error, unexpected '}' 
    else {
        ?>

That is why you get the parsing error in one case but not the other.

Anonymous
  • 11,748
  • 6
  • 35
  • 57
1

The syntax error here is due to the opening and closing curly bracket for if block are in different types of opening php tag. For example below will throw similar syntax error:

<?php 
if (TRUE) {
?>
    <script> ..... </script>
<? //Short open tag, differnet from <?php in previous block
}
?>

While this will not throw any syntax error:

<?php 
if (TRUE) {
?>
    <script> ..... </script>
<?php 
}
?>
Ulver
  • 905
  • 8
  • 13