Home  >  Article  >  Backend Development  >  A good PHP basic study note_PHP tutorial

A good PHP basic study note_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:56:13726browse

1. Four representation forms of PHP fragments.
Standard tags:
short tags: You need to set short _open_tag=on in php.ini, the default is on >Need to set asp_tags=on in php.ini, the default is off
script tags:
2. PHP variables and data types
1) $variable, variables start with letters, _, and cannot have spaces
2) Assignment $variable=value;
3) Weak type, direct assignment, no need to display the declared data type
4) Basic data Type: Integer, Double, String, Boolean, Object (object or class), Array (array)
5) Special data types: Resource (reference to third-party resources (such as database)), Null (empty, uninitialized) Variables)
3. Operators
1) Assignment operators: =
2) Arithmetic operators: +, -, *, /, % (modulo)
3) Connection operators :., no matter what the operand is, it will be treated as String, and the result will be returned String
4) Combined Assignment Operators total assignment operators: +=, *=, /=, -=, %=, .=
5 ) Automatically Incrementing and Decrementing automatic increase and decrease operators:
(1)$variable+=1 <=>$variable++;$variable-=1 <=>$variable-, just like c language, do it first For other operations, follow ++ or -
(2) ++$variable, -$variable, first ++ or -, and then do other operations
6) Comparison operator: = = (left side is equal to 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 (when one and only one of the left and right sides is true, return true),!
4. Comments:
Single-line comments: //, #
Multi-line comments: /* */
5 , Each statement ends with ;, the same as java
6. Define constants: define("CONSTANS_NAME",value)
7. Print statement: print, the same as c language
8. Process control statement
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. Write function
1) Define function:
function function_name($argument1,$argument2,……) //Formal parameters
{
//function code here;
}
2) Function call
function_name($argument1,$argument2,……); //Formal parameters
3) Dynamic Function Calls:


Listing 6.5


function sayHello() { //Define function sayHello
print "hello
";
}
$function_holder = "sayHello"; //Assign the function name to the variable $function_holder
$function_holder(); //The variable $function_holder becomes a reference to the function sayHello. Calling $function_holder() is equivalent to calling sayHello
? >


4) Variable scope:
Global variables:


Listing 6.8


$life=42;
function meaningOfLife() {
global $life;
/*Redeclare $life as a global variable here. Accessing global variables within a function must be done this way. If the value of the variable is changed within the function, it will be changed in all code fragments*/
print "The meaning of life is $life
";
}
meaningOfLife();
?>


5) Use static


Listing 6.10


function numberedHeading( $txt ) {
static $num_of_calls = 0;
$num_of_calls++;
print "

$num_of_calls. $txt

";
}
numberedHeading("Widgets"); //When called for the first time, print $num_of_calls value is 1
print("We build a fine range of widgets

");
numberedHeading("Doodads"); /*When called for the first time, print $num_of_calls value is 2, because the variable is static type, static type is resident in memory*/
print("Finest in the world?>


6) Passing value (value) and passing reference (reference):
Passing value: function function_name($argument)


Listing 6.13


function addFive( $num ) {
$num += 5;
}
$orignum = 10;
addFive( &$orignum );
print ( $orignum );
?>


Result: 10
Address: funciton function_name(&$argument)
< ;html>

Listing 6.14


function addFive( &$num ) {
$num += 5; /*What is passed is a reference to the variable $num, so changing the value of the formal parameter $num is actually changing the value stored in the physical memory of the variable $orignum*/
}
$orignum = 10;
addFive( $orignum );
print( $orignum );
?>


Result: 15
7) Create an anonymous function: create_function('string1','string2'); create_function is a built-in function in PHP, specially used to create anonymous functions. It accepts two string parameters, the first one is the parameter List, the second one is the body of the function


Listing 6.15

< body>
$my_anon = create_function( '$a, $b', 'return $a+$b;' );
print $my_anon( 3, 9 );
// prints 12
?>


8) Determine whether the 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) Database and connection Establish a connection: mysql_select_db(database name, connection index);
4) Execute the SQL statement to MySQL: $result = mysql_query($sql, $conn); //Add, delete, modify and check are all like this
5) Retrieve data: Return the number of records: $number_of_rows = mysql_num_rows($result);
Put the records into the array: $newArray = mysql_fetch_array($result);
Example:
/ / 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
";
}
?>
11. Accept form elements: $_POST[form element name],
such asó$_POST[user]
Accept the queryString value in the URL (GET method): $_GET[queryString]
12. Go to other pages: header("Location: http://www.samspublishing.com");
13. Characters String operations:
1) explode(“-”,str)ósplite in Java
2) str_replace($str1,$str2,$str3) =>$str1 is the string to be found, $str2 is used To replace the string, $str3 starts to search and replace from this string
3) substr_replace:
14. session:
1) Open session: session_start(); // It can also be set in php.ini session_auto_start=1, you don’t need to write this sentence in every script, but the default is 0, so you must write it.
2)给session赋值:$_SESSION[session_variable_name]=$variable;
3)访问session:$variable =$_SESSION[session_variable_name];
4)销毁session:session_destroy();
15、显示分类的完整例子:
//connect to database
$conn = mysql_connect("localhost", "joeuser", "somepass")
or die(mysql_error());
mysql_select_db("testDB",$conn) or die(mysql_error());
$display_block = "

My Categories


Select a category to see its items.

";
//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) { //如果返回记录行数小于1,则说明没有分类
$display_block = "

Sorry, no categories to browse.

";
} else {
while ($cats = mysql_fetch_array($get_cats_res)) { //将记录放入变量$cats中
$cat_id = $cats[id];
$cat_title = strtoupper(stripslashes($cats[cat_title]));
$cat_desc = stripslashes($cats[cat_desc]);
$display_block .= "

href="$_SERVER[PHP_SELF][U1] ?cat_id=$cat_id">$cat_title//点击此url,刷新本页,第28行读取cat_id,显示相应分类的条目

$cat_desc

";
if ($_GET[cat_id] == $cat_id) { //选择一个分类,看下面的条目
//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 = "

Sorry, no items in
this category.

";
} else {
$display_block .= "
    ";
    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 .= "
  • href="showitem.php?item_id=$item_id">$item_title
     ($$item_price)";
    [U2]                   }
    $display_block .= "
";
}
}
}
}
?>


My Categories





16、PHP连接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();  
?> 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/318111.htmlTechArticle1、PHP片段四种表示形式。 标准tags:?php? shorttags:??需要在php.ini中设置short_open_tag=on,默认是on asptags:%%需要在php.ini中设置asp_tags=on,默认是...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn