I don't want to repeat writing $value in if statement. Is there any way to write once and add conditions like below?

Thanks

if ($value==1 || $value== 2 || $value== 3) { 
    echo "I don't want this way";
}

if ($value==1 || 2 || 3) {
    echo "I want something like this";
}

Recommended Answers

All 2 Replies

It's called a switch statement.

switch($value) {
  case 1:
    do something;
    break;
  case 2:
    do something;
    break;
  default:
    do something if the others aren't true;
    break;
}

Though there is also the much more hacky way of

if(in_array($value, array(1, 2, 3, 4, 5)) {
  do something;
}

Thank you. I'll try array style.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.