Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Notice: Undefined variable [duplicate]

Tags:

variables

php

I have a php file:

<?php
$a = 1;
function test(){ 
    echo $a;
} 
test();
?>

And I get this error:

Notice: Undefined variable: a in X:\...\test.php on line 4

Using XAMPP @ 32bit W7.

like image 537
enloz Avatar asked Mar 29 '26 02:03

enloz


1 Answers

Variables have function scope. $a inside the function is not the same as $a outside the function. Inside the function you have not defined a variable $a, so it doesn't exist. Pass it into the function:

$a = 1;
function test($a) { 
    echo $a;
} 
test($a);
like image 116
deceze Avatar answered Apr 02 '26 07:04

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!