bDir
bDir

Reputation: 173

PHP If Function inside Variable

How can I add if function in my PHP code below :

$html = 'blabla
'.if($a = $b).'
{
}
';

When I run that code, it show error like this :

Parse error: syntax error, unexpected 'if' (T_IF) in...

Please help to advice.

Thanks.

Upvotes: 2

Views: 110

Answers (3)

Viktor Svensson
Viktor Svensson

Reputation: 755

You need to use a ternary operator.

Example:

$html = 'blabla' . ($a == $b ? 'write something' : 'or something else');

Upvotes: 4

TFennis
TFennis

Reputation: 1423

You probably want to do something like this.

<?php

$html = 'Your order was ';

if ($order->status === 'success') { // I don't know just doing something random
    $html .= ' successful';
} else {
    $html .= ' unsuccessful';
}

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

use append again to string and use == to compare like

$html = 'blabla';
if($a == $b){
$html .='yes';
}

or use ternary operator like @Viktor Svensson answer

Upvotes: 1

Related Questions