下面这个html字符串,如何用php的 echo或print来输出?

下面这个html字符串,如何用php的echoprint来输出?

<td width=275><input type="text" name="title" value="<?php echo $row['title']; ?>"></td>  
echo "<td width=275><input type="text" name="title" value="<?php echo $row['title']; ?>"></td>"

不可以

echo '<td width=275><input type="text" name="title" value="<?php echo $row['title']; ?>"></td>'

也不可以

print <<<EOT
<td width=275><input type="text" name="title" value="<?php echo $row['title']; ?>"></td>
EOT;

也不可以,应为,这段html还包含php语句。

我的真实需求是

<?php
if(isset($_POST['flag'])
{ 
print <<<EOT
<td width=275><input type="text" name="title" value="<?php echo $row['title']; ?>"></td>
EOT;
else 
{

} 
?>
阅读 3.2k
3 个回答

2种实现方式

1.把html代码直接写在页面,与php标签分开

<?php
//你的其他php代码
$row = ['title' => '123'];
?>
<td width=275><input type="text" name="title" value="<?= $row['title']; ?>"></td>
<?php
//你的其他php代码
?>

2.html采用PHP的字符串内赋值

echo "<td width=275><input type=\"text\" name=\"title\" value=\"{$row['title']}\"></td>"

<?php
$_POST['flag'] = true;
$row = ['title' => '111', 'ugly' => '222'];
?>

<?php if (isset($_POST['flag'])): ?>
  <td width=275>
    <input type="text" name="title" value="<?= $row['title']; ?>">
  </td>
<?php else: ?>
  <td width=275>
    <input type="text" name="title" value="<?= $row['ugly']; ?>">
  </td>
<?php endif; ?>

1并不是不能满足你需求

//方法1
echo '<td width=275><input type="text" name="title" value="'.$row['title'].'"></td>';

//方法1变形体
$a = '<td width=275><input type="text" name="title" value="';
$a .= $row['title'];
$a .= '"></td>';
echo $a;

//方法2
echo
<<<EOT
<td width=275><input type="text" name="title" value="{$row['title']}"></td>
EOT;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题