
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Difference between gettype() in PHP and get_debug_type() in PHP 8
In the earlier versions of PHP, if we wanted to get the type of a variable, we used to employ the gettype() function. This function returns the type of a variable in the custom of a string. It returns all possible values like integer, string, array, boolean, double, resource, NULL, unknown type, etc.
However, there were issues in the gettype function. It does not return native and familiar type names. It returns double instead of float and integer instead of int, and so on.
To overcome this problem, PHP 8 uses the get_debug_type function.
get_debug_type() function
In PHP 8, the get_debug_type function returns the true native type of variables. It returns a float, int instead of double and integer. This function automatically resolves the class names of objects.
get_debug_type() function helps in
debugging
business logic
error reporting
Example: using gettype() function in PHP
<?php class Novel {} class Comments {} $novel = new Novel(); if(! ($novel instanceof Comment)) { echo 'Expected ' . Comment::class . ' still got My' . (is_object($novel) ? get_class($novel) : gettype($novel)); } ?>
Output
Expected Comment still got MyNovel
Example : Using get_debug_type() function in PHP 8
<?php class Novel {} class Comments {} $novel = new Novel(); if(! ($novel instanceof Comment)) { echo 'Expected '.Comment::class.' still got My'.get_debug_type($novel); } ?>
Output
Expected Comment still got MyNovel
- Related Articles
- Union Type in PHP 8
- Mixed Pseudo Type in PHP 8
- Difference between !== and ==! operator in PHP
- How to get resource ID using get_resource_id() function in PHP and PHP 8?
- Difference between the and$ operator in php
- Difference between bindParam and bindValue in PHP
- Difference Between For and Foreach in PHP
- Difference between Python and PHP.
- Difference Between PHP and JavaScript
- Difference Between PHP and Python
- Difference between PHP and C
- How to debug and log PHP OPcache Issues
- Difference between the AND and && operator in php
- Explain difference between Abstraction and Encapsulation in PHP.
- Difference between the | and || or operator in php
