.Open 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 .See ./php.glossary.html which contains meta-linguistic terms, lexemes, and keyword definitions based on the C++ glossary found at .See http://www.csci.csusb.edu/dick/samples/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. comments::=C_C++.comment|Shell.comment. C_C++_comment::=C.comment|C++.comment. C.comment::="/*" string_with_no_end_comment) "*/". C comment containing all characters for comments except for the terminating */. C++.comment::="//" O(the_comment) eol. Shell.comment::="#" O(the_comment) eol. string_with_no_end_comment::= #char ~(#char "*/" #char). the_comment::=#(non(eol)). This is the string of all no $eol characters in the comments. eol::=`end of line character`. For various styles of comments in PHP, see .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 keyword::=See .See ./php.glossary.html#core_keyword punctuation::=To Be Done. operators::=see .See PHP Expressions and Operators below. identifier::=See .See PHP Identifiers next. . PHP Identifiers . Variables variable_declaration::=O("static") "$" O("$") $identifier_name, identifier_name::=( letter | underscore ) #( letter | digit | underscore | extended_ascii ), extended_ascii::=char_nbr(127)..char_nbr(255), For the definitions of letter, underscore, and digit, see .See http://www.csci.csusb.edu/dick/maths/intro_ebnf.html#Lexemes 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 .See ./php.samples.html#PHP variables . . Functions function_declaration::= O("&") ignore_case( $function_name ) "(" parameter #( , parameter ) ) "{" $statement "}", parameter::="$" identifier_name, 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 .See ./php.samples.html#anonymous_functions . function_name::= "$" identifier_name, For ignore_case, see .See http://www.csci.csusb.edu/dick/samples/comp.text.ASCII.html#ignore_case for more details. See function semantics at .See ./php.semantics.html#functions for more details. For various examples of functions in PHP, see .See ./php.samples.html#PHP functions . . Classes class_definition::= "class" $class_name O( "extends" $class_name ) "{" $class_body "}", class_name::=$identifier_name, 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 .See ./php.samples.html#PHP classes . . Constants define::="define( O(') $identifier_name O('), "$constant_value", O( $case_sensitive ) )", case_sensitive::= ( true | false ), constant_value::=( boolean | integer | double | string ) ~(quotation_mark), quotation_mark::=""" | character_nbr(34). For examples of constants, see .See ./php.samples.html#PHP constants . . PHP Data Types . Scalar Types 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. 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. string::=( single_quote_string | double_quote_string | heredoc ), double_quote_string::=quotation_mark (#char(~quotation_mark) | #escape_seq) quotation_mark, escape_seq::= "\"" | "\n" | "\r" | "\t" | "\\" | "\$" | "\{" | "\}" | "\[" | "\]" | ascii_octal | ascii_hex, ascii_octal::= "\0".."\777", ascii_hex::= "\x0".."\xFF', single_quote_string::="'" #char(~"'") "'", heredoc::= "$"$identifer_name " = <<<" $sentinel #(line~sentinel) $sentinel";", sentinel::= $identifier_name, line::= #(char~eol) eol, Note: The sentinel must be the same on both sides, opening and closing the heredoc. boolean::=ignore_case( TRUE | FALSE ), . Compound Data Types For arrays and objects, see .See ./php.misc.html#compound_types . . PHP Special Types 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 .See ./php.misc.html#resource_type . . PHP Expressions and Operators .Table Operator_Precedence Associativity Operator Operation .Row 19 Non Associative new Creates a new object .Row 18 Right Associative [ Array subscript .Row 17 Right Associative ! Logical NOT .Row 17 Right Associative ~ Bitwise NOT .Row 17 Right Associative ++ Increment .Row 17 Right Associative -- Decrement .Row 17 Right Associative (int),(double),(string),(array),(object) Cast type .Row 17 Right Associative @ Inhibit error reporting .Row 16 Left Associative * Multiplication .Row 16 Left Associative / Division .Row 16 Left Associative % Modulus .Row 15 Left Associative + Addition .Row 15 Left Associative - Subtraction .Row 15 Left Associative . String concatenation .Row 14 Left Associative << Bitwise SHIFT LEFT .Row 14 Left Associative >> Bitwise SHIFT RIGHT .Row 13 Non Associative <,<= Less Than, Less Than or Equal To .Row 13 Non Associative >,>= Greater Than, Greater Than or Equal To .Row 12 Non Associative == Value Equality .Row 12 Non Associative !=,<> Value Inequality .Row 12 Non Associative === Type and Value Equality .Row 12 Non Associative !== Type and Value Inequality .Row 11 Left Associative & Bitwise AND .Row 10 Left Associative ^ Bitwise XOR .Row 9 Left Associative | Bitwise OR .Row 8 Left Associative && Logical AND .Row 7 Left Associative || Logical OR .Row 6 Left Associative ?: Conditional Operator .Row 5 Left Associative = Assignment Operator .Row 5 Left Associative +=,-=,*=,/=,.=,%=,&=,|=,^=,~=,<<=,>>= Assignment with Operation .Row 4 Left Associative and Logical AND .Row 3 Left Associative xor Logical XOR .Row 2 Left Associative or Logical OR .Row 1 Left Associative , List separation .Close.Table S(E,Op)::=$serial_operator_expression(E, Op) serial_operator_expression(E,Op)::= E #(Op E). .As_is S(E,Op) = E Op E Op E Op ... E . Arithmetic Operations post_fix::="++" | "--", post_fix_expression::=($primary_expression) #($post_fix), unary_operator::="&" | "*" | "+" | "-" | "!" | "-", pre_fix::="++" | "--" | "sizeof", unary_expression::=#($pre-$fix) $post_fix_expression | $unary_operator $variable_name, multiplicative_expression::=$S($cast_expression, $multiplicative_operator). .See 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. multiplicative_operator::="*" | "%" | "/", additive_expression::=$S($multiplicative_expression, $additive_operator). This means that addition and subtraction occurs after multiplication and from left to right. additive_operator::="+" | "-", . Shift Operations shift_expression::=$S($additive_expression, $shift_operator), shift_operator::=">>" | "<<", "<<" is left shift of bits (multiply by 2), and ">>" is the reverse and divides by 2. . Relation Operations relational_expression::= $S($shift_expression, $relational_operator), relational_operator::="<" | ">" | "<=" | ">=", equality_expression::=$S($relational_expression, $equality_operator), equality_operator::="==" | "!=", . Bitwise Expressions AND_expression::=$S($equality_expression, $and_operator), and_operator::="&" , XOR_expression::=$S($AND_expression, $XOR_operator), XOR_operator::="^", OR_expression::=$S($XOR_expression, $OR_operator), OR_operator::="|", . Logical Expressions logical_AND_expression::=$S($XOR_expression, $logical_AND_operator), logical_AND_operator::="&&" | "and", logical_XOR_expression::=$S($logical_OR_expression, $logical_XOR_operator), logical_XOR_operator::="xor", logical_OR_expression::=$S($logical_XOR_expression, $logical_OR_operator), logical_OR_operator::="||" | "or", . Conditional Expressions conditional_expression::=$logical_OR_expression | $logical_OR_expression "?" $expression ":" $conditional_expression, . Assignment Statements assignment_expression::=$S($unary_expression, $assignment_operator), assignment_operator::="=" | "*=" | "/=" | "%=" | "+=" | "<<=" | ">>=" | "&=" | "^=" | "|=", expression::=$List($assignment_expression ), 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: cast_expression::= $integer | $float | $string | $boolean, integer::= ( "(int)" | "(integer)" ) O( $unary_expression) $variable, float::= ( "(float)" | "(real)" ) O( $unary_expression) $variable, string::= "(string)" O( $unary_expression) $variable, boolean::= ( "(bool)" | "(boolean)" ) O( $unary_expression) $variable, . Compound Data Types array::= "(array)" $variable, object::= "(object)" $variable. . Miscellaneous Operators error_suppression::= "@", execution::="`" command_string "`", command_string::=#char, conditional::= expression "?" true_expression ":" false_expression, true_expression::= `Expression to be evaluated when conditional is true`, false_expression::= `Expression to be evaluated when conditional is false`, . Statements statement::= $compound_statement | $expression_statement | $switch_statement | $iteration_statement | $for_statement | $foreach_statement | $if_statement | $labeled_statement | $declare_statement | $exit_or_return block_statement::= "{" #($statement ";") "}", compound_statement::=#($statement ";"), . Flow Control Statements 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;", switch_statement::= "switch (" $expression ") { " $compound_statement "}" | "switch (" $expression ") : " $compound_statement "endswitch;", iteration_statement::= "while (" $expression ") $statement | "while (" $expression ") { " $compound_statement " } " | "while (" $expression ") : " $compound_statement " endwhile;" | "do " $compound_statement "while (" $expression ");", for_statement::= "for (" O( $expression ) ";" O( $expression ) ";" O( $expression ) ")" ( $statement | $block_statement ), foreach_statement::= "foreach (" $array " in " ( $current_value | $key => $current_value ) ") ( $statement | "{ $compound_statement }" ), current_value::= $identifier_name, key::= $array_key, For information on array key see .See ./php.misc.html#array_keys . declare_statement::= "declare ( $directive ) { $compound_statement }", directive::= "ticks = " number_ticks, number_ticks::= #digits, See notes on the declare function for more information at .See ./php.misc.html . exit_or_return::= ( "exit" $O( $expression ) | "return" $O( $expression ) ), labeled_statement::="case " $constant_expression ":" $statement | "default: " $statement, . Including Code . Posix Regular Expressions .Table Posix_Character_Classes RegExpressions .Row [:alnum:] [0-9a-zA-Z] .Row [:alpha:] [a-zA-Z] .Row [:ascii:] [\x01-\x7F] .Row [:blank:] [ \t] .Row [:cntrl:] [\x01-\x1F] .Row [:digit:] [0-9] .Row [:graph:] [^\x01-^\x20] .Row [:lower:] [a-z] .Row [:print:] [\t\x20-\xFF] .Row [:punct:] [-!"#$%&'()*+,./:;<=>?@[\\]^_`{|}~] .Row [:space:] [\n\r\t \x0B] .Row [:upper:] [A-Z] .Row [:xdigit:] [0-9a-fA-F] .Close.Table start_word::= "[[:<:]]", end_word::= "[[:>:]]", posix_reg_expression_functions::= $matching | $replacing | $split, matching::= ( "ereg" | "eregi" )( $pattern, $target_string, O( $captured ) ), Note: eregi is just the case-insensitive form of ereg. pattern::= $O($at_start) #( ($wildcard | $special | $start_word | $end_word ) $O("*") ) $O($at_end), at_start::="^", at_end::="$", wildcard::= "." | "[" $O($not_operator) #$set_of_character "]", not_operator::="^", set_of_character::= char | char "-" char target_string::= $string, this string contains the text to search, replacing::= ( "ereg_replace" | "eregi_replace" )( $pattern, $replacement, $string ), $replacement::=expression, $string::=expression, Note: eregi_replace is just the case-insensitive form of ereg_replace. split::= split($pattern, $string, O( $limit ) ), . Perl Regular Expressions Please visit .See ./php.misc.html#perl_regex_todo . .Close The Syntax of PHP