duanbi2003 2017-04-04 04:03
浏览 55
已采纳

高级php格式化需要帮助理解布局

Here is the function:

function simplePresent($e) {

    $w = ($f = preg_match)('/ey|se|d |[sI]$|We/', $a = $e[0]);

    be == ($b = $e[1])
    ?
    $b = $w ? $a == I ? am : are : is
    :
    $w ?: $b = $b == have ? has : $b .= $f('/[h-z]$/', $b) ? es : s;

    return "$a $b $e[2]";
}

this is a solution on codefights. it handles formatting under defined parameters. I am trying to understand the function assignment to the variables $f and $w. Also the the use of variables with no quotes. lastly the nested question marks and colons. How is this functioning?

Thanks

  • 写回答

1条回答 默认 最新

  • dsvtnz6350 2017-04-04 04:21
    关注

    The function preg_match() is being assigned to the variable $f and reused later in the code. $w is simply the result of preg_match(). For example:

    <?php
    $w = ($f = 'sprintf')("foo");
    // same as this:
    $f = 'sprintf';
    $w = $f("foo");
    // which is the same as this:
    $w = sprintf('foo');
    

    The unquoted values will first be interpreted as constants, and the undefined constants will be interpreted as strings. Here's how a proper constant definition looks:

    <?php
    define("foo", "bar");
    echo foo;
    echo bar;
    // PHP Notice:  Use of undefined constant bar - assumed 'bar'
    
    // same as this:
    echo "bar";
    echo "bar";
    

    The question marks and colons are part of a ternary statement. This is a useful shorthand, but PHP recommends against stacking them together as is done there (the result is "non-obvious.") A simple one looks like this:

    <?php
    echo ($foo == "foo" ? "equal" : "inequal");
    $a = $b ? $c : $d;
    
    // this is the same as:
    if ($foo == "foo") {
        echo "equal";
    } else {
        echo "inequal";
    }
    if ($b) {
        $a = $c;
    } else {
        $a = $d;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)