"Global" and "Local" Variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

"Global" and "Local" Variables

What’s the defferent between Global variables and Local ones? I heard that using Local variables is faster than using Global ones. Places they are saved are different?

27th Feb 2019, 5:45 AM
stilts
stilts - avatar
11 Answers
+ 21
A global variable is avaliable throughout the whole code. A local variable is only avaliable in the function that it is in.
27th Feb 2019, 5:56 AM
Rowsej
Rowsej - avatar
+ 5
In "func" block, if it outputs a ,then "local" is displayed, right?
27th Feb 2019, 2:04 PM
stilts
stilts - avatar
+ 5
Marat Khudaiberdin did you mean var a instead of car a?
28th Feb 2019, 1:24 AM
Sonic
Sonic - avatar
+ 4
OK. I see. I’ ll be careful about the lifespans of variables !
27th Feb 2019, 6:09 AM
stilts
stilts - avatar
+ 4
A global variable is accessible from anywhere in the code (in any blocks). And the local variable has a local scope, means it is only accessible in the code block where it is declared. But languages has different methods to declare a global variable from a local scope.
28th Feb 2019, 10:18 AM
Rakibul Yeasin
Rakibul Yeasin - avatar
+ 4
This code from a C quiz question is a good example of global and local variables both of the same name i https://code.sololearn.com/cp4Z6UtRgH2z/?ref=app
9th Mar 2019, 6:01 AM
boneSpider
boneSpider - avatar
+ 3
Oh, PHP has such a defferent rule? There are various features among the languages...
28th Feb 2019, 10:12 AM
stilts
stilts - avatar
+ 3
Wow! I didn’t know that.
28th Feb 2019, 12:29 PM
stilts
stilts - avatar
+ 2
You can cite to global variables anywhere in your code (except for PHP)
28th Feb 2019, 9:29 AM
Luka Timofeev
Luka Timofeev - avatar
+ 1
One doubt. Should we have to save a file called someexample.php...what should that file contain
2nd Mar 2019, 12:15 PM
Akash. V
Akash. V - avatar
+ 1
Global belongs to whole code but local is belongs toy present In the {} function
21st Sep 2019, 7:08 AM
Swaminathan Venkataraman
Swaminathan Venkataraman - avatar