0

Cannot write class attribute in php file for html element. Here is my code

echo '<div><p class="'.($value->getIsRequire() == 1) ? 'required' : ''.'">
</p> <input type="text" name="field" id="option-value" /></div>';

My output should be if the condition is true the class should be required otherwise the class should be empty but i got output as required text instead of input box.

Result I got is

enter image description here

3
  • Use proper parentheses. Commented Nov 16, 2016 at 11:59
  • ($value->getIsRequire() == 1) ? 'required' : '' -> ($value->getIsRequire() == 1 ? 'required' : '') Commented Nov 16, 2016 at 12:00
  • It shows nothing input element is not displaying Commented Nov 16, 2016 at 12:04

1 Answer 1

1

try this, you should wrap it in parentheses :

echo '<div><p class="'.(($value->getIsRequire() == 1) ? 'required' : '').'">
</p> <input type="text" name="field" id="option-value" /></div>';
Sign up to request clarification or add additional context in comments.

1 Comment

Great, you're welcome, feel free to mark it as the right answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.