Page 2 of 2
Posted: Mon Mar 20, 2006 4:17 pm
by s.dot
there's one valid instance (at least I think it's valid) where I use the @
I have a header.php file that I include on every page. This page includes a call to session_start();
but sometimes I need to check if a user is logged in (making a call to session_start()) before I load the header page, so I can redirect them to a login page.
Thus, I put the @ in front of session_start to avoid session already started notices.
Posted: Mon Mar 20, 2006 5:30 pm
by John Cartwright
scrotaye wrote:there's one valid instance (at least I think it's valid) where I use the @
I have a header.php file that I include on every page. This page includes a call to session_start();
but sometimes I need to check if a user is logged in (making a call to session_start()) before I load the header page, so I can redirect them to a login page.
Thus, I put the @ in front of session_start to avoid session already started notices.
That just smells like bad design.. and not a good reason for @
Posted: Mon Mar 20, 2006 8:26 pm
by Benjamin
Ok tell me how this is bad design,
Code: Select all
if (@$_POST['value'] == 'something') {
// do this
}
It works great.
It saves time vs writing another if statement
It saves overhead vs calling another php function (isset)
It adheres the programmers practice of writing code to make jobs easier
It's not a security vulnerability
etc. etc..
Posted: Mon Mar 20, 2006 9:02 pm
by feyd
it fires a notice, whether you hide it or not if the element isn't set.
Posted: Mon Mar 20, 2006 9:03 pm
by Benjamin
Why is that bad?
Posted: Mon Mar 20, 2006 9:10 pm
by feyd
Any notices, warnings or errors being fired in my book is quite bad.
Posted: Mon Mar 20, 2006 9:58 pm
by John Cartwright
How lazy can you get?
Code: Select all
if (!empty($_POST['somevar']) && $_POST['somevar'] == 'somevalue') {
That only took about 1-2 seconds..
Posted: Mon Mar 20, 2006 10:07 pm
by feyd
I have something like this written into many projects
Code: Select all
function check($element, $array = '') {
if(func_num_args() < 2 or !is_array($array)) {
$array =& $_REQUEST;
}
$return = (!empty($array) and isset($array[$element]));
return $return;
}
function fetch($element, $default = '', $array = '') {
if(func_num_args() < 3 or !is_array($array)) {
$array =& $_REQUEST;
}
if(check($element, $array)) {
$return = $array[$element];
} else {
$return = $default;
}
return $return;
}
Posted: Mon Mar 20, 2006 10:17 pm
by John Cartwright
Cool snipplet, thanks feyd.
Posted: Thu Mar 30, 2006 9:57 am
by Benjamin
Jcart wrote:How lazy can you get?
Code: Select all
if (!empty($_POST['somevar']) && $_POST['somevar'] == 'somevalue') {
That only took about 1-2 seconds..
Even if you organize it like that, isn't the second part of the IF statement still evaluated? Does PHP stop parsing the IF statement at the first failure? If it doesn't then it's pointless to code it like that, and in order to truly accomplish this you would need a nested IF statement.
Posted: Thu Mar 30, 2006 10:14 am
by feyd
with logical ands, if the first fails, evaluation stops.
Posted: Thu Mar 30, 2006 10:15 am
by Benjamin
Ok