Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP和HTML混合中添加if语句?_Php_Html - Fatal编程技术网

如何在PHP和HTML混合中添加if语句?

如何在PHP和HTML混合中添加if语句?,php,html,Php,Html,如果$\u GET['text'],我只想将数据prefilltext参数添加到按钮中“未定义”。如何做到这一点 HTML: id=“共享按钮” class=“g-interactivepost” data prefilltext=“如果您希望$\u GET['text']与字符串连接,则使用“未定义” 为了保持HTML的整洁,我可能会在实际的HTML按钮元素之外定义一个字符串,如下所示: <?php $prefilltext = isset($_GET['text']) ? 'data

如果
$\u GET['text'],我只想将
数据prefilltext
参数添加到按钮中“未定义”
。如何做到这一点

HTML:


id=“共享按钮”
class=“g-interactivepost”

data prefilltext=“如果您希望
$\u GET['text']
与字符串
连接,则使用
“未定义”


为了保持HTML的整洁,我可能会在实际的HTML按钮元素之外定义一个字符串,如下所示:

<?php $prefilltext = isset($_GET['text']) ? 'data-prefilltext="' . $_GET['text'] . '"' : ''; ?>

<button id="sharebutton" class="g-interactivepost" <?php echo $prefilltext;?>>
Send
</button>


您可以执行以下操作:

<button
id="sharebutton"
class="g-interactivepost"
<?php echo ($_GET['text'] != 'undefined')? "data-prefilltext=\"".$_GET['text']."\" ": "";?>
Send
</button>

<button
  id="sharebutton"
  class="g-interactivepost"
  <?php if (isset($_GET['text']) && !empty($_GET['text']) && $_GET['text'] !== 'undefined') { ?>
    data-prefilltext="<?php echo $_GET['text'];?>"
  <?php } ?>
>
  Send
</button>
<?php $prefilltext = isset($_GET['text']) ? 'data-prefilltext="' . $_GET['text'] . '"' : ''; ?>

<button id="sharebutton" class="g-interactivepost" <?php echo $prefilltext;?>>
Send
</button>
<button
id="sharebutton"
class="g-interactivepost"
<?php echo ($_GET['text'] != 'undefined')? "data-prefilltext=\"".$_GET['text']."\" ": "";?>
Send
</button>
<button
id="sharebutton"
class="g-interactivepost"
data-prefilltext="<?php if(isset($_GET['text']) && !empty($_GET['text']) { if($_GET['text'] == "undefined") { echo $_GET['text']; } else { // if text is not equal to undefined }?>"
Send
</button>