单引号与双引号。

Everyone programming PHP or most of the other languages should at least understand the difference between the single quote and double quote.

每个使用PHP或其他大多数语言进行编程的人都应该至少了解单引号和双引号之间的区别。

in an coded example;

在一个编码示例中;

<?php
$a = 1;
echo '$a';   // String Literal
echo "$a";   // variable expansion and escape intepretation.
?>

Will output;

将输出;

$a1

$ a1

Where $a is the string literal ( echo '$a'; ) and 1 is the expanded $a that had the value 1.

其中$ a是字符串文字(echo'$ a';),1是扩展后的$ a,其值为1。

Logically using the " " will make php interpret the vars used inside the quotes, but another thing about PHP is that its weakly typed. This means we don't "declare" data-types. Now how will PHP perform using these two in conjunction.

从逻辑上使用“”将使php解释引号内使用的var,但是关于PHP的另一件事是其弱类型。 这意味着我们不“声明”数据类型。 现在,PHP将如何结合使用这两者来执行性能。

This means the Code we write is in fact valid PHP code, and as PHP is still weakly written we might measure the performance of the engine using the right code...

这意味着我们编写的代码实际上是有效PHP代码,并且由于PHP仍然编写得很差,我们可能会使用正确的代码来衡量引擎的性能...

So my first test was quite simple, i wanted to echo a string telling me '$x is not a declared var.... To test the difference i wrote the following code.

因此,我的第一个测试非常简单,我想回显一个字符串,告诉我“ $ x不是声明的var...。为了测试两者之间的区别,我编写了以下代码。

<?php 
// Example using double quotes 
$sm = microtime(true);
$data = "\$a is not a declared var";
$hm = microtime(true);
echo $data;
$time1 = $hm - $sm;
echo $time1; 
		
// Example using single qoutes 
$sm = microtime(true);
$data =  '\$a is not a declared var';
$hm = microtime(true);
echo $data;
$time2 = $hm - $sm;
echo $time2; 
$diff = $time1 - $time2; 
echo '<br>'.$diff; 
?>

Because micro time contains such small numbers other processes on the machine might actually be "notable" inside the results, so these are the results of a few refreshes.

由于微时间包含的数量如此之少,因此计算机内部的其他进程实际上可能在结果中“值得注意”,因此这些都是一些刷新的结果。

Amounts single ' where quicker then double "

数量单'快然后双'

1.0967254638672E-5

1.0967254638672E-5

1.0251998901367E-5

1.0251998901367E-5

8.1062316894531E-6

8.1062316894531E-6

1.215934753418E-5

1.215934753418E-5

If you ask me the difference in performance between singles and doubles in such a simple string are remarkable. Naturally the amount of work the PHP Engine needs to do to figure out what exactly is asked is immense in comparison.

如果您问我,在如此简单的字符串中单打和双打之间的表现差异是惊人的。 很自然,PHP引擎需要做的工作就是弄清楚到底要问什么才是比较大的工作。

Now this was the simple example of the escape char? now what about a string build-up we see allot inside various PHP applications? The one I am talking about is the famous double quotes inside SQL statements for example.

现在,这是转义字符的简单示例? 现在我们可以在各种PHP应用程序中看到分配的字符串了吗? 我正在谈论的是例如SQL语句中著名的双引号。

You prob. all have seen its structure somewhere.

你真有可能 所有人都在某个地方看到了它的结构。

<?php
$sql = "UPDATE table
        SET username = $username, password = $password
        WHERE userid = $userid";
if(mysql_query($sql)){
   \\....
?>

Well i wrote this in a way and measured the time php needed to execute the code. Here is the code I used.

好吧,我以某种方式编写了此文件,并测量了执行代码所需的php时间。 这是我使用的代码。

<?php 
$a = 1;
$b = 2;
$c = 3;
$d = 6; 
// Example using double quotes
$sm = microtime(true);
$data = "$a + $b + $c = $d <br />";
$hm = microtime(true);
echo $data;
$time1 = $hm - $sm;
echo $time1; 
echo '<br/>';
		
// Example using single qoutes
$sm = microtime(true);
$data =  $a . ' + ' . $b . ' + ' . $c . ' = ' . $d . '<br />';
$hm = microtime(true);
echo $data;
$time2 = $hm - $sm;
echo $time2; 
$diff = $time1 - $time2; 
echo '<br>'.$diff; 
?>

Well you might wonder about the results? Here are the measurements during a few refreshes.

好吧,您可能想知道结果吗? 这是一些刷新期间的测量值。

1.2874603271484E-5

1.2874603271484E-5

1.5974044799805E-5

1.5974044799805E-5

1.4305114746094E-5

1.4305114746094E-5

2.3841857910156E-5

2.3841857910156E-5

Conclusion.

结论。

Even though the differences are small, in huge applications it might be worth writing your code as clean as possible and it might improve the performance over time. Realize what the goal of each declaration is and use the best declaration method for that goal.

即使差异很小,但在大型应用程序中,可能值得将代码编写得尽可能干净,并且随着时间的推移可能会提高性能。 了解每个声明的目标是什么,并针对该目标使用最佳的声明方法。

Be honest, readability might be an issue but in the end it about how well your application performs and how stable your application operates, not how "readable" your code is composed. ;-)

老实说,可读性可能是一个问题,但最终它关系到应用程序的性能如何和应用程序的稳定性如何,而不是代码的“可读性”如何。 ;-)

Rgrds,

Rgrds,

翻译自: https://www.experts-exchange.com/articles/1342/Single-versus-double-quotes.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值