0

I'm gettin' a headache on that. I need to put if statement inside an echo (this echo is in a function, it's for a form submit actually)

Here is an example on a partial of my code. In this situation, how can I put theses if statement inside my echo??

   <?php echo '<td><select id="depuis" name="depuis">
    <option value=\'4\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'4\'){ echo \'selected\'; } else { echo ''; } ?> ></option>
    <option value=\'1\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'1\'){ echo \'selected\'; } else { echo ''; } ?> >2 ans et moins</option>
    <option value=\'2\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'2\'){ echo \'selected\'; } else { echo ''; } ?> >2 &agrave; 5 ans</option>
    <option value=\'3\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'3\'){ echo \'selected\'; } else { echo ''; } ?> >5 ans et plus</option>
</select>
</td>'
; ?>
0

6 Answers 6

6

Everything is php so need to use more than the first <?php Finish each echo before checking with if. Like this:

<?php 
  echo '<td><select id="depuis" name="depuis">
    <option value="4"'; 
    if(isset($_POST['depuis']) && $_POST['depuis'] == '4') { 
      echo ' selected'; 
    } 
 echo ' >Something here maybe?</option>...etc
1
  • Great! This is quite simple, no need of new variables, +1 ! Commented Jan 17, 2014 at 0:55
5

Use an inline if statement:

echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody');
4
  • 1
    @machineaddict - echo can take a comma separated list of arguments, as well as a string.
    – andrewsi
    Commented Jan 17, 2014 at 1:09
  • @andrewsi: Thanks for the info. I just tested it and it works. Sorry Aidan. Commented Jan 17, 2014 at 1:11
  • @machineaddict: No worries
    – Aidan
    Commented Jan 17, 2014 at 1:13
  • Works perfect, I love it
    – Eben Watts
    Commented Jan 27, 2021 at 2:36
3

This would work - although I'm sure it could be streamlined:

<?php

$out = '
    <td>
        <select id="depuis" name="depuis">
        <option value="4"
    ';

    if(isset($_POST['depuis']) && $_POST['depuis'] == '4'){ 
        $out .= 'selected';
    } 

    $out .= '
        ></option>
        <option value='1' 
    ';

    if(isset($_POST['depuis']) && $_POST['depuis'] == '1'){ 
        $out .= 'selected';
    }

    $out .= '
        >2 ans et moins</option>
        <option value=\'2\' 
    ';


    if(isset($_POST['depuis']) && $_POST['depuis'] == '2'){ 
        $out .= 'selected';
    }

    $out .= '
        >2 &agrave; 5 ans</option>
        <option value='3' 
    ';

    if(isset($_POST['depuis']) && $_POST['depuis'] == '3'){ 
        $out .= 'selected';
    }

    $out .= '
            >5 ans et plus</option>
        </select>
    </td>
    ';

    echo $out;
?>

Meilleurs voeux...

1
  • Merci beaucoup, a toi aussi! :) Commented Jan 17, 2014 at 0:55
2

You will want to use the a ternary operator which acts as a shortened IF/Else statement:

echo '<option value="'.$value.'" '.(($value=='United States')?'selected="selected"':"").'>'.$value.'</option>';

1

You shouldn't escape the array keys in the string

if(isset($_POST[\'depuis\']) ...

Should be

if(isset($_POST['depuis']) && ...

Also you could clean up the the generation of the options with a array and a loop

$options = array(
  'label1' => 1,
  'label2' => 2,
  'label3' => 3,
  'label4' => 4
);
$value = (isset($_POST['depuis'])) ? $_POST['depuis'] : 0;

foreach ($options as $label => $optionValue) {
  $selected = ($optionValue == $value) ? ' selected' : '';

  printf('<option value="%s"%s>%s</option>', $optionValue, $selected, $label); 
}
2
  • If the order is important, you might say foreach (['4', '1', '2', '3'] as $x).
    – cHao
    Commented Jan 17, 2014 at 0:53
  • (BTW, all your options have value="4".)
    – cHao
    Commented Jan 17, 2014 at 0:56
0

Not sure you can do that like you're asking. You'd just need to use regular if/else if block and output the outcome separately.

<?php 
echo("<td><select id=\"depuis\" name=\"depuis\">");
echo("<option value='4'");
if(isset($_POST['depuis']) && $_POST['depuis'] == '4'){ echo("selected"); } 
echo("></option>");

[ ...etc... ]
?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.