简体   繁体   English

PHP-将变量的值更改为另一个变量中的后续If语句

[英]PHP - Change a variable's value for a later If statement within another variable

I am trying to create a $clause variable that contains the variable $team1 and can change the value of the variable $team1 for a later If statement. 我试图创建一个包含变量$ team1的$ clause变量,并且可以为以后的If语句更改变量$ team1的值。

Below is a simplified version of my code: 下面是我的代码的简化版本:

$team1 = "";

$clause = "$team1 == 'Wildcats' OR $team1 == 'Bulldogs";

$team1 = 'Bears';

if ($clause) { echo "Yes"; }
else {echo "No";}

I would like $clause in the If statement to have the value (in this example): 我希望If语句中的$ clause具有值(在此示例中):

'Bears' == 'Wildcats' OR 'Bears' == 'Bulldogs";

No matter what I make the value of $team1, the output is "Yes". 无论我将$ team1的值做成什么,输出都是“是”。 How can I get the If statement to correctly determine if value of $team1 is (or isn't) meeting the condition in $clause? 我如何获得If语句来正确确定$ team1的值是否(或不)满足$ clause中的条件?

Your if statement checks whether or not the variable is declared or not. 您的if语句检查是否声明了变量。 So it will always print "Yes". 因此它将始终打印“是”。

The type you want can be acheived by using eval which translates text into PHP-Code. 您可以通过使用eval来实现所需的类型,该eval可以将文本转换为PHP代码。

See http://php.net/manual/de/function.eval.php for further instructions. 有关更多说明,请参见http://php.net/manual/de/function.eval.php

use with some return function .Because variable already printed the value in first definition .so call function after if condition satisfied .Then new variable value was updated 与某些返回函数一起使用。因为变量已经在第一个定义中打印了值。因此,如果条件满足,请调用函数。然后更新新的变量值

    function varReset($team1){
    return "$team1 == 'Wildcats' OR $team1 == 'Bulldogs";
}
$team1 = "";
$clause = varReset($team1);
$team1 = 'Bears';
if ($clause) {
    $clause = varReset($team1);
    echo $clause;
    echo "Yes";
}
else {echo "No";}

I think your question is illogical because Php will execute statement line by line, so $cause value will be (== 'Wildcats' OR == 'Bulldogs') and then if($cause) -- will return TRUE because if will check the variable is 'isset' and 'not empty', It won't take as you expected condition I recommend you to use function 我认为您的问题是不合逻辑的,因为Php将逐行执行语句,因此$ cause值将为(=='Wildcats'OR =='Bulldogs'),然后if($ cause)-将返回TRUE,因为if如果将检查变量为“ isset”和“ not not empty”,不会达到您期望的条件,我建议您使用函数

function checkVal($param) {
    return ($param == 'Wildcats' || $param == 'Bulldogs') ?  "Yes" : "No";
}

echo checkVal('Bears');

Based on the feedback, I decided that I needed different approach to solve this problem. 根据反馈,我决定需要其他方法来解决此问题。 I was able to get what I wanted by putting the teams into an array ($teams) and then looping through that array with the If statement comparing to $team1. 通过将团队放入一个数组($ teams),然后使用与$ team1比较的If语句遍历该数组,可以得到我想要的东西。 The following code works how I want it to by changing the value of $team1. 以下代码通过更改$ team1的值来实现我想要的方式。

$teams = array('Wildcats', 'Bulldogs', 'Cougars');
$team1 = 'Bears';

$count = 0;
$met = "no";

do {
    if($team1 == $teams[$count]) { $met="yes"; break;}
    $count++;
} while ($count < count($teams));


echo $met;

Thanks to everyone for the help! 感谢大家的帮助! I've used this forum a lot to solve previous problems, but this is the first time I've actually posted. 我已经使用了很多这个论坛来解决以前的问题,但这是我第一次实际发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM