I noticed that in the CodeIgniter 4 documentation there are two types of echo's
: <?php echo $variable; ?>
and <?= $variable ?>
. I particularly prefer the <?= $variable ?>
.
I have an update form, where the fields use value="<?= $user_hunter['name_hunter'] ?>"
to return the value, but a I don't know how it would work anymore using <?= ?>
.
I would like to standardize this, I know the snippet below works, but I wanted to understand how the alternative works.
<div >
<div for="blood_type">blood_type:
<select name="blood_type" required>
<option value="" <?php if($user_hunter['blood_type']==''){echo 'selected';}?>>Choose the blood type </option>
<option value="A " <?php if($user_hunter['blood_type']=='A '){echo 'selected';}?>>A </option>
<option value="A-" <?php if($user_hunter['blood_type']=='A-'){echo 'selected';}?>>A-</option>
<option value="B " <?php if($user_hunter['blood_type']=='B '){echo 'selected';}?>>B </option>
<option value="B-" <?php if($user_hunter['blood_type']=='B-'){echo 'selected';}?>>B-</option>
<option value="AB " <?php if($user_hunter['blood_type']=='AB '){echo 'selected';}?>>AB </option>
<option value="AB-" <?php if($user_hunter['blood_type']=='AB-'){echo 'selected';}?>>AB-</option>
<option value="O " <?php if($user_hunter['blood_type']=='O '){echo 'selected';}?>>O </option>
<option value="O-" <?php if($user_hunter['blood_type']=='O-'){echo 'selected';}?>>O-</option>
</select>
</div>
</div>
CodePudding user response:
When you are using <?= ?>
, you need to provide a echoable value.
In your case you want to echo the result of an if
condition. A ternary condition would be nice.
A ternary condition is like an if .. else
but in a single line. You can search for Ternary Behaviour here
A solution with your data will be <?= $user_hunter['blood_type'] == 'A ' ? 'selected' : '' ?>
. You can translate this condition by if the user blood type is A then echo selected else echo nothing