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
Programming Articles
Page 1038 of 2547
PHP Defining namespace
PHP namespaces provide a way to encapsulate and organize code by grouping related classes, functions, and constants. The namespace keyword must be declared as the very first statement in a PHP file, right after the opening Example Here's a simple example of defining a namespace with a class and function − Namespace Declaration Rules If the namespace declaration is not at the top of the file, PHP throws a fatal error. The following example demonstrates this − PHP Fatal error: Namespace declaration statement has ...
Read MorePHP Generators.
Traversing a big collection of data using looping constructs such as foreach would require large memory and considerable processing time. With generators it is possible to iterate over a set of data without these overheads. A generator function is similar to a normal function. However, instead of return statement in a function, generator uses yield keyword to be executed repeatedly so that it provides values to be iterated. The yield keyword is the heart of generator mechanism. Even though its use appears to be similar to return, it doesn't stop execution of function. It provides next value for iteration ...
Read MorePHP Generators vs Iterator objects
In PHP, generators and iterator objects both provide ways to iterate over data, but they work differently. When a generator function is called, it returns a Generator object that implements the Iterator interface, making it behave like an iterator with some limitations. Iterator Interface Methods The Iterator interface defines the following abstract methods − Iterator::current — Return the current element Iterator::key — Return the key of the current element Iterator::next — Move forward to next element Iterator::rewind — Rewind the Iterator to the first element Iterator::valid — Checks if current position is valid A ...
Read MorePHP Nested Exception
In PHP, nested exceptions allow you to handle multiple types of errors at different levels. You can nest try-catch blocks to any desired depth, with exceptions being handled in reverse order − innermost exceptions are processed first. Syntax The basic structure of nested exception handling ? try { // Outer try block try { // Inner try block // Code that may throw exceptions } catch (SpecificException $e) ...
Read MorePHP Interaction between finally and return
In PHP, there is a peculiar behavior of the finally block when either the try block or catch block contains a return statement. Normally, a return statement causes program control to go back to the calling position. However, in functions with try/catch blocks containing return statements, the finally block is always executed first before returning. Example In the following example, the div() function has a try-catch-finally construct. The try block returns the result of division when no exception occurs. In case of an exception, the catch block returns an error message. However, in either case, the statement in ...
Read MorePHP Exception Handling with finally
In PHP, the finally block is used with try-catch statements to ensure that certain code always executes, regardless of whether an exception occurs or not. This block appears after the catch block or can be used instead of a catch block. Syntax The finally block can be used in two ways − try { // Code that may throw an exception } catch (Exception $e) { // Handle exception } finally { // Always executed } // OR without catch block try { ...
Read MorePHP Extending Exceptions
Exception class implements Throwable interface and is base class for all Exception classes, predefined exceptions as well as user defined exceptions. The Exception class defines some final (non-overridable) methods to implement those from Throwable interface, and __toString() method that can be overridden to return a string representation of Exception object. Exception Class Methods Method Description final public function getMessage() message of exception final public function getCode() code of exception final public function getFile() source filename final public function getLine() source line final public ...
Read MorePHP declare Statement
The PHP declare statement allows you to control the execution environment of a script block. Its syntax is similar to other control structures like while, for, and foreach loops. Syntax declare (directive) { statement1; statement2; // ... } The behavior of the block is defined by the type of directive. Three types of directives can be used: ticks, encoding, and strict_types. Ticks Directive A tick is a special event that occurs after a certain number of tickable statements are executed. You ...
Read MorePHP program to find the maximum element in an array
In PHP, finding the maximum element in an array can be accomplished using a custom function that iterates through the array or by using PHP's built-in max() function. Here are different approaches to find the maximum value. Using Custom Function We can create a custom function that compares each element to find the maximum value ? The highest value of the array is 91 Using Built-in max() Function PHP provides the max() function that directly returns the highest value from an array ? ...
Read MorePHP program to check if a given number is present in an infinite series or not
To check if a given number is present in an infinite arithmetic series, we need to determine if the number can be reached by starting from a base value and adding a common difference repeatedly. The PHP code below demonstrates this logic − Example Output The number is not present in the infinite series How It Works The function contains_val takes three parameters − $start − The starting number of the series $target − The number we want to check $diff − The common difference in the ...
Read More