Jump to content


Photo

"localness" of variables and functions


  • Please log in to reply
5 replies to this topic

#1 rshadarack

rshadarack
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 23 June 2006 - 04:43 PM

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:

<?php
$someValue = 5;
?>

...html and junk...

<?php
$x = $someValue;
?>

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:

<?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");
?>

But the value in $pages always gets lost as soon as a function exits. Why, and how do I fix it?

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 23 June 2006 - 04:47 PM

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) View Post[/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:

<?php
$someValue = 5;
?>

...html and junk...

<?php
$x = $someValue;
?>

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:

<?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");
?>

But the value in $pages always gets lost as soon as a function exits. Why, and how do I fix it?
[/quote]


#3 jworisek

jworisek
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 23 June 2006 - 05:49 PM

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.

/*$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;
                }


#4 rshadarack

rshadarack
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 23 June 2006 - 06:02 PM

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.

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 June 2006 - 06:17 PM

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.

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 June 2006 - 06:23 PM

you can also pass variables by reference like so:
<?php
$foobar = 'foo';

function add_bar(& $foobar) { 
   $foobar.='bar';
}

echo $foobar . "<br>";
add_bar($foobar);
echo $foobar;
?>
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).

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users