douzui6173 2017-10-05 14:59
浏览 23
已采纳

哪些字符可以使用简单的语法直接跟随解析的PHP变量?

For instance, the following will not print "hello john_doe" as desired, since $first_ is not defined.

$first = 'john';
$last = 'doe';

echo "hello $first_$last";

Maybe there are none?

  • 写回答

1条回答 默认 最新

  • doutao5499 2017-10-05 15:02
    关注

    Any character that cannot be part of a variable name can be used there - if the character can be part of the variable name, PHP cannot determine whether it's part of it or not...

    According to the manual:

    Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

    So, to answer your question: any character other than a letter, a number, or an underscore can immediately follow a parsed PHP variable using simple syntax...
    (Note that special characters like ƒ, ©, , etc... fall under that \x7f-\xff UTF-8 range in the regexp and are valid characters).

    With some exceptions - e.g., you can't use:

    • [ because it's considered an access to an array key.
    • {$ because it's considered as an attempt to use a variable (e.g., {$var}).
    • -> followed by a character that can be part of a variable, because it's considered an access to a property (e.g., ->prop).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成