This is a discussion on How to define a global variable in PHP? within the PHP Programming forums, part of the Web Development category; Hi all... How to define a global variable in PHP? Thanks......
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi all... If you want variables defined out side the function to be accessed inside the function scope then you have make them global inside the fuction. You can use the global term. <?php $integer_var = 3; function print_var(){ global $integer_var; // echos 3 on the screen echo $integer_var; } print_var(); ?> Thanks... |
| |||
| Hi, You can use also $GLOBALS instead of global. For eg: <?php $a = 1; $b = 2; function Sum(){ $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b']; } Sum(); echo $c; ?> I think this method very useful for some times. Thanks & Regards, R.Kamalakannan. |
| |||
| Hi, The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example: First, an example use of global: <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>
__________________ Venkat knowledge is Power |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to define a static variables in php. | itbarota | PHP Programming | 1 | 11-10-2007 04:40 AM |
| Define Quality Control | itbarota | Software Testing | 2 | 07-24-2007 12:37 AM |
| Define Qos | vadivelanvaidyanathan | Server Management | 1 | 07-16-2007 09:36 AM |
| Static Global Variable and Static Local Variable | vigneshgets | C and C++ Programming | 1 | 05-30-2007 11:50 AM |
| Please Define the Terms | djree | General Web hosting Discussions | 2 | 03-13-2007 07:23 AM |