Urmila Samariya

Urmila Samariya

108 Articles Published

Articles by Urmila Samariya

Page 9 of 11

str_starts_with and str_ends_with function in PHP 8

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 296 Views

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 More

What are Weak Maps in PHP?

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 492 Views

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 More

What is Trailing Comma in PHP?

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 2K+ Views

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 More

What is Stringable Interface in PHP 8?

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 543 Views

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 More

Nullsafe Operator in PHP 8

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 1K+ Views

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 More

Match Expression in PHP 8

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 848 Views

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 More

Constructor Property Promotion in PHP 8

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 827 Views

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 More

Mixed Pseudo Type in PHP 8

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 801 Views

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

Union Type in PHP 8

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 930 Views

PHP 8 introduced Union Types, allowing variables, parameters, and return types to accept values of two or more types instead of a single type. Multiple types are joined using the vertical line (|) operator. Union types support function parameters, return types, and class properties, providing greater flexibility while maintaining type safety. Syntax type1|type2|...|type_n Basic Union Type Example Here's a simple class demonstrating union types for properties and methods − Integer value: 5 Float value: 11.54 Nullable Union Types In PHP 8, nullable types can ...

Read More

Attributes in PHP 8

Urmila Samariya
Urmila Samariya
Updated on 15-Mar-2026 3K+ Views

Attributes in PHP 8 are special classes that add metadata to other classes, functions, methods, properties, constants, and parameters. They provide a structured way to add meta−information without affecting runtime behavior. Attributes have no impact on code execution but are accessible through PHP's reflection API. This allows other code to examine and process the metadata attached to various code elements. Key Features Multiple attributes can be applied to a single declaration Attribute class names are resolved like regular classes Attributes can be namespaced for better organization Parameters are optional but must use parentheses when present ...

Read More
Showing 81–90 of 108 articles
« Prev 1 7 8 9 10 11 Next »
Advertisements