HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
PHP›Variables

Variables

Creating variables with $, naming, and constants (define/const).

Simple enough for a 5-year-old to understand

Variable with $

Example code
1
<?php
Start.
DefinitionStart.
2
$name = "Alex";
String variable.
VariableString variable.
3
$age = 36;
Number variable (type is automatic).
VariableNumber variable (type is automatic).
4
echo "$name - $age";
Alex - 36
Output / InputAlex - 36
DefinitionVariableOutput / Input
💡 Hint: Don't forget the $ sign: name = 5 is wrong, $name = 5 is correct.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Assign "Berlin" to the $city variable and print it.

🎯 Expected output: Berlin

Loading editor…

About this topic

Variables in PHP start with a dollar sign and you don't declare their type — the assigned value determines the type. You can assign a number first and then text to the same variable; PHP allows this.

This flexibility allows for fast writing, but it comes with a price: type errors are not visible until runtime. Modern PHP therefore allows type declarations in function parameters and properties, and it is recommended to enable strict type mode.

Variable names in PHP are CASE-SENSITIVE, but function names are not. This inconsistency stems from the language's history and is a classic detail that surprises beginners.

What you will learn

  • Defining variables with a dollar sign
  • Dynamic type behavior
  • Variable scope and the global keyword
  • Defining constants
  • Variable interpolation inside text
  • Checking existence with isset and empty

When to use?

Reading an undefined variable produces a warning since PHP 8 and is evaluated as null. For incoming data from external sources — form fields, query strings — checking with isset or providing a default with the null coalescing operator before using the value is standard protection.

Constants are used for settings whose value will not change throughout the program; constants do not take a dollar sign and cannot be reassigned.

Common mistakes

Wrong

$name = "Ada";
echo 'Hello $name';

Variables are NOT INTERPOLATED inside single quotes; they are printed literally to the screen with the dollar sign. Double quotes are required for variable interpolation.

Correct

$name = "Ada";
echo "Hello $name";

Wrong

$count = 5;
function show() {
    echo $count;
}
show();

Functions in PHP DO NOT SEE the outer scope — they behave differently from JavaScript and Python. $count is undefined inside the function and prints empty with a warning. Passing the value as a parameter is the correct solution.

Correct

$count = 5;
function show(int $value) {
    echo $value;
}
show($count);

Wrong

$total = "10";
$result = $total + 5;
$label = $total . 5;

PHP silently converts text to a number: addition gives 15, but concatenation gives "105". The same variable behaves as a different type on two lines, and this leads to bugs that are very hard to spot. External data must be explicitly converted.

Correct

$total = (int) "10";
$result = $total + 5;
$label = (string) $total . "5";

Frequently asked questions

How do I check if a variable is defined?

With isset. isset returns false if the variable does not exist or is null. empty also considers zero and empty text as empty — which is why using empty when "0" is a valid value leads to errors. The null coalescing operator is the cleanest way to provide a default value.

Should I use single quotes or double quotes?

Double quotes if variables will be interpolated inside. If it's plain text, single quotes clearly indicate intent and are a tick faster since no interpolation is performed. The difference is practically negligible; being consistent is what matters.

What does strict type mode do?

When you add a strict type declaration to the top of the file, PHP stops doing automatic type coercion: if a function expects an int and you pass the text "5", it throws an error. This makes silent type errors visible and is standard practice in modern PHP projects.

Summary

PHP variables start with a dollar sign, and the assigned value determines the type. Functions do not see the outer scope, single quotes do not interpolate variables, and strict type mode is recommended because automatic type coercion leads to silent bugs.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code