PHP Variable Scope | Local Vs Global Scope in PHP - PHP Tutorial 61

ChidresTechTutorials
ChidresTechTutorials
2.2 هزار بار بازدید - 5 سال پیش - PHP Variable Scope.- Scope indicates
PHP Variable Scope.
- Scope indicates the accessibility and visibility of variables.
- variables in PHP will be either in local scope or global scope.

Local scope:
- Any variable declared inside a function is considered as in local scope or function scope
- can be accessible only inside that function where it is declared
Note: Local scope variables will be available only in the function execution (i.e. are created as soon as the control enters the function body and deleted as soon as the control exits the function body)

Example:
function display()
{
$b=20;
echo "b= ", $b,"<br/>";  // 20
}
display();
echo "b= ", $b,"<br/>"; // error: b is not defined


Global scope:
- Any variable declared outside a function is considered as in global scope
- can be accessible anywhere in the script
Note: Global scope variables will be available throughout the script execution
- if you want to access a global scope variable inside a  local scope you must use global keyword

Example 1:
$a=10;
echo "a= ", $a, "<br/>"; // 10
function display()
{
//echo "a= ", $a,"<br/>"; // undefined error
global $a;
echo "a= ", $a,"<br/>"; // 10
}
display();


Example 2:
$a=10;
echo "a= ", $a, "<br/>"; // 10

$b = 20;
function display()
{
//echo "a= ", $a,"<br/>"; // undefined error
global $a;
echo "a= ", $a,"<br/>"; // 10

$b =30;
echo "b=", $b,"<br/>"; // 30
}
display();

echo "b=", $b,"<br/>"; // 20

Note:
- replace < with less-than symbol.
- replace > with greater-than symbol.

=========================================

Follow the link for next video:
PHP Tutorial 62 - PHP String Functions | PHP Strings Tutorial
PHP String Functions | String Functio...

Follow the link for previous video:
PHP Tutorial 60 - Types of Functions in PHP | PHP Types of Functions
PHP Types of Functions | Types of Fun...

=========================================

PHP Tutorials Playlist:-
PHP Tutorials

=========================================
Watch My Other Useful Tutorials:-

MySQL Tutorials Playlist:-
MySQL Tutorials

HTML Tutorials Playlist:-
HTML Tutorials

CSS Tutorials Playlist:-
CSS Tutorials

JavaScript Tutorials Playlist:-
JavaScript Tutorials

=========================================

► Subscribe to our YouTube channel:
chidrestechtutorials  

► Visit our Website:
https://www.chidrestechtutorials.com

=========================================
Hash Tags:-
#ChidresTechTutorials #PHP #PHPTutorial
5 سال پیش در تاریخ 1398/08/16 منتشر شده است.
2,209 بـار بازدید شده
... بیشتر