PHP show the Undefined variable if you are not initialized the variable before its call. You should initialized the variable before start the scope.
if there is conditional structure and looping structure for global variable you should call and initialized the variable first
$error ='';
if(something conditional){
$error = 'some message' or $error .="something";
}
if looping structure
$error ='';
for and foreach and while loop{
$error = 'some message' or $error .="something";
}
the main reason if you use the RAW PHP code is would be better to call and initialize the variable at start of PHP script such as
<?php
$error='';
if(something conditional){
$error = 'some message' or $error .="something";
}
?>
the other reason is in your syntax, you contact the string message with $error variable, which has no global defination and initialization before the IF statement and string concatenation. that why it show the undefined variable.It would be safe call and initialized the variable before it's scope of using.