Hi, there is something like this:

HTML templates in the views folder, something like:

<html> <meta charset='utf-8' /> <body> <?php foreach ($mytext as $key => $val) { ?> <h1> <?php echo $val; ?> </h1> <?php } ?> </body> </html> 

and for some reason it is impossible to call them through a function, writes that the variable was not found:

 <?php function view($tmp) { include_once ('views/' . $tmp); } $mytext = array('hello', 'my', 'dear', 'friend'); view('test2.php'); ?> 

although if you write directly:

 <?php $mytext = array('hello', 'my', 'dear', 'friend'); include_once ('views/test2.php'); ?> 

That all works fine. Tell me where the error is. I understand that the connection goes to a function where common variables are not visible, and there are probably 2 ways, either to declare the variable (array) "visible everywhere", or to designate a function so that it can see all the variables from that Where it comes from, tell me how to implement it? (the way to transfer variables to the function besides the file name would not be desirable)

    2 answers 2

    I will not explain:

     function view($tmp) { global $mytext; include_once ('views/' . $tmp); } 

      Perhaps it makes sense to set the $ tmp parameter as optional in the view function?

       function view($tmp = null) { if($tmp) { global $mytext; include_once ('views/' . $tmp); } }