I use codeigniter, one of the main issues i have with the views is undefined variables that may or may not exist.
I know that it’s easy to get rid of the notices when using something like:
<?=html($this->input->post('name'))?>
By adding if(isset(..){ html($this->input->post('name')); }
But it’s very annoying and it makes the code look ugly, I also don’t want to disable notice reporting.
Is there any advice about that?
The “best” way? I still say it’s
isset. It’s nice to be explicit, and it would help me, as someone maintaining your code later, to realize that you are okay with that variable not existing and that it wasn’t just a mistake. Unless theissetcheck were there, I’d assume you just didn’t realize it was possible for it not to be set, and would wonder what you actually meant to do.Additionally, I’d assert that one of the main reasons it looks ugly is because all the code is being crammed into one line. There are two things you’re trying to do: check if it’s set, and print it if so. Maybe spacing them out would look nicer, even if it’s a bit more verbose:
This also makes the snippet easier to manage later, in case you later decide to, say, wrap the output in an HTML tag, or add an
elsecase.Plus, since, according to the question, we can’t mess with the error reporting settings, checking for the variable’s existence is the only option. Sorry. Either you check if it’s set, or disable the error messages saying it’s not. No middle road.