CONSTANT VARIABLES
- Are identifiers containing values that do not change throughout the execution of a program
- Are case-sensitive
- Are static, meaning constants once defined cannot be changed
- Are declared using the define() function
- Are accessed from anywhere in the script
- Constant names do not need a leading dollar sign ($)
- Case insensitive in define() function
Source Code Of Constant Variable
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
//$name = "Ali";
define("name","Adil",true);
echo NAME;
// define("name","Ali");
//
// echo name;
?>
</body>
</html>
SCOPE OF VARIABLES
- Is the context within which a variable is defined
- Is the lifetime of a variable from the time the variable is created to the time the execution of the script ends
- Different scopes that a variable can have are as follows:
- Local
- Global
- Static
Local variable
- Is a variable initialized and used inside a function
- Is similar to a normal variable
- Is declared inside a function
- Its lifetime begins when the function is called, and ends when the function is completed
- You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
Source Code Of Local Variable
$var = 15;
function Show()
{
$var = 10; // local variable
echo $var;
}
function Show1()
{
$var = 20; // local variable
echo $var;
}
echo $var; // 15
echo "<br>";
Show(); // 10
echo "<br>";
Show1(); // 20
Global Variables
- Is a variable retaining its value throughout with the lifetime of a Web page
- Is declared within the function using global keyword
- Is accessed from any part of the program
- The global keyword is used to access a global variable from within a function.
Source Code Of Global Variable
$a = 10; // global variable
function Show()
{
global $a;
echo $a;
}
function Show1()
{
global $a;
echo $a;
}
Show();
echo "<br>";
Show1();
Static Variables
- Retains its value even after the function terminates
- Are only accessible from within the function they are declared and their value remains intact between function calls
- Can be initialized during declaration and the static declarations are resolved during compile time
- Are commonly used in the recursive functions.
- Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
Source Code Of Static Variable
function Show()
{
static $var = 10;
$var++;
echo $var;
}
Show(); // 11
echo "<br>";
Show();
echo "<br>";
Show();
Click Below Link to Download Notes & Source Code Of This Blog
https://www.mediafire.com/file/u6wib7h1glr9yev/Constant+&+Scope+Of+Variables.rar/file
No responses yet