← Back to blog
9/18/2025

PHP Variables, Data Types, and Operators Explained

In the previous post, we introduced PHP, set up our environment, and wrote our first script. Now, it’s time to explore the core building blocks of...

In the previous post, we introduced PHP, set up our environment, and wrote our first script. Now, it’s time to explore the core building blocks of PHP: variables, data types, and operators. Without these, you cannot perform meaningful logic or calculations in PHP.


What Are Variables?

A variable is a named container used to store data. In PHP, variables always begin with a dollar sign ($).

Rules for Naming Variables

  1. Must start with $.
  2. Must begin with a letter or underscore.
  3. Can contain letters, numbers, and underscores (_).
  4. Case-sensitive ($name and $Name are different).

Example

<?php
  $name = "Andy";
  $age = 25;
  $isDeveloper = true;

  echo "Name: $name <br>";
  echo "Age: $age <br>";
  echo "Developer: $isDeveloper";
?>

Output:

Name: Andy
Age: 25
Developer: 1

(Booleans display 1 for true, and nothing for false.)


Data Types in PHP

PHP is a loosely typed language. This means you don’t need to declare a variable’s type — PHP figures it out automatically.

1. String

Text inside quotes (" " or ' ').

<?php
  $city = "Douala";
  echo "I live in $city";
?>

2. Integer

Whole numbers.

<?php
  $year = 2025;
  echo $year;
?>

3. Float (Double)

Decimal numbers.

<?php
  $price = 19.99;
  echo $price;
?>

4. Boolean

true or false.

<?php
  $isLoggedIn = false;
  var_dump($isLoggedIn);
?>

5. Array

Collection of values in one variable.

<?php
  $fruits = ["Apple", "Banana", "Mango"];
  echo $fruits[1]; // Outputs: Banana
?>

6. Object

Instances of classes (covered in OOP).

<?php
  class Car {
    public $brand;
  }

  $car = new Car();
  $car->brand = "Toyota";
  echo $car->brand;
?>

7. NULL

Represents an empty variable.

<?php
  $value = null;
  var_dump($value);
?>

Constants in PHP

A constant is a variable whose value cannot change.

<?php
  define("SITE_NAME", "My PHP Blog");
  echo SITE_NAME;
?>

PHP 7+ also allows:

<?php
  const VERSION = "1.0";
  echo VERSION;
?>

PHP Operators

Operators are symbols used to perform actions on variables and values.

1. Arithmetic Operators

<?php
  $x = 10;
  $y = 3;

  echo $x + $y; // 13
  echo $x - $y; // 7
  echo $x * $y; // 30
  echo $x / $y; // 3.333...
  echo $x % $y; // 1
?>

2. Assignment Operators

<?php
  $a = 5;
  $a += 3; // 8
  $a -= 2; // 6
  $a *= 2; // 12
  $a /= 3; // 4
?>

3. Comparison Operators

<?php
  $x = 10;
  $y = "10";

  var_dump($x == $y);  // true (values equal)
  var_dump($x === $y); // false (type different)
  var_dump($x != 5);   // true
  var_dump($x > 5);    // true
  var_dump($x < 5);    // false
?>

4. Logical Operators

<?php
  $isAdmin = true;
  $isLoggedIn = false;

  var_dump($isAdmin && $isLoggedIn); // false
  var_dump($isAdmin || $isLoggedIn); // true
  var_dump(!$isAdmin);               // false
?>

5. String Operators

<?php
  $first = "Hello";
  $second = "World";

  echo $first . " " . $second; // Concatenation: Hello World
  $first .= " Everyone";
  echo $first; // Hello Everyone
?>

6. Increment/Decrement Operators

<?php
  $count = 5;

  echo ++$count; // 6 (pre-increment)
  echo $count++; // 6 (post-increment, then becomes 7)
  echo --$count; // 6 (pre-decrement)
  echo $count--; // 6 (post-decrement, then becomes 5)
?>

7. Ternary Operator

Shortcut for if-else.

<?php
  $age = 18;
  $status = ($age >= 18) ? "Adult" : "Minor";
  echo $status;
?>

var_dump() vs print_r()

When debugging, you often need to inspect values.

<?php
  $data = ["name" => "Andy", "role" => "Developer"];
  var_dump($data);   // Shows type + structure
  print_r($data);    // Human-readable output
?>

Conclusion

In this post, you learned:

  • How to declare variables and follow naming rules.
  • Different PHP data types (string, integer, float, boolean, array, object, null).
  • How to create constants.
  • Operators for arithmetic, comparison, logic, strings, and assignment.
  • Useful debugging functions (var_dump() and print_r()).

These concepts are the foundation of PHP programming. In the next post, we’ll move into control structuresif, switch, for, while, and more.