A good PHP basic Learning Notes _php Foundation

Source: Internet
Author: User
Tags anonymous 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 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 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):
<title>listing 6.5</title>
<body>
<?php
function SayHello () {//define functions SayHello
print "hello<br>";
}
$function _holder = "SayHello"; Assigning a function name to a variable $function_holder
$function _holder (); Variable $function_holder becomes a reference to the function SayHello, and calling $function_holder () is equivalent to calling SayHello
?>
</body>
4 Variable Scope:
Global variables:
<title>listing 6.8</title>
<body>
<?php
$life = 42;
function Meaningoflife () {
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.
Print "The meaning is $life <br>";
}
Meaningoflife ();
?>
</body>
5) using static
<title>listing 6.10</title>
<body>
<?php
function numberedheading ($txt) {
static $num _of_calls = 0;
$num _of_calls++;
Print "}
Numberedheading ("Widgets"); When first called, the print $num_of_calls value is 1
Print ("We build a fine range of widgets<p>");
Numberedheading ("Doodads"); /* The first call, the print $num_of_calls value is 2, because the variable is static type, the static is resident memory.
Print ("Finest in the World<p>");
?>
</body>
6) (value) and address (reference):
Pass Value: function function_name ($argument)
<title>listing 6.13</title>
<body>
<?php
function Addfive ($num) {
$num + 5;
}
$orignum = 10;
Addfive (& $orignum);
Print ($orignum);
?>
</body>
Results: 10
Address: Funciton function_name (& $argument)
<title>listing 6.14</title>
<body>
<?php
Function addfive (& $num) {
$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.
}
$orignum = 10;
Addfive ($orignum);
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
<title>listing 6.15</title>
<body>
<?php
$my _anon = create_function (' $a, $b ', ' return $a + $b; ');
Print $my _anon (3, 9);
Prints 12
?>
</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:
<?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, 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 $testField <br>";
}
?>
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.samspublishing.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:
<?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 the items.</p> ";
Show categories
$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, no category is indicated
$display _block = "<p><em>sorry, no categories to browse.</em></p>";
} 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. = "<p><strong><a
Href= "$_server[php_self][u1]? cat_id= $cat _id" > $cat _title</a></strong>//click on this URL, refresh this page, read the 28th line cat_id, Show entries for corresponding categories
<br> $cat _desc</p> ";
if ($_get[cat_id] = = $cat _id) {//Select a category, look at the following entry
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><a
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);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.