I am trying to write my logic based on what variable is returned in php function. The example function xyz has two options for return. All variables $a, $b, $c will be dynamic and returned value will be dynamic based on what users enter in inputbox. these variables can be null or string or numeric values. I want to check if there is any value returning variable $a from the xyz function and apply logic based on the returned value.
Thank you!
function xyz($a, $b, $c){
if (!empty($a)){
return $a;
}else{
return $b;
}
}
$passingvalues = xyz(5, 2, 1);
if (!empty($passingvalues)){
echo "there is value returning";
//if returned value is $a and $a variable will have dynamic values depending on what user have entered.
if ($a){
echo "apply this logic";
}else{
echo "apply other logic";
}
} else{
echo "there is nothing returning from xyz";
}
My Answer:
In your code, you are trying to check if the returned value from the function is $a or $b. However, you need to store the returned value in a variable and then check that variable in the if conditional statement.
Here is the updated code:
php
function xyz($a, $b, $c){
if (!empty($a)){
return $a;
}else{
return $b;
}
}
$passingvalues = xyz(5, 2, 1);
if (!empty($passingvalues)){
echo "there is value returning";
// Store the returned value in a variable
$returnedValue = $passingvalues;
// Check if the returned value is $a
if ($returnedValue == $a){
echo "apply this logic";
}else{
echo "apply other logic";
}
} else{
echo "there is nothing returning from xyz";
}
In this code, we store the returned value from the function in a variable called $returnedValue and then check if it is equal to $a in the if conditional statement.
Rate this post
4 of 5 based on 7936 votesComments