What makes a variable global in PHP? What does it mean to register a veriable?
Global Variables
you can declare a variable as global using the Global function:
global $total_price;
global $items;
if(!$items) $items = "0";
if(!$total_price) $total_price = "0.00";
I think variables are reigistered via "Post" in a form.
Or a varianle is automatically registered when entered
example:
$name="SeanPaulCaillouette"
the variable name now has a value that can be accessed anywhere in the scripts since it is not within a function call.
If the variable is registered within a function call
function register_name() {
$name="seanPaulCaillouette"}
now name is local because it is within a function.
global $buf,$tmp;
$buf = (int)14;
$tmp = $buf*2;
This isn't a really good way to use global variables, but by declaring a variable as global before you set it to a value, you can make it available throughout an entire script. It may not seem very helpful right now, but you will learn to love it.
-JPF
in your php.ini, the register_globals (I believe this is right, or something like this), must be set to "Yes". The default in recent releases of php is set to "No", so you must change this (they did it for security reasons).
Also, depending on the warning level you have set, it's best to declare a global variable, otherwise you might get a notice saying that a variable wasn't properly registered in your script (though php will set it automatically and it will still work).
I have read (somewhere?) that all variable should only be used within classes. Is that a common/desirable practice?
Using variables only in OOP/classes is the most ridiculous thing I've ever heard.
Originally posted by IndianaRogers
I have read (somewhere?) that all variable should only be used within classes. Is that a common/desirable practice?
Though I don't claim to be any sort of security expert (and in fact, I really need to learn more), I do know that it's more secure if you only use your variables inside of the classes from where they originate. Though I know very little else about what the larger explanation behind this is.