/ [ MATHS ] / php.syntax || [Notation] || [Copyright] || [Contact]
[Contents] [Source Text]
Sat Aug 23 13:05:41 PDT 2014

Contents (index)


    The Syntax of PHP

    This page was created by Paul Conrad as part of his Graduate Independent Study (CSCI695) of the syntax and semantics of the PHP language.

    This page describes the syntax of the PHP programming language based on the definitions of the language in Programming PHP by Rasmus Lerdorf, and Kevin Tatroe, published by O'Reilly Publishing. Since PHP is still a rapidly evolving programming language, the syntax presented here is for PHP 4.

    See Also

    For a quick, concise reference to terms used through out this page, see [ ./php.glossary.html ] which contains meta-linguistic terms, lexemes, and keyword definitions based on the C++ glossary found at [ c++.glossary.html ] with the appropriate adaptations to PHP.

    PHP Comments

    Commenting code in PHP can be achieved in three different styles. The first two styles are the C/C++ style of comments, and the third commenting style used in PHP is the Shell style comments. These three styles of commenting are quite universal through out computer science. C/C++/Java programmers, and Unix Shell programmers do not have to learn a new style of commenting in order to comment PHP code. This allows these programmers to comment PHP with a comment style they are most familiar or comfortable with.

  1. comments::=C_C++.comment|Shell.comment.

  2. C_C++_comment::=C.comment|C++.comment.

  3. C.comment::="/*" string_with_no_end_comment) "*/".

    C comment containing all characters for comments except for the terminating */.

  4. C++.comment::="//" O(the_comment) eol.

  5. Shell.comment::="#" O(the_comment) eol.

  6. string_with_no_end_comment::= #char ~(#char "*/" #char).

  7. the_comment::=#(non(eol)).

    This is the string of all no eol characters in the comments.

  8. eol::=end of line character.

    For various styles of comments in PHP, see [ ./php.samples.html#PHP comments ] .

    Note: Just as a quick point, the HTML code generated by PHP will not contain the comments. The comments are only on the server side for the programmer, so do feel free to comment the code for better readability and do not concern having comments affect the bandwidth or time it takes to load the generated HTML.

    PHP Lexemes

  9. keyword::=See [ ./php.glossary.html#core_keyword ]
  10. punctuation::=To Be Done.
  11. operators::=see [ PHP Expressions and Operators ] below.
  12. identifier::=See [ PHP Identifiers ] next.

    PHP Identifiers

    Variables

  13. variable_declaration::=O("static") "$" O("$") identifier_name,

  14. identifier_name::=( letter | underscore ) #( letter | digit | underscore | extended_ascii ),

  15. extended_ascii::=char_nbr(127)..char_nbr(255),

    For the definitions of letter, underscore, and digit, see [ Lexemes in intro_ebnf ] for details.

    Note: PHP allows for the use of ASCII codes from 0x7F (127) to 0xFF (255) for valid characters to begin an identifier with. To represent the i'th ASCII character, the notation char_nbr(i) is used.

    For various examples of variables in PHP, see [ ./php.samples.html#PHP variables ] .

    Functions

  16. function_declaration::= O("&") ignore_case( function_name ) "(" parameter #( , parameter ) ) "{" statement "}",

  17. parameter::="$" identifier_name,

  18. anonymous_function::= create_function( argument_string, code_string ),

    Note: Anonymous functions are like LISP lambda expressions. They are functions created on the fly inside an expression. The result can be assigned to a variable or passed as a parameter.

    See the examples on using anonymous functions at [ ./php.samples.html#anonymous_functions ] .

  19. function_name::= "$" identifier_name,

    For ignore_case, see [ ignore_case in comp.text.ASCII ] for more details.

    See function semantics at [ ./php.semantics.html#functions ] for more details.

    For various examples of functions in PHP, see [ ./php.samples.html#PHP functions ] .

    Classes

  20. class_definition::= "class" class_name O( "extends" class_name ) "{" class_body "}",

  21. class_name::=identifier_name,

  22. class_body::= #( variable_declaration | function_declaration ),

    The class name "stdClass" is reserved and cannot be used as an identifier for a class.

    Note: In PHP, there is no concept of private, public, or protected member functions or properties.

    For a class example, see [ ./php.samples.html#PHP classes ] .

    Constants

  23. define::="define( O(') identifier_name O('), "constant_value", O( case_sensitive ) )",

  24. case_sensitive::= ( true | false ),

  25. constant_value::=( boolean | integer | double | string ) ~(quotation_mark),

  26. quotation_mark::=""" | character_nbr(34).

    For examples of constants, see [ ./php.samples.html#PHP constants ] .

    PHP Data Types

    Scalar Types

  27. integer::=( O("+") | - ) digit #digit,

    In PHP the range of integers is between -2147483648 and 2147483647. An interesting note about PHP and integers, when the value assigned goes outside these minimum/maximum boundaries, the value is automatically converted to floating point.

  28. floating_point::=( O("+") | - ) digit "." digit #digit O( "E" integer ),

    In PHP if a variable is assigned a value 7 or 7., echoing to the browser will display 7.

  29. string::=( single_quote_string | double_quote_string | heredoc ),

  30. double_quote_string::=quotation_mark (#char(~quotation_mark) | #escape_seq) quotation_mark,

  31. escape_seq::= "\"" | "\n" | "\r" | "\t" | "\\" | "\$" | "\{" | "\}" | "\[" | "\]" | ascii_octal | ascii_hex,

  32. ascii_octal::= "\0".."\777",

  33. ascii_hex::= "\x0".."\xFF',

  34. single_quote_string::="'" #char(~"'") "'",

  35. heredoc::= "$"identifer_name " = <<<" sentinel #(line~sentinel) sentinel";",

  36. sentinel::= identifier_name,

  37. line::= #(char~eol) eol,

    Note: The sentinel must be the same on both sides, opening and closing the heredoc.

  38. boolean::=ignore_case( TRUE | FALSE ),

    Compound Data Types

    For arrays and objects, see [ ./php.misc.html#compound_types ] .

    PHP Special Types

  39. NULL::=ignore_case("NULL"), nullifies the value of a variable,

    This special type is used to represent a variable that does not contain any value, and is similar to undef in Perl and 'None' in Python.

    The predefined PHP function, is_null() allows for testing whether or not a variable holds a NULL value or not.

    For the PHP special type, resource, see [ ./php.misc.html#resource_type ] .

    PHP Expressions and Operators

    Operator_Precedence Associativity Operator Operation
    19 Non Associative new Creates a new object
    18 Right Associative [ Array subscript
    17 Right Associative ! Logical NOT
    17 Right Associative ~ Bitwise NOT
    17 Right Associative ++ Increment
    17 Right Associative -- Decrement
    17 Right Associative (int),(double),(string),(array),(object) Cast type
    17 Right Associative @ Inhibit error reporting
    16 Left Associative * Multiplication
    16 Left Associative / Division
    16 Left Associative % Modulus
    15 Left Associative + Addition
    15 Left Associative - Subtraction
    15 Left Associative . String concatenation
    14 Left Associative << Bitwise SHIFT LEFT
    14 Left Associative >> Bitwise SHIFT RIGHT
    13 Non Associative <,<= Less Than, Less Than or Equal To
    13 Non Associative >,>= Greater Than, Greater Than or Equal To
    12 Non Associative == Value Equality
    12 Non Associative !=,<> Value Inequality
    12 Non Associative === Type and Value Equality
    12 Non Associative !== Type and Value Inequality
    11 Left Associative & Bitwise AND
    10 Left Associative ^ Bitwise XOR
    9 Left Associative | Bitwise OR
    8 Left Associative && Logical AND
    7 Left Associative || Logical OR
    6 Left Associative ?: Conditional Operator
    5 Left Associative = Assignment Operator
    5 Left Associative +=,-=,*=,/=,.=,%=,&=,|=,^=,~=,<<=,>>= Assignment with Operation
    4 Left Associative and Logical AND
    3 Left Associative xor Logical XOR
    2 Left Associative or Logical OR
    1 Left Associative , List separation

  40. S(E,Op)::=serial_operator_expression(E, Op)

  41. serial_operator_expression(E,Op)::= E #(Op E).
              S(E,Op) = E Op E Op E Op ... E

    Arithmetic Operations

  42. post_fix::="++" | "--",

  43. post_fix_expression::=(primary_expression) #(post_fix),

  44. unary_operator::="&" | "*" | "+" | "-" | "!" | "-",

  45. pre_fix::="++" | "--" | "sizeof",

  46. unary_expression::=#(pre-fix) post_fix_expression | unary_operator variable_name,

  47. multiplicative_expression::=S(cast_expression, multiplicative_operator). [ serial_operator_expression ]

    The rule above means that 'casts' are done before multiplication and division, and that multiplication and division are done from left to right.

  48. multiplicative_operator::="*" | "%" | "/",

  49. additive_expression::=S(multiplicative_expression, additive_operator). This means that addition and subtraction occurs after multiplication and from left to right.

  50. additive_operator::="+" | "-",

    Shift Operations

  51. shift_expression::=S(additive_expression, shift_operator),

  52. shift_operator::=">>" | "<<", "<<" is left shift of bits (multiply by 2), and ">>" is the reverse and divides by 2.

    Relation Operations

  53. relational_expression::= S(shift_expression, relational_operator),

  54. relational_operator::="<" | ">" | "<=" | ">=",

  55. equality_expression::=S(relational_expression, equality_operator),

  56. equality_operator::="==" | "!=",

    Bitwise Expressions

  57. AND_expression::=S(equality_expression, and_operator),

  58. and_operator::="&" ,
  59. XOR_expression::=S(AND_expression, XOR_operator),

  60. XOR_operator::="^",

  61. OR_expression::=S(XOR_expression, OR_operator),

  62. OR_operator::="|",

    Logical Expressions

  63. logical_AND_expression::=S(XOR_expression, logical_AND_operator),

  64. logical_AND_operator::="&&" | "and",

  65. logical_XOR_expression::=S(logical_OR_expression, logical_XOR_operator),

  66. logical_XOR_operator::="xor",

  67. logical_OR_expression::=S(logical_XOR_expression, logical_OR_operator),

  68. logical_OR_operator::="||" | "or",

    Conditional Expressions

  69. conditional_expression::=logical_OR_expression | logical_OR_expression "?" expression ":" conditional_expression,

    Assignment Statements

  70. assignment_expression::=S(unary_expression, assignment_operator),

  71. assignment_operator::="=" | "*=" | "/=" | "%=" | "+=" | "<<=" | ">>=" | "&=" | "^=" | "|=",

  72. expression::=List(assignment_expression ),

  73. constant_expression::=conditional_expression,

    Casting Operators

    Even though PHP is a weak typed language, there are ways to specify a type for a variable. We have the following casting operators:

  74. cast_expression::= integer | float | string | boolean,

  75. integer::= ( "(int)" | "(integer)" ) O( unary_expression) variable,

  76. float::= ( "(float)" | "(real)" ) O( unary_expression) variable,

  77. string::= "(string)" O( unary_expression) variable,

  78. boolean::= ( "(bool)" | "(boolean)" ) O( unary_expression) variable,

    Compound Data Types

  79. array::= "(array)" variable,

  80. object::= "(object)" variable.

    Miscellaneous Operators

  81. error_suppression::= "@",

  82. execution::="" command_string "",

  83. command_string::=#char,

  84. conditional::= expression "?" true_expression ":" false_expression,

  85. true_expression::= Expression to be evaluated when conditional is true,

  86. false_expression::= Expression to be evaluated when conditional is false,

    Statements

  87. statement::= compound_statement | expression_statement | switch_statement | iteration_statement | for_statement | foreach_statement | if_statement | labeled_statement | declare_statement | exit_or_return

  88. block_statement::= "{" #(statement ";") "}",

  89. compound_statement::=#(statement ";"),

    Flow Control Statements

  90. if_statement::= "if (" expression ")" statement | "if (" expression ")" statement " else " statement | "if (" expression ") {" compound_statement "} else {" compound_statement "}" | "if (" expression ") : " compound_statement O( "else: " compound_statement ) "endif;",

  91. switch_statement::= "switch (" expression ") { " compound_statement "}" | "switch (" expression ") : " compound_statement "endswitch;",

  92. iteration_statement::= "while (" expression ") statement | "while (" expression ") { " compound_statement " } " | "while (" expression ") : " compound_statement " endwhile;" | "do " compound_statement "while (" expression ");",

  93. for_statement::= "for (" O( expression ) ";" O( expression ) ";" O( expression ) ")" ( statement | block_statement ),

  94. foreach_statement::= "foreach (" array " in " ( current_value | key => current_value ) ") ( statement | "{ compound_statement }" ),

  95. current_value::= identifier_name,

  96. key::= array_key,

    For information on array key see [ ./php.misc.html#array_keys ] .

  97. declare_statement::= "declare ( directive ) { compound_statement }",

  98. directive::= "ticks = " number_ticks,

  99. number_ticks::= #digits,

    See notes on the declare function for more information at [ ./php.misc.html ] .

  100. exit_or_return::= ( "exit" O( expression ) | "return" O( expression ) ),

  101. labeled_statement::="case " constant_expression ":" statement | "default: " statement,

    Including Code

    Posix Regular Expressions

    Posix_Character_Classes RegExpressions
    [:alnum:] [0-9a-zA-Z]
    [:alpha:] [a-zA-Z]
    [:ascii:] [\x01-\x7F]
    [:blank:] [ \t]
    [:cntrl:] [\x01-\x1F]
    [:digit:] [0-9]
    [:graph:] [^\x01-^\x20]
    [:lower:] [a-z]
    [:print:] [\t\x20-\xFF]
    [:punct:] [-!"#$%&'()*+,./:;<=>?@[\\]^_`{|}~]
    [:space:] [\n\r\t \x0B]
    [:upper:] [A-Z]
    [:xdigit:] [0-9a-fA-F]

  102. start_word::= "[[:<:]]",

  103. end_word::= "[[:>:]]",

  104. posix_reg_expression_functions::= matching | replacing | split,

  105. matching::= ( "ereg" | "eregi" )( pattern, target_string, O( captured ) ),

    Note: eregi is just the case-insensitive form of ereg.

  106. pattern::= O(at_start) #( (wildcard | special | start_word | end_word ) O("*") ) O(at_end),

  107. at_start::="^",

  108. at_end::="$",

  109. wildcard::= "." | "[" O(not_operator) #set_of_character "]",

  110. not_operator::="^",

  111. set_of_character::= char | char "-" char

  112. target_string::= string, this string contains the text to search,

  113. replacing::= ( "ereg_replace" | "eregi_replace" )( pattern, replacement, string ),

    replacement::=expression,

    string::=expression,

    Note: eregi_replace is just the case-insensitive form of ereg_replace.

  114. split::= split(pattern, string, O( limit ) ),

    Perl Regular Expressions

    Please visit [ ./php.misc.html#perl_regex_todo ] .

    . . . . . . . . . ( end of section The Syntax of PHP) <<Contents | Index>>


Formulae and Definitions in Alphabetical Order