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部分字符串之间用”.” 来连接
====
一.首先想想PHP里所有的单词(其实应该叫符号)有几类.
1.PHP,mysql两方的关键词与函数.例如echo,print,mysql_connect等等.这些肯定不加引号的.
2.常量.新手可能用得不多,常量的好处是全局性,穿透函数.速度也快些不过新手可以暂时不管常量这玩意儿.
3.变量.前面带"$"号的就是变量.可以为变量设一个"值",例如一串字符,一个数字,逻辑(真/假)值等.也可以表示一组值(数组,对象等)
4.值.通常要给常量与变量设置值.赋值语句$a=\'abc\'中,右边的\'abc\'即为值.
5.函数的参数(在括号里的).可以是常量,变量,值三种.
变量(常量)与值的关系正如下列各种情况.
"颜色"与"红",
"长度"与100,
"日期"与2007年10月25号"
二.什么情况下用PHP引号
其实只有第4项"值"需要用到引号,函数的里也只有值要用引号.并且只有字符串(日期值可以当成字符串)内容需要用到引号.数字(可用可不用),真假(不能用)例外.
例子
三.单引号与双引号的区别
一般情况下两者是通用的.但双引号内部变量会解析,单引号则不解析.
例子
所以如果内部只有纯字符串的时候,用单引号(速度快),内部有别的东西(如变量)的时候,用双号引更好点.
四.字符串内部如果出现PHP引号怎么办--关于转义.
比如我们想输出: 我"是\'天才
这时候就必须用到转义了.转义即把本来有作用的符号转成无意义的字符.
这样就正常了,因为号把它后面的任何字符都转成无意义的符号.在这里来说,PHP解析器根本没把号后面的引号当成引号来看待.
同样的,还可以转义分号,$符号等特殊符号.
五.字符串的连接.
这是个麻烦的问题.一般来说,变量值,直接包含在双引号中就可以了.另外字符串的叠加用"."字符.
在复杂的情况里可以用大括号来包含,PHP便知道这是一个完整的东西,里面的引号不会影响到外面的引号关系.
与html的混合也很简单,最好养成HTML中全部用双引号,PHP中尽量用单引号的习惯.这样方便把大段的HTML代码复制过来,只要头尾加上单引号就是一个正确的字符串了.几百行的HTML代码也不用担心PHP引号错乱.
总结一下PHP引号使用原则
1.字符串的值用引号
2.PHP中尽量用单引号,HTML代码全部用双引号
3.在包含变量的时候,用双引号可以简化操作
4.复杂的情况下用大括号包起来

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Created by Ripple, Ripple is used for cross-border payments, which are fast and low-cost and suitable for small transaction payments. After registering a wallet and exchange, purchase and storage can be made.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.
