PHP light speed tutorial

Source: Internet
Author: User
PHP light speed tutorial

Date: | Source: redice's Blog | author: redice | 182 onlookers | 0 people applaud! // By redice 2010.07.29
// Redice@163.com


Based on your own learning experience and project development experience, I have summarized the most important PHP language knowledge points. It can be used as a PHP Quick Start tutorial.

1. client script and server script
Client: VBscript (highly dependent on IE, discard), Javascript
Server: ASP, PHP, Perl

JSP (server, non-script)
Python (available server, non-script)



2. key points/sequence of learning a language
Functions, features, syntax, variables, operators, process control, functions, and data structures



3. key points for learning the server language
Data input and output, database operations, session and cookie usage


4. PHP functions

Supports data processing in combination with a variety of server software (Apache, iis isapi/FastCGI, Nginx, etc.)


5. PHP features

Cross-platform, built-in function libraries are rich (much less written), simple syntax, and many references


6. PHP syntax

// This is the comment line. the comment line starts //

Phpinfo (); // The statement ends;
?>

7. PHP variables

Loose type, automatic declaration, forced conversion
PHP variables start with $ (a very affordable language starting with money)

$ Txt = "Hello World! "; // The string is enclosed by" ", escape ",\
$ Number = 16;
?>


Loose language: VB, VBscript, ASP, PHP, Python
Strong language: C (C ++), JSP


8 PHP operators

Arithmetic operations: +,-, *,/, % (remainder), ++ (auto Increment 1), -- (auto Increment 1)

Value assignment: =, + =,-=, * =,/=,. =, % =

Comparison operator: =, = ,! =,>, <, >=, <=

Logical operators: &, | ,!

Other operators:. (string join)

$ I = 10;
$ I + = 1;

Echo $ I; // echo data output function, you can also use print

Echo ++ $ I ;//?
Echo $ I ++ ;//?
?>


* Equal sign and third sign

= Only compare whether the values are equal, type conversion is performed on both sides
=== Whether the comparison value and type are the same, no type conversion is performed, and the type and value are the same to true

For example:

$ A = "2"; // string type 2
$ B = 2; // numeric type 2
$ A = $ B, yes, both are 2
$ A ===$ B, which is incorrect, because $ a is numeric and $ B is numeric. although the value is the same, the type is different.


9 PHP process control

Sequential statements

Branch statement: if else, Switch

// If else statement
If (condition)
Code to be executed if condition is true;
Elseif (condition)
Code to be executed if condition is true;
Else
Code to be executed if condition is false;

// Switch statement
Switch (expression)
{
Case label1:
Code to be executed if expression = label1;
Break;
Case label2:
Code to be executed if expression = label2;
Break;
Default:
Code to be executed
If expression is different
From both label1 and label2;
}


Loop statement: while, for, foreach

// While loop
While (condition)
Code to be executed;

// For loop
For (initialization; condition; increment)
{
Code to be executed;
}

// Foreach loop, traversing the array
Foreach (array as value)
{
Code to be executed;
}


// Foreach loop example

$ Arr = array ("one", "two", "three ");

Foreach ($ arr as $ value)
{
Echo "Value:". $ value ."
";
}
?>


* End PHP program execution:

Exit ($ str); // end PHP execution and output $ str
Die (); // Only end PHP execution


10 PHP functions

Built-in functions:
Http://php.chinaunix.net/manual/en/

(1) data output: echo, print, print_r (output array)
(2) string operations:
// Returns the length of $ string.
Int strlen (string $ string)

// Delete spaces or other characters at both ends of $ str
String trim (string $ str [, string $ charlist])

$ Str = "xiao ping ni hao! ";
Echo trim ($ str );
Echo"
";
Echo trim ($ str, "x! ")
?>


// Case-insensitive string

String strtolower (string $ str)
String strtoupper (string $ string)

// Search for strings

Int strpos (string $ haystack, mixed $ needle [, int $ offset = 0])

Return value: return the position of the substring. If no substring is found, false is returned.
Note:
If the substring $ needle appears at the beginning of $ haystack, 0 is returned.
Therefore, to determine whether to find the string, use = instead of =.

$ Str = "redicecn.com ";

// The following is an incorrect judgment
If (strpos ($ str, "redice") = false)
{
Echo "no substring found! ";
}
Else
{
Echo "found the substring! ";
}
?>


// String truncation

String substr (string $ string, int $ start [, int $ length])

// String replacement

