Found 1060 Articles for PHP

PHP $_FILES

Malhar Lathkar
Updated on 21-Sep-2020 12:14:49

26K+ Views

IntroductionThe global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.$HTTP_POST_FILES also contains the same information, but is not a superglobal, and now been deprecatedThe _FILES array contains following properties −$_FILES['file']['name'] - The original name of the file to be uploaded.$_FILES['file']['type'] - The mime type of the file.$_FILES['file']['size'] - The size, in bytes, of the uploaded file.$_FILES['file']['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.$_FILES['file']['error'] - The error code associated with this file upload.Following ... Read More

PHP $_ENV

Malhar Lathkar
Updated on 21-Sep-2020 12:13:43

6K+ Views

Introduction$_ENV is another superglobal associative array in PHP. It stores environment variables available to current script. $HTTP_ENV_VARS also contains the same information, but is not a superglobal, and now been deprecated.Environment variables are imported into global namespace. Most of these variables are provided by the shell under which PHP parser is running. Hence, list of environment variables may be different on different platforms.This array also includes CGI variables in case whether PHP is running as a server module orCGI processor.PHP library has getenv()function to retrieve list of all environment variables or value of a specific environment variablegetenvFollowing script displays values ... Read More

PHP $argv

Malhar Lathkar
Updated on 21-Sep-2020 12:09:24

5K+ Views

IntroductionWhen a PHP script is run from command line, $argv superglobal array contains arguments passed to it. First element in array $argv[0] is always the name of script. This variable is not available if register_argc_argv directive in php.ini is disabled.$argvFollowing script is executed from command line.Example Live DemoOutputarray(1) {    [0]=>    string(8) "main.php" }In another example as follows, addition of command line arguments is performedExampleOutputC:\xampp\php>php test1.php 10 20 addition = 30

PHP $argc

Malhar Lathkar
Updated on 21-Sep-2020 12:08:10

585 Views

IntroductionThis superglobal variable is available when a PHP script is run from command line (and not when executed from HTTP server's document root). It is an integer that corresponds to number of command line arguments passed to current script. As script's filename has to be entered in command line, minimumn value of $argc is 1. This variable is not available if register_argc_argv directive in php.ini is disabled.$argcFollowing script is expected to be run from command line with 3 arguments including name of scriptExample Live DemoOutputThis script is run with invalid number of argumentsC:\xampp\php>php test1.php 1 2 3 invalid number of argumentsThis script ... Read More

PHP WeakReference class

Malhar Lathkar
Updated on 21-Sep-2020 12:06:26

468 Views

IntroductionWith Weak references, it is possible to retain a reference to an object which does not prevent the object from being destroyed. Implementing cache like structures can be done by Weak reference.A weak reference is similar to a normal reference, except that it doesn’t prevent the garbage collector from collecting the object. If strong references to that object are not found, it will be immediately removed frommemory. This way it is possible to implement most of the benefits of a cache, with no memory issues.WeakReference class has been introduced in PHP 7.4. Before this version, same effect used to be ... Read More

PHP Traversable interface

Malhar Lathkar
Updated on 21-Sep-2020 12:04:55

468 Views

IntroductionTraversable is an abstract interface, hence it can not be directly implemented by any class. Generally, Iterator or IteratorAggregate interfaces, which extend Traversable, are used to check if implementing class is traversable using foreach construct in PHP.Certain built-in classes that implement this interface can be used in foreach and need not implement iterator interfaces. Since Traversable is an abstract interface, it doesn't have any methods in it.SyntaxTraversable {    // }When implementing IteratorAggregate or Iterator interface which extends Traversable, they must be listed before its name in the implements clause.

PHP Throwable interface

Malhar Lathkar
Updated on 21-Sep-2020 12:02:37

520 Views

IntroductionIn PHP 7, Throwable interface acts as base for any object that can be a parameter to throw statement, including Error and Exception. Both Error and Exception classes, from which predefined and user defined error and exception classes are derived respectively, implement Throwable interface. Following abstract methods are defined in Throwable interface −SyntaxThrowable {    /* Methods */    abstract public getMessage ( void ) : string    abstract public getCode ( void ) : int    abstract public getFile ( void ) : string    abstract public getLine ( void ) : int    abstract public getTrace ( void ... Read More

PHP Serializable interface

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

611 Views

IntroductionThe Serializable interface is present in PHP library to build a class that provides custimised serialzing. PHP's serialize() function is able to serialize most of the values to a storable representation. However, objects of user defined classes can not be serialized. This interface makes it possible.SyntaxSerializable {    /* Methods */    abstract public serialize ( void ) : string    abstract public unserialize ( string $serialized ) : void }MethodsSerializable::serialize — String representation of objectSerializable::unserialize — Constructs the object from serialized string representationThe built-in serialze() function Generates a storable representation of a valueserialize ( mixed $value ) : stringunserialize() ... Read More

PHP IteratorAggregate interface

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

491 Views

IntroductionIteratorAggregate interface extends abstract Traversable interface. It is implemented by a class to create external iterator. This interface introduces on abstract method called getIterator.SyntaxIteratorAggregate extends Traversable {    /* Methods */    abstract public getIterator ( void ) : Traversable }MethodsIteratorAggregate::getIterator — Retrieve an external iteratorThis function has no parameters and returns an instance of an object implementing Iterator or Traversable.IteratorAggregate ExampleIn following PHP script, a class that implements IteratorAggregate interface contains an array as a propertyThe getIterator() method returns ArrayIterator object out of this array. We can traverse the array using foreach loop.Example Live DemoOutputtraversal of array property shows following result0=>10 1=>20 2=>30 3=>40

PHP Iterable interface

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

977 Views

IntroductionIterator interface extends abstract Traversable interface. PHP provides many built-in iterators (called SPL iterators) for many routine functionalities. Examples are ArrayIterator, DirectoryIterator etc. A user class that implements Iterator interface should implement abstract methods as defined in it.SyntaxIterator extends Traversable {    /* Methods */    abstract public current ( void ) : mixed    abstract public key ( void ) : scalar    abstract public next ( void ) : void    abstract public rewind ( void ) : void    abstract public valid ( void ) : bool }MethodsIterator::current — Return the current elementIterator::key — Return the key of ... Read More

Advertisements