Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: having trouble with an IF statement here  (Read 3506 times)

jlhaslip

  • Sr. Member
  • ****
  • Karma: 16
  • Posts: 374
    • My snews with AEF Forum site
having trouble with an IF statement here
« 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.
« Last Edit: May 18, 2009, 11:17:06 pm by jlhaslip »
Logged
Yes! I have no siggy.

centered

  • Guest
Re: having trouble with an IF statement here
« Reply #1 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
Logged

jlhaslip

  • Sr. Member
  • ****
  • Karma: 16
  • Posts: 374
    • My snews with AEF Forum site
Re: having trouble with an IF statement here
« Reply #2 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.
Logged
Yes! I have no siggy.