About The Tutorial - IT Present

Transcription

PHPAbout the TutorialThe PHP Hypertext Preprocessor (PHP) is a programming language that allows webdevelopers to create dynamic content that interacts with databases. PHP is basically usedfor developing web-based software applications. This tutorial will help you understand thebasics of PHP and how to put it in practice.AudienceThis tutorial is designed for beginners who are completely unaware of the concepts of PHPbut they have a basic understanding of computer programming.PrerequisitesBefore proceeding with this tutorial, you should have a basic understanding of computerprogramming, Internet, Database, and MySQL etc is very helpful.Copyright & Disclaimer Copyright 2015 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.comi

PHPTable of ContentsAbout the Tutorial .Audience . iPrerequisites . iCopyright & Disclaimer . iTable of Contents . iiPART 1: LEARNING PHP . 11.Introduction . 2Common uses of PHP: . 2Characteristics of PHP . 3"Hello World" Script in PHP . 3Environment Setup . 4PHP Parser Installation . 4PHP Installation on Linux or Unix with Apache . 4PHP Installation on Mac OS X with Apache . 6PHP Installation on Windows NT/2000/XP with IIS . 7PHP Installation on Windows NT/2000/XP with Apache . 82.Syntax Overview . 15Escaping to PHP . 15Commenting PHP Code . 16PHP is whitespace insensitive . 16PHP is case sensitive . 17Statements are expressions terminated by semicolons . 17Expressions are combinations of tokens . 17Braces make blocks . 18Running PHP Script from Command Prompt . 183.Variable Types . 19Integers . 20Doubles . 20Boolean . 20NULL . 21Strings . 21Variable Naming . 23PHP – Variables. 24PHP Local Variables . 24PHP Function Parameters . 24PHP Global Variables . 25PHP Static Variables . 264. Constants . 275.Operator Types . 29Arithmetic Operators. 29Comparison Operators . 31Logical Operators. 33Assignment Operators . 36Conditional Operator . 37ii

PHPOperators Categories . 38Precedence of PHP Operators . 396.Decision Making . 40The If.Else Statement. 40The ElseIf Statement. 41The Switch Statement . 427.Loop Types . 44The for loop statement . 44The while loop statement . 45The do.while loop statement . 46The foreach loop statement . 47The break statement . 48The continue statement . 488.Arrays . 50Numeric Array . 50Associative Arrays . 51Multidimensional Arrays . 529.Strings . 55String Concatenation Operator . 56Using the strlen() function . 56Using the strpos() function . 5710. Web Concepts . 58Identifying Browser & Platform . 58Display Images Randomly . 59Using HTML Forms. 60Browser Redirection . 61Displaying "File Download" Dialog Box . 61PHP – Regular Expression . 62POSIX Regular Expressions . 6211. GET and POST Method . 65The GET Method . 65The POST Method . 66The REQUEST variable . 6712. File Inclusion . 68The include() Function . 68The require() Function . 6913. Files & I/O . 70Opening and Closing Files . 70Reading a file . 71Writing a file . 72PHP Filesystem Functions . 7314. Functions . 80Creating PHP Function . 80PHP Functions with Parameters . 81iii

PHPPassing Arguments by Reference . 81PHP Functions returning value . 82Setting Default Values for Function Parameters . 83Dynamic Function Calls. 8415. Cookies . 85The Anatomy of a Cookie . 85Setting Cookies with PHP. 86Accessing Cookies with PHP . 87Deleting Cookie with PHP . 8816. Sessions . 89Starting a PHP Session . 89Destroying a PHP Session . 90Turning on Auto Session . 91Sessions without cookies . 9117. Sending Emails . 92Sending plain text email . 92Sending HTML email . 94Sending attachments with email . 9518. File Uploading . 98Creating an upload form. 98Creating an upload script . 9919. Coding Standard . 101PART 2: ADVANCED PHP . 10420. Predefined Variables . 105PHP Superglobals . 105Server variables: SERVER . 10621. Regular Expression . 110POSIX Regular Expressions . 110PHP's Regexp POSIX Functions . 112PERL Style Regular Expressions . 113PHP's Regexp PERL Compatible Functions . 11422. Error and Exception Handling. 115Using die() function . 115Defining Custom Error Handling Function . 115Exceptions Handling . 11823. Error and Logging Functions . 121Installation . 121Runtime Configuration . 121PHP Error and Logging Constants . 122List of Functions . 12324. Bugs Debugging . 125iv

PHP25. Date and Time. 127Getting the Time Stamp with time() . 127Converting a Time Stamp with getdate() . 127Converting a Time Stamp with date() . 129PHP Date and Time Functions . 131PHP Date / Time Constants . 13426. PHP and MySQL . 136What you should already have? . 13627. PHP and AJAX . 137What is AJAX ? . 137PHP and AJAX Example . 137Client Side HTML file. 138Server Side PHP file . 14028. PHP and XML . 142HTML list that's not valid XML . 142HTML list that is valid XML . 142Parsing an XML Document . 142Generating an XML Document . 14329. Object Oriented Programming . 145Object Oriented Concepts . 145Defining PHP Classes . 146Creating Objects in PHP . 147Calling Member Functions . 147Constructor Functions . 148Destructor. 149Inheritance . 149Function Overriding . 150Public Members . 150Private members . 151Protected members . 151Interfaces . 152Constants . 152Abstract Classes . 153Static Keyword . 153Final Keyword . 15430. PHP for C Developers . 156Similarities . 156Differences . 15631. PHP for PERL Developers. 158Similarities . 158Differences . 158PART 3: FUNCTION REFERENCE. 16032. Function Reference . 161v

PHPPart 1: Learning PHP1

1. INTRODUCTIONPHPPHP started out as a small open source project that evolved as more and more peoplefound out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way backin 1994. PHP is a recursive acronym for "PHP: Hypertext Preprocessor". PHP is a server side scripting language that is embedded in HTML. It is used tomanage dynamic content, databases, session tracking, even build entire ecommerce sites. It is integrated with a number of popular databases, including MySQL, PostgreSQL,Oracle, Sybase, Informix, and Microsoft SQL Server. PHP is pleasingly zippy in its execution, especially when compiled as an Apachemodule on the Unix side. The MySQL server, once started, executes even verycomplex queries with huge result sets in record-setting time. PHP supports a large number of major protocols such as POP3, IMAP, and LDAP.PHP4 added support for Java and distributed object architectures (COM andCORBA), making n-tier development a possibility for the first time. PHP is forgiving: PHP language tries to be as forgiving as possible. PHP Syntax is C-Like.Common uses of PHP:PHP performs system functions, i.e. from files on a system it can create, open, read, write,and close them. The other uses of PHP are: PHP can handle forms, i.e. gather data from files, save data to a file, thru emailyou can send data, return data to the user. You add, delete, modify elements within your database thru PHP. Access cookies variables and set cookies. Using PHP, you can restrict users to access some pages of your website. It can encrypt data. 2

PHPCharacteristics of PHPFive important characteristics make PHP's practical nature possible: Simplicity Efficiency Security Flexibility Familiarity"Hello World" Script in PHPTo get a feel for PHP, first start with simple PHP scripts. Since "Hello, World!" is an essentialexample, first we will creat

PHP is forgiving: PHP language tries to be as forgiving as possible. PHP Syntax is C-Like. Common uses of PHP: PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them. The other uses of PHP are: PHP can handle forms, i.e. gather data from files, save data to a file, thru email