Parse error: syntax error, Unexpected '' (T_ENCAPSED_AND_WHITESPACE)

Asked

Viewed 348 times

0

I have this Function:

public function showCredentialsPhoto(){
        if (isset($_SESSION["logado"]) && $_SESSION["logado"] === TRUE && (!empty($_SESSION['avatar']))):
            echo "<img class='nav-user-photo' src='<?php echo $_SESSION['avatar'];?>'/>";
        else: 
            echo "<img class='nav-user-photo' src='galeria/sistema/user.png'>";
        endif;
    }

But when I call she makes a mistake:

( ! ) Parse error: syntax error, Unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting Identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C: wamp class.User.php on line 196

Why? How can I fix this?

The line 196 is this:

echo "<img class='nav-user-photo' src='<?php echo $_SESSION['avatar'];?>'/>";
  • 1

    You have a PHP code in a string in PHP? PHP Inception.

  • 1

    PHP what? Inception?? @Andersoncarloswoss

  • It’s a php code on a php page. All my code is this

  • Still having the bug @Marceloboni. Same

  • the Inception was a joke referring to the film Inception, that people enter the dream dream of the dream... I hope you didn’t misunderstand and see if the answer was clear enough.

2 answers

3

The problem is basically using a PHP code within a text in PHP. You set the text in double quotes and this tells PHP to interpret your content. What does that mean? That if there is a PHP variable inside string it will try to replace it with its respective value. You did:

echo "<img class='nav-user-photo' src='<?php echo $_SESSION['avatar'];?>'/>";

And $_SESSION is a PHP variable, so it tries to replace with its value. In this case, the value would be a array, which already complicates in replacing the value of a array within a string, but soon after you use ['avatar'], in order to inform the value of the array that you want. In this case, the syntax becomes invalid, PHP cannot correctly interpret and triggers the cited error. To do so, when you need to access a certain position of a array within a string, you will need to do this between keys ({}). In this way, you tell PHP that what is inside the keys must be analyzed together and thus PHP will understand that what you need is to access the key avatar of array $_SESSION.

The right thing would be:

echo "<img class='nav-user-photo' src='{$_SESSION['avatar']}'/>";

Note that the PHP code inside the string is unnecessary and will not work. Even if you did not get this wrong, your HTML would be wrong, because the internal PHP code would be sent as text. Either do the above solution using the keys, or you can use the concatenation operator:

echo "<img class='nav-user-photo' src='" . $_SESSION['avatar'] . "'/>";

2

You are incorrectly using string concatenations.

See what you’re doing on the line:

echo "<img class='nav-user-photo' src='<?php echo $_SESSION['avatar'];?>'/>";

Within the echo you are opening a tag PHP to give another echo, when you should do something like:

echo "<img class='nav-user-photo' src=" . $_SESSION['avatar'] . "/>";

Or

echo "<img class='nav-user-photo' src='{$_SESSION['avatar']}'/>";

Browser other questions tagged

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