PHP & Laravel — Zero to Hero Episode 03: Variables and Data Types — How PHP Stores Information

What Are We Doing in This Post?

Your environment is ready. PHP is running. Now we start writing real code.

In this episode, we learn about variables and data types. These are the absolute foundation of any programming language. Every program you will ever write — no matter how complex — uses variables. Understanding them properly from the start makes everything else easier.


What is a Variable?

A variable is a container that stores information.

That is it. Nothing more complicated than that.

Real world analogy: Think of a variable like a labeled box. You have a box, you write a name on it, and you put something inside. Later, whenever you need that thing, you just look for the box with that label.

In PHP, every variable starts with a dollar sign followed by the name you choose.

<?php

$name = "Gagan"; $age = 22;

echo $name; echo $age;

?>

The dollar sign tells PHP: "this is a variable." The equals sign does not mean "equal to" in PHP — it means "store this value into this container." This is called assignment.

Run this in your browser. You will see: Gagan22

They appear together because echo just prints the value directly. We will fix the spacing in a moment.


PHP Variable Rules

Before we go further, learn these rules once. They will save you from confusing errors later.

Variable names must start with a dollar sign followed by a letter or underscore. They cannot start with a number.

$name — valid $_price — valid $1product — not valid, will cause an error

Variable names are case sensitive. $Name and $name are two completely different variables in PHP.

Variable names cannot have spaces. Use underscore or camelCase instead.

$first_name — valid $firstName — valid $first name — not valid


Data Types — What Kind of Information Can PHP Store?

Not all information is the same. A name is different from a number. A number is different from a true/false value. PHP handles different kinds of information using data types.

Here are the four most important ones for now.

1. String

A string is text. Any sequence of characters wrapped in quotes.

<?php

$name = "Gagan"; $city = "Delhi"; $message = "Welcome to my website!";

echo $message;

?>

You can use double quotes or single quotes for strings. Both work. Double quotes have a special ability we will see shortly.

2. Integer

An integer is a whole number — no decimal point.

<?php

$age = 22; $year = 2025; $score = -10;

echo $age;

?>

No quotes around numbers. If you write $age = "22" with quotes, PHP treats it as a string, not a number. The difference matters when you do math.

3. Float

A float is a number with a decimal point.

<?php

$price = 99.99; $temperature = 36.6; $discount = 0.15;

echo $price;

?>

4. Boolean

A boolean stores only one of two values: true or false. Nothing else.

<?php

$is_logged_in = true; $has_paid = false;

echo $is_logged_in;

?>

When you echo a boolean, true prints as 1 and false prints as nothing. Booleans are not really meant to be printed — they are meant to be used in conditions. We will use them heavily starting Episode 05 when we cover if/else.


Seeing Your Variables Clearly — Using echo Properly

Earlier our output came out as Gagan22 with no space. Let us fix that and learn a few echo tricks.

Adding text and variables together:

<?php

$name = "Gagan"; $age = 22;

echo "My name is " . $name . " and I am " . $age . " years old.";

?>

The dot is the concatenation operator in PHP. It joins strings together. Think of it like glue — it sticks pieces of text together.

Output: My name is Gagan and I am 22 years old.

A cleaner way using double quotes:

<?php

$name = "Gagan"; $age = 22;

echo "My name is $name and I am $age years old.";

?>

When you use double quotes, PHP automatically replaces variable names inside the string with their values. This is called variable interpolation. Single quotes do not do this — they print the variable name literally.

Try this and see the difference:

<?php

$city = "Delhi";

echo "I live in $city"; echo "<br>"; echo 'I live in $city';

?>

First line outputs: I live in Delhi Second line outputs: I live in $city

The <br> is an HTML line break — it pushes the next output to a new line in the browser.


PHP is Loosely Typed — What Does That Mean?

In some languages, you must declare what type a variable is before using it. PHP does not require this. You just assign a value and PHP figures out the type automatically.

<?php

$value = 10; echo $value + 5;

$value = "Hello"; echo $value;

?>

First echo gives 15. Second echo gives Hello. The same variable held an integer and then a string — PHP allowed it without complaint.

This makes PHP beginner-friendly, but it also means you need to be careful. If you accidentally store "50" as a string and try to do math with it, PHP will usually handle it correctly, but not always. We will cover type handling properly when we get to functions.


A Real World Example — Putting It All Together

Let us build something slightly real. A simple user profile display.

Create a new file in your phplearning folder. Name it profile.php. Write this:

<!DOCTYPE html> <html> <head> <title>User Profile</title> </head> <body>

<?php

$name = "Gagan"; $age = 22; $city = "Delhi"; $is_premium = true; $balance = 1250.75;

echo "<h1>Welcome, $name!</h1>"; echo "<p>Age: $age</p>"; echo "<p>City: $city</p>"; echo "<p>Account Balance: Rs. $balance</p>";

if ($is_premium) { echo "<p>Account Type: Premium</p>"; } else { echo "<p>Account Type: Free</p>"; }

?>

</body> </html>

Visit http://localhost:8080/phplearning/profile.php in your browser.

You will see a simple profile page displaying all the variable values. The if/else block checks the boolean and shows different text based on whether the user is premium or not. We will go deep into if/else in Episode 05 — for now just notice how a boolean value controls what gets displayed.


Checking What Type a Variable Is

PHP gives you a built-in tool called var_dump() that shows you the value and the data type of any variable. This is extremely useful when debugging.

<?php

$name = "Gagan"; $age = 22; $price = 99.99; $active = true;

var_dump($name); echo "<br>"; var_dump($age); echo "<br>"; var_dump($price); echo "<br>"; var_dump($active);

?>

Output will look like this:

string(5) "Gagan" int(22) float(99.99) bool(true)

var_dump tells you the type and the value together. You will use this constantly while learning PHP to understand what is actually stored inside a variable.


What Did We Learn in This Post?

Variables are labeled containers that store information. In PHP, every variable starts with a dollar sign.

PHP has four basic data types: string for text, integer for whole numbers, float for decimal numbers, and boolean for true or false.

The dot operator joins strings together. Double quotes allow you to embed variables directly inside text.

PHP is loosely typed — you do not need to declare types, PHP figures them out automatically.

var_dump() shows you the type and value of any variable and is your best friend while debugging.


What is Coming in Episode 04?

Next, we cover operators and expressions — how PHP does math, compares values, and combines conditions. We will also cover string functions: built-in tools PHP gives you to manipulate text, like making text uppercase, counting characters, and finding words inside a string.

See you in the next one.


Next Episode: Operators and String Functions — Making PHP Do the Work For You

This is Episode 03 of the PHP and Laravel — Zero to Hero series.

No comments:

Post a Comment

PHP & Laravel — Zero to Hero Episode 04: Operators and String Functions — Making PHP Do the Work For You

What Are We Doing in This Post? In Episode 03, we learned how to store information in variables. But storing data is only half the job. The ...