PHP basic tutorial (php basic tutorial) some code _ php Basics-php Manual

Source: Internet
Author: User
Tags define function echo name php basics php form php form processing php operator
Basic PHP tutorials (suitable for beginners who have Programming basics but are unfamiliar with PHP-the tutorials are mainly presented in the form of code) before this tutorial, I will not always say anything about PHP. For more information about variables and statements, see this document. This document is intended for people who have Programming basics and are unfamiliar with PHP. The article is relatively simple. Mainly look at the structure. For more information
PHP environment installation:
The common combination of PHP is: MySql + PHP + Apche, IIS + PHP + MySQL or SqlServer
Of course, we can select a combination package for installation. AppServ or phpnow is recommended for beginners.
In iis, you can install and run php. mysql needs to be installed.
You can also install each part on your own. Then configure it by yourself.
PHP versions: http://museum.php.net/php5/
Http://prdownloads.sourceforge.net/appserv/appserv-win32-2.5.10.exe? Download
MySQL: http://www.mysql.cn/
Configuration installation tutorial: http://wenku.baidu.com/view/c6118b1810a6f524ccbf85f9.html
Or http://www.jb51.net/article/33062.htm
Writing Tools: Notepad ++ or dreamweaver cs4 is recommended.
========================================================== ======================================
Syntax:
PHP syntax is very simple-directly look at the code: This is how PHP code is declared. Note: It can also be written in this way, but it is not recommended to do so.
Mark the end of a statement: a semicolon marks the end of a statement ";" -- use a semicolon to end each statement.
========================================================== ==================================
Note in PHP: -- for details, refer to the code in the tutorial.
The comment in php has a single line comment: // This is a comment
Comment on the big Module:/* This is a comment */
========================================================== ==================================
Variable:
PHP variables are loose. However, it is case sensitive. Before using it, you do not need to declare-according to the variable declaration method, PHP will automatically convert the variable to the correct data type.
Declare variables in PHP using the $ keyword. all variables are identified by $.
Variable naming rules:
The variable name must start with a letter or underscore.
Variable names can only contain letters, numbers, and underscores.
Variable names cannot contain spaces. If the variable name is composed of multiple words, it should be separated by underscores (for example, $ my_string) or begin with an uppercase letter (for example, $ myString ).
Note: (basically, the variable naming rules for all programming languages are similar !)

Example:

The code is as follows:


// Declare variables
$ Var_name = "snow ";
// Use variables
Echo $ var_name;
/*
Result: snow
*/
?>


Constant:
Declaration of constants in PHP:
Declare constants in PHP using the define function. View code directly

The code is as follows:


/*
The define function has three parameters.
The first parameter: specifies the constant name. keywords are not allowed. constants cannot contain the $ symbol.
The second parameter: specifies the constant value, which can only be Boolean, integer, floating point, or string.
Third parameter: specifies whether the constant is case sensitive -- true ignores case sensitivity, and false is case sensitive.
*/
Define ("Name", "Zhangsan", true );
Echo name;
/* Display result: James -- the result is case insensitive because it is true */
?>


There are also predefined constants in PHP-you can query the PHP manual or related materials
========================================================== ==================================
Array: -- PHP array is relatively simple and easy to use.
PHP arrays can be used as collections in other languages
Any types supported by PHP can be stored in the PHP array. Of course, you can also store class objects.

The code is as follows:


/* ===================================================== ===================================== */
// Numeric array
$ Nums = array (1, 2, 3 );
// Or equivalent
$ Nums [0] = 1;
$ Nums [1] = 2;
$ Nums [2] = 4;
Echo $ nums [2]."
";
/* Output: 4 */
/* ===================================================== ===================================== */
// Join array -- where "=>" is the association symbol in PHP, which specifies the key-value pair.
$ Ns = array ("name" => "zhang san", "age" => 22, "sex" => "man ");
// Or equivalent
$ Ns ["name"] = "James ";
$ Ns ["age"] = 22;
$ Ns ["sex"] = "man ";
Echo "name:". $ ns ["name"]."
Age: ". $ ns [" age "]."
Gender: ". $ ns [" sex "]."
";
/* Output:
Name: James
Age: 22
Gender: man
*/
/* ===================================================== ===================================== */
// Multi-dimensional array -- array can also store arrays
$ Bs = array ("zhang san" => array ("hobbies" => "computer", "age" => "23", "gender" => "male "), "Xiaohong" => array ("hobbies" => "dinner", "gender" => "female "));
// Adjust the format to make it clearer.
$ Bs = array
(
"Zhang San" => array
(
"Hobbies" => "computer ",
"Age" => "23 ",
"Gender" => "male"
),
"Xiaohong" => array
(
"Hobbies" => "dinner ",
"Gender" => "female"
)
);
// Or equivalent
$ Bs [""] ["gender"] = 2; $ bs [""] [""] = 2 ;//....
// Or
$ Bs ["zhang san"] = array ("hobbies" => "computer", "age" => "23", "gender" => "male "); $ bs ["Xiaohong"] = array ("hobbies" => "dinner", "gender" => "female ");
Echo $ bs ["Xiaohong"] ["gender"]."
";
/* Output: Female */
/* ===================================================== ===================================== */
?>


