奇怪的PHP让我难过

I'm looking at this code and thinking I can't see the error because there is somethign wrong but I just can't see it... I'm hoping someone here can see it as staring at it for the last half an hour hasn't made it clear to me.

The code:

if (!empty($_SESSION['email_notifications'])) { 

  print '<br>SESSION[email_notifications] = ['.$_SESSION['email_notifications'].']';
  print '<br>Session exists!!!';

  $from = $_SESSION['display_name'].' <'.$_SESSION['email_notifications'].'>';
  print '<br>$from = ['.$from.']';

}

Outputs this:

SESSION[email_notifications] = [myemailaddress@email.com]

Session exists!!!

$from = [ ]

The session value is set but when it is stored in a variable it disappears???

Hmmmm. Php5?

This is crazy silly, but have you checked the output SOURCE, rather than html? You've put angle brackets around it, it may just not be rendering on the screen.

You are outputting that to HTML, right? Remember &lt; and &gt;? Check out the page source if it's there. In all likelihood, the email address portion is being interpreted as an unknown HTML tag, and therefore ignored.

As to why your display_name isn't displayed, I don't know. Does it have a value?

Since the <br> tags do not show up in the output, I'm guessing that you've copy-pasted this directly from what the browser shows. The browser will not display HTML tags, but uses them to format the real text, or ignore them if it doesn't "know" what to do with a tag. If you want to see the real output, try the "display page source" option of your browser.

That being said, in the second case the email address is outputted with '<' and '>', so it appears as a tag to the browser. The displayname variable is probably not set in the session, or it is a string that has '<' and '>' around it too.

If you want to output sanatised HTML you can use some of the built in functions PHP offers to make it output to the browser correctly. Using your code, try the following:

if (!empty($_SESSION['email_notifications'])) { 

  print '<br>SESSION[email_notifications] = ['.$_SESSION['email_notifications'].']';
  print '<br>Session exists!!!';

  $from = $_SESSION['display_name'].' <'.$_SESSION['email_notifications'].'>';
  print '<br>$from = ['.htmlentities($from).']';

}

Ok... I found out the issue...

$_SESSION['display_name'] was empty which was throwing me...

By encapsulating $_SESSION['email_notifications'] in < > html acts like a mail program and was hiding the email address behind the preceeding display_name and because the preceeding display_name was empty, it was just being hidden altogether...