eli312 Posted February 23, 2010 Share Posted February 23, 2010 Ok so i have been trying so solve this for a few days now. So i am using a function to include my layout files. But if i place a variable outside the file it can't seem to call from it. Example index file: <?php // Includes the layout pages function site_layout($template="") { include('layout/'.$template.".php"); } $hello_world = "helloWorld!"; ?> Call variable from page: <?php echo $hello_world;?> <br/> <br/> Call layout From function: <?php site_layout("test2");?> Test2 file: <?php echo $hello_world; ?> When script is run on website: Call variable from page: helloWorld! Call layout From function: Notice: Undefined variable: hello_world in /Users/Eli/Sites/MusicSite/layout/test2.php on line 2 So what is going wrong? Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/ Share on other sites More sharing options...
aeroswat Posted February 23, 2010 Share Posted February 23, 2010 Have you tried declaring the variable above the function? I don't believe that should make a difference but try it anyways Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/#findComment-1017002 Share on other sites More sharing options...
lovelyvik293 Posted February 23, 2010 Share Posted February 23, 2010 make the variable $hello_world global .. Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/#findComment-1017003 Share on other sites More sharing options...
wildteen88 Posted February 23, 2010 Share Posted February 23, 2010 You have a variable scope issue: http://php.net/manual/en/language.variables.scope.php What I would recommend you to do is to send any variables you wish to use in the template you're including to your site_layout function. For example <?php // Includes the layout pages function site_layout($template="",$_vars=array()) { if(is_array($_vars) && count($_vars) > 0) extract($_vars); include('layout/'.$template.".php"); } // define all variables we use in the template in the $data array, ge // $data['var_name'] = 'value'; $data=array(); $data['hello_world'] = "helloWorld!"; ?> Call variable from page: <?php echo $data['hello_world'];?> <br/> <br/> Call layout From function: <?php site_layout("test2", $data);?> Now in your template file you can use $hello_world Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/#findComment-1017007 Share on other sites More sharing options...
lovelyvik293 Posted February 23, 2010 Share Posted February 23, 2010 use //in the head part define("helloworld","helloworld"); //in the body part echo helloworld; Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/#findComment-1017009 Share on other sites More sharing options...
eli312 Posted February 23, 2010 Author Share Posted February 23, 2010 Thank you for the fast reply. But this is only a small part of a big problem. But i think it is still caused by Variable scope. as i want to get to my session class file and run a function but i am still getting that problem. <?php require_once("includes/session.php"); // Includes the layout pages function site_layout($template="") { include('layout/'.$template.".php"); } ?> Run raw code:<br/> <?php if($session->is_logged_in()) {echo "<a href=\"#\">Logout</a></div>";}else{echo "<a href=\"javascript:showhide('login')\">Login</a> | <a href=\"#\">SignUp</a></div>";} ?> <br/> <br/> Run from function:<br/> <?php site_layout('test')?> Session.php file: <?php class Session { private $logged_in=false; public $user_id; function __construct() { session_start(); $this->check_login(); if($this->logged_in) { } else { } } public function is_logged_in() { return $this->logged_in; } public function login($user) { // database should find user based on username/password if($user){ $this->user_id = $_SESSION['user_id'] = $user->id; $this->logged_in = true; } } public function logout() { unset($_SESSION['user_id']); unset($this->user_id); $this->logged_in = false; } private function check_login() { if(isset($_SESSION['user_id'])) { $this->user_id = $_SESSION['user_id']; $this->logged_in = true; } else { unset($this->user_id); $this->logged_in = false; } } } $session = new Session(); $s =& $session; ?> This gives me this: Run raw code: Logout Run from function: Notice: Undefined variable: session in /Users/Eli/Sites/MusicSite/layout/test.php on line 4 Fatal error: Call to a member function is_logged_in() on a non-object in /Users/Eli/Sites/MusicSite/layout/test.php on line 4 Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/#findComment-1017022 Share on other sites More sharing options...
wildteen88 Posted February 23, 2010 Share Posted February 23, 2010 You will need to pass the $session variable as an argument to your site_layout function, or use the dirty fix and define $session as global in your site_layout function This is explained in the link the I provided earlier about variable scope. Some info on function arguments http://www.php.net/manual/en/functions.arguments.php Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/#findComment-1017028 Share on other sites More sharing options...
eli312 Posted February 23, 2010 Author Share Posted February 23, 2010 You will need to pass the $session variable as an argument to your site_layout function, or use the dirty fix and define $session as global in your site_layout function This is explained in the link the I provided earlier about variable scope. Some info on function arguments http://www.php.net/manual/en/functions.arguments.php Thank you very much! problem solved! // Includes the layout pages function site_layout($template="") { global $session; include(SITE_ROOT.DS.'layout'.DS.$template.".php"); } Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/#findComment-1017038 Share on other sites More sharing options...
KevinM1 Posted February 23, 2010 Share Posted February 23, 2010 A couple things - This: $s =& $session; Isn't necessary. PHP 5 automatically uses references when dealing with object assignment. Also, do you realize why using the 'global' keyword is, as wildteen said, a dirty fix? I actually strongly suggest you pass your session object to the function through its argument list. Link to comment https://forums.phpfreaks.com/topic/193120-problem-getting-variable-when-using-function/#findComment-1017048 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.