How to embed if statement inside echo

2020-02-13 07:37发布

问题:

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>'
; ?>

回答1:

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


回答2:

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...



回答3:

Use an inline if statement:

echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody');


回答4:

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); 
}


回答5:

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>';



回答6:

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... ]
?>