`
Odysseus_110
  • 浏览: 120473 次
  • 性别: 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

分享到:
评论

相关推荐

    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中,`json`模块提供了一种非常方便的方式来处理JSON数据,包括读取、写入...

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

    `coffeelint-prefer-double-quotes` 是一个针对CoffeeScript语言的开源库,它旨在帮助开发者遵循特定的编码规范,特别是倾向于使用双引号的规则。CoffeeScript是一种简洁、富有表达力的JavaScript方言,它在语法上...

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

    《PyPI官网下载:flake8-single-quotes-0.1.0.tar.gz——Python代码风格检查工具解析》 在Python编程领域,保持代码的一致性和规范性至关重要,这有助于提高代码可读性和团队协作效率。PyPI(Python Package Index...

    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.)

    Beginning PHP 5.3

    - **String Literals:** Explanation of different ways to define strings in PHP, including single quotes, double quotes, and heredoc syntax. - **String Functions:** Overview of common string functions, ...

    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, ...

    get_magic_quotes函数详解

    ### get_magic_quotes函数详解 在PHP环境中,处理输入数据的安全性是至关重要的,尤其是在Web开发领域。`get_magic_quotes_gpc()` 和 `get_magic_quotes_runtime()` 函数是PHP早期版本中用于自动转义用户提交的数据...

    JavaScript in 10 Minutes

    3. **Strings**: Represented using single or double quotes (e.g., `'foo'`, `"foo"`). Strings can be boxed and are instances of `String` when boxed. 4. **Numbers**: Represent all numbers as floating-...

    Unix shell programming in 24 hours.pdf

    - **Using Double Quotes** Double quotes `" "` preserve the text but allow for variable substitution and command substitution. - **Quoting Rules and Situations** Different situations require different ...

    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()函数说明

    在PHP编程语言中,`get_magic_quotes_gpc()`是一个非常重要的函数,主要用于处理用户通过GET、POST或COOKIE方式提交的数据。这个函数的核心作用是检查PHP的配置选项`magic_quotes_gpc`的状态,该选项决定了PHP是否会...

    The Underground PHP and Oracle Manual

    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); } ``` 2. **执行SQL查询**: - 一旦建立了连接,就可以执行各种SQL查询语句。 - 示例代码(使用OCI8扩展): ```php $stmt = oci_...

    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, ...

Global site tag (gtag.js) - Google Analytics