PHP create_function returns false - php

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 !==

Related

Comparison operator not working in if statement PHP

This below is printing hello even though the statement is false
$Originating_country_region = $country_region[$i]['region']; // value of var is AM after assigning
$order_shipping_country_region = $country_region[$i]['region']; // value of var is EU after assigning
if(isset($Originating_country_region) == "EU" && isset($order_shipping_country_region) == "EU")
{
echo "Hello";
}
You're testing the return value of isset, and not the contents of the variables directly. Try:
if((isset($Originating_country_region) && $Originating_country_region) == "EU") && (isset($order_shipping_country_region) && $order_shipping_country_region == "EU"))
This checks that the codes are first of all set, and then checks their values.
It's a useful trick to learn :-)

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

if and else don't assign false

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();
}

PHP Possible ways to write an if

I'm wondering if there is any way to make this code shorter. I'm using 2 if statements and I'm looking to only use one. The things is $user is the session and if you check if $user->userId exists on the same line, the code will error when no session exists. Caused by requesting the userId from an object that does not exist. That's pretty logical but now is there any solution?
if ($user != null) {
if ($user->userId == 1) {
..
}
}
How about using the && operator:
if ($user && $user->userId == 1) {
//...
}
You can add as many sentences as you want, as long as they are properly built, in this case:
if (($user != null) && ($user->userId == 1)) {
or you could simply:
if ($user && ($user->userId == 1)) {
if ($user) just checks if the variable is set, or if it is not null.
You want to use the && operator. It means and
if ($user && $user->userId == 1) {
// do some things
}
You may also want to look into the || operator, it means or.
The && operator will return true ONLY if the two predicates return true.
The || operator will return true as long as one of the predicates return true.

PHP not showing error when I put only single = in if condition

My code as follows:
if($_POST['user_id'] = ''){
//some statement;
}
In the above if condition I have put only single =. PHP is not showing any error but I am getting a white blank page. Does anyone has any clue?
if($_POST['user_id'] = '') means:
$_POST['user_id'] becomes '' .. if ('') // always false
if($_POST['user_id'] == '') means:
$_POST['user_id'] compares to '' .. if ( comparison)
Not sure if trolling or real question...
you said it yourself, you're using a single =. You need 2 to check for equality.
if($_POST['user_id'] == ''){
//some statement;
}
When you use a single equal sign, you're basically "set $_POST['user_id'] to '', then test if it's true). Since '' evaluates to false, you get nothing.
This:
if($_POST['user_id'] = ''){
Tries to assign an empty string to $_POST['user_id']. This:
if($_POST['user_id'] == ''){
Is a comparison. You should almost always be doing the second one - the first one over-rides the value in $_POST, and returns the value of the assignment.
try it
Use it
$user_id = $_POST['user_id'];
if($user_id == ''){
//some statement;
}
inseted of
if($user_id = ''){
//some statement;
}
Or try another one
$user_id = $_POST['user_id'];
if(isset($user_id) && !empty($user_id)){
//some statement;
}
It's not triggering any error because it is a valid condition.
if($_POST['user_id'] = '')
equals to
$_POST['user_id'] = '';
if($_POST['user_id']){
//Boolean comparison of a string. Empty = false. Not empty = true.
}
An example of use:
function division($var1, $var2){
if($var2 > 0){
return $var1/$var2;
return false;
}
if($result = division(50,2)){
//It returned 25 which is true!
}
if(!$result = division(50,0)){
//Returned FALSE because you can't divide by zero!
}

Categories