Sie sind auf Seite 1von 3

CPT 301 Internet Technology

Tutorial 8 Introduction to PHP


March 27, 2013

Introduction

< / html > 2. save the le in c:\wamp\www\php1.php 3. To run the le in a browser open a browser and type //localhost/ php1.php 4. Now you should be able to run your script. 5. Now modify the code as follows < html > <head > < t i t l e >My F i r s t PHP Page < / t i t l e > < / head > <body > <?php echo H e l l o World ! ; echo H e l l o World ! ; ?> < / body > < / html > 6. Now modify the code so that it will be as follows Listing 2: code listing new line < html > <head > < t i t l e >My F i r s t PHP Page < / t i t l e > < / head > <body > <?php echo H e l l o World ! < br > ; echo T h i s i s t h e s e c o n d l i n e < br > ;

PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specic features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly. This is generally a good denition of PHP. However, it does contain a lot of terms you may not be used to. Another way to think of PHP is a powerful, behind the scenes scripting language that your visitors wont see! When someone visits your PHP webpage, your web server processes the PHP code. It then sees which parts it needs to show to visitors(content and pictures) and hides the other stuff(le operations, math calculations, etc.) then translates your PHP into HTML. After the translation into HTML, it sends the webpage to your visitors web browser.

PHP create a simple Hello World


1. Open Notepad++ and write the following code Listing 1: php code listing Hello World < html > <head > < t i t l e >My F i r s t PHP Page < / t i t l e > < / head > <body > <?php echo H e l l o World ! ; ?> < / body >

echo H e l l o World ; ?> < / body > < / html > 7. Now create the following. < html > <head > < t i t l e >My F i r s t PHP Page < / t i t l e > < / head > <body > <?php echo <h1> < c e n t e r > T h i s i s my P e r s o n a l Page < / c e n t e r ></h1 > ; echo < hr > ; echo < br > ; echo <H2> E d u c a t i o n < br ></H2> ; echo <H2> H o b b i e s < br ></H2> ; echo <H2> S p o r t s < br ></H2> ; ?> < / body > < / html > 8. When using quotes observe the following Dont use quotes inside your string Escape your quotes that are within the string with a slash. To escape a quote just place a slash directly before the quotation mark, i.e. bs Use single quotes (apostrophes) for quotes inside your string. 9. variables Write the following code and observe the output Listing 3: echoing variables < html > <head > < t i t l e >My F i r s t PHP Page < / t i t l e > < / head > <body > <?php 2

$ m y s t r i n g = h e l l o t h i s i s my f i r s t p r o g r a m i n php <br > ; $my number = 4 ; $my nextNumber = 6 ; $sum numbers = $my number + $my nextNumber ; echo $ m y s t r i n g ; echo t h e sum o f t h e numbers . $my number . + . $my nextNumber . = . $sum numbers ; ?> < / body > < / html > 10. Including les 11. Create the following les. save the le menu.php Listing 4: menu le < html > <body > <a h r e f = h t t p : / / www. e x a m p l e . com / i n d e x . php >Home < / a> <a h r e f = h t t p : / / www. e x a m p l e . com / a b o u t . php >About Us < / a> <a h r e f = h t t p : / / www. e x a m p l e . com / l i n k s . php > L i n k s < / a> <a h r e f = h t t p : / / www. e x a m p l e . com / c o n t a c t . php > C o n t a c t Us < / a> < b r /> < / body > < / html > 12. Now create the following index les and name it index.php. Run the menu le Listing 5: menu le <?php i n c l u d e ( menu . php ) ; ?> <p> T h i s i s my home p a g e t h a t u s e s a common menu t o s a v e me t i m e when I add new p a g e s t o my w e b s i t e ! < / p> < / body > < / html > 13. Save the le and run the le

14. Types can be changed using the settype keyword. The following is an example. Run the following code. < html > <body > <?php $ t e s t s t r i n g = 3 ,5 seconds ; $testdouble = 79.2; $ t e s t i n t = 12; ?> <?php p r i n t ( $ t e s t s t r i n g ) ; ?> i s a s t r i n g <b r /> <?php p r i n t ( $ t e s t d o u b l e ) ; ?> i s a d o u b l e <b r /> <?php p r i n t ( $ t e s t i n t ) ; ?> i s an i n t e g e r <b r /> <h1> C h a n g i n g t y p e s < /h1> <?php print ( $ t e s t s t r i n g ) ; settype ( $ t e s t s t r i n g , double ) ; print ( as a double i s ) ; print ( $ t e s t s t r i n g ) ; p r i n t ( < b r / > ) ; print ( $ t e s t i n t ) ; settype ( $ t e s t i n t , double ) ; print ( as a double i s ) ; print ( $ t e s t i n t ) ; ?> < / body > < / html >

< form a c t i o n = p r o c e s s . php method = p o s t > < s e l e c t name= i t e m > < o p t i o n >P a i n t </ o p t i o n > < o p t i o n >B r u s h e s < / o p t i o n > < o p t i o n >E r a s e r s </ o p t i o n > </ s e l e c t > < br > < br > Q u a n t i t y : < i n p u t name= q u a n t i t y t y p e = t e x t / > < br > < br > < i n p u t t y p e = s u b m i t / > < / form > < / body ></html > 2. Now create the corresponding post php le < html ><body > <?php $ q u a n t i t y = $ POST [ q u a n t i t y ] ; $ i t e m = $ POST [ i t e m ] ; echo You o r d e r e d . $ q u a n t i t y . . $ i t e m . .< b r /> ; echo Thank you f o r o r d e r i n g from Tizag Art Supplies ! ; ?> < / body ></html > 3. Now run the programs

Form handling
1. Create the following form < html > <body > <h4> T i z a g A r t S u p p l y O r d e r Form < / h4> 3

Das könnte Ihnen auch gefallen