sNews Forum

Website Talk => Web Programming => Topic started by: jlhaslip on May 18, 2009, 11:14:19 pm

Title: having trouble with an IF statement here
Post by: jlhaslip on May 18, 2009, 11:14:19 pm
When the post[create] value is equal to 'yes', I get the reply 'no', even though an echo of the value confirms the 'yes' entry.
Must be something about the way I have the nested ternary if statement, but I can't figure it out...
Code: [Select]
<?php

unset($_POST);
echo 
' Case 1 : yes<br />';
$_POST['create'] = 'yes';
$msg .= "Message " . (($_POST['create'] == "yes") ? "yes" : ($_POST['create'] == 'no') ? "no" "maybe" ) . "\n";
echo 
'$_POST[\'create\'] is ' $_POST['create'] . '<br />';
echo 
$msg '<br /><br />';

unset(
$_POST);
echo 
' Case 2 : no<br />'
$_POST['create'] = 'no';
$msg "Message: " . (($_POST['create'] == 'yes') ? "yes" : ($_POST['create'] == 'no') ? "no" "maybe" ) . "\n";
echo 
'$_POST[\'create\'] is ' $_POST['create'] . '<br />';
echo 
$msg '<br /><br />';

unset(
$_POST);
echo 
' Case 3 : maybe<br />';
$_POST['create'] = 'maybe';
$msg "Message " . (($_POST['create'] == 'yes') ? "yes" : ($_POST['create'] == 'no') ? "no" "maybe" ) . "\n";
echo 
'$_POST[\'create\'] is ' $_POST['create'] . '<br />';
echo 
$msg '<br />';

?>
Resulting Output:
Quote
Case 1 : yes
$_POST['create'] is yes
CREATE AWARENESS AND IDENTITY: no

Case 2 : no
$_POST['create'] is no
CREATE AWARENESS AND IDENTITY: no

Case 3 : maybe
$_POST['create'] is maybe
CREATE AWARENESS AND IDENTITY: maybe
I can't seem to figure the Logic here, or something. There are three possible answers to the question and I would like to have the text output 'yes', 'no' or 'maybe' based on a single IF statement. I can get the correct answer for 'no' and 'maybe' (other reply), but the if doesn't seem to be able to figure out the 'yes' reply.

???

Any ideas? Thanks.
Title: Re: having trouble with an IF statement here
Post by: centered on May 18, 2009, 11:39:37 pm
Code: [Select]
<?php

unset($_POST);
echo 
' Case 1 : yes<br />';
$_POST['create'] = 'yes';
$msg "Message " . (($_POST['create'] == "yes") ? "yes" : (($_POST['create'] == 'no') ? "no" "maybe" )) . "\n";
echo 
'$_POST[\'create\'] is ' $_POST['create'] . '<br />';
echo 
$msg '<br /><br />';

unset(
$_POST);
echo 
' Case 2 : no<br />'
$_POST['create'] = 'no';
$msg "Message " . (($_POST['create'] == "yes") ? "yes" : (($_POST['create'] == 'no') ? "no" "maybe" )) . "\n";
echo 
'$_POST[\'create\'] is ' $_POST['create'] . '<br />';
echo 
$msg '<br /><br />';

unset(
$_POST);
echo 
' Case 3 : maybe<br />';
$_POST['create'] = 'maybe';
$msg "Message " . (($_POST['create'] == "yes") ? "yes" : (($_POST['create'] == 'no') ? "no" "maybe" )) . "\n";
echo 
'$_POST[\'create\'] is ' $_POST['create'] . '<br />';
echo 
$msg '<br /><br />';

?>

(($_POST['create'] == 'no') ? "no" : "maybe" ) - you needed to enclose this ternary statement
Title: Re: having trouble with an IF statement here
Post by: jlhaslip on May 19, 2009, 02:20:22 am
Gotcha, works great... I'll need to remember that word --- "ternary".

I was googling "tertiary", which is different, of course.

Thanks much EQ.