php i need null to be null, zero to be zero - php

my table field status is NULL[default], or it is 0, or 1. then i assign to PHP var $status. when value is NULL i want to display no icon, when value is 0, display a gray check image, when value is 1, display a green check image.
trouble is, NULL value shows a gray check image, 0 does not show a check image. somehow NULL and 0 are alike but only in one direction. what i mean is, regardless of how i conditionally test if var is null, not null, null but not zero, they get interpreted wrongly. it is confusing. there must be a simple straight foward way to keep NULL and 0 separate and distinct. i grab the value:
$status = $Card['status']; //from above array.
if ($status == 1) {
$status = '1';
} else if ($status == 0) {
$status = '0';
} else if ($status === NULL) {
$status = 'NULL';
}
then to display the images either gray, green, or none at all i am trying this:
if ($status == '1') {
echo "<img src='../images/status_check_green.png' />";
} else if ($status == '0') {
echo "<img src='../images/porc_check_gray.png' />";
} else if ($status == 'NULL') {
echo "<img src='' />";
}
}
i know i do not need the '' around the values, but i am trying to literalize everything to force valid comparisons. likely no need for someone to try unraveling my code; but to elucidate how to keep NULL and 0 separate. it's like i am missing something fundamental here. btw, when i stuff a js var with the PHP var, it gets the correct value; they just don't follow the comparison like i need them to.
ideas?

HI your issue is procedure order or not type checking the 0 ( depending if you want to catch false )
if ($status == 1) {
$status = '1';
} else if ($status == 0) {
$status = '0'; //<--- this runs on null because (null == 0) is true
} else if ($status === NULL) {
$status = 'NULL'; //<--- this block is un-reachable
}
Because your not type checking with === of 0 null will return true for that condition.
See this sandbox with and example using $status = null;
http://sandbox.onlinephpfunctions.com/code/1f3dd9d83d0026aa0f682b61bed2ba858ae285aa
Outputs:
'0'
If you change it to this
if ($status == 1) {
$status = '1';
} else if ($status === 0) {
$status = '0';
} else if ($status === NULL) {
$status = 'NULL';
}
As you can see here using the same setting for $status
http://sandbox.onlinephpfunctions.com/code/d86b0c60c06338d2d6ee1c1fa9d3fa7e08a22663
Outputs
'NULL'
The other way to fix it would be to switch them so the more specific one is first.
if ($status == 1) {
$status = '1';
} else if ($status === NULL) {
$status = 'NULL';
} else if ($status == 0) { //I would prefer if(!$status){ but I'm lazy
$status = '0';
}
Then 0 would catch false as well as 0 but not null as the block above it will catch it first. You can see this last one here
http://sandbox.onlinephpfunctions.com/code/3028f5826dcede29b14dd8cfc03618ea5830c12c
Which also outputs
'NULL'
Cheers!

Related

Show where is equal to 0 (PHP)

I have a form, and I want to validate it with PHP, for that I going to make just one if the problem is I want to detect where is the variable that equal to 0
My code
if(isset($_POST["submit"])){
$name = $_POST["name"];
$lname = $_POST["lname"];
if($name == "" || $lname == ""){
echo "Please, Enter All informations";
}
else {
$namepr = preg_match("/^[A-Za-z]{3,}$/",$name);
$lnamepr = preg_match("/^[A-Za-z]{3,}$/",$lname);
if($namepr == 0 || $lnamepr == 0){
<!-- here I want to select the variable that equal to 0 -->
}
else {
echo "Your name is : ".$name."<br>Your Last name is : ".$lname
}
}
}
my question is in the comment part, I want to show the variable equal to 0, I have two variable, I want to select the variable equal to 0
For your issue, you can use a ternary operator:
if($namepr == 0 || $lnamepr == 0){
$myVar = ($namepr == 0 ? $namepr : $lnamepr);
// do something with $myVar
}
or of course, a standard if block:
if($namepr == 0 || $lnamepr == 0){
if($namepr == 0){
$myVar = $namepr;
} else {
$myVar = $lnamepr;
}
// do something with $myVar
}

If else not working properly

