View Full Version : [Tutorial - PHP] Variables and operators

08-15-2009, 10:34 AM
PHP Tutorial - Variables and operators


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.

Number variables may contain integers (regular positive or negative numbers) or floating-point (decimal) numbers. No fractions, letters, or extra decimals allowed.


$integer_example = 5;
$floating_point_example = 365.25;

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.


$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.


print "$string_example_4 <br />";
print '$string_example_5';


The value is 5.

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.


$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:


To get our value printed out we need to specify which row we want to print:

print $my_second_array[2];

This will return:


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 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.


Assignment Operators:
Assignment operators assign a value to the variable. The most used one is the equals (=) symbol.


Comparison Operators:
Comparison operators are used for comparison. They tell you if two numbers or strings are of the same value.


Logical Operators:
They will usually take two of the comparison operator conditions (above) and compare them to each other.


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

08-19-2009, 05:45 AM
How do you include layouts from HTML to PHP?

08-19-2009, 05:56 AM
There are many different ways to do so, but the easiest one is probably using the include function.


08-19-2009, 06:00 AM
You only need this small code?
I browsed all the time and found very ... confusing codes ... something with "footer" and "header"

<?PHP include("header.php");?>

<?PHP include("footer.php");?>

08-19-2009, 06:18 AM
Yes, that's because the codes you have found are much more complex and don't just include stuff but also work with the included stuff.
Header is only the top part of the webpage, while footer the bottom part. In the between, you put the content which is in the same file where you used the two includes.

To make you an example:

header.php content:

print "This is the header!<br />";

footer.php content:

print "This is the footer!";

index.php content:

print "Hello world<br />";

index.php will return:

This is the header!
Hello world
This is the footer!

So yes, include() is the only thing you need if you just want to include and not edit the file.