dynamic website
DESCRIPTION
TRANSCRIPT
1
Including Multiple Files
Pengaturcaraan PHP
Every PHP script can consist of a single file that contains all of the required HTML and PHP code. But as you develop more complex Web sites, you'll see that this methodology has many limitations. PHP can readily make use of external files, a capability that allows you to divide your scripts into their distinct parts. Frequently you will use external files to extract your HTML from your PHP or to separate out commonly used processes.
Pengaturcaraan PHP
2
PHP has four functions for using external files: include(), include_once(), require(), and require_once(). To use them, your PHP script would have a line like:
Pengaturcaraan PHP
Pengaturcaraan PHP
3
Both functions also have a *_once() version, which guarantees that the file in question is included only once regardless of how many times a script may (presumably inadvertently) attempt to include it.
Pengaturcaraan PHP
In this first example, included files are used to separate HTML formatting from PHP code. Then, the rest of the examples in this lesson will be able to have the same appearance without the need to rewrite the HTML every time. The concept results in a template system, an easy way to make large applications consistent and manageable.
In our example, we've used a Cascading Style Sheet file to control the layout of all pages. The style sheet code is referenced in each HTML file for consistent styles and formatting throughout the Web site.
4
Pengaturcaraan PHP
Using PHP Redux
Pengaturcaraan PHP
5
In many instances, two separate scripts for handling HTML forms can be used: one that displays the form and another that receives it. While there's certainly nothing wrong with this method, you may find your scripts run more efficiently when the entire process is in one script.
To have one page both display and handle a form, use a conditional.
Pengaturcaraan PHP
To determine if the form has been submitted, set a $_POST variable (assuming that the form uses the POST method, of course). For example, you can check $_POST['submitted'], assuming that's the name of a hidden input type in the form.
Pengaturcaraan PHP
6
If you want a page to handle a form and then display it again (e.g., to add a record to a database and then give an option to add another), use
Using the preceding code, a script will handle a form if it has been submitted and display the form every time the page is loaded.
Pengaturcaraan PHP
Write the conditional for handling the form. As mentioned previously, if the $_POST ['submitted'] variable is set, we know that the form has been submitted and we can process it. This variable will be created by a hidden input in the form that will act as a submission indicator.
Pengaturcaraan PHP
7
Pengaturcaraan PHP
Pengaturcaraan PHP
8
Pengaturcaraan PHP
Pengaturcaraan PHP
9
Pengaturcaraan PHP
You can also have a form submit back to itself by having PHP print the name of the current script — stored in $_SERVER['PHP_ SELF'] — as the action attribute:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Pengaturcaraan PHP
10
Pengaturcaraan PHP
Pengaturcaraan PHP
11
Pengaturcaraan PHP
Pengaturcaraan PHP
12
Making Sticky Forms
Pengaturcaraan PHP
You've certainly come across sticky forms, even if you didn't know that's what they were called. A sticky form is simply a standard HTML form that remembers how you filled it out. This is a particularly nice feature for end users, especially if you are requiring them to resubmit a form (for instance, after filling it out incorrectly in the first place).
To preset what's entered in a text box, use its value attribute:
Pengaturcaraan PHP
13
To have PHP preset that value, print the appropriate variable:
Pengaturcaraan PHP
Pengaturcaraan PHP
14
Pengaturcaraan PHP
Pengaturcaraan PHP
15
Pengaturcaraan PHP
Creating Your Own Functions
Pengaturcaraan PHP
16
PHP has a lot of built-in functions, addressing almost every need. More importantly, though, it has the capability for you to define and use your own functions for whatever purpose. The syntax for making your own function is
The name of your function can be any combination of letters, numbers, and the underscore, but it must begin with either a letter or the underscore. The main restriction is that you cannot use an existing function name for your function (print, echo, isset, and so on).
Pengaturcaraan PHP
In PHP, function names are case-insensitive (unlike variable names), so you could call that function in several different ways such as:
Pengaturcaraan PHP
17
Pengaturcaraan PHP
Pengaturcaraan PHP
18
Pengaturcaraan PHP
Pengaturcaraan PHP
19
Pengaturcaraan PHP
Creating a Function That Takes Arguments
Pengaturcaraan PHP
20
Just like PHP's built-in functions, those you write can take arguments (also called parameters). For example, the print() function takes what you want sent to the browser as an argument and strlen() takes a string whose character length will be determined.
A function can take any number of arguments that you choose, but the order in which you put them is critical. To allow for arguments, add variables to your function's definition:
Pengaturcaraan PHP
You can then call the function as you would any other function in PHP, sending literal values or variables to it:
Pengaturcaraan PHP
21
Pengaturcaraan PHP
Pengaturcaraan PHP
22
Pengaturcaraan PHP
Setting Default Argument Values
Pengaturcaraan PHP
23
Another variant on defining your own functions is to preset an argument's value. To do so, assign the argument a value in the function's definition:
The end result of setting a default argument value is that that particular argument becomes optional when calling the function. If a value is passed to it, the passed value is used; otherwise, the default value is used.
Pengaturcaraan PHP
You can set default values for as many of the arguments as you want, as long as those arguments come last in the function definition. In other words, the required arguments should always be first. With the example function just defined, any of these will work:
However, greet() will not work, and there's no way to pass $greeting a value without passing one to $name as well.
Pengaturcaraan PHP
24
Pengaturcaraan PHP
Pengaturcaraan PHP
25
Pengaturcaraan PHP
Pengaturcaraan PHP
26
Pengaturcaraan PHP
Pengaturcaraan PHP
27
Returning Values from a Function
Pengaturcaraan PHP
Some, but not all, functions return values. For example, print() will return either a 1 or a 0 indicating its success, whereas echo() will not. As another example, the strlen() function returns a number correlating to the number of characters in a string.
To have your function return a value, use the return statement.
Pengaturcaraan PHP
28
The function can return a value (say a string or a number) or a variable whose value has been created by the function. When calling this function, you can assign the returned value to a variable:
$my_sign = find_sign ('October', 23);
or use it as a parameter to another function:
print find_sign ('October', 23);
Pengaturcaraan PHP
Pengaturcaraan PHP
29
Pengaturcaraan PHP
Pengaturcaraan PHP
30
Pengaturcaraan PHP
Variable Scope
Pengaturcaraan PHP
31
Variable scope is a tricky but important concept. Every variable in PHP has a scope to it, which is to say a realm in which the variable (and therefore its value) can be accessed. For starters, variables have the scope of the page in which they reside. So if you define $var, the rest of the page can access $var, but other pages generally cannot (unless you use special variables).
Since included files act as if they were part of the original (including) script, variables defined before the include() line are available to the included file. Further, variables defined within the included file are available to the parent (including) script after the include() line.
Pengaturcaraan PHP
All of this becomes murkier when using your own defined functions. These functions have their own scope, which means that variables used within a function are not available outside of it, and variables defined outside of a function are not available within it. For this reason, a variable inside of a function can have the same name as one outside of it and still be an entirely different variable with a different value. This is a confusing concept for most beginning programmers.
Pengaturcaraan PHP
32
To alter the variable scope within a function, you can use the global statement.
In this example, $var inside of the function is now the same as $var outside of it. This means that the function $var already has a value of 20, and if that value changes inside of the function, the external $var's value will also change.
Pengaturcaraan PHP
Pengaturcaraan PHP
33
Pengaturcaraan PHP
Pengaturcaraan PHP
34
Pengaturcaraan PHP
Pengaturcaraan PHP
35
Date and Time Functions
Pengaturcaraan PHP
PHP has several date- and time-related functions for use in your scripts. The most important of these is the aptly named date() function, which returns a string of text for a certain date and time according to a format you specify.
The timestamp is an optional argument representing the number ofseconds since the Unix Epoch (midnight on January 1, 1970) for the date in question. It allows you to get information, like the day of the week, for a particular date. If a timestamp is not specified, PHP will just use the current time on the server.
Pengaturcaraan PHP
36
There are myriad formatting parameters available, and these can be used in conjunction with literal text. For example,
Pengaturcaraan PHP
PM AM or PM A
am am or pm a
18 seconds s
45 minutes i
18 hour, 24-hour format as 2 digits H
06 hour, 12-hour format as 2 digits h
18 hour, 24-hour format as 1 or 2 digits G
6 hour, 12-hour format as 1 or 2 digits g
Mon day of the week as 3 letters D
Monday day of the week l (lowercase L)
08 day of the month as 2 digits d
8 day of the month as 1 or 2 digits j
Feb month as 3 letters M
February month F
02 month as 2 digits m
2 month as 1 or 2 digits n
05 year as 2 digits y
2005 year as 4 digits Y
ExampleMeaningCharacter
37
You can find the timestamp for a particular date using the mktime() function.
Pengaturcaraan PHP
Finally, the getdate() function can be used to return an array of values for a date and time. For example,
This function also takes an optional timestamp argument. If thatargument is not used, getdate() returns information for the current date and time.
Pengaturcaraan PHP
38
The getdate() function returns this associative array.
47 seconds seconds
56 minutes minutes
11 hours hours
Sunday day of the week weekday
25 day of the month mday
December month name month
12 month mon
2005 year year
ExampleValueKey
Pengaturcaraan PHP
Pengaturcaraan PHP
39
Pengaturcaraan PHP
Pengaturcaraan PHP
40
Pengaturcaraan PHP
Pengaturcaraan PHP
41
Pengaturcaraan PHP
Pengaturcaraan PHP
42
Date – “time()”To display current time and dateSyntax : time();
Example :echo time();
Output = 23// sample output: 1060751270//this represent August 12th, 2003 at 10:07PM
Pengaturcaraan PHP
Date – “mktime”To display date and time in timestamp
Syntax : mktime();
Example :echo mktime(); or$tarikh = $mktime(4,15,0,8,23,2003);$tarikh = date(“d/m/Y”,mktime()+50);
Pengaturcaraan PHP
43
Others Syntax :
$tomorrow = mktime (0,0,0,date("m") ,date("d")+1,date("Y"));
$lastmonth = mktime (0,0,0,date("m")-1,date("d"), date("Y"));
$nextyear = mktime (0,0,0,date("m"), date("d"), date("Y")+1);
Pengaturcaraan PHP
Sending Email
Pengaturcaraan PHP
44
Using PHP makes sending email very easy. On a properly configured server, the process is as simple as using the mail() function.
The $to value should be an email address or a series of addresses, separated by commas. The $subject value will create the email's subject line, and $body is where you put the contents of the email. Use the newline character (\n) within double quotation marks when creating your body to make the text go over multiple lines.
Pengaturcaraan PHP
The mail() function takes a fourth, optional parameter for additional headers. This is where you could set the From, Reply-To, Cc, Bcc, and similar settings. For example,
Pengaturcaraan PHP
45
To use multiple headers of different types in your email, separate each with \r\n:
Pengaturcaraan PHP
Pengaturcaraan PHP
46
Pengaturcaraan PHP
Pengaturcaraan PHP
47
Pengaturcaraan PHP
Pengaturcaraan PHP
48
End
Pengaturcaraan PHP