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
相关产品推荐
相关产品推荐

