SlideShare a Scribd company logo
PHP – An Introduction
Albert Morita – UCR Libraries Systems Dept.
December 9, 2004
Version 2.0
Agenda
1. Brief History of PHP1. Brief History of PHP
2. Getting started2. Getting started
3. Examples3. Examples
Brief History of PHP
PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was
initially developed for HTTP usage logging and server-side form generation in
Unix.
PHP 2 (1995) transformed the language into a Server-side embedded scripting
language. Added database support, file uploads, variables, arrays, recursive
functions, conditionals, iteration, regular expressions, etc.
PHP 3 (1998) added support for ODBC data sources, multiple platform support,
email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi
Gutmans .
PHP 4 (2000) became an independent component of the web server for added
efficiency. The parser was renamed the Zend Engine. Many security features
were added.
PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML
support using the libxml2 library, SOAP extension for interoperability with Web
Services, SQLite has been bundled with PHP
Brief History of PHP
As of August 2004, PHP is used on 16,946,328 Domains, 1,348,793 IP
Addresses http://www.php.net/usage.php This is roughly 32% of all
domains on the web.
Why is PHP used?
1. Easy to Use
Code is embedded into HTML. The PHP code is enclosed in special start and end tags
that allow you to jump into and out of "PHP mode".
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
Why is PHP used?
2. Cross Platform
Runs on almost any Web server on several operating systems.
One of the strongest features is the wide range of supported databases
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 (read-
only), 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
Why is PHP used?
3. Cost Benefits
PHP is free. Open source code means that the entire PHP community will contribute
towards bug fixes. There are several add-on technologies (libraries) for PHP that are
also free.
PHP
Software Free
Platform Free (Linux)
Development Tools 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.
Starting tag Ending tag Notes
<?php ?> 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
<script language="php"> ?> Always available, best if used when
FrontPage is the HTML editor
<% %> Not recommended. ASP tags support was
added in 3.0.4
<?php echo “Hello World”; ?>
PHP CODE HTMLHTML
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. Using conditional statements
• Conditional statements are very useful for displaying specific
content to the user. The following example shows how to display
content according to the day of the week.
<?php
$today_dayofweek = date(“w”);
if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}
?>
Getting Started
3. Using conditional statements
The if statement checks the value of $today_dayofweek
(which is the numerical day of the week, 0=Sunday… 6=Saturday)
• If it is equal to 4 (the numeric representation of Thurs.) it will display
everything within the first { } bracket after the “if()”.
• If it is not equal to 4, it will display everything in the second { } bracket
after the “else”.
<?php
$today_dayofweek = date(“w”);
if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}
?>
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
• Step 1: Universal header and footer in a single file
• Create a file called header.php. This file will have all of the
header HTML code. You can use FrontPage/Dreamweaver to create the
header, but remember to remove the closing </BODY> and </HTML> tags.
<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
• Step 2: Universal header and footer in a single file
• Next, create a file called footer.php. This file will have all of the footer HTML
code.
<table width=80% height=30>
<tr><td>
<div align=center> UC Riverside Department<BR>
<a href=mailto:someuser@ucr.edu>someuser@ucr.edu</a>
</div>
</td></tr></table>
</body>
</html>
Examples
• Step 3: Universal header and footer in a single file
• This is the basic template that you will use on all of the pages. Make sure you name the
files with a .php extension so that the server will process the PHP code. In this example,
we assume the header and footer files are located in the same directory.
<?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
Page 1
Content
Page 5
Content
Page 3
Content
Page 2
Content
Page 4
Content
Header
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.
• That’s 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.
<?php echo“$hits”; ?> visitors.
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 doesn’t 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
Ad

Recommended

PHP BASIC PRESENTATION
PHP BASIC PRESENTATION
krutitrivedi
 
PHP language presentation
PHP language presentation
Annujj Agrawaal
 
PHP Hypertext Preprocessor
PHP Hypertext Preprocessor
adeel990
 
PHP Function
PHP Function
Reber Novanta
 
A History of PHP
A History of PHP
Xinchen Hui
 
PHP presentation - Com 585
PHP presentation - Com 585
jstout007
 
Php Presentation
Php Presentation
Manish Bothra
 
