Introduction to PHP Yasar Hussain Malik NISTE PHP

  • Slides: 17
Download presentation
Introduction to PHP Yasar Hussain Malik - NISTE

Introduction to PHP Yasar Hussain Malik - NISTE

PHP Origins Rasmus Lerdorf PHP originally abbreviation for ‘Personal Home Pages’, now ‘PHP Hypertext

PHP Origins Rasmus Lerdorf PHP originally abbreviation for ‘Personal Home Pages’, now ‘PHP Hypertext Processor’ Other key developers: Zeev Surashi and Andi Gutmans Open Source

Brief History of PHP 1 (Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994.

Brief History of PHP 1 (Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was initially developed for HTTP usage logging and server-side form generation in Unix. PHP 2 (1995) transformed the language into a Serverside embedded scripting language. Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc. PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP, IMAP ), and new parser written by Zeev Suraski and Andi Gutmans. PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml 2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP

Brief History of PHP As of August 2004, PHP is used on 16, 946,

Brief History of PHP As of August 2004, PHP is used on 16, 946, 328 Domains, 1, 348, 793 IP Addresses http: //www. php. net/usage. php This is roughly 32% of all domains on the web.

Brief History of PHP As of April 2007, PHP is used on 20, 917,

Brief History of PHP As of April 2007, PHP is used on 20, 917, 850 Domains

Open Source In general, open source refers to any program whose source code is

Open Source In general, open source refers to any program whose source code is made available for use or modification as users or other developers see fit. Open source software is usually developed as a public collaboration and made freely available. Open Source is a certification mark owned by the Open Source Initiative (OSI). Developers of software that is intended to be freely shared and possibly improved and redistributed by others can use the Open Source trademark if their distribution terms conform to the OSI's Open Source Definition. To summarize, the Definition model of distribution terms require that: The software being distributed must be redistributed to anyone else without any restriction. The source code must be made available (so that the receiving party will be able to improve or modify it). The license can require improved versions of the software to carry a different name or version from the original software.

Why is PHP used? Cross Platform Runs on almost any Web server on several

Why is PHP used? Cross Platform Runs on almost any Web server on several operating systems. One of the strongest features is the wide range of supported databases Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server Operating Systems: UNIX (HP-UX, Open. BSD, Solaris, Linux), Mac OSX, Windows NT/98/2000/XP/2003 Supported Databases: Adabas D, d. Base, Empress, File. Pro (read-only), Hyperwave, IBM DB 2, Informix, Ingres, Inter. Base, Front. Base, m. SQL, Direct MS-SQL, My. SQL, ODBC, Oracle (OCI 7 and OCI 8), Ovrimos, Postgre. SQL, SQLite, Solid, Sybase, Velocis, Unix dbm

…Why is PHP used? 3. Cost Benefits PHP is free. Open source code means

…Why is PHP used? 3. Cost Benefits PHP is free. Open source code means that the entire PHP community will contribute towards bug fixes. There are several add-on technologies (libraries) for PHP that are also free. PHP Software Free Platform Free (Linux) Development Tools Free PHP Coder, j. Edit

Scripting languages 1. Often evolved not designed 2. Cross-platform since interpreter is easy to

Scripting languages 1. Often evolved not designed 2. Cross-platform since interpreter is easy to port 3. Designed to support a specific task – PHP -> Web support 4. Un-typed variables (but values are typed) 5. Implicit variable declaration 6. Implicit type conversion 7. Stored only as script files

PHP details Procedural language Compare with Javascript which is event-driven C-like syntax - {

PHP details Procedural language Compare with Javascript which is event-driven C-like syntax - { } ; Extensive Function Library Good Web-server integration Script embedded in HTML Easy access to form data and output of HTML pages Not fully object-oriented Java is fully object oriented – all functions have to be in a class In PHP, classes are additional but quite simple to use

Getting Started How to escape from HTML and enter PHP mode 1. PHP parses

Getting Started How to escape from HTML and enter PHP mode 1. PHP parses a file by looking for one of the special tags that tells it to start interpreting the text as PHP code. The parser then executes all of the code it finds until it runs into a PHP closing tag. HTML <? php PHP CODE echo “Hello World”; HTML ? > Starting tag Ending tag Notes <? php ? > Preferred method as it allows the use of PHP with XHTML <? ? > Not recommended. Easier to type, but has to be enabled and may conflict with XML <script language="php"> ? > Always available, best if used when Front. Page is the HTML editor <% %> Not recommended. ASP tags support was added in 3. 0. 4

PHP and HTML-embedded PHP scripts are essentially HTML pages with the occasional section of

PHP and HTML-embedded PHP scripts are essentially HTML pages with the occasional section of PHP script is enclosed in the tag pair: <? php print date(“H: I”) ? >

Examples Benefits: - Any changes to header or footer only require editing of a

Examples Benefits: - Any changes to header or footer only require editing of a single file. This reduces the amount of work necessary for site maintenance and redesign. - Helps separate the content and design for easier maintenance Header Page 1 Content Page 2 Content Page 3 Content Footer Page 4 Content Page 5 Content

C-like language Free format - white space is ignored Statements are terminated by semi-colon

C-like language Free format - white space is ignored Statements are terminated by semi-colon ; Statements grouped by { … } Comments begin with // or a set of comments /* */ Assignment is ‘=’: $a=6 Relational operators are , < , > == ( not a single equal) Control structures include if (cond) {. . } else { }, while (cond) {. . } , for(sstartcond; increment; endcond) { } Arrays are accessed with [ ] : $x[4] is the 5 th element of the array $x – indexes start at 0 Functions are called with the name followed by arguments in a fixed order enclosed in ( ) : substr(“fred”, 0, 2) Case sensitive - $red is a different variable to $RED

Function library Basic tasks String Handling Mathematics – random numbers, trig functions. . Regular

Function library Basic tasks String Handling Mathematics – random numbers, trig functions. . Regular Expressions Date and time handling File Input and Output And more specific functions for- Database interaction – My. SQL, Oracle, Postgres, Sybase, MSSQL. . Encryption Text translation Spell-checking Image creation XML

String Handling String literals (constants) enclosed in double quotes “ ” or single quotes

String Handling String literals (constants) enclosed in double quotes “ ” or single quotes ‘ ’ Within “”, variables are replaced by their value: – called variable interpolation. “My name is $name, I think” Within single quoted strings, interpolation doesn’t occur Strings are concatenated (joined end to end) with the dot operator “key”. ”board” == “keyboard” Standard functions exist: strlen(), substr() etc Values of other types can be easily converted to and from strings – numbers implicitly converted to strings in a string context. Regular expressions be used for complex pattern matching.

3 Tier architecture voice touch DHTML Browser (IE, Fire. Fox, Opera) vision HTTP HTML

3 Tier architecture voice touch DHTML Browser (IE, Fire. Fox, Opera) vision HTTP HTML Desktop (PC or MAC) PHP script Web Server (Apache, IIS) SQL Database tables Database Server Web Service Client application Remote services