What I'm trying to do:
treat the POST data from a multi-select input with the array_diff() function
Initial code:
$relations_to_delete=array_diff($selectedEnjeuxMetiers,$this->request->data['EnjeuxMembership']['EnjeuxMetier']);
Probem: It was not working when nothing was selected in the multiselect input
Current solution:
if(!empty($this->request->data['EnjeuxMembership']['EnjeuxMetier'])){
$relations_to_delete=array_diff($selectedEnjeuxMetiers,$this->request->data['EnjeuxMembership']['EnjeuxMetier']);
}else{
$relations_to_delete=$selectedEnjeuxMetiers;
}
This solution works. !=null was not working, nor gettype()=="array"
Question: Could anyone could explain why the if(!empty()) test is necessary, and if the problem comes from the POST data or the array_diff function?
EDIT: It works with gettype()=="array". The problem was that the type when there is no data is not an empty array but an empty string.
Additional info: CakePHP docs about the way Post data are converted to an array.
With the function "empty()", the variable is considered empty if it is equal to:
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
The value should be coming "" or NULL when no option is selected.
The problem was that the type when there is no data is not an empty array but an empty string.
Related
In a form, I have a multiple select, where the input can have 0 or X values.
Problem is, if user doesn't send value, in ajax, the variable is an empty array and it is not sent in the request : the object property is not set to null and it keeps its old value, and validation keeps ok.
The only trick I found is to send a variable [0], and in this case it works.
Do you have any idea how can I empty an object property in validation ?
Please read about Zend\Filter\Null: https://packages.zendframework.com/docs/latest/manual/en/modules/zend.filter.set.html#null
Per default this filter works like PHP‘s empty() method; in other words, if empty() returns a boolean TRUE, then a NULL value will be returned.
If you use this filter in your Input Filter for this field, it will return a null instead of an empty array.
I'm facing this dilemma, basically I want to create an error object if a certain task isn't met. Now to understand what I've got to send back to the user, I need to check to see if that error object is either empty or has data. The issue is this:
$obj = new stdClass();
var_dump(empty($obj)); // returns false
As you can see in this example, it's returning false instead of true as it's empty.
$o = new stdClass();
$a = array();
var_dump(empty($o));
var_dump(empty($a));
Example
This works perfectly fine for an array, but does anyone know why this is happening for objects?
I've read answers like this which state to cast it as an array, but my question is this:
Why does it return false when it's empty? What is the logic behind it?
If I wanted an array, I would've started with that.
From php.net:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
Every object you create won't be "empty".
multi-select lists are named thusly: <select name="list[]" multiple>
When the form is submitted, if the user hasn't selected anything, then I wish to ignore that input.
I'm trying:
if (isset($_POST["list"]))
and
if (count($_POST["list"]))
but that throws an error, presumably because PHP doesn't receive the array unless at least one item is selected. If the user does select at least one of the options, there is no error.
The complaint is that "list" is a nonexistent index.
I need at least one of these solutions:
To force the submit to send an empty array so the index in $_POST will be legal, OR
I need to test for the existence of the index variable without throwing an error.
I'm also trying to use the try-catch syntax but having issues with that as well. I have a separate post open for that issue.
any suggestions on detecting a listbox with nothing selected?
Thanks,
Dana
Try using empty():
if (!empty($_POST["list"])) {
For your case, !empty() would be TRUE if list[] is set and if it has at least one value.
From the documentation:
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
In PHP, can a value from the global $_POST array be something else than an array or a string?
The goal is to not have to check if everything is something else than an array or string in a script. If I know what type a variable has, I don't have to do some weird validation. If I expect a string, I don't have to cast everything to a string to make sure it is one.
$_POST["key"] = true;
var_dump($_POST["key"]); // bool(true)
The values set by the environment are strings though.
As per the documentation http://php.net/manual/en/reserved.variables.post.php
An associative array of variables passed to the current script via the HTTP POST method.
So all the data sent is in associative array, in key value pair. There are Numbers (int, float etc), Strings, Arrays (of numbers or strings) and Objects data types.
Using the rule of elimination we can remove the Object from the supported data type, and the remaining left are strings, numbers and array.
Now, if you see the form, the input fields are taking strings, there is no indication that the value entered in the input field is number or string. So to be on safe side all the values which are posted are in strings. The array of elements also have the string values.
When you get the value in $_POST it is simply an array and you can override it any time
$_POST['username'] = 1;
var_dump($_POST['username']); // int (1)
I hope this make some sense
You can cast it to whatever you wish ^^
intval($_POST['INTEGER']);
or simply
(int)$_POST['int']
I am trying to read some values from the membase.
I observer that when there is any integer the following command is not working.
var_dump($memcache->get("keyset123"));
print_r($memcache->get("keyset123"));
If the get result is a string the above command prints.
If the get result is a Integer the above commands are printing none.
vardump prints =string(0) ""
print_r prints none.
can you please tell me what is the issue
That is because the $memcache->get() call is returning a string value. Your problem lies elsewhere (likely deeper within the code in use), not within var_dump().
Look into what you're storing within whatever is inside of the variable $memcache.
var_dump($memcache->get("keyset123"));
//outputs
//string(0) ""
Memcached is storing an empty string at the key "keyset123", otherwise you would be getting FALSE (key doesn't exist) or NULL (key exists, but no value exists)