August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by...

16
August 2006 1 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology Radford University

Transcript of August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by...

Page 1: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 1

Chapter 1 - Essential PHPspring into PHP 5by Steven Holzner

Slides were developed by Jack Davis

College of Information Scienceand Technology

Radford University

Page 2: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 2

Origins and Uses of PHP

- Origins - Rasmus Lerdorf - 1994 - Developed to allow him to track visitors to his Web site

- PHP is an open-source product

- PHP is an acronym for Personal Home Page, or PHP: Hypertext Preprocessor

- PHP is used for form handling, file processing, and database access

Overview of PHP

- PHP is a server-side scripting language whose scripts are embedded in HTML documents - Similar to JavaScript, but on the server side

- PHP is an alternative to CGI, ASP.NET, JSP, and Allaire’s ColdFusion

- The PHP processor has two modes: copy (XHTML) and interpret (PHP)

Page 3: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 3

Review of the HTTP

request response process

- Client issues the request for a given URL (the browser)

- Server receives the request and returns the document (html) to the client

- Browser receives the document from the server and renders the document

- Request could be for an html document or could be for some other file

- For server side programs the requests are issued for other file types, like PHP

Page 4: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 4

Radford University

PHP file processing

- Check to see if there is a dynamic_php directory on your RU virtual drive.It will be under your home directory.So, for me – the path is//homedir/users/jcdavis/dynamic_php

- If the directory (folder) isn’t there you need to create it.

- If permissions are appropriately set, then you can put php files in this directory and they will be served, by the campus web server.

- The URL will be:- https://php.radford.edu/~user/file.php

- Example:https://php.radford.edu/~jcdavis/p1.php

Page 5: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 5

PHP Processing

• You can mix PHP and xhtml in a single file• When the client sends a request for a PHP

file, the web serving software interprets the file

It executes the PHP and copies the xhtml into an output file to be returned to the client

• So, a PHP file could just contain valid xhtml

file ---- firstphp.php

<html><head><title>First PHP </title></head><body> <h1> <First PHP> </h1></body></html>

• The above is a valid PHP program.

Page 6: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 6

Adding Code

• When the previous file was requested. The web serving program will get the request, see that it is a php file. It will examine the file, one line at a time. It copies html statements into the output file. When it encounters php code, it translates them and puts the output (if any)into the output file. The created output file is returned to the client. It must be a valid html file.

• PHP file with code

<html><head><title>PHP + CODE</title></head><body> <h2>PHP with Code</h2> <?php phpinfo(); # valid php function ?></body></html>

• What’s wrong with the above????

Page 7: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 7

Printing Text

• echoThe echo statement inserts text into the output file.

• <html><head><title>ECHO</title></head><body> <h1>Echo text.</h1> <?php echo "<p>"; echo "Hello from PHP."; echo "</p>"; ?> </p></body></html>

• What’s wrong with the above?• https://php.radford.edu/~jcdavis

/it325examples/phpecho.php

Page 8: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 8

PHP Outputs HTML

• The output of a php program that is intended for the use of a browser, should contain all the appropriate html tags. In addition, usually we’ll want the source code to be structured well, so you’ll need to include the appropriate carriage controls.

<html><head><title>Carriage Controls</title></head><body><?php echo "<p>Here’s a paragraph output via echo note we can have text on multiple lines without ending the quote.</p>"; echo "<p>A secondparagraph. </p>"; echo "\nTo structure the html include new- linecharacters.\n" ?></body></html>

Page 9: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 9

Formatting Characters

• \n newline• \r carriage return• \t tab• \\ displays a \• \$ displays a $ (\ is escape char.)• \” displays “

• echo "he said, \"I like ice cream.\"";

• You can also use the print statement, which is a php function, so it returns a value. Normally, this is of little use, so it’s use is almost identical to echo.

• print ("he said, \"I like ITEC 325\".");

Page 10: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 10

PHP Comments

• Three ways to indicate a comment in php.

/* Multiline comments use asterisk and slash */

// Single line comment

# Single line comment

• <?php/* here’s a comment,with more than one line */

print ("PHP is fun!"); # print statement?>

• Remember use html comments in html sections, and php comments inside php code

• <html><!-- echo.php -->

Page 11: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 11

PHP Variables

• We prefix all variable names with a $ in PHP.

Valid variable names start with a letter or underscore, followed by any number of letters, numbers, or underscores, and the name can be any length. Normally, PHP programmers use lower case letters for variable names.

$pizza $counter $_number

$planet_number_9

• Assignment statements are typical.

$temp = 90;$num_of_students = 23;$pi = 3.1415;$class_name = "ITEC 325";

echo "The class name is ", $class_name;

Page 12: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 12

Interpolating Variables

• Inside double quotes, variables will be replaced by their values.

<?php$dept = "ITEC";echo "The department is $dept.";

• Sometimes you need to separate the variable name from a literal string that you want ouput next to it.

$text = "news";echo "Where’s the {$text}paper?";

Page 13: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 13

Reference Variables

• You can create reference variables in PHP.

$apples = 4;

$fruitname = "apples";

Now you can refer to the value in $apples as $$fruitname.

echo "number of apples: ${$fruitname}";

$apples = 4; $oranges = 3;$fruitname = "oranges";echo "number of {$fruitname}: ${$fruitname}";$fruitname = “apples”;echo "number of {$fruitname}: ${$fruitname}";

• These are very useful when dealing with loops and arrays.

Page 14: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 14

PHP Constants

• PHP Constants can’t have their values altered, like most programming languages. You create constants with the define function, giving it the name of the constant and the value you want to assign.

define ("pi", 3.1415926535);

The name of the constant is always quoted, but the value you assign to the constant is only quoted if it is a string literal. When you use a constant name in code you don’t precede it with a $.

• <?php define ("pi",3.14152926535); echo "The constant pi holds ", pi, "<br />";?>

• Don’t confuse php by defining constants that are already php key words, like

and default die foreach function

see page 29 in your text.

Page 15: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 15

PHP Data Types

• boolean• integer• float• string• array• object• resource• NULL

• PHP is dynamically typed, the interpreter determines the type of a variable and will change the type as needed.

$variable = "a string";$variable = 1.2345;$variable = TRUE;

• When you mix types in an assignment statement PHP will attempt to coerce and perform the operation.$var = 2 + "8 apples"; $var is now 10;

Page 16: August 20061 Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.

August 2006 16

TYPE CASTS

• PHP provides built-in function that cast variables to a particular type.

$variable = "22";

$intvar = (integer) $variable;

$floatvar = (float) $variable;

$stringvar = (string) $variable;

• Values that will be converted to the boolean false value.

FALSE00.0"" "0"array with 0 elementsNULL