pixie123 pixie123 - 1 year ago 61
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

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

here is my code:

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


and here is my

$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 Source

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);


public function render($vars) {
    // loop values
    foreach($vars as $var){
        echo $var;
    // or access one by one
    echo $vars[0];
    echo $vars[1];
    echo $vars[2];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download