Learn about PHP development

Variables in PHP lesson 2 Tutorials PHP

Variable in PHP programming Tutorial

2. Variables in PHP

First concept in PHP is that of variables. If you have never used a variable before think of it as a box, where you can put stuff into. If you wanted to have some words represented by a variable you would write something like this. All variables in php are identified by using the dollar sign in front of the variable name. Then the name which can be anything that you want to call it. The equal sign will then tell the server that this variable now contains that string value.

$myVarilable = 'Some Words in this variable';

Now when you look at the value of $myVarilable it will equal 'Some Words in this variable'. There are a few types of variables but with PHP they are defined the same way. Strings are words or letters in an order, and integer are numeric representations. With integers you don't need to add the quotes around it.

$myValue = 1;

Another type of variable is a boolean. Which uses the keywords TRUE or FALSE

$myBoolean = FALSE;

PHP does not require variables to be declared before using them as it does with some other languages. All of the above examples can be used within PHP code to display the value output like below.

PHP Code Sample:

$myVarilable = 'Some Words in this variable';
$myValue = 1;
$myBoolean = FALSE;

echo $myVarilable;


PHP output display

Some Words in this variable

The real value of variables will become evident as the value of them can be changed. Variables are meant to be changed dynamically so that you can construct dynamic content which gets outputs on your site. Variables can be used to hold numbers and text.

Do Not Sell My Personal Information