Parse error: syntax error, Unexpected {

Asked

Viewed 406 times

3

I have a PHP file to check if SESSION is defined:

<?php

session_start();
if(isset($_SESSION['user']){
   echo "<script>document.location.href='../areaprivada.html';</script>";
   }
 else{
    echo "<script>document.location.href='login.php';</script>";*/
}
?>

But when I run it makes that mistake:

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\exercicios\saber2\php\session.php on line 4

PS: Line 4 is the one that closes the if.

  • 1

    remove the characters */ after the command of else will probably fix the problem..

3 answers

5


Missing close the Parentheses () and remove the tag */ closing remarks

<?php
session_start();
if(isset($_SESSION['user'])){
   echo "<script>document.location.href='../areaprivada.html';</script>";
} else {
    echo "<script>document.location.href='login.php';</script>";
}

4

You already have the answer to your problem, but since no one has explained how the error should be read, here is my contribution so that in the future you can understand what PHP is saying:

Parse error: syntax error, Unexpected '{' in C: xampp htdocs exercicios saber2 php Session.php on line 4

The error is divided into 3 portions:

  • Type of error:

    You got a Parse error, family E_PARSE which are basically mistakes that will prevent the execution from continuing.

  • Error:

    You got syntax error which indicates that something is wrong with the PHP code whose interpreter cannot understand.

  • Description:

    The description of your error identifies the problem, the line and the file where the error is located.

    In your case, unexpected '{' on the line 4 of file C:\xampp\htdocs\exercicios\saber2\php\session.php, that is, was found a { on line 4 of said file, when something else was expected.

Once the error points to an unexpected character, the real problem is before it:

#01 <?php
#02
#03 session_start();
#04 if(isset($_SESSION['user']){
#05   echo "<script>document.location.href='../areaprivada.html';</script>";
#06   }
#07 else{
#08    echo "<script>document.location.href='login.php';</script>";*/
#09 }
#10 ?>

And so, on your #4 line, before the { there should be something else. Analyzing from the beginning of the line to the { we end up realizing that the parentheses of the if with a ).


More about the mistakes and their types:

  • 4

    Very good your answer! There should be more people giving answers like that. For anyone gets to understand what was wrong. I am grateful that time is needed to prepare a response with head, torso and limbs for a better understanding of the situation.

  • @Brunogibellino doesn’t need to do anything, but if you think you should, it’s possible to exchange the acceptance of an answer for another one that you think was better. Did you know that? The choice criterion is yours. I always choose the best answer.

2

It should stay this way by closing the key to the else and the parenthesis of if:

<?php
session_start();
if(isset($_SESSION['user'])){
   echo "<script>document.location.href='../areaprivada.html';</script>";
} else {
    echo "<script>document.location.href='login.php';</script>";
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.