john samuel 2008 introduction to php note: these slides are not included in coverage for the bif703...

25
©John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam...

Upload: olivia-collins

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

©John Samuel 2008 Objectives Write and execute simple PHP programs using the following language features: Comments Variables Arrays and array functions Use php to create simple dynamic XHTML programs.

TRANSCRIPT

Page 1: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Introduction to PHP

Note: These slides are not included in coverage for the BIF703 final exam...

Page 2: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Objectives Create and run a simple php program using their web authoring accounts such as Zenit and Matrix.Explain the basic way in with PHP works.Explain how to add PHP code to an XHTML document.

Page 3: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

ObjectivesWrite and execute simple PHP programs using the following language features:

Comments Variables Arrays and array functions Use php to create simple dynamic XHTML

programs.

Page 4: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

PHP Stands for Personal HomePage toolsCreated by Rasmus Lerdorf for his own use.Latest version 5.x Part of “LAMP” technologies (www.onlamp.com)

Page 5: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

PHP It is extremely easy to learn.Syntax is a mix of C and Perl.PHP is a function-based language.There are thousands of PHP functions for just about any imaginable purpose.

Page 6: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

PHP Start with HTML and add specially marked sections of code.Code is executed, any changes or additions are made to the HTML code, and it is returned to the browser/user agent for ‘display’.

Page 7: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

PHP Any XHTML page can be turned into a PHP page simply by changing the extension to .php

Page 8: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

PHP tagsThere are 3 (4) ways to mark PHP code inside a document.Note ; at end of each line.Multiple lines allowed between start and end tags.__ML can be included in PHP print statements.

Page 9: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

PHP tagsThere are 2 shortcuts for including literal or variable data without a print statement,<?= ?> (if short tags enabled) <%= %> (if asp tags enabled)E.g.<?= “hello” ?><?= $name ?>Note, no ; after data

Page 10: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

PHP tags When developing PHP code that may be used by others (modules), it is recommended that you use the formal tags <?php ?> since the other forms are configurable and may not be supported on the user’s system.

Page 11: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Comments Inside the PHP tags, the following comment styles are allowed. Outside the PHP tags, comments must follow the style of the ML (usually <!-- -->).

Single line PHP comments: // # Multiple line PHP comments:

/*

*/

Page 12: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

CommentsNote that PHP comment markers only apply inside PHP tags

For example:

<? //comment ?>

Page 13: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Variable typesThere are six types: boolean integer string float objects (beyond scope of course) Array

Variable capacity is system dependent.

Page 14: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

CastingYou can cast in the C style, or use the function settype().

Page 15: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Variable functions There are many useful functions for working with variables

gettype() returns the type of the variable.There is a boolean function for each type, e.g. is_int($x) returns true if $x is an integer, etc.Can use var_dump() to print details about a value.See http://ca3.php.net/variables for a list of variable functions.

Page 16: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

ArraysCan be multidimensional.Can be indexed or associative.Three important keywords are: array list range

See arrays.php

Page 17: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Array functionsThere are many useful array functions.See arrayfunctions.phpSee http://www.php.net/manual/en/ref.array.php for a complete list.

Page 18: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Quotation - “” vs. ‘’ Variables and special characters inside “” are interpolated, but ‘’ are not.

Same as in Perl.

Page 19: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Examples Refer to the following link for examples of code (.txt) and viewing the results (.php):

http:cs.senecac.on.ca/~murray.saul/php

The example basicformwithfunctions.php provides the best example with many of the functions that BIF students are familiar...

Page 20: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Php installationYou can run php programs on your Zenit and Matrix accounts.

If you want your own development environment for php, you will need to install a web server first (Apache recommended) and then php. To duplicate the environment on Zenit you will also need to install MySql.

Page 21: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Apache installationIf you already have another web server installed, (e.g. IIS or PWS) you will need to assign a different port to Apache, by editing httpd.conf.

Page 22: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

ConfigurationConfiguration of php can be done by editing

the php.ini file.Read the php documentation and the internal

comment in the ini file to make sure that you know what you are changing.Refer to link:

http://www.php.net/manual/en/configuration.directives.php

Page 23: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Configurationasp-style tags are not enabled on zenit. Make sure that any assignment code you write at home will run on zenit.If you wish, you can change your php.ini file so that asp/jsp stype tags are also accepted, i.e. change the following line:; Allow ASP-style <% %> tags.asp_tags = Offto read:; Allow ASP-style <% %> tags.asp_tags = On

Page 24: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

Configuration

Allowing asp-style and short open tags on Zenit:

Create a file named .htaccess in the directory that has your php code.

Add the following content:php_flag asp_tags onphp_flag short_open_tag on

Page 25: John Samuel 2008 Introduction to PHP Note: These slides are not included in coverage for the BIF703 final exam

©John Samuel 2008

ConfigurationYou can test PHP and learn about its settings by using the function phpinfo().