Bryon
I Fix Things
- Messages
- 8,149
- Reaction score
- 101
- Points
- 48
Variables In PHP
What are variables: Variables are "little" pieces of information that can store pretty much any data in them. From integers to strings to arrays, Variables can hold them.
Introduction: In almost all PHP scripts, you will need to store, move, and manipulate data. You can do this by using variables. This tutorial is going to be aimed at teaching you how to use variables in PHP.
To start:
Variable types:
The two main data types that PHP variables use that a "beginner" should be worried with are integers and strings. There are many more data types of variables, just beginners don't need to worry about them until they "advance" in their knowledge of PHP. Some of the other data types that PHP can hold in vairables are:
- Arrays
- Boolean
- Objects
- Doubles
Rules When Creating Variables:
Variables in PHP can be named whatever you would like. But, there are some basic "rules" to go by:
- Variables start with a "$", always.
- Variables must begin with a letter, "[a-Z]", or underscore, "_".
- When placing data into variables, you must "encase" the data, if it is a string of mixed chars with "quotes", or 'apostrophes' . Example:
Code:
$hmhm = "some chars in here... 1321231";
$hmhm = 'some chars in here... 1321231';
Code:
$hmhm = 54654;
- Variable names are CaSe-SeNsItIvE, meaning that, if you have the variable "$hello", and the variable "$Hello", they are not the same variable. Yet, two completely seperate ones.
- Variables in PHP are "loose", meaning that they can change "on-the-fly" anywhere in your script. That also means that the "type" of the variable can be changed also. An example would be:
Say you have the variable $heyhey. Now:
Code:
$heyhey = 1234;
Since PHP is "loose" when dealing with variables, you can then do something like:
Code:
$heyhey = "Haha";
Somewhere later in your script, which would change teh value of $heyhey to "Haha", instead of "1234". You will see how this can be to your benifit later on.
What can be done with variables:
You can do many things with variables. They can be used in mathmatical equations, functions, mysql querys, and pretty much anything else you could think of. Here is an example of what could be done:
Code:
$name = $_POST['name'];
if (!$name) {
echo "No name was entered.";
} else {
$name = ucfirst($name);
echo $name ." is the name you have entered.";
}
Know, you might not understand what that does, but it uses variables to do "it". It takes the $_POST array, with the "name" form submit entered, or $_POST['name'], and places it into the variable $name. The script does some simple checking and formatting of $name, then either echos it, if it exists, or echos that no name was entered if it doesnt.
Anyways, to move on.
Manipulating variables:
You can do most "arithmatic" operations to variables. Including, but not limited to:
+ Addition
- Subtraction
/ Division
* Multiplication
% Modulus
I'm not goign to go into depth and describe what those all do, as you should be able to figure that out.
You can perform any of these on variables. Example:
Code:
$a = 5;
$b = 15;
$c = $a + $b;
echo $c; // would output 20
......
Code:
$a = 5;
$b = 15;
$c = $b - $a;
echo $c; // would output 10
......
Code:
$a = 5;
$b = 15;
$c = $b/$a;
echo $c; // would output 3
Get what I mean?
Also, you can join two or more variables, using the "concatenate" operator. All that this is, is a "period" placed between 2 or more variables. Example:
Code:
$a = "Hello ";
$b = "World!";
$c = $a . $b;
echo $c; // Would output "Hello World!"
There are many more operators that can be used with PHP variables, including comparison, logical, incremental/decremental, and combined operators. I will discuss them in a later tutorial.
Once you become more knowledgeable in PHP, you might come across a situation where you need to do many mathmatical "operations" and put the result into a variable. Just like in math, (geometry, etc), order of operations "occurs". So if you have:
Code:
$a = 4 + 6 / 2;
echo $a;
That would echo 7, instead of 5.
You can use "paentheses ( and )" though, to change which operations you want first. An example would be:
Code:
$a = ((5 + 7) - 2) * 2;
echo $a;
That would echo 20.
Conclusion:
I think that just about covers basic variables in PHP. There are many, many more things that I have skipped over, that maybe I will write more tutorials on in the future.
Some of the things I have skipped are:
- comparison, logical, incremental/decremental, and combined operators.
- Arrays
- Logical statements
- Boolean variables
- Variables being "true", "false"
Like I said, I hope to talk abotu all of those in later tutorials.
One last thing. If you ever want to get rid of a variable, completly, you can use PHP's built-in function "unset()". Unset() completly "erases" a variable from memory, so it does not exist anymore, until created again. Example:
Code:
$a = "Hmm";
echo $a; // would output "Hmm"
unset($a);
echo $a // Wouldn't output anything.
If I have forgotten ANYTHING at all in here, or you feel anything should be added into this, please tell me ASAP. This was written by ME, NedreN, and should not appear on any other websites without my permission. Unless I place it on there. Right now, the only site this is going on is x10Hosting's.
I'll prolly edit this a few times to fix mistakes.
Last edited: