$_SERVER – Super Global Variable in PHP
$_SERVER is an array containing information such as headers, paths, and script locations. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.
‘PHP_SELF‘ – The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar.
‘GATEWAY_INTERFACE‘ – What revision of the CGI specification the server is using; i.e. ‘CGI/1.1‘.
‘SERVER_ADDR‘ – The IP address of the server under which the current script is executing.
‘SERVER_NAME‘ – The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.
‘SERVER_SOFTWARE‘ – Server identification string, given in the headers when responding to requests.
‘SERVER_PROTOCOL‘ – Name and revision of the information protocol via which the page was requested; i.e. ‘HTTP/1.0‘;
‘REQUEST_METHOD‘ – Which request method was used to access the page; i.e. ‘GET‘, ‘HEAD‘, ‘POST‘, ‘PUT‘.
‘REQUEST_TIME‘ – The timestamp of the start of the request. Available since PHP 5.1.0.
‘QUERY_STRING‘ – The query string, if any, via which the page was accessed.
‘DOCUMENT_ROOT‘ – The document root directory under which the current script is executing, as defined in the server’s configuration file.
‘HTTP_ACCEPT‘ – Contents of the Accept: header from the current request, if there is one.
‘HTTP_ACCEPT_CHARSET‘ – Contents of the Accept-Charset: header from the current request, if there is one. Example: ‘iso-8859-1,*,utf-8‘.
‘HTTP_ACCEPT_ENCODING‘ – Contents of the Accept-Encoding: header from the current request, if there is one. Example: ‘gzip‘.
‘HTTP_CONNECTION‘ – Contents of the Connection: header from the current request, if there is one. Example: ‘Keep-Alive‘.
‘HTTP_HOST‘ – Contents of the Host: header from the current request, if there is one.
‘HTTP_REFERER‘ – The address of the page (if any) which referred the user agent to the current page.
‘HTTP_USER_AGENT‘ – Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page.
‘HTTPS‘
Set to a non-empty value if the script was queried through the HTTPS protocol.
‘REMOTE_ADDR‘ – The IP address from which the user is viewing the current page.
‘REMOTE_HOST‘ – The Host name from which the user is viewing the current page. The reverse dns lookup is based off the REMOTE_ADDR of the user.
‘REMOTE_PORT‘ – The port being used on the user’s machine to communicate with the web server.
‘SCRIPT_FILENAME‘ – The absolute pathname of the currently executing script.
‘SERVER_ADMIN‘ – The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host.
‘SERVER_PORT‘ – The port on the server machine being used by the web server for communication. For default setups, this will be ‘80‘; using SSL, for instance, will change this to whatever your defined secure HTTP port is.
‘SERVER_SIGNATURE‘ – String containing the server version and virtual host name which are added to server-generated pages, if enabled.
‘SCRIPT_NAME‘ – Contains the current script’s path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.
‘REQUEST_URI‘ – The URI which was given in order to access this page; for instance, ‘/index.html‘.
‘PHP_AUTH_DIGEST‘ – When running under Apache as module doing Digest HTTP authentication this variable is set to the ‘Authorization’ header sent by the client (which you should then use to make the appropriate validation).
‘PHP_AUTH_USER‘ – When running under Apache or IIS (ISAPI on PHP 5) as module doing HTTP authentication this variable is set to the username provided by the user.
‘PHP_AUTH_PW‘ – When running under Apache or IIS (ISAPI on PHP 5) as module doing HTTP authentication this variable is set to the password provided by the user.
‘AUTH_TYPE‘ – When running under Apache as module doing HTTP authenticated this variable is set to the authentication type.
‘PATH_INFO‘ – Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.
Categories: Array, PHP - Hypertext Preprocessor Tags: array in php, PHP5, Variable in PHP
Superglobals Variable in PHP
Several predefined variables in PHP are “superglobals”, which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.
These superglobal variables are:
- $GLOBALS
- $_SERVER
- $_GET
- $_POST
- $_FILES
- $_COOKIE
- $_SESSION
- $_REQUEST
- $_ENV
Categories: PHP - Hypertext Preprocessor Tags: data type in PHP, Oops in PHP, PHP data type, PHP5, Variable in PHP
Object Oriented Programmings (OOPs) Concept in PHP5
Classes begin with the keyword ‘class’, followed by the class name which the user may choose. The keyword ‘new’ used to create object from a class.
Inside the scope of a current class context, it is possible to create a objects by using keywords ‘self’ and ‘parent’;
eg: new self; new parent;
A class can inherit methods and members of another class by using the keyword ‘extends’;
eg: class inheritedclass extends parentclassname{ }
Any inherited methods and members of the parent class can be overridden, unless the parent class has defined a method by using the keyword ‘final’, by re declaring those methods or members with the same name defined in the parent class.
Visibility of Class member
The visibility of a member property or method of a class can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.
Parent and child class can have a private set of private variables that no-one else has access to, other than the class itself even it was inherited. Which means private members of a class can’t be overridden.
Class constants
keyword used to declare a constant is ‘const’. Constants differ from normal variables in that you don’t use the ‘$’ symbol to declare or use them. The value must be a constant expression, and should not be a variable, a class member, result of a mathematical operation or a function call.
Scope resolution operator (::)
The Scope Resolution Operator is a token that allows access to static, constant, and other members or methods of a class. Two special keywords self and parent are used to access members or methods from inside the class definition. Call it by the name of the class, when referencing these from outside the class.
Constructors and Destructors
Constructors
Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
Destructors
The destructor method will be called automatically when as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.
parent destructors will not be called implicitly/ automatically by the engine. we need call manually by calling parent::__destruct() in the destructor body.
Autoloading Classes
In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
The function __autoload gets the class name whenever any object gets created. so we can include the class definition for the particular class. So the main usage is to avoid include lot of class file in every page.
Static Keyword
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
Static properties cannot be accessed through the object using the arrow operator ->.
Class Abstraction
When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
It is not allowed to create an instance from a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method’s signature they cannot define the implementation.
when overridding an abstarce method or abstarct member property, the visibility would be less restrictive or same; For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
An abstract class that extends another abstract class doesn’t need to define the abstract methods from the parent class.
Overloading
PHP’s interpretation of “overloading” is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.
Overloading in PHP provides means to dynamically “create” properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms “inaccessible properties” and “inaccessible methods” to refer to this combination of declaration and visibility.
All overloading methods must be defined as public.
Member overloading:
void __set ( string $name , mixed $value ) – is run when writing data to inaccessible members.
mixed __get ( string $name ) – is utilized for reading data from inaccessible members
bool __isset ( string $name ) – is triggered by calling isset() or empty() on inaccessible members.
void __unset ( string $name ) – is invoked when unset() is used on inaccessible members.
Method overloading
mixed __call ( string $name , array $arguments ) – is triggered when invoking inaccessible methods in an object context.
mixed __callStatic ( string $name , array $arguments ) – is triggered when invoking inaccessible methods in a static context.
Object Iteration
PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.
object’s elements can be viewed as array format by using the print_r() function.
Magic Methods
NOTE ON MAGIC METHODS:
- Magic methods are the members functions that is available to all the instance of class
- Magic methods always starts with “__” (double underscore). Eg. __construct
- All magic methods needs to be declared as public
- To use magic method they should be defined within the class or program scope
Various Magic Methods used in PHP 5 are:
- __construct()
- __destruct()
- __set()
- __get()
- __call()
- __toString()
- __sleep()
- __wakeup()
- __isset()
- __unset()
- __autoload()
- __clone()
Categories: OOPs in PHP, PHP - Hypertext Preprocessor Tags: class in PHP5, Oops in PHP, PHP5
Class constants in PHP
keyword used to declare a constant is ‘const’. Constants differ from normal variables in that you don’t use the ‘$’ symbol to declare or use them. The value must be a constant expression, and should not be a variable, a class member, result of a mathematical operation or a function call.
Example #1:
<?php
class MyClass
{
const constant = ‘constant value’;
function showConstant() {
echo self::constant . “\n”;
}
}
echo MyClass::constant . “\n”;
$classname = “MyClass”;
echo $classname::constant . “\n”; // As of PHP 5.3.0
$class = new MyClass();
$class->showConstant();
echo $class::constant.“\n”; // As of PHP 5.3.0
?>
Categories: OOPs in PHP, PHP5 Tags: class in PHP5, Oops in PHP, PHP5
visibility of class member in PHP5
The visibility of a member property or method of a class can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.
Parent and child class can have a private set of private variables that no-one else has access to, other than the class itself even it was inherited. Which means private members of a class can’t be overridden.
Categories: OOPs in PHP Tags: class in PHP5, Oops in PHP, PHP5
Classes and Objects in PHP 5
Classes begin with the keyword ‘class’, followed by the class name which the user may choose. The keyword ‘new’ used to create object from a class.
Inside the scope of a current class context, it is possible to create a objects by using keywords ‘self’ and ‘parent’;
eg: new self; new parent;
A class can inherit methods and members of another class by using the keyword ‘extends’;
eg: class inheritedclass extends parentclassname{ }
Any inherited methods and members of the parent class can be overridden, unless the parent class has defined a method by using the keyword ‘final’, by re declaring those methods or members with the same name defined in the parent class.
Categories: PHP - Hypertext Preprocessor Tags: Oops in PHP, PHP5
Associative Arrays
Associative Arrays
In the products array, we allowed PHP to give each item the default index. This meant that the first item we added became item 0, the second item 1, and so on. PHP also supports associative arrays. In an associative array, we can associate any key or index we want with each value.
Initializing an Associative Array
The following code creates an associative array with product names as keys and prices as values.
$prices = array( 'Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 );
Categories: Array Tags: Associative Arrays, Initializing an Associative Array
Arrays in PHP
A standard datatype which can hold one or more than one value.
Two Type of Array
1. Indexed Array
- Single Dimentional
- Multi Dimentional
2. Associative Array
- Single Dimentional
- Multi Dimentional
Categories: Array, PHP - Hypertext Preprocessor Tags: array in php
Function In PHP
There are two types of function
1. User Defined Function
- Function without arguments
- Function with arguments
- Function with default arguments
- Function call by value
- Function call by reference
2. Pre Defined Function
Categories: function, PHP - Hypertext Preprocessor Tags: function in php, php function
Operator in PHP
- Assignment Operator
- Arithmetic Operator
- Comparison Operator Ex :
==, !=, >=, <=, <, >
===, !== - Conditional Operator
Ex: &&, ||, ! - Intertical Operator
- Bitwise Operator
Eg. & (And), | (Or), ^ (Xor), << (Left Shift), >> (Right Shift) - Increament / Decreament
- Ternary Operator
Categories: Operator, PHP - Hypertext Preprocessor Tags: operator in php

.jpg)