pixie123
pixie123

Reputation: 979

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';
    }

Upvotes: 1

Views: 4880

Answers (2)

Gayan
Gayan

Reputation: 2935

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];
}

Upvotes: 2

Rizban Ahmad
Rizban Ahmad

Reputation: 139

Create an instance for your view file and use predefined method of a controller file to set variables. Like this way:

return new ViewModel(array(
        'order_by' => $order_by,
        'order' => $order,
        'page' => $page,
        'paginator' => $paginator,
    ));

Since i am using Zend Framework-2. So it is basic syntax to send multiple variables to view file. and you can access these vars by using array keys.

Upvotes: 1

Related Questions