0

I'm trying to make a simple little templating system where I don't want to define every variable that's available in the template. For example.

Every page would have

<?php displayHTML($variable); ?> 

but only some pages would actually have $variable defined.

$variable = "something";

In included functions.php I have tried

function displayHTML($variable){
if(isset($variable)){
echo $variable;
}else{
echo '';
        }
}

But the error seems to be coming from the fact that $variable isn't defined rather than not being set with a value. This error only shows with E_ALL enabled and I know I can shut it with @ or simply defining $variable across the board. Any function or best practice to avoid having to do this?

I know this seems weird but I would like to option to not define every variable if its not gonna be used on a page.

2
  • Why avoid @ if that's EXACTLY what you want to do? It's like trying to figure out a hack to avoid a hack. Commented Sep 28, 2011 at 23:27
  • I read that having the undefined errors can slow down your code. I would like to stay lazy but not slow down my code. Does using @ still cause the slight perfomance hits that undefined errors cause? Commented Sep 29, 2011 at 3:57

4 Answers 4

4

Define each variable before you use it.

If your templates are trying to use variables that don't exist, then that is a logical fault with your design, and you should fix that.

Give them some "default" or empty value if that's the visualisation that you wish to assign to those values.

Leaving them undefined is simply error-prone and, forgive me for saying so, lazy.


Some people would tell you to use the @ error-suppressant. This is a fast, cheap, hacky workaround to an underlying fault... and my advice would be to steer well clear of it.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought laziness was the sign of a potentially great programmer ;) You have a solid points but can you please explain why the & solution that mfonda is bad practise?
2

The problem is that the code where displayHTML() is being called is trying to reference $variable. Checking if it's set within the function itself won't work. You would need to check if it's set where the function is being called:

<?php displayHTML(isset($variable) ? $variable : ''); ?>

1 Comment

good answer but I would like my HTML to be as clean as possible by referencing the function from another page.
1

Your displayHTML function will not omit notices if you change it to:

function displayHTML(&$variable){
...
}

This will have the side effect of defining the variable if it is undefined, but will do what you want. As to what you are trying to do, you are probably better off making sure every variable in your template is defined, or just using plain ol' isset() before using a potentially undefined variable.

1 Comment

This is very interesting and works! Why would you not recommend it for my efforts tho?
0

You can either use http://php.net/manual/en/function.error-reporting.php to turn off error reporting or make manual error handler which is more intelligent about error display http://php.net/manual/en/function.set-error-handler.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.