IF Statement always executed in PHP [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does PHP consider 0 to be equal to a string?
php string comparasion to 0 integer returns true?
it seems that as one has in PHP an if-statement where a function some_function() returns zero
<?php
if( some_function() == "whatever_you_want" ) { ... }
the statement will always be executed since
<?php
echo some_function() == "whatever_you_want";
is then TRUE.
Why behaves PHP in such a counter intuitive way?

This is a defined behavior of PHP when you compare a number value and a string value:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
Use strict value comparison with === or !== and you’re getting the expected result.

Related

Is there really no simple way to check if variable is integer (whether in string or int type)? [duplicate]

This question already has answers here:
Checking that a value contains only digits, regex or no?
(3 answers)
Closed 2 years ago.
I need to check whether a variable contains integers only, and since PHP doesn't really care if numbers are represented in STRING type or not, I don't care either.
But when I use is_int() it returns false for '1' and when I use ctype_digit() it returns false for 1!
And is_numeric(), while accepting both string and float, accepts decimal numbers too.
I'm looking for a function that works like is_numeric() but doesn't accept decimals.
$var[] = 2;
$var[] = '2';
foreach($var as $key => $val){
if( ! is_int($val) && ! ctype_digit($val)) // <-- Any single function equivalent to these two checks?
return false;
}
P.S. I'm not lazy, but this has been bothering me for a long time that is_numeric() accepts string format numbers, but is_int() doesn't!
You can do a regex match to check if variable contains only integers using preg_match function
<?php
if(preg_match('/^\d+$/',$x) === 1){
// your code goes here
}

Confusion in comparing string with a number in php [duplicate]

This question already has answers here:
Comparing String to Integer gives strange results
(5 answers)
How does PHP compare strings with comparison operators?
(4 answers)
Closed 3 years ago.
This is my code
$preference = '151000';
$range = 'above';
if($preference <= $range){
echo "Yes"; die;
}else{
echo "No"; die;
}
This provides 'Yes', i want to know why.
You can see it in the php manual. https://www.php.net/manual/en/language.operators.comparison.php
When comparing a string, number or resource with another string, number or resource:
Translate strings and resources to numbers, usual math
Btw: '151000' is a string, not a number. 15100 would be a number.
Here you basically compare two strings and php uses their ASCII codes to compare them. The first symbol 1 is lower than 'a'.
If you want to compare two strings properly, use function:
strcmp()
If you want compare different types, you can read about PHP type comparison tables.
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
Read more here.

Conditional in PHP [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.

Why is if(empty(strlen(trim($_POST['name'])))) invalid? [duplicate]

This question already has answers here:
Can't use method return value in write context
(8 answers)
Closed 8 years ago.
I have a if statement check to see if a string is empty
if(empty(strlen(trim($_POST['name'])))){
$error_empty = true;
}
gives me this error:
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\requestaccess\index.php on line 51
empty is not a function -- it's a "language construct" that prior to PHP 5.5 can only be used to evaluate variables, not the results of arbitrary expressions.
If you wanted to use empty in exactly this manner (which is meaningless) you would have to store the result in an intermediate variable:
$var = strlen(trim($_POST['name']));
if(empty($var)) ...
But you don't need to do anything like this: strlen will always return an integer, so empty will effectively be reduced to checking for zero. You don't need empty for that; zero converts to boolean false automatically, so a saner option is
if(!strlen(trim($_POST['name']))) ...
or the equivalent
if(trim($_POST['name']) === '') ...

IMPORTANCE OF LOGICAL NOT IN PHP [duplicate]

This question already has answers here:
How does true/false work in PHP?
(6 answers)
Closed 9 years ago.
Is it same in PHP:
if($x!=5)
{
//code
}
VS
$x=5;
if(!$x)
{
//code
}
What about if($x)? Expression in IF statement evaluates to either TRUE or FALSE unlike C where it is either 0 or anything other than 0 (say 1 or more). We can test the expression by using var_dump(!$x) in PHP. So,what about if($x)?
They are not the same.
The first block of code tests whether or not the variable x does not equal 5.
The 2nd block of code tests whether x is not true. Since you declared a value for $x, the statement will be evaluated as false and the content inside the brackets will not execute.
No,it is not same in PHP:
Logical Operator.
! $x Not TRUE if $x is not
TRUE.
Comparison Operators
$x!=5 Not equal TRUE if $x is not equal to 5
Source: PHP Documentation.

Categories