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
Server Side Programming Articles
Page 1011 of 2109
What 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 MoreUnion Type in PHP 8
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 MoreAttributes in PHP 8
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 MoreReading Attributes with Reflection API in PHP 8
In PHP 8, the Reflection API provides powerful methods to read attributes from classes, methods, properties, functions, and parameters. The getAttributes() method returns an array of ReflectionAttribute objects that allow you to inspect attribute names, arguments, and instantiate attribute instances. Syntax The getAttributes() method is available on all Reflection objects ? public function getAttributes(?string $name = null, int $flags = 0): array Parameters $name − Optional attribute class name to filter by $flags − Flags to control attribute retrieval behavior Example Here's how to read attributes from a function using the ...
Read MoreNumber comparisons in PHP 8
PHP 8 introduced significant changes to number comparisons, making them more consistent and predictable. When comparing values, PHP 8 uses number comparison for numeric types, but converts numbers to strings for string comparisons, following stricter rules. String Categories Strings can be categorized in three ways − Numeric string − Contains only numeric characters. Example − "1234" or "1.24e1". Leading-numeric string − Starts with numeric characters followed by non-numeric characters. Example − "12xyz" or "123 " Non-numeric string − Cannot be interpreted as numeric. Example − "foo" or "abc123" PHP 7 vs PHP 8 Comparison ...
Read MoreUniform variable syntax in PHP 7
In older versions of PHP, variable syntax was inconsistent and could be confusing. For example, expressions like ${$first['name']} created ambiguity in parsing order. PHP 7 introduced Uniform Variable Syntax to resolve these inconsistencies by enforcing left-to-right evaluation. The uniform variable syntax evaluates variables from left to right and requires proper use of curly brackets for clarity − echo ${$first['name']}; This syntax allows new combinations of operators but may break backward compatibility in expressions that relied on older evaluation patterns. Function Expression Example With uniform variable syntax, you can immediately invoke function expressions − ...
Read More