6. Variable Scope

  1. File to File Variable Scope
  2. Function to function variable scope

File to file variable scope:
Have 4 Functions (include, include_once, require, require_once). Where the difference between is include & require: if you have used include function and include file path didn’t match. You will see a warning msg. but next code is executed or run. if you have used require function and require file path didn’t match. You will see an error msg. but next code is not executed or run.

Variable scope with function: If you want to access outer variable of the function. you have  to use global function.

<?php
	/*$var=10;
	function ajob(){
		global $var;
		return $var;
	}
	echo ajob();
	*/
	$a=5; // This is global variable
	function test(){
		$a =6; // This is local variable
		return $a+$GLOBALS[‘a’];
	}
	echo test();
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s