Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Really fu*!@ng me off! :(

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Ok..can someone PLEASe explain how the Globals thing works in PHP! In Perl you can define a variable as a variable, and it is automatically defined as a global...unless you give it a 'my' or 'local' value. So, comming from a Perl background this is really confusing me :(

I'm using the following code;

Code:
<?php

// VARIABLES TO SET UP!

// Admin password...for editing the users and adding new ones.
$admin_password = &quot;password&quot;;

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

$action = $_action;
$password = $_password;
$crypt_password = crypt($admin_password, 'MM'); 
$crypt_submit_password = crypt($_submit_admin_password, 'MM'); 
$new_username_add = $_new_username;
$new_password_add = $_new_password;
$path = $

global $action, $action, $password, $crypt_password, $crypt_submit_password, $new_username_add, $new_password_add;

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

load_codes();
decide(); // now we decide what to do

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

function decide() {

global $action, $action, $password, $crypt_password, $crypt_submit_password, $rate_code, $new_username_add, $new_password_add;

if ($action == &quot;&quot;) { login_page(); exit;} 
elseif ($action == &quot;login&quot;) { login(); exit; } 
else  { login_page(); exit;}


} // end the decide function

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

function login_page() {

// deifne the globals
global $action, $action, $password, $crypt_password, $crypt_submit_password, $rate_code, $new_username_add, $new_password_add;
	
	echo &quot;logging in page&quot;;

}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

function login() {

// globalise the main things
global $action, $action, $password, $crypt_password, $crypt_submit_password, $rate_code, $new_username_add, $new_password_add;

	echo &quot;logging in page after submit $rate_code&quot;;
echo $GLOBALS;	
	echo $rate_code;

}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

// load up improtant codes..like the rate codes...
function load_codes() {
	
$rate_code = &quot;rate code&quot;;

global $rate_code;

}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

// error sub...in case something goes wrong!
function error($error) {

 echo &quot;There was an error. It was;<BR><BR>&quot;;
 echo $error;

 exit;

} //end the error sub

?>

Basically, what I was trying to do is get the authentication thing to work. To start with I just want to print out the $rate_code thing as a variable and print it in login(). However, everything is getting printed EXCEPT the $rate_code! Anyone wanna offer some help?

Thanks

Andy
 
Andy,

First of all, PHP doesn't need a declaration for the variable. It's unlike Perl, that you need to use my or local. So you can use any variable, for instance:
<?
$ct0 = 0;
$ct1 = &quot;Example 1&quot;;
$ct3 = 'a';
?>
Once you initiate value to the variable, then that variable will function as a global variable. You can use it anywhere. But if you want to pass it to the function, then you need to pass it.
Hope this helps ... :)

Mike
 
Well, it's a little more complicated than that. Have you read the online manual's info about variable scope?
Mikefn is right, though, You don't declare variables in PHP, which also means you don't initiate a variable with global scope from outside a function. You do it inside the function, if you want to affect a variable by that same name that has already been defined elsewhere. Or you access the pre-defined $GLOBALS array, such as $GLOBALS[&quot;var1&quot;], $GLOBALS[&quot;var2&quot;]. Now, these methods get laborious, so there are other mechanisms for accomplishing this. If you pass a variable by reference as a function argument, you don't make an internal copy for the function, but you directly modify the external variable. ( For example:
Code:
function foo (&$var)
{
    $var++;
}

$a=5;
foo ($a);
// $a is 6 here

Also, there is the use of constants. ( Constants are very useful when you are setting values for main configuration files for a web application, such as a place to hold database, username, password, hostname, etc..., basically any value that will not be modified by the script, but is used as an overall configuration setting. Constants by default are completely global, meaning they can be accessed inside and outside functions without any further declarations anywhere. Since they are not dynamically modifiable, they use less resources than variables. The other benefit is security: since they cannot be modified by the script, there is no chance of crackers spoofing querystrings to hijack your vars and make them do other things. This seems like a no-brainer, but I can't tell you how often I see scripts and PHP applications which use standard variables for global configuration settings, and then slavishly pass them into functions, or declare them as global, and generally jump through hoops, when there is an easier way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top