PHP Variable Scope - PHP Tutorial 61

Publicado el: 07 noviembre 2019
en el canal de: 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


En esta página del sitio puede ver el video en línea PHP Variable Scope - PHP Tutorial 61 de Duración hora minuto segunda en buena calidad , que subió el usuario ChidresTechTutorials 07 noviembre 2019, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 2,247 veces y le gustó 45 a los espectadores. Disfruta viendo!