String str_replace (mixed $ search, mixed $ replace, mixed $ subject)
String str_ireplace (mixed $ search, mixed $ replace, mixed $ subject) // case insensitive

(3) date and time:

// The time () function returns the Unix timestamp of the current time.
Time (void)

// Date () function format a local time/date
// The default value is Greenwich mean time zone.
Date (format, timestamp)

// Set it to East 8
Date_default_timezone_set ('etc/GMT-8 ');

Echo date ("Y-m-d H: I: s", time ());
?>


Custom functions:

$ Sitename = "E-engineering community ";

Function welcom ($ user)
{
Global $ sitename; // Reference global variables

// Return value
Return $ user. ", welcome to". $ sitename ."! ";
}

// Function call
Echo welcom ("redice ");
?>

11. PHP Data structure

Array

// Numeric array definition

(1) automatically allocate keys
$ Names = array ("Sister Furong", "Sister Feng", "Sharp Brother ");

(2) manually allocate keys
$ Names [0] = "Sister Furong ";
$ Names [1] = "Fengjie ";
$ Names [2] = "sharp brother ";

// Define the associated array

$ Ages = array ("Sister Furong" => 32, "sister Fengjie" => 29, "Sharp Brother" => 42 );

You can also

$ Ages ["Sister Furong"] = 32;
$ Ages ["Fengjie"] = 29;
$ Ages ["Rhinoceros"] = 42;


// Multi-dimensional array

$ Students = array
(
& Quot; 0911120688 & quot; = & gt; array
(
"Name" => "Qi Peng ",
"Age" => 24,
"Gender" => "male"
),
& Quot; 0911120699 & quot; = & gt; array
(
"Name" => "song Yuwei ",
"Age" => 22,
"Gender" => "female"
),
& Quot; 0911120670 & quot; = & gt; array
(
"Name" => "Chen Sufang ",
"Age" => 22,
"Gender" => "female"
)
);

12. PHP input (obtain client input)

(1) $ _ GET variable
The $ _ GET variable is an array with the variable name and value sent by the http get method.

(2) $ _ POST variable
The $ _ POST variable is an array with the variable name and value sent by the http post method.

(3) $ _ REQUEST variable
The $ _ REQUEST variable in PHP contains $ _ GET, $ _ POST, and $ _ COOKIE content.


13 SESSION and COOKIE usage

(1) SESSION

Stored on the server, the server is determined by the SESSIONID in the COOKIE, which is often used for identity authentication.

Run session_start () to start a SESSION,
Because session_start () needs to modify the COOKIE header (save SESSIONID) of the HTTP response message ),
Therefore, session_start () must be called before the HTTP response body is output.

// Create a session
Session_start ();
$ _ SESSION ['user'] = "redice ";
?>


// Read the session
Session_start ();
Echo $ _ SESSION ['user'];
?>

Delete SESSION
Unset ($ _ SESSION ['user']);
?>
Or
Session_destroy (); // All sessions will be deleted.
?>

(2) COOKIE

Stored on the client and sent together with the request message to the server,
It is often used to store user-defined settings, browsing records, and other security-independent data.

* Cookies are stored on the client and can be modified by users. Therefore, sensitive data cannot be stored.


// Create a cookie
Setcookie (name, value, expire );

Setcookie () also needs to modify the HTTP response header, so it needs to be called before outputting any body

Setcookie ("user", "redice", time () + 3600 );
?>

// Read cookie
Echo $ _ COOKIE ["user"];
?>

// Delete the cookie
// Set to expire immediately, and the client (browser) will be automatically deleted
Setcookie ("user", "", time ()-3600 );
?>


14 database operations

Procedure:

Connect to database-> Select database-> set the character set used
-> Operation data (query, update, delete, and insert)-> Close the database

$ Conn = 0;
$ Conn = mysql_connect ("localhost", "root", "redice2009 ");
If (! $ Conn)
{
Die ("cannot open the database connection, error:". mysql_error ());
}

// Select a database
Mysql_select_db ("thymall", $ conn );

// Set the character set of mysql output data
Mysql_query ("set names 'gbk '");

$ SQL = "select * from thym_goods LIMIT 5 ";

// Execute the query
$ Result = mysql_query ($ SQL, $ conn );

// Traverse the query results
While ($ result & $ row = mysql_fetch_array ($ result ))
{
}

// Close the database
Mysql_close ($ conn );
?>



15 others

Good program style: indent, comment

Selection principles of development tools: code keywords highlighted, automatically completed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.