Jul 23

Chapter 1 , from a C/C++ programmer’s point of view, introduced the PHP basic syntax, variables, constants, variables and constants scope, operator, operator precedence, variable functions, conditional statements, etc for Crash Course.

1. Basic Syntax

PHP Tags

  • XML style (recommended):
    <?php echo '<p>Order processed.</p>'; ?>
  • Short style (need to enable short_open_tag setting):
    <? echo '<p>Order processed.</p>'; ?>
  • Script style:
    <script language='php'> echo '<p>Order processed.</p>'; </script>
  • ASP style (Need to enable asp_tags setting, not recommended):
    <% echo '<p>Order processed.</p>'; %>

Comments

  • Multiline comments: /*…*/
  • single-line comments: //… or #…

2. Variables

Identifiers Naming Rules

  • Variables must start with $
  • Identifiers can be of any length and can consist of letters, numbers, and under-scores
  • Identifiers cannot begin with a digit
  • In PHP, identifiers are case sensitive
  • Function names are not case sensitive, and a variable can have the same name as a function (not make sense enough)

Variable Types

PHP supports the following basic data types: Integer, Float, String, Boolean, Array, Object.
Two special types are also available: NULL and Resource.

PHP is called weakly typed, or dynamically typed language. So when you needn’t explicitly specify the type when you declare a variable, PHP changes the variable type according to what is stored in it at any given time. In addition, the variables need not declare a variable before using it.

Variable Variables

Variable variables enable you to change the name of a variable dynamically.
$varname = 'tireqty';
$$varname = 5;
This is exactly equivalent to
$tireqty = 5;

3. Constants

Declaring Constants

define('USER_NAME', 'admin');
define('IS_ADMIN', TRUE);

Data Type

Constants can store only boolean, integer, float, or string data.

Using Constants

You needn’t type a “$” before a constant name.

4. Variable and Constant Scope

  • Built-in superglobal variables are visible everywhere within a script.
  • Constants, once declared, are always visible globally; that is, they can be used inside and outside functions.
  • Global variables declared in a script are visible throughout that script, but not inside functions except you declare global refer:
    <?php
    $a = 1;
    $b = 2;
    function Sum()
    {
    global $a, $b; //global refer
    $b = $a + $b;
    }
    Sum();
    echo $b; //Output result is 3
    ?>
  • Variables created inside functions are local to the function and cease to exist when the function terminates.
  • Variables created inside functions and declared as static are invisible from outside the function but keep their value between one execution of the function and the next.

5. Operators

Arithmetic Operators

Including “+,-,*,/,% (Modulus) “.

String Operators

“.” String concatenation operator adds two strings and generates and stores a result.

Assignment and Combined Assignment Operators

Including “=,+=,-=,*=,/=,%=,.=”, similar to the C language.

Self-increment and Self-decrement Operators

Including “++,- -”, like the C language, just understand the difference between $a++ and ++a$.

Reference Operator

$a = 5;
$b = &$a;
$a = 7; // $a and $b are now both 7

Comparison Operators

Including “==,===,!=,!==,<>,<,>,<=,>=”.
Note that “7″ == 7 return TRUE, while 7 === “7″ returns False, because the identical operator (===), which returns true only if the two operands are both equal and of the same type.

Logical Operators

Including “!, &&, ||, and, or, xor”, similar to the C language.

Bitwise Operators

Including “~, &, |, ^, >>, <<”, similar to the C language.

Other Operators

  • “,,new,->,(?:)” similar to the C language.
  • “@” The error suppression operator (@) can be used in front of any expression.
  • “` `” The execution operator is really a pair of operators—a pair of backticks (“) , PHP attempts to execute whatever is contained between the backticks as a command at the server’s command line.

6. Operator Precedence

No need to remember precedence and associativity, you can use parentheses when you are uncertain.

7. Variables and Types Related Functions

  • string gettype(mixed var); //Returns a string containing the type name
  • bool settype(mixed var, string type); //change the type of the var
  • bool is_array(mixed var); //Checks whether the variable is an array
  • bool is_long(mixed var); / is_int() / is_integer() //Checks whether the variable is an integer
  • bool is_double(mixed var); / is_float() / is_real() //Checks whether the variable is a float
  • bool is_scalar(mixed var); //Checks whether the variable is a scalar, that is, an integer, boolean, string, or float
  • bool is_callable(mixed var); //Checks whether the variable is the name of a valid function
  • There are similar functions like is_string(), is_bool(), is_object(), is_resource(), is_null() etc.
  • bool isset ( mixed var [, mixed var [, ...]]); //Returns true if it exists and false otherwise
  • void unset ( mixed var [, mixed var [, ...]]); //Gets rid of the variable it is passed
  • bool empty ( mixed var [, mixed var [, ...]]); //Checks to see whether a variable exists and has a nonempty, nonzero value

8. Conditional statements

  • if/elseif/else
  • switch
  • while/do…while
  • for/foreach
  • break

All these conditional and loop statements are similar to other programming languages.

2 Responses to “PHP Study Notes – Crash Course”

  1. May 21, 2012 at 1:50 am
    Menelom
  2. November 30, 2011 at 9:31 am
    webinone

Leave a Reply (Set Globally Recognized Avatar