Tutorials Logic, IN info@tutorialslogic.com

PHP Data Types and Variables: Values, Conversion, and Inspection

PHP Values

A PHP variable stores a value and begins with $. PHP determines the variable type from the assigned value, so the same variable can technically receive another type later.

Useful PHP code still treats types deliberately. A price, user name, enabled flag, and missing result have different operations and different failure modes.

Variable Rules

  • A name starts with $ followed by a letter or underscore.
  • Names are case-sensitive: $user and $User are different variables.
  • Assignment uses =; comparison uses operators such as ===.
  • Choose names that describe the value rather than its position in an example.

Values with Different Types

Values with Different Types
<?php
$course = 'PHP';
$lessons = 12;
$progress = 0.25;
$isActive = true;
$nextLesson = null;

var_dump($course, $lessons, $progress, $isActive, $nextLesson);
Output
string(3) "PHP"
int(12)
float(0.25)
bool(true)
NULL

var_dump() shows both the type and value, which is useful when input does not behave as expected.

Built-in Types

Type Example Typical use
string 'PHP' Text
int 42 Whole numbers and identifiers
float 19.95 Measurements and decimal calculations
bool true Two-state decisions
null null No value is present
array ['php', 'sql'] Ordered or keyed collections
object new Course() State and behavior defined by a class
resource fopen(...) Handle to an external resource

Conversion and Juggling

PHP may coerce values in arithmetic, comparison, or a non-strict function call. That convenience can hide bad input, especially with numeric strings and loose comparison.

Convert at the boundary when the expected type is known. Validate first when a failed conversion must be distinguished from a legitimate zero.

Validate Before Converting

Validate Before Converting
<?php
$rawQuantity = '12';
$quantity = filter_var($rawQuantity, FILTER_VALIDATE_INT);

if ($quantity === false) {
    echo 'Quantity must be a whole number.';
} else {
    echo 'Items: ' . $quantity;
}
Output
Items: 12

The strict === false check keeps a valid integer 0 separate from failed validation.

Missing Value Cases

isset($value) is false when a variable is undefined or null. array_key_exists() is useful when an array key may intentionally contain null. The null coalescing operator provides a default for undefined or null values.

Question Tool
Is this variable defined and non-null? isset($value)
Does this array key exist even if its value is null? array_key_exists('key', $array)
Use a default for missing or null input $value ?? 'default'
Inspect the exact runtime type get_debug_type($value) or var_dump($value)

Choose the Right Type Check

0 of 2 checked

Q1. Why compare FILTER_VALIDATE_INT results with === false?

Q2. Which tool detects an array key whose value is null?

Try this next

Inspect Real Input

0 of 2 completed

  1. Test the strings 12, 0, 2.5, and twelve with FILTER_VALIDATE_INT. Record the type and value for every result.
  2. Create an array with a null key and compare isset() with array_key_exists().
Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.