0
<html>
<head>
<title> GRADE DETAILS </title>
</head>
<body>

<?php
$myname=$_GET ['myname'];
$mygrade=$_GET ['mygrade'];

if(($myname=='@' or $myname=='!'))
{
    echo "Special character not allowed.";
}

else if($myname==" ")
{
    echo "Please enter your name";

}


else
{
    switch($mygrade)
{
case "";
echo "you did not enter grade";
break;

case A;
echo "Increment 30% of basic salary";
break;

case B;
echo "Increment 10% of basic salary";
break;

default;
echo"Wrong Grade";
break;
}
}

?>
</body>
</html>

我想在我的 if else 语句中设置多个条件..我尝试了很多方法,但仍然没有工作...我希望有人可以帮助我..与我的 Switch Case 无关,但问题仅在 IF ELSE ..请帮助。谢谢。

4

3 回答 3

1

基本上没问题,但是你需要将“CASE”语句后面的分号替换为冒号,然后将文本字符串(A,B等)放在引号中:

switch($mygrade) {
    case "":
        echo "you did not enter grade";
    break;

    case "A":
        echo "Increment 30% of basic salary";
    break;

    case "B":
        echo "Increment 10% of basic salary";
    break;

    default:
        echo "Wrong Grade";
    break;
}
于 2012-07-02T08:23:59.197 回答
1

您缺少一些引号,A并被B解释为常量

<?php
$myname= trim($_GET ['myname']);
$mygrade= trim($_GET ['mygrade']);

if(strstr('@',$myname) or strstr('!',$myname) )
{
    echo "Special character not allowed.";
}

else if(empty($myname))
{
    echo "Please enter your name";

}


else
{
    switch($mygrade)
{
case "":
echo "you did not enter grade";
break;

case 'A':
echo "Increment 30% of basic salary";
break;

case 'B':
echo "Increment 10% of basic salary";
break;

default:
echo"Wrong Grade";
break;
}
}
于 2012-07-02T08:26:11.577 回答
1

这是一个普通的PHP吗?我在下面有我的答案。但是,我不建议您继续这样做。您可以搜索其他可以帮助您进行验证的类或框架。这也可能易于注射。

回答你的问题:(基本上)

$invalid = array("@", "!");
if (in_array($myname, $invalid)) {
    echo 'Special character not allowed.';
}

if (empty($myname)) {
    echo "Please enter your name";
}

if (!empty($mygrade)) {
    switch($mygrade) {
       case "":
        echo "you did not enter grade";
        break;

    case "A":
        echo "Increment 30% of basic salary";
        break;

    case "B":
        echo "Increment 10% of basic salary";
        break;

    default:
        echo "Wrong Grade";
        break;
    }
}
else {
    echo 'Grade was empty';
}
于 2012-07-02T08:31:16.833 回答