Title: Chapter 2 Getting Started with PHP PHP Programming with MySQL
1Chapter 2Getting Started with PHPPHP
Programming with MySQL
2Objectives
- Install and configure a Web server
- Install and configure PHP
- Install and configure MySQL
- Create basic PHP scripts
3Understanding Binary and Source Code Installations
- Binary format (or binaries) refer to compiled
files, such as executable installation programs - Source code is the original programming code in
which an application was written - Source code must be compiled, or processed, and
assembled into an executable format before it is
used - Compiled programs only need to be recompiled when
their code changes
4Installing and Configuring a Web Server
- Apache is the most popular Web server software
used on the Internet - Microsoft IIS for Windows is the second most
popular server software - In Windows, a service refers to a program that
performs a specific function to support other
programs
5Installing and Running Apache on UNIX and Linux
- Go to http/httpd.apache.org/download.cgi
- Run the gunzip command gunzip
httpd-2.0.52.tar.gz - Run the tar commandtar xvf httpd-2.0.52.tar
- Change to the http-2.0.52 directorycd
httpd-2.0.52 - Run the configure command./configure
6Installing and Running Apache on UNIX and Linux
(continued)
- Compile the Apache source code with the make
command - Run the make install command in the httpd-2.0.52
directory - Start, stop, and restart Apache using the
apachectl control script
7Installing and Running Apache on Windows
- Go to http/httpd.apache.org/download.cgi
- Download the apache_2.0.52-win32-x86-no_ssl.msi
installation file - Navigate to the installation file and from the
Welcome screen, click Next - Accept the terms of the License Agreement, click
Next - Read the contents of the Read This First screen,
click Next
8Installing and Running Apache on Windows
(continued)
- Accept the default values, click Next
- Select a Typical installation, click Next
- Accept the default Destination Folder directory,
click Next - Click Back to make changes or click Install to
finish - Click Finish
9Installing and Running Internet Information
Services on Windows
- Open the Control Panel from the Start menu
- If using Windows XP, select Switch to Classic
View - Select the Add or Remove Programs icon
- Click Add/Remove Windows Components
- Click the check box next to Internet Information
Services (IIS), click Next
10Installing and Running IIS on Windows (continued)
- After the installation is complete, click Finish
- If prompted, restart Windows otherwise close the
Control Panel
11Testing Your Web Server
- Open your Web browser
- Type http//localhost/ in the Address box, click
Enter -
-
- Figure 2-2 Apaches default Web
page
12Testing Your Web Server (continued)
- Type http//127.0.0.1/ in the Address box, click
Enter -
- Figure 2-3 Web page informing you that IIS
is running
13Configuring Apache
- To configure ports and other settings you must
edit the httpd.conf file - For UNIX/Linux
- /usr/local/apache2/conf
- For Windows
- C\Program Files\Apache Group\Apache2\conf
- Lines that begin with the pound sign () are
informational comments - Lines without pound signs contain directives
14Configuring Apache (continued)
15Configuring Apache (continued)
- Directives define information about how a program
should be configured - The DocumentRoot directive identifies the default
directory from where Apache serves Web pages - The Alias directive identifies other directories
that Apache can use to serve Web pages
16Configuring Internet Information Services
-
- Figure 2-5 Default Web Site Properties
dialog box
17Installing PHP on UNIX and Linux Systems Running
Apache
- Go to http//www.php.net/downloads.php
- Run the gunzip command gunzip php-5.0.3.tar.gz
- Run the tar commandtar xvf php-5.0.3.tar
- Change to the php-5.0.3 directorycd php-5.0.3
- Run the configure command./configure
18Installing PHP on UNIX and Linux Systems Running
Apache (continued)
- Compile the PHP source code with the make
command - Run the make install command in the php-5.0.3
directory - Specify which configuration file you want to use
with PHPcp php.ini-dist /usr/local/lib/php.ini
19Installing PHP Windows Running Apache or IIS
- Go to http//www.php.net/downloads.php and
download the latest Windows binary installer - Navigate to the installation file and from the
Welcome screen, click Next - In the License Agreement screen, click I Agree
- In the Installation Type screen, select Standard,
then click Next - Accept the default destination location, click
Next
20Installing PHP Windows Running Apache or IIS
(continued)
- In the Mail Configuration screen, accept the
default values of localhost, click Next - In the Server Type screen, select the type of Web
server that you want to use with PHP, click Next - In the Start Installation screen, click Next to
begin installation - Click OK to close the dialog box of the Web
server you selected
21Configuring Apache for PHP on UNIX/Linux Platforms
- Open the httpd.conf file from the
/usr/local/apache2/conf directory - Search for the LoadModule directiveLoadModule
php5_module libexec/libphp5.so - Add the AddType directive to the end of the
fileAddType application/x-httpd-php .php - Save and close the httpd.conf file
- Restart Apache with the command/usr/local/apache
2/bin/apachectl restart
22Configuring Apache for PHP on Windows
- Click the Start menu and point to All Programs
- Select the Edit the Apache httpd.config
Configuration File command - Add the following to the end of the
fileScriptAlias /PHP/ C/PHP/AddType
application/x-httpd-php .phpAction
application/x-httpd-php /PHP/php-cgi.exe - Save and close the httpd.conf file
- Restart Apache and select the Restart command
23Configuring PHP
-
- Figure 2-6 The php.ini configuration file
24Installing and Configuring MySQL on UNIX and Linux
- Go to http//dev.mysql.com/downloads/ and
download the latest version of MySQL - Create a separate group and user named for
running MySQLgroupadd mysqluseradd -g mysql
mysql - Run the gunzip mysql-4.1.9.tar.gz command
- Run the tar xvf mysql-4.1.9.tar command
- Change to the mysql-4.1.9 directory
25Installing and Configuring MySQL on UNIX and
Linux (continued)
- Run the ./configure command
- Compile the MySQL code with the make command
- Run the make install command
- Change to the scripts directory
- Run the mysql_install_db --usermysql script
- Run the ownership commandschown -R root
/usr/local/mysqlchown -R mysql
/usr/local/mysql/varchgrp -R mysql
/usr/local/mysql
26Installing and Configuring MySQL on Windows
- Go to http//dev.mysql.com/downloads/
- Open Windows Explorer or My Computer and start
the MySQL installation - In the Welcome screen, click Next to start the
installation - Accept the default setup type Typical, click
Next - Click Back to make changes or click Install to
continue
27Installing and Configuring MySQL on Windows
(continued)
- Create a new account or skip sign-in, click Next
- In the Wizard Completed screen, click Finish
- In the first screen of the MySQL Server Instance
Configuration Wizard, click Next - In the Configuration Type screen, select Standard
Configuration, click Next
28Installing and Configuring MySQL on Windows
(continued)
- In the Windows Options screen, accept the default
values (do not select the Include Bin Directory
in Windows PATH check box), click Next - In the Security Options screen, deselect the
Modify Security Settings check box, click Next - Click Back to change any of the configuration
operations or Execute to finish
29Testing the MySQL Server
- Check to see if MySQL is running
- For UNIX/Linux systems
- /usr/local/mysql/bin/mysqld_safe --usermysql
- For Windows, use the Services window
- Run the mysqladmin version command
- For UNIX/Linux systems
- /usr/local/mysql/bin/mysqladmin version
- For Windows, change to the C\Program
Files\MySQL\MySQL Server 4.1\bin\ directory and
run - mysqladmin version
30Creating Basic PHP Scripts
- Embedded scripting languages (JavaScript or PHP)
refer to code that is embedded within a Web page
(either an HTML or XHTML document) - This code is typed directly into a Web page as a
separate section - A Web page document containing PHP code must have
an extension of .php - PHP code is never sent to a clients Web browser
31Creating Basic PHP Scripts (continued)
- The Web page generated from the PHP code, and
HTML or XHTML elements found within the PHP file,
is returned to the client - A PHP file that does not contain any PHP code
should have an .html extension - .php is the default extension that most Web
servers use to process PHP scripts
32Creating PHP Code Blocks
- Code declaration blocks are separate sections
within a Web page that are interpreted by the
scripting engine - There are four types of code declaration blocks
- Standard PHP script delimiters
- The ltscriptgt element
- Short PHP script delimiters
- ASP-style script delimiters
33Standard PHP Script Delimiters
- A delimiter is a character or sequence of
characters used to mark the beginning and end of
a code segment - The standard method of writing PHP code
declaration blocks is to use the lt?php and ?gt
script delimiters - The individual lines of code that make up a PHP
script are called statements
34The ltscriptgt Element
- The ltscriptgt element identifies a script section
in a Web page document - For client-side scripting, the type attribute of
the ltscriptgt element indicates which scripting
language and version is being used - When the ltscriptgt element is used with PHP, you
do not include the type attribute
35Short PHP Script Delimiters
- The syntax for the short PHP script delimiters is
- lt? statements ?gt
- Short delimiters can be disabled in a Web
servers php.ini configuration file - PHP scripts will not work if your Web site ISP
does not support short PHP script delimiters - Short delimiters can be used in XHTML documents,
but not in XML documents
36ASP-Style Script Delimiters
- The syntax for the ASP-style script delimiters is
- lt statements gt
- ASP-style script delimiters can be used in XHTML
documents, but not in XML documents - ASP-style script delimiters can be enabled or
disabled in the php.ini configuration file - To enable or disable ASP-style script delimiters,
assign a value of On or Off to the asp_tags
directive in the php.ini configuration file
37Understanding Functions
- A function refers to a procedure that performs a
specific task - To execute a function, you must invoke, or call,
it from somewhere in the script - A function call is the function name followed by
any data that the function needs - The data (in parentheses following the function
name) are called arguments or actual parameters - Sending data to a called function is called
passing arguments
38Displaying Script Results
- To return to the client the results of any
processing that occurs within a PHP code block,
you must use an echo() statement or the print()
statement - The echo() and print() statements create new text
on a Web page that is returned as a response to a
client
39Displaying Script Results (continued)
-
-
- Figure 2-13 PHP Diagnostic Information Web
page
40Displaying Script Results (continued)
- The echo() and print() statements are language
constructs of the PHP programming language - A programming language construct refers to a
built-in feature of a programming language - The echo() and print() statements are virtually
identical except - The print() statement returns a value of 1 if it
is successful - It returns a value of 0 if it is not successful
41Displaying Script Results (continued)
- Use the echo() and print() statements to return
the results of a PHP script within a Web page
that is returned to a client - A text string, or literal string, is text that is
contained within double or single quotation marks - To pass multiple arguments to the echo() and
print() statements, separate them with commas
like arguments passed to a function
42Creating Multiple Code Declaration Blocks
- For multiple script sections in a document,
include a separate code declaration block for
each section - ...
- lt/headgt
- ltbodygt
- lth1gtMultiple Script Sectionslt/h1gt
- lth2gtFirst Script Sectionlt/h2gt
- lt?php echo ltpgtOutput from the first script
section.lt/pgt - ?gt
- lth2gtSecond Script Sectionlt/h2gt
- lt?php echo ltpgtOutput from the second script
section.lt/pgt - ?gt
- lt/bodygt
- lt/htmlgt
43Creating Multiple Code Declaration Blocks
(continued)
- PHP code declaration blocks execute on a Web
server before a Web page is sent to a client - ...
- lt/headgt
- ltbodygt
- lth1gtMultiple Script Sectionslt/h1gt
- lth2gtFirst Script Sectionlt/h2gt
- ltpgtOutput from the first script section.lt/pgt
- lth2gtSecond Script Sectionlt/h2gt
- ltpgtOutput from the second script section.lt/pgt
- lt/bodygt
- lt/htmlgt
44Creating Multiple Code Declaration Blocks
(continued)
-
- Figure 2-17 Output of a document with two PHP
script sections
45Case Sensitivity in PHP
- Programming language constructs in PHP are mostly
case insensitive - lt?php
- echo ltpgtExplore ltstronggtAfricalt/stronggt, ltbr
/gt - Echo ltstronggtSouth Americalt/stronggt, ltbr /gt
- ECHO and ltstronggtAustralialt/stronggt!lt/pgt
- ?gt
46Adding Comments to a PHP Script
- Comments are nonprinting lines placed in code
such as - The name of the script
- Your name and the date you created the program
- Notes to yourself
- Instructions to future programmers who might need
to modify your work
47Adding Comments to a PHP Script (continued)
- Line comments hide a single line of code
- Add // or before the text
- Block comments hide multiple lines of code
- Add / to the first line of code
- And / after the last character in the code
48Adding Comments to a PHP Script (continued)
- lt?php
- /
- This line is part of the block comment.
- This line is also part of the block comment.
- /
- echo lth1gtComments Examplelt/h1gt // Line
comments can follow - code statements
- // This line comment takes up an entire line.
- This is another way of creating a line comment.
- / This is another way of creating
- a block comment. /
- ?gt
49Summary
- Binary format (or binaries) refers to files
that can be in the form of executable
installation programs - Source code is the original programming code in
which an application was written - Directives define information about how a program
should be configured - JavaScript and PHP are both referred to as
embedded scripting languages
50Summary (continued)
- You write PHP scripts within code declaration
blocks, which are separate sections within a Web
page that are interpreted by the scripting engine - The individual lines of code that make up a PHP
script are called statements - The term function refers to a procedure (or
individual statements grouped into a logical
unit) that perform a specific task
51Summary (continued)
- The term programming language construct refers to
a built-in feature of a programming language - Programming language constructs in PHP are mostly
case insensitive, although there are some
exceptions - Comments are nonprinting lines that you place in
code to contain various types of remarks