pixie123 pixie123 - 18 days ago 5
PHP Question

Pass multiple variables to function array

How can i pass multiple variables to a function?

For example, i want to pass variables from my controller to my view.

i want to pass

var1
,
var2
, and
var3
by calling render() and i want it to go into my
$vars
array. how can i do that?

here is my code:

$var1 = "hi";
$var2 = "hello";
$var3 = "lol";

$this->render();


and here is my
render()
function:

$vars = array();

public function render($vars) {
require dirname(__DIR__).'/views/header.php';
require dirname(__DIR__).'/views/body.php';
require dirname(__DIR__).'/views/footer.php';
}

Answer

Check this way, First assigned var 1, 2, 3 to vars array, then assign the vars array to render function

$var1 = "hi";
$var2 = "hello";
$var3 = "lol";

$vars = array($var1, $var2, $var3);

$this->render($vars);

public function render($vars) {
    print_r($vars);
    // loop values
    foreach($vars as $var){
        echo $var;
    }
    // or access one by one
    echo $vars[0];
    echo $vars[1];
    echo $vars[2];
}