jimfog Posted December 28, 2012 Author Share Posted December 28, 2012 So...because I got confused a little...is the above code implementation correct?It seems to work ok so far: <?php function check_page_title(){ global $page_title; if (!isset($page_title)) $page_title = 'agendopio';}check_page_title();?> Do you have to notice anything wrong with the above? Link to comment Share on other sites More sharing options...
Ingolme Posted December 28, 2012 Share Posted December 28, 2012 So...because I got confused a little...is the above code implementation correct?It seems to work ok so far:<?php function check_page_title(){ global $page_title; if (!isset($page_title)) $page_title = 'agendopio';}check_page_title();?> Do you have to notice anything wrong with the above? The global keyword creates a copy of the variable, not a reference to the variable itself. Link to comment Share on other sites More sharing options...
jimfog Posted December 28, 2012 Author Share Posted December 28, 2012 And what does it matter here-in the case above? Link to comment Share on other sites More sharing options...
Ingolme Posted December 29, 2012 Share Posted December 29, 2012 It means that $page_title is local to the function and can't be read from outside.After that function is executed, if $page_title was undefined it will continue to be undefined. I believe global &$page_title will fix that, though I don't think a function is necessary to check the variable in the first place. Link to comment Share on other sites More sharing options...
jimfog Posted January 3, 2013 Author Share Posted January 3, 2013 (edited) so, you are saying that code like the below is not good, from a logic point of view: <?php function check_page_title(){ global $page_title; if (!isset($page_title)) $page_title = 'agendopio';}check_page_title();?><!DOCTYPE html> <head> <title><?php echo $page_title;?></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="css/admingeneral.css"/> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" ></script> <script type="text/javascript" src="js/js-code.js"></script> </head> And something else which I just noticed:In Netbeans, in the line where the title tags appear above I get a message which says the $page_title is not initialized.Despite the code works as expected(the title tag gets the name I want-agedopio) I wonderif there is something that requires adjustment Edited January 3, 2013 by jimfog Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2013 Share Posted January 3, 2013 $page_title doesn't have a value. Like I told you, when the function stops running the variables in it are destroyed, including $page_title.If you take that code out of the function then it will work. Link to comment Share on other sites More sharing options...
jimfog Posted January 7, 2013 Author Share Posted January 7, 2013 To add to that, you'll get an undefined variable error if you try to pass an undefined variable to a function. Even though you might be checking if things are set in that function, trying to pass an undefined variable to the function is an error. Trying to use an undefined variable at all, other than to check if it is set, is an error. That's why I suggested the one line of code to just set the variable if it's not set.See this code and tell me if what you described originally-thanks: $page_title = 'agendopio'; require 'header.php'; Contents in header.php file: <?php function check_page_title(){ global $page_title; if (!isset($page_title)) $page_title = 'agendopio';}check_page_title();?><!DOCTYPE html> <head> <title><?php echo $page_title;?></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="css/admingeneral.css"/> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" ></script> <script type="text/javascript" src="js/js-code.js"></script> </head> Link to comment Share on other sites More sharing options...
justsomeguy Posted January 7, 2013 Share Posted January 7, 2013 I suggested you just use a single line: if (!isset($page_title)) $page_title = 'Default'; I don't see a reason to use a function when a single line will do what you need to do. Link to comment Share on other sites More sharing options...
jimfog Posted January 8, 2013 Author Share Posted January 8, 2013 Ok, apart from using a function...which is "too much" as you say...is the rest of the code ok you think?Is that what you meant from the beginning? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 8, 2013 Share Posted January 8, 2013 Right. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now