========================================================== ==================================
PHP operator: -- Excerpt w3school tutorial
  
This section lists various operators used in PHP:
Arithmetic operators
Operator Description Example Result
+ Addition X = 2
X + 2
4
- Subtraction X = 2
5-x
3
* Multiplication X = 4
X * 5
20
/ Division 15/5
5/2
3
2.5
% Modulus (pision remainder) 5% 2
10% 8
10% 2
1
2
0
++ Increment X = 5
X ++
X = 6
-- Decrement X = 5
X --
X = 4
Value assignment operator
Operator Description Example
= X = y X = y
+ = X + = y X = x + y
-= X-= y X = x-y
* = X * = y X = x * y
/= X/= y X = x/y
. = X. = y X = x. y
% = X % = y X = x % y

Comparison Operators

Operator Description Example
= Is equal 5 = 8 returns false
! = Is not equal 5! = 8 returns true
> Is greater 5> 8 returns false
< Is less 5 <8 returns true
> = Is greater than or equal 5> = 8 returns false
<= Is less than or equal 5 <= 8 returns true

Logical operators

Operator Description Example
&& And X = 6
Y = 3

(X <10 & y> 1) returns true

| Or X = 6
Y = 3

(X = 5 | y = 5) returns false

! Not X = 6
Y = 3

! (X = y) returns true


Program judgment statement:

Similar to C #, java, C, and other judgment statements. If. else/else... if and switch statements are available -- View Code directly

The code is as follows:


$ Name = "James"; // declare the variable
/* If. else only executes one of the statements, and one condition is true. Even if it is set up later, it will be ignored */
// Determine whether the name is James
If ($ name = "James ")
{
Echo "James ";
}
Else if ($ name = "") // then judge
{
Echo "Li Si ";
}
Else // none of the above will go into else
{
Echo "others ";
}
Print ('
'); // Print the output
$ Num = 1;
/*
The principle of switch selection structure is similar to that of if. Just add break in case -- of course, you can not add break.
In this case, the next case branch will not be executed after case 1 is executed. It won't jump out until it encounters a break ,. You can try it yourself
*/
Switch ($ num)
{
Case 1:
Echo "1 ";
Break;
Case 2:
Echo "2 ";
Break;
Default: // default branch. Run the command when none of the conditions are true.
Echo "others ";
}

/*
The final execution result is:
Zhang San
I
*/
?>


PHP loop:

It is the same as other strong programming languages. Php also has while, do while, for, and foreach -- View code directly

The code is as follows:


$ Index = 1;
While ($ index <= 10)
{
Echo "no. $ index." times "."
";
$ Index ++; // accumulate
}
/* Output the above results 10 times */

Echo'
';
$ Index = 1;
Do
{
Echo "no. $ index." times "."
";
$ Index ++;
}
While ($ index <= 1 );

/* Output the preceding result once */
Echo'
';
For ($ index = 1; $ index <= 3; $ index ++)
{
Echo "no. $ index." times "."
";
}

/* Output the preceding result three times */
Echo'
';
$ Index = array ("1", "2", "3 ");
Foreach ($ index as $ temp) // traverses the array
{
Echo "value:". $ temp ."
";
}
/* Output the preceding result three times */
?>


PHP functions:

The declaration of php functions is very simple. you only need to add the keyword function followed by the function name. -- View the code in specific format

The code is as follows:


/* PHP function */
// No parameter function
Function MyEcho ()
{
Echo "no parameter function
";
}

// Parameter-the input parameter can also be a class object
Function MyEcho2 ($ str)
{
Echo $ str;
}

MyEcho (); // output: no parameter function
MyEcho2 ("Hey, haha! "); // Output: Hey, haha!
?>

PHP class:

Php also supports object-oriented programming like other advanced languages. Here I will talk about the declaration of the basic php class. You can study the object-oriented programming methods on your own.

To declare a class in php, you also need to add the keyword class -- for details, see code-(including static functions. Function call)

The code is as follows:


Class MyClass // class declaration
{
Private $ jum1; // defines private variables
Private $ jum2;
Static public $ test = "test static method"; // defines public variables.
Function Calc () // class function
{
Return $ this-> jum1 + $ this-> jum2; // "->" indicates a class call.
}

Function SetNum ($ Num1, $ Num2) // has a parameter function
{
$ This-> jum1 = $ Num1;
$ This-> jum2 = $ Num2;
Return $ this; // return the class object itself.
}

Static function Tt ()
{
Echo"
". MyClass: $ test ."
";
}
}

/* Implement the computing function */
$ Temp = new MyClass;
Echo $ temp-> SetNum (2, 8)-> Calc (); // output: 10
MyClass: Tt (); // ":" static call // output: Test static method
?>


PHP form processing:

When a page user submits a value, use $ _ GET and $ _ POST or $ _ REQUEST (it contains $ _ GET, $ _ POST, and $ _ COOKIE) the variables defined by the system are used to read the submitted values.

The code is as follows:



Echo $ _ POST ["xx"]."
"; // Read The post value
Echo $ _ REQUEST ["xx"];
// Read the value with get. Try it yourself
?>



So much now... if I have time, I will write down common PHP applications. Advanced section. (Including Sessions, cookies, object-oriented, and common functions)

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.