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 by Malhar Lathkar
Page 8 of 11
PHP 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 Predefined Mathematical Constants
PHP provides a comprehensive set of predefined mathematical constants that are essential for mathematical calculations. These constants are globally available and provide precise values for common mathematical operations. Core Mathematical Constants The most commonly used mathematical constants include pi, Euler's number, and their derivatives ?
Read MorePHP tan() Function
The tan() function returns the tangent ratio of a given angle in radians. In trigonometry, tangent of an angle is defined as the ratio of lengths of opposite side and adjacent side. tan(x) = opposite/adjacent Tangent of an angle is also defined as the ratio of its sine and cosine: tan(x) = sin(x)/cos(x) If x = 45 degrees, tan(x) = 1, as in a right-angled triangle, opposite and adjacent sides are equal. This function returns a float value. Syntax tan ( float $arg ) : float Parameters ...
Read MorePHP srand() Function
The srand() function is used to seed the random number generator in PHP. Seeding initializes the random number generator with a specific starting value, ensuring reproducible or truly random sequences. While seeding is done automatically in modern PHP versions, manual seeding gives you control over randomization. Syntax srand([ int $seed ]) : void Parameters Parameter Description Required seed An integer to be used as seed. If not provided, a random number is used automatically Optional Return Value This function doesn't return any value (void). Examples ...
Read More