Why is the error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the error?

<?php $name = 'David'; function getName() { echo $name; } getName(); // Error: Undefined variable: name ?>

9th Oct 2018, 5:14 PM
Mel
Mel - avatar
2 Answers
+ 2
It's because variables can have different scope. In your case in the function, variable name is not defined, here name is local function scope variable. You can see here a possible solution with global scope variables => accessible from anywhere. https://code.sololearn.com/wDf0Sxr3HGeq/?ref=app
9th Oct 2018, 6:00 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
9th Oct 2018, 5:36 PM
MO ELomari