Why is there @ in front...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 @
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it fires a notice, whether you hide it or not if the element isn't set.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Why is that bad?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Any notices, warnings or errors being fired in my book is quite bad.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Cool snipplet, thanks feyd.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

with logical ands, if the first fails, evaluation stops.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ok
Post Reply