• PHP单引号和双引号区别


      原文地址:http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2009/0828/3240.html

     

      单引号内部的变量不会执行

      双引号会执行

      如

      $name = 'hello';

      echo "the $name";

      会输出 the hello

      而如果是单引号

      $name = 'hello';

      echo 'the $name';

      会输出 the $name

      主要区别就是这个。

     

    其实在之前的PHP100视频教程里我就讲过,单引号和双引号的区别和效率问题,但还是很多朋友了解的不是很清楚,一直以为PHP中单引号和双引号是互通的,直到有一天,发现单引号和双引号出现错误的时候才去学习研究。所以今天再拿出来谈谈他们的区别,希望大家不要再为此困惑。

    ” ” 双引号里面的字段会经过编译器解释,然后再当作HTML代码输出。

    ‘ ‘ 单引号里面的不进行解释,直接输出。

    从字面意思上就可以看出,单引号比双引号要快了。

    例如:
    $abc=’my name is tome’;
    echo $abc //结果是:my name is tom
    echo ‘$abc’ //结果是:$abc
    echo “$abc” //结果是:my name is tom

    特别在使用MYSQL语句的时候,双引号和单引号的用法让新手不知所措,在这里,举个例子,来进行说明。

    假设查询条件中使用的是常量,例如:

    select * from abc_table where user_name=’abc’;

    SQL语句可以写成:

    SQLstr = “select * from abc_table where user _name= ‘abc’” ;

    假设查询条件中使用的是变量,例如:

    $user_name = $_REQUEST['user_name']; //字符串变量

    $user=array (”name”=> $_REQUEST['user_name‘,"age"=>$_REQUEST['age'];//数组变量

    SQL语句就可以写成:

    SQLstr = “select * from abc_table where user_name = ‘ ” . $user_name . ” ‘ “;

    SQLstr = “select * from abc_table where user_name = ‘ ” . $user["name"] . ” ‘ “;

    对比一下:

    SQLstr=”select * from abc_table where user_name = ‘ abc ‘ ” ;

    SQLstr=”select * from abc_table where user_name =’ ” . $user _name . ” ‘ “;

    SQLstr=”select * from abc_table where user_name =’ ” . $user["name"] . ” ‘ “;

    SQLstr可以分解为以下3个部分:
    1:”select * from table where user_name = ‘ ” //固定SQL语句
    2:$user //变量
    3:” ‘ ”
    1,2,3部分字符串之间用”.” 来连接

  • 相关阅读:
    C++ CGI Helloword
    国内外12个免费域名解析服务网站推荐
    U制作LFS linux
    LFS 中文版手册发布:如何打造自己的 Linux 发行版
    windows下的BT服务器搭建方案
    Linux下搭建BT服务器
    Codeforces 842B Gleb And Pizza【几何,水】
    Codeforces 842A Kirill And The Game【暴力,水】
    Wannafly模拟赛 A.矩阵(二分答案+hash)
    数据结构学习笔记【持续更新】
  • 原文地址:https://www.cnblogs.com/wanyakun/p/1904005.html
Copyright © 2020-2023  润新知