PHP light speed tutorial

Source: Internet
Author: User
Tags array definition echo date
PHP light speed tutorial


Date: 2010-07-29|
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

<? PHP
// 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)

<? PHP
$ 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)

<? PHP
$ 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;
}

<? PHP
// Foreach loop example

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

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

* End PHPProgram:

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])

<? PHP
$ STR = "Xiao ping Ni hao! ";
Echo trim ($ Str );
Echo "<br/> ";
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 =.

<? PHP
$ 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)

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

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

Custom functions:

<? PHP
$ Sitename = "e-engineeringCommunity";

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
<? PHP
Session_start ();
$ _ Session ['user'] = "redice ";
?>

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

Delete session
<? PHP
Unset ($ _ session ['user']);
?>
Or
<? PHP
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

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

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

// Delete the cookie
<? PHP
// 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

<? PHP
$ 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:CodeKeyword 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.