Null / Text Values getting treated as "0" - php

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"

Related

Dynamically checking the multiple same naming variables with a number on the end

I have about 15 variable with a same name and a number on the end.
For example $Something1 $Something2 $Something3 ........
Before I use them I must check are they null or not.
I wrote some for loop for that. But it is not really good solution. Have a bad smell.
for ($Something = 1; $Something < 16; $Something++){
$SomethingNu = 'Something'.$Something;
if($$SomethingNu === null){
$$SomethingNu = 'some default value';
}
}
I can do something like this. But also, it has a bad smell.
if($Something1 === null){
$Something1 = 'some default value';
}
if($Something2 === null){
$Something2 = 'some default value';
}
if($Something3 === null){
$Something3 = 'some default value';
}
.....
They will have the same default value if are they null some default value in this particular example.
My question is:
What is the best solution of doing that?
You can create a variable parameter function that sets the default value for any number of variables you throw at it:
<?php
function var_checker(&...$vars) {
foreach ($vars as $var_keys => &$var_var) {
if ($var_var == NULL) {
$var_var = "some default value";
}
}
}
$foobar = "something";
$foobar2 = NULL;
var_checker($foobar, $foobar2);
var_dump($foobar);
var_dump($foobar2);
Output:
string(9) "something"
string(18) "some default value"

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

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!

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

How could a PHP value contain a string of zero length not equal to the empty string or null?

I am retrieving some data from an in-house store and in case of failure, I get a very specific response. Calling strlen() on this variable returns the value of zero. It is also not equal to NULL or "". I'm using this code to test:
if ($data === NULL)
{
echo("data was null\n");
}
else if ($data === "")
{
echo("data was empty string\n");
}
else if (strlen($data) == 0)
{
echo("data was length zero\n");
}
This result is outputting data was length zero. What could the variable contain that is zero length, not null, and not the empty string?
Returned value must be false then.
echo strlen(false); // outputs 0
This may not being an answer. I can only answer if you present a var_dump($data); But I think also suprising for me is this:
$data = "\0";
if ($data === NULL)
{
echo("data was null\n");
}
else if ($data === "")
{
echo "data was empty string\n";
}
else if (strlen($data) == 0)
{
echo "data was length zero\n";
}
else
{
echo "something strange happened\n";
}
Output: something strange happened
:)
Try this :
$data = false;
I'm not sure why false has a strlen, but it does.

How to suppress the "Division by zero" error and set the result to null for the whole application?

How to suppress the "Division by zero" error and set the result to null for the whole application? By saying "for the whole application", I mean it is not for a single expression. Instead, whenever a "Division by zero" error occurs, the result is set to null automatically and no error will be thrown.
This should do the trick.
$a = #(1/0);
if(false === $a) {
$a = null;
}
var_dump($a);
outputs
NULL
See the refs here error controls.
EDIT
function division($a, $b) {
$c = #(a/b);
if($b === 0) {
$c = null;
}
return $c;
}
In any place substitute 1/0 by the function call division(1,0).
EDIT - Without third variable
function division($a, $b) {
if($b === 0)
return null;
return $a/$b;
}
Simple as.. well abc*123-pi
$number = 23;
$div = 0;
//If it's not 0 then divide
if($div != 0)
$result = $number/$div;//is set to number divided by x
}
//if it is zero than set it to null
else{
$result = null;//is set to null
}
As a function
function mydivide($divisior, $div){
if($div != 0)
$result = $divisor/$div;//is set to number divided by x
}
//if it is zero than set it to null
else{
$result = null;//is set to null
}
return $result;
}
Use it like this
$number = mydivide(20,5)//equals four
I can't think of a way to set it whenever there's division but I'd use the function and rename it to something like "d" so it's short!
This is a horrible solution, but thankfully, you won't use it because the variable is set to false instead of null.
function ignore_divide_by_zero($errno, $errstring)
{
return ($errstring == 'Division by zero');
}
set_error_handler('ignore_divide_by_zero', E_WARNING);
In your case, I'd create a function that does your division for you.
What about using a ternary operator, like so:
$a = $c ? $b/$c : null;

Categories