Reputation: 41
How can i check if exists or not in the same line something like that but it dosen't work :
<td><?= isset([$a['type1']]) ? [$a['type1']] : "0"; ?></td>
This's my code :
<td align="center" valign="top" class="textContent">
<h2 style="text-align:center;font-weight:normal;font- family:Helvetica,Arial,sans-serif;font-size:23px;margin-bottom:10px;color:#72C8F3;line-height:135%;">Error1 : '.($a["type1"]).' <br/>Error2 : '.($a["type2"]).'<br/>Error3 : '.($a["type3"]).'</h3>
</td>
Thanks for any help.
Upvotes: 2
Views: 1907
Reputation: 3093
You can use the ternary operator as followed. Note you don't need to wrap the array in '[]'
<?php echo (isset($a['type1']) ? $a['type1'] : 0); ?>
If you want to check all are set you could use;
<?php echo ( isset($a['type1']) && isset($a['type2']) && isset($a['type3'] ) ? $a['type1'] : 0 ); ?>
To simplify further as suggested by Magnus Eriksson:
<?php echo ( isset($a['type1'], $a['type2'], $a['type3']) ? $a['type1'] : 0 ); ?>
If you wish to check for just one use the or/double pipe ||
expression
<?php echo ( isset($a['type1']) || isset($a['type2']) || isset($a['type3'] ) ? $a['type1'] : 0 ); ?>
Upvotes: 3
Reputation: 383
PHP 7 introduces the so called null coalescing operator which simplifies the statements to:
$var = $var ?? "default";
So, your code would become
<td> <?php echo $a['type1'] ?? "0"; ?> </td>
Upvotes: 4