Skip to content
Advertisement

PHP Append to empty string without error

I have got the following issue, my function appends code to a string $string ($string .= bla), but in the beginning the string is empty, so I get this error message

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: $string
Filename: libraries/file.php
Line Number: 90

Of course if I define the string in advance, like $string = NULL, the error would not occur, but but I though maybe there is a way to append to a string without the need to define the variable in advance / having an !empty check.

It should be just a one-line thing, I am looking for a slim code.

Thanks.

Advertisement

Answer

Slim code and removing all notice level errors don’t usually go hand in hand. As you know, the error is about using an uninitialized variable. You can’t initialize it without..initializing it.

I wouldn’t consider $string = ''; bloat, though.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement