0

When my server updated to PHP version 5.4.24, I've encountered some errors on my script using this code

.=

sample code are

$test .= 'hello';
$test .= 'to';
$test .= 'all';
echo $test;

I got undefined variable test. Everything is working when my server PHP version is 5.2.10

Is .= still supported on PHP version 5.4.24

Is there any alternative code for this? Or how can I fix this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 3
    You would get `undefined variable test` on your 5.2.10 PHP as well, it's just that you were either not reporting errors, or not displaying them. If `error_reporting` was turned up to `E_ALL`, you would've gotten the error there too. Initialize it with `$test = ''` before appending to it with `.=`. – Michael Berkowski Feb 06 '14 at 19:32
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Michael Berkowski Feb 06 '14 at 19:33
  • Always when developing code, on any PHP version, make sure `error_reporting` is turned all the way up and you are using `display_errors` to show them on screen as you code. Fix _all_ of them, including notices and warnings. Set those in php.ini or at runtime with `error_reporting(E_ALL); ini_set('display_errors', 1);` – Michael Berkowski Feb 06 '14 at 19:35
  • @MichaelBerkowski If only WordPress plug-in authors would do this, it would make the times I work in WordPress for clients so much less painful. – Matt Feb 06 '14 at 20:10

2 Answers2

1

I think this is what you would need to do. Just use = the first time.

$test = 'hello';
$test .= 'to';
$test .= 'all';
echo $test;
Dominick
  • 448
  • 5
  • 18
0

It looks like it's related to this: PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

If you do

$test='';
$test .= 'hello';
$test .= 'to';
$test .= 'all';
echo $test;

It should solve your problem.

Community
  • 1
  • 1
TecBrat
  • 3,643
  • 3
  • 28
  • 45