Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 05

Data Types in PHP (with var_dump Output)

In this lesson: All 8 PHP data types with var_dump output — string, integer, float, boolean, array, object, NULL, resource. Hindi & English.

What is a data type

A data type is the kind of value a variable holds — is it text, a whole number, a decimal, a yes/no, or a list of items? PHP decides the type automatically from the value you assign, because it is loosely typed. But knowing the types helps you write correct code and understand what PHP is doing.

💡 The value "18" (in quotes) is text, while 18 (no quotes) is a number. They look the same but behave differently.

Scalar types (string, int, float, bool)

Scalar means a single value. These four are the ones you use every day:

<?php
  $name   = "Aman";   // string  - text
  $age    = 18;       // integer - whole number
  $price  = 99.5;     // float   - decimal number
  $active = true;     // boolean - true or false
?>
TypeExampleUsed for
string"Aman"Names, messages, any text
integer18Counts, ages, IDs
float99.5Prices, averages, measurements
booleantrue / falseYes/no flags, conditions

Array and NULL

An array holds many values in one variable. NULL means no value at all.

<?php
  $marks = [85, 90, 78];        // array of numbers
  $city  = null;                // no value yet

  echo $marks[1];               // 90  (second item)
?>
90

Array items are numbered from 0, so $marks[1] is the second value. NULL is useful when a value does not exist yet, like a middle name that was left blank.

Checking a type (var_dump / gettype)

To see what PHP thinks a value is, use var_dump() (type + value) or gettype() (type name only).

<?php
  $age = 18;
  var_dump($age);        // int(18)
  echo gettype($age);    // integer
?>
int(18)
integer

var_dump is your best friend while debugging — it removes all guessing about whether a value is a number or text.

Type conversion (casting)

You can force a value into another type by writing the target type in brackets — this is called casting.

<?php
  $text = "123";
  $num  = (int) $text;   // now the number 123
  echo $num + 1;         // 124
?>
124

PHP also converts automatically in many cases (type juggling). For example "5" + 2 gives 7 because the string is turned into a number for the calculation.

Common mistakes

  • Confusing "18" with 18. Text in quotes is a string, not a number; it can behave unexpectedly in maths.
  • Thinking NULL equals 0. NULL means "no value"; 0 is an actual number.
  • Using == when you mean ===. == ignores type (so "5" == 5 is true), while === checks type too.
  • Forgetting arrays start at index 0. The first item is [0], not [1].

Summary

  • PHP's everyday types: string, integer, float, boolean, array and NULL.
  • PHP picks the type automatically from the value (loosely typed).
  • Use var_dump() or gettype() to check a type.
  • Cast with (int), (float), etc.; PHP also juggles types on its own.
  • Use === for strict comparison that also checks the type.

Next, store many values together with arrays in PHP, or make decisions using if-else in PHP.

Frequently Asked Questions

How many data types does PHP have?
PHP has four scalar types (string, integer, float, boolean), two compound types (array, object), and two special types (NULL, resource). For beginners the everyday ones are string, integer, float, boolean, array and NULL.
How do I check the type of a variable in PHP?
Use var_dump($x) to see both the type and the value, or gettype($x) to get just the type name as text. var_dump is the most useful while debugging because it shows exactly what PHP thinks a value is.
What is the difference between integer and float?
An integer is a whole number like 18. A float has a decimal point like 99.5. If you divide two integers and the result is not whole, PHP gives you a float automatically.
What does NULL mean in PHP?
NULL means the variable has no value at all. A variable is NULL if you assign null to it, or if you use it before assigning anything. It is different from 0 or an empty string.
Does PHP change types automatically?
Yes. Because PHP is loosely typed, it converts types when needed — for example the string "5" becomes the number 5 in a calculation. This is called type juggling, and it is handy but can cause surprises, so use === for strict comparisons.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicOperators in PHP Your progress: 5 / 50 (10%) · Lesson 6 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free