php多条如果条件 [英] php multiple if conditions

查看:166
本文介绍了php多条如果条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试过滤所有这些参数时,php只会输入第一个if条件,忽略所有其他条件。

when i try to filter all these parameters php only enters in the first if conditions, ignoring all others conditions.

if($t_red<0){
    $t_red=0;
}

else if($t_red>256){
    $t_red=255;
}

else if($t_green<0){
    $t_red=0;
}

else if($t_green>256){
    $t_red=255;
}

if($t_blue<0){
    $t_red=0;
}

if($t_blue>256){
    $t_red=255;
}

if($t_red<0){
    $t_red=0;
}


推荐答案

如果跑过,可能是最合适的过滤函数。

Probably best suited if ran through a filtering function.

function setParam($param) {
  if($param < 0) {
    $param = 0;
  } elseif($param > 256) {
    $param = 255;
  }

  return $param;
}

$t_green = setParam($t_green);
$t_red = setParam($t_red);
$t_blue = setParam($t_blue);

如有必要,您还可以使用传递引用。

You could also use pass-by-reference if necessary.

这篇关于php多条如果条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