代码之家  ›  专栏  ›  技术社区  ›  AndreaNobili

这个PHP插入方法到底是如何工作的?为什么是这种语法?[副本]

  •  -3
  • AndreaNobili  · 技术社区  · 6 年前

    这是什么?

    这是一组时不时出现的关于PHP语法的问题。这也是一个社区Wiki,因此邀请每个人都参与维护此列表。

    为什么会这样?

    以前很难找到关于运算符和其他语法标记的问题。
    其主要思想是提供有关堆栈溢出的现有问题的链接,这样我们就更容易引用它们,而不是复制PHP手册中的内容。

    注:自2013年1月起,堆栈溢出 does support special characters [php] "==" vs "==="

    我该怎么办?

    PHP manual

    名单

    如果您的特定令牌未在下面列出,您可能会在 List of Parser Tokens .


    & Bitwise Operators 或 References


    =& 工具书类


    &= 位运算符


    && Logical Operators


    % Arithmetic Operators


    !! 逻辑运算符


    @ Error Control Operators


    ?: Ternary Operator


    ?? Null Coalesce Operator (自PHP 7起)


    ?string ?int ?array ?bool ?float Nullable return type declaration


    : Alternative syntax for control structures , 三元运算符


    :: Scope Resolution Operator


    \ Namespaces


    -> Classes And Objects


    => Arrays


    ^ 位运算符


    >>


    << 位运算符


    <<< Heredoc or Nowdoc


    = Assignment Operators


    == Comparison Operators


    ===


    !== 比较运算符


    !=


    <> 比较运算符


    <=> (自PHP 7.0以来)


    | 位运算符


    ||


    ~ 位运算符


    + 算术运算符 Array Operators


    += -=


    ++ 和 -- Incrementing/Decrementing Operators


    .= 赋值运算符


    . String Operators


    , Function Arguments

    , Variable Declarations


    $$ Variable Variables


    ` Execution Operator


    <?= Short Open Tags


    [] (自PHP 5.4以来的简短语法)


    <? Opening and Closing tags


    ... Argument unpacking (自PHP 5.6起)


    ** Exponentiation (自PHP 5.6起)


    # One-line shell-style comment


    ?-> NullSafe Operator Calls


    0 回复  |  直到 5 年前
        1
  •  7
  •   Alberto Sinigaglia    6 年前

    Incrementing / Decrementing Operators

    ++ 增量运算符

    -- 减量运算符

    Example    Name              Effect
    ---------------------------------------------------------------------
    ++$a       Pre-increment     Increments $a by one, then returns $a.
    $a++       Post-increment    Returns $a, then increments $a by one.
    --$a       Pre-decrement     Decrements $a by one, then returns $a.
    $a--       Post-decrement    Returns $a, then decrements $a by one.
    

    这些可以在变量之前或之后。

    如果放在变量之前,则对变量执行递增/递减操作 然后返回结果。如果放在变量后面,则变量为 第一 返回,则完成递增/递减操作。

    例如:

    $apples = 10;
    for ($i = 0; $i < 10; ++$i) {
        echo 'I have ' . $apples-- . " apples. I just ate one.\n";
    }
    

    Live example

    在上述情况下 ++$i $i++ 会有同样的结果。

    但是,您必须使用 $apples-- ,因为首先要显示当前的苹果数,然后 然后 你想从中减去一个。

    您还可以在PHP中增加字母:

    $i = "a";
    while ($i < "c") {
        echo $i++;
    }
    

    一次 z 到达 aa

    请注意,字符变量可以递增,但不能递减,即使如此,也只支持普通ASCII字符(a-z和a-z)。


        2
  •  7
  •   John Conde    5 年前

    位运算符

    什么是字节?一个字节由8位组成,字节的最大值是255,这意味着每个位都被设置了。我们来看看为什么一个字节的最大值是255。

    -------------------------------------------
    |      1 Byte ( 8 bits )                  |
    -------------------------------------------
    |Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
    -------------------------------------------
    

    1+2+4+8+16+32+64+128=255(1字节)

    “AND”运算符: &

    $a =  9;
    $b = 10;
    echo $a & $b;
    

    这将输出数字8。为什么?好吧,让我们看看使用我们的表的例子。

    -------------------------------------------
    |      1 Byte ( 8 bits )                  |
    -------------------------------------------
    |Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
    -------------------------------------------
    |      $a    |   0|  0|  0|  0| 1| 0| 0| 1|    
    -------------------------------------------
    |      $b    |   0|  0|  0|  0| 1| 0| 1| 0|
    ------------------------------------------- 
    |      &     |   0|  0|  0|  0| 1| 0| 0| 0|
    ------------------------------------------- 
    

    所以你可以从表中看到他们共享的唯一一位是8位。

    第二个例子

    $a =  36;
    $b = 103;
    echo $a & $b; // This would output the number 36.
    $a = 00100100
    $b = 01100111
    

    这两个共享位是32和4,加在一起返回36。

    |

    $a =  9;
    $b = 10;
    echo $a | $b;
    

    这将输出数字11。为什么?

    -------------------------------------------
    |      1 Byte ( 8 bits )                  |
    -------------------------------------------
    |Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
    -------------------------------------------
    |      $a    |   0|  0|  0|  0| 1| 0| 0| 1|    
    -------------------------------------------
    |      $b    |   0|  0|  0|  0| 1| 0| 1| 0|
    ------------------------------------------- 
    |      |     |   0|  0|  0|  0| 1| 0| 1| 1|
    -------------------------------------------
    

    您会注意到我们在8、2和1列中设置了3位。把这些加起来:8+2+1=11。