rshadarack Posted June 23, 2006 Share Posted June 23, 2006 I know a bunch of programming languages such as C++ and Java, and while php is quite similar, the way local/global variables and functions seems to be much different.First of all, is there anyway I can have:[code]<?php$someValue = 5;?>...html and junk...<?php$x = $someValue;?>[/code]And have values carry over? I figure the answer is no.Second question: I have functions defined like this, which sets up a matrix (aray of arrays) for a menu with submenus:[code]<?php $pages = array(); function addMenuHeader($name) { $pages[] = array($name); } function addMenuItem($name) { $n = sizeof($pages); $pages[$n-1][] = $name; }addMenuHeader("Home");addMenuHeader("About_Us");addMenuItem("Mission");addMenuItem("Management"); addMenuItem("Staff");addMenuItem("History");?>[/code]But the value in $pages always gets lost as soon as a function exits. Why, and how do I fix it? Quote Link to comment https://forums.phpfreaks.com/topic/12743-localness-of-variables-and-functions/ Share on other sites More sharing options...
AV1611 Posted June 23, 2006 Share Posted June 23, 2006 well, the answer to the first part is, sure, you can call variable at any point on the page, or include them from other pages...[!--quoteo(post=387222:date=Jun 23 2006, 12:43 PM:name=rshadarack)--][div class=\'quotetop\']QUOTE(rshadarack @ Jun 23 2006, 12:43 PM) [snapback]387222[/snapback][/div][div class=\'quotemain\'][!--quotec--]I know a bunch of programming languages such as C++ and Java, and while php is quite similar, the way local/global variables and functions seems to be much different.First of all, is there anyway I can have:[code]<?php$someValue = 5;?>...html and junk...<?php$x = $someValue;?>[/code]And have values carry over? I figure the answer is no.Second question: I have functions defined like this, which sets up a matrix (aray of arrays) for a menu with submenus:[code]<?php $pages = array(); function addMenuHeader($name) { $pages[] = array($name); } function addMenuItem($name) { $n = sizeof($pages); $pages[$n-1][] = $name; }addMenuHeader("Home");addMenuHeader("About_Us");addMenuItem("Mission");addMenuItem("Management"); addMenuItem("Staff");addMenuItem("History");?>[/code]But the value in $pages always gets lost as soon as a function exits. Why, and how do I fix it?[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/12743-localness-of-variables-and-functions/#findComment-48845 Share on other sites More sharing options...
jworisek Posted June 23, 2006 Share Posted June 23, 2006 try something like this:heres a quote from PHP Cookbook[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Also, unless specified all values being passed into and out of a function are passed by value, not reference. This means PHP makes a copy of the value and provides you with that copy to access and manipulate. Therefore, any changes you make to your copy don't alter the original value.[/quote]so unless you specifically return the values, only copies are modified then lost when not returned from the function.[code]/*$pages = array();*/ /* No need to declare it in php */ function addMenuHeader($name) { $pages[] = array($name); return $pages; /* need the return to get the variables out of the function */ } function addMenuItem($name,$pages) { /* need to pass it the pages variable for it to use it. */ $n = sizeof($pages); $pages[$n-1][] = $name; return $pages; }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12743-localness-of-variables-and-functions/#findComment-48876 Share on other sites More sharing options...
rshadarack Posted June 23, 2006 Author Share Posted June 23, 2006 Thanks. After a bit more searching, I found a good page on local/global variables. My made the assumption that php works like C++, it first looks for local, then looks for global, and if both aren't found, it creates a new local variable. But this is wrong. It first looks for local, then creates a new local if it isn't found. That is, unless you use the global keyword. So doing:global $pages;As the first line in the function makes my code work. And for this, you do need to declare the array in the global scope. Or at least I think you do...Edit:Oh, and for that first question, I could have swore I was losing values when ending one php tag and starting another. But now it seems like I'm not. I think I'm going out of my mind. Quote Link to comment https://forums.phpfreaks.com/topic/12743-localness-of-variables-and-functions/#findComment-48882 Share on other sites More sharing options...
wildteen88 Posted June 23, 2006 Share Posted June 23, 2006 Yes when you use variables that is set out side of the function you need to declare them as global, otherwise the variables you use in the functioin will be tied to that function only. Quote Link to comment https://forums.phpfreaks.com/topic/12743-localness-of-variables-and-functions/#findComment-48884 Share on other sites More sharing options...
.josh Posted June 23, 2006 Share Posted June 23, 2006 you can also pass variables by reference like so:[code]<?php$foobar = 'foo';function add_bar(& $foobar) { $foobar.='bar';}echo $foobar . "<br>";add_bar($foobar);echo $foobar;?>[/code]in this example, there is no local copy of $foobar inside add_bar(). it changes the original variable from the scope it was called from (in this case, the global level). Quote Link to comment https://forums.phpfreaks.com/topic/12743-localness-of-variables-and-functions/#findComment-48885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.