I have my own class for templates. In this class, I replace string likes {NEXT_PAGE}
to
<?php echo $tpl->vars["NEXT_PAGE"]; ?>
But sometimes happen, that this index doesn't exists.
I found topic PHP: is_array on $arr['key'] with non existing 'key' and I know how to check if some index exists.
My question is, what is better to improve performance.
- Always check if index exist and if yes, print it
- Just print it without checking if index exists.
First solution is great, no warning, no errors, no notices, but I go through array twice, first time to check it, second time to print it. Need more CPU time.
Second solution just try to find it, if it exists print it, otherwise print empty string, it's OK for me, and warnings I can disable by error_reporting
So what is better? I think that second solution with disabling warnings. PHP always have to check if index exists, but when I check it too, it is checked twice. Am I right?
Just to be clear, accessing to index which doesn't exists is max 3% of all accessing to this array.
Example
I have a form, where I put back login name, if pass is incorrect.
When I replace {LOGIN_NAME}
by <?php echo $tpl->vars["LOGIN_NAME"]; ?>
I save this new file like a PHP script, so later I just run this, no replacing again!
So my compiled script is something like this
<form ...>
<input type="login" ... value="<?php echo $tpl->vars["LOGIN_NAME"] ?>" />
(pass etc...)
</form>
So when someone visit this page for the first time LOGIN_NAME isn't set, PHP can't print anything but it's OK, field is still empty. If form is send, script add to LOGIN_NAME sent login name and then field isn't empty.