0

So basically what am trying to achieve is the following.

I am trying to make it so the following script does something in this instance:

If $something == "0" then $something1 == "no"
If $something == "1" then $something1 == "yes"
else echo "Error."

That is how I would explain what Im trying to do.

This is my current code:

<?php
if(isset($_POST['resolve'])){
    $api = "http://test.com/php/";
    if(strlen($_POST['name'])==0){
        echo "fill in all fields!";
    } else {
        $response = file_get_contents($api.$_POST['name']); 
        $array = unserialize($response);
        ?>
        <div align="center"><?php echo "".$array['something1']; ?></div>
        <?php
     }
}
?>

I would like it to echo "no" if the result of array "something" is "0" and echo "yes" if the result of array "something" is "1".

georg
  • 211,518
  • 52
  • 313
  • 390
user3724476
  • 4,720
  • 3
  • 15
  • 20

6 Answers6

1
<?php
if($array['something'] == '0'){echo 'No';}
elseif($array['something'] == '1'){ echo 'Yes';}
else{ echo 'Error!'; }
?>
k32y
  • 407
  • 6
  • 11
1

switch case is the most elegant way to go here:

switch($array['something']) {
    case 0: echo 'No';break;
    case 1: echo 'Yes';break;
    default: echo 'Error.';
}
vcanales
  • 1,818
  • 16
  • 20
0
<?php
if(isset($_POST['resolve'])) {
    $api = "http://test.com/php/";
    if(!$_POST['name']) {
        echo "Please, fill in all fields!";
    } else {
        $response = file_get_contents($api.$_POST['name']); 
        $array = unserialize($response);
        echo "<div align='center'>";
            if($array['something'] == '0') {
                echo 'No';
            }
            elseif($array['something'] == '1') {
                echo 'Yes';
            }
            else {
                echo 'Error.';
            }
        echo "</div>";
    }
}
?>

Don't forget to also do a security input check on $_POST['name']

Paul
  • 634
  • 3
  • 7
  • 18
0

Voila

echo $array['something1'] ? "Yes" : "No"; 
Adam
  • 17,838
  • 32
  • 54
0

This sets $array['something1'] to either 'yes' or 'no' depending on the value of $array['something'].

<?php
if(isset($_POST['resolve'])){
    $api = "http://test.com/php/";
    if(strlen($_POST['name'])==0){
        echo "fill in all fields!";
    } else {
        $response = file_get_contents($api.$_POST['name']); 
        $array = unserialize($response);
        $array['something1'] = $array['something'] == 0 ? 'no' : 'yes';
        ?>
        <div align="center"><?php echo "".$array['something1']; ?></div>
        <?php
     }
}
0
$yesno = ['No', 'Yes'];
$something1 = $yesno[$something];

That is the simplest way I know of doing it.

vascowhite
  • 18,120
  • 9
  • 61
  • 77