Dec 7, 2011

Learn PHP | PHP Tutorials | PHP Lessons | Part 2

So welcome back to part 2 of the PHP tutorial. If you are new to PHP then I would recommend you to first check the Part 1


  • Within this lesson you will learn how to assign values to variables in PHP and use them in some expression.

  • The topics covered are :

  1. Understanding Variables.

  2. Data Types.

Let’s get started right away…..

Understanding Variables

  • Variables

    • Variables are containers in which values can be stored and later retrieved.

    • They are basically the building blocks of any programming language.

For eg :- you can have a variable called “$number” that holds the value the value  “5

[php]$number = 5;[/php]


$name that holds the value “bruce

[php]$name = "bruce";[/php]

Note :-        In PHP a variable will always be prefixed with a “$” symbol. If you can remember that declaring variable will be SUPER-EASY….

  • Just take “=” symbol with variable name on the left and value you want on right.

Here take a look at the example : -

[php]$name = "bruce";
echo "Hello";
echo $name;[/php]


Output produced is : Hello bruce.

  • Above example uses the “echo” command to display the value stored in a variable, in the same way that you would display a fixed piece of text.

  • The more descriptive your variable names are the more easily you will remember what they are used for, when you come back to a script several months later.

  •  Good variable name tells exactly what kind of value you can expect to find and stored in them.

  • Also, Variable name can only contain letters, numbers and underscore character. And each must begin with a letter or a underscore.

  • When the variable is assigned, the value given doesn’t have to be a fixed value

For eg :-

[php]$sum = 16 + 30;[/php]

-      ’16 + 30’ can also be an expression. An expression is where two or more values are combined using an operator to produce a result.

Let’s take a look at this …..

[php]$sum = 16 + 30;
echo $sum;[/php]

-      As seen above the values ‘16’ and ‘30’ are combined using the ‘+’ symbol & result is returned using “echo” command.

  •  Text Strings

    • Text Strings always have to be enclosed in quotation mark(“”) mark, and most of the time it doesn’t make any difference whether you use single(‘’) or double(“”) quotes.

    • But when you are dealing with variables it does make a difference.

Let’s look at an example to be clearer on this.

[php]$name = "bruce";
echo "Hello, $name";[/php]

  • Here the value of the variable is included in the string.

Output produced is : Hello, bruce.

Now look at this example below.

[php]$name = 'bruce';
echo 'Hello, $name';[/php]

  • Here no substitution of any variable takes place and output thrown is

Output produced is : Hello, $name.

  • You can overcome this by using ( ) OR { }

  • So if you want to display weight value with a suffix, your statement might look like this

[php]echo "The total weight is { $weight } lb";[/php]

  • If the braces hadn’t being placed around this particular weight variable , PHP would find a value which doesn’t exist. Like this

[php]echo  "The total weight is $weight lb";[/php]

  • Not to be too confusing but you can also do the same thing by using concatenation operator “.” to join two or more strings together as shown here

[php]echo 'The total weight is ' . $weight . 'lb' ;[/php]

-      The three values here are two fixed string and a variable.

-      If you assign a value to variable i.e $weight = 99 . The statement would produce output like this.

Output produced is : The total weight is 99lb.

Data Types

  • Each variable that holds certain value has a data type that defines what kind of a value it is holding.



BooleanA truth value can be TRUE or FALSE
IntegerA number value, can be a positive / negative whole number
Double (or float)A floating point value can be any decimal number
StringAny alphanumeric value, any ASCII character.

  • When you assign a value to a variable the data type is also set.

  • PHP determine the data type automatically, based on value you assign to the variable.

  • If you need to check what data-type PHP thinks the value is you can use the gettype() function.

[php]$value = 7.2 ;
echo gettype($value);[/php]

  • Running this function, the data-type of a decimal number is double.

  • The complimentary function of gettype() is settype(), which lets you overwrite the data-type of a variable.

For eg:-

[php]$value = "22 January 2010";
settype( $value, "integer");
echo $value;[/php]

  • In this case the string begins with number, but the whole string is not an integer. The conversion converts the first non-numeric character and discards the rest and the output produced is just the number 22.


Output produced is : 22

  • Sometimes PHP will perform an implicit data-type conversion if values are expected to be of a particular type which is known as Type Juggling.

     For eg:-

[php]echo 100 + "10 inches" ;[/php]

-      The addition (+) operator expects to sit between two numbers. String type values are converted to double or integer in this case “10 inches” is treated is “10” before the addition operation is performed so the result is “110

Output produced is : 110

  • The same thing happens when a string operator is used on numeric data.

  • If you perform a string operation on numeric data-type, the numeric value is converted to a string first. We saw this earlier when we used the concatenation “.” operator to display a numeric value.

  • It is possible to use the value stored in a variable as the name of another variable.

-      Again, I know this might sound a bit confusing so let’s look at an example.

[php]$my_age = 21 ;
$varname = "my_age";
echo "the value of $varname is ${ $varname }";[/php]

-      The output produced in this example is

Output produced is : The value of my_age is 21

-      Because the string “my_age” is enclosed in a double quotes the dollar($) sign indicated that a variables value should become a part of string.

-      The construct shown i.e $ { $ varname } indicates that the value of variable should become a part of the string which is known as Variable Variables.

-      The braces around the variable $ { $ varname } is to indicate that it should be referenced first.

-      The next example shows the same output as the last one except that it uses the concatenation operator.

[php]echo 'The value of ’ . $varname . 'is' . $$varname;[/php]

Output produced is : The value of my_age is 21

So here we end our part 2 and I recommend you to go through the entire tutorial once again, and replicate all the examples in your localhost and test it if you are prepared for the 3 part.