0

Is there a way to echo a variable that is already echoing something, I try doing it this way but its not echoing it out

if (logged_in() === true) {
            echo
            '

<li ><a href="#">',$user_data['username'],'</a>
<ul>
    <li><a href="../social.php">socail</a></li>
    <li><a  href="../my/pictures.php">my pictures</a></li>
    <li><a  href="../profile.php">profile</a></li>
    <li><a href="../logout.php">logout</a></li>
</ul>   ';
        } else {
            include'cpages/cmain/menuforms/formsmenu.php';

        } 
S. Albano
  • 707
  • 7
  • 21
Pablo Lopez
  • 741
  • 1
  • 8
  • 11
  • As @minitech has pointed out, that's actually syntactically correct - are you sure that `$user_data['username']` is set properly? – andrewsi Aug 27 '12 at 20:09

3 Answers3

1
        if (logged_in() === true) {
            echo
            '

<li ><a href="#">'.$user_data['username'].'</a>
<ul>
    <li><a href="../social.php">socail</a></li>
    <li><a  href="../my/pictures.php">my pictures</a></li>
    <li><a  href="../profile.php">profile</a></li>
    <li><a href="../logout.php">logout</a></li>
</ul>   ';
        } else {
            include'cpages/cmain/menuforms/formsmenu.php';

        } 
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
1

Do you mean concatenation?

<li ><a href="#">' . $user_data['username'] . '</a>
andrewsi
  • 10,807
  • 132
  • 35
  • 51
0

You're concatenating the string wrong, you should use dots:

EDIT

After some googling I found out that it is apparently OK to concatenate with commas also, I did not know that...

 echo "text".$variable."text".$variable2; //and so on.

EDIT 2

Apparently echo can take multiple parameters, which is what happens when you pass in values by separating them with commas. And then it isn't really string concatenation, user ACJ pointed that one out.

I would however still go with the HEREDOC in this case or concatenate the string, probably to a variable and then echo it out.

Something to look for is that you escape double and single quotes correctly, this is a common problem when working with long strings, especially strings containing html.

Also you might want to check out Heredoc:

$variable = <<<EOT 
    Place your multiline string here 
    and dont forget to end it with the same as you started it.
EOT; //Must not be indented

You should check out this question also: Best practices working with long strings in PHP

Okay so to actually get to your problem, assuming that you've checked to see that logged_in() really evaluates to true, then the only problem i can think of is that $user_data does not contain a key named username.

I'd start with enabling error_reporting by calling the following at the top of your file:

error_reporting(E_ALL | E_STRICT) //reports all errors.

And then you should get a pretty quick answer to what is wrong with your code, because it runs just fine on my computer.

Community
  • 1
  • 1
Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66