PHP 基础问题

WBOY
Release: 2016-06-23 14:30:49
Original
1091 people have browsed it

最近在接触PHP, 感觉挺灵活的。变量这个东西有时就用不好,麻烦能高手指点迷津。
  $userID=1;
  $str='{"userID":'.$userID.'}'; 

  可以这样去写吗:
  $str='{"userID":$userID}';是否为自动识别变量并做值替换?
   
  如果不可以,但好像在mysql中又可以:
  $sql ="insert into user(userID) values($userID)";


  什么时候可以用,什么时候不能用?

答案:

 变量标示符$可以在双引号中直接使用,比如

PHP code
            <p class="sycode">                  $str           =           "     {'userID':      $userID     }     "     ;      //     这里的$userID就是指变量            </p>
Copy after login


但是$符在单引号中就是被当做字符处理而不是变量标示符

PHP code
            <p class="sycode">                  echo           '     $userID     '     ;      //      输出字符串$userID,而不是输出1            </p>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template