`
Odysseus_110
  • 浏览: 117090 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

PHP, The difference between single and double quotes

    博客分类:
  • PHP
php 
阅读更多

Well first of all, single quotes are much more efficient than double quotes, which is shown in the results from a simple test, which you can find here:http://phpbar.isgreat.org/viewtopic.php?f=2&t=56

So, there obviously is a difference between them, but what is it?

Simply put, single quotes are completely static, where as double quotes are dynamic with changing values.
For example:

CODE: SELECT ALL
$someVar = 'more Text';
echo 'Some Text $someVar';


will output

CODE: SELECT ALL
Some Text $someVar



However, the same example but with double quotes:

CODE: SELECT ALL
$someVar = 'more Text';
echo "Some Text $someVar";


will output

CODE: SELECT ALL
Some Text more Text



Obviously single quotes will have a better efficiency than double quotes, because in single quotes, php does not process anything within it, where as with double quotes, php is constantly looking through the string for variable names to check and call.

So how do we use this new knowledge to our advantage?

Well, since single quotes do not process variables how do we add variables to the string?
Well if you haven't figured it out, we simply use the string addition character.
For example:

CODE: SELECT ALL
$str = 'more text';
echo 'some text and '.$str;


It is a bit of a hassle, but like in most things about programming, its usually efficiency vs programing time. A classic example of this is, C++ vs C#.

However, when using single quotes, depending on your php version, '\n' will output \n, so you will occasionally have to use, "\n" or PHP_EOL

well, if you still want to use double quotes, because your lazy :D
there are still times when you may find it easier to use single quotes. for example, if you are outputting static text like HTML that has mass amounts of double quotes in it. Or if you want to output the name of a variable.

Of course, you could still do

CODE: SELECT ALL
echo "$"."varname";


instead of

CODE: SELECT ALL
echo '$varname';


but in that case you might as well just use single quotes.

---
So yes, there is a huge difference between the single and double quotes other than simple quote escaping.

Well, i hope this helps you a lot, and from a game programming experience, ive learned that it is best to try to be as efficient as possible, ESPECIALLY when using loops. so its alright to use double quotes when your lazy, but be sure to avoid them when writing large loops, or commonly used functions.

Enjoy

 

 所以为了效率,尽量多使用 单引号吧。

 

转自http://www.devppl.com/forum/post62676.html

分享到:
评论

相关推荐

    Financial econometric analysis at ultra-high frequency

    Data collection at ultra high-frequency on ... Overall the difference in the coefficients is bigger between the dirty series and the clean series than among series filtered with different algorithms.

    Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)

    主要给大家介绍了关于Python解析json报错:ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)的解决方法,文中介绍的非常详细,需要的朋友们可以参考借鉴,下面来一起看看...

    Python:Expecting property name enclosed in double quotes: line 1 column 2 (char 1)问题解决

    今天在处理 JSON 数据的时候遇到了一个意想不到的问题,不过也算是个小问题吧,记录一下解决方案,同时提醒自己下次不要犯同样的错误了。 目录 一、json (一)json.loads (二)json.dumps 二、问题 ...

    Invent Your Own Computer Games with Python (2008).pdf

    The Difference Between Statements and Expressions x "My Favorite Stuff" x Crazy Answers and Crazy Names for our Favorite Stuff x Capitalizing our Variables x Chapter 2 - Guess the Number x Source ...

    XMIExport.exe

    o launch the API, use the 'Run' API of the ... (No blanks are allowed between the '=' and the file name. The file is created if it does not exist. An existing file will be overwritten.)

    PyPI 官网下载 | flake8-single-quotes-0.1.0.tar.gz

    资源来自pypi官网。 资源全名:flake8-single-quotes-0.1.0.tar.gz

    UNIX SHELL Quote Tutorial

    Weak Quotes with the Double Quotes • Using quotes to include spaces and characters in filenames • Quotes within Quotes • Finding out if your quotes are wrong • The verbose and echo ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The PointOnLine() function calulations have "single" type numbers overflow problem (changed to "double"). - FIX: The pfJoin and pfClose flags incorrectly calculates in GetEditPathCaps(). ...

    微软内部资料-SQL性能优化3

    Note the differences between Key and Key Range locks. Key Range locks will be covered in a couple of slides. SQL Server can lock these resources: Item Description DB A database. File A database file ...

    Digital Asset Management

    The difference between DAMs, CMSs, and WCMs How to identify the need for a DAM, and how to conduct a needs assessment Why there is no single best DAM solution for every need How to discuss servers, ...

    to-single-quotes-cli:将匹配的双引号转换为单引号

    单引号cli 将匹配的双引号转换为单引号: I "love" unicorns → I 'love' unicorns安装$ npm install --global to-single-quotes-cli用法$ to-single-quotes --help Usage $ to-single-quotes <string> $ echo ...

    php中get_magic_quotes_gpc()函数说明

    get_magic_quotes_gpc函数是一个用来判断是否为用户提供的数据增加斜线了,这个在php.ini配置文件中哦,下面我来介绍一下get_magic_quotes_gpc()函数说明. get_magic_quotes_gpc函数介绍 取得 PHP 环境变数 magic_...

    前端开源库-coffeelint-prefer-double-quotes

    前端开源库-coffeelint-prefer-double-quotescofeelint更喜欢双引号,自定义cofeelint规则更喜欢双引号

    ag_news文本分类数据集

    The title and description are escaped using double quotes ("), and any internal double quote is escaped by 2 double quotes (""). New lines are escaped by a backslash followed with an "n" character, ...

    曲线拟合工具CurveExpert 1.0

    the single-precision and double-precision versions (registered version only) Bug Fixes: + Periods by themselves in the user models were parsed as the value zero instead generating a syntax ...

    Handbook of Real-Time and Embedded Systems

    time and embedded systems are essential to our lives, from controlling car engines and regulating traffic lights to monitoring plane takeoffs and landings to providing up-to-the-minute stock quotes....

    PHP的单引号和双引号 字符串效率

    php $single_quotes = ‘This is a String’; $double_quotes = “This is a String”; echo $single_quotes; echo $double_quotes; $var = ‘String’; $single_quotes_var = ‘This is a ‘.$var; $double_quotes_...

    ag_news数据集

    The title and description are escaped using double quotes ("), and any internal double quote is escaped by 2 double quotes (""). New lines are escaped by a backslash followed with an "n" character, ...

    ag_news_csv.tgz

    The title and description are escaped using double quotes ("), and any internal double quote is escaped by 2 double quotes (""). New lines are escaped by a backslash followed with an "n" character, ...

    DevC++记事本高级版

    应粉丝需求,穿了一下exe。 ...spm=1001.2014.3001.5502 Version 6.7.5 AUGUST 2021 - add: blend mode for debug cpu window - change: use msys2 x86_64 gcc ... - fix: parse error when single quotes not correctl

Global site tag (gtag.js) - Google Analytics