This "error" is a Notice, rather than an actual error. You can change the php config on your server so that you don't get this Notice. This will make your code work.
In the php.ini file change the error_reporting config from...
error_reporting = E_ALL; display all errors, warnings and notices