Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 Radford University

2 August 20062 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)

3 August 20063 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

4 August 20064 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 https://php.radford.edu/~jcdavis/p1.php

5 August 20065 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 First PHP The above is a valid PHP program.

6 August 20066 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 PHP + CODE PHP with Code What’s wrong with the above????

7 August 20067 Printing Text echo The echo statement inserts text into the output file. ECHO Echo text. "; echo "Hello from PHP."; echo " "; ?> What’s wrong with the above? https://php.radford.edu/~jcdavis/ it325examples/phpecho.phphttps://php.radford.edu/~jcdavis/ it325examples/phpecho.php

8 August 20068 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. Carriage Controls Here’s a paragraph output via echo note we can have text on multiple lines without ending the quote. "; echo " A secondparagraph. "; echo "\nTo structure the html include new- linecharacters.\n" ?>

9 August 20069 Formatting Characters \nnewline \rcarriage return \ttab \\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\".");

10 August 200610 PHP Comments Three ways to indicate a comment in php. /* Multiline comments use asterisk and slash */ // Single line comment # Single line comment Remember use html comments in html sections, and php comments inside php code

11 August 200611 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;

12 August 200612 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?";

13 August 200613 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.

14 August 200614 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 $. "; ?> Don’t confuse php by defining constants that are already php key words, like anddefault die foreach function see page 29 in your text.

15 August 200615 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;

16 August 200616 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. FALSE 0 0.0 "" "0" array with 0 elements NULL


Download ppt "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."

Similar presentations


Ads by Google