PHP - why I can't get the variable $name ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

PHP - why I can't get the variable $name ?

Hello everyone :) In the course "variable scope", there is this code (posted bellow) but I don't understand why the course say that $name is not accesible in the function if it's a global variable (this is exactly what the course say : "This script will produce an error, as the $name variable has a global scope, and is not accessible within the getName() function.") Because if I right understand global variable, it's can be accesed from everywhere, not only in a specific function (?). <?php $name = 'David'; function getName() { echo $name; } getName(); // Error: Undefined variable: name ?>

10th Jan 2019, 4:53 PM
Ophélie De Sousa Oliveira
Ophélie De Sousa Oliveira - avatar
10 Answers
+ 9
You can assign it into the global array $GLOBALS["name"] = "David"; Or use global keyword inside the function before using it $name = 'David'; function getName() { global $name; echo $name; }
10th Jan 2019, 5:09 PM
Toni Isotalo
Toni Isotalo - avatar
+ 5
Thank's for you answer ! So, by default the function is "local" in a specific variable. But, if I precise "global" in front of $name I could access to it everywhere (even in my variable) ?
10th Jan 2019, 5:21 PM
Ophélie De Sousa Oliveira
Ophélie De Sousa Oliveira - avatar
+ 3
In function instead of calling as $name call as $GLOBALS['name'];
10th Jan 2019, 5:11 PM
Adarsh.n. Bidari
Adarsh.n. Bidari - avatar
+ 2
it will be better if you use OOP programming class User{ private $name; public function setName($name){ $this->name = $name; } public function getName(){ return $this->name; } } //using class $user = new User; $user->setName('David'); echo $user->getName();
11th Jan 2019, 6:56 AM
Ikrom Jalolov
Ikrom Jalolov - avatar
+ 2
thank you
12th Jan 2019, 4:38 PM
Mohammad Elahi
Mohammad Elahi - avatar
+ 1
To use a variable in PHP you put it as $GLOBALS[“name”] = “David”;
12th Jan 2019, 8:59 AM
Musa Iman yahaya
Musa Iman yahaya  - avatar
+ 1
Donald Faulknor $name in this example is a global variable. It's in the $GLOBALS array just as $_POST, $_SERVER, and the other superglobals are in the $GLOBALS array. Take a look at this code to see this more clearly. https://code.sololearn.com/wnj8d35bQJJM/?ref=app
21st Jan 2019, 9:30 AM
David Carroll
David Carroll - avatar
0
$name has to be defined within the function to use it in the function. On another note $name is not a global variable. $_POST is an example of a global variable. You might be confusing global variable with prefefined variable or reserved keyword; however, $name is neither. $name is free to use however you want withing the rules of variables.
20th Jan 2019, 8:54 PM
Donald Faulknor
Donald Faulknor - avatar
0
you most write:$this->getName(); and this variable defind $global
25th Jan 2019, 1:07 AM
reza ashrafi
reza ashrafi - avatar
0
no,int bihaend
1st Feb 2019, 5:04 AM
Кирилл