Class 1 - World Wide Web Introduction
Class 1 - World Wide Web Introduction
Ahmed Swilam
 
Php basic for vit university
Php basic for vit university
Mandakini Kumari
 
Php technical presentation
Php technical presentation
dharmendra kumar dhakar
 
PHP in one presentation
PHP in one presentation
Milad Rahimi
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
Introduction to php
Introduction to php
Taha Malampatti
 
PHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP Server
Rajiv Bhatia
 
Introduction to php web programming - get and post
Introduction to php web programming - get and post
baabtra.com - No. 1 supplier of quality freshers
 
Lean Php Presentation
Lean Php Presentation
Alan Pinstein
 
Php
Php
anandha ganesh
 
PHP - History, Introduction, Summary, Extensions and Frameworks
PHP - History, Introduction, Summary, Extensions and Frameworks
Royston Olivera
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
iimjobs and hirist
 
Php interview-questions and answers
Php interview-questions and answers
sheibansari
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql
Geshan Manandhar
 
Introduction To PHP
Introduction To PHP
Shweta A
 
File Upload
File Upload
webhostingguy
 
Overview of PHP and MYSQL
Overview of PHP and MYSQL
Deblina Chowdhury
 
Php file upload, cookies & session
Php file upload, cookies & session
Jamshid Hashimi
 
Ajax
Ajax
NIRMAL FELIX
 
Introduction to PHP
Introduction to PHP
Collaboration Technologies
 
Automatic P2R Published Paper P1277-1283
Automatic P2R Published Paper P1277-1283
TechnoKraft Training & Solution PVT. LTD.
 
MANGAUNG 2014 ANNUAL DISABILITY TRANSPORT SUMMIT REPORT 3
MANGAUNG 2014 ANNUAL DISABILITY TRANSPORT SUMMIT REPORT 3
Abednego Lehloka
 
Report IYSP 2005
Report IYSP 2005
Pierre Lienhard
 

More Related Content

What's hot (19)

Php basic for vit university
Php basic for vit university
Mandakini Kumari
 
Php technical presentation
Php technical presentation
dharmendra kumar dhakar
 
PHP in one presentation
PHP in one presentation
Milad Rahimi
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
Introduction to php
Introduction to php
Taha Malampatti
 
PHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP Server
Rajiv Bhatia
 
Introduction to php web programming - get and post
Introduction to php web programming - get and post
baabtra.com - No. 1 supplier of quality freshers
 
Lean Php Presentation
Lean Php Presentation
Alan Pinstein
 
Php
Php
anandha ganesh
 
PHP - History, Introduction, Summary, Extensions and Frameworks
PHP - History, Introduction, Summary, Extensions and Frameworks
Royston Olivera
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
iimjobs and hirist
 
Php interview-questions and answers
Php interview-questions and answers
sheibansari
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql
Geshan Manandhar
 
Introduction To PHP
Introduction To PHP
Shweta A
 
File Upload
File Upload
webhostingguy
 
Overview of PHP and MYSQL
Overview of PHP and MYSQL
Deblina Chowdhury
 
Php file upload, cookies & session
Php file upload, cookies & session
Jamshid Hashimi
 
Ajax
Ajax
NIRMAL FELIX
 
Introduction to PHP
Introduction to PHP
Collaboration Technologies
 
Php basic for vit university
Php basic for vit university
Mandakini Kumari
 
PHP in one presentation
PHP in one presentation
Milad Rahimi
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
 
PHP-MySQL Database Connectivity Using XAMPP Server
PHP-MySQL Database Connectivity Using XAMPP Server
Rajiv Bhatia
 
Lean Php Presentation
Lean Php Presentation
Alan Pinstein
 
PHP - History, Introduction, Summary, Extensions and Frameworks
PHP - History, Introduction, Summary, Extensions and Frameworks
Royston Olivera
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
iimjobs and hirist
 
Php interview-questions and answers
Php interview-questions and answers
sheibansari
 
05 File Handling Upload Mysql
05 File Handling Upload Mysql
Geshan Manandhar
 
Introduction To PHP
Introduction To PHP
Shweta A
 
