Found 1060 Articles for PHP

PHP Generator class

Malhar Lathkar
Updated on 21-Sep-2020 11:45:54

767 Views

IntroductionTraversing a big collection of data using looping construct 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 and pauses ... Read More

PHP Closure class

Malhar Lathkar
Updated on 21-Sep-2020 11:43:49

1K+ Views

IntroductionAnonymous functions (also called lambda) return object of Closure class. This class has some additional methods that provide further control over anonymous functions.SyntaxClosure {    /* Methods */    private __construct ( void )    public static bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) : Closure    public bindTo ( object $newthis [, mixed $newscope = "static" ] ) : Closure    public call ( object $newthis [, mixed $... ] ) : mixed    public static fromCallable ( callable $callable ) : Closure }Methodsprivate Closure::__construct ( void ) — This method exists ... Read More

PHP ArrayAcccess interface

Malhar Lathkar
Updated on 21-Sep-2020 11:42:01

261 Views

IntroductionIn PHP, ArrayAccess interface is used to develop a class that provides array like access to one of properties which is an array. Such array property may be manipulated without exposing it during object creation. ArrayAccess interface defines following abstract methodsSyntaxArrayAccess {    /* Methods */    abstract public offsetExists ( mixed $offset ) : bool    abstract public offsetGet ( mixed $offset ) : mixed    abstract public offsetSet ( mixed $offset , mixed $value ) : void    abstract public offsetUnset ( mixed $offset ) : void }MethodsArrayAccess::offsetExists − Whether an offset existsArrayAccess::offsetGet − Offset to retrieveArrayAccess::offsetSet − Assign ... Read More

PHP ArgumentCountError

Malhar Lathkar
Updated on 21-Sep-2020 11:37:53

530 Views

IntroductionPHP parser throws ArgumentCountError when arguments passed to a user defined function or method are less than those in its definition. ArgumentCountError class is inherited from TypeError classArgumentCountError ExampleIn Following example, a user defined function add() is defined to receive two arguments. However, if less than required number of arguments is provided while calling, ArgumentCountError will be thrown which can be handled with catch block.Example Live DemoOutputThis will produce following result −Too few arguments to function add(), 1 passed in C:\xampp\php\test.php on line 6 and exactly 2 expectedIn Following example, setdata() method in myclass is defined to have two formal arguments. ... Read More

PHP ArithmeticError

Malhar Lathkar
Updated on 21-Sep-2020 11:34:49

281 Views

IntroductionArithmeticError class is inherited from Error class. This type of error may occurwhile performing certain mathemetical operations. One such scenario is attempt to perform bitwise shift operation by negative amount. This error is also thrown when call to intdiv() function results in value such that it is beyond legitimate boundaries of integer.ArithmeticError ExampleIn Following example, an attempt is made to use binary shift operator with negative operand. This results in ArithmeticError.Example Live DemoOutputThis will produce following result −Bit shift by negative numberIf call to intdiv() function results in invalid integer, ArithmeticError is thrown. As shown in the example below, the minimum allowed integer ... Read More

PHP AssertionError

Malhar Lathkar
Updated on 21-Sep-2020 11:32:36

292 Views

IntroductionAssertionError class is a subclass of Error class. This type of error is thrown when assert() returns FALSEassert() checks if given assertion is true or false, and throws AssertionError if it is false. The assert() function is defined as follows −Syntaxfor PHP 5 and PHP 7 assert ( mixed $assertion [, string $description ] ) : bool PHP 7 only assert ( mixed $assertion [, Throwable $exception ] ) : boolParametersSr.NoParameter & Description1assertionstring or boolean expression2descriptionfailure mssage3exception (for PHP 7 only)throwable objectSince PHP 7.0, assert() is now a language construct and not a function. assertion parameter can now be an expression ... Read More

PHP CompileError

Malhar Lathkar
Updated on 21-Sep-2020 11:30:17

248 Views

IntroductionIn PHP 7.3 onwards, CompileError exception has been added. This class inherits Error class. Some error conditions that previously resulted in fatal error, now throw a CompileError. This affects compilation errors that are likely to be thrown by token_get_all() function.The token_get_all() function uses Zend lexical scanner to parse a given string into PHP language tokens.Syntaxtoken_get_all ( string $source [, int $flags = 0 ] ) : arrayParametersSr.NoParameter & Description1sourcePHP source to parse2flagTOKEN_PARSE - Recognises the ability to use reserved words in specific contexts.The function should be used in TOKEN_PARSE mode to be able to raise CompileError.Read More

PHP DivisionByZeroError

Malhar Lathkar
Updated on 21-Sep-2020 11:27:48

2K+ Views

IntroductionDivisionByZeroError class is a subclass of ArithmeticError class. This type of error occurs when division operation involves value of denominator as zero. This can also occur when a modulo operator (%) has 0 as second operator, and intdiv() function having second argument as 0.DivisionByZeroError ExampleIn first example, we try to perform modulo division of 10 and 0 using % operator to induce DivisionByZeroError.Example Live DemoOutputThis will produce following result −Modulo by zeroIf call to intdiv() function with 0 as second argument also raises DivisionByZeroError as followsExample Live DemoOutputThis will produce following result −Division by zeroDivision operator (/) having 0 as denominator, however fails to raise error, ... Read More

PHP ErrorException

Malhar Lathkar
Updated on 21-Sep-2020 11:25:22

442 Views

IntroductionPHP's Exception class implements the Throwable interface. ErrorException class extends the Exception class. ErrorException is meant to be explicitly thrown when you want to catch and handle errors that would otherwise be ignored, such as Notices or Warnings.PHP core consists of following predefined error constantsValueConstantDescription1E_ERRORFatal run-time errors.2E_WARNINGRun-time warnings (non-fatal errors).4E_PARSECompile-time parse errors.8E_NOTICERun-time notices.16E_CORE_ERRORFatal errors that occur during PHP's initial startup.32E_CORE_WARNINGWarnings (non-fatal errors) that occur during PHP's initial startup.64E_COMPILE_ERRORFatal compile-time errors.128E_COMPILE_WARNINGCompile-time warnings (non-fatal errors).256E_USER_ERRORUser-generated error message.512E_USER_WARNINGUser-generated warning message.1024E_USER_NOTICEUser-generated notice message.2048E_STRICTIf Enabled PHP suggests changes to your code to ensure interoperability and forward compatibility of your code.4096E_RECOVERABLE_ERRORCatchable fatal error.8192E_DEPRECATEDRun-time notices.16384E_USER_DEPRECATEDUser-generated warning message.32767E_ALLAll ... Read More

PHP ParseError

Malhar Lathkar
Updated on 21-Sep-2020 11:21:55

337 Views

IntroductionParseError class extends CompileError class. (Earlier it used to be subclass of Error class). This type of error is thrown while a PHP code inside a string that is given to eval() function as argument.The eval() function evaluates given string as PHP code.Syntaxeval ( string $code ) : mixedParametersSr.NoParameter & Description1codevalid PHP code to be evaluatedCode to be evaluated must not be embedded in PHP opening and closing tags and must be terminated by semicolon. Valide code retuns NULL whereas error in code throws ParseErrorFollowing example throws ParseError and is handled by catch blockExample Live DemoOutputThis will produce following result −Parse Error:syntax ... Read More

Advertisements