Learn PHP

function scope

Variables declared outside of functions and classes are global. global variables are available else where in the script.

Function variables are self-contained and do not affect variables in the main script.

Variables from the main script are not implicitly made available inside functions.

			function foo() { 
			   $bar = "studentsempire"; 
			$bar = "PHP"; 
			print $bar; 

Execution of the script starts at the $bar = "PHP" line, and then calls the foo() function.

PHP Global Variables

A global variable can be accessed anywhere in your script, whether inside or outside a function.

In PHP, all variables created outside a function are, in a sense, global in that they can be accessed by any other code in the script that's not inside a function.

To use such a variable inside a function, write the word global followed by the variable name inside the function ' s code block.

	         $myGlobal = "Hello there!"; 

	         function hello() { 
	            global $myGlobal; 
	            echo "$myGlobal\n"; 

	         hello(); // Displays "Hello there!"  

	         function setup() { 
	           global $myGlobal; 
	           $myGlobal = "Hello there!"; 

	         function hello() { 
	           global $myGlobal; 
	           echo "$myGlobal\n"; 

	         hello(); // Displays "Hello there!"   

The $GLOBALS array can access global variables within functions. All variables declared in the global scope are in the $GLOBALS array, which you can access anywhere in the script. Here is a demonstration:

			function foo() { 
			   $GLOBALS['bar'] = "studentsempire.com"; 

			$bar = "PHP"; 
			print $bar; 

We can also declare more than one global variable at once on the same line, just separate the variables using commas:

		  	function myFunction() { 
			  global $oneGlobal, $anotherGlobal; 

Be careful with global variables. If you modify the value of a global variable in many different places within your application, it can make it hard to debug your code.

PHP Tutorials






Echo and Print Statement

Data Types



Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o