0

I'm trying to set an if statement in my email body but I got this error. This is the part of the body message in my PHP code:

$body = <<<EOD
<strong>Tipo contatto:</strong> $contacttype <br>
EOD;
$body .= $contacttype == "telefonopref" ? "<strong>Chiamatemi in questo orario:</strong>".$orariotel."<br>"; <!-- line with error -->
$body.= <<<EOD
    <strong>Contatto:</strong> $contatto <br>
    <strong>Messaggio:</strong> $message <br>
EOD;

What's wrong? Any suggestion on how I should write it correctly? Thanks in advance.

DFD
  • 37
  • 9

2 Answers2

3

The ternary operator works like this:

$something = $condition ? 'this if true' : 'this if false';

You just need to add the : '' at the end of the line (before the semicolon) for your else statement.

Stevish
  • 734
  • 5
  • 17
  • 1
    There is no such thing like an *"inline if"*. The [ternary operator (`?:`)](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) is an operator, [`if` is a control structure](http://php.net/manual/en/control-structures.if.php). Operators and statements are different concepts, with different purposes in a program. They cannot be interchanged. – axiac Aug 16 '17 at 16:13
  • Thanks Stevish, complete answer! – DFD Aug 16 '17 at 16:14
  • @axiac Thanks, I couldn't think of the word "ternary", All I could think of was "Tertiary" and I KNEW that wasn't right! – Stevish Aug 16 '17 at 16:17
3

? : condition is missing..

$body = <<<EOD
<strong>Tipo contatto:</strong> $contacttype <br>
EOD;
$body .= $contacttype == "telefonopref" ? "<strong>Chiamatemi in questo orario:</strong>".$orariotel."<br>" : ""; 
$body.= <<<EOD
    <strong>Contatto:</strong> $contatto <br>
    <strong>Messaggio:</strong> $message <br>
EOD;
Rajapandian
  • 216
  • 2
  • 7