0

I was told I had to add a ternary operator to make my conditional work. Am a newb so not really sure how to do that.

<div id="notification-container">
<div id="notification">'.if(isset($_COOKIE['HotspotUserEmail'])){$m01}else{$m03}.'<div id="secondary">'.$m04.'</div>
</div></div>

What I am trying to achieve is, if cookie is present I want the contents of the first variable to be display. If there isn't one, I want the else to be.

Nikk
  • 7,384
  • 8
  • 44
  • 90
  • I *really* love it when my comments gets ignored. (I probably guess you don't print this html with php as I guessed in your last question, if you would read my comments) – Rizier123 Apr 29 '15 at 22:58
  • I do print it. Yes. I echo the a few lines of html that change. – Nikk Apr 29 '15 at 22:59

2 Answers2

2

You would replace your if (conditional) { statement; } else {statement; } with something like this:

isset($_COOKIE['HotspotUserEmail']) ? $m01 : $m03
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • It doesn't work. This is what I have: `
    '.if(isset($_COOKIE['HotspotUserEmailUNIROYAL'])) ? $m01 : $m03;.'
    '.$m04.'
    `
    – Nikk Apr 29 '15 at 22:51
  • you have an extra if in there. please re-read the answer and I also suggest check out the PHP manual – Mike Dinescu Apr 30 '15 at 06:00
0

Just marshall your variable ahead of time:

<?
  $mVar = (isset($_COOKIE['HotspotUserEmail'])) ? $m01 : $m0;
?>
<div id="notification-container">
<div id="notification">'{$mVar}'</div>
</div>

N.B. I'm assuming you're using something that is echoing $mVar like a Smarty template. It's unclear from your original question if you're echoing the div parts and trying to in-line template logic or something else. '{$mVar}' won't work without some sort of templating, you'd have to <?php echo $mVar;?>

If you're doing this entirely inside a php script, this is what you want:

$mVar = ;
echo "<div id="notification-container"><div id="notification">".$mVar."</div></div>";

Or inline:

echo "<div id="notification-container"><div id="notification">".((isset($_COOKIE['HotspotUserEmail'])) ? $m01 : $m0)."</div></div>";
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • It doesn't work. This is what I have:
    '.if(isset($_COOKIE['HotspotUserEmailUNIROYAL'])) ? $m01 : $m03;.'
    '.$m04.'
    – Nikk Apr 29 '15 at 22:51
  • What is your output? Also I note you're NOT marshaling ahead of the fact. Your statement above will quit after the ternary operator because you include a `;`. – Nathaniel Ford Apr 29 '15 at 22:55
  • Error is: syntax error, unexpected T_IF. I tried with";" and without. Still can't get it to work. As for the variables, they are set priorly. So I am just trying to show their value, if the cookie is present. – Nikk Apr 29 '15 at 22:57
  • Argh, sorry: see update. Just elide the `if`. – Nathaniel Ford Apr 29 '15 at 23:06
  • There is another problem now. The variable contents are displayed, however. There is another div, following after the html I posted. And that gets cut off. Is there a fix for that? – Nikk Apr 29 '15 at 23:16
  • That sounds like another problem that we have no context on. I would note that you aren't including *compileable* code for us to examine. See http://www.sscce.org/ – Nathaniel Ford Apr 29 '15 at 23:25
  • For example, not even `
    '.$m04.'
    ` is displayed. Everything past just gets cut off. Am I missing something?
    – Nikk Apr 29 '15 at 23:29
  • Note that the code you *just* provided **will not compile**. You HAVE to give us examples that will compile so we can check what we're giving you. That means *more code* around what you're showing us. – Nathaniel Ford Apr 29 '15 at 23:30
  • I fixed it by added parenthesis around the conditional, like this: `(isset($_COOKIE['HotspotUserEmail']) ? $m01 : $m03)`; is this approach correct? – Nikk Apr 29 '15 at 23:33
  • Does it work? Also don't forget to reward the people helping you: Mike gave you an answer worth upvoting. – Nathaniel Ford Apr 29 '15 at 23:34
  • Yeah, it does work with the parenthesis – Nikk Apr 29 '15 at 23:35