Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP if statements with multiple values

Is there a shorter way of writing this?

<? 
if($_GET['id']==1 ||
$_GET['id']==3 ||
$_GET['id']==4 || 
$_GET['id']==5)
{echo 'does it really have to be this explicit?'};
?>

Something like this perhaps?

<?
if($_GET['id']==1 || 3 || 4 || 5){echo 'this is much shorter'};
?>
like image 662
Richard Tinkler Avatar asked Oct 02 '13 14:10

Richard Tinkler


1 Answers

Just try with:

if ( in_array($_GET['id'], array(1, 3, 4, 5)) ) {}
like image 133
hsz Avatar answered Sep 28 '22 13:09

hsz