PHP checking for NULL value - php

Just out of curiosity (and a bit of necessity):
if(! is_null($var)){
//do something
}
Is the above statement the same as
if($var != NULL){
//do something
}

No they are not the same.
The is_null function compairs the type also.
Example:
var_dump(is_null(0)); // bool(false)
var_dump(0 == NULL); // bool(true)
var_dump(0 === NULL); // bool(false)
So in your case
if(! is_null($var)){
//do something
}
Would be the same as
if($var !== NULL){
//do something
}

Yes this is (almost) correct, you can test this yourself:
$emptyvar1 = null;
$emptyvar2="";
if(is_null($emptyvar1) && $emptyvar1 == NULL){
echo "1";
}
if(is_null($emptyvar2)){
echo "2";
}
if($emptyvar2 == null){
echo "3";
}
if($emptyvar2 === null){
echo "4";
}
This will print 1 and 3.
because an empty string is equal to null if you only use 2 times =
if you use 3 times = it aint.
=== also checks object type
== only checks value

I'm not sure what exactly you're testing, but on:
a) $var = NULL;
neither of the statements triggers,
b) $var = 0;
is_null triggers and
c) $var = ''; is_null triggers aswell.
So the statements above are definitely not coming to the same conclusion.
See for yourself:
echo 'testing NULL case<br>';
$var = NULL;
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
echo 'testing 0 case<br>';
$var = 0;
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
echo 'testing empty string case<br>';
$var = '';
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
this outputs
testing NULL case
testing 0 case
var is_null
testing empty string case
var is_null

Related

Is it possible to do a statement within a statement?

So I'm trying to do something like this
if($id == 1 || $id == 2 || $id == 5 || $id == 8) {
echo 'test';
} elseif($id == 6) {
echo 'test2';
} else {
echo 'error';
}
It's so I can show a specific message (with html etc in the finished version) when the id obtained via post method is different.
EDIT: The issue is the code repeats itself twice.
How could I go about resolving this?
Thanks to anyone who contributes into helping me with this!
Use in_array().
$arr = [1,2,5,8];
$id = 1;
if(in_array($id, $arr)) {
echo 'test';
} elseif($id == 6) {
echo 'test2';
} else {
echo 'error';
}
https://3v4l.org/T5giX
As i see you have more than 3 id to compare so easy Solution is take all id as an array
And use in_array.
<?php
$id = 3;
$myid = array(1,2,5,8);
if(in_array($id,$myid)) {
echo 'test';
} elseif($id == 6) {
echo 'test2';
} else {
echo 'error';
}
Also You Can Use PHP Switch Statement
<?php
$id = 6;
switch ($id) {
case 1:
case 2:
case 5:
case 8:
echo "Passed .!";
break;
case 6:
echo "Passwd 2!";
break;
default:
echo "Failed .!";
}
to make this statement in one line use ternary operator. hope it satisfy you.
echo ( ($id == 1 || $id == 2 || $id == 5 || $id == 8) ? "test" : (($id == 6) ? "test2" : "error"));

PHP Zero integer is being evaluated as false

As the title says, PHP seems to be evaluating the integer value 0 as false.
Take for example this snippet:
http://sandbox.onlinephpfunctions.com/code/13d885fb68359a3154999c2ef85db7c913c49bc5
<?php
if($exists = checkDup()){
echo $exits;
}
else{
echo "error!";
}
function checkDup ($foo = 'blah', $bar = 'blah'){
if ($foo == $bar) return (int) 0;
return false;
}
As you can see, despite casting the reply as an int PHP parsing the return as false which in incorrect.
PHP is evaluating a lot to false ;)
For example null, '', 0
You have to include a type check as well, you can do so by using === or !==
$exists = checkDup();
if($exists !== false){
echo $exits;
}
else{
echo "error!";
}
function checkDup ($foo = 'blah', $bar = 'blah'){
if ($foo == $bar) return 0;
return false;
}
You should use if($exists = checkDup() !== false)
0 == false; //true
0 === false; //false
When you don't specify the a boolean expression in the if, it will execute the == operator

checking zero is null or not in php

i am working on validation and comparisons!! i have a field that can contain the value $val=0 or $val="some-value" or $val="" or $val=0 basically i want the $val="0"or $val=0 to be validated as true..
if($val){
//works for $val="some-value"
//doesnot work for $val=0 or $val="0";
} else
{
//works corrent for $val=""
}
one conditional approach i used is
$val="";
if($val || $val==0){
echo "true";
}
else
{
//should be false but it is true
echo "false";
}
did you try this?
$val = "";
if ($val == '0') {
echo "TRUE";
# code...
}
elseif ($val == "") {
echo "FALSE";
}
There is a useful php native function is_null
if (is_null($val) || $val === "") {
//invalid
} else {
//valid
}
You can use PHP integer casting & can do it like this:
if ((int) $val === 0) {
return true;
} else {
return false;
}
Hope this helps!

If a $var1 equals a string then set $var2 equal to another string

Basically if $var1 = 'Refi' then I want $var2 = 'Refinance'. If $var = 'Purch' or 'Purchase' then I need $var2 = 'Purchase'.
$var2 = '';
if (!(strcasecmp($var1, 'Refi') === false)) {
$var2 = 'Refinance';
}
if (!(strcasecmp($var1, 'Purch') === false) || !(strcasecmp($var1, 'Purchase') === false)) {
$var2 = 'Purchase';
}
The output I am getting is just defaulting to 'Purchase'. I also need the strings to be case insensitive. I don't know what is going on with it, the logic seems correct in my help.
strcasecmp does not return a bool. It returns an int.
$var2 = '';
if ((strcasecmp($var1, 'Refi') == 0)) {
$var2 = 'Refinance';
}
if ((strcasecmp($var1, 'Purch') == 0) || (strcasecmp($var1, 'Purchase') == 0)) {
$var2 = 'Purchase';
}
Check out documentation for details. http://php.net/manual/en/function.strcasecmp.php

Put IF condition inside a variable

Is there any way to put conditions within a variable and then use that variable in an if statement? See the example below:
$value1 = 10;
$value2 = 10;
$value_condition = '($value1 == $value2)';
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
I understand this may be a bizarre question. I am learning the basics of PHP.
No need to use strings. Use it directly this way:
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
Or to evaluate, you can use this way using ", as it expands and evaluates variables inside { ... }.
I reckon it might work! Also, using eval() is evil! So make sure you use it in right place, where you are sure that there cannot be any other input to the eval() function!
Depending on what you are trying to do, an anonymous function could help here.
$value1 = 10;
$value2 = 10;
$equals = function($a, $b) {
return $a == $b;
};
if ($equals($value1, $value2)) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
However, I would only do it like this (and not with a regular function), when you make use of use ().
== operator evaluates as a boolean so you can do
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
Just assign result of comparision to variable.
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
An if statement tests a boolean value. You could have something like this:
if (true) {
You can assign boolean values to a variable:
$boolValue = true;
You can use variables in your if statement:
if ($boolValue) {
// true
In your example:
$value_condition = $value1 == $value2; // $value_condition is now true or false
if ($value_condition) {

Categories