var_dump not printing the integer values - php

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)

Related

Dont display varaibile if it contains no value (PHP|

I am using SOAP to get some data from other service, i get XML and convert and display it with PHP. The thing is sometimes there is no value for it in XML file and i dont know how to display some message like "This thing was not set", instead of that i am just getting notice with
Notice: Array to string conversion in \data2.php on line 636
Array
What i tried:
if(isset($claim ['vehicle']['engine-cc']))
echo var_dump($claim ['vehicle']['engine-cc']);
output: array(0) { }
After that i try something like:
if(isset($claim ['vehicle']['engine-cc']))
echo var_dump(isset($claim ['vehicle']['engine-cc']));
Output: bool(true)
So it looks i am doing things bad, can you gimme some advise how can i fix it?
p.s. I know i can stop displaying errors and notices but its not the way how i want to "fix" it
you no need to echo after var_dump already have var_dump(used for printing array) or may be you have not an array for $claim ['vehicle']['engine-cc'] it's a string
var_dump($claim ['vehicle']['engine-cc']); // for array
echo $claim ['vehicle']['engine-cc'] // for string
returning bool(true) cause you have isset()(will return boolean true/false)
var_dump(isset($claim ['vehicle']['engine-cc']));
For printing array values you only need :-
var_dump($claim ['vehicle']['engine-cc']); or var_dump($claim);
will return you complete array output
for any check empty values try
if(!empty($claim ['vehicle']['engine-cc']))
var_dump($claim ['vehicle']['engine-cc']);
You can do it by simply echo ing the message.
`if( ! isset($claim ['vehicle']['engine-cc']))
echo 'engine-cc - This thing was not set';`
You need to use option "SOAP_SINGLE_ELEMENT_ARRAYS" most likely.
By default SOAP returns empty or a single value for a SOAP attribute as a string, not an array. Adding option SOAP_SINGLE_ELEMENT_ARRAYS into SOAP client request would force to return the value as an empty ARRAY rather than empty string. See example here.

Convert key-value JSON to object and refer individual values directly

I am making a web app. In one part of it, I have JS send a string(in json format) to PHP.
The value php receives is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
Now, this is saved in a variable $meta. The problem I am facing is, as to how do I convert this string into an object and reference each individual entry separately.
I have tried json_decode and json_encode
and then I have referenced each variable using $meta.["date"] and $meta.date but nothing seams to work. I am getting just { as the output.
What's the correct way to do this?
$str = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$obj = json_decode($str);
echo $obj->date;
// 24-03-2014
Usually a $my_obj = json_decode($_POST['jsonstring'], 1); (true supply means it'll be returned as an assoviative array) should be the way to go. If I were you I'd probably try a var_dump($my_obj); to see what actually comes through. If it doesn't work you'll want to make sure that you correctly submit a valid json string, e.g. JSON.stringify();
You should check out the PHP doc page for json_decode here.
By default, unless you pass true as the second parameter for json_decode, the function call will return an object, which you can access the members of by using:
$meta->date
The arrow operator will allow you to access object values, not the square brackets or a dot.

Unit testing - how many test cases here

I have a method that takes an array as an argument, and returns true or false depending on the presence of a particular value.
In this scenario how many test cases should be written?
I think 3:
If the value is present
If the value is not present
If the array is empty (could be covered by 2 though?? )
I can think of 3 test cases:
If the array is not empty (or not null)
If the value is valid or not (I can pass an object where it expects a string :) )
If the value is present in array
It is the code of the function you want to test, so you cannot tell how many test cases are useful. Think again what your code does, how will the value be found?
An example: If your code tries to find a value with a certain name, and you make a string comparison, then think of the problems that can arise with string comparisons -> should the key be found case (in)sensitive, is null equal to an empty string, how does it handle duplicates and are other types converted correctly to strings (type juggling)?

Using POST data in array_diff with CakePHP

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.

PHP integer type getting lost on function call

I have some code that pulls database primary keys and iterates through them by calling a function.
When I get the key from the database and do a is_int($key) it returns true.
I then call a function: thisfunction($key)
In the calling function, I made it so that you could pass in the $key and that function loads the row for that key or you can pass the row as an object. At the beginning of the called function, it checks to see if $key is_int. It is returning false when I am calling it with an integer value.
Everything you get from database is string.
That means, even if you have database with INTs for columns, you are going to get them like:
id, name, age
array("43", "Rok", "19");
Since everything you fetch from the database is a type string, you could try using is_numeric() instead of is_int().
Perhaps try the ctype_digit() function.

Categories