FlashD
08-15-2009, 10:34 AM
PHP Tutorial - Variables and operators
Variables
Variables are containers. Think of an ordinary box that you put stuff into. That box may have a label on it so you remember what is inside of it. Variables are boxes that you put data information into. They are labeled with names.
Once you have some information in variables, it can be sorted, organized, manipulated, and so forth.
A variable name starts with a dollar ($) symbol. After that you can use any combination of higher case letters (A-Z), lower case letters (a-z), or the underscore (_). Numbers 0-9 can be used as well, but only after the dollar and a character. Spaces are not allowed. New PHP programmers often forget the dollar sign at the beginning of the variable. In that case it will not work.
The correct way to declare variables in PHP:
$var_name = value;
$varName = value;
$var_name30 = value;
$_var_name = value;
The wrong way to declare variables in PHP:
$var name = value;
var_name = value;
$30vars = value;
Variable names are CaSe SeNsItIvE. If you are referring to a variable in your coding, make sure the name matches exactly. $MyVariableName is different from $myvariablename.
Variable names should be easy to understand. Give some thought into the names.
$fn or $FirstName or $first_name
Which one looks more easier to understand and remember?
There are three types of variables.
Numbers
Strings
Arrays
Numbers:
Number variables may contain integers (regular positive or negative numbers) or floating-point (decimal) numbers. No fractions, letters, or extra decimals allowed.
Example:
$integer_example = 5;
$floating_point_example = 365.25;
Strings:
String variables are word variables. They may contain number information and other variables. The values of string variables are contained within single quotes (') or double quotes (") depending on how you want to use them.
Example:
$string_example_1 = "This is a test.";
$string_example_2 = 'This is another test.';
$string_example_3 = "5";
$string_example_4 = "The value is $integer_example.";
$string_example_5 = 'The value is $integer_example.';
The difference between single and double quotes is: single quotes will print out the exact value of the string as is, while double quotes will transfer variable values into place before printing.
Example:
print "$string_example_4 <br />";
print '$string_example_5';
Returns:
The value is 5.
$string_example_5
Arrays:
Arrays are groups of variables. They are defined as a table with rows which can contain either numbers or strings. Arrays are defined with the array() function.
Example:
$my_first_array = array();
$my_second_array = array(1, 2, "text1", 3, "text2");
In the example above we created an empty array and an array filled with numbers and random text.
Printing arrays is a little bit tricky, because if you print it the usual way:
print $my_second_array;
You will get this output:
array()
To get our value printed out we need to specify which row we want to print:
print $my_second_array[2];
This will return:
text1
From our example above we can see each entry gets it's own row number, which we use to retrieve our data. The rows start with number 0, which means row 0 has value 1, row 1 has value 2, row 3 has value text1 and so forth.
In PHP, a variable does not need to be declared before adding a value to it and it can be used over and over again in your script until the script runs. In the examples above, you see that you do not have to tell PHP which data type the variable is, either. PHP automatically converts the variable to the correct data type, depending on its value.
Operators
Operators are used to operate on values. With them you can assign, compare and combine numbers and strings.
PHP uses 4 different types of operators:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Arithmetic Operators:
Arithmetic operators are used for elementary mathematical operations.
http://www.animeforum.com/image/309344a86d40d3cf03.png
Assignment Operators:
Assignment operators assign a value to the variable. The most used one is the equals (=) symbol.
http://www.animeforum.com/image/309344a86d454b734d.png
Comparison Operators:
Comparison operators are used for comparison. They tell you if two numbers or strings are of the same value.
http://www.animeforum.com/image/309344a86d494d98ff.png
Logical Operators:
They will usually take two of the comparison operator conditions (above) and compare them to each other.
http://www.animeforum.com/image/309344a86d4bdc33ea.png
This ends the second part of the PHP tutorial. Next time we will be discussing strings, numbers and arrays in detail.
NOTE: Images taken from w3schools.com
Variables
Variables are containers. Think of an ordinary box that you put stuff into. That box may have a label on it so you remember what is inside of it. Variables are boxes that you put data information into. They are labeled with names.
Once you have some information in variables, it can be sorted, organized, manipulated, and so forth.
A variable name starts with a dollar ($) symbol. After that you can use any combination of higher case letters (A-Z), lower case letters (a-z), or the underscore (_). Numbers 0-9 can be used as well, but only after the dollar and a character. Spaces are not allowed. New PHP programmers often forget the dollar sign at the beginning of the variable. In that case it will not work.
The correct way to declare variables in PHP:
$var_name = value;
$varName = value;
$var_name30 = value;
$_var_name = value;
The wrong way to declare variables in PHP:
$var name = value;
var_name = value;
$30vars = value;
Variable names are CaSe SeNsItIvE. If you are referring to a variable in your coding, make sure the name matches exactly. $MyVariableName is different from $myvariablename.
Variable names should be easy to understand. Give some thought into the names.
$fn or $FirstName or $first_name
Which one looks more easier to understand and remember?
There are three types of variables.
Numbers
Strings
Arrays
Numbers:
Number variables may contain integers (regular positive or negative numbers) or floating-point (decimal) numbers. No fractions, letters, or extra decimals allowed.
Example:
$integer_example = 5;
$floating_point_example = 365.25;
Strings:
String variables are word variables. They may contain number information and other variables. The values of string variables are contained within single quotes (') or double quotes (") depending on how you want to use them.
Example:
$string_example_1 = "This is a test.";
$string_example_2 = 'This is another test.';
$string_example_3 = "5";
$string_example_4 = "The value is $integer_example.";
$string_example_5 = 'The value is $integer_example.';
The difference between single and double quotes is: single quotes will print out the exact value of the string as is, while double quotes will transfer variable values into place before printing.
Example:
print "$string_example_4 <br />";
print '$string_example_5';
Returns:
The value is 5.
$string_example_5
Arrays:
Arrays are groups of variables. They are defined as a table with rows which can contain either numbers or strings. Arrays are defined with the array() function.
Example:
$my_first_array = array();
$my_second_array = array(1, 2, "text1", 3, "text2");
In the example above we created an empty array and an array filled with numbers and random text.
Printing arrays is a little bit tricky, because if you print it the usual way:
print $my_second_array;
You will get this output:
array()
To get our value printed out we need to specify which row we want to print:
print $my_second_array[2];
This will return:
text1
From our example above we can see each entry gets it's own row number, which we use to retrieve our data. The rows start with number 0, which means row 0 has value 1, row 1 has value 2, row 3 has value text1 and so forth.
In PHP, a variable does not need to be declared before adding a value to it and it can be used over and over again in your script until the script runs. In the examples above, you see that you do not have to tell PHP which data type the variable is, either. PHP automatically converts the variable to the correct data type, depending on its value.
Operators
Operators are used to operate on values. With them you can assign, compare and combine numbers and strings.
PHP uses 4 different types of operators:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Arithmetic Operators:
Arithmetic operators are used for elementary mathematical operations.
http://www.animeforum.com/image/309344a86d40d3cf03.png
Assignment Operators:
Assignment operators assign a value to the variable. The most used one is the equals (=) symbol.
http://www.animeforum.com/image/309344a86d454b734d.png
Comparison Operators:
Comparison operators are used for comparison. They tell you if two numbers or strings are of the same value.
http://www.animeforum.com/image/309344a86d494d98ff.png
Logical Operators:
They will usually take two of the comparison operator conditions (above) and compare them to each other.
http://www.animeforum.com/image/309344a86d4bdc33ea.png
This ends the second part of the PHP tutorial. Next time we will be discussing strings, numbers and arrays in detail.
NOTE: Images taken from w3schools.com