PHP Part 2: Basic Data Types and Variables in PHP


Please Subscribe Youtube| Like Facebook | Follow Twitter


In PHP, a variable is a name that refers to a value. PHP supports several data types that can be used to define variables. In this article, we’ll discuss the basic data types and variables in PHP.

Data Types in PHP

  1. Integer: Integers are whole numbers that can be positive, negative or zero. In PHP, integers can be specified in decimal, hexadecimal or octal format.
  2. Float: Floats are numbers with a decimal point or an exponent. They are sometimes called double or real numbers. In PHP, floats can be specified in decimal or exponential notation.
  3. String: Strings are a series of characters, like “Hello World”. They can be specified using single or double quotes.
  4. Boolean: Booleans represent two possible states, true or false.
  5. Array: Arrays are used to store multiple values in a single variable. They can be indexed or associative.
  6. Object: Objects are instances of classes that contain properties and methods.
  7. NULL: NULL represents a variable with no value.

Variables in PHP

In PHP, variables are declared using the $ sign followed by the variable name. Here’s an example:

$name = "John";
$age = 30;
$height = 1.75;
$student = true;

In the above example, we have declared four variables: $name, $age, $height, and $student. The first variable is a string, the second variable is an integer, the third variable is a float, and the fourth variable is a boolean.

You can also declare an empty variable using the NULL keyword, like this:

$location = NULL;

Example Code

Here’s an example code that demonstrates the use of basic data types and variables in PHP.

// Integer data type
$a = 42;
echo "Value of a: " . $a . "\n";
echo "Data type of a: " . gettype($a) . "\n";

// Float data type
$b = 3.14;
echo "Value of b: " . $b . "\n";
echo "Data type of b: " . gettype($b) . "\n";

// String data type
$c = "Hello, World!";
echo "Value of c: " . $c . "\n";
echo "Data type of c: " . gettype($c) . "\n";

// Boolean data type
$d = true;
echo "Value of d: " . $d . "\n";
echo "Data type of d: " . gettype($d) . "\n";

// Array data type
$e = array(1, 2, 3, 4, 5);
echo "Value of e: ";
echo "Data type of e: " . gettype($e) . "\n";

// Null data type
$f = null;
echo "Value of f: " . $f . "\n";
echo "Data type of f: " . gettype($f) . "\n";

// Object data type
class Person {
  public $name;
  public $age;

$g = new Person();
$g->name = "John";
$g->age = 30;
echo "Value of g: ";
echo "Data type of g: " . gettype($g) . "\n";


Value of a: 42
Data type of a: integer
Value of b: 3.14
Data type of b: double
Value of c: Hello, World!
Data type of c: string
Value of d: 1
Data type of d: boolean
Value of e: Array
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
Data type of e: array
Value of f:
Data type of f: NULL
Value of g: Person Object
    [name] => John
    [age] => 30
Data type of g: object

In the above code, we have declared variables and then used the echo statement to output their values and data type to the browser.

In the above output, the boolean value of true is displayed as 1, which is a common convention in PHP.

PHP Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *