Understanding Variables, Data Types, and Operators in PHP

Learn the basics of PHP with an introduction of variables, data types, and operators to create dynamic web page or web application .

CodeEncape - Understanding Variables, Data Types, and Operators in PHP

When you start to learn PHP, one of the most important thing is to learn about Variables, data types and operators. In this tutorial we will discuss about how to create or define variables to create dynamic web page or web application. And also learn about data types and different types of operators and usage of it to create dynamic web application.

Variables in PHP

A variable is a container that stores data which can be used and manipulated throughout your program.

In PHP, all variables must be starts with a dollar sign ($).

let's we discuss about basic rules to create variables in PHP,

  • A variable can store numbers, text, arrays, objects, and more.
  • A valid variable name must be starts with a letter or underscore.

  • The variable name is case-sensitive.
For Example :
<?php
$var = 'Hello';
$Var = "World";
echo "{$var}, {$Var}"; // Outputs "Hello, World"

$1var = "Hello"; // invalid, starts with a number
$_1var = "World"; // valid, starts with an underscore.
?>

Datatypes in PHP

The data type defines, which type of data is stored in variables, whether it is string, integer or boolean or NULL. In PHP, there is no need to declare the data type whenever you create a variable - PHP determines it automatically.

Let's See, Which main data type is supported by PHP?

  • String - A piece of text like a word, sentence, or phrase.
    <?php
    $str = 'This is String';
    ?>
  • Float (Double) – Numbers with decimal points.
    <?php
    $float_num = 1250.50;
    ?>
  • Boolean – Represents true or false.
    <?php
    $is_logged_in  = true;
    ?>
    
  • Array – Collection of multiple values.
    <?php
    $fruits= array("Apple","Mango","Banana","Orange");
    ?>
  • NULL – A variable with no value.
    <?php
    $data= null;
    ?>

Operators in PHP

Operators are symbols that are used to perform operations on variables and values. They allow you to carry out tasks like mathematical calculations, comparisons, assignments, logical decisions, string handling, and working with arrays. In simple words, operators act like tools that help you manipulate data and control how your program behaves.

  • Arithmetic operators (+, -, *, /) : Used to perform mathematical calculations.
    <?php
    $a = 10;
    $b = 5;
    
    echo $a + $b; // 15
    echo $a - $b; // 5
    echo $a * $b; // 50
    echo $a / $b; // 2
    echo $a % $b; // 0 (remainder)
    ?>
  • Assignment Operators : Used to assign values to variables.
    <?php
    $x = 10;
    $x += 5; // 15
    $x -= 3; // 12
    ?>
  • Comparison Operators (==, !=, <, >) : Used to compare values and returns the Boolean value (true / false).
    <?php
    $a = 10;
    $b = 20;
    
    var_dump($a == $b);  // false
    var_dump($a != $b);  // true
    var_dump($a < $b);   // true
    ?>
  • Logical operators (&&, ||, !) : Used to combine multiple conditions.
    <?php
    $age = 25;
    $country = "India";
    
    if ($age > 18 && $country == "India") {
        echo "Eligible to vote";
    }
    ?>
  • Increment/Decrement operators : Used Increment or Decrement by one One (1) in any variable. Commonly it's used in loops or counters.
    <?php
    $x = 5;
    echo ++$x; // 6 (pre-increment)
    echo $x--; // 6 (then decreases to 5)
    ?>
  • String operators (.) : Used to join or append two string or text.
    • . (concatenation) : combines two strings.
    • .= (concatenation assignment) : appends one string to another
    <?php
    $a = "Hello";
    $b = " World";
    echo $a . $b;  // Hello World
    
    $str = "Hello";
    $str .= " World";
    echo $str;  // Hello World
    ?>
  • Array operators (+) : used to compare or combine arrays.
    <?php
    $a = ["a" => "red", "b" => "green"];
    $b = ["c" => "blue", "d" => "yellow"];
    
    print_r($a + $b); // Union of $a and $b
    ?>

Summary

  • Variables are containers for storing data.
  • Data types define the kind of data a variable can hold.
  • Operators help us perform operations on variables and values.

Categories

Latest Templates

Related Posts

Control Structures in PHP

Control structures in PHP allow developers to control the flow of code execution. In This tutorial,...

PHP Introduction

Basic introduction about what is php, benifits of php and how it runs on server and why it's widely...

Subscribe to our newsletter

Stay up-to-date about latest tech and new world. Unsubscribe at anytime!