Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Elseif Ternary Operators

Tags:

I am trying to convert the following code into a Ternary Operator, but it is not working and I am unsure why. I think my problem is that I do not know how to express the elseif operation in ternary format. From my understanding and elseif is performed the same way as an if operation by using the format : (condition) ? 'result'.

if ($i == 0) {     $top = '<div class="active item">'; } elseif ($i % 5 == 0) {     $top = '<div class="item">'; } else {     $top = ''; }  $top = ($i == 0) ? '<div class="active item">' : ($i % 5 == 0) ? '<div class="item">' : ''; 
like image 532
Jon Avatar asked Oct 31 '12 16:10

Jon


1 Answers

$top = ($i == 0) ? '<div class="active item">' : (($i % 5 == 0) ? '<div class="item">' : ''); 

you need to add parenthesis' around the entire else block

like image 60
Samuel Cook Avatar answered Jan 11 '23 09:01

Samuel Cook