PHP, shorthand, If..Else using ternary operators
Is there a oneliner for this? Good OP Ternard?
$F_NAME = $_SESSION['USR']['F_NAME'];
if(isset($_POST['F_NAME'])) {$F_NAME = $_POST['F_NAME'];}
Basically "If a POST is sent, show that even if the message is empty, otherwise grab the value from the session, but only if the message was not set or is empty"
Really splitting hair here ...
looking for something like this:
$F_NAME = ? ($F_NAME ? isset($_POST['F_NAME']) : $_SESSION['USR']['F_NAME']);
+3
Christian Žagarskas
source
to share
2 answers
It was assumed that:
(conditions) ? true : false
satisfies <--^ ^----> did not satisfy
So this is the same as:
$F_NAME = isset($_POST['F_NAME']) ? $_POST['F_NAME'] : $_SESSION['USR']['F_NAME'];
+3
Ghost
source
to share
Like the Ghost's answer, or even shorter
$F_NAME = $_POST['F_NAME'] ? : $_SESSION['USR']['F_NAME'];
+1
Tom
source
to share