
- 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
PHP 8 – How to use ValueError to check if the value encountered is of correct type?
PHP 8 uses a new built-in exception ValueError. PHP throws this exception when we pass a value to a function, which has a valid type but cannot be used for operation. In the earlier versions of PHP, we used to get a Warning error in such cases, but PHP 8 will show a ValueError.
Example: ValueError in PHP 8
<?php declare(strict_types=1); array_rand([1,2,3], 0); json_decode('{}', true, -1); ?>
Output
Fatal error: Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be empty
Example
<?php $x = strpos("h", "hello", 16); var_dump($x); ?>
Output
bool(false)
Example: ValueError in PHP 8
<?php $x = strpos("h", "hello", 16); var_dump($x); ?>
Output
Fatal error: Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be empty
- Related Articles
- How to check if String value is Boolean type in java?
- PHP - How to use $timestamp to check if today is Monday or 1st of the month?
- Make PHP pathinfo() return the correct filename if the filename is UTF-8
- How to check a file is video type or not in php?
- How to check if square root is either correct or wrong ?
- How to check if type of a variable is string in Python?
- PHP 8 – Using str_contains() to check if a string contains a substring
- What is the correct way to check if String is empty in Java?
- Union Type in PHP 8
- How to use BIGINT(8)value in Android sqlite?
- How to check if the value is primitive or not in JavaScript?
- How to Check if PHP session has already started?
- How to catch ValueError using Exception in Python?
- Mixed Pseudo Type in PHP 8
- How to check if a value is object-like in JavaScript?

Advertisements