如何为 php 文件中的 html 元素编写 class 属性?

How to write class attribute for html element in php file?

无法在 php 文件中为 html 元素写入 class 属性。 这是我的代码

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

如果条件为真,我的输出应该是 class 否则 class 应该是空的,但我得到的输出是必需的文本而不是输入框。

我得到的结果是

试试这个,你应该把它括在括号中:

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