Avoid Replication with the PHP Reference Operator
Since PHP version 4+ PHP has offered us a way to reduce memory usage in our programs when assigning a variable to another variable. Normally when you assign the value of one variable to another separate variable, the variable that is getting assigned to another is then duplicated by PHP in memory to be used in the operation.
What this means for us as programmers is that if we want to take the value of one variable and assign it to another, we must fist allow PHP to duplicate the variable so that it may be assigned to another.
The reference operator must be used in conjunction with the assignment operator.
We can circumvent this activity by using what is called the reference operator "&". Here is an example:
<?php $a = 10; $b = $a; //$a is duplicated in the background by PHP so that it can be assigned to $b // $b is no 10 //avoid duplicating the assigned variable using the reference operator "&" $a = 10; $b = &$a; //& used in conjunction with the assignment operator $a = 15; //$a and $b = 15 and there is only one $a ?>
The way this works is that even after $b is assigned "&$a" later in the script when $a changes so does $b because the variable that was assigned to $b is not a copy, but the value of $a no matter where is its as long as it is after the assignment using the reference operator.
Basically, $b = &$a means, that $b references the value of $a from this point forward in your script, so if $a changes below your reference, then the value of the referenced variable changes with the new assignment of $a as long as it happens below the referenced call in your script.
Zooming in on PHP Variable Scope
Variables within php have a property that is called "scope". Scope refers to places within your php script that you can use your variable, and where you cannot.
Within PHP there are four specific types of scope.
- SuperGlobal Scope
- This type of scope are available everywhere within your scripts.
- Global Scope
- This type of scope are visible throughout your script however they are not visible inside of a function (this can be overcome by using Function Global Scope)
- Function Scope
- This type of scope relates to variables created within functions. Meaning that any variable created within a function is only visible to your script within the function.
- Function Global Scope
- This type of scope relates to variables created outside of your functions. If you declare a variable inside your function as "global", this "global" variable is not global to your entire script, however this definition of "global" inside of a function points to the value of a variable outside of your function that has been defined as global as previously defined. you would tend to think that defining a variable "global" inside of a function would make that variable available to your entire script, however this is wrong. this definition points your local function variable to the counterparts value defined outside your function (example follows)
<?php //define a variable outside of our function $var_two = 2; //global scope, meaning it is global to your current script function your_function($variable_one) { $var = "one"; $var_two = 4; //function scope variable, local to inside the function only $var_three = global $var_two //$var_two is equivalent to "2" set outside the function //$var_three is now set to "2" } ?>
Constantly Remaining Constant
Constants are special types of variables that recieve a value and from then on can no longer be changed. this type of variable has a special place in PHP. It is normailly used when you have something that needs to be contained in a variable that iwll never change in your script, or has the potential of bing overwritten accidentially, and you wish to prevent aht from happening.
Many of the programmers in the PHP community borrow a syntax from the "C" programming language when defining a constant. That syntax is to name the constant variable in all capital letters like so.
- A constant with a string value:
- A constant with a number value:
<?php define('YOURCONSTANT', 'your_string_value') ?>
<?php define('YOURNUMBERCONSTANT', 100000) ?>
Naming your constants with this naming convention will help you and other programmers distinguish your constants from other variables and text. When you call your constant, like in an echo statement you do not call it with a dollar sign in front of it like you do with other variable types. This is another significant difference between constants and other types of variables within PHP.
<?php echo YOURCONSTANT ?>
The output for this echo statement will be, "your_string_value" depending on what you placed as your value. Remember that strings need to be within quotes, and numbers need not be.