用户评论:

[#1]

aluciffer at hotmail dot com [2014-10-10 09:27:28]

Regarding character incrementing and PHP following Perl's convention with character operations.

Actually i found that there is a difference, and incrementing and decrementing unfortunately does not yield the reverse, expected results.

For example, the following piece of code:

for ($n=0;$n<10;$n++) {

echo ++$s.' ';

}

echoPHP_EOL;

for ($n=10;$n>0;$n--) {

echo (--$s) .' ';

}?>

Will output:

== Alphabets ==

X Y Z AA AB AC AD AE AF AG

AG AG AG AG AG AG AG AG AG AG

Please note that the decrement operator has no effect on the character or string.

On the other hand, in Perl, the similar script:

#!/usr/bin/perl

my $s = 'W';

foreach (1 .. 10) {

print  ++$s . " ";

}

print "\n";

foreach (1 .. 10) {

print --$s . " ";

}

Will output:

X Y Z AA AB AC AD AE AF AG

-1 -2 -3 -4 -5 -6 -7 -8 -9 -10

[#2]

michiel ed thalent circle nl [2012-08-24 07:39:15]

BEWARE:

If incrementing an uninitialized variable you will not get an E_NOTICE error. This may caused you to not find issue's like the visibility of a property.

private$foo=1;

}

classbextendsa{

public functioninc() {

echo ++$this->foo;

}

}$b= newb;$b->inc();?>

Will output 1 and not 2 (if $foo was accessible).

Also no notices are given.

[#3]

hartmut at php dot net [2012-08-02 15:28:54]

Note that

$a="9D9"; var_dump(++$a);   => string(3) "9E0"

but counting onwards from there

$a="9E0"; var_dump(++$a);   => float(10)

this is due to "9E0" being interpreted as a string representation of the float constant 9E0 (or 9e0), and thus evalutes to 9 * 10^0 = 9 (in a float context)

[#4]

johnnythan at nospam dot gmx dot com [2011-11-22 04:05:09]

If you have a trailing zero and use the increment, the trailing zero will not remain. Was at least unexpected for me at first, although it's logical if you think about it.

$start='01';$start++;

print$start;//Outputs '2' not '02'?>

[#5]

Brad Proctor [2010-11-06 20:51:21]

I ran some tests (on PHP 5.3.3) of my own and was surprised to find $i += 1 to be the fastest method of incrementing.  Here are the methods fastest to slowest:

$i += 1;

++$i;

$i++;

$i = $i + 1;

[#6]

dsbeam at gmail dot com [2009-08-31 15:35:24]

When using the ++ operator by itself on a variable, ++$var is faster than $var++ and uses slightly less memory (in my experiments).  It would seem like this could be optimized in the language during runtime (if $var++ is the only thing in the whole statement, it could be treated as ++$var).

I conducted many tests (I believe to be fair), and here's one of the results:

$i++ took 8.47515535355 seconds and 2360 bytes

++$i took 7.80081486702 seconds and 2160 bytes

Here's my code.  If anyone sees a bias in it, tell me.  I conducted it many times, each time going through a loop one million iterations and doing each test 10 - 15 times (10 - 15 million uses of the ++ operator).

ini_set('MAX_EXEC_TIME',120);ob_start( );$num_tests=10;$startFirst=$startSecond=$endFirst=$endSecond=$startFirstMemory=$endFirstMemory=$startSecondMemory=$endSecondMemory=$someVal=0;$times= array('$i++'=> array('time'=>0,'memory'=>0),'++$i'=> array('total'=>0,'memory'=>0) );

for($j=0;$j

{

for($i=0,$startFirstMemory=memory_get_usage( ),$startFirst=microtime(true);$i<10000000;$i++ ){$someval=2; }$endFirstMemory=memory_get_usage( );$endFirst=microtime(true);

for($i=0,$startSecondMemory=memory_get_usage( ),$startSecond=microtime(true);$i<10000000; ++$i){$someval=2; }$endSecondMemory=memory_get_usage( );$endSecond=microtime(true);$times['$i++'][$j] = array('startTime'=>$startFirst,'endTime'=>$endFirst,'startMemory'=>$startFirstMemory,'endMemory'=>$endFirstMemory);$times['++$i'][$j] = array('startTime'=>$startSecond,'endTime'=>$endSecond,'startMemory'=>$startSecondMemory,'endMemory'=>$endSecondMemory);

}

for($i=0;$i

{$times['$i++']['time'] += ($times['$i++'][$i]['endTime'] -$times['$i++'][$i]['startTime'] );$times['++$i']['time'] += ($times['++$i'][$i]['endTime'] -$times['++$i'][$i]['startTime'] );$times['$i++']['memory'] += ($times['$i++'][$i]['endMemory'] -$times['$i++'][$i]['startMemory'] );$times['++$i']['memory'] += ($times['++$i'][$i]['endMemory'] -$times['++$i'][$i]['startMemory'] );

}

echo'There were '.$num_tests.' tests conducted, here\'s the totals

$i++ took '.$times['$i++']['time'] .' seconds and '.$times['$i++']['memory'] .' bytes

++$i took '.$times['++$i']['time'] .' seconds and '.$times['++$i']['memory'] .' bytes';ob_end_flush( );?>

Try it yourself, ;)

[#7]

sneskid at hotmail dot com [2009-08-07 15:49:23]

(related to what "Are Pedersen" wrote)

With arrays it can lead to much confusion if your index variable is altered on the right side of the = sign, either with ++|-- or even when passed to a function by reference..

Consider these (PHP 5):

$A[$a] = ++$a;// [1]=1$B[++$b] = ++$b;// [1]=2$C[$c+=0] = ++$c;// [0]=1?>

In 'A' you have to be aware that PHP evaluates $A[$a] last.

Yet in 'B' and 'C' PHP evaluates the index and saves it in a temporary variable.

You can always force PHP to evaluate a variable without explicitly storing it as a named variable first, with a simple "+=0" like in example 'C'.

Compared to 'A', 'C' gives the more logically expected result, when we expect evaluation occurs left to right.

PHP does evaluate left to right BUT it will attempt to cut down on temporary variables, which can lead to confusing results.

So just be aware and use either behavior to your advantage for the desired functionality.

[#8]

cleong at letstalk dot com [2001-10-17 19:52:17]

Note that the ++ and -- don't convert a boolean to an int. The following code will loop forever.

function a($start_index) {

for($i = $start_index; $i

}

a(false);

This behavior is, of course, very different from that in C. Had me pulling out my hair for a while.

[#9]

fred at surleau dot com [2001-07-18 12:02:19]

Other samples :

$l="A";      $l++; -> $l="B"

$l="A0";     $l++; -> $l="A1"

$l="A9";     $l++; -> $l="B0"

$l="Z99";    $l++; -> $l="AA00"

$l="5Z9";    $l++; -> $l="6A0"

$l="9Z9";    $l++; -> $l="10A0"

$l="9z9";    $l++; -> $l="10a0"

$l="J85410"; $l++; -> $l="J85411"

$l="J99999"; $l++; -> $l="K00000"

$l="K00000"; $l++; -> $l="K00001"

php 递增函数,递增/递减运算符 - [ php中文手册 ] - 在线原生手册 - php中文网相关推荐

  1. cbrt c语音_sqrt - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

    在头文件中定义float sqrtf(float arg);(1)(自C99以来) double sqrt(double arg);(2) long double sqrtl(long double ...

  2. php recordarray,Array 数组 - [ php中文手册 ] - 在线原生手册 - php中文网

    用户评论: [#1] florenxe [2015-10-07 18:53:45] //a nice little way to print leap years using array for ($ ...

  3. php gridview,GridView - [ Android中文手册 ] - 在线原生手册 - php中文网

    GridView 版本:Android 2.2 r1 public final class GridView extendsAbsListView java.lang.Object android.v ...

  4. c语言fsetpos是什么,fsetpos - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

    在头文件中定义int fsetpos(FILE * stream,const fpos_t * pos); stream根据指向的值设置文件流的文件位置指示符和多字节解析状态(如果有)pos. 除了建 ...

  5. c语言中mw shl code,cacoshl - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

    在头文件中定义float complex       cacoshf( float complex z );(1)(since C99) double complex      cacosh( dou ...

  6. php vprintf,vprintf - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

    格式-指向以空字符结尾的字符串的指针,指定如何解释数据.格式字符串由普通的多字节字符(%除外)组成,它们被原样复制到输出流和转换规范中.每个转换规范具有以下格式:介绍%字符(可选)一个或多个标志,用于 ...

  7. cbrt c语音_isgraph - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

    在头文件中定义int isgraph(int ch); 检查给定字符是否具有图形表示形式,即它是数字(0123456789),大写字母(ABCDEFGHIJKLMNOPQRSTUVWXYZ),小写字母 ...

  8. c语言L文件,frexpl - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

    在头文件中定义float frexpf(float arg,int * exp);(1)(自C99以来) double frexp(double arg,int * exp);(2) long dou ...

  9. android gravity参数,Gravity - [ Android中文手册 ] - 在线原生手册 - php中文网

    Gravity 版本:Android 4.0 r1 结构 继承关系 public class Gravity extends Object java.lang.Object android.view. ...

最新文章

  1. hdu2276 矩阵构造
  2. gazebo仿真环境加载多个机器人
  3. mutex的加锁与解锁问题
  4. linux cut 命令(转)
  5. 第2讲 | 网络分层的真实含义是什么?
  6. where is os type and version determined for a ui5 html
  7. 楼梯计算机公式,各种楼梯面积的计算公式汇总
  8. Web安全笔记-Fidder与浏览器找关键Cookie(Cookie劫持前的准备)
  9. Qt实现桌面右下角放置窗体
  10. 写html前端代码的软件_你能看懂高贵的前端程序员的工作内容?
  11. c# 以太坊代币_C代币
  12. C/C++学习笔记: 字符串匹配Sunday算法
  13. aruino四轮蓝牙小车控制
  14. Go 爬虫软件 Pholcus
  15. 3D建模师会因为年龄大而失业吗?30岁了还能学习游戏建模吗?
  16. 颈椎护理小助手,轻松缓解颈部酸痛,宾多康智能颈枕按摩器体验
  17. Python爬虫 - scrapy - 爬取妹子图 Lv1
  18. java sql 违反协议_SQLException:违反协议。Oracle JDBC驱动程序问题
  19. 中国营销杀手独门暗器揭秘
  20. 大觉寺到鹫峰线路_体验古香道---从大觉寺过鹫峰到妙峰山涧沟村

热门文章

  1. 【谷粒商城】分布式事务与下单
  2. 很久很久以前写的,博客转啦,放这里吧
  3. Apache Geronimo 介绍
  4. 智伴机器人或阿尔法蛋_阿尔法蛋S为何销量第一,看完这篇儿童智能机器人的测评就懂了...
  5. 基于51单片机的语音采集系统设计(录音笔选择方案)
  6. 社区表情包总结,看完不动心算我输!
  7. 转变范式:如何使用 5 种新模式重塑 2023 年的实体店体验
  8. 文件上传到ftp服务器大小变小,ftp服务器文件上传大小设置
  9. Flash骨骼绑定做动画
  10. 电压放大器在混凝土组合结构界面损伤检测研究中的应用