我怎么能修复这个if语句php?

时间:2015-03-16 06:51:30

标签: php if-statement

在我的if语句中,我有大小和性别,看看我的代码。当我的if语句不需要大小但仍然需要性别时,它会花费一个问题。

出于某种原因,我的网站不会检测某个地方的头像大小,所以我不需要这个if语句末尾的大小,通常,当if语句没有任何匹配时我应该使用else 。

但是,我仍然需要强调性别,我能解决这个问题吗?

<?php
    if ($size <= 48 && $gender=='Female') $custom_avatar_size = '48';
    else if ($size > 48 && $size <= 96 && $gender=='Female') $custom_avatar_size = '96';
    else if ($size > 96 && $size <= 128 && $gender=='Female') $custom_avatar_size = '128';
    else if ($size > 128 && $size <= 256 && $gender=='Female') $custom_avatar_size = '256';
    else if ($size <= 48 && $gender=='Male') $custom_avatar_size = '1';
    else if ($size > 48 && $size <= 96 && $gender=='Male') $custom_avatar_size = '2';
    else if ($size > 96 && $size <= 128 && $gender=='Male') $custom_avatar_size = '3';
    else if ($size > 128 && $size <= 256 && $gender=='Male') $custom_avatar_size = '4';
    else if ($gender=='Female') $custom_avatar_size = '512';//problem is here
    else if ($gender=='Male') $custom_avatar_size = '5';//problem is here
?>

2 个答案:

答案 0 :(得分:3)

你可以检查$ size是否为空,你可以更新最后两个&#34;否则如果&#34;声明如下:

else if (empty($size) && $gender=='Female') $custom_avatar_size = '512';
else if (empty($size) && $gender=='Male') $custom_avatar_size = '5';

答案 1 :(得分:1)

检查一下 -

<?php
if ($size <= 48) {
    if ($gender == 'Female') {
        $custom_avatar_size = '48';
    } else if ($gender == 'Male') {
        $custom_avatar_size = '1';
    }
} else if ($size > 48 && $size <= 96) {
    if ($gender == 'Female') {
        $custom_avatar_size = '96';
    } else if ($gender == 'Male') {
        $custom_avatar_size = '2';
    }
} else if ($size > 96 && $size <= 128) {
    if ($gender == 'Female') {
        $custom_avatar_size = '128';
    } else if ($gender == 'Male') {
        $custom_avatar_size = '3';
    }
} else if ($size > 128 && $size <= 256) {
    if ($gender == 'Female') {
        $custom_avatar_size = '256';
    } else if ($gender == 'Male') {
        $custom_avatar_size = '4';
    }
} else {
    if ($gender == 'Female') {
        $custom_avatar_size = '512';
    } else if ($gender == 'Male') {
        $custom_avatar_size = '5';
    }
}
?>