I have an IF statement and I need to check two variables, then execute the action if either of the variables are false.
I know I can use the OR (||) statement however for some reason it is not working. I am in the process of learning PHP so it's probably a syntax error on my behalf.
If I create the if statement with only one (either) variable, it works fine:
<?php if ($var1 == false) { do whatever...} ?>
However when I try to check two, neither of the variables seem to be checked. I have tried different syntax variations but nothing works:
<?php if (($var1 == false) || ($var2 == false)) { do whatever...} ?>
<?php if (($var1 == false) OR ($var2 == false)) { do whatever...} ?>
<?php if ($var1 == false || $var2 == false) { do whatever...} ?>
<?php if ($var1 == false OR $var2 == false) { do whatever...} ?>
Can someone please point out what my error is?
Thanks!
EDIT: Including the actual code.
<?php $member = $members_template->member; $bpmember = bp_get_member_user_id(); ?>
<?php $membersearchinclude = xprofile_get_field_data( 'Exclude yourself from website search results?', $bpmember ); ?>
<?php $adminsearchinclude = xprofile_get_field_data( 'Exclude From Search Results', $bpmember ); ?>
<?php if (($adminsearchinclude == false) || ($membersearchinclude == false)) { ?>
This is extracting the xprofile field state from two different checkboxes in BuddyPress. I am checking if either of the checkboxes are false, then executing code.
I have managed to solve the issue.
It seems my logical operators needed changing:
if ((!$adminsearchexclude) && (!$membersearchexclude))
I am sure if I posted the entire code one of you would have got it. This is a sensitive project at the moment at unfortunately posting the entire code wasn't an option.
Many thanks to everyone that chipped in especially #ChrisO'Kelly for the var dump snippets. You got me thinking in the right direction and I now have a new tool in my arsenal :)
THANK YOU ALL!!
I'm not sure what causing it to not run as expected because from my view its all valid. Try to post your code so we know the whole logic.
Try to use strict comparison.
$var1 = 0;
$var2 = 1;
// The echo will not be executed
if ($var1 === false || $var2 === false) {
echo "Strict comparison\n";
}
// The echo will be executed
if ($var1 == false || $var2 == false) {
echo "No strict comparison\n";
}
References:
http://php.net/manual/en/language.operators.logical.php
http://php.net/manual/en/language.operators.comparison.php
Related
The best way to check the conditions and what is difference between them?
This is my usual way:
if ($this->_is_valid_number() == TRUE) {
//do some thing...
}
I've seen some code written in this way(for example):
if (TRUE == $this->_is_valid_number()) {
//do some thing...
}
Are these different from each other? Which method is standard?
None of the above, really.
== true is redundant with if(condition) so it could just be written as if ($this->_is_valid_number()) which is pretty standard. If you want to check for false, you would do if (!$this->_is_valid_number()) and if you would check for any other condition, you usually write like you would speak:
If my number is not one -> if($number !== 1)
Notice: Also check this article for difference between == and === operators
The second method has a added benefit of failing if it's used incorrect.
The second one sets true to $number and creates no error, which is what the code should do but not what the coder is expecting.
Do it the other way around and it will fail
$number =5;
if($number == true) echo "true"; // true
if($number = true) echo "true"; //true
if(true == $number) echo "true"; // true
if(true = $number) echo "true"; // fails
https://3v4l.org/suF9s
I was writing in PHP and making so the page was declared by get method. For an example if it were index.php?page=home it would take home and compare to other strings and... ya include the home BUT the compression in the if statement dosnt work... I have also write that if get method == start then dont show the side news but it still showed it!
Here's my code:
if(isset($_GET['page']) && $_GET['page'] == 'start'){
$adbool = false;
}else{
include('inc/main.php');
}
And here is the if statement for deleting side news:
if(isset($adbool) && !$adbool == false){
include('inc/ad.php');
}
and remember this !$adbool == false is not equal to this $adbool != false
You better understand this part as well !$adbool == false;
!$adbool means that $adbool is equal to false;
so !$adbool == false; means if false=false hence this condition always set to to true.
Thats why your logic is failing
Change this
if(isset($adbool) && !$adbool == false){
include('inc/ad.php');
}
To this
if($adbool == TRUE){//simply check weather its set to to true or not
include('inc/ad.php');
}
use
if(isset($adbool) && $adbool){
include('inc/ad.php');
}
this will check if $adbool is set, and if it's set to true.
I want to check the GET variables are not empty, I tried ways but they didn't work.
So I had the code like this:
$u = isset($_GET["u"]);
$p = isset($_GET["p"]);
if ($u !== "" && $p !== "") {
//something
} else {
//do something
}
The I checked the code by sending create.php?u=&p=, but the code didn't work. It kept running the //do something part. The I tried:
echo $u;
echo $p;
It returned 1 and 1. Then I changed it to:
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "") {
//something
} else {
//do something
}
But it continued to run //do something.
Please help.
You can just use empty which is a PHP function. It will automatically check if it exists and whether there is any data in it:
if(empty($var))
{
// This variable is either not set or has nothing in it.
}
In your case, as you want to check AGAINST it being empty you can use:
if (!empty($u) && !empty($p))
{
// You can continue...
}
Edit: Additionally the comparison !== will check for not equal to AND of the same type. While in this case GET/POST data are strings, so the use is correct (comparing to an empty string), be careful when using this. The normal PHP comparison for not equal to is !=.
Additional Edit: Actually, (amusingly) it is. Had you used a != to do the comparison, it would have worked. As the == and != operators perform a loose comparison, false == "" returns true - hence your if statement code of ($u != "" && $p != "") would have worked the way you expected.
<?php
$var1=false;
$var2="";
$var3=0;
echo ($var1!=$var2)? "Not Equal" : "Equal";
echo ($var1!==$var2)? "Not Equal" : "Equal";
echo ($var1!=$var3)? "Not Equal" : "Equal";
echo ($var1!==$var3)? "Not Equal" : "Equal";
print_r($var1);
print_r($var2);
?>
// Output: Equal
// Output: Not Equal
// Output: Equal
// Output: Not Equal
Final edit: Change your condition in your if statement to:
if ($u != "" && $p != "")
It will work as you expected, it won't be the best way of doing it (nor the shortest) but it will work the way you intended.
Really the Final Edit:
Consider the following:
$u = isset($_GET["u"]); // Assuming GET is set, $u == TRUE
$p = isset($_GET["p"]); // Assuming GET is not set, $p == FALSE
Strict Comparisons:
if ($u !== "")
// (TRUE !== "" - is not met. Strict Comparison used - As expected)
if ($p !== "")
// (FALSE !== "" - is not met. Strict Comparison used - Not as expected)
While the Loose Comparisons:
if ($u != "")
// (TRUE != "" - is not met. Loose Comparison used - As expected)
if ($p != "")
// (FALSE != "" - is met. Loose Comparison used)
You need !empty()
if (!empty($_GET["p"]) && !empty($_GET["u"])) {
//something
} else {
//do something
}
Helpful Link
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "")
why are you using "!==" and not "!=".
to always simplify your problem solve the logic on paper once using the runtime $u and $p value.
To check if $_GET value is blank or not you can use 2 methods.
since $_GET is an array you can use if(count($_GET)) if you have only u and p to check or check all incoming $_GET parameters.
empty function #Fluffeh referred to.
if($_GET['u']!=""&&$_GET['p']!="")
Hope it helps thx
In you code you should correctly check the variable existence like
if ($u != NULL && $p != NULL && $u != 0 && $p != 0) {
//something
} else {
//do something
}
Wow! I was so dumb... isset returns a boolean. I fixed my problem now. Thank you for answering anyway :)
This fixes:
$u = $_GET["u"];
$p = $_GET["p"];
I'm trying to compare a POST variable with a string. Can someone help me see what in my PHP code is not written correctly? I've tried both '==' and '==='. Thank you for your help.
$action = mysqli_real_escape_string($mysqli, $_POST['action']);
if(strcmp($action, "save") == 0){
//do stuff
}elseif(strcmp($action, "load") == 0){
//do other stuff
}else{
//do even more stuff
}
why not simply use
if ($_POST['action']=='save'){
}elseif($_POST['action']=='load'){
}
don't understand the mysql in this contenxt
Don't know why you want to do this, but try casting $aciton, like (string)$action.
== is used to see if the two sides of comparison are equal, while === is used to check to see if they're identical meaning they are equal AND of the same type.
As for your code, you should just be able to do
if($action == 'save'){
echo 'save';
}
elseif ($action == 'load'){
echo 'load';
}
else{
echo 'none';
}
For the record, I read documentation on boolean comparisons in PHP, but I do not understand how my code relates in this context. I thought I had a problem connecting to MySQL, but it turns out everything is fine and I can make queries without issue. So, MySQL is not really that relevant to this post.
I use this to report errors in an object that tried to connect on construction. Note that I tried ==, ===, != and !== only to get the same problem. I even tried casting the argument to bool and boolean.
Comments in below code blocks are relevant.
private function assert($test){ // Tried renaming in case PHP was funny about it.
if ($test === FALSE){ // Tried ==, ===, !== and != and casting $test.
if ($this->use_exception){
throw new mysql_exception();
}else{
die("MySQL ".mysql_errno()." - ".mysql_error());
}
}
}
Connecting is typical.
$this->con = mysql_connect($host, $un, $pw, false, $ssl ? MYSQL_CLIENT_SSL : 0);
// I get 'yay'
echo mysql_ping($this->con) ? "yay" : "nay";
// This disagrees. Tried no cast, and a cast to bool.
$this->assert((boolean)($this->con != FALSE));
mysql_ping() says everything is ok, but assert() stops the presses no matter what.
I tried every operator and cast combination, even renaming the function out of paranoia over a name clash. Why does assert() only see true?
EDIT:
To make my problem clearer, consider the alternative implementation with Eugen's suggested use of is_resource. The problem is that I just have no idea why the below happens.
private function assert($test){
$test = $test ? true : false;
if ($test === FALSE){
echo "$test === false<br />";
}
if ($test == FALSE){
echo "$test == false<br />";
}
if ($test !== FALSE){
echo "$test !== false<br />";
}
if ($test == FALSE){
echo "$test != false<br />";
}
}
Output is out of order and the value changed after one comparison. Since I can make queries, $test must be true. I should not get output at all, but I do for every operator. I also tried it with FALSE replaced by 0.
Bad PHP instance?
1 !== false
=== false
!== false
!= false
$this->assert(is_resource($this->con));
can't you use your second example and combine the function? you already get a value (yay/nay)
$this->con = mysql_connect($host, $un, $pw, false, $ssl ? MYSQL_CLIENT_SSL : 0);
$this->assert = mysql_ping($this->con) ? 1 : 0;
just thinking out loud