PHP Undefined variable

465 Views Asked by At

When I test an easy PHP program , I found that there is a strange problem. The code is in fllow:

<?php

$a = "abc";
function Test()
{
    global $a;
    $b .= $a."e";
    return $b;
}

echo Test();

As I know,it is legal.But Visual Studio Code tells me that the $b is Undefined variable. enter image description here

At the same time, the program can run successfully with the output:abce

enter image description here

Anybody can help me? I want to know the reason.

1

There are 1 best solutions below

0
On

It's because you used .= instead of just =. That means the RHS gets appended to the LHS, which means it's expected to have been set before. If you didn't expect $b to exist before, then you should have just used =.