Is there any more elegant way to write an IF with multiple OR conditions? Currently, my code is like below, but it doesn’t look interesting like that.
if ( has_block( 'cgb/block-imoney-blocks' ) || has_block( 'cgb/block-idh-affiliates' ) || has_block( 'cgb/block-idh-best-summary') || has_block('cgb/block-idh-highlights')) {
echo 'Value Found';
}
4
Answers
If it is a lot of blocks it’s more readable to iterate through an array like this:
The
break
is added to prevent multiple times execution of the if-statement but not strictly nessecary.With better formatting?
you could put the conditions in a function and pass the values in an array.
Imo, make a
has()
function or similar which you can reuse, it then doesnt matter how long the lines are you have abstracted it. It would be equivalent tosome
(i.e some values in the array should be true).