©john samuel 2008 introduction to php. ©john samuel 2008 objectives at the end of this class the...
TRANSCRIPT
©John Samuel 2008
Introduction to PHP
©John Samuel 2008
Objectives
At the end of this class the student will be able to;
Create and run a simple php program using their Zenit account.
Explain the basic way in with PHP works.
Explain how to add PHP code to an XHTML document.
©John Samuel 2008
Objectives
At the end of this class the student will be able to;
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.
©John Samuel 2008
Php installation
You can run php programs on your Zenit 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.
Follow the install instructions included with each download carefully.
©John Samuel 2008
Apache installation
If 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.
©John Samuel 2008
Configuration
Configuration 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.
http://www.php.net/manual/en/configuration.directives.php
©John Samuel 2008
Configuration
asp-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 = Off
To; Allow ASP-style <% %> tags.asp_tags = On
©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
©John Samuel 2008
Configuration
You can test PHP and learn about its settings by using the function phpinfo().
©John Samuel 2008
PHP
Stands for Personal HomePage Created by Rasmus Lerdorf for his
own use. Latest version 5.x Part of “LAMP” technologies
(www.onlamp.com)
©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.
©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’.
©John Samuel 2008
PHP
Any XHTML page can be turned into a PHP page simply by changing the extension to .php
©John Samuel 2008
PHP tags
There 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.
©John Samuel 2008
PHP tags
There 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
©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.
©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:/*
*/
©John Samuel 2008
Comments
Note that PHP comment markers only apply inside PHP tags, e.g.<? //comment ?> this part gets printed
©John Samuel 2008
Variable types
There are six types: boolean integer string float objects (beyond scope of course) array
Variable capacity is system dependent.
©John Samuel 2008
Casting
You can cast in the C style, or use the function settype().
See variables.php
©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 variables.phpSee http://ca3.php.net/variables for a list of
variable functions.
©John Samuel 2008
Arrays
Can be multidimensional. Can be indexed or associative. Three important keywords are:
array list range
See arrays.php
©John Samuel 2008
Array functions
There are many useful array functions.
See arrayfunctions.php See http://www.php.net/manual/en/ref.array.php for
a complete list.
©John Samuel 2008
“” vs. ‘’
Variables and special characters inside “” are interpolated, but ‘’ are not.
Same as in Perl.
©John Samuel 2008
Review
What are the PHP comment styles? How does PHP treat variables? What are PHP’s variable types?