Returning Arrays in PHP

Php Array Return

Returning Arrays in PHP

 With PHP, when getting a return from some functions, we may need to return more than one value. Let's take a look at how we should write our functions in such cases.

<?PHP

function deneme(){
    $a = true;
    $b = "www.urhoba.net";
    $c = 857;
    $d = "www.urhoba.blogspot.com";
    return array($a, $b, $c , $d);
}

list($birinci, $ikinci, $ucuncu, $dorduncu) = deneme();

if($birinci){
    echo $ikinci . "<br/>". $dorduncu . "<br/>";
    echo 3 + $ucuncu;
}

?>

This is how an example function and our code look; now, let's elaborate on these codes a little more.

Normally, we could define a return as return $variable; and return a single variable, but here, we defined an array inside the return and added 4 variables into this array, thus returning 4 variables at once.

To read all the variables we have returned, we created a list outside the function and defined as many variables in this list as the number of variables we returned, then assigned this list to our function.

Then, we call the variables defined in the list wherever we want to use them within our code, thus using the values returned by the function.