Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use continue keyword in a ternary operator in php

Tags:

php

I want to use continue keyword in ternary operator for code simplification. I am trying to following way and found an syntax error.

     in_array($SqlPackageCategoryProductResultRowObj->product_id, $individualProduct)?continue:"";

How can i use it.

like image 790
karim_fci Avatar asked Dec 25 '22 09:12

karim_fci


1 Answers

The ternary operator takes 3 expressions in the form of (expr1) ? (expr2) : (expr3).

continue is a control structure statement, not an expression. And hence, it can't be an operand of the ternary operator. In fact, none of PHP's operators accept control structures as operands, as far as I'm aware. The same applies to the rest of the broad C-family languages as well.

And for the sake of readability, it is probably better to use an if statement in your case.

like image 145
Fabrício Matté Avatar answered Jan 06 '23 08:01

Fabrício Matté