doushou8730 2013-02-01 11:37
浏览 87
已采纳

在PHP中使用html和javascript标签

I am confused about using single and double quotes and back slash while using java script and html tags in php can any one please clarify i googled it but still not clear about it. i am confused for this small thing.i am new to programming - here is my code

 <?php
if(isset($_GET['id'])) {
echo '<div id="d2">';
include "test2.php";
 echo '</div>'; }
 else
 { 
  echo '<div id="d1">';
 include "insert.php";
 print "<script type=javascript>"
 print "document.getEelementById('alertdiv1').innerHTML='hi' ;"
 print "</script>"
echo '</div>';
     }
?>
  • 写回答

5条回答 默认 最新

  • dongsha7215 2013-02-01 11:43
    关注

    In PHP, you can enclose a string in either single quotes or double quotes. Both are valid:

    $var = "this is a string";
    $var2 = 'this is also a string';
    

    The main difference is that if your string contains a variable, and you want the variable content to be treated as part of the string, you need to use double quotes:

    echo "$var which I made";
    

    will return:

    this is a string which I made

    When you are manipulating html, css and JavaScript strings, you need to make sure that you don't accidentally close your PHP string. For example:

    echo "<h1 class='myheading'>Heading Text</h1>";
    

    Notice how I used double quotes to enclose my string? Because I did that, I was able to use single quotes in the html, without escaping them.

    If I'd wanted to use double quotes in my string, I would have had to escape them, like this:

    echo "<h1 class=\"myheading\">Heading Text</h1>";
    

    The \ tells PHP that the double quote which immediately follows is to be treated as a literal, and not used to terminate the string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题