如何在php if语句中使用超过1或2个条件?

时间:2017-07-26 06:28:57

标签: php

我想根据条件输出,正如我在代码中提到的那样。但我还有3个条件,即 - 中,大和超大。这些都是服务。我可以一次提到所有四个条件,输出应该是重要的!

以下是if Statement:

的代码
if ($item['description'] == "Small") {
    $typeoutput = Lang::trans('invoicessacsms').'<br />';
    }

请检查上面的代码,并提出建议。感谢

4 个答案:

答案 0 :(得分:2)

您可以添加#base这样的案例以获得不同的结果:

<?php

$action=$_REQUEST['action'];

if ($action=="")    /* display the contact form */
    {
    ?>

    <form name="contactform" method="post" enctype="multipart/form-data">

        <input type="hidden" name="action" value="submit">

      <label for="your_name">Your Name <font color="red">*</font></label>
      <input  type="text" id="reset" name="your_name"  placeholder="Enter Your Name" maxlength="20" size="40" >

      <label for="email">Email Address <font color="red">*</font></label>
      <input  type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required>

      <label for="mobile_number">Mobile Number</label>
      <input  type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >

      <label for="message">Message <font color="red">*</font></label>
      <textarea  name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>

        <input type="submit" value="Submit">
        </form>



<?php
    }

else                /* send the submitted data */

    {

$your_name = $_REQUEST['your_name'];

$email = $_REQUEST['email'];

$mobile_number = $_REQUEST['mobile_number'];

$message = $_REQUEST['message'];

$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";

$recipient = "name@email.com";

$subject = "Contact Form";

$mailheader = "From: $email \r\n";

    if (($your_name=="")||($email=="")||($message==""))
        {
        echo "All fields are required, please fill <a href=\"\">the form</a> again.";
        }
    else{       
        @mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

        echo "Email sent!";
        }
    }  
?>

如果结果相同,那么您可以在Switch

中使用多个条件
switch ($item['description']) {
    case 'small':
        $typeoutput = Lang::trans('invoicessacsms').'<br />';
        break;
    case 'Medium':
        # code...
        break;
    default:
        # code...
        break;
}

答案 1 :(得分:2)

您可以简单地定义一个关联数组,以避免使用任何其他语句:

$translations = [
    'Small' => Lang::trans('invoicessacsms'),
    'Large' => Lang::trans('invoicelarge')
];

$typeout = $translations[$item['description']] . '<br />';

答案 2 :(得分:1)

您可以使用逻辑运算符。

http://php.net/manual/en/language.operators.logical.php

if($a && $b) // a AND b
if($a || $b) // a OR b

答案 3 :(得分:1)

创建您的值数组并使用in_array检查,如下所示

$valArray = array ("small","large","medium");

if (in_array($item['description'],$valArray) ) {
    $typeoutput = Lang::trans('invoicessacsms').'<br />';
    }
  

in_array - 检查数组中是否存在值