检查大量变量是否包含字符串的最佳方法是否更改? PHP

时间:2018-01-29 03:43:15

标签: php function variables if-statement

我需要检查很多变量,看它是否等于“无”。如果变量等于“None”,我想将其更改为=“ - ”;

如果不为每个变量设置单独的IF函数,最好的做法是什么?

//Variables
$profileEthnicity = h($result["profile_ethnicity"]);
$profileHeight = h($result["profile_height"]);
$profileBuild = h($result["profile_build"]);
$profileEyeColor = h($result["profile_eye_color"]);
$profileHairColor = h($result["profile_hair_color"]);
$profileTattoos = h($result["profile_tattoos"]);
$profilePiercings = h($result["profile_piercings"]);

//Example
if($profileEthnicity == "None") { $profileEthnicity = "-"; }

1 个答案:

答案 0 :(得分:1)

由于值在数组中,您可以这样做:

foreach($result AS $key => $val) {
    ('None' == $val) ? $result[$key] = '-' : $result[$key] = $val;
}