i got into some trouble by using OR if one condition is true and the other false.
in my constillation right now it is always only the second condition token.
if($num_rows === '0' OR $_GET['id'] !== $_SESSION['UserID'])
{ echo "show me something"; }
else
{ show nothing; }
i get only always 'show nothing'.
Note that === is strict comparison. I think the following:
$num_rows === '0'
should rather be:
$num_rows == 0
$num_rows is presumably an integer and not a string (a piece of text).
Related: PHP == vs === on Stack Overflow
Watch out with the second comparison, too:
$_GET['id'] !== $_SESSION['UserID']
Here, it's probably better to use != in favor of !== as well. $_GET is generally read as string, so even something like ?id=5 will return as string "5" and not as integer 5
Here's a quick test to illustrate:
if (isset($_GET['id'])) {
echo '<h2>Comparison with == 5</h2>';
var_dump( ($_GET['id'] == 5) );
echo '<h2>Comparison with == "5"</h2>';
var_dump( ($_GET['id'] == "5") );
echo '<h2>Comparison with === 5</h2>';
var_dump( ($_GET['id'] === 5) );
echo '<h2>Comparison with === "5"</h2>';
var_dump( ($_GET['id'] === "5") );
}
else {
echo 'Please set an ?id to test';
}
This will output (notice the third item is false) the following with ?id=5:
Comparison with == 5
boolean true
Comparison with == "5"
boolean true
Comparison with === 5
boolean false
Comparison with === "5"
boolean true
Probably because you use absolute equality with a string.Mysli_num_rows returns an int.
http://php.net/manual/en/language.operators.comparison.php
Equal and of the same type
You dont need to use === as it is a strict comparison and $num_rows is probably an int Try this:-
if($num_rows == 0 OR $_GET['id'] != $_SESSION['UserID'])
$_GET['id'] will provide you a string. You may chech the manual for details
To be really correct, need something like this:
if ($num_rows === FALSE) {
echo 'db resource/link gone';
}
elseif ($num_rows === 0 || intval($_GET['id']) !== intval($_SESSION['UserID'])) {
echo 'no db result or id mismatch';
}
elseif (intval($num_rows) > 0 && intval($_GET['id']) === intval($_SESSION['UserID'])) {
echo 'everything good';
}
else {
echo 'something unhandled went wrong';
}
thanks for all the answers. i got it to work. i change the order and the opreator to what you've suggested.
i made it like this:
if($num_rows > 0 OR $_GET['id'] == $_SESSION['UserID']) { echo "show nothing"; }
else { show me some stuff }
so thanks again for all the help. :)
Related
I have array look like this.
$setLogic = array(array("Conj"=>null,"Topic"=>True),array("Conj"=>"Or","Topic"=>True),array("Conj"=>"Or","Topic"=>false),
I try to build a IF statement with the dynamicly with this way.
foreach($setLogic as $value){
echo $value['Conj'].(int)$value['Topic'];
if($value['Conj'].(int)$value['Topic'] == true){
$getBoolean[] = true;
}
else{
$getBoolean[] = false;
}
I just need something like this.
(true or true or false) and return true or false
I found my answer with my self.
It's look like this.
$abc= '';
foreach($setLogic2 as $key => $value){
$abc.= $value['Conj'].' '.(int)$value['Topic'].' ';
}
//$abc = "0 And 1 And 1";
if(eval("return $abc;") == true) {
echo 'true boolean';
}
else if(eval("return $abc;") == false){
echo 'false boolean';
}
else{
echo 'none';
}
Ok i will re formulate my answer....
You want to dynamically evaluate an array which contains for each subarray
* a verb : "and" or "or"
* a value "true or "false"
Quicker and most dirty way is to use eval().
* build your conditions as a string (replace "and" by "&&" and "or" by "||"
'true || true && false'
then put the result in a variable and evaluate
eval('$result = (true || true && false);');
var_dump($result);
This works for sure...
Here is my sample code:
$issue_id = $_POST['issue_id'];
if(!empty($issue_id)){
echo 'true';
}
else{
echo 'false';
}
If I pass 0 to $_POST['issue_id'] by form submitting then it echo false. Which I want is: Condition will be true if the following conditions are fulfilled:
1. true when I pass any value having 0.
2. false when I don't pass any value. i.e: $_POST['issue_id'] is undefined.
I also tried this:
if(!isset($issue_id)){
echo 'true';
}
else{
echo 'false';
}
if(!empty($issue_id) || $issue==0){
echo 'true';
}
else{
echo 'false';
}
The last one is okay, meaning if I pass any value having ZERO then it will echo true. But it will also echo true if I don't pass any value. Any idea?
The last is okay, meaning if I pass any value having ZERO then it echo true. But it also echo true if I don't pass any value. Any idea?
if (isset($_POST["issue_id"]) && $_POST["issue_id"] !== "") {
}
please notice I used !== not !=. this is why:
0 == "" // true
0 === "" // false
See more at http://php.net/manual/en/language.operators.comparison.php
also if you are expecting number you can use
if (isset($_POST["issue_id"]) && is_numeric($_POST["issue_id"])) {
}
since is_numeric("") returns false
http://php.net/manual/en/function.is-numeric.php
Alternatively if you expect number good option is filter_var
if (isset($_POST["issue_id"]) {
$issue_id = filter_var($_POST["issue_id"], FILTER_VALIDATE_INT);
if ($issue_id !== false) {
}
}
since filter_var("", FILTER_VALIDATE_INT) will returns false and filter_var("0", FILTER_VALIDATE_INT) will return (int) 0
http://php.net/manual/en/function.filter-var.php
if(isset($_POST['issue_id'])) {
if($_POST['issue_id'] == 0) {
echo "true";
}
else {
echo "false";
}
}
When you get data from a form, remember:
All text boxes, whether input or textarea will come as strings. That includes empty text boxes, and text boxes which contain numbers.
All selected buttons will have a value, but buttons which are not selected will not be present at all. This includes radio buttons, check boxes and actual buttons.
This means that $_POST['issue_id'] will be the string '0', which is actually truthy.
If you need it to be an integer, use something like: $issue_id=intval($_POST['issue_id']);
#Abdus Sattar Bhuiyan you can also full fill your two condition like below one:
<?php
$_POST["issue_id"] = "0";
$issue_id = isset($_POST['issue_id']) ? (!empty($_POST['issue_id']) || $_POST['issue_id'] === 0 || $_POST['issue_id'] === "0") ? true : false : false;
if($issue_id){
echo 'true';
}
else{
echo 'false';
}
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 have a variable that can be int or bool, this is because the db from where im querying it change the variable type at some point from bool to int, where now 1 is true and 0 is false.
Since php is "delicate" with the '===' i like to ask if this is the correct why to know if that var is true:
if($wallet->locked === 1 || $wallet->locked === true)
I think in this way im asking for: is the type is int and one? or is the var type bool and true?
How will you approach this problem?
Your code is the correct way.
It indeed checks if the type is integer and the value is 1, or the type is boolean and the value is true.
The expression ($x === 1 || $x === true) will be false in every other case.
If you know your variable is an integer or boolean already, and you're okay with all integers other than 0 evaluating to true, then you can just use:
if($wallet->locked) {
Which will be true whenever the above expression is, but also for values like -1, 2, 1000 or any other non-zero integer.
$wallet->locked = 1;
if($wallet->locked === true){
echo 'true';
}else{
echo 'false';
}
will produce:
false
and
$wallet->locked = 1;
if($wallet->locked == true){
echo 'true';
}else{
echo 'false';
}
will produce:
true
Let me know if that helps!
Your solution seems to be perfect, but You can also use gettype. After that You can check the return value with "integer" or "boolean". Depending on the result You can process the data the way You need it.
solution #1. If $wallet has the value of either false or 0, then PHP will not bother to check its type (because && operator is short-circuit in PHP):
$wallet = true;
//$wallet = 1;
if( $wallet && (gettype($wallet) == "integer" || gettype($wallet) == "boolean") )
{ echo "This value is either 'true and 1' OR it is '1 and an integer'"; }
else { echo "This value is not true"; }
solution #2 (depending on what You want to achieve):
$wallet = 0;
//$wallet = 1; // $wallet = 25;
//$wallet = true;
//$wallet = false;
if($wallet)
{ echo "This value is true"; }
else { echo "This value is not true"; }
if(0 == ('Pictures'))
{
echo 'true';
}
why it's giving me 'true' ?
Your string will be evaluated as an Integer, so becomes 0, use this : 0 === 'Pictures' that verifies identity (same value and same type)
Check PHP type comparison tables to understand how comparison operators behave in PHP.
In your case, 'Pictures' becomes "0" and therefore 0 = 0.
Let's check following example:
echo (int)'Pictures'; // 0 => 'Picture' as int
echo 0 == 'Pictures'; // 1 => true, 0 = 0
Use:
if (0 === 'Pictures')
{
echo 'true';
}
The === is strict type operator, it not only checks the value but the type as well.
Quick Test:
if(0 == 'Pictures')
{
echo 'true';
}
else
{
echo 'false';
}
outputs true but:
if(0 === 'Pictures')
{
echo 'true';
}
else
{
echo 'false';
}
outputs false