Jump to content

shorthand syntax help


cloudll
Go to solution Solved by requinix,

Recommended Posts

Hey,

 

I have only recently learnt about using shorthand. I have managed to convert most of my simple if else's and isset checks to shorthand but am struggeling with a few.

 

Could someone show me how I would change this to shorthand, when it has two variables in the else.

if (isset($_SESSION['posY'])) {
    $posY = $_SESSION["posY"];
} else {
    $posY = 50;
     $_SESSION["posY"] = 50;
}

Thanks

 

Link to comment
Share on other sites

  • Solution

If I may translate that,

What do you mean by "shorthand"?

If you mean the ternary operator ? : then the answer is,

While possible, in this case you shouldn't. That's more appropriate if you have "if ($condition) $var = $value; else $var = $other_value;". Your if has two statements in that else so it's really just better to leave it as is. And this is coming from someone who generally does use shorthand.

Link to comment
Share on other sites


// set a default value if not already set -
if(!isset($_SESSION['posY'])){
    $_SESSION["posY"] = 50;
}
// and if you really, really, really are creating more variables holding values that you already have variables for (what's wrong with using $_SESSION["posY"] ?) -
$posY = $_SESSION["posY"];
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.