if and else don't assign false - php

I have simple code which is something like this:
$options = new Options();
$page = new Pages();
if($page->page_limit() <= $options->pageno) {
$page->userid = $user_details->userid;
$page->date_of_pub = $_POST['date_of_pub'];
$resultss=$page->page_create();
}
else {
$resultss=false;
}
Then at bottom I am putting a condition
if(isset($resultss) && isset($resultss) == true) {
echo $alert->SuccessMsg("Page created successfully!");
}
if(isset($resultss) && isset($resultss) == false) {
echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");
}
Instead of printing error value even I have set the value of $result = false is shows success message, means its showing $resultss = true statement.
Suggest something. This is so strange. I got the answer thank you so much :)
One more thing.
can you please tell me how can I get rid of this " echo $alert->ErrorMsg" this is so annoying for all the class and functions. I want to make it a single word.

You check the same twice:
isset($resultss) && isset($resultss)==true
You should do:
isset($ressults) && $ressults == true

You have a problem in your logic with isset(). This:
if(isset($resultss) && isset($resultss)==true){echo $alert->SuccessMsg("Page created successfully!");}
if(isset($resultss) && isset($resultss)==false){echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");}
Should be
if(isset($resultss) && $resultss == true){echo $alert->SuccessMsg("Page created successfully!");}
if(isset($resultss) && $resultss ===false){echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");}
In your existing code, the second isset() in each statement is incorrect. In the first one, it is redundant, and you are asking the same thing as if(isset($resultss) && isset($resultss)), which is always true. In the second one, isset($resultss) && isset($resultss)==false could never be true. It's like true && false.

You really don't need to check if the variables are set, since you are setting it in both branches of the if/else. Just do:
if ($resultss) {
echo $alert->SuccessMsg("Page created successfully!");
} else {
echo $alert->ErrorMsg("You Have Been Reached to your maximum page limit");
}

Change your IF conditions to:
if(isset($resultss) && $resultss == true)
isset(x) and isset(x) == true bacially mean the same. What you want is to match two conditions:
the variable IS SET and IT IS EQUAL TO TRUE.
Since I'm not sure what the value might be I'd suggest using these confitions:
if(isset($resultss) && $resultss !== false) //the value is set and it is NOT set to false
AND
if(isset($resultss) && $resultss === false) //the value is set to false

Have you tried setting it to false as default?
$options = new Options();
$page = new Pages();
$resultss = false;
if($page->page_limit() <= $options->pageno){
$page->userid = $user_details->userid;
$page->date_of_pub = $_POST['date_of_pub'];
$resultss = $page->page_create();
}

Related

PHP string compare (if) does not work

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.

PHP - Check GET Variables Exist And Not Empty

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"];

PHP create_function returns false

Situation:
This works:
$functionCode = 'return ($myvar1 == "something") && ($myvar2 != "");';
$newfunc = create_function($functionParamsConcat, $functionCode);
But the problem is that the funcitonCode is dynamic, and the boolean expression inside it can vary (it is taken from the database). The confussing part is that that when i retrieve the SAME functionCode from the database and assign it to the variable, the $newfunc returned by create_function is always false.
Any ideas? Thank you
$functionCode = 'return ($myvar1 == "something") && ($myvar2 != "");';
$newfunc = create_function($functionParamsConcat, $functionCode);
in checking don't use = as it will set it, but use == or ===
for ! use !==

Idea with php logic using operators

I am new to PHP and find it very hard to explain.
I have a PHP navigation script with 5 categories and two variables relevant to my question:
$catname = 'used-cars'; // the same value for all categories
$currentpage; // pages 1 to 5
index.php of my site has $currentpage == '1'
The issue is that I need a logic that will say:
If $catname IS NOT 'used-cars', do something, BUT, IF $currentpage is equal to 1, even if $catname is 'used-cats' do it anyway
I am thinking of something like this:
if($catname != 'used-cars' && !($currentpage > '1')):
endif;
Hope you can help!
This is merely a single or condition. On the right side, $currentpage === 1 will evaluate to TRUE without regard to the value of $catname. If either part of the condition is TRUE, you'll enter the if () block to execute your code there.
if ($catname !== "used-cars" || $currentpage === 1) {
// do your thing
}
This is just:
if (strcmp($catname, 'used-cars') != 0 || $currentpage == 1)
(Careful with the string comparison.)
Alternatively, you could declare it as a boolean first:
$proceed = false;
if($catname != 'used-cars')
$proceed = true;
if($currentpage == 1)
$proceed = true;
if($proceed){
// whatever you want
}
$doflag = 0;
if($catname != 'used-cars')
{
$doflag = 1;
} else if($currentpage == 1) {
$doflag = 1;
}
if($doflag == 1) {
//do something
}
Basically instead of trying to do everything with the block, use the block to set a flag and use the flag to do something.

Catching hell with if's in php?

ok forgive my technique in writing here, but i can't seem to understand why this code recognizes things and then doesn't recognize some other things.
my code:
if (!isset($id)) {
$_SESSION["logged_in"] = False;
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
if ($_SESSION["logged_in"] = False)
{ echo "session variable is set at False"; }
if ( $_SESSION["logged_in"] = True)
{ echo "session variable is set at True"; }
}
now as I see it, if it enters this bit of code at all, the first thing that happens should be that the variable gets set to "false". elsewhere in some code not shown it gets set to true and that part works fine but when i try to force it into this for a false setting it remains true.. can anyone see why this wouldn't get set to "False" at this point in the execution?
You should be using == for comparison here, not = for assignment:
if ($_SESSION["logged_in"] = False)
// ---------------^^^
// Should be
if ($_SESSION["logged_in"] == False)
// Also here:
if ( $_SESSION["logged_in"] = True)
//--------------------------^^
// Should be
if ($_SESSION["logged_in"] == True)
You are making a classic mistake by assigning the variable in your if statement instead of comparing it. So, change your if statements to:
if ($_SESSION["logged_in"] == false)
Instead of (where you are assigning):
if ($_SESSION["logged_in"] = false)
By the way, you're statement has now some duplication since the variable itself already is true of false. So, there is no need to check it against the boolean. So, this can be enough:
if ($_SESSION["logged_in"]) //equals true if user is logged in
if (!$_SESSION["logged_in"]) // equals true if user is NOT logged in
You need == or ===
Using = means its equal :)
if (!isset($id)) {
$_SESSION["logged_in"] = False;
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
if ($_SESSION["logged_in"] == False)
{ echo "session variable is set at False"; }
if ( $_SESSION["logged_in"] == True)
{ echo "session variable is set at True"; }
}
You should be using the == operator rather than = in your if statements.
EG
if ($_SESSION["logged_in"] == False)

Categories