This type-hinting only works for validating function arguments, return values, properties, and class constants; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when blah is called, but $bur could be reassigned to a non-Bur value inside the function.

Type-hinting works for any data type except for resources. Initially, it was available only with class names, but later, support for array (as of PHP 5.1), callable (as of PHP 5.4), string, int, float, bool (as of PHP 7.0) was added. Since then, PHP has added support for even more types, which you can see listed in the PHP manual.

The type can also be made nullable by adding ? before it. This is identical to a type union with null.

foo(?string $var) { }
// is the same as
foo(string|null $var) { }

You can also use composite types such as string|int as of PHP 8.0.

An important distinction is that, unless your PHP file is in strict mode, the arguments that you pass to a function call will be type-juggled to the appropriate scalar type if possible. To avoid this, your PHP file must use strict typing mode.

Answer from JW. on Stack Overflow
🌐
PHP
php.net › manual › en › language.variables.variable.php
PHP: Variable variables - Manual
This information becomes relevant when writing a parser, tokenizer or something else that operates on PHP syntax. <?php $foo = 'bar'; $ /* I am complete legal and will compile without notices or error as a variable variable. */ $foo = 'magic'; echo $bar; // Outputs magic.
🌐
W3Schools
w3schools.com › pHP › php_variables.asp
PHP Variables
A variable can have a short name (like $x and $y) or a more descriptive name ($age, $carname, $total_volume). ... Remember that PHP variable names are case-sensitive!
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-variables
PHP Variables - GeeksforGeeks
April 11, 2025 - A variable in PHP is a container used to store data such as numbers, strings, arrays, or objects.
Top answer
1 of 5
91

This type-hinting only works for validating function arguments, return values, properties, and class constants; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when blah is called, but $bur could be reassigned to a non-Bur value inside the function.

Type-hinting works for any data type except for resources. Initially, it was available only with class names, but later, support for array (as of PHP 5.1), callable (as of PHP 5.4), string, int, float, bool (as of PHP 7.0) was added. Since then, PHP has added support for even more types, which you can see listed in the PHP manual.

The type can also be made nullable by adding ? before it. This is identical to a type union with null.

foo(?string $var) { }
// is the same as
foo(string|null $var) { }

You can also use composite types such as string|int as of PHP 8.0.

An important distinction is that, unless your PHP file is in strict mode, the arguments that you pass to a function call will be type-juggled to the appropriate scalar type if possible. To avoid this, your PHP file must use strict typing mode.

2 of 5
45
  1. Specifying a data type for a function parameter will cause PHP to throw a catchable fatal error if you pass a value which is not of that type. Please note though, you can only specify types for classes, and not primitives such as strings or integers.
  2. Most IDE's can infer a data type from a PHPDoc style comment if one is provided. e.g.

/**
 * @var string
 */
public $variable = "Blah";

As of PHP 7 (which is several years old at this point), primitive types can also be declared for function arguments. Nullability can also be indicated with a ? in front of the type from 7.1 onward. You can declare return types now, too. So this is valid PHP these days:

public function hasFoo(?int $numFoos) :bool {

PhpStorm (my current preferred IDE) is happy to use all of these types for code completion, so I don't need as many phpDoc comments for typing as I used to.

🌐
Codecademy
codecademy.com › learn › learn-php › modules › learn-php-variables › cheatsheet
Learn PHP: Learn PHP Variables Cheatsheet | Codecademy
Variable names can contain numbers, letters, and underscores (_). A sigil ($) must always precede a variable name. They cannot start with a number and they cannot have spaces or any special characters. The convention in PHP is to use snake case for variable naming; this means that lowercase words are delimited with an underscore character (_).
🌐
Tutorialspoint
tutorialspoint.com › php › php_variable_types.htm
PHP - Variables
Variables in PHP are used to store data that can be accessed and modified across the program. A variable can store a wide range of values, like numbers, text, arrays and even objects.
Find elsewhere
🌐
Codecademy
codecademy.com › learn › learn-php-introduction › modules › php-strings-numbers-variables › cheatsheet
Learn PHP: Introduction: PHP Strings, Numbers, and Variables Cheatsheet | Codecademy
Variable names can contain numbers, letters, and underscores (_). A sigil ($) must always precede a variable name. They cannot start with a number and they cannot have spaces or any special characters. The convention in PHP is to use snake case for variable naming; this means that lowercase words are delimited with an underscore character (_).
🌐
W3Schools
w3schools.com › php › php_variables_scope.asp
PHP Variable Scope
To do this, use the global keyword before the variables (inside the function): $x = 5; $y = 10; function myTest() { global $x, $y; $y = $x + $y; } myTest(); echo $y; // outputs 15 Try it Yourself » · PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.
🌐
YouTube
youtube.com › dani krossing
4: How to Create PHP Variables | PHP Tutorial | Learn PHP Programming | PHP for Beginners - YouTube
How to Create PHP Variables | PHP Tutorial | Learn PHP Programming | PHP for Beginners. In this PHP tutorial you will learn about variables which are used to...
Published   October 22, 2015
Views   235K
🌐
In Easy Steps
ineasysteps.com › home › old › creating variables in php 7
Creating Variables in PHP 7 - In Easy Steps
May 26, 2022 - A “variable” is a named container in a PHP script in which a data value can be stored. The stored value can be referenced using the variable’s name and changed (varied) as the script proceeds.
🌐
Shodor
shodor.org › ~kevink › phpTutorial › destinye_PHPvariables.php
PHP Variables
PHP variables are characters that stores value or information such as text or integers in your code. It is important to know that variables in PHP are usually represented by a dollar sign($) followed by the name of the variable. Those variables can be used to hold values or expressions and ...
🌐
SitePoint
sitepoint.com › blog › php › php variables
phpmaster | PHP Variables
November 11, 2024 - There are a few standard variables that PHP creates automatically, but most of the time variables are created (or declared) by you, the programmer. By creating two variables called $name and $color, you could create generic code which can handle any input values.
🌐
Quora
quora.com › What-is-a-PHP-variable-and-how-is-it-declared-1
What is a PHP variable, and how is it declared? - Quora
Answer: Variables in a program are used to store some values or data that can be used later in a program. The variables are also like containers that store character values, numeric values, memory addresses, and strings.
🌐
W3Schools
w3schools.com › php › php_datatypes.asp
PHP Data Types
Casting allows you to change data type on variables: $x = 5; $x = (string) $x; var_dump($x); Try it Yourself » · You will learn more about casting in the PHP Casting Chapter.
🌐
Scaler
scaler.com › home › topics › php-tutorial › php variables
PHP Variables - Scaler Topics
March 19, 2024 - In PHP (Hypertext Preprocessor) a variable is a container that can store a value or a reference to a value. Variables can hold various data types such as strings, integers, floats, and booleans.