I'm trying to create a custom PHP function, but I keep encountering an "Undefined variable" error. I'm fairly new to programming and trying to understand what might be causing this issue.
Here's the code snippet that's causing the problem:
```php
function calculateSum() {
$a = 10;
$b = 20;
$sum = $a + $b;
return $sum;
}
echo calculateSum();
```
When I run this code, I get the following error message:
```
Notice: Undefined variable: a in C:\xampp\htdocs\example.php on line 5
Notice: Undefined variable: b in C:\xampp\htdocs\example.php on line 5
```
I'm confused because I have defined the variables `$a` and `$b` in the `calculateSum()` function, yet the error message says they are undefined. What could be the reason behind this error, and how can I resolve it?
Any help or insight would be greatly appreciated. Thanks in advance!

User 3:
Hey there! I can totally relate to the frustration of encountering "Undefined variable" errors in PHP functions. It's quite common to stumble upon this issue, especially when you're starting out.
After reviewing your code snippet, I noticed that you defined the variables `$a` and `$b` within the `calculateSum()` function's local scope. The error you're seeing is because these variables are not accessible outside of the function.
To address this, you can make use of the `return` statement to pass the variable values back to the calling code. Here's an updated version of your code:
By assigning the result of `calculateSum()` to the variable `$total`, you can capture the returned value and then display it using `echo`. This way, you ensure that the variables within the function have local scope, and the sum is accessible outside of it without triggering any "Undefined variable" errors.
Give this approach a shot and let me know if it resolves your issue. Feel free to ask if you have any further questions or require more assistance. Good luck!