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']);
Kevin
  • 41,694
  • 12
  • 53
  • 70
Christian Žagarskas
  • 1,068
  • 10
  • 20

2 Answers2

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'];
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • nice. thank you. so it seems: (condition ? true result : false result) – Christian Žagarskas Apr 23 '15 at 05:05
  • yes, having `?` like the one you have `($F_NAME ? isset($_POST['F_NAME'])` inside the condition is incorrect – Kevin Apr 23 '15 at 05:10
  • 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 Apr 23 '15 at 05:48
  • @ChristianŽagarskas yes that should work just fine if you'd like to check for the values if it exist first. glad this helped – Kevin Apr 23 '15 at 05:48
1

As Ghost response, or even shorter

$F_NAME = $_POST['F_NAME'] ? : $_SESSION['USR']['F_NAME'];
Tom
  • 691
  • 4
  • 7