If there any way to write short cut code instead of this one
I want to say if page id is 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 then show something and else show something else
<?PHP
if ($page[id]==1 || $page[id]==2 || $page[id]==3 || $page[id]==4 || $page[id]==5 || $page[id]==6 || $page[id]==7 || $page[id]==8 || $page[id]==9 || $page[id]==10){
echo "something";
}else{
echo "something else";
}
?>
thanks that would helps me a lot for longer example since the ids are numbers 1,2,3...etc
You could do
<?PHP
if ($page['id'] >=1 && $page['id'] <=10){
echo "something";
}else{
echo "something else";
}
you can use in_array.
if (in_array($page['id'], array(1,2,3,4,5,6,7,8,9,10)))
{
echo "something";
}
else
{
echo "something else";
}
In such cases you can usally use in_array()
to compare a variable against a list of values. In your case you can combine it with range()
(if it's a short consecutive numeric list) like so:
if (in_array($page[id], range(1,8))) {
Note that $page[id]
should probably be $page["id"]
.
if($page['id'] > 0 and $page['id'] < 11) {
echo 'something';
}
you can use ternary operator as below
echo ($page[id] >=1 && $page[id] <=10)?"something":"something else";
you can also use in_array to do similar condition
you can just use an if condition like if($page[id]>=1 && $page[id]<=10)..