PHP Elements PHP Language Reference PHP Quick Reference

The first step learning any programming language is to know about the language reference elements. PHP is a scripting language that is suited for Web application development and is widely used today. Like any other programing language PHP consists of following basic language elements which are very similar to 'c' language. Here is the quick PHP Language Reference.

PHP Variables

Variables in PHP are represented by a $ sign followed by the name of the variable and is case-sensitive. PHP variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. Examples $a, $user_name, $_xclick.
  • Assinging value to variable $a = 2; , $b = $a + 2; , $user_name = "White";
  • Value by reference: '&' is used to referance a variable. In following examle $b is point to $a
Predefined variables: PHP Superglobals are available to any running PHP script. Following is the list of PHP Predefined variables.
  • $GLOBALS Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables.
  • $_SERVER Variables set by the web server or otherwise directly related to the execution environment of the current script.
  • $_GET Variables provided to the script via HTTP GET.
  • $_POST Variables provided to the script via HTTP POST.
  • $_COOKIE Variables provided to the script via HTTP cookies.
  • $_FILES Variables provided to the script via HTTP post file uploads.
  • $_ENV Variables provided to the script via the environment.
  • $_REQUEST Variables provided to the script via the GET, POST, and COOKIE input mechanisms.
Variable scope
  • For the most part all PHP variables only have a single scope. See example below:

    In above example $user is available to main.php also.
  • PHP Variable used inside a function is limited to the local function scope. For example:

    The global keyword is used in this case to refer to variable $user. See Below:

  • A static variable exists only in a local function scope, it retain its value when program execution leaves this scope. see example below:

    Everytime Test() is called it increments the value of $a and prints it. First time will print 1, second time 2 and so on.
  • Variable variable names takes the name of the variable from other variable. See Below:

PHP Constants

define() functin is used to create constants in PHP. Its value cannot be changed during the execution of the script and they are having global scope and accessed anywhere in the script. See exampe Below:

Predefined constants PHP provides some special constants which are case-insensitive and are as follows:
  • __LINE__ current line number of the file.
  • __FILE__ full path and filename of the file.
  • __FUNCTION__ function name
  • __CLASS__ class name.
  • __METHOD__ class method name

PHP Data Types

PHP supports followin Data types
Standard Data Types
  • Integer Integers are signed or unsigned whole numbers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based). See Examples Below:

  • floats/doubles/real numbers Floating point numbers can be specified using any of the following:

  • String String data type is a collection of characters. backslash (\) is an escape character. Following are some string examples

  • Boolean It can be either TRUE or FALSE and case in-sensitive. See Below

  • Object This is an instance of a class. The new statement is used to instantiate the object to a variable. See Below

  • Array An array in PHP is an ordered map that maps values to the keys. See examples below:

Special Data Types
  • Resource A resource is a special variable, holding a reference to an external resource. A reference handlers to opened files, database connections, image canvas for example.
  • NULL An uninitialized variable, represents that a variable has no value

PHP Operators

Arithmetic Operators
  • = Assignment, for example $a = 5; , $a = $b+5;
  • + Addition for example $a+$b
  • - Subtraction for example $a-$b
  • / Division for example $a/$b
  • * Multiplication for example $a*$b
  • % Modulus for example $a%$b
Comparison Operators are used to perform tests on their operands and they return true or false based on the value of operands.
  • == Equal to for example $a == $b
  • != Not Equal to $a != $b
  • <> Not Equal to $a <> $b
  • === Equal to and of same type for example $a === $b
  • !== Not Equal to or they are not of the same type $a !== $b
  • > Greater than for example $a > $b
  • >= Greater than or equal to for example $a >= $b
  • < Less than for example $a < $b
  • <= Less than or equal to for example $a <= $b
Bitwise Operators
  • & And Bits, $x = $a & $b; will set bits in $x which are set in both $a and $b
  • | Or Bits, $x = $a | $b; will set bits in $x which set in either $a or $b
  • ^ Xor Bits, $x = $a ^ $b; will set bits in $x which set in $a or $b but not in both
  • ~ Not Bits, $x = ~ $a; Reverse bits of $a i.e. 1 to 0 and 0 to 1
  • << Shift left, $a << 4; Shifts bits of $a 4 steps to left
  • >> Shift right, $a >> 3; Shifts bits of $a 3 steps to right
Logical Operators
  • and, && And, ($a>1) && ($a<5) TRUE if both are true
  • or, || Or, ($a>1) or ($a<5) TRUE if either is true
  • xor Xor, ($a>1) xor ($a<5) TRUE if either is true but not both
  • ! Not, !$a TRUE if $a is not true
Increment/decrement Operators
  • ++$a Increments $a by one, then returns $a
  • --$a Decrements $a by one, then returns $a
  • $a++ Returns $a, then increments $a by one
  • $a-- Returns $a, then decrements $a by one
Other Operators
  • @ This is an error control operator when prepended to an expression any error messages that might be generated by that expression will be ignored
  • `` This is an execution operator will execute the contents of the backticks as a shell command for example $a = `ls`; will return the ourput of ls shell command.
  • . This is a string operator which returns the concatenation of its right and left strings for example $c = $a."Some String"
  • .= This is a string concatenating assignment operator for example $a .= "Some String" will append "Some String" to the right of $a

PHP Control Structures

  • if allows for conditional execution of code fragments. see example below
  • else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. see example below
  • elseif / else if unlike else, as its name suggests, it will execute alternative code only if the elseif condition is TRUE. see example below
  • switch is used to compare the same variable with many different values, and execute a different code depending on value of variable or expression. see example below

    This is equivalent to a series of if..elseif statements and "break" statement is used to terminate the execution of code for the given case.
  • Alternative syntax for Control Structures is to change the opening brace to a colon (:) and the closing brace to endif;, or endswitch; see example below

PHP Functions

  • Defining a Function in PHP can be done using function statement. see below

    A return statement is used to return values from the function
  • Function with an Optional Argument can be defined using following

    In above function $id is optional and if given will be appended to the returning url.
  • Making arguments be passed by reference allows the function to change its argument which is not possbile when it is passed by value. This is done by '&' see below

PHP Coding

A code in any programming language is a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even an empty statement which does nothing. In PHP a statement is terminated with semicolon(;). Multiple statements can be grouped using the curly braces ({...}). // and /*..*/ is used to insert comments in PHP. See example below

Embedding PHP in HTML: PHP is especially suited for Web development and can be embedded into HTML. This can be done by using some special start and end tags. See example below

1 comment:

Hotscanner said...

Thanks for the quick reference. It is exactly what I was looking for. Very good for experienced developers in other languages who just want to check out the php syntax in several minutes. Thanks a lot.