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
Related
When i am using this code it show me correct result
<?php
$string = 'there is some text';
$find = 'some';
function iscontain($string,$find){
$check = strpos($string, $find);
if ($check === false) { return 0; }else{ return 1; }
}
if(iscontain($string,$find)){
echo "true";
}else{
echo "false";
}
//output: true
?>
But when i change false into true and also change return values like if check is true so return 1 else 0 so logically is correct but result not showing correct
<?php
$string = 'there is some text';
$find = 'some';
function iscontain($string,$find){
$check = strpos($string, $find);
if ($check === true) { return 1; }else{ return 0; }
}
if(iscontain($string,$find)){
echo "true";
}else{
echo "false";
}
//output : false
?>
Why both results are different ..? Thanks.
This happens because strpos never returns true.
It can return false or integer number.
So in your second function else branch always run returning 0 which is considered falsy.
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!
I'm trying to combine these two:
$test1 = (isset($_GET["var1"]) && (isset($_GET["var2"]);
$test2 = test = !(strpos($_GET['var1'], '{') !== FALSE || strpos($_GET['var1'], '}') !== FALSE );
This is what I tried thats giving me an unexpected ; parse error parse error:
$test1 = (isset($_GET["var1"]) &&
!(strpos($_GET['var1'], '{') !== FALSE ||
strpos($_GET['var1'], '}') !== FALSE ) ||
(isset($_GET["var2"]) &&
!(strpos($_GET['var2'], '{') !== FALSE ||
strpos($_GET['var2'], '}') !== FALSE );
EDIT
what $test1 does is checks to see if var1 and var2 are in the url
what $test2 does is checks to see if var1= or var2= has { or } in the string. I'm just trying to put this all into 1 variable
Bad parenthesis on line 1, and I assume you want that to be '$test', not 'test' on line 2:
<?php
$test1 = isset($_GET['var1']) && isset($_GET['var2']);
$test2 = $test = !(strpos($_GET['var1'], '{') !== FALSE || strpos($_GET['var1'], '}') !== FALSE );
This code works:
$_GET['var1'] = "vvv";
$_GET['var2'] = "ddd";
$test1 = (isset($_GET["var1"]) && !(strpos($_GET['var1'], '{') !== FALSE || strpos($_GET['var1'], '}') !== FALSE ) ||
(isset($_GET["var2"]) && !(strpos($_GET['var2'], '{') !== FALSE || strpos($_GET['var2'], '}') !== FALSE )));
print $test1;
In short this is a mess. Don't do that you'll never recognize what it does after 6 month or so.
This is a little verbose, but may change the way you're thinking about this.
$test1 will have a truthy value if both vars are set. test2 will have a truthy value if the either { or } exists in the string.
function testBrackets($string)
{
$firstBracket = strpos($string, '{');
$secondBracket = strpos($string, '}');
if ($firstBracket == false and $secondBracket == false)
{
return false;
}
return true;
}
if (isset($_GET['var1']) and isset($_GET['var2']))
{
$test1 = true;
}
$test2 = 0;
if (isset($_GET['var1']))
{
$test2 = $test2 + testBrackets($_GET['var1']);
}
if (isset($_GET['var2']))
{
$test2 = $test2 + testBrackets($_GET['var2']);
}
I've a simple function that apprently puts me up with a lot of trouble. The function is:
function valid_mail($email) {
$atpointers = strstr($email, "#");
$spacepointers = count(explode(" ", $email));
$dotpointers = strstr($email, ".");
$ltpointers = strstr($email, "<");
$gtpointers = strstr($email, ">");
$illegalpts = $ltpointers + $gtpointers;
if($atpointers >= 2 || $dotpointers == 0 || strlen($email) <= 6 || $illegalpts >= 1) { return false; } else { return true; }
}
And calling it in the context:
if(valid_mail($email) === false) { // Code } else { // Code }
The problem is apparently it only returns false. Any ideas for why this happens ?
strstr returns a substr of the string being checked, not a length. Use strpos instead.
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