An Introduction to Variables
What is a variable?
Good question. Think of a variable as one piece of data stored in memory, which can be called and changed or used later. Ok, you call your doctor and he tells you that your appointment is at 5:30, and you store that in your memory. He calls back in an hour, and he says "we've changed your appointment to 4:00." So you in turn grab that appointment time from memory, change it to 4:00, and put it back in memory. Let's do that in php:
PHP Example: (!)
<?php
//you call your doctor...and an appointment is made:
$appointment = "5:30";
//he calls back in an hour... and changes your appointment time:
$appointment = "4:00";
//one of your friends asks what time your going to the doctor, so you tell them:
echo $appointment; //this will return 4:00.
?>
That is one way to think about it, but lets go further in the explanation. There are different types of variables. The ones I will be explaining here will be string, double, and integer. These 'types' of variables are called "Scalar" variables, and are defined using the dollar sign ( $ ).
string
A string is merely a group of characters. Meaning... a string is basically a sentence. A string can be assigned with letters or numbers (or both), and can be assigned with other variables included as part of the string. Lets take a look at some examples...
PHP Example: (!)
<?php
$this_string = "this sentence is a string.";
//contains the value "this sentence is a string"
$thisother_string = "$this_string, that includes another variable.";
//contains the value "this sentence is a string that contains another variable."
$THIS_STRING = "12345....678910! notice I have THIS_STRING and this_string?";
?>
Notice how I used this_string and THIS_STRING as variable names? Variable names are case sensetive. Meaning that this_string, and THIS_STRING are two different variables. Take special care when naming your variables, don't name 2 variables the same thing (unless you want to change the value of that variable), and don't call a variable with the wrong name.
Integer
An 'integer' is any whole number. Here are some examples...
PHP Example: (!)
<?php
$forty_five = 45;
$seventy = 70;
$sixty_four = 64;
$two_plus_four = 2+4;//this variable now contains the 'Integer' '6'
?>
Do you get the idea now? Hopefully so... cause were moving on to the beloved Double.
Double
A Double is any number that contains a decimal point. You might have seen these being called "float", or "floating point" variables. Lets see some examples:
PHP Example: (!)
<?php
$pi = 3.14;
$another_double = 50000.09;
$and_another = 45.96;
?>
Fun, no? Of course it is!
You want more, right?
Ok, so now you know how to define variables, and you want more. You're a very needy programmer aren't you? Well, we better get started...
Lets learn how to do basic math on variables, and increment/decrement doubles...
PHP Example: (!)
<?php
$one = 1;
$two = 2;
$three = $one+$two;//+ is the 'addition' operator in PHP
$four = $two*$two;//* is the 'multiplication' operator in PHP
$negative_one = $one-$two;//- is the 'subtraction' operator in PHP
$one_half = $one/$two;// / is the 'division' operator in PHP
$one++; // $one becomes 2, because it was auto incremented 1 time.
$one++;//now $one becomes 3, because it was auto incremented again.
$one--;//now $one is 2 again.
$one--;//now $one is back to 1.
?>
Ok, now lets look at how to "add" (so to speak) strings... We'll use a method called 'concatenation', which uses the dot ( . ) operator.
PHP Example: (!)
<?php
$one = "this is";
$two = "a string that has been put together";
$three = "using concatenation.";
echo $one." ".$two." ".$three;
//would return "this is a string that has been put together using concatenation."
$thing = "woo";
$otherthing = "hoo!";
echo $thing . $otherthing;
//would return "woohoo!"
?>
Now that you have basic examples, play around-> see what you can create.
Related Functions:
doubleval, empty, floatval, get_defined_vars, get_resource_type, gettype, import_request_variables, intval, is_array, is_bool, is_callable, is_double, is_float, is_int, is_integer, is_long, is_null, is_numeric, is_object, is_real, is_resource, is_scalar, is_string, isset, print_r, serialize, settype, strval, unserialize, unset, var_dump, var_export