VARIABLE

1.Variables in PHP are represented by a dollar sign followed by the name of the variable.

example:
  	
			<?php
			$a;
			?>
		

2.case sensitive

3.To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable).

example:
  	
			<?php
			$b=&$a;
			$a=13;
			print $b;
			?>
		
Output :
13
example:
  	
			<?php
			$a = 'b';
			$b = 'hello';
			print $$a;
			?>
		
Output :
hello

SCOPE OF VARIABLE

1.local:

Variables declared inside a function are in scope from the statement in which they are declared to the closing brace at the end of the function .This is called function scope.These variables are called local variables.

example:
  	
			<?php
			$x = 4;
			function assignx () {
			$x = 0;
			print "\$x inside function is $x.";
			}
			assignx();
			print "\$x outside of function is $x. ";
			?>
		
Output :
$x inside function is 0. $x outside of function is 4.hp

2.global:

Varaibles declared outside the function are in scope from the statement in which they are declared to the end of the file but not inside function .This is called global scope .These variables are called global variables

example:
  	
			<?php
			function a(){
				global  $fruit; 
				$fruit="Apple";
				}
				function d(){ 
				global  $fruit;
				print $fruit ;
				}
				a();
				d();
			?>
		
Output :
Apple

3.superglobal:

(i) The special superglobal variables are visible both inside and outside functions.
(ii) A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.


Static Variable:

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

example:
  	
			<?php
			function non_static_example()
					{
						$a =0;
						echo $a." ";
						$a++;
					}
					function static_example()
					{
						static $a =0;
						echo $a." ";     //   for space
						$a++;
					}
					for($i=0;$i<5;$i++)
						static_example(); 
						non_static_example();
			?>
		
Output :
0 1 2 3 4 5
0 0 0 0 0

Superglobals:

1.$_REQUEST:

This variables provided to the script via the GET, POST, and COOKIE input mechanisms

example:
  	
			<?php
					
Print_r($_REQUEST ['age']); ?>
Output :
20

2.$_GET:

This variables provided to the script via URL query string

example:
  	
			<?php
					Suppose the url to run is trainee.afixiindia.com/tapas/x.php?name=tapas
					print_r($_GET['name']);
			?>
		
Output :
tapas

3.$_POST:

This variables provided to the script via HTTP POST

example:
  	
			<?php
					
Print_r($_POST['age']); ?>
Output :
20

4.$_SESSION:

An abstract concept to represent a series of HTTP requests and responses exchanged between a specific Web browser and a specific Web server. Session concept is very useful for Web based applications to pass and share information from one Web page (request) to another Web page (request).

example:
  	
			<?php
					Print_r($_SESSION);
			?>
		

5.$_COOKIE:

A cookie is a message given to a Web browser by a Web server. The browser stores the message in a small text file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, the cookie is sent back to the server too.

example:
  	
			<?php
					Print_r($_COOKIE);
			?>
		

6.$_SERVER:

This variables set by the web server or otherwise directly related to the execution environment of the current script.

Common used ones:-
1.SERVER_NAME
2.DOCUMENT_ROOT
3.HTTP_HOST
4.HTTP_REFERER
5.REMOTE_ADDR
example:
  	
			<?php
					Print_r($_SERVER);
			?>
		

7.$_FILES:

This variables provided to the script via HTTP post file uploads.

example:
  	
			<?php
					
Print_r($_FILES); ?>

parse_ini_file ( string filename [, bool process_sections])

parse_ini_file() loads in the ini file specified in filename, and returns the settings in it in an associative array. By setting the last process_sections parameter to TRUE, you get a multidimensional array, with the section names and settings included.

[STATE]
1=orissa
2=ap
[CITY]
1=bbsr
2=ctc
Save thus file as config.ini.php
example:
  	
			<?php
					$conf = parse_ini_file("config.ini.php",true);
					print_r($GLOBALS['conf']);
			?>
		
Output :
Array ( [STATE] => Array ( [1] => orissa [2] => ap ) [CITY] => Array ( [1] => bbsr [2] => ctc ) )

Include

includes and evaluates the specified file

example:
  	
			<?php
					$color = 'green';
					$fruit = 'apple';
			?>
			Save this file as vars.php
			<?php
					echo "A $color $fruit"; // A
					include 'vars.php';
					echo "A $color $fruit";
			?>
		
Output :
A green apple

include_once

The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

require and require_once

The require() statement includes and evaluates the specific file.

require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.

Constants:

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

example:
  	
			<?php
					define("FOO","something");
					echo FOO ;
			?>
		
Output :
something

Questions

  1. What are the super global and what are there use ?
  2. I have username and password. Which are the super global variables can I use for keeping my data when i need not to be set again from one page to other ?
  3. Difference between include_once() and require_once() ?
  4. i) What will be the output ?
    <?php
    $x = 'sameer';
    $sameer = 'This must be the output $x.';
    echo $x;
    print $$x;
    echo print $$x";
    ?>
    ii)What will be the output of code snippet ?
    <?php
    $x = 'sameer';
    $sameer = 'This must be the output';
    print echo $$x;
    ?>
    iii)What will be the output of code snippet ?
    <?php
    $x = 'sameer';
    $sameer = 'This must be the output';
    print print $$x;
    ?>
    
  5. <?php
    $message = 'Afixi';
    $Afixi = 'My name is Amar Nath.';
    print $$message;
    ?>
    
    What will be the output ?
After editing Click here:
Output:
Hello World