Php file upload, cookies & session
Php file upload, cookies & session
Jamshid Hashimi
 

Viewers also liked (12)

Automatic P2R Published Paper P1277-1283
Automatic P2R Published Paper P1277-1283
TechnoKraft Training & Solution PVT. LTD.
 
MANGAUNG 2014 ANNUAL DISABILITY TRANSPORT SUMMIT REPORT 3
MANGAUNG 2014 ANNUAL DISABILITY TRANSPORT SUMMIT REPORT 3
Abednego Lehloka
 
Report IYSP 2005
Report IYSP 2005
Pierre Lienhard
 
Carlos delgado
Carlos delgado
clsnake
 
Presentation_Women_2014_JMD Garden
Presentation_Women_2014_JMD Garden
Shilpi Johri, CFP (India)
 
Workshop_Financial Planning
Workshop_Financial Planning
Shilpi Johri, CFP (India)
 
Perilakuterpujisemester2
Perilakuterpujisemester2
Dani Saputra
 
Yosiry Suriel Resume
Yosiry Suriel Resume
Yosiry Suriel
 
kintone Café 札幌 Vol.7 「kintoneエコシステム真時代の幕開け! -kintoneエコシステムとAWSサーバレスアーキテクチャ-」
kintone Café 札幌 Vol.7 「kintoneエコシステム真時代の幕開け! -kintoneエコシステムとAWSサーバレスアーキテクチャ-」
JOYZO
 
Northroad cane corso juno
Northroad cane corso juno
Sarah O'Brien
 
West Suffolk Trust Recycling Presentation-26112014
West Suffolk Trust Recycling Presentation-26112014
Mike Thompson
 
Truffly Made Catalog-Recipes 2014 (2)
Truffly Made Catalog-Recipes 2014 (2)
Truffly Made
 
MANGAUNG 2014 ANNUAL DISABILITY TRANSPORT SUMMIT REPORT 3
MANGAUNG 2014 ANNUAL DISABILITY TRANSPORT SUMMIT REPORT 3
Abednego Lehloka
 
Carlos delgado
Carlos delgado
clsnake
 
Perilakuterpujisemester2
Perilakuterpujisemester2
Dani Saputra
 
Yosiry Suriel Resume
Yosiry Suriel Resume
Yosiry Suriel
 
kintone Café 札幌 Vol.7 「kintoneエコシステム真時代の幕開け! -kintoneエコシステムとAWSサーバレスアーキテクチャ-」
kintone Café 札幌 Vol.7 「kintoneエコシステム真時代の幕開け! -kintoneエコシステムとAWSサーバレスアーキテクチャ-」
JOYZO
 
Northroad cane corso juno
Northroad cane corso juno
Sarah O'Brien
 
West Suffolk Trust Recycling Presentation-26112014
West Suffolk Trust Recycling Presentation-26112014
Mike Thompson
 
Truffly Made Catalog-Recipes 2014 (2)
Truffly Made Catalog-Recipes 2014 (2)
Truffly Made
 
Ad

Similar to Php intro (20)

PHP ITCS 323
PHP ITCS 323
Sleepy Head
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
Shashank Skills Academy
 
Php intro
Php intro
Rajesh Jha
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Basics PHP
Basics PHP
Alokin Software Pvt Ltd
 
Intro to PHP for Students and professionals
Intro to PHP for Students and professionals
cuyak
 
Php reports sumit
Php reports sumit
Sumit Biswas
 
Introduction to php
Introduction to php
shanmukhareddy dasi
 
Php Tutorial
Php Tutorial
SHARANBAJWA
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
Introduction to web and php mysql
Introduction to web and php mysql
Programmer Blog
 
Chowdhury-webtech.ppt
Chowdhury-webtech.ppt
RonakBothra8
 
Chowdhury-webtech.ppt
Chowdhury-webtech.ppt
ProjectCexsys
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Justmeans power point
Justmeans power point
justmeanscsr
 
Intro to PHP for Students and professionals
Intro to PHP for Students and professionals
cuyak
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
Introduction to web and php mysql
Introduction to web and php mysql
Programmer Blog
 
Chowdhury-webtech.ppt
Chowdhury-webtech.ppt
RonakBothra8
 
