mazlix mazlix - 3 months ago 12
PHP Question

Undefined variable: errors from empty 3rd uri params (code igniter)

I have many controllers with functions like

function search($person) {
if empty($person) {
redirect('...');
}
else {
.........
}
}


I wasn't getting Undefined variable errors before, but now i do (if i go to .../search/) without passing a $person. I found i can fix this by doing
function search($person = null)
but i was wondering if there is a "better" way. I am also curious why i wasn't getting errors before (on localhost) but am now.. (on godaddy).

THANKS!!!

Answer

I wouldn't provide a default value for the argument in this case, function search($person) is how the function should be declared if $person is indeed a required argument. You want this to throw a warning so you can quickly see what's wrong during development.

In production, you simply switch off error reporting/error display.
See http://codeigniter.com/user_guide/general/errors.html.