Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How the Non-Capturing Exception Catches work in PHP 8?
In PHP versions prior to 8, catching an exception required storing it in a variable, even if that variable wasn't used. PHP 8 introduced non-capturing exception catches, allowing you to catch exceptions without the mandatory variable assignment. Traditional Exception Catching (Before PHP 8) Before PHP 8, the catch block required a variable to store the exception object ? Hello World In this example, the exception is caught using the variable $e, which holds the exception object and allows access to its methods like getMessage(). Non-Capturing Exception Catches (PHP 8+) PHP 8 allows you to omit the variable when you don't need to access exception details ?
Read Morefdiv() function in PHP 8
In PHP 8, the fdiv() function performs floating−point division according to the IEEE 754 standard. Unlike regular division, fdiv() allows division by zero without throwing errors, instead returning special IEEE 754 values. Syntax fdiv(float $num1, float $num2): float Parameters $num1 − The dividend (number to be divided) $num2 − The divisor (number to divide by) Return Value The function returns one of the following values ? INF (Infinity) − When a positive number is divided by zero -INF (Negative Infinity) − When a negative number is divided by ...
Read Morestr_starts_with and str_ends_with function in PHP 8
PHP 8 introduced two convenient string functions: str_starts_with() and str_ends_with(). These functions check if a given string starts or ends with another string, returning true if the condition is met, otherwise false. str_starts_with() Function This function checks if a string starts with a specified substring (needle). It performs a case-sensitive comparison. Syntax str_starts_with(string $haystack, string $needle): bool Example
Read MoreWhat are Weak Maps in PHP?
Weak Maps in PHP are a memory-efficient data structure that creates weak references to objects, allowing automatic garbage collection when objects are no longer needed elsewhere in your code. Introduced in PHP 8.0, weak maps help prevent memory leaks by automatically removing entries when their object keys are destroyed. How Weak Maps Work A WeakMap stores key-value pairs where the keys must be objects and are weakly referenced. Unlike regular arrays or maps, if an object used as a key is destroyed (no other references exist), the corresponding entry in the WeakMap is automatically removed during garbage collection ...
Read MoreWhat is Trailing Comma in PHP?
Trailing commas in PHP allow you to add a comma after the last element in arrays, function parameters, and function calls. This feature was introduced in PHP 7.2 for arrays and expanded in PHP 8.0 for function parameters and calls. Benefits of Trailing Commas Trailing commas make code more maintainable by allowing you to add new elements without modifying existing lines. This reduces merge conflicts in version control and makes code changes cleaner. Arrays (PHP 7.2+) You can use trailing commas in arrays since PHP 7.2 − Array ( ...
Read MoreWhat is Stringable Interface in PHP 8?
In PHP 8, a new Stringable interface is added that works with the __toString() magic method. The __toString method allows an object to be represented as a string. When a class defines this method, PHP automatically implements the Stringable interface, enabling better type hinting and string conversion handling. Syntax interface Stringable { public function __toString(): string; } Basic Example Here's how to implement the __toString method in a class − Employee Name Automatic Stringable Implementation In PHP 8, any class that ...
Read MoreNullsafe Operator in PHP 8
PHP 8 introduces the nullsafe operator (?->) as a cleaner alternative to null check conditions. Using the nullsafe operator, we can safely chain method calls and property accesses without worrying about null values causing fatal errors. When the left-hand side of the nullsafe operator evaluates to null, the entire chain of execution stops and returns null. If it does not evaluate to null, it behaves like a normal object operator (->). Syntax The nullsafe operator uses the syntax ?-> instead of the regular -> operator − $object?->method()?->property?->anotherMethod(); Example Here's a basic example ...
Read MoreMatch Expression in PHP 8
The match expression is a new feature introduced in PHP 8 that provides a more concise and type-safe alternative to switch-case statements. Unlike switch statements, match expressions perform strict comparisons and return values directly. Key Differences from Switch Statements Match expressions use strict comparison (===) instead of loose comparison (==) No need for break statements − execution doesn't fall through Returns a value that can be assigned to variables or used in expressions Supports multiple conditions with comma separation Example: PHP 7 Switch Statement Behavior In PHP 7, switch statements use loose comparison, which ...
Read MoreConstructor Property Promotion in PHP 8
Constructor Property Promotion is a powerful PHP 8 feature that significantly reduces boilerplate code when creating simple classes. It allows you to combine property declaration, constructor definition, and variable assignment into a single, clean syntax. Traditional PHP 7 Approach Before PHP 8, creating a class with properties required separate property declarations and manual assignments ? 10.9 20 30.8 PHP 8 Constructor Property Promotion With constructor property promotion, you can declare properties directly in the constructor parameters ? 10.9 20 30.8 Employee ...
Read MoreMixed Pseudo Type in PHP 8
The mixed type in PHP 8 is a new built-in union type that accepts any value type. It represents the union of array|bool|callable|int|float|string|object|null|resource, making it more explicit than omitting type declarations entirely. The mixed type indicates that a parameter, return value, or property can accept any PHP type, providing better code documentation and IDE support compared to having no type declaration at all. Syntax function functionName(mixed $parameter): mixed { // Function body } class ClassName { public mixed $property; } Equivalent Union Type The ...
Read More