当前位置: 首页 > 文档资料 > PHP 语言规范 >

19 语法

优质
小牛编辑
125浏览
2023-12-01

一般

语法符号描述 语法部分.

语法词汇

一般

  input-file::
    input-element
    input-file   input-element
  input-element::
    comment
    white-space
    token

注释

  comment::
    single-line-comment
    delimited-comment

  single-line-comment::
    //   input-charactersopt
    #    input-charactersopt

  input-characters::
    input-character
    input-characters   input-character

  input-character::
    Any source character except new-line

  new-line::
    Carriage-return character (U+000D)
    Line-feed character (U+000A)
    Carriage-return character (U+000D) followed by line-feed character (U+000A)

  delimited-comment::
    /*   No characters or any source character sequence except */   */

空格

  white-space::
    white-space-character
    white-space   white-space-character

  white-space-character::
    new-line
    Space character (U+0020)
    Horizontal-tab character (U+0009)

标记

General

  token::
    variable-name
    name
    keyword
    literal
    operator-or-punctuator

命名

  variable-name::
    $   name

  namespace-name::
    name
    namespace-name   \   name

  namespace-name-as-a-prefix::
    \
    \opt   namespace-name   \
    namespace   \
    namespace   \   namespace-name   \

  qualified-name::
    namespace-name-as-a-prefixopt   name

  name::
    name-nondigit
    name   name-nondigit
    name   digit

  name-nondigit::
    nondigit
    one of the characters U+007f–U+00ff

  nondigit:: one of
    _
    a   b   c   d   e   f   g   h   i   j   k   l   m
    n   o   p   q   r   s   t   u   v   w   x   y   z
    A   B   C   D   E   F   G   H   I   J   K   L   M
    N   O   P   Q   R   S   T   U   V   W   X   Y   Z

关键词

  keyword:: one of
    abstract   and   as   break   callable   case   catch   class   clone
    const   continue   declare   default   do   echo   else   elseif
    enddeclare   endfor   endforeach   endif   endswitch   endwhile
    extends   final   finally   for   foreach   function   global
    goto   if   implements   include   include_once   instanceof
    insteadof   interface   list   namespace   new   or   print   private
    protected   public   require   require_once   return static   switch
    throw   trait   try   use   var   while   xor   yield

常量

一般

  literal::
    integer-literal
    floating-literal
    string-literal

整数常量

  integer-literal::
    decimal-literal
    octal-literal
    hexadecimal-literal
    binary-literal

    decimal-literal::
      nonzero-digit
      decimal-literal   digit

    octal-literal::
      0
      octal-literal   octal-digit

    hexadecimal-literal::
      hexadecimal-prefix   hexadecimal-digit
      hexadecimal-literal   hexadecimal-digit

    hexadecimal-prefix:: one of
      0x  0X

    binary-literal::
      binary-prefix   binary-digit
      binary-literal   binary-digit

    binary-prefix:: one of
      0b  0B

    digit:: one of
      0  1  2  3  4  5  6  7  8  9

    nonzero-digit:: one of
      1  2  3  4  5  6  7  8  9

    octal-digit:: one of
      0  1  2  3  4  5  6  7

    hexadecimal-digit:: one of
      0  1  2  3  4  5  6  7  8  9
      a  b  c  d  e  f
      A  B  C  D  E  F

    binary-digit:: one of
        0  1

浮点数常量

  floating-literal::
    fractional-literal   exponent-partopt
    digit-sequence   exponent-part

  fractional-literal::
    digit-sequenceopt . digit-sequence
    digit-sequence .

  exponent-part::
    e  signopt   digit-sequence
    E  signopt   digit-sequence

  sign:: one of
    +  -

  digit-sequence::
    digit
    digit-sequence   digit

