You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP新手如何修复WhatsApp链接生成的字符串拼接语法错误

错误原因

  • 你在PHP字符串赋值语句中嵌套了<?= ?>短输出标签,属于语法错误:该标签只能用在脱离PHP解析的HTML代码段中,不能写在已经处于PHP上下文的字符串里。
  • 混用了字符串拼接符:PHP中字符串拼接用.,+是数字运算符,用于字符串会触发类型转换报错。

修复方案

直接按照PHP字符串拼接规则构造链接即可,正确代码如下:

$tempRow['whatsapp'] = "<a href='https://api.whatsapp.com/send?phone=" . $row['mobile'] . "' target='_blank' title='Send Whatsapp Notification'> Send Whatsapp Notification</a>";

也可以利用双引号变量解析特性简化写法,不需要额外拼接:

$tempRow['whatsapp'] = "<a href='https://api.whatsapp.com/send?phone={$row['mobile']}' target='_blank' title='Send Whatsapp Notification'> Send Whatsapp Notification</a>";

可选优化

建议对手机号做URL编码处理,避免手机号中存在+、空格等特殊字符时链接失效:

$tempRow['whatsapp'] = "<a href='https://api.whatsapp.com/send?phone=" . urlencode($row['mobile']) . "' target='_blank' title='Send Whatsapp Notification'> Send Whatsapp Notification</a>";

内容的提问来源于stack exchange,提问作者user1764481

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 09:39:02