ternary in a function

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Could I have a ternary operator in a function? Defining a variable? Something like the following...
PHP Code: [ Select ]
$auth = new auth();
$errors = new errors();
 
if($auth->reset_session(<span style="font-weight: bold">(isset($sid) ? $sid : null)</span>, true) != true)
{
       $errors->set_new(SESSION_NOT_SET, _LINE_, _FILE_);
}
  1. $auth = new auth();
  2. $errors = new errors();
  3.  
  4. if($auth->reset_session(<span style="font-weight: bold">(isset($sid) ? $sid : null)</span>, true) != true)
  5. {
  6.        $errors->set_new(SESSION_NOT_SET, _LINE_, _FILE_);
  7. }

The bold thing is what I'm talking about. Thanks :)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

The short answer: yes. In my opinion, however, it's bad form, because when you (or someone else) goes to look at that code a while from now, it's going to be more difficult to understand.

My code would look like this:
Code: [ Select ]
$auth = new auth();
$errors = new errors();
$sid = (empty($sid) ? null : $sid);

if( $auth->reset_session($sid, true) ) {
    $errors->set_new(SESSION_NOT_SET, _LINE_, _FILE_);
}
  1. $auth = new auth();
  2. $errors = new errors();
  3. $sid = (empty($sid) ? null : $sid);
  4. if( $auth->reset_session($sid, true) ) {
  5.     $errors->set_new(SESSION_NOT_SET, _LINE_, _FILE_);
  6. }
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

ok, thanks :)

One more thing. If $sid = null then if(empty($sid)) would be true?
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

PHP.net wrote:
The following things are considered to be empty:
 
  * "" (an empty string)
  * 0 (0 as an integer)
  * "0" (0 as a string)
  * NULL
  * FALSE
  * array() (an empty array)
  * var $var; (a variable declared, but without a value in a class)


http://us3.php.net/empty
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

oh lol thanks. Wonder why I haven't went there :roll:

Thanks.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

It would be safer to check the variable within the auth classes reset_session method. Having methods check their own arguments is not only safer security-wise, it also saves you needing to add that line of test code before or within every call of the method.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

joebert wrote:
It would be safer to check the variable within the auth classes reset_session method. Having methods check their own arguments is not only safer security-wise, it also saves you needing to add that line of test code before or within every call of the method.

That's what I ended up doing :)

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 57 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.