字符串常量

  string-literal::
    single-quoted-string-literal
    double-quoted-string-literal
    heredoc-string-literal
    nowdoc-string-literal

  single-quoted-string-literal::
    bopt  ' sq-char-sequenceopt  '

  sq-char-sequence::
    sq-char
    sq-char-sequence   sq-char

  sq-char::
    sq-escape-sequence
    \opt   any member of the source character set except single-quote (') or backslash (\)

  sq-escape-sequence:: one of
    \'  \\

  double-quoted-string-literal::
    bopt  " dq-char-sequenceopt  "

  dq-char-sequence::
    dq-char
    dq-char-sequence   dq-char

  dq-char::
    dq-escape-sequence
    any member of the source character set except double-quote (") or backslash (\)
    \  any member of the source character set except "\$efnrtvxX or octal-digit

  dq-escape-sequence::
    dq-simple-escape-sequence
    dq-octal-escape-sequence
    dq-hexadecimal-escape-sequence

  dq-simple-escape-sequence:: one of
    \"   \\   \$   \e   \f   \n   \r   \t   \v

  dq-octal-escape-sequence::
    \   octal-digit
    \   octal-digit   octal-digit
    \   octal-digit   octal-digit   octal-digit

  dq-hexadecimal-escape-sequence::
    \x  hexadecimal-digit   hexadecimal-digitopt
    \X  hexadecimal-digit   hexadecimal-digitopt

    string-variable::
        variable-name   offset-or-propertyopt
        ${   expression   }

    offset-or-property::
        offset-in-string
        property-in-string

    offset-in-string::
        [   name   ]
        [   variable-name   ]
        [   integer-literal   ]

    property-in-string::
        ->   name

  heredoc-string-literal::
    <<<  hd-start-identifier   new-line   hd-char-sequenceopt  new-line hd-end-identifier  ;opt   new-line

  hd-start-identifier::
    name
    "   name  "

  hd-end-identifier::
    name

  hd-char-sequence::
    hd-char
    hd-char-sequence   hd-char

  hd-char::
    hd-escape-sequence
    any member of the source character set except backslash (\)
    \  any member of the source character set except \$efnrtvxX or octal-digit

  hd-escape-sequence::
    hd-simple-escape-sequence
    dq-octal-escape-sequence
    dq-hexadecimal-escape-sequence

  hd-simple-escape-sequence:: one of
    \\   \$   \e   \f   \n   \r   \t   \v

  nowdoc-string-literal::
    <<<  '  name  '  new-line  hd-char-sequenceopt   new-line name  ;opt   new-line

运算符和标点符号

  operator-or-punctuator:: one of
    [   ]    (   )   {    }   .   ->   ++   --   **   *   +   -   ~   !
    $   /   %   <<    >>   <   >   <=   >=   ==   ===   !=   !==   ^   |
    &   &&   ||   ?   :   ; =   **=   *=   /=   %=   +=   -=   .=   <<=
    >>=   &=   ^=   |=   =&   ,

语法

程序结构

script:
 script-section
 script   script-section

script-section:
   textopt start-tag statement-listopt end-tagopt textopt

start-tag:
  <?php
  <?=

end-tag:
  ?>

text:
  arbitrary text not containing any of start-tag sequences

变量

  function-static-declaration:
    static static-variable-name-list  ;

  static-variable-name-list:
    static-variable-declaration
    static-variable-name-list  ,  static-variable-declaration

  static-variable-declaration:
    variable-name function-static-initializeropt

  function-static-initializer:
    = constant-expression

  global-declaration:
    global variable-name-list ;

  variable-name-list:
    expression
    variable-name-list  ,  expression

表达式

原子表达式

  primary-expression:
    variable-name
    qualified-name
    literal
    constant-expression
    intrinsic
    anonymous-function-creation-expression
    (  expression  )
    $this

  intrinsic:
    intrisic-construct
    intrisic-operator

  intrisic-construct:
    echo-intrinsic
    list-intrinsic
    unset-intrinsic

  intrinsic-operator:
    array-intrinsic
    empty-intrinsic
    eval-intrinsic
    exit-intrinsic
    isset-intrinsic
    print-intrinsic

  array-intrinsic:
    array ( array-initializeropt  )

  echo-intrinsic:
    echo  expression
    echo  expression-list-two-or-more

  expression-list-two-or-more:
    expression  ,  expression
    expression-list-two-or-more  ,  expression

  empty-intrinsic:
    empty ( expression  )

  eval-intrinsic:
    eval (  expression  )

  exit-intrinsic:
    exit  expressionopt
    exit  (  expressionopt  )
    die   expressionopt
    die   (   expressionopt )

  isset-intrinsic:
    isset  (  expression-list-one-or-more  )

  expression-list-one-or-more:
    expression
    expression-list-one-or-more  ,  expression

  list-intrinsic:
    list  (  list-expression-listopt  )

  list-expression-list:
    unkeyed-list-expression-list
    keyed-list-expression-list ,opt

  unkeyed-list-expression-list:
    list-or-variable
    ,
    unkeyed-list-expression-list  ,  list-or-variableopt

  keyed-list-expression-list:
    expression  =>  list-or-variable
    keyed-list-expression-list  ,  expression  =>  list-or-variable

  list-or-variable:
    list-intrinsic
    expression

  print-intrinsic:
    print  expression
    print  (  expression  )

  unset-intrinsic:
    unset  (  expression-list-one-or-more  )

  anonymous-function-creation-expression:
  staticopt function  &opt (  parameter-declaration-listopt  )  anonymous-function-use-clauseopt
      compound-statement

  anonymous-function-use-clause:
    use  (  use-variable-name-list  )

  use-variable-name-list:
    &opt   variable-name
    use-variable-name-list  ,  &opt  variable-name

后缀运算符

  postfix-expression:
    primary-expression
    clone-expression
    object-creation-expression
    array-creation-expression
    subscript-expression
    function-call-expression
    member-selection-expression
    postfix-increment-expression
    postfix-decrement-expression
    scope-resolution-expression
    exponentiation-expression

  clone-expression:
    clone  expression

  object-creation-expression:
    new  class-type-designator  (  argument-expression-listopt  )
    new  class-type-designator

  class-type-designator:
    qualified-name
    expression

  array-creation-expression:
    array  (  array-initializeropt  )
    [ array-initializeropt ]

  array-initializer:
    array-initializer-list  ,opt

  array-initializer-list:
    array-element-initializer
    array-element-initializer  ,  array-initializer-list

  array-element-initializer:
    &opt   element-value
    element-key  =>  &opt   element-value

  element-key:
    expression

  element-value
    expression

  subscript-expression:
    postfix-expression  [  expressionopt  ]
    postfix-expression  {  expression  }   [Deprecated form]

  function-call-expression:
    qualified-name  (  argument-expression-listopt  )
    postfix-expression  (  argument-expression-listopt  )

  argument-expression-list:
    argument-expression
    argument-expression-list  ,  argument-expression

  argument-expression:
    variadic-unpacking
    assignment-expression

  variadic-unpacking:
    ... assignment-expression

  member-selection-expression:
    postfix-expression  ->  member-selection-designator

  member-selection-designator:
    name
    expression

  postfix-increment-expression:
    unary-expression  ++

  postfix-decrement-expression:
    unary-expression  --

  scope-resolution-expression:
    scope-resolution-qualifier  ::  member-selection-designator
    scope-resolution-qualifier  ::  class

  scope-resolution-qualifier:
    relative-scope
    qualified-name
    expression

  relative-scope:
    self
    parent
    static

  exponentiation-expression:
    expression  **  expression

一元运算符

  unary-expression:
    postfix-expression
    prefix-increment-expression
    prefix-decrement-expression
    unary-op-expression
    error-control-expression
    shell-command-expression
    cast-expression
    variable-name-creation-expression

  prefix-increment-expression:
    ++ unary-expression

  prefix-decrement-expression:
    -- unary-expression

  unary-op-expression:
    unary-operator cast-expression

  unary-operator: one of
    +  -  !  ~

  error-control-expression:
    @   expression

  shell-command-expression:
    `  dq-char-sequenceopt  `

  cast-expression:
    unary-expression
    (  cast-type  ) expression

  cast-type: one of
    array  binary  bool  boolean  double  int  integer  float  object
    real  string  unset

  variable-name-creation-expression:
    $   expression
    $  {  expression  }

instanceof运算符

  instanceof-expression:
    unary-expression
    instanceof-subject  instanceof   instanceof-type-designator

  instanceof-subject:
    expression

  instanceof-type-designator:
    qualified-name
    expression

倒数运算符

  multiplicative-expression:
    instanceof-expression
    multiplicative-expression  *  instanceof-expression
    multiplicative-expression  /  instanceof-expression
    multiplicative-expression  %  instanceof-expression

加性运算符

  additive-expression:
    multiplicative-expression
    additive-expression  +  multiplicative-expression
    additive-expression  -  multiplicative-expression
    additive-expression  .  multiplicative-expression

位移运算符

  shift-expression:
    additive-expression
    shift-expression  <<  additive-expression
    shift-expression  >>  additive-expression

关系型运算符

  relational-expression:
    shift-expression
    relational-expression  <   shift-expression
    relational-expression  >   shift-expression
    relational-expression  <=  shift-expression
    relational-expression  >=  shift-expression
    relational-expression  <=> shift-expression

等式运算符

  equality-expression:
    relational-expression
    equality-expression  ==  relational-expression
    equality-expression  !=  relational-expression
    equality-expression  <>  relational-expression
    equality-expression  ===  relational-expression
    equality-expression  !==  relational-expression

位运算符

  bitwise-AND-expression:
    equality-expression
    bit-wise-AND-expression  &  equality-expression

  bitwise-exc-OR-expression:
    bitwise-AND-expression
    bitwise-exc-OR-expression  ^  bitwise-AND-expression

  bitwise-inc-OR-expression:
    bitwise-exc-OR-expression
    bitwise-inc-OR-expression  |  bitwise-exc-OR-expression

逻辑运算符 (form 1)

  logical-AND-expression-1:
    bitwise-incl-OR-expression
    logical-AND-expression-1  &&  bitwise-inc-OR-expression

  logical-inc-OR-expression-1:
    logical-AND-expression-1
    logical-inc-OR-expression-1  ||  logical-AND-expression-1

三元运算符

  conditional-expression:
    logical-inc-OR-expression-1
    logical-inc-OR-expression-1  ?  expressionopt  :  conditional-expression

联合操作符

  coalesce-expression:
    logical-inc-OR-expression  ??  expression

赋值操作符

  assignment-expression:
    conditional-expression
    coalesce-expression
    simple-assignment-expression
    byref-assignment-expression
    compound-assignment-expression

  simple-assignment-expression:
    unary-expression  =  assignment-expression

  byref-assignment-expression:
    unary-expression  =  &  assignment-expression

  compound-assignment-expression:
    unary-expression   compound-assignment-operator   assignment-expression

  compound-assignment-operator: one of
    **=  *=  /=  %=  +=  -=  .=  <>=  &=  ^=  |=

逻辑运算符 (form 2)

  logical-AND-expression-2:
    assignment-expression
    logical-AND-expression-2  and  assignment-expression

  logical-exc-OR-expression:
    logical-AND-expression-2
    logical-exc-OR-expression  xor  logical-AND-expression-2

  logical-inc-OR-expression-2:
    logical-exc-OR-expression
    logical-inc-OR-expression-2  or  logical-exc-OR-expression

yield

  yield-expression:
    logical-inc-OR-expression-2
    yield  array-element-initializer

脚本包含

  expression:
    yield-expression
    include-expression
    include-once-expression
    require-expression
    require-once-expression

  include-expression:
    include  (  expression  )
    include  expression

  include-once-expression:
    include_once  (  expression  )
    include_once  expression

  require-expression:
    require  (  expression  )
    require  expression

  require-once-expression:
    require_once  (  expression  )
    require_once  expression

0常量表达式

  constant-expression:
    array-creation-expression
    expression

声明

一般


  statement:
    compound-statement
    labeled-statement
    expression-statement
    selection-statement
    iteration-statement
    jump-statement
    declare-statement
    const-declaration
    function-definition
    class-declaration
    interface-declaration
    trait-declaration
    namespace-definition
    namespace-use-declaration
    global-declaration
    function-static-declaration

符合语句

  compound-statement:
    {   statement-listopt  }

  statement-list:
    statement
    statement-list   statement

标记语句

  labeled-statement:
    named-label-statement
    case-statement
    default-statement

  named-label-statement:
    name  :  statement

  case-statement:
    case   expression   case-default-label-terminator   statement

  default-statement:
    default  case-default-label-terminator   statement

  case-default-label-terminator:
    :
    ;

表达式

   expression-statement:
     expressionopt  ;

  selection-statement:
    if-statement
    switch-statement

  if-statement:
    if   (   expression   )   statement   elseif-clauses-1opt   else-clause-1opt
    if   (   expression   )   :   statement-list   elseif-clauses-2opt   else-clause-2opt   endif   ;

  elseif-clauses-1:
    elseif-clause-1
    elseif-clauses-1   elseif-clause-1

  elseif-clause-1:
    elseif   (   expression   )   statement

  else-clause-1:
    else   statement

  elseif-clauses-2:
    elseif-clause-2
    elseif-clauses-2   elseif-clause-2

  elseif-clause-2:
    elseif   (   expression   )   :   statement-list

  else-clause-2:
    else   :   statement-list

  switch-statement:
    switch  (  expression  )  { case-statementsopt }
    switch  (  expression  )  :   case-statementsopt  endswitch;

  case-statements:
    case-statement statement-listopt case-statementsopt
    default-statement statement-listopt case-statementsopt

迭代

  iteration-statement:
    while-statement
    do-statement
    for-statement
    foreach-statement

  while-statement:
    while  (  expression  )  statement
    while  (  expression  )  :   statement-list  endwhile ;

  do-statement:
    do  statement  while  (  expression  )  ;

  for-statement:
    for   (   for-initializeropt   ;   for-controlopt   ;   for-end-of-loopopt   )   statement
    for   (   for-initializeropt   ;   for-controlopt   ;   for-end-of-loopopt   )   :   statement-list   endfor   ;

  for-initializer:
    for-expression-group

  for-control:
    for-expression-group

  for-end-of-loop:
    for-expression-group

  for-expression-group:
    expression
    for-expression-group   ,   expression

  foreach-statement:
    foreach  (  foreach-collection-name  as  foreach-keyopt  foreach-value  )   statement
    foreach  (  foreach-collection-name  as  foreach-keyopt  foreach-value  )  :  statement-list  endforeach  ;

  foreach-collection-name:
    expression

  foreach-key:
    expression  =>

  foreach-value:
    &opt   expression
    list-intrinsic

跳转

  jump-statement:
    goto-statement
    continue-statement
    break-statement
    return-statement
    throw-statement

  goto-statement:
    goto  name  ;

  continue-statement:
    continue   breakout-levelopt  ;

  breakout-level:
    integer-literal

  break-statement:
    break  breakout-levelopt  ;

  return-statement:
    return  expressionopt  ;

  throw-statement:
    throw  expression  ;

Try

  try-statement:
    try  compound-statement   catch-clauses
    try  compound-statement   finally-clause
    try  compound-statement   catch-clauses   finally-clause

  catch-clauses:
    catch-clause
    catch-clauses   catch-clause

  catch-clause:
    catch  (  qualified-name variable-name )  compound-statement

  finally-clause:
    finally   compound-statement

declare

  declare-statement:
    declare  (  declare-directive  )  statement
    declare  (  declare-directive  )  :  statement-list  enddeclare  ;
    declare  (  declare-directive  )  ;

  declare-directive:
    ticks  =  literal
    encoding  =  literal
    strict_types  =  literal

函数

  function-definition:
    function-definition-header   compound-statement

  function-definition-header:
    function  &opt   name return-typeopt (  parameter-declaration-listopt  )

  parameter-declaration-list:
    simple-parameter-declaration-list
    variadic-declaration-list

  simple-parameter-declaration-list:
    parameter-declaration
    parameter-declaration-list  ,  parameter-declaration

  variadic-declaration-list:
    simple-parameter-declaration-list  ,  variadic-parameter
    variadic-parameter

  parameter-declaration:
    type-declarationopt  &opt  variable-name   default-argument-specifieropt

  variadic-parameter:
    type-declarationopt  &opt  ...  variable-name

  return-type:
    : type-declaration
    : void

  type-declaration:
    array
    callable
    scalar-type
    qualified-name

  scalar-type:
    bool
    float
    int
    string

  default-argument-specifier:
    =  constant-expression

  class-declaration:
    class-modifieropt  class  name   class-base-clauseopt  class-interface-clauseopt   {   trait-use-clausesopt   class-member-declarationsopt }

  class-modifier:
    abstract
    final

  class-base-clause:
    extends  qualified-name

  class-interface-clause:
    implements  qualified-name
    class-interface-clause  ,  qualified-name

  class-member-declarations:
    class-member-declaration
    class-member-declarations   class-member-declaration

   class-member-declaration:
     const-declaration
     property-declaration
     method-declaration
     constructor-declaration
     destructor-declaration

  const-declaration:
    const  name  =  constant-expression   ;

  property-declaration:
    property-modifier   variable-name   property-initializeropt  ;

  property-modifier:
    var
    visibility-modifier   static-modifieropt
    static-modifier   visibility-modifieropt

  visibility-modifier:
    public
    protected
    private

  static-modifier:
    static

  property-initializer:
    =  constant-expression

  method-declaration:
    method-modifiersopt   function-definition
    method-modifiers   function-definition-header  ;

  method-modifiers:
    method-modifier
    method-modifiers   method-modifier

  method-modifier:
    visibility-modifier
    static-modifier
    class-modifier

  constructor-declaration:
    method-modifiers  function &opt   __construct  (  parameter-declaration-listopt  )  compound-statement
    method-modifiers  function &opt    name  (  parameter-declaration-listopt  )  compound-statement     [Deprecated form]

  destructor-declaration:
    method-modifiers  function  &opt  __destruct  ( ) compound-statement

接口

  interface-declaration:
    interface   name   interface-base-clauseopt {  interface-member-declarationsopt  }

  interface-base-clause:
    extends   qualified-name
    interface-base-clause  ,  qualified-name

  interface-member-declarations:
    interface-member-declaration
    interface-member-declarations   interface-member-declaration

  interface-member-declaration:
    const-declaration
    method-declaration

Traits

  trait-declaration:
    trait   name   {   trait-use-clausesopt   trait-member-declarationsopt   }

  trait-use-clauses:
    trait-use-clause
    trait-use-clauses   trait-use-clause

  trait-use-clause:
    use   trait-name-list   trait-use-specification

  trait-name-list:
    qualified-name
    trait-name-list   ,   qualified-name

  trait-use-specification:
    ;
    {   trait-select-and-alias-clausesopt   }

  trait-select-and-alias-clauses:
    trait-select-and-alias-clause
    trait-select-and-alias-clauses   trait-select-and-alias-clause

  trait-select-and-alias-clause:
    trait-select-insteadof-clause ;
    trait-alias-as-clause ;

  trait-select-insteadof-clause:
    name   insteadof   name

  trait-alias-as-clause:
    name   as   visibility-modifieropt   name
    name   as   visibility-modifier   nameopt

  trait-member-declarations:
    trait-member-declaration
    trait-member-declarations   trait-member-declaration

  trait-member-declaration:
    property-declaration
    method-declaration
    constructor-declaration
    destructor-declaration

命名空间

  namespace-definition:
    namespace  name  ;
    namespace  nameopt   compound-statement

  namespace-use-declaration:
    use  namespace-function-or-constopt namespace-use-clauses  ;

  namespace-use-clauses:
    namespace-use-clause
    namespace-use-clauses  ,  namespace-use-clause

  namespace-use-clause:
    qualified-name   namespace-aliasing-clauseopt

  namespace-aliasing-clause:
    as  name

  namespace-function-or-const:
    function
    const