Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Variables & Data Types
PHP Intermediate FREE

Variables & Data Types

Lesson 2 of 17 Intermediate Interactive

Variables in PHP store data that can be used and manipulated throughout your script. All variables in PHP start with a $ sign followed by the variable name.

Variable Rules

  • Must start with $ followed by a letter or underscore
  • Can contain letters, numbers, and underscores
  • Are case-sensitive ($name and $Name are different)
  • Do not need to be declared before use

PHP Data Types

  • String — Text: $name = "SukhNexus";
  • Integer — Whole numbers: $age = 25;
  • Float — Decimal numbers: $price = 99.99;
  • Boolean — True or false: $active = true;
  • Array — Collection of values: $colors = ["red", "green"];
  • NULL — No value: $item = null;

Type Juggling

PHP is loosely typed — a variable can change its type automatically. Use gettype() to check the type and var_dump() to debug.

Syntax

PHP
<?php
// Strings

$name = "SukhNexus Academy";

// Integers

$age = 25;
$negative = -10;

// Floats

$price = 49.99;

// Booleans

$isActive = true;
$isAdmin = false;

// Arrays

$fruits = ["apple", "banana", "mango"];

// NULL

$empty = null;

// Check type

echo gettype($name);  // string

var_dump($age);        // int(25)

?>
PHP Data Types
PHP
<?php
// String
$name = "Alice";
echo gettype($name) . ": $name
";

// Integer
$age = 25;
echo gettype($age) . ": $age
";

// Float
$price = 19.99;
echo gettype($price) . ": $price
";

// Boolean
$active = true;
echo gettype($active) . ": " . ($active ? "true" : "false") . "
";

// Array
$colors = ["red", "green", "blue"];
echo gettype($colors) . ": " . implode(", ", $colors) . "
";

// Null
$val = null;
echo gettype($val) . "
";
?>

Practice

1
Exercise

Create variables of different types (string, integer, boolean) and display them with echo.

Answer
<?php
$language = "PHP";
$version = 8;
$isFree = true;

echo "Language: $language<br>";
echo "Version: $version<br>";
echo "Free: " . ($isFree ? "Yes" : "No");
?>
2
Exercise

Use var_dump() to inspect the type and value of a variable.

Answer
<?php
$price = 19.99;
var_dump($price);
// float(19.99)
?>

Quick Quiz

1

What symbol do all PHP variables start with?

All PHP variables must start with a $ sign.

2

What does var_dump() do?

var_dump() displays detailed information about a variable including its type and value.

3

What is the output of gettype(42)?

42 is a whole number, so gettype returns "integer".

Interview Questions

Type juggling is PHP's automatic type conversion. For example, when you concatenate a string with an integer, PHP automatically converts the integer to a string. This makes PHP flexible but can lead to unexpected results if you're not careful.

The == operator checks value equality with type coercion (e.g., "0" == 0 is true). The === operator checks both value and type without conversion (e.g., "0" === 0 is false). Always use === for strict comparisons to avoid bugs.