3

Is there a oneliner for this? A nice Ternary OP?

$F_NAME = $_SESSION['USR']['F_NAME'];
if(isset($_POST['F_NAME'])) {$F_NAME = $_POST['F_NAME'];}

Basically "If the POST is sent, show that, even if the post is empty, otherwise grab the value from the session, but only if the post was not set or empty"

Really splitting hairs here...

looking for something like this:

$F_NAME = ? ($F_NAME ? isset($_POST['F_NAME']) : $_SESSION['USR']['F_NAME']);
1
  • $F_NAME = (isset($_POST['F_NAME']) ? $_POST['F_NAME'] : $_SESSION['USR']['F_NAME']); Commented Apr 23, 2015 at 5:03

2 Answers 2

3

Its supposed to be:

(conditions) ? true : false
   satisfies <--^      ^----> did not satisfy

So this equates into:

$F_NAME = isset($_POST['F_NAME']) ? $_POST['F_NAME'] : $_SESSION['USR']['F_NAME'];
Sign up to request clarification or add additional context in comments.

4 Comments

nice. thank you. so it seems: (condition ? true result : false result)
yes, having ? like the one you have ($F_NAME ? isset($_POST['F_NAME']) inside the condition is incorrect
AWESOME. I tossed that into a foreach with the {variable} set option ` foreach ($_SESSION['USR'] as $key => $value) { ${$key} = (isset($_POST[$key]) ? $_POST[$key] : $_SESSION['USR'][$key]); }`
@ChristianŽagarskas yes that should work just fine if you'd like to check for the values if it exist first. glad this helped
1

As Ghost response, or even shorter

$F_NAME = $_POST['F_NAME'] ? : $_SESSION['USR']['F_NAME'];

2 Comments

thank you! Ghost however is correct, there are 2 conditions I must satisfy, the statement can not default to $_SESSION['USR']['F_NAME']
But you get the picture on shorthand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.