PHP's in_array bevahior surprises me [duplicate] - php

This question already has answers here:
A problem about in_array
(3 answers)
PHP's variable type leniency
(3 answers)
Closed 5 years ago.
While validating a dynamic saving method in a controller I wanted to make sure a given case only accepts 0 and 1 as valid values. When I tried to manipulate the input form, submitting 'aaa' as a value the following still returns true. Why is that?
var_dump(in_array('aaa', [0, 1])); // true, I was expecting it to return false

You need to use the "strict" setting, to force the function to check the types of the elements as well:
var_dump(in_array('aaa', [0, 1], true));
http://php.net/manual/en/function.in-array.php states
If the third parameter strict is set to TRUE then the in_array()
function will also check the types of the needle in the haystack.
The reason it returns true is because a string is truthy, and so is 1.
if( "aaa" ){ echo "you will see me"; }

Related

PHP in_array() Ignoring leading 0 (zero) in the first parameter [duplicate]

This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 1 year ago.
$array_list = ['125268', '526985', '8566958'];
in_array('125268', $array_list); //Returning True
in_array('0125268', $array_list); // Also Returning True
I want second one should return false as it have zero at the beginning. But in_array() is ignoring the zero.
Any Suggestions?
You have to set the third parameter. This is an optional parameter and is of boolean type. This parameter specifies the mode in which we want to perform the search. If it is set to TRUE, then the in_array() function searches for the value with the same type of value as specified by the first parameter. The default value of this parameter is FALSE.
$array_list = ['125268', '526985', '8566958'];
in_array('125268', $array_list, true); // Returning True
in_array('0125268', $array_list, true); // Returning False
Visit here for more information.

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']) === '') ...

Return a value if a PHP GET doesn't exist? [duplicate]

This question already has answers here:
Check if url contains parameters [duplicate]
(3 answers)
How to verify if $_GET exists?
(7 answers)
Closed 9 years ago.
I have lots of PHP statements along the lines of:
$getvalue = $_GET['valueiwant'];
In some scenarios not all variables are available. So, let's say 'valueiwant' doesn't exist in the URL string, how can I return a value based on the fact it doesn't exist?
For example if 'valueiwant' can't be found set $getvalue to -1
Currently it appears the value defaults to 0 and I need to be equal less than 0 if it doesn't exist.
Any ideas?
thanks
I always use
$getvalue=isset($_GET['valueiwant'])?$_GET['valueiwant']:-1;
Use of the isset() function checks if the offset exists, and returns a boolean value indicating it's existence.
This means that you can structure an if statement around the output.

IF Statement always executed in PHP [duplicate]

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.

empty($_POST['var']) returns true when the input value is '0' [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php: ‘0’ as a string with empty()
I have an input in a form like so
<input name='var' type='number'>
When the form is submitted, input is sent to a PHP file through POST.
On the PHP file, it checks to see if the input is filled out by checking
empty($_POST['var'])
When I enter '0' (zero) into the textbox and submit the form, the PHP code returns '1' for empty($_POST['var']) even though I have tried print_r($_POST) and 'var' clearly has the value of '0'.
Is this supposed to happen? Do I just need to also check for == 0 for this exception? Thanks.
To check whether the field is filled or not, please use isset($_POST['var']) instead of empty($_POST['var']).
You can't use empty only to check whether it is empty string or string only contains blank char.
Use like this:
function isEmpty($key) {
return isset($_POST[$key]) && trim($_POST[$key]) === '';
}
empty() returns true if the variable being checked is undefined, 0, empty string '', false, an empty array, or null. So the result you are seeing is 100% correct.
In the case where 0 is a legitimate value, use is_set().

Categories