Variables & Data Types in PHP
Variables & Data Types in PHP
A variable in PHP is a named container for a value, and it always starts with a dollar sign ($). PHP figures out the data type automatically based on the value you store, so you do not have to declare a type up front.
Declaring variables
You create a variable by assigning a value to a name that starts with $.
<?php
$name = "Sneha"; // a string
$age = 21; // an integer
$height = 5.6; // a float (decimal number)
echo $name; // prints Sneha
echo $age; // prints 21
?>
Naming rules
- A variable name must start with
$followed by a letter or underscore. - It can contain letters, numbers, and underscores, but not spaces.
- Names are case-sensitive:
$scoreand$Scoreare different.
<?php
$first_name = "Rahul"; // valid
$_count = 10; // valid
// $1total = 5; // invalid: cannot start with a number
?>
Core data types
PHP has several built-in types. The most common ones for beginners are below.
Strings
A string is text, written inside single or double quotes.
<?php
$city = "Jalgaon";
$greeting = 'Hello';
// Double quotes allow variables inside the string.
echo "Welcome to $city"; // Welcome to Jalgaon
// Single quotes are taken literally.
echo 'Welcome to $city'; // Welcome to $city
?>
Integers and floats
Integers are whole numbers; floats have decimal points.
<?php
$quantity = 12; // integer
$price = 49.99; // float
$total = $quantity * $price; // PHP handles the maths
echo $total;
?>
Booleans
A boolean holds either true or false, often used in conditions.
<?php
$isActive = true;
$isAdmin = false;
if ($isActive) {
echo "The account is active.";
}
?>
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesArrays
An array holds multiple values in one variable. (Arrays have their own dedicated lesson.)
<?php
$subjects = ["Maths", "Science", "English"];
echo $subjects[0]; // prints Maths (counting starts at 0)
?>
Null
null represents "no value". A variable is null until you give it a value, or you can set it explicitly.
<?php
$pending = null;
// var_dump shows both the type and the value - great for learning.
var_dump($pending); // NULL
?>
Checking a variable's type
Use var_dump() or gettype() to inspect what a variable holds. This is one of the best learning habits in PHP.
<?php
$value = 42;
var_dump($value); // int(42)
echo gettype($value); // integer
?>
Type juggling and casting
PHP automatically converts types when needed, which is called type juggling. You can also force a type with a cast.
<?php
$text = "10";
$number = (int) $text; // explicitly cast string "10" to integer 10
echo $number + 5; // 15
?>
While automatic conversion is convenient, it can cause surprises, so it helps to know the type you are working with.
Constants
Unlike variables, a constant cannot change once set. Define one with const or define().
<?php
const PI = 3.14159;
echo PI; // constants do not use the $ sign
?>
Common mistakes
- Leaving off the
$sign when using a variable. - Assuming
"5" + 5will throw an error — PHP may juggle the string to a number. Be explicit when it matters. - Mixing up single and double quotes. Only double quotes expand variables.
- Expecting array indexes to start at 1. They start at 0.
FAQ
Do I have to declare the data type? No. PHP is dynamically typed and chooses the type based on the value. You can still cast types when needed.
What is the difference between null and an empty string?
null means no value at all; an empty string "" is a string with zero characters. var_dump() shows the difference clearly.
How do I see what type a variable is?
Use var_dump($variable) for the most detailed output, or gettype($variable) for a simple name.
Keep learning
Return to the PHP & MySQL hub, revisit PHP Syntax & Basics, or move on to Operators in PHP.
Practise these concepts with mentor feedback. Join the waitlist for the PHP course at Infoplanet in Jalgaon.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in PHP: Indexed, Associative & Multidimensional
Understand PHP arrays - indexed, associative, and multidimensional - and the most useful array functions, with examples for adding, reading, and looping over data.
Control Flow in PHP: if, switch & Loops
Make PHP code decide and repeat with control flow: if/else, switch, the match expression, and the for, while, and foreach loops, with clear examples.
Handling Forms in PHP (GET & POST)
Handle HTML forms in PHP: the difference between GET and POST, reading input from the superglobals, validating data, and escaping output to prevent XSS.
