1.

How To Access A Global Variable Inside A Function?

Answer»

By DEFAULT, global variables are not ACCESSIBLE inside a function. However, you can MAKE them accessible by declare them as "global" inside a function. Here is a PHP SCRIPT on declaring "global" variables:
<?php
?>
$intRate = 5.5;
function myAccount() {
global $intRate;
print("Defined inside the function? ". isset($intRate)."\n");
}
myAccount();
print("Defined outside the function? ". isset($intRate)."\n");
?>
This script will print:
Defined inside the function? 1
Defined outside the function? 1

By default, global variables are not accessible inside a function. However, you can make them accessible by declare them as "global" inside a function. Here is a PHP script on declaring "global" variables:
<?php
?>
$intRate = 5.5;
function myAccount() {
global $intRate;
print("Defined inside the function? ". isset($intRate)."\n");
}
myAccount();
print("Defined outside the function? ". isset($intRate)."\n");
?>
This script will print:
Defined inside the function? 1
Defined outside the function? 1



Discussion

No Comment Found