A good PHP basic study note

Source: Internet
Author: User
Tags variable scope

1. PHP snippets are represented in four forms.
Standard tags: <? Php?>
Short tags: <? ?> Set short _ open_tag = on in php. ini. The default value is on.
Asp tags: <%> you need to set asp_tags = on in php. ini. The default value is off.
Script tags: <script language = "php"> </script>
2. PHP variables and Data Types
1) $ variable. The variable starts with a letter or a space.
2) assign $ variable = value;
3) weak type, direct value assignment, no need to display declared Data Types
4) Basic data types: Integer, Double, String, Boolean, Object (Object or class), Array (Array)
5) special data types: Resourse (reference to third-party resources (such as databases), Null (Null, uninitialized variable)
3. Operators
1) Value assignment operator: =
2) Arithmetic Operators: +,-, *,/, % (Modulo)
3) join OPERATOR:. No matter what the operand is, it is treated as a String and the result returns a String.
4) Combined Assignment Operators Total Assignment Operators: + =, * =,/=,-=, % =,. =
5) Automatically Incrementing and Decrementing:
(1) $ variable + = 1 <=> $ variable ++; $ variable-= 1 <=> $ variable-. Perform other operations like C, plus ++ or-
(2) ++ $ variable,-$ variable, first ++ or-, and then perform other operations
6) comparison operator: = (the left side is equal to the right side ),! = (The left side is not equal to the right side), ===( the left side is equal to the right side, and the data type is the same), >=, >,<,< =
7) logical operators: | óor, & óand, xor (true is returned when only one of the left and right sides is true ),!
4. Notes:
Single line comment ://,#
Multi-line comment :/**/
5. Each statement ends with a; sign, which is the same as java
6. define constants: define ("CONSTANS_NAME", value)
7. print statement: print, same as C Language
8. Process Control statements
1) if statement:
(1) if (expression)
{
// Code to excute if expression evaluates to true
}
(2) if (expression)
{
}
Else
{
}
(3) if (expression1)
{
}
Elseif (expression2)
{
}
Else
{
}
2) swich statement
Switch (expression)
{
Case result
// Execute this if expression results in result1
Break;
Case result
// Execute this if expression results in result2
Break;
Default:
// Execute this if no break statement
// Has been encountered hitherto
}
3 )? OPERATOR:
(Expression )? Returned_if_expression_is_true: returned_if_expression_is_false;
4) while statement:
(1) while (expression)
{
// Do something
}
(2) do
{
// Code to be executed
} While (expression );
5) for statement:
For (initialization expression; test expression; modification expression ){
// Code to be executed
}
6) break; continue
9. Compile Functions
1) define a function:
Function function_name ($ argument1, $ argument2 ,......) // Form parameters
{
// Function code here;
}
2) function call
Function_name ($ argument1, $ argument2 ,......); // Form parameters
3) Dynamic Function call (Dynamic Function cballs ):
<Html>
<Head>
<Title> listing6.5 </title>
</Head>
<Body>
<? Php
Function sayHello () {// defines the function sayHello
Print "hello <br> ";
}
$ Function_holder = "sayHello"; // assign the function name to the variable $ function_holder
$ Function_holder (); // variable $ function_holder becomes a reference to the sayHello function. Calling $ function_holder () is equivalent to calling sayHello.
?>
</Body>
</Html>
4) variable scope:
Global variables:
<Html>
<Head>
<Title> listing6.8 </title>
</Head>
<Body>
<? Php
$ Life = 42;
Function meaningOfLife (){
Global $ life;
/* Re-declare $ life as a global variable here. Access to global variables within the function must be like this. If the value of the variable is changed within the function, it will be changed in all code snippets */
Print "The meaning of life is $ life <br> ";
}
MeaningOfLife ();
?>
</Body>
</Html>
5) use static
<Html>
<Head>
<Title> listing6.10 </title>
</Head>
<Body>
<? Php
Function numberedHeading ($ txt ){
Static $ num_of_cballs = 0;
$ Num_of_cballs ++;
Print "}
NumberedHeading ("Widgets"); // when the first call is made, the value of $ num_of_cals is 1.
Print ("We build a fine range of widgets <p> ");
NumberedHeading ("Doodads");/* when the first call is made, the value of $ num_of_cils is printed to 2, because the variable is static and the static type is resident memory */
Print ("Finest in the world <p> ");
?>
</Body>
</Html>
6) value and reference ):
Pass value: function function_name ($ argument)
<Html>
<Head>
<Title> listing6.13 </title>
</Head>
<Body>
<? Php
Function addFive ($ num ){
$ Num + = 5;
}
$ Orignum = 10;
AddFive (& $ orignum );
Print ($ orignum );
?>
</Body>
</Html>
Result: 10
Address: funciton function_name (& $ argument)
<Html>
<Head>
<Title> listing6.14 </title>
</Head>
<Body>
<? Php
Function addFive (& $ num ){
$ Num + = 5;/* is passed as a reference to the variable $ num, so changing the value of the parameter $ num is actually changing the value saved in the variable $ orignum physical memory */
}
$ Orignum = 10;
AddFive ($ orignum );
Print ($ orignum );
?>
</Body>
</Html>
Result: 15
7) Create an anonymous function: create_function ('string1', 'string2'); create_function is a built-in function of PHP. It is used to create an anonymous function and accepts two string parameters, the first is the parameter list, and the second is the main body of the function.
<Html>
<Head>
<Title> listing6.15 </title>
</Head>
<Body>
<? Php
$ My_anon = create_function ('$ a, $ B', 'Return $ a + $ B ;');
Print $ my_anon (3, 9 );
// Prints 12
?>
</Body>
</Html>
8) determine whether a function exists: function_exists (function_name). The parameter is the function name.
10. Use PHP to connect to MySQL
1) connection: & conn = mysql_connect ("localhost", "joeuser", "somepass ");
2) Close the connection: mysql_close ($ conn );
3) establish a connection between the database and the connection: mysql_select_db (database name, connection index );
4) execute the SQL statement to MySQL: $ result = mysql_query ($ SQL, $ conn); // add, delete, modify, and query the statement.
5) data retrieval: number of returned records: $ number_of_rows = mysql_num_rows ($ result );
Put records in the array: $ newArray = mysql_fetch_array ($ result );
Example:
<? Php
// Open the connection
$ Conn = mysql_connect ("localhost", "joeuser", "somepass ");
// Pick the database to use
Mysql_select_db ("testDB", $ conn );
// Create the SQL statement
$ SQL = "SELECT * FROM testTable ";
// Execute the SQL statement
$ Result = mysql_query ($ SQL, $ conn) or die (mysql_error ());
// Go through each row in the result set and display data
While ($ newArray = mysql_fetch_array ($ result )){
// Give a name to the fields
$ Id = $ newArray ['id'];
$ TestField = $ newArray ['testfield'];
// Echo the results onscreen
Echo "The ID is $ id and the text is $ testField <br> ";
}
?>
11. Accept form elements: $ _ POST [Form element name],
For example, <input type = text name = user> ó $ _ POST [user]
Accept the value of queryString in the url (GET method): $ _ GET [queryString]
12. Go to other pages: header ("Location: http://www.samspublishing.com ");
13. String operations:
1) explode ("-", str) ósplite in Java
2) str_replace ($ str1, $ str2, $ str3) => $ str1: string to be searched, $ str2: string to be replaced, $ str3: Search for replacement from this string
3) substr_replace:
14. session:
1) open the session: session_start (); // You can also open the session in php. set session_auto_start = 1 in ini. You do not need to write this sentence in every script, but the default value is 0.
2) assign a value to the session: $ _ SESSION [session_variable_name] = $ variable;
3) Access session: $ variable =$ _ SESSION [session_variable_name];
4) destroy session: session_destroy ();
15. Complete example of displaying categories:
<? Php
// Connect to database
$ Conn = mysql_connect ("localhost", "joeuser", "somepass ")
Or die (mysql_error ());
Mysql_select_db ("testDB", $ conn) or die (mysql_error ());
$ Display_block = "<P> Select a category to see its items. </p> ";
// Show categories first
$ Get_cats = "select id, cat_title, cat_desc from
Store_categories order by cat_title ";
$ Get_cats_res = mysql_query ($ get_cats) or die (mysql_error ());
If (mysql_num_rows ($ get_cats_res) <1) {// if the number of returned records is less than 1, it indicates no category
$ Display_block = "<P> <em> Sorry, no categories to browse. </em> </p> ";
} Else {
While ($ cats = mysql_fetch_array ($ get_cats_res) {// put records in the variable $ cats
$ Cat_id = $ cats [id];
$ Cat_title = strtoupper (stripslashes ($ cats [cat_title]);
$ Cat_desc = stripslashes ($ cats [cat_desc]);
$ Display_block. = "<p> <strong> <
Href = "$ _ SERVER [PHP_SELF] [U1]? Cat_id = $ cat_id "> $ cat_title </a> </strong> // click this url to refresh this page. Row 3 reads cat_id and displays entries of the corresponding category.
<Br> $ cat_desc </p> ";
If ($ _ GET [cat_id] = $ cat_id) {// select a category and check the following entries.
// Get items
$ Get_items = "select id, item_title, item_price
From store_items where cat_id = $ cat_id
Order by item_title ";
$ Get_items_res = mysql_query ($ get_items) or die (mysql_error ());
If (mysql_num_rows ($ get_items_res) <1 ){
$ Display_block = "<P> <em> Sorry, no items in
This category. </em> </p> ";
} Else {
$ Display_block. = "<ul> ";
While ($ items = mysql_fetch_array ($ get_items_res )){
$ Item_id = $ items [id];
$ Item_title = stripslashes ($ items [item_title]);
$ Item_price = $ items [item_price];
$ Display_block. = "<li> <
Href = "showitem. php? Item_id = $ item_id "> $ item_title </a>
</Strong> ($ item_price )";
[U2]}
$ Display_block. = "</ul> ";
}
}
}
}
?>
<HTML>
<HEAD>
<TITLE> My Categories </TITLE>
</HEAD>
<BODY>
<? Print $ display_block;?>
</BODY>
</HTML>
16. PHP connection Access:
<?
$ Dbc = new com ("adodb. connection ");
$ Dbc-> open ("driver = microsoft access driver (*. mdb); dbq = c: member. mdb ");
$ Rs = $ dbc-> execute ("select * from tablename ");
$ I = 0;
While (! $ Rs-> eof ){
$ I + = 1
$ Fld0 = $ rs-> fields ["UserName"];
$ Fld0 = $ rs-> fields ["Password"];
....
Echo "$ fld0-> value $ fld1-> value ....";
$ Rs-> movenext ();
}
$ Rs-> close ();
?>

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.