PHP Variable Scope - PHP Tutorial 61

Опубликовано: 07 Ноябрь 2019
на канале: ChidresTechTutorials
2,247
45

PHP Variable Scope - PHP Tutorial 61
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 - PHP Tutorial 62  

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

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

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


На этой странице сайта вы можете посмотреть видео онлайн PHP Variable Scope - PHP Tutorial 61 длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь ChidresTechTutorials 07 Ноябрь 2019, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 2,247 раз и оно понравилось 45 зрителям. Приятного просмотра!