A good PHP basic Learning Note _php Tutorial

Source: Internet
Author: User
1, PHP fragment four kinds of representations.
Standard Tags:
Short Tags: Need to set short _open_tag=on in php.ini, default is on
ASP Tags: <%%> need to set asp_tags=on in php.ini, default is off
Script Tags:
2. PHP variables and data types
1) $variable, the variable begins with a letter, _, and cannot have spaces
2) assigned value $variable=value;
3) weak type, direct assignment, no need to display the declaration data type
4) Basic Data type: Integer,double,string,boolean,object (object or Class), array (array)
5) Special Data type: Resourse (reference to a third-party resource (such as a database), null (NULL, uninitialized variable)
3. Operator
1) assignment Operator: =
2) arithmetic operator: +,-,*,/,% (modulo)
3) connection operator:. , regardless of the operand, as a string, the result returns a string
4) Combined Assignment operators total assignment operator: +=,*=,/=,-=,%=,.=
5) automatically incrementing and decrementing auto increment operator:
(1) $variable +=1 <=> $variable + +, $variable-=1 <=> $variable-, like C, do other things first, after + + or-
(2) + + $variable,-$variable, first + + or-, then do other operations
6) comparison operator: = = (left equals right),! = (left not equal to right), = = = (left equals right and data type is the same), >=,>,<,<=
7) logical Operator: | | Óor,&&óand,xor (when the left and right sides have and only one is true, return true)!
4. Comments:
Single-line Comment://, #
Multiline Comment:/* */
5. End of each statement with a number, same as Java
6. Define constants: Define ("Constans_name", value)
7, print the statement: printing, the same as the 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. Writing functions
1) define the function:
function function_name ($argument 1, $argument 2,......)/parameter
{
function code here;
}
2) Function call
Function_name ($argument 1, $argument 2,......); Formal parameters
3) Dynamic function call (Calls):


<title>Listing 6.5</title>


function SayHello () {//define functions SayHello
print "Hello
";
}
$function _holder = "SayHello"; Assigning a function name to a variable $function_holder
$function _holder (); The variable $function_holder becomes a reference to the function SayHello, and calling $function_holder () is equivalent to calling SayHello
?>


4) Variable scope:
Global variables:


<title>Listing 6.8</title>


$life = 42;
function Meaningoflife () {
Global $life;
/* To re-declare $life as a global variable here, access the global variable inside the function must be so, if the value of the variable is changed within the function, it will change in all code fragments */
Print "The Meaning of life is $life
";
}
Meaningoflife ();
?>


5) Use static


<title>Listing 6.10</title>


function numberedheading ($txt) {
static $num _of_calls = 0;
$num _of_calls++;
Print "

$num _of_calls. $txt

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

");
Numberedheading ("Doodads"); /* For the first call, the print $num_of_calls value is 2 because the variable is static and the static type is resident memory */
Print ("Finest in the World

");
?>


6) Value and address (reference):
Pass Value: function function_name ($argument)


<title>Listing 6.13</title>


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


Results: 10
Address: Funciton function_name (& $argument)


<title>Listing 6.14</title>


Function addfive (& $num) {
$num + = 5; /* passed in is a reference to the variable $num, so changing the value of the parameter $num is really changing the value stored in the variable $orignum physical memory */
}
$orignum = 10;
Addfive ($orignum);
Print ($orignum);
?>


Results: 15
7) Create anonymous function: Create_function (' string1 ', ' string2 '); Create_function is a PHP built-in function designed to create anonymous functions, accept two string arguments, the first is a parameter list, and the second is the body of a function


<title>Listing 6.15</title>


$my _anon = create_function (' $a, $b ', ' return $a + $b; ');
Print $my _anon (3, 9);
Prints 12
?>


8) Determine if the function exists: function_exists (function_name), the parameter is the letter name
10. Connect MySQL with PHP
1) Connection: &conn=mysql_connect ("localhost", "joeuser", "Somepass");
2) Close connection: Mysql_close ($conn);
3) database connected to connection: mysql_select_db (DB name, connection index);
4) Execute the SQL statement to MySQL: $result = mysql_query ($sql, $conn); Adding and removing change is this sentence
5) Retrieve data: Return record number: $number _of_rows = mysql_num_rows ($result);
Put the record in 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 mode): $_get[querystring]
12, turn to other pages: header ("location:http://www.samspublishing.com");
13. String manipulation:
1) Explode ("-", str) Ójava in Splite
2) Str_replace ($str 1, $str 2, $str 3) = + $STR 1 The string to find, $str 2 to replace the string, $str 3 to start looking for replacement from this string
3) Substr_replace:
14. Session:
1) Open Session:session_start (); You can also set session_auto_start=1 in PHP.ini, you don't have to write this sentence for each script, but the default is 0, you have to write.
2) Assign value to SESSION: $_session[session_variable_name]= $variable;
3) Visit SESSION: $variable =$_session[session_variable_name];
4) Destruction of Session:session_destroy ();
15, display the complete example of the classification:
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) {//If the number of rows returned is less than 1, then the description is not classified
$display _block = "

Sorry, no categories to browse.

";
} else {
while ($cats = mysql_fetch_array ($get _cats_res)) {//Put records in variable $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//Click this URL, refresh this page, 28th row read cat_id, display the corresponding category of entries

$cat _desc

";
if ($_get[cat_id] = = $cat _id) {//Select a category to see the entry below
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. = "
";
}
}
}
}
?>


<title>My Categories</title>





16. PHP Connection to access:
$dbc =new com ("adodb.connection");
$dbc->open ("Driver=microsoft Access Driver (*.mdb);d bq=c:member.mdb");
$rs = $dbc->execute ("SELECT * FROM TableName");
$i = 0;
while (! $rs->eof) {
$i +=1
$FLD 0= $rs->fields["UserName"];
$FLD 0= $rs->fields["Password"];
....
echo "$fld 0->value $fld 1->value ...";
$rs->movenext ();
}
$rs->close ();
?>

http://www.bkjia.com/PHPjc/318111.html www.bkjia.com true http://www.bkjia.com/PHPjc/318111.html techarticle 1, PHP fragment four kinds of representations. Standard tags:?php? Shorttags:?? Need to set Short_open_tag=on in PHP.ini, default is on asptags:%% need to set asp_tags=on in php.ini, default is ...

  • 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.