PHP Tutorial 3: Variables in PHP

declaring variables in PHP

Variables are the one that play a key role in making dynamic web applications and web sites out of  PHP. Basically variables are used to store values.  The variables in PHP are declared using $ symbol.

The correct way to declare any variable in PHP is $name. Now name acts as variable and ready to store values. If you don’t place a $ symbol along with variable then it doesn’t work.

  • Rules for declaring variables

1. Variable must start with the symbol $, followed by underscore or letter.

2. PHP variable can hold alphanumeric symbols (A-Z,a-z,0-9).

3. PHP variables are case sensitive $name is not equal to $Name.

Example to show how variables can be used in PHP:

$name = ‘John’;
$marks = ’10’;
echo $name.'<br>’;
echo $marks;

declaring variables in PHP

Variables are assigned using = operator.

  • Types of variables in PHP

PHP supports almost all types of data types, they are:

  1. Integers any numeric value. {….-1 0 1….}
  2. Floating point numbers (decimal numbers)
  3. Strings (Series of characters between the quotes ‘Thank you for the help’ this is a string.
  4. Objects ( That are user defined)
  5. Boolean (either TRUE or FALSE)
  6. Pseudo types (Hybrid PHP variables)

 No type definition required.

This is another feature that makes thing language very easy to learn. There is no specific declaration for variable types. That is if you can store any data type supported in a PHP variable just by declaring it.


$xyz = ‘john’;
$abc = ‘1000’;
$pqr = ‘ Welcome to the tutorial’;

The above example shows assigning of characters, integers and string values without declaration of data types.

  • Printing the variables:

Printing the values of these variables can be done using echo function


 echo $name;

Will display the values stored in $name variable.

Also Check:

  • PHP Tutorial 1: Introduction for Beginners
  • PHP Tutorial 2: PHP Script Syntax and HTML File Compatibility

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.