Chowdhury-webtech.ppt
Chowdhury-webtech.ppt
ProjectCexsys
 
Ad

Php intro

  • 1. PHP – An Introduction Albert Morita – UCR Libraries Systems Dept. December 9, 2004 Version 2.0
  • 2. Agenda 1. Brief History of PHP1. Brief History of PHP 2. Getting started2. Getting started 3. Examples3. Examples
  • 3. Brief History of PHP PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was initially developed for HTTP usage logging and server-side form generation in Unix. PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc. PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans . PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP
  • 4. Brief History of PHP As of August 2004, PHP is used on 16,946,328 Domains, 1,348,793 IP Addresses http://www.php.net/usage.php This is roughly 32% of all domains on the web.
  • 5. Why is PHP used? 1. Easy to Use Code is embedded into HTML. The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode". <html> <head> <title>Example</title> </head> <body> <?php echo "Hi, I'm a PHP script!"; ?> </body> </html>
  • 6. Why is PHP used? 2. Cross Platform Runs on almost any Web server on several operating systems. One of the strongest features is the wide range of supported databases 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 (read- only), 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
  • 7. Why is PHP used? 3. Cost Benefits PHP is free. Open source code means that the entire PHP community will contribute towards bug fixes. There are several add-on technologies (libraries) for PHP that are also free. PHP Software Free Platform Free (Linux) Development Tools Free PHP Coder, jEdit
  • 8. 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. Starting tag Ending tag Notes <?php ?> 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 <script language="php"> ?> Always available, best if used when FrontPage is the HTML editor <% %> Not recommended. ASP tags support was added in 3.0.4 <?php echo “Hello World”; ?> PHP CODE HTMLHTML
  • 9. 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>)
  • 10. Getting Started 3. Using conditional statements • Conditional statements are very useful for displaying specific content to the user. The following example shows how to display content according to the day of the week. <?php $today_dayofweek = date(“w”); if ($today_dayofweek == 4){ echo “Today is Thursday!”; } else{ echo “Today is not Thursday.”; } ?>
  • 11. Getting Started 3. Using conditional statements The if statement checks the value of $today_dayofweek (which is the numerical day of the week, 0=Sunday… 6=Saturday) • If it is equal to 4 (the numeric representation of Thurs.) it will display everything within the first { } bracket after the “if()”. • If it is not equal to 4, it will display everything in the second { } bracket after the “else”. <?php $today_dayofweek = date(“w”); if ($today_dayofweek == 4){ echo “Today is Thursday!”; } else{ echo “Today is not Thursday.”; } ?>
  • 12. 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.”; } ?>
  • 13. Examples • PHP is a great way to implement templates on your website. • How to implement a simple page counter
  • 14. Examples • Step 1: Universal header and footer in a single file • Create a file called header.php. This file will have all of the header HTML code. You can use FrontPage/Dreamweaver to create the header, but remember to remove the closing </BODY> and </HTML> tags. <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>
  • 15. Examples • Step 2: Universal header and footer in a single file • Next, create a file called footer.php. This file will have all of the footer HTML code. <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>
  • 16. Examples • Step 3: Universal header and footer in a single file • This is the basic template that you will use on all of the pages. Make sure you name the files with a .php extension so that the server will process the PHP code. In this example, we assume the header and footer files are located in the same directory. <?php // header include(“header.php”); ?> Insert content here! <?php // footer include(“footer.php”); ?>
  • 17. 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 Page 1 Content Page 5 Content Page 3 Content Page 2 Content Page 4 Content Header Footer
  • 18. 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.
  • 19. 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); } ?>
  • 20. 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. • That’s it! The result should look something similar to:
  • 21. Examples • Step 3: Simple Page Counter • You can change the text around the <?php echo“$hits”; ?> tags to your liking. <?php echo“$hits”; ?> visitors. This example shows 1. How to escape from HTML and enter PHP mode 2. How to output variables onto the screen using PHP
  • 22. Examples 2. How to output variables using PHP • Echo is the common method in outputting data. Since it is a language construct, echo doesn’t 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 ?>
  • 23. 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” ”; ?>
  • 24. 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
  • 25. 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