dsxay48646 2017-10-12 10:06
浏览 7

PHP变量中的撇号中断代码[重复]

This question already has an answer here:

I have created a PHP variable:

$msg_gifted='<center><h1>It's a gift! </h1>';

If I leave it like this the code is breaking.

When I replace the apostrophe with:

$msg_gifted='<center><h1>It is a gift! </h1>';

everything works fine. Is there a known way to solve this?

</div>
  • 写回答

2条回答 默认 最新

  • dongzhenshen7435 2017-10-12 10:08
    关注

    you can do

    $msg_gifted="<center><h1>It's a gift! </h1>";
    

    or

    $msg_gifted='<center><h1>It\'s a gift! </h1>';
    

    Updated answer

    You can not use the same quote inside the string without escaping the quote. So you either use different quotes surrounding the string or you escape it with \. Simplified your code to show different possibilites which will all have the same output.

    String in single ' quotes

    $msg = '<h1>It\'s a gift!</h1><img src="http://path-to-image.jpg" />';
    

    which is almost the same as tring in double " quotes

    $msg = "<h1>It's a gift!</h1><img src=\"http://path-to-image.jpg\" />";
    

    Or string concatenation with different quotes just as you prefer:

    $msg = "<h1>It's a gift!</h1>";
    $msg.= '<img src="http://path-to-image.jpg" />';
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度