Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
[Edit] Using Variables in PHP
Variables are a fundamental part of any programming language. A variable is simply a container that holds a certain value. Variables get their name because that certain value can change throughout the execution of the script. It ’ s this ability to contain changing values that make variables so useful. For example, consider the following simple PHP script:
echo 2 + 2;
As you might imagine, this code outputs the number 4 when it ’ s run. This is all well and good; however, if you wanted to print the value of, say, 5 + 6 instead, you ’ d have to write another PHP script, as follows:
echo 5 + 6;
This is where variables come into play. By using variables instead of numbers in your script, you make the script much more useful and flexible:
echo $x + $y;
You now have a general - purpose script. You can set the variables $x and $y to any two values you want, either at some other place in your code, or as a result of input from the user. Then, when you run the preceding line of code, the script outputs the sum of those two values. Re - run the script with different values for $x and $y , and you get a different result.
September 15, 2011