Jump to content

"localness" of variables and functions


rshadarack

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/12743-localness-of-variables-and-functions/
Share on other sites

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]
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]
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.
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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.