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
PHP automatically associates a data type to the variable, depending on its value.
🌐
Tutorialspoint
tutorialspoint.com › php › php_variable_types.htm
PHP - Variables
Below is the example of each type of variable. String : A sequence of characters. Integer : A whole number (without decimals). Float (Double) : A decimal number. Boolean : Represents true or false values.
🌐
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.
🌐
PHP
php.net › manual › en › language.types.php
PHP: Types - Manual
Types · Variables · Constants · Expressions · Operators · Control Structures · Functions · Classes and Objects · Namespaces · Enumerations · Errors · Exceptions · Fibers · Generators · Attributes · References Explained · Predefined Variables ·
🌐
Laravel
laravel.com › learn › php-fundamentals › variables-types
Variables & Types | Laravel - The clean stack for Artisans and agents
The main difference is that double quotes allow you to embed variables directly in your string, while single quotes treat everything as literal text. Strings can be combined using the dot (.) operator: ... In PHP, like in many other programming languages, a boolean represents two possible values: true or false.
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.

🌐
GeeksforGeeks
geeksforgeeks.org › php › php-variables
PHP Variables - GeeksforGeeks
April 11, 2025 - The value stored in a variable can be changed or updated during the execution of the script. All variable names start with a dollar sign ($). Variables can store different data types, like integers, strings, arrays, etc.
🌐
Tutorial Republic
tutorialrepublic.com › php-tutorial › php-data-types.php
Understanding the PHP Data Types - Tutorial Republic
PHP supports total eight primitive data types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource and NULL. These data types are used to construct variables.
Find elsewhere
🌐
Learn-php
learn-php.org › en › Variables_and_Types
Variables and Types - Learn PHP - Free Interactive PHP Tutorial
Once they are defined, we can use them in the code. PHP has many types of variables, but the most basic variable types are integer (whole numbers), float (real numbers), strings, and booleans.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-data-types
PHP Data Types - GeeksforGeeks
April 12, 2025 - Note: Double-quoted strings allow variable parsing; single-quoted strings don’t. ... <?php $greeting = "Hello"; echo "Message: $greeting \n"; // Message: Hello echo 'Message: $greeting'; // Message: $greeting ?> ... A boolean represents either true or false. It is useful for conditional statements and logic control. Now, let us understand with the help of the example: ... Compound types hold multiple values or are used to structure data more meaningfully.
🌐
dailycomputerscience
dailycomputerscience.com › post › variables-and-data-types-in-php-83
Variables and Data Types in PHP 8
October 4, 2024 - In this case, $id must be an integer, $name must be a string, and $isActive must be a boolean. If you try to assign a value of a different type, PHP will throw an error. PHP 8.3 brings several new features and improvements that are relevant to variables and data types:
🌐
Medium
medium.com › @radhwanrouihm › php-variable-types-23dbb904de6c
PHP — Variable Types - by radhwan ben youssef
March 2, 2022 - PHP variables do not have internal types — the variable does not know in advance whether it is used to store a number or a set of characters.
🌐
Medium
medium.com › @julmer526 › understanding-php-data-types-and-variables-a-beginners-guide-f8ac478a0599
Understanding PHP Data Types and Variables: A Beginner’s Guide | by Jordan Ulmer | Medium
December 16, 2023 - Think about my example above. Name… name of what, the user? Name of a website? Variable types should be descriptive, clear and succinct. A better example would be: ... Any variables declared in PHP must begin with a dollar sign ($), followed by the variable name.
🌐
Shodor
shodor.org › ~kevink › phpTutorial › destinye_PHPvariables.php
PHP Variables
Local Scope- is a variable that is declared within a PHP. Global Scope- is a variable that must be declared to be a global function inside the created function. You simply must put the word GLOBAL in all capitalized letter in front of the variable you want to make global.
🌐
C# Corner
c-sharpcorner.com › article › different-type-of-variables-in-php
Different Type of Variables in PHP
June 23, 2025 - Learn about the different types of variables in PHP, including scalar, arrays, objects, and more. This guide covers their usage, examples, and best practices for beginners and developers alike.