tags:

views:

77

answers:

6

I have a basic PHP question, take the code below for example, let's say I need to use this 10 times on a page, is there a better way to do it?

  • I realize I could wrap it in a function and just keep calling that function but is there a better way then to keep on checking if the item is set and equals a a certain value. After finding this out the first time is there some other way of remembering the result from the first time instead of doing it 10 different times?

Hope that makes sense.

<?PHP
if (isset($_SESSION['auto_id']) && $_SESSION['auto_id'] == "1") {
    //do something
}

// do other code here that breaks these up

if (isset($_SESSION['auto_id']) && $_SESSION['auto_id'] == "1") {
    //do something else
}

// do other code here that breaks these up

if (isset($_SESSION['auto_id']) && $_SESSION['auto_id'] == "1") {
    //do something else
}

// do other code here that breaks these up

if (isset($_SESSION['auto_id']) && $_SESSION['auto_id'] == "1") {
    //do something else
}

...ect

?>

+2  A: 

Maybe a better approach is to use isset once at the top of the function, and set the variable to a default value there. Then you can simply use the value through the rest of the function.

In your example, you could set it to "0", though I realize that may not be the real code...

Ned Batchelder
+4  A: 

In this case, yes you have to, although you could do it once and assign the result to a variable.

how about...

<?PHP
$myCheck = (isset($_SESSION['auto_id']) && $_SESSION['auto_id'] == "1") ;

if($myCheck) {
    //do something
}

// do other code here that breaks these up

if($myCheck) {
    //do something else
}

// do other code here that breaks these up

if($myCheck) {
    //do something else
}

// do other code here that breaks these up

if($myCheck) {
    //do something else
}
etc.
?>

Syntax may be off - it's a long time since I've done any PHP work...

ZombieSheep
I wonder if $myCheck would be true is it was set to 0
jasondavis
If what was set to zero? If you set $myCheck = 0 then $myCheck is false (javascript follows the C rule that 0 is false and non-zero is true). If auto_id is "0", then $myCheck will still be false since the value of the boolean expression assigned to $myCheck is false.
Tyler McHenry
awsome thank you
jasondavis
+3  A: 

Sure. Just save the value of the boolean expression in another variable.

<?php

  $auto_id_is_one = ($_SESSION['auto_id']) && $_SESSION['auto_id'] == "1");

  // ...

  if ($auto_id_is_one) {
     // do something
  }

  // ...

  if ($auto_id_is_one) {
     // do something else
  }

  // ...
?>

You probably want to give it a more meaningful name than $auto_id_is_one, though.

Tyler McHenry
$auto_id_is_one is better than my $myCheck :)
ZombieSheep
A: 

It depends what the "do something" block of code is, and whether or not the auto_id index of $_SESSION is changed in the other code. You can be certain that, in the body of the first if, the variable exists and is 1. Once that if concludes, you can no longer be certain - you'll have to check again later unless all the rest of the code is executed in a context that only exists if the first test succeeds (i.e. there's an else clause that terminates the script), and you are sure you don't change the value (and no external code you call changes it).

A better way to check the sanity might be to ensure most of the environment is as you expect it just once, then just check specific values at various places. However, if you're constantly rechecking this, it might indicate a design flaw, where similar logic (i.e. that dependent on auto_id = 1) is not well isolated and grouped.

Adam Wright
A: 
<?PHP
if (isset($_SESSION['auto_id']) && $_SESSION['auto_id'] == "1") {
    $sessionOK = TRUE;
}


if ($sessionOK) {
    //do this
}

if ($sessionOK) {
    //do that
}
code_burgar
It would be cool if downvoting valid answers came with at least a short reason
code_burgar
It might have been because you don't set $sessionOK if the check fails; it would be simpler to just assign $sessionOK to the result of the check / comparison as others have done. I didn't downvote you.
Tom Haigh
it looks good to me
jasondavis
A: 

In the example you provided, PHP will just issue an E_NOTICE that the index is not found in the $_SESSION super global (It will not throw the notice if you turned off strict mode). The best practice would be to go and set the value to a default so that you know for sure that the variable is set.

ex

<?php

$myVar = isset($_SESSION['auto_id']) ? $_SESSION['auto_id'] : FALSE;

if (false !== $myVar)
{
  //do something
}

//do something not realated to myVar being set

if (false !== $myVar)
{
  //do somethign else
}
?>
MANCHUCK