Novice Learning: Dynamic Web PHP Basic Learning Notes

Source: Internet
Author: User
Tags anonymous define expression functions ini mysql reference variable scope

1, PHP fragment four kinds of representation.

Standard tags:<?php?>

Short tags:<? ?> need to set short _open_tag=on in PHP.ini, by default on

ASP Tags: <%%> need to set asp_tags=on in php.ini, default is off

Script Tags:<script language= "PHP" ></script>

2, PHP variables and data types

1 $variable, the variable begins with a letter, _, and cannot have spaces.

2) assigning value $variable=value;

3 weak type, directly assigned, do not need to display the declaration data type

4 Basic Data type: Integer,double,string,boolean,object (object or Class), array (arrays)

5 Special Data type: Resourse (reference to third party resources (such as database), null (NULL, uninitialized variable)

3. Operator

1) assignment Operator: =

2 arithmetic operator: +,-,*,/,% (modulo)

3) connection operator:. , regardless of the operand, as string, the result returns a string

4) Combined Assignment operators total assignment operator: +=,*=,/=,-=,%=,.=

5) automatically incrementing and decrementing automatically add and subtract operators:

(1) $variable +=1 <=> $variable + + $variable-=1 <=> $variable-as in C, do other things first, after + + or-

(2) + + $variable,-$variable, first + + or-, do other operations

