why global variables cant be accessed from within a function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

why global variables cant be accessed from within a function?

global variables can be accessed from anywhere in a programme.

8th Aug 2016, 7:56 AM
zaheer
zaheer - avatar
6 Answers
0
use global keyword before using them like <?php $x=10 //is a global variable then function func (){ global $x; //use global keyword then these var are accessible } ?>
8th Aug 2016, 4:34 PM
venky
venky - avatar
0
i know this bt y it is so in php. ....y we cant use it directly
8th Aug 2016, 4:37 PM
zaheer
zaheer - avatar
0
This maybe about scope. The $x inside the function does not know if you are talking about a local variable $x or global variable $x. So first it tries to search $x locally but there is no $x locally, hence an error occurs
14th Aug 2016, 3:59 PM
Aakash Vyas
Aakash Vyas - avatar
- 1
what i didn't get u?
8th Aug 2016, 5:21 PM
venky
venky - avatar
- 1
why we have to use global keyword....cant we use global variables directly
8th Aug 2016, 5:47 PM
zaheer
zaheer - avatar
- 1
I'm not now
21st Aug 2016, 9:55 AM
Hemid Qarayev
Hemid Qarayev - avatar