In PHP, is there an alternative to putting @ in front of functions whose variables are defined later? Explanation + code inside -


i'd preface saying i've read answers similar questions, , haven't managed find both job , not slow site down.

i have site displays messages based on users' choices , actions. code might :

if (option 1) {     $message1 = "message a"; } else if (option 2){     $message1 = "message b"; } else {     $message1 = "message c"; } 

there hand full of these throughout site. when want echo messages somewhere within html structure have write:

<?php     if (isset($message1)) {        echo $message1;     } ?> 

i've written simple function job:

function message($msg){     if (isset($msg)) {         echo $msg;     } } 

the problem notices undefined variables, makes sense because variable isn't defined before user clicks button. avoid turning off error reporting.

so, adding @ in front of function way? code like:

<?php @message($message1); ?> 

if acceptable, great. if not, i'm open alternatives.

based on comment, use-case seems make sense. in case have array error messages, like:

$messages = array(); ... $messages['registration-form']['error']['password-mismatch'] = 'passwords not match'; ... 

and when validate input , find mismatching passwords, do:

// @ top $errors = array(); ... // passwords don't match $errors['passwords-mismatch'] = $messages['registration-form']['error']['password-mismatch']; 

and output form below passwords:

messages($errors, 'password-mismatch'); 

and function like:

function messages($errors, $error) {   if (isset($errors[$error])) {     // wrap in span highlight error     echo '<span class="error">' . $errors[$error] . '</span>';   } } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -