看板 rikaka
作者 標題 PHP quiz
時間 2012年03月23日 Fri. AM 03:23:39
PHP stands for PHP: Hypertext Preprocessor
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
<?php
echo "Hello World";
?>
function
function myTest()
{
echo $a; // local scope
}
*return values
function add($x,$y)
{
$total=$x+$y;
return $total;
}
Static Scope
When a function is completed, all of its variables are normally deleted.
However, sometimes you want a local variable to not be deleted.
To do this, use the static keyword when you first declare the variable:
static $rememberMe;
string:
The strpos() function
The strpos() function is used to search for a character/text within a string.
array
$cars=array("Saab","Volvo","BMW","Toyota");
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
for
for (init; condition; increment)
{
code to be executed;
}
form handling
==================================POST==================================
**html檔
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
URL不變
**welcome.php
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
**WHY POST
Information sent from a form with the POST method is invisible to others
and has no limits on the amount of information to send.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
==================================GET==================================
**html檔
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
**URL
When the user clicks the "Submit" button, the URL sent to the server could look something like this:
http://www.w3schools.com/welcome.php?fname=Peter&age=37
**php檔
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
The PHP $_REQUEST Variable
The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.
Example
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.
date function
Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:
<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y-m-d");
?>
PHP Date() - Adding a Timestamp
The optional timestamp parameter in the date() function specifies a timestamp.
If you do not specify a timestamp, the current date and time will be used.
The mktime() function returns the Unix timestamp for a date.
The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax for mktime()
mktime(hour,minute,second,month,day,year,is_dst)
To go one day in the future we simply add one to the day argument of mktime():
<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>
The output of the code above could be something like this:
Tomorrow is 2009/05/12
include()與require()
http://www.w3schools.com/php/php_includes.asp
Server Side Includes (SSI)
You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function.
The two functions are identical in every way, except how they handle errors:
include() generates a warning, but the script will continue execution
require() generates a fatal error, and the script will stop
These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages.
Server side includes saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. When the header needs to be updated, you can only update the include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all your web pages).
File Handling
$file=fopen("welcome.txt","r");
r
Read only. Starts at the beginning of the file
r+
Read/Write. Starts at the beginning of the file
w
Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+
Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a
Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+
Read/Append. Preserves file content by writing to the end of the file
x
Write only. Creates a new file. Returns FALSE and an error if file already exists
x+
Read/Write. Creates a new file. Returns FALSE and an error if file already exists
$file = fopen("test.txt","r");
if (feof($file)) echo "End of file"; //Check End-of-file
The fgets() function is used to read a single line from a file. //Reading a File Line by Line
Note: After a call to this function the file pointer has moved to the next line.
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
The fgetc() function is used to read a single character from a file.
fgets()
fgetc()
============================================
file upload
http://www.w3schools.com/php/php_file_upload.asp
--------------------------------------------------------------------------
cookie
http://www.w3schools.com/php/php_cookies.asp
--------------------------------------------------------------------------
session
http://www.w3schools.com/php/php_sessions.asp
--------------------------------------------------------------------------
http://www.w3schools.com/php/php_mail.asp
PHP allows you to send e-mails directly from a script.
mail(to,subject,message,headers,parameters)
---------------
Create a Connection to a MySQL Database
mysql_connect(servername,username,password);
==>
$con = mysql_connect("localhost","peter","abc123");
mysql_query($sql,$con);
mysql_close($con);
---------------
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysql_close($con);
?>
-------------
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
-------------
PHP Database ODBC
ODBC is an Application Programming Interface (API) that allows you to connect to a data source (e.g. an MS Access database).
Create an ODBC Connection
With an ODBC connection, you can connect to any database, on any computer in your network, as long as an ODBC connection is available.
Here is how to create an ODBC connection to a MS Access Database:
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver. Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
--
※ 作者: rikaka 時間: 2012-03-23 03:23:39
※ 看板: rikaka 文章推薦值: 0 目前人氣: 0 累積人氣: 104
回列表(←)
分享