Thank you so much for reading and responding if you can.
非常感谢您阅读和回复,如果可以的话。
- In one function I test a condition and make a string 'true' or 'false', which then I make a global variable.
- 在一个函数中,我测试一个条件并使字符串'true'或'false',然后我创建一个全局变量。
- Then I call another function with that string as the parameter
- 然后我用该字符串作为参数调用另一个函数
-
Within that function's if statement, I want to test based off the strings boolean value of 'true' or 'false'
在该函数的if语句中,我想根据字符串布尔值'true'或'false'进行测试
$email_form_comments = $_POST['comments']; // pull post data from form if ($email_form_comments) $comments_status = true; // test if $email_form_comments is instantiated. If so, $comments_status is set to true else $error = true; // if not, error set to true. test_another_condition($comments_status); // pass $comments_status value as parameter function test_another_condition($condition) { if($condition != 'true') { // I expect $condition to == 'true'parameter $output = "Your Condition Failed"; return $output; } }
My thinking is that $condition will hold a 'true' value, but this is not so.
我的想法是$条件将保持“真实”值,但事实并非如此。
1 个解决方案
#1
1
I think the key here is PHP will evaluate empty strings as false and non-empty strings as true, and when setting and comparing booleans make sure to use constants without quotes. Use true
or false
not 'true'
or 'false'
. Also, I suggest writing your if statements so they will set alternate values on a single variable, or in the case of a function return an alternate value when the condition fails.
我认为这里的关键是PHP将空字符串评估为false和非空字符串为true,并且在设置和比较布尔值时,请确保使用不带引号的常量。使用true或false不是'true'或'false'。另外,我建议编写if语句,以便它们在单个变量上设置备用值,或者在条件失败时函数返回备用值。
I made some small modifications to your code so that your function will evaluate true
我对您的代码进行了一些小的修改,以便您的函数评估为true
// simulate post content
$_POST['comments'] = 'foo'; // non-empty string will evaluate true
#$_POST['comments'] = ''; // empty string will evaluate false
$email_form_comments = $_POST['comments']; // pull post data from form
if ($email_form_comments) {
$comments_status = true; // test if $email_form_comments is instantiated. If so, $comments_status is set to true
} else {
$comments_status = false; // if not, error set to true.
}
echo test_another_condition($comments_status); // pass $comments_status value as parameter
function test_another_condition($condition)
{
if ($condition !== true) {
return 'Your Condition Failed';
}
return 'Your Condition Passed';
}
#1
1
I think the key here is PHP will evaluate empty strings as false and non-empty strings as true, and when setting and comparing booleans make sure to use constants without quotes. Use true
or false
not 'true'
or 'false'
. Also, I suggest writing your if statements so they will set alternate values on a single variable, or in the case of a function return an alternate value when the condition fails.
我认为这里的关键是PHP将空字符串评估为false和非空字符串为true,并且在设置和比较布尔值时,请确保使用不带引号的常量。使用true或false不是'true'或'false'。另外,我建议编写if语句,以便它们在单个变量上设置备用值,或者在条件失败时函数返回备用值。
I made some small modifications to your code so that your function will evaluate true
我对您的代码进行了一些小的修改,以便您的函数评估为true
// simulate post content
$_POST['comments'] = 'foo'; // non-empty string will evaluate true
#$_POST['comments'] = ''; // empty string will evaluate false
$email_form_comments = $_POST['comments']; // pull post data from form
if ($email_form_comments) {
$comments_status = true; // test if $email_form_comments is instantiated. If so, $comments_status is set to true
} else {
$comments_status = false; // if not, error set to true.
}
echo test_another_condition($comments_status); // pass $comments_status value as parameter
function test_another_condition($condition)
{
if ($condition !== true) {
return 'Your Condition Failed';
}
return 'Your Condition Passed';
}