C C C C: Albert Morita UCR Libraries Systems Dept. December 9, 2004
C C C C: Albert Morita UCR Libraries Systems Dept. December 9, 2004
Albert Morita UCR Libraries Systems Dept. December 9, 2004 Version 2.0
Agenda
1. Brief History of PHP 2. Getting started 3. Examples
Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server Operating Systems: UNIX (HP-UX,OpenBSD,Solaris,Linux), Mac OSX, Windows NT/98/2000/XP/2003 Supported Databases: Adabas D, dBase,Empress, FilePro (readonly), Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis,Unix dbm
PHP
Software Platform Development Tools Free Free (Linux) Free PHP Coder, jEdit
Getting Started
1. How to escape from HTML and enter PHP mode
PHP parses a file by looking for one of the special tags that tells it to start interpreting the text as PHP code. The parser then executes all of the code it finds until it runs into a PHP closing tag.
HTML
PHP CODE
HTML
Ending tag
?> ?> ?>
Notes
Preferred method as it allows the use of PHP with XHTML Not recommended. Easier to type, but has to be enabled and may conflict with XML Always available, best if used when FrontPage is the HTML editor Not recommended. ASP tags support was added in 3.0.4
<%
%>
Getting Started
2. Simple HTML Page with PHP The following is a basic example to output text using PHP.
<html><head> <title>My First PHP Page</title> </head> <body> <?php echo "Hello World!"; ?> </body></html>
Copy the code onto your web server and save it as test.php. You should see Hello World! displayed. Notice that the semicolon is used at the end of each line of PHP code to signify a line break. Like HTML, PHP ignores whitespace between lines of code. (An HTML equivalent is <BR>)
Getting Started
3.
<?php $today_dayofweek = date(w); if ($today_dayofweek == 4){ echo Today is Thursday!; } else{ echo Today is not Thursday.; } ?>
Getting Started
3.
Getting Started
3. Using conditional statements
If we run the script on a Thursday, we should see: Today is Thursday. On days other than Thursday, we will see: Today is not Thursday.
<?php $today_dayofweek = date(w); if ($today_dayofweek == 4){ echo Today is Thursday!; } else{ echo Today is not Thursday.; } ?>
Examples
PHP is a great way to implement templates on your website. How to implement a simple page counter
Examples
<html><head> <title>UCR Webmaster Support Group</title> <link rel="stylesheet" type="text/css" href=mycssfile.css"> </head> <body> <table width=80% height=30> <tr><td> <div align=center> Page Title </div> </td></tr></table>
Examples
<table width=80% height=30> <tr><td> <div align=center> UC Riverside Department<BR> <a href=mailto:[email protected]>[email protected]</a> </div> </td></tr></table> </body> </html>
Examples
<?php // header include(header.php); ?> Insert content here! <?php // footer include(footer.php); ?>
Examples
Benefits: - Any changes to header or footer only require editing of a single file. This reduces the amount of work necessary for site maintenance and redesign. - Helps separate the content and design for easier maintenance Header
Page 1 Content
Page 2 Content
Page 3 Content
Page 4 Content
Page 5 Content
Footer
Examples
Step 1: Simple Page Counter
Download the counter file webcounter.txt onto your machine Upload the webcounter.txt file onto your web server (via FTP, WinSCP, etc) Change the file permissions of the webcounter.txt file to 777 to allow the counter file to be updated.
Examples
Step 2: Simple Page Counter
Copy this code into a page where you want a counter.
<?php $COUNTER_FILE = webcounter.txt"; if (file_exists($COUNTER_FILE)) { $fp = fopen("$COUNTER_FILE", "r+"); flock($fp, 1); $hits = fgets($fp, 4096); $hits += 1; fseek($fp,0); fputs($fp, $hits); flock($fp, 3); fclose($fp); } ?>
Examples
Step 3: Simple Page Counter
Next, output the counter value using PHP. Copy this line after the main block of code.
This page has been viewed <?php echo$hits; ?> times. Thats it! The result should look something similar to:
Examples
Step 3: Simple Page Counter You can change the text around the <?php echo$hits; ?> tags to your liking.
This example shows 1. How to escape from HTML and enter PHP mode 2. How to output variables onto the screen using PHP
Examples
2. How to output variables using PHP
Echo is the common method in outputting data. Since it is a language construct, echo doesnt require parenthesis like print(). Output Text Usage: <?php echo Hello World; ?> // prints out Hello World Output the value of a PHP variable: <?php echo $hits; ?> // prints out the number of hits Echo has a shortcut syntax, but it only works with the short open tag configuration enabled on the server. <?= $hits ?>
Examples
3. Other uses with echo() Automatically generate the year on your pages. This will print out 2004 UC Riverside. <?php echo date(Y); ?> UC Riverside You will need to escape any quotation marks with a backslash. <?php echo I said \She sells sea shells\ ; ?>
Additional Resources
PHP Manual http://docs.php.net/ PHP Tutorial http://academ.hvcc.edu/~kantopet/php/index.php PHP Coder http://www.phpide.de/ JEdit http://www.jedit.org/ PHP's creator offers his thoughts on the PHP phenomenon, what has shaped and motivated the language, and where the PHP movement is heading http://www.oracle.com/technology/pub/articles/php_experts/rasmus_php.html Hotscripts A large number of PHP scripts can be found at: http://hotscripts.com/PHP/Scripts_and_Programs/index.html
Additional Information
Some of the new functions added in version 5:
Arrays: array_combine() - Creates an array by using one array for keys and another for its values array_walk_recursive() - Apply a user function recursively to every member of an array Date and Time Related: idate() - Format a local time/date as integer date_sunset() - Time of sunset for a given day and location date_sunrise() - Time of sunrise for a given day and location time_nanosleep() - Delay for a number of seconds and nano seconds Strings: str_split() - Convert a string to an array strpbrk() - Search a string for any of a set of characters substr_compare() - Binary safe optionally case insensitive comparison of two strings from an offset, up to length characters Other: php_check_syntax() - Check the syntax of the specified file php_strip_whitespace() - Return source with stripped comments and whitespace