6 comparison operator: = = (left equal to right),! = (left not equal to right), = = = (left is equal to 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. Note:

Single Note://, #

Multi-line Comment:/*/

5, each statement at the end of the, the same as Java

6. Defining Constants : Define ("Constans_name", value)

7, print statement : print, the same as the 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 RESULT1:

Execute this if expression results in RESULT1

Break

Case RESULT2:

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 IS executed

while (expression);

5) for statement:

for (initialization expression; test expression; modification expression) {

Code to IS executed

}

6) Break;continue

9. Write function

1) Define functions:

function function_name ($argument 1, $argument 2,......)//formal parameter

{

function code here;

}

2) Function call

Function_name ($argument 1, $argument 2,......); Formal parameters

3 Dynamic function call (Calls):

1:

2:

3: <title>listing 6.5</title>

4:

5: <body>

6: <?php

7:function SayHello () {//define function SayHello

8:print "hello<br>";

9:}

$function _holder = "SayHello"; Assigning a function name to a variable $function_holder

One: $function _holder (); Variable $function_holder becomes a reference to the function SayHello, and calling $function_holder () is equivalent to calling SayHello

A:?>

</body>

:

4 Variable Scope:

Global variables:

1:

2:

3: <title>listing 6.8</title>

4:

5: <body>

6: <?php

7: $life = 42;

8:function Meaningoflife () {

9:global $life;

/* Here, re-declare $life as a global variable, access the global variable inside the function must be this way, if you change the value of the variable within the function, it will change in all code fragments.

10:print "The meaning is $life <br>";

11:}

12:meaningoflife ();

:?>

: </body>

:

5) using static

1:

2:

3: <title>listing 6.10</title>

4:

5: <body>

6: <?php

7:function numberedheading ($txt) {

8:static $num _of_calls = 0;

9: $num _of_calls++;

10:print "

11:}

12:numberedheading ("Widgets"); When first called, the print $num_of_calls value is 1

13:print ("We build a fine range of widgets<p>");

14:numberedheading ("Doodads"); /* The first call, the print $num_of_calls value is 2, because the variable is static type, the static is resident memory.

15:print ("Finest in the World<p>");

:?>

: </body>

:

6) (value) and address (reference):

Pass Value: function function_name ($argument)

1:

2:

3: <title>listing 6.13</title>

4:

5: <body>

6: <?php

7:function addfive ($num) {

8: $num = 5;

9:}

$orignum = 10;

11:addfive (& $orignum);

12:print ($orignum);

:?>

: </body>

:

Results: 10

Address: Funciton function_name (& $argument)

1:

2:

3: <title>listing 6.14</title>

4:

5: <body>

6: <?php

7:function addfive (& $num) {

8: $num = 5; /* Passing is the reference of variable $num, so changing the value of the parameter $num is to actually change the value of the variable $orignum physical memory.

9:}

$orignum = 10;

11:addfive ($orignum);

12:print ($orignum);

:?>

: </body>

:

Results: 15

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

1:

2:

3: <title>listing 6.15</title>

4:

5: <body>

6: <?php

7: $my _anon = create_function (' $a, $b ', ' return $a + $b; ');

8:print $my _anon (3, 9);

9://Prints 12

Ten:?>

One: </body>

8 determine whether the function exists: function_exists (function_name), the parameter is the name of the letter

10. Connect MySQL with PHP

1) Connection: &conn=mysql_connect ("localhost", "joeuser", "Somepass");

2) Close connection: Mysql_close ($conn);

3 Database and connection establishment contact: mysql_select_db (db name, connection index);

4 The SQL statement to the MySQL implementation: $result = mysql_query ($sql, $conn); It's the same thing about checking and deleting.

5) Retrieving data: Returns the number of records: $number _of_rows = mysql_num_rows ($result);

Put records in an array: $newArray = Mysql_fetch_array ($result);

Example:

  1: <?php
  2://Open the connection
  3: $conn = mysql_connect ("localhost", "joeuser", "some Pass ");
  4://Pick the database to use
  5:mysql_select_db ("TestDB", $conn);
  6://Create the SQL statement
  7: $sql = "SELECT * from TestTable";
  8://Execute the SQL statement
  9: $result = mysql_query ($sql, $conn) or Die (Mysql_error ());
&NBSP;10://go through each row of the result set and display data
 11:while ($newArray = mysql_fetch_array ($resu LT) {
 12:    //Give a name to the fields
 13:     $id = $newA rray[' id '];
 14:     $testField = $newArray [' Testfield '];
 15:    //echo The results onscreen
 16:     echo "The ID is $id and the text is $testField <br> ";
 17:}
 18:?

11, accept the form element: $_post[form element name],

such as <input Type=text Name=user>ó$_post[user]

Accept querystring median in URL (Get method): $_get[querystring]

12, turn to other pages:header ("location:http://www.webjx.com");

13. String Operation:

1 Splite in Explode ("-", str) Ójava

2 Str_replace ($str 1, $str 2, $str 3) => $str 1 The string to look up, $STR 2 to replace the string, $str 3 to find the replacement from this string

3) Substr_replace:

14, Session:

1) Open Session:session_start (); You can also set the session_auto_start=1 in php.ini, you do not have to write this sentence for each script, but the default is 0, you must 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 classification:

1: <?php

2://connect to Database

3: $conn = mysql_connect ("localhost", "joeuser", "Somepass")

4:or die (Mysql_error ());

5:mysql_select_db ("TestDB", $conn) or Die (Mysql_error ());

6:

7: $display _block = "

8: <p>select A category to the items.</p> ";

9:

//show Categories-A

One: $get _cats = "SELECT ID, Cat_title, cat_desc from

12:store_categories ORDER by Cat_title ";

$get _cats_res = mysql_query ($get _cats) or Die (Mysql_error ());

14:

15:if (mysql_num_rows ($get _cats_res) < 1) {//If the number of rows returned is less than 1, no classification is indicated

: $display _block = "<p><em>sorry, no categories to browse.</em></p>";

} else {

18:

19: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]);

23:

$display _block. = "<p><strong><a

25:href=\ "$_server[php_self][u1]? cat_id= $cat _id\" > $cat _title</a></strong>//Click this URL to refresh this page, Line 28th reads cat_id, showing the entries for the corresponding categories

: <br> $cat _desc</p> ";

27:

28:if ($_get[cat_id] = = $cat _id) {//Select a category, look at the following entries

://get Items

$get _items = "SELECT ID, Item_title, item_price

31:from store_items where cat_id = $cat _id

32:order by Item_title ";

A: $get _items_res = mysql_query ($get _items) or Die (Mysql_error ());

34:

35:if (mysql_num_rows ($get _items_res) < 1) {

$display _block = "<p><em>sorry, no items in

37:this category.</em></p> ";

/else {

39:

$display _block. = "<ul>";

41:

42:while ($items = mysql_fetch_array ($get _items_res)) {

$item _id = $items [id];

$item _title = stripslashes ($items [item_title]);

A: $item _price = $items [Item_price];

46:

$display _block. = "<li><a

48:href=\ "showitem.php?item_id= $item _id\" > $item _title</a>

: </strong> (\$ $item _price) ";

[U2] 50:}

51:

$display _block. = "</ul>";

53:}

54:}

55:}

56:}

?>

: <HTML>

: <HEAD>

<title>my categories</title>

: </HEAD>

: <BODY>

A:? Print $display _block;?>

: </BODY>

: </HTML>

16, PHP connection 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 ();
?>



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.