Loop until null value is given - php

I've a third party library that returns values from a function and gives null if no value is present (not a database).
I've got the first value and I want to use it to return the second one and use the second one to return the third and so on. When a null value is returned, this loop should stop.
So the function uses an ID to get the next value eg: getNextValue($id). The return of this function is value or null.
So how should I include this function in a loop that uses a start value and returns the second, and uses the second to return the third and so on, until it returns a null value so it stops?

while ($value = getNextValue($id)) ...

while ($id = getNextValue($id)) {
//this will keep passing $id to the function over and over again
//Assuming your function will return different input or a null, this will work.
//code
}

while (null !== ($value = getNextValue($id))) {
}

Related

How to check inside PHP function if parameter (that is passed by reference) was passed into it?

Here's an example:
function example(&$outDbgOutput = null)
{
$bUseDbgOutput =
how_to_know_if_this_parameter_was_passed_into_this_function($outDbgOutput);
//...
}
And then two types of calls:
example();
and
example($output);
PS. I'm using PHP 7.4
Since the default value of $outDbgOutput is null,
if($outDbgOutput !== null){
//logic
}
or you can use isset() and check if the value is not equal to null since isset() is not returning anything when the value that is assigned to the variable is null.
if(isset($outDbgOutput) && $outDbgOutput !== null){
//logic
}
I was reading about the function func_num_args() as it was mentioned in the comment section as well.For the use of future me I'll add that in here as well.
if(func_num_args($outDbgOutput) > 0){
//since func_num_args will return the number of parameters that was passed
}

Transform any null input to empty string

So I've got a while loop, inside I have $array_collections that gives me 35 value per loop, I want to verify for every value if it's equal to NULL then give it an empty string. I did that :
while ($array_collections = tep_db_fetch_array($query)) {
foreach ($array_collections as $key => $value) {
if (is_null($value)) {
$array_collections[$key] = "";
}
}
$docs[] = new \Elastica\Document('', \Glam\HttpUtils::jsonEncode(
array(
'X' => $array_collections['X'],
... etc
)));
}
This technically should work, but the loop goes over 500K elements so it's huge, and for every element we put it into a table, problem is that I run out of memory at some point. So is there another simple way to give any given NULL value an empty string without looping?
well, you can put NOT NULL constraint with empty string as DEFAULT value in the DB for that so you dont need to do that in php using looping, but if you dont want to change the DB design then you can use COALESCE in your query
select COALESCE(yourfield,'') from table
it will convert NULL value into empty string
You can use array_map function to replace null values into empty string.
$array_collections=array_map(function($ar)
{
if(isset($ar) && $ar!=null){
return $ar;
}
return '';
},$array_collections);
Above code replace all null values to empty string. No need of loop.
you can use array_walk:
function replace_null(&$lsValue, $key) {
if(is_null($lsValue)) {
$lsValue = "";
}
}
array_walk($array_collections, 'replace_null');

php, key check on NULL variable gets set, how to?

here is my example:
$data = null;
var_dump($data); // returns null
is($data['test']);
var_dump($data); // returns array (size=1)
// 'test' => null
function is(&$var, $default = null)
{
return isset($var) ? $var : $default;
}
notice that after I run is($data['test']), $data becomes $data['test'] = null
any ideas why this behavior?
I am looking to get null. I am running php 7
edit: it's the & symbol, just not sure why would yield that result
You cannot pass a variable with a non existing key to a function (even with reference) since the value will be passed to the function.
If we have $data and in the next line if we do $data['test'], the $data variable will be updated to an array.
So in your case, when you use is($data['test']);, it updates the variable.
Then it goes to the function, and checks isset($var). It is already set since the variable is updated already. So the isset gets a true return. That return will contain the updated variable which is $data['test'].
I think the solution from #Fred B will work in this case.
try using array_key_exists() and return null if key not found.
This solution seems to work, though less elegant:
is($data, 'test');
var_dump($data); // returns array (size=1)
// 'test' => null
function is($var, $key, $default = null)
{
return isset($var[$key]) ? $var[$key] : $default;
}

Passing 0 to function with default value

Im currently testing a simple PHP function.
I want to to return the currently value of a field if the function is called without any parameter passed or set a new value if a parameter is passed.
Strange thing is: if I pass 0 (var_dump is showing correct value int(1) 0), the function goes into the if branch like i called the function without any value and i just don't get why.
function:
public function u_strasse($u_strasse = 'asdjfklhqwef'){
if($u_strasse == 'asdjfklhqwef'){
return $this->u_strasse;
} else {
// set new value here
}
}
either u_strasse() or u_strasse(0) gets me in the if branch.
You should use null as the default value:
public function u_strasse($u_strasse = null)
{
if ($u_strasse === null) { $u_strasse = 'asdjfklhqwef'; }
// rest of function
}
When comparing variables of different types (specifically strings and numbers), both values will be converted to a number. Therefore, your 'asdjfklhqwef' converts to 0 (number), the comparison is true.
http://www.php.net/manual/en/language.operators.comparison.php
Use === instead of ==:
public function u_strasse($u_strasse = 'asdjfklhqwef'){
if($u_strasse === 'asdjfklhqwef'){
return $this->u_strasse;
} else {
// set new value here
}
}
In case of == php tries to convert 'asdjfklhqwef' to number (because you pass $u_strasse as a number) and (int)'asdjfklhqwef' equals 0. To avoid this behavior you need to compare strictly (===)
Read more about difference in == and === here
Pass '0' instead of 0. The former will be a string.
You can cast it like this:
$myvar = 0;
u_strasse((string)$myvar);

Can you describe the following PHP function?

Can anyone describe the following php function:
function get_setting_value($settings_array, $setting_name, $default_value = "")
{
return (is_array($settings_array) && isset($settings_array[$setting_name]) && strlen($settings_array[$setting_name])) ? $settings_array[$setting_name] : $default_value;
}
What does it return and whats its purpose?
This is equivalent:
function get_setting_value($settings_array, $setting_name, $default_value = "")
{
// Check that settings array really is an array
if (!is_array($settings_array)) {
return $default_value;
}
// Check that the array contains the key $setting_name
if (!isset($settings_array[$setting_name])) {
return $default_value;
}
// Check that the value of that index isn't an empty string
if (!strlen($settings_array[$setting_name])) {
return $default_value;
}
// Return the requested value
return $settings_array[$setting_name];
}
The function returns a setting value if found, or the default value (which is optional).
A more detailed answer:
if the the given settings array is an actual array
if the setting_name exists in the array
if the setting value represented by the setting name is not empty, false, or 0 then return it
else return the default value, which, if not set, is an empty string
if $settings_array is an array and the setting $setting_name (which is fournd in the settings array) has a value and the value of $setting_array[$setting_name] has a value, then return the value of $setting_array[$setting_name] otherwise return the $default value.
I guess the purpose of this is go get a particular setting and check that it exists (the settings are all in the array, they are set and the have a length) if not then return your default values.
This uses an "inline if statement"

Categories