if/elseif/else的PHP语法,但适用于4个条件
我需要在我的脚本中实现3个“如果条件”,我已经在线查找了它,但我只能找到最多2个“如果条件”的解决方案,如下所示if/elseif/else的PHP语法,但适用于4个条件,php,if-statement,Php,If Statement,我需要在我的脚本中实现3个“如果条件”,我已经在线查找了它,但我只能找到最多2个“如果条件”的解决方案,如下所示 <? if (condition 1){ do something; } elseif (condition 2){ do something else; } else { do this last; } ?> 如何执行此操作?使用or运算符,|如何: if (condition 1) { do this } else if (condition
<?
if (condition 1){
do something;
}
elseif (condition 2){
do something else;
}
else {
do this last;
}
?>
如何执行此操作?使用or运算符,
|
如何:
if (condition 1) { do this }
else if (condition 2 || condition 3) {do that}
else {do this}
例如:
<?php
function testCondition($a, $b) {
if ($a == $b) {
print ("They are the same<br />\n");
}
else if ($a == "a" || $b == "b") {
print ("One is the same as its letter<br />\n");
}
else {
print ("They are some other sort<br />\n");
}
}
testCondition("c","c");
testCondition("a","c");
testCondition("a","b");
testCondition("d","e");
?>
简单地
您也可以使用switch case语句。如果你想要的话,你可以用其他的。if()中的每个条件都可以接受或和和运算符作为| | for或&&for,并且您可以使用
if (condition 1) { do this
} else if (condition 2) {do that
} else if (condition 3) {do that
} else { do this }
或者,如果每次都要检查一个变量,可以使用开关
$myvar = 5;
switch($myvar){
case 1:
//do this
break;
case 2:
//do that
break;
case 3:
//do that
break;
default:
//do this
}
你可以使用这个结构
if ( $a == $b ) {
// something...
} else if ( $a == $c ) {
// something...
} else if ( $a == $d ) {
// something...
} else {
// otherwise...
}
但是如果所有的条件都是($a等于某物),那么最好使用switch。。。案例:
switch ( $a ) {
case $b:
// something...
break;
case $c:
// something...
break;
case $d:
// something...
break;
default:
// otherwise...
break;
}
解决方案1
根据需要使用许多elseif
语句
当您的条件复杂或比较不同的变量时,请使用此解决方案
if (/*condition 1*/) {
// Action to condition 1
} else if (/*condition 2*/) {
// Action to condition 2
} else if (/*condition 3*/) {
// Action to condition 3
} else if (/*condition n*/) {
// Action to condition n
} else {
// Action when no conditions match.
}
解决方案2
使用开关
语句:
要将变量与常量值进行比较时,请使用此条件:
switch ($age) {
case 0:
return 'You are a baby';
break;
case 18:
return 'You are 18 years old';
break;
case 21:
case 22:
case 23:
return 'You are too old';
default:
return 'Unexpected age :(';
}
我的问题和第一次提到的一样。我有四个变量,但第二个被跳过,只有1、3和4起作用。为什么?
if(empty($fromName) or empty($fromEmail) or empty($subject) or empty($comments)) {
echo 'You cannot submit the form with empty fields. Please correct the form and resubmit.';
return false;
}
elseif($fieldDelete == "Delete this text!"){
echo "Delete the contents of the fourth field before submitting.";
return false;
}
elseif (($fromName == "Curtisvien") || ($fromName == "Thomastymn") || ($fromName == "RichardMark")) {
echo "Failed. Please try again.";
return false;
}
else {
$flgchk = mail ("$to", "$subject", "$message", "$headers");
$imgfile = "images/NatMap logo2.gif";
$handle = fopen($filename, "r");
$imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile));
echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary) . '" width=427 height=72 />';
echo "\n<br />\n<br />Thank You! An e-mail has been sent to the National Map web team and they will get back to you in the next 24-48 hours.";
}`enter code here`
if(空($fromName)或空($fromEmail)或空($subject)或空($comments)){
echo“您不能提交带有空字段的表单。请更正表单并重新提交。”;
返回false;
}
elseif($fieldDelete==“删除此文本!”){
echo“在提交之前删除第四个字段的内容。”;
返回false;
}
elseif($fromName==“Curtisvien”)| |($fromName==“Thomastymn”)| | |($fromName==“RichardMark”)){
echo“失败。请重试。”;
返回false;
}
否则{
$flgchk=邮件(“$to”、“$subject”、“$message”、“$headers”);
$imgfile=“images/NatMap logo2.gif”;
$handle=fopen($filename,“r”);
$imgbinary=fread(fopen($imgfile,“r”),filesize($imgfile));
回声';
echo“\n
\n
谢谢!已向国家地图网络团队发送了一封电子邮件,他们将在未来24-48小时内回复您。”;
}`在这里输入代码`
签出开关语句。如果您删除单词或
删除或
并且代码有效,则您的代码将与编写的代码一样工作。如果你想添加更多的,你可以添加更多的。我尝试了2x elseif,但它不是那样工作的。。。我使用的是Kohana Framework您是否尝试反转语句2和3以确保不是您的条件导致跳过它?elseif($fromName…)首先,然后是elseif($fieldDelete…)这样,您将发现它是否与您的if/elseif语句相关……第二个条件被打破,无论是第二个还是第三个。有什么建议吗?
if (/*condition 1*/) {
// Action to condition 1
} else if (/*condition 2*/) {
// Action to condition 2
} else if (/*condition 3*/) {
// Action to condition 3
} else if (/*condition n*/) {
// Action to condition n
} else {
// Action when no conditions match.
}
switch ($age) {
case 0:
return 'You are a baby';
break;
case 18:
return 'You are 18 years old';
break;
case 21:
case 22:
case 23:
return 'You are too old';
default:
return 'Unexpected age :(';
}
if(empty($fromName) or empty($fromEmail) or empty($subject) or empty($comments)) {
echo 'You cannot submit the form with empty fields. Please correct the form and resubmit.';
return false;
}
elseif($fieldDelete == "Delete this text!"){
echo "Delete the contents of the fourth field before submitting.";
return false;
}
elseif (($fromName == "Curtisvien") || ($fromName == "Thomastymn") || ($fromName == "RichardMark")) {
echo "Failed. Please try again.";
return false;
}
else {
$flgchk = mail ("$to", "$subject", "$message", "$headers");
$imgfile = "images/NatMap logo2.gif";
$handle = fopen($filename, "r");
$imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile));
echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary) . '" width=427 height=72 />';
echo "\n<br />\n<br />Thank You! An e-mail has been sent to the National Map web team and they will get back to you in the next 24-48 hours.";
}`enter code here`