kennedy32
V2EX  ›  PHP

关于/n及?:

  •  
  •   kennedy32 · Aug 23, 2013 via Android · 4253 views
    This topic created in 4821 days ago, the information mentioned may be changed or developed.
    /n在html无法输出的话,为什么不直接br呢,费事还要回头nl2br

    一句话if很简短,为什么看很多人的代码都没用,很少见的感觉。
    20 replies  •  1970-01-01 08:00:00 +08:00
    siw
    siw
    Aug 23, 2013
    1
    在console里就用\n
    在网页里就用<br />

    你的输出是网页还是console, 就用哪个。。。
    rwx
    rwx
    Aug 23, 2013
    2
    \n吧?在我看来输出\n的唯一用处是生成的html可读性会好一点,因为会有换行,但在生产中真没见谁用过,基本都是模板了,而php又是最好的模板语言。。
    三元运算符很常见啊,我以前就偏爱用这个,以至于现在用go的时候对于一行能搞定的东西写成四行极其不爽。。
    siw
    siw
    Aug 23, 2013
    3
    如果写四行代码写多了, 就真的只是那种四行代码, 你可能会感谢支持? 功能
    Keyes
    Keyes
    Aug 23, 2013
    4
    经过爬虫程序的蹂躏,私以为正确的\n替换法应该是酱紫:
    1、查找\r\n替换为<br />
    2、查找\r替换为<br />
    3、查找\n替换为<br />
    rayhome1987
    rayhome1987
    Aug 23, 2013
    5
    请使用PHP_EOL
    msg7086
    msg7086
    Aug 23, 2013
    6
    if只能执行语句,三元运算符是表达式。

    比如极端点的例子,这样一句话

    $data[$idx1][$i]->payload[2] = $core->ProcessData(isset($_POST['type']) ? 'a' : 'b') . PHP_EOL;

    你改写成if就会很不方便。要么额外声明变量,要么就得repeat code
    thbourlove
    thbourlove
    Aug 23, 2013
    7
    其实 ?: 比 if..else.. 慢。。。虽然能用的时候还是会去用 ?:
    nybux
    nybux
    Aug 23, 2013
    8
    @thbourlove 误导群众呀,?:会编译成cmov,可以避免分支惩罚
    thbourlove
    thbourlove
    Aug 24, 2013
    9
    @nybux 如果是我使用 ?: 的姿势不对,我道歉。
    benchmark在此:https://gist.github.com/thbourlove/6325340
    thbourlove
    thbourlove
    Aug 24, 2013
    10
    补一个可以直接运行的,结果略有误差,但跟上面差不了太多。
    https://gist.github.com/thbourlove/6325403
    vibbow
    vibbow
    Aug 24, 2013
    11
    @thbourlove 结果和你的刚好相反

    C:\Program Files (x86)\PHP\php.exe C:\Users\Administrator\Documents\NetBeansProjects\test\index.php
    0.013000965118408
    0.018001079559326
    完成。

    PHP 5.4.18 @ Windows 7
    thbourlove
    thbourlove
    Aug 24, 2013
    12
    @vibbow 直接运行的上面这段代码?
    vibbow
    vibbow
    Aug 24, 2013
    13
    vibbow
    vibbow
    Aug 24, 2013
    14
    卧槽,PHP 5.4.19肯定rollback什么代码了,执行那段代码耗时比php 5.4.18长了。
    但是也只是把两种方法的执行时间都拖到了0.017s左右。
    并没有体现出哪种方法更快。
    thbourlove
    thbourlove
    Aug 24, 2013
    15
    @vibbow 我是 PHP5.5 @ Mac。刚才又在虚拟机( PHP 5.4 @ ubuntu )下面测了一下,结果是反过来了
    0.023139953613281
    0.027631998062134
    thbourlove
    thbourlove
    Aug 24, 2013
    16
    @vibbow 小版本号之间也会有这么大差别??我再试试。。
    vibbow
    vibbow
    Aug 24, 2013
    17
    @thbourlove 5.4.18 是个悲剧的版本
    只有windows版,没有源代码版
    vibbow
    vibbow
    Aug 24, 2013
    18
    不过在这玩意兼容PHP 5.5之前,我升级到PHP 5.5的概率很渺茫了
    http://www.microsoft.com/en-us/download/details.aspx?id=20098
    thbourlove
    thbourlove
    Aug 24, 2013
    19
    @vibbow 5.5 也只是自己电脑上随便玩玩的时候才用。生产环境和开发环境依然还是 php5.4,没有吃螃蟹的爱好。。
    yinian1992
    yinian1992
    Aug 24, 2013 via Android
    20
    white-space 是干吗的?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2562 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 15:03 · PVG 23:03 · LAX 07:03 · JFK 10:03
    ♥ Do have faith in what you're doing.