So I made page with unban request and in users table I save if user already sent request or not so I don't have multiple unban requests from one user.Now when I check if user sent request it's not working.In database it stands 0 and it's still showing me error pop out.
Here is code, thanks for help in advance
if(isset($_POST['btn-unban_req']))
{
if($unban_sent = 0)//THIS IS WHERE I CHECK
{
//MY THIGNS HERE
if($connection ->query($unbanquery) === TRUE)
{
//MY THIGNS HERE
if ($connection->query($sentquery) === TRUE)
{
}
else
{
echo $connection->error;
}
}
else
{
}
}
else // AND I GET THIS ERROR EVEN IF IT STANDS 0 IN DATABASE
{
echo "Unban already sent!";
}
}
You are not comparing the values, this is assigning the values:
if($unban_sent = 0) // assigning values
This should be:
if($unban_sent == 0) // comparing values
Basic Example:
Lets say,
1 = 1 its assigning
1 == 1 its checking the condition will return TRUE.
For more help: PHP Comparison Operators
You're using this code which is wrong.
$unban_sent = 0
$unban_sent = 0 means to assign 0 to $unban_sent
It should be:
$unban_sent == 0
$unban_sent == 0 means $unban_sent is equal to 0
== is for comparison, = is for assignment, and === is for identical or same type.
More information at http://php.net/manual/en/language.operators.comparison.php.
In line 3 you missed a = for comparision. Instead, you set $unban_set to 0
Try it with this code:
if(isset($_POST['btn-unban_req']))
{
if($unban_sent == 0) //<- Now you are checking it here
{
//MY THIGNS HERE
if($connection ->query($unbanquery) === TRUE)
{
//MY THIGNS HERE
if ($connection->query($sentquery) === TRUE)
{
}
else
{
echo $connection->error;
}
}
else
{
}
}
else // AND I GET THIS ERROR EVEN IF IT STANDS 0 IN DATABASE
{
echo "Unban already sent!";
}
}

Null / Text Values getting treated as "0"

I want my function to do the following:
if price is 0, return free message.
If price is positive, show the price.
If price value is unavailable - for example: if the database cell for the price is null, or have a value like unknown, then return unavailable message.
So I came with this code:
function get_rate($foo,$bar) {
if ($bar== "something") {
//$test= "testing";
} elseif ($foo== 0) {
$message = 'Free';
} elseif ($foo> 0) {
$message = '$'.$foo;
} else {
$message = 'Unavailable';
}
return $message;
}
HTML:
<?= get_rate( $price) ?>
But for the values:
$price="unknown"; or if $price is null, I'm still getting "Free" message.
Now you have two solution for this:
Solution 1:
else if ($foo === 0) // use === for checking value and datatype
Solution 2:
else if ($foo == 0 && $foo != null) // adding != null
Value "" or null treated as 0, if you also check the data type using === this issue will resolve else use second solution.
After your comment, sharing a basic example you will get the idea:
$foo = 'bla';
var_dump($foo);
It will give you string(3) "bla"

How to check the value and type of a unknown var

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

Multiple else if not working correctly PHP

I'm having an issue with a login script. The rest of it works fine, but there is something odd happening here. The issue is that even though $IPCHK is returning true the elseif function does not execute. It only executes when I set $IPCHK to jibberish. Any help would be great. Thanks in advance
if ($Numrows == 0)
{
if ($Fail >= 3)
{
$Connection = connectToDb();
//return true, false,pending
$IPCHK = checkIP();
$IPCHK = true; //forcing it to be true and still broke
//If no ip id there
if($IPCHK == false)
{
$IP = getIP();
$Query = "INSERT INTO ip VALUES ('','$IP',Now())";
mysqli_query($Connection, $Query)
or die(error(mysqli_error($Connection)));
echo "You have failed to login too many times";
echo "<br />Please <a href='login.php'>try again</a> later.";
$Lock = true;
}
//If ip is there but timer is not up
elseif ($IPCHK == 'pending')
{
echo "You have failed to login too many times";
echo "<br />Please <a href='login.php'>try again</a> later.";
$Lock = true;
}
//Timers Up
elseif ($IPCHK == true) //here does not execute when it returns true
{
$_SESSION['FailedLogin'] = 0;
$Lock = false;
}
else
{
error("End of if check");
}
}
else
{
$Fail = 3 - $Fail;
$_SESSION['FailedLogin'] = $_SESSION['FailedLogin'] + 1;
$Error = $Error."<br />You have ".$Fail." attempts remaining";
}
}
In your Condition you have
elseif ($IPCHK == 'pending')
then
elseif ($IPCHK == true)
the second else will never execute because $IPCHK == 'pending' means also that $IPCHK == true
if you want to execute your second else you have to change the seconde condition to somethig like this
elseif($IPCHK == 'done')
or simply use === like this
elseif($IPCHK === 'pending')
then
elseif($IPCHK === true)
Lamari Alaa is correct, and the relevant documentation entry on type juggling can help understand why.
The following script outputs: boolean = string:
$test = true;
if( $test == 'pending' ) {
echo 'boolean = string';
} else if ( $test ) {
echo 'boolean != string';
}
This is because the string 'pending' is being coerced into a boolean value before being compared to the boolean value true. Since it evaluates as true, the first condition is taken. Consider replacing == 'pending' to === 'pending'

Categories