Code defined outside function, solved

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Code defined outside function, solved

Post by scarface222 »

If you have code outside a function for example, on the same page, and that function is called, will $variable or and mysql queries be defined universally as a rule throughout multiple functions that use those variables on the same page?

Code: Select all

 
$variable=1;
 
function random($parameter1, $parameter2){
 
echo $variable=1;
 
}
 
function random2($parameter1, $parameter2){
 
echo $variable=1;
 
}
 
Last edited by scarface222 on Thu Jan 21, 2010 8:36 pm, edited 1 time in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Code defined outside function, quick question...

Post by AbraCadaver »

I'm not sure I understand what you're asking, but do this and it should make it clear:

Code: Select all

$variable = 'start';
echo $variable;
 
random(null,null);
echo "after random() = " . $variable;
 
random2(null,null);
echo "after random2() = " . $variable;
 
function random($parameter1, $parameter2){
    echo "inside random() = " . $variable;
    $variable = 'random';
    echo "inside random() = " . $variable;
}
 
function random2($parameter1, $parameter2){
    echo "inside random2() = " . $variable;
    $variable = 'random2';
    echo "inside random2() = " . $variable;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Code defined outside function, quick question...

Post by Eran »

You need to consider how scope works in PHP - variables declared outside of functions and class methods are in a global scope, variables declared inside functions are in a local scope. The global and local scope are separate except for the use of the global keyword, which allows variables from the global scope to be accessed inside a local scope. That is generally considered bad practice, since scoping is very important for the integrity of variable data and locality of changes - ie, changes you make in one place should not have unforeseen effects on another.

For more information, the manual is your best friend
http://php.net/manual/en/language.variables.scope.php
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Code defined outside function, quick question...

Post by scarface222 »

As usual, really appreciate the feedback, very well explained pytrin, and abra.
Post Reply