Introduction to PHP
n Open Source
n Server-side scripts
n Installing PHP
So many blogs with LAMP setting up, of course you can refer to http://blog.csdn.net/wildhorseli/archive/2006/07/20/947479.aspx
n Now, it is time to say “hello world”
Language Basics (Lexical Structure)
n Case sensitivity
Ø Classes, functions of user-defined are case-insensitive
Ø Variables are case sensitive
n Statements and semicolons
Ø The semicolon is optional before a closing PHP
eg <?php echo “Enjoy PHP” ?>
n Comments
Ø Shell-style comments, with a hash mark; it sometimes is used to mark off blocks of codes: eg
##################
##Happy Functions
##################
Ø C++ comments, with two slash characters // to comment on code: eg
//Check if there is enough money
if( money > $10w )
Ø C comments, multiline comment
Language Basics (Lexical Structure)
n Identifier
Ø Only scalar valuesBoolean, integer, double, and stringcan be constants
Ø Using define function
n Keywords case-insensitive
Ø _ _CLASS_ _cloneendif_ _FILE_ _Constendswitch_ _FUNCTION_ _Continueendwhile_ _LINE_ _Declareeval( )_ _METHOD_ _DefaultexceptionAbstractdie( )exit( )AndDoextendsarray( )echo( )extendsAsElsefinalBreakelseifforCaseempty( )foreachcatchenddeclarefunctioncfunctionendforglobalClassendforeachifimplementsphp_user_filterswitchinclude( )print( )tHRowinclude_once( )privateTRyinterfaceprotectedunset( )isset( )publicuselist( )require( )varnewrequire_once( )whileold_functionreturn( )xorOrstatic
Language Basics (Data Types)
n Introduction
Ø PHP provides eight types of values, or data types. Four are scalar (single-value) types: integers , floating-point numbers, strings, and Booleans. Two are compound (collection) types: arrays and objects
n Integers
Ø Use is_int() to test whether a value is integer
n Floating-Point Numbers
Ø Use is_float() to test whether a value is float
n Strings
Ø Variables are expanded within double quotes, while within single quotes they are not: $name=“Martin”; echo “Hi, $name”; echo ‘Hi, $name’;
Ø Double quotes also support a variety of string escapes, such as /” /n /r /t // /$ /{ /[
n Booleans (false)
Ø The keyword false
Ø 0
Ø 0.0
Ø “” and “0”
Ø An array with zero elements
Ø An object with no values or functions
Ø NULL
Language Basics (Data Types)
n Arrays
Ø Loop through arrays
1 foreach( $person as $name)
echo “hello, $name”;
2 foreach( $creator as @key=>$value)
Ø Sort($array) asort($array)
Ø Use the is_array() function to test whether a value is an array.
n Objects
Ø Use is_object() to test whether a value is an object
Ø More reference, see the following
n Resources
Ø Like a “handle”
n NULL
Ø No value
Ø Case –insensitive
Ø Is_null()
Language Basics (Variables)
n Variable variables
Ø Eg $foo = ‘bar’;
$$foo = ‘baz’; // $bar = ‘baz’;
n Variable References
Ø &
Ø More info, refer to function section
n Variable Scope
Ø Local scope
by default, variables defined outside a function are not accessible inside the function
Ø Global scope
use the global keyword inside the function to declare the variable within the function. { global $counter; }
Ø Static variables
only useful for the function.
Ø Function parameters
function parameters are local.
n Garbage Collection
Ø Copy-on-write change one, copy two
Ø Isset() To see if empty, string is suited
Ø unset()
Language Basics (Expressions and Operators)
n .
Ø String
n operator
Ø + .. “9 Jo” +1 = 10
n Comparison
n Casting
Ø (int)(object)(array)
Language Basics (Others)
n Declare
n Exit return
Ø die
Ø return
n Include code
Ø require
such as html
Ø include
Functions (Function parameters)
n Passing Parameters by value
Ø In most cases
n Passing Parameters by Reference
Ø The argument must be a variable
Ø &
n Default Parameters
Ø Assign the parameter value in the function declare
Ø The value can only be a constant
n Variable Parameters
Ø $array = func_get_args()
Ø $count = func_num_args()
Ø $value = func_get_arg(argument_number)
n Missing Parameters
Ø Lets you be as lazy
Ø Remain unset
Functions (Others)
n Return Values
Ø Single value
Ø Array
n Variable Functions
Ø function_exists()
n Anonymous Functions
Ø Create_function
Strings (Quoting String Constants)
n Variable Interpolation
Ø Using double quotes
echo “$variable hello”;
echo “{$n}th”;
Ø Using single quote
escape sequences: /’ //
n Here Documents
Ø $variable = <<< Name
Name;
Strings (Printing Strings)
n echo
Ø echo “”
Ø Echo(“”)
Ø echo ”a”,”b” commas
Ø No return
n Print
Ø Print one value
Ø Return a boolean value
n printf
Ø Format modifiers
n Print_r & var_dump
Ø Print_r() means objects tostring()
Ø Var_dump for debuging