W3docs

PHP Variable Handling

PHP is a popular server-side scripting language that is used to create dynamic web pages. It is essential to have a good understanding of PHP variable handling

Introduction

Variable handling is the set of techniques you use to create, inspect, copy, and remove the values your PHP script works with. This page goes beyond simply assigning a value: it covers checking whether a variable exists (isset), finding and changing its type (gettype / settype), creating references, and the more dynamic features PHP offers — variable variables and variable functions.

If you have never declared a PHP variable before, start with PHP Variables and PHP Data Types, then come back here for the handling techniques.

Declaring and assigning variables

In PHP a variable name starts with a dollar sign ($) followed by a letter or underscore. You don't declare a type — PHP infers it from the value you assign with the = operator.

<?php
$name  = "John";   // string
$age   = 30;        // integer
$price = 19.99;     // float
$active = true;     // boolean

PHP is dynamically typed: the same variable can hold a string now and a number later. The variable simply takes on the type of whatever you last assigned.

String interpolation

Inside double-quoted strings (and heredocs) PHP replaces a variable with its value. Single-quoted strings are taken literally and do not interpolate.

php— editable, runs on the server

Checking and removing variables

Before reading a variable that might not exist, test for it. These three functions are the core of safe variable handling:

FunctionReturns true when...
isset($var)the variable exists and is not null
empty($var)the variable is unset, null, 0, "", "0", false, or []
unset($var)(no return) destroys the variable so isset becomes false
<?php
$user = null;
var_dump(isset($user));   // bool(false) — null counts as "not set"

$count = 0;
var_dump(empty($count));  // bool(true)  — 0 is "empty"

$temp = 10;
unset($temp);
var_dump(isset($temp));   // bool(false) — destroyed

isset is the guard you reach for most often, especially with superglobals like $_GET and $_POST where a key may be missing.

Inspecting and changing types

Because PHP infers types, you sometimes need to ask what type a value currently has, or force a conversion.

<?php
$price = 19.99;
echo gettype($price);     // double  (PHP's name for float)

$input = "42";
settype($input, "integer");
echo $input + 8;          // 50 — now a real integer, not the string "42"

Use gettype for a readable type name and settype (or a cast like (int)) to convert in place. For comparisons, prefer the type-aware functions is_int(), is_string(), is_array(), and so on.

Advanced variable handling

Once the basics are comfortable, PHP offers three more dynamic techniques. Use them sparingly — they are powerful but can hurt readability.

Variable references

A reference makes two names point to the same underlying value. Assign with =& and any change through one name is visible through the other.

<?php
$count = 1;
$alias =& $count;   // $alias is now another name for $count
$alias = 5;
echo $count;        // 5 — changed through the reference

References matter most when passing arguments to functions by reference (function f(&$x)), letting the function modify the caller's variable.

Variable variables

A variable variable uses the value of one variable as the name of another. The $$ syntax reads the inner variable to build the outer name.

<?php
$var = "greeting";
$$var = "Hello";     // creates $greeting
echo $greeting;      // Hello

This is occasionally handy for dynamic field names, but an associative array is almost always clearer and safer.

Variable functions

If a string variable holds a function name, appending () calls that function dynamically.

<?php
$fn = "strlen";
echo $fn("Hello World");   // 11

This powers simple dispatch tables and callbacks. Modern PHP usually prefers first-class callables or closures, but variable functions remain valid and fast.

Best practices

  • Use meaningful names. $counter or $totalPrice documents intent far better than $x or $tmp.
  • Initialize before use. Reading an undefined variable raises a warning; set a default or guard with isset().
  • Avoid globals. Pass values as function arguments or wrap state in objects instead of relying on global. See Variables Scope for why scope isolation matters.
  • Reach for arrays over variable variables. They are easier to iterate, debug, and reason about.
  • Use type declarations. Typed parameters (function setAge(int $age)) catch mistakes early and make code self-documenting.

Summary

PHP variable handling is more than assignment: it includes checking existence with isset/empty, removing values with unset, inspecting and converting types with gettype/settype, and the dynamic tools — references, variable variables, and variable functions. Lean on the simple, explicit techniques day to day and keep the dynamic ones for the rare cases that genuinely need them.

Practice

Practice
In PHP, which of the following is/are ways to handle variables?
In PHP, which of the following is/are ways to handle variables?

Practice

Practice
In PHP, which of the following is/are ways to handle variables?
In PHP, which of the following is/are ways to handle variables?
Was this page helpful?