本文介绍了在 PHP 中嵌入 HTML 中实现内联三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PHP 的学习者,我有一段代码,我在其中实现了 HTML5 代码.我想要做的是在我的占位符中嵌入内联三元运算符.

I am a learner in PHP, and I have a code in which I am implementing HTML5 code. What I am trying to do is to embed the inline ternary operator in my placeholder.

我已经点击了这个链接,但没有一个有相同的问题陈述:

I have followed this link, but none of them are having same problem statement:

  1. 使用三元运算符 php 放置内联样式
  2. 内联 PHP/HTML 三元 If

我的代码:

echo "<input type='text' class='form-control' value='".$value."' placeholder='HERE I WANT TO OPERATE'>";

尝试过:我已经在我的代码中尝试过这个,但出现错误

Tried: I have tried this in my code, but I am getting errors

echo "<input type='text' class='form-control' value='".$value."' placeholder='"empty($value) ? 'Some Text' : 'Enter Data';"'>";

我知道如何在PHP的echo code之外进行三元运算,但我正在寻找这种方式.

I know how to do the ternary operation outside the echo code in PHP, but I am looking for this way.

推荐答案

三元运算将返回一个字符串.因此,它必须连接到字符串的其余部分.

The ternary operation will return a string. As such, it has to be concatenated to the rest of the string.

echo "<input type='text' class='form-control' value='".$value."' placeholder='". (empty($value) ? 'Some Text' : 'Enter Data') ."'>";

这篇关于在 PHP 中嵌入 HTML 中实现内联三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:45