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
๐ŸŒ
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!
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ language.variables.basics.php
PHP: Basics - Manual
PHP 8.5.4 Released! ... Variables in PHP are represented by a dollar sign followed by the name of the variable.
Discussions

Declaring Variable Types in PHP? - Stack Overflow
I was trying to get my Netbeans to autocomplete with PHP, and I learned that this code is valid in PHP: function blah(Bur $bur) {} A couple of questions: Does this actually impose any limits on what More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the proper way to declare variables in php? - Stack Overflow
You need to declare variables before echoing them out. An example is here: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Variable variables in PHP - What is their purpose? - Stack Overflow
In PHP there's a functionality officially called "Variable Variables" where one can assign variable variables. A variable variable takes the value of one variable as the name for a new variable! For More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the use case for variable variables?
To be honest, they're not very useful in real world situations. Usually variable variables are a code smell. If you see them in a real world situation, it's -usually- because someone took a bad shortcut. I suppose in certain situations they might be useful for unit testing... but that's a stretch. More on reddit.com
๐ŸŒ r/PHPhelp
13
1
July 20, 2023
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 (_).
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ php: variable
How to store and process data with PHP variables
May 24, 2024 - A PHP variable is a type of container that can store information while a PHP program is running for later access. Variables are identified by a name and can contain values from different data types, like numbers, strings, booleans, or complex ...
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 (_).
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.

๐ŸŒ
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.
๐ŸŒ
Plus2Net
plus2net.com โ€บ php_tutorial โ€บ php-variables.php
Variables in PHP the syntax declaration and using
February 5, 2000 - Getting Type of Variableโ†’ var_dump()โ†’ isset()โ†’ empty()โ†’ unset()โ†’ strval()โ†’ Transfer variables between pages and between sites โ†’ Cookies to store Infomation โ†’ constant: getting the value stored in a constant urlencode: Encoding variablas before sending them through URL Session Variables:Starting storing and destroying Sessions with Session ID โ† PHP
๐ŸŒ
Jobtensor
jobtensor.com โ€บ Tutorial โ€บ PHP โ€บ en โ€บ Variables
PHP Variables - Global, Local, Static Variables | jobtensor
<?php $txt = "Hello world!"; $myNumber = 5; $myFloat = 10.5; echo $txt . "<br>"; echo $myNumber . "<br>"; echo $myFloat . "<br>"; Variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used.
๐ŸŒ
Pi My Life Up
pimylifeup.com โ€บ home โ€บ variables in php
Variables in PHP - Pi My Life Up
July 6, 2022 - The following two sections will discuss declaring and outputting the variables in a PHP script. Declaring a variable in PHP is super easy, you specify your variable name and assign the value you want using the = operator.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ php-beginners-variables-data-types-muthukumaran-singaravelu
PHP for Beginners - Variables and Data types
October 7, 2019 - A variable is created the moment you assign a value to it. <?php $city="Chennai"; $_city="Madhurai"; $city3="Tanjore"; ?>
๐ŸŒ
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.