PHP Introduction

Bill Jerome

PHP Hypertext Preprocessor      No joke. that¶s what it stands for Now very widely used for websites Only three years ago it was considered a risky alternative for development Has a lot of community support Is developed by hackers (see name) Is hacky .

work very much like c/c++ Designed to work in the world of HTML Is run-time interpreted by the web server Exception: those running Zend Cache  We sure won¶t be  Reminder: it¶s hacky . but (also?) very c-like Classes.PHP     Hacky. etc..

php3 for version 3) Initial use was control flow and simple scripting .Simple PHP     PHP is meant to be invoked inline with content Page ³escapes´ into and out of a regular html document File extension is .php (was .

A quick example  <html> <head>Test page</head> <body> The time is now <?php echo date(). ?> <hr> </body> </html> .

A quick example  <html> <head>Test page</head> <body> The time is now <?php here we ³jump into´ php echo date(). ?> here we ³jump´ back out <hr> </body> </html> .

date(). } ?> <hr> </body> </html> . } else { echo ³I will not give you the time of day´. ?> <html> <head>Test page</head> <body> <?php if ($utils->isFriendly()) { echo ³The time is now ³ .php´.Another example <?php include ³utilities.

} else { ?> <i>I will not give you the time of day!</i> <?php } ?> <hr> </body> </html> .Another example ² harder to read <?php include ³utilities.php´. date(). ?> <html> <head>Test page</head> <body> <?php if ($utils->isFriendly()) { echo ³The time is now ³ .

More PHP language details  Variables are implicitly typed This is good This is bad   Variables start with $ All get/post variables automatically defined With most default server settings With an inline directive if need be .

Defined variable example   foo.php: <html><head></head></body> Your name is <?php if (!strcmp($username)) { echo $username. } ?> !<br> </body></html> .html: <html><head></head><body> <form submit=³getFoo.php´> Enter your name: <input type=³text´ name=³username´> <input type=³submit´> </body></html> getFoo. } else { echo ³not given´.

« Some are web tailored  Others are new (hacky)  . htmlspecialchars addcslashes. soundex.php. php All string functions Some are ³obvious´ to c programmers printf. fprintf.strlen. strpos htmlentities.Function list examples  http://www. quotemeta. explode.

Classes  OOP Class structures will be defined. helping integration with other apps and work together APIs followed by implementation   Inheritance Object serialization ³Magic functions´ .

} // Take $num articles of $artnr out of the cart function remove_item ($artnr. } } } . // Items in our shopping cart // Add $num articles of $artnr to the cart function add_item ($artnr. return true. $num) { $this->items[$artnr] += $num. } else { return false. $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num.Class example class Cart { var $items.

echo "String Contents: " . "\n". echo "Value Contents: " . $object->ObjPointer->string . function ObjTest() { $tmp = new MemberObject. } } class MemberObject extends ParentObject { var $string. function MemberObject() { $this->string = "This is a test string. } } $object = new ObjTest. $this->ParentObject(). "\n". .". $this->ObjPointer = $tmp.Inheritance example Class ParentObject { var $value. function ParentObject() { $this->value = 42. } } class ObjTest { var $ObjPointer. $object->ObjPointer->value .

you wanted pointers? Variable variables  Yeah. you heard right Don¶t get me started« .Back to being hacky«  ³->´ is NOT the same thing as it is in c++ No pointers in PHP ONLY a member operator  Oh.

echo $object->$member_name. $object->b = "hello".Variable variables and classes example class a { var $b. $member_name = 'b'. } $object = new a. echo $object->$member_name. . $object->$member_name = " world".

evilwalrus.Resources .net http://www.

Sign up to vote on this title
UsefulNot useful