0

I have the following line of code:

<input id="b_first_name" type="text" name="b_first_name" class="required"
    value="<?= $this->settings['fill_form'] ? "Bob" : "" ?>">

Is there a cleaner way to write this? I don't like writing a ternary where one side doesn't need output, it feels dirty. I am looking to output "Bob" ONLY if the setting "fill_form" is set to true. I wish I could something like this, but neither work:

<input id="b_first_name" type="text" name="b_first_name" class="required"
    value="<?= $this->settings['fill_form'] ? "Bob" ?>">

or

<input id="b_first_name" type="text" name="b_first_name" class="required"
    value="<?= if ($this->settings['fill_form']) "Bob" ?>">
Roeland
  • 3,698
  • 7
  • 46
  • 62
  • 3
    There's no cleaner way. Another option is ` if ($this->settings['fill_form']) echo "Bob" ?>` – u_mulder Sep 10 '14 at 20:19
  • 2
    `settings['fill_form']) echo "Bob";?>` would work if you prefer it to the first option. Or: `=!$this->settings['fill_form'] ?:'Bob';?>` – Wrikken Sep 10 '14 at 20:23
  • Related: http://stackoverflow.com/questions/7278835/cs-null-coalescing-operator-in-php – Clive Sep 10 '14 at 20:24

1 Answers1

0

Missing the echo.

<input id="b_first_name" type="text" name="b_first_name" class="required"
value="<?php if ($this->settings['fill_form']) echo "Bob";  ?>">

But I would put the brackets as well

<input id="b_first_name" type="text" name="b_first_name" class="required"
value="<?php if ($this->settings['fill_form']) { echo "Bob"; } ?>">
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Demodave
  • 6,242
  • 6
  • 43
  • 58