PHP Variables and Data Types
Learning PHP Variables and Data Types are the basic concept of PHP. A variable is a kind of container that we used to store information. and datatype defines type of variable.
Variables are used in PHP to store data that can be reused and modified while a program runs. They make your code dynamic and flexible instead of hard coded.
What is a Variable in PHP
A variable is a named container that holds a value. In PHP every variable starts with a dollar sign followed by the variable name.
Example
//datatype & variable
<?php
$name = "Varsha";
$age = 21;
?>
In this example $name stores a string and $age stores a number.
Rules for Naming Variables
- A variable name must start with a dollar sign
- It must begin with a letter or an underscore
- It cannot start with a number
- Variable names are case sensitive. (It means if you are declaring a variable then $Name, $NAME, $name, $nAME all are different)
Example
<?php
$city = "Delhi";
$City = "Mumbai"; // This is a different variable
?>
PHP Data Types Overview
PHP supports different types of data depending on what kind of value you are storing. Understanding data types and variables help you write correct and efficient code.
String
A string is a group of characters enclosed in single or double quotes. Strings are commonly used for text like names messages or labels etc.
Example
<?php
$message = "Welcome to PHP Tutorials";
?>
Integer
An integer is a whole number or numeric value without any decimal point. It can be positive or negative.
Example
<?php
$quantity = 18;
?>
Float
A float is simply a number with a decimal point, useful for handling exact values.
Example
<?php
$price = 99.50;
?>
Boolean
A Boolean data type can hold only two values: True or False. It is primarily used in conditional and building decision-making logics.
<?php
$isLoggedIn = true;
?>
Array
In PHP an array stores multiple values in a single variable. In array we can group the same type of data or different type of data under a single name. This is very useful when working with lists of data.
<?php
$colors = array("Red", "Green", "Blue", "Yellow");
echo $colors [1]; //Output = Green
?>
You can also access array values using their index numbers.
<?php
echo $colors[0]; // Output: Red
?>
NULL
In PHP, NULL is a specific type of datatype which indicates a variable with no value set to it.
Example
<?php
$Fee = null;
?>
Checking Variable Type
PHP also has a built-in function called var_dump() which shows the data type and value of a variable. This is very helpful for debugging.
Example
<?php
var_dump($marks);
?>
Dynamic Typing in PHP
PHP is a dynamically typed language. This means you do not need to declare a data type in advance. PHP automatically knows the type based on the value supplied.
Example
<?php
$data = 10;
$data = "Now I am a string";
echo $data;
?>
Why Variables and Data Types are Important
Variables act like containers that store values, making code reusable, readable, and dynamic, while data types describe the nature of those values whether they are integers, text, true/false etc ensuring correct operations and preventing mistakes. They allow you to store user input do calculations display material and interface with databases.
Check out our resources!
- Bootstrap Templates: Explore our Bootstrap Projects section.
- Free E-Books: Download your Free E-Books here.



4 Responses
[…] Link to PHP Control StructuresLink to PHP OperatorsLink to PHP Variables and Data Types […]
[…] Link to PHP IntroductionLink to PHP Syntax and CommentsLink to PHP Variables and Data Types […]
[…] Link to PHP IntroductionLink to PHP Syntax and CommentsLink to PHP Variables and Data Types […]
[…] Link to PHP Variables and Data TypesLink to PHP Loops in DepthLink to PHP Control Structures […]