Description:
$_SESSION variable is used to store and retrieve session variables.
Example:
Code:
session_start(); // without store session data $_SESSION['views']=1; //retrieve session data echo "views=". $_SESSION['views'];
Output:
views=1
Description:
$_SESSION variable is used to store and retrieve session variables.
Example:
Code:
session_start(); // without store session data $_SESSION['views']=1; //retrieve session data echo "views=". $_SESSION['views'];
Output:
views=1
Syntax: $_REQUEST
Description:
The PHP built-in $_REQUEST function can be used with both the GET and POST methods.
Example:
Code:
Explanation: HTML code.
Example:
Code:
echo $_REQUEST["fname"]; echo$_REQUEST["age"];
Output:
Watson
29.
Syntax: $_FILES
Files can be uploaded from a browser to the server with forms using the “file” input type.
Example:
Code:
Explanation: HTML code for file.
Example:
Code:
print_r($_FILES);
Output:
Array (
[file] => Array (
[name] => Desert.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\phpD31E.tmp
[error] => 0
[size] => 845941 ) )
Explanation: It gives information about uploaded files.
Syntax: $_POST
Description:
The function is used to collect values from a form sent with method=”post”.
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
This is a ‘super global’, or automatic global, variable.It means that it is available in all scopes throughout a script. You don’t need to do a global $_POST; to access it within functions or methods.
Example:
Code:
Explanation: HTML code.
Example:
Code:
echo $_POST["fname"]; echo $_POST["age"];
Explanation: PHP code.
Output:
Sachin!
36.
Syntax: $_GET
Description:
The $_GET function is used to collect values in a form with method=”get”.
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser’s address bar) and has limits on the amount of information to send.
This is a script. You don’t need to do a global $_GET; to access it within functions or methods.
Example:
Code:
Explanation: HTML code.
Example:
Code:
echo $_GET["fname"]; echo $_GET["age"];
Explanation: PHP code
Output:
Warne!
40 years old.
This is a ‘superglobal’, or automatic global, variable. It means that it is available in all scopes throughout a script. There is no need to do global $variable to access it within functions or methods.
Example:
Code:
$_ENV [ 'mystring'] = 'Hello World' ;
$_ENV [ 'myarray' ] = array( 'Nokia' , 'Samsung' , 'Blackberry' );
function check () {
print $_ENV [ 'mystring' ];
print_r ( $_ENV [ 'myarray' ]);
}
check ();
Output:
Hello World
Array
(
[0] => Nokia
[1] => Samsung
[2] => Blackberry
)
This is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these.
Description:
$_SERVER['PHP_SELF']: The filename of the currently executing script, relative to the document root.
$_SERVER['argv']: Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string.
$_SERVER['argc']: Contains the number of command line parameters passed to the script if run on the command line.
$_SERVER['GATEWAY_INTERFACE']: What revision of the CGI specification the server is using; i.e. ‘CGI/1.1′.
$_SERVER['SERVER_ADDR']:The IP address of the server under which the current script is executing.
$_SERVER['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['SERVER_SOFTWARE']:Server identification string, given in the headers when responding to requests.
$_SERVER['SERVER_PROTOCOL']:Name and revision of the information protocol via which the page was requested; i.e. ‘HTTP/1.0′;
$_SERVER['REQUEST_METHOD']:Which request method was used to access the page; i.e. ‘GET’, ‘HEAD’, ‘POST’, ‘PUT’.
$_SERVER['REQUEST_TIME']:The timestamp of the start of the request. Available since PHP 5.1.0.
$_SERVER['QUERY_STRING']:The query string, if any, via which the page was accessed.
$_SERVER['DOCUMENT_ROOT']:The document root directory under which the current script is executing, as defined in the server’s configuration file.
$_SERVER['HTTP_ACCEPT']:Contents of the Accept: header from the current request, if there is one.
$_SERVER['HTTP_ACCEPT_CHARSET']:Contents of the Accept-Charset: header from the current request, if there is one. Example: ‘iso-8859-1,*,utf-8′.
$_SERVER['HTTP_ACCEPT_ENCODING']:Contents of the Accept-Encoding: header from the current request, if there is one. Example: ‘gzip’.
$_SERVER['HTTP_ACCEPT_LANGUAGE']:Contents of the Accept-Language: header from the current request, if there is one. Example: ‘en’.
$_SERVER['HTTP_CONNECTION']:Contents of the Connection: header from the current request, if there is one. Example: ‘Keep-Alive’.
$_SERVER['HTTP_HOST']:Contents of the Host: header from the current request, if there is one.
$_SERVER['HTTP_REFERER']:The address of the page (if any) which referred the user agent to the current page.
$_SERVER['HTTP_USER_AGENT']:This is a string denoting the user agent being which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586).
$_SERVER['HTTPS']:Set to a non-empty value if the script was queried through the HTTPS protocol.
$_SERVER['REMOTE_ADDR']:The IP address from which the user is viewing the current page.
$_SERVER['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.
$_SERVER['REMOTE_PORT']:The port being used on the user’s machine to communicate with the web server.
$_SERVER['SCRIPT_FILENAME']:The absolute pathname of the currently executing script.
$_SERVER['SERVER_ADMIN']:The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file.
$_SERVER['SERVER_PORT']:The port on the server machine being used by the web server for communication. For default setups, this will be ’80′.
$_SERVER['SERVER_SIGNATURE']:String containing the server version and virtual host name which are added to server-generated pages, if enabled.
$_SERVER['PATH_TRANSLATED']:Filesystem based path to the current script.
$_SERVER['SCRIPT_NAME']:Contains the current script’s path. This is useful for pages which need to point to themselves.
$_SERVER['REQUEST_URI']:The URI which was given in order to access this page; for instance, ‘/index.html’.
$_SERVER['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.
$_SERVER['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.
$_SERVER['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.
$_SERVER['AUTH_TYPE']:When running under Apache as module doing HTTP authenticated this variable is set to the authentication type.
Example:
Code:
echo $_SERVER['SERVER_NAME'];
Output:
localhost
$GLOBALS: It contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables.
Syntax: $GLOBALS
Example1:
Code:
function check() {
$scope = "local variable";
echo '$scope in global scope: ' . $GLOBALS["scope"] . "\n";
echo '$scope in current scope: ' .$scope . "\n";
}
$scope = "Global variable";
check();
Output:
$scope in global scope: Global variable
$scope in current scope: local variable
Example2:
Code:
$x = 10;
$y = 5;
function change()
{
$x += 5;
}
function change_global()
{
global $y;
$y += 5;
}
change();
change_global();
print "x: $x ";
print "y: $y ";
Output:
x: 10
y: 10
Explanation:
The function change() cannot access the variable x, because it is not in the global scope.
Definition of a Constructor
A constructor is a special function of a class that is automatically executed whenever an object of a class gets instantiated.
A constructor is a special function – this means that a constructor is a function; but its special. But, why is it special? It’s special because it is automatically executed or called when an object of a class is created.
PHP5 will first search for __construct() method and execute it if available, otherwise it will execute the same class name function.
Example:
Code:
class Student {
var $first_name;
var $last_name;
public function __construct() {
echo $first_name = "Tom"."
";
echo $last_name = "Cruise"."
";
}
public function setData($first_name, $last_name) {
$this->first_name = $first_name;
$this->last_name = $last_name;
}
public function getData() {
echo $this->first_name."
";
echo $this->last_name;
}
}
$c1 = new Student();
$c1->setData("John","Abraham");
$c1->getData();
Output:
Tom
Cruise
John
Abraham
Explanation:
In the above example , we create a new object of the Student class. the ‘new’ operator is responsible for creating the Student class. At this point PHP5 searches the Student class to see if a constructor has been defined. Therefore, it calls the constructor method i.e. __construct(). The __construct() method sets the $first_name and $last_name to Tom and Cruise.
An attribute is also know as data members and is used to hold data of a class.
Example:
Code:
class Person {
var $first_name;
var $last_name;
}
$person1=new Student();
echo $person1->first_name="Andy";
echo $person1->last_name="Flower";
Output:
Andy
Flower
Explanation:
In the above example $first_name, $last_name are data members of class Person. These data members define the data that one Person Object will store.
An object is an
instance of a class. This means that an object is created from the definition of the class and is loaded in memory.
Example:
Code:
name=$value;
}
function get_name()
{
echo $this->name;
}
}
$student1=new student();
$student1->set_name("Rahul");
$student1->get_name();
Example:
Rahul
Explanation:
Here $student1 is an instance of a class.
A class is user defined data type that contains attributes or data members; and methods which work on the data members.
To create a class, you need to use the keyword class followed by the name of the class. The name of the class should be meaningful to exist within the system.The body of the class is placed between two curly brackets within which you declare class data members/variables and class methods.
Example:
Code:
class student
{
var $name;
function set_name($value)
{
$this->name=$value;
}
function get_name()
{
echo $this->name;
}
}
$student1=new student();
$student1->set_name("Rahul");
$student1->get_name();
Output:
Rahul
Explanation:
Here the name of class is “student” and $student1 is instance of a class student.we make the instance of a class using “new” keyword with class name.$this is a reference to the calling object.
In programming it is a very common task to have how to increment a variable by some fixed amount. The most common example of this is a counter.Say you want to increment a counter by 1, you would have:
* $counter = $counter + 1;
However, there is a shorthand for doing this.
* $counter += 1;
This combination assignment/arithmetic operator would accomplish the same task. In general, “+=” and “-=” are the most widely used combination operators.
+= Plus Equals
-= Minus Equals
*= Multiply Equals
/= Divide Equals
%= Modulo Equals
.= Concatenate Equals
$a=5;
echo $a+=5;
The following are Logical Operators:
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE,but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.
Example1:
Code:
$a=5;
$b=7;
if($a and $b ==5)
{
echo "Right Input"."
";
}
else
{
echo "wrong Input"."
";
}
Output:
wrong Input
Example2:
Code:
$a=5;
$b=7;
if($a or $b ==5)
{
echo "Right Input"."
";
}
else
{
echo "wrong Input"."
";
}
Output:
Right Input
Example3:
Code:
$a=5;
$b=7;
if(!($a==$b))
{
echo "Right";
}
else
{
echo "wrong";
}
Output:
Right
$a + $b Union Union of $a and $b.
$a == $b Equality TRUE if $a and $b have the same key/value pairs.
$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
$a != $b Inequality TRUE if $a is not equal to $b.
$a <> $b Inequality TRUE if $a is not equal to $b.
$a !== $b Non-identity TRUE if $a is not identical to $b.
The + operator appends the right handed array to the left handed, whereas duplicated keys are NOT overwritten.
Example:
Code:
$p = array("a" => "Tom", "b" => "Jerry");
$q = array("a" => "Akon", "b" => "Abhay", "c" => "Mahnama");
$r = $p + $q; // Union of $p and $q
print_r ($r);
$r = $q + $p; // Union of $q and $p
print_r($r);
Output:
Array ( [a] => Tom [b] => Jerry [c] => Mahnama )
Array ( [a] => Akon [b] => Abhay [c] => Mahnama )
The following are Incrementing/Decrementing Operators:
++$x Pre-increment Increments $x by one, then returns $x.
$x++ Post-increment Returns $x, then increments $x by one.
–$x Pre-decrement Decrements $x by one, then returns $x.
$x– Post-decrement Returns $x, then decrements $x by one.
Example1:
Code:
echo "postincrement"; $a = 5; echo $a++ . "
"; echo $a . "
";
Output:
postincrement
5
6
Example2:
Code:
echo "preincrement"; $a = 5; echo ++$a . ""; echo $a . "";
preincrement
6
6
Example3:
Code:
echo "Postdecrement"; $a = 5; echo $a-- . ""; echo $a . "";
Postdecrement
5
4
Example4:
Code:
echo "Predecrement"; $a = 5; echo --$a . ""; echo $a . "";
Output:
Predecrement
4
4
The period “.” is used to add two strings together, or more technically, the period is the concatenation operator < for strings.
Example:
Code:
$a = "Hello "; echo $b = $a . "World!"."
"; // now $b contains "Hello World!" $a = "Hello "; echo $a .= "World!"; // now $a contains "Hello World!"
Output:
Hello World!
Hello World!
Comparisons are used to check the relationship between variables and/or values. Comparison operators are used inside conditional statements and evaluate to either true or false. The most important comparison operators of PHP are:
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.
Example1:
Code:
$a=5;
$b=6;
if($a==$b)
{
echo "Right";
}
else
{
echo "wrong";
}
Output:
wrong
Example2:
Code:
$a=5;
$b="5";
if($a===$b)
{
echo "Both are same type";
}
else
{
echo "Both are not same type";
}
Output:
Both are not same type
Example3:
Code:
$a=5;
$b=6;
if($a!=$b)
{
echo "Right";
}
else
{
echo "wrong";
}
Output:
Right
Example4:
Code:
$a=9;
$b=6;
if($a>$b)
{
echo "Right";
}
else
{
echo "wrong";
}
Output:
Right
The Arithmetic Operators are:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
The division operator (“/”) returns a float value anytime,even if the two operands are integers (or strings that get converted
Example:
Code:
$a=7; $b=5; echo $c=$a+$b."
"; echo $d=$a-$b."
"; echo $e=$a*$b."
"; echo $f=$a/$b."
"; echo $g=$a%$b;
Output:
12
2
35
1.4
2
Assignment operators are used to assign a variable equal to a value or set a variable to another variable’s value. Such an assignment of value is done with the “=”, or equal character.
Example:
Code:
$a = 8; $b = $a; echo $a."
"; echo $b;
Output:
8
8
Explanation:
Both $a and $b contain the value 8.
© PHPInterviewQuestion.com 2009 - 2012