This question already has answers here:
php is null when empty? [duplicate]
(10 answers)
Closed 5 years ago.
The following code gives TRUE,FALSE,FALSE,FALSE,
I dont understand the TRUE response on empty arrays.
Someone has an explanation?
$results=array();
// Case 1 : Empty array
$myArray=array();
array_push($results, ($myArray==null));
array_push($results, ($myArray===null));
// Case 2 : Non Empty array
$myArray=array(1);
array_push($results,($myArray==null));
array_push($results,($myArray===null));
//
foreach ($results as $result) {
if ($result) echo("TRUE,"); else echo ("FALSE,");
}
Response here : PHP treats NULL, false, 0, and the empty string as equal, see stackoverflow here php is null or empty?
... and empty arrays
Need to be very careful so
Related
This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 1 year ago.
I'm fairly new to PHP. I'm trying to filter out an array of items that contain a specific string. In order to then push those items to array, I need to pass the array to the function as a reference (or so my research has told me) however I can't seem to get it to work, any ideas? Thanks all.
$filteredS3Results = array();
function filterResults($var, &$filteredS3Results) {
if (strpos($var, '008-20160916') !== false) {
array_push($filteredS3Results, $var);
};
};
array_filter($s3Results,"filterResults");
The callback function just receives one argument, the array element being tested. It should return a boolean value that indicates whether the element should be included in the filtered result.
$filteredS3Results = array_filter($s3Results, "filterResults");
function filterResults($var) {
return strpos($var, '008-21060916') !== false;
}
This question already has answers here:
MySQL and PHP - insert NULL rather than empty string
(10 answers)
Closed 3 years ago.
I can't make the empty value to be checked as NULL in database instead of leaving an empty space when I am inserting a value.
I don't want to leave an empty/blank space for an entry when it has no value.
So if there is no file selected, make that entry into NULL
Is it unnecessary to be in NULL?
if (empty($_FILES['logo']['name'])) {
$code = NULL;
} else {
}
It's a normal problem with null-able strings. For a quick solution, you can change your code to:
$code = NULL;
if( !empty($_FILES['logo']['name']) ){
...
}
For more details, you can check this question:
MySQL and PHP - insert NULL rather than empty string
$logo = (!empty($_FILES['logo']['name']) ? "logo" : "NULL");
try this
This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 6 years ago.
It is known that in_array() function can be used to check if an array contains a certain value. However, when an array contains the value 0, an empty or null values pass the test, too.
For example
<?php
$testing = null;
$another_testing = 0;
if ( in_array($testing, [0,1,5,6,7]) )
echo "Found";
else
echo "Not Found";
echo "<br>";
if ( in_array($another_testing, [0,1,5,6,7]) )
echo "Found";
else
echo "Not Found";
?>
In both cases "Found" is printed. But I would like the first case to print "Not Found", and the second case - "Found".
I know I can solve the problem by adding an extra if statement, or by writing my own function, but I want to know if there are any built-ins in PHP that can perform the checks.
This behavior is explained by the fact that null == 0. But null !== 0. In other words, you should check for the types as well.
You don't need another function. Simply pass true as the third parameter:
in_array($testing, [0,1,5,6,7], true)
In this case in_array() will also check the types.
This question already has answers here:
PHP in_array isn't finding a value that is there
(3 answers)
Closed 9 years ago.
I am using a simple php script to look for an element in an array like
$restricted = array('root/base', 'root2' );
print_r($restricted);
if( array_search('root/base', $restricted) ){
echo "1";
} else {
echo "0";
}
But I am always getting the following output
Array ( [0] => root/base [1] => root2 ) 0
This means that the array_search is failing to find the element in the given array. Can anybody show some light on whats happening?
I tried to replace array_search() with in_array() also. But that too returned the same error.
From PHP DOC
array_search — Searches the array for a given value and returns the corresponding key if successful
The index is 0 that is why you think its fails
Use
array_search('root/base', $restricted) !== false
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Checking if array is multidimensional or not?
How do I check whether an array is multidimensional or not in PHP?
Use count twice, one with single parameter, and one with recursive mode
if (count($myarray) == count($myarray, COUNT_RECURSIVE))
{
echo 'MyArray is not multidimensional';
}
else
{
echo 'MyArray is multidimensional';
}
count(array,mode)
array ---Required. Specifies the array or object to count.
mode ---Optional. Specifies the mode of the function. Possible values:
0 - Default. Does not detect multidimensional arrays (arrays within arrays)
1 - Detects multidimensional arrays
Note: This parameter was added in PHP 4.2
Multi-dimensional arrays in PHP are simply arrays containing arrays. So a simple function for this could be written as
function is_multidim_array($arr) {
if (!is_array($arr))
return false;
foreach ($arr as $elm) {
if (!is_array($elm))
return false;
}
return true;
}
This will run through every element of $arr and check whether it's an array. Should it encounter an element that is not an array, it will return false. Otherwise, return true.