domifrosle domifrosle - 4 months ago 9
PHP Question

Getting the return value from a function

Say I got some function that run some code and then

return
something, like this:

function something()
{
//some code
return $some[$whatever];
}


So, if I want to extract the data I generated in the function - the new value for
$some
, how should I do it? for example this won't do anything:

echo ($some);


Or what am I missing here, please

Answer

Since your Function returns a value, You may need to catch & store it inside a variable and then echo the variable if it is a String or do some casting to that effect. Here's an example:

<?php
    function something(){
        //some code
        $whatever  = 3;
        $some      = ["Peace", "Amongst", "All", "Humanity"];
        return $some[$whatever];
    }

    $var = something();

    var_dump($var); //<== DUMPS :: "Humanity"
    echo $var;      //<== ECHOES:: "Humanity"

Test it out here.

Cheers and Good Luck....