AN INTRODUCTION to PHP Create Dynamic Web Applications
Total Page:16
File Type:pdf, Size:1020Kb
1. What Is PHP Programming language created to enable web developers to quickly AN INTRODUCTION TO PHP create dynamic web applications PHP stands for "PHP: hypertext preprocessor" 4LDQJ;X DQG<XDQOLQJ5HQ Html-embedded programming language syntactically similar to C, Perl, and java 2. What a PHP Script Looks Like PHP preprocessor execute all code <HTML> between <?php and ?> tag and return <HEAD> results as text <TITLE>A Simple PHP Example</TITLE> </HEAD> <HTML> <BODY> <HEAD> <?php echo "Hello from PHP!“ ?> <TITLE>A Simple PHP Example</TITLE> </BODY> </HEAD> </HTML> <BODY> Hello from PHP!</BODY> </HTML> 3. The Syntax of the Language Constant similar to C, Perl, and Java Predefined constant Type HTTP_HOST, HTTP_USER_AGENT ... floating-point, integer, string, array, and object Define your own constant Variable <?php $i=5; $f=1.2; define ("aString", “xxxxxx"); $s="This is a simple string."; define("aNumber", 1); $y[0] = "red"; $y[1] = "green"; print(aNumber . "constant with value $color["blue"] = "#0000FF"; of" . aString . "<br>"); $color["green"] = "#00FF00"; ?> Functions OO/Classes Arguments can be passed by value support class creation with syntax similar to C++ and by reference Constructor exist, no destructor <?php function ReturnSum(&$a, $b) { Requires $this variable within classes $c = $a + $b; to refer to member variables and return $c; methods } Single inheritance, but not multiple ?> <?php function AddItem($aName, $aV, $aQty){ $this->item[$aName]["Q"] = $aQty; $this->item[$aName]["V"] = $aV; class ShoppingBasket { $this->value += $aV * $aQty; var $item; return true; var $value; } ... function ShoppingBasket ($initValue) { } $this->value = $initValue; $aBasket = new ShoppingBasket(3.5); } $aBasket->AddItem("Rose", 10, 2); $aBasket->PrintBasket(); ?> Pattern Matching 4. Forms and Cookies PHP supports two types of pattern- Forms matching functions: <form action = "post1.phtml" method = Perl-compatible regular expression "post"> ...... </form> functions, function names all start with preg_ string Form values stored in PHP-Generated variables, e.g, HTTP_POST_VARS POSIX-style functions: ereg(), ergi(), ereg_replace(), Form data validation eregi_replace(), split() regular expression functions in PHP programs validator class 5. Working With File Objects provide C-like functions for One of beautiful thing about PHP is its managing file objects open-source.http://www.thewebmasters.net provides great open-source classes and source modules. For data validation, fopen(),fclose(), fput(), popen() Validator class provides an array of fsockopen("208.129.36.164", 17), functions, e.g, is_email(), is_url(), is_phone() upload files whose names are submitted in form data, i.e., it Cookies can read a file of client and setcookie(), HTTP_COOKIE_VARS. save it in server side. 6. Working With Databases An example using MySQL support <?php Adabas D, dBase, Empress, FilePro, $aLink = mysql_connect(“xx.xx.com", Informix, InterBase, mSQL, MySQL, "user", "passwd" ); Oracle, postgreSQL, Solid, Sybase, Velocis, Unix dbm, Microsoft SQL if( !empty($aLink) ) { Server, ODBC if( mysql_select_db( "mydb", $aLink ) == True ) { With inclusion of ODBC, PHP probably $Qry = "select * from E"; can be used to access any available $R = mysql_query($Qry, $aLink); DBMS. if($R == True ) { 7. Debugging while($aRow = assert() mysql_fetch_array($R)){ $aFName = $aRow["first"]; assert('is_array($anArray )'); $aPos = $aRow["position"]; print( "$aFName, $aPos <br>" ); error_log() } error_log(“Error here!", 0); } error_log(“Err!",3,"/tmp/error.log"); } error_log(“Error!", 1, “[email protected]", } "From: [email protected]\r\n"); ?> write your own error handler 8. Code Reuse Reusing code is significant function myErrorHandler( $aErrorNo, consideration in any programming $aErrorStr, $aFile, $aLine, $aContext) language { } PHP provides means for including set_error_handler("myErrorHandler"); external files and creating OO classes, it facilitates code reuse. With a little bit effort, you can reuse code written in C/C++, Java, COM, and PHP 9. Cool PHP support template system Send Non_HTML files to browser For E-Commerce, PHP provides full scripting language that can be interfaces to several payment used for any programming task processing systems including CyberCash,VeriSign, and CCVS support The Web Distributed Data Exchange (WDDX) support sockets and network protocols , developing network monitoring utility in PHP is straightforward syntax and structure resemble C with 10.Why Better than Its Alternatives complexity(e.g. memory management, pointers, and strong typing) taken out. PHP is free support direct access to Java objects on any system with Java Virtual Machine Open Source software available, as well as Distributed COM on download complete source code Windows. broad platform support modifiable. designed to allow extension of functionality. It's coded in C and provides a well_defined API Enforce cleaner separation of 11.Conclusion layout and application logic PHP is full-fledged programming language that will enable you to Abundant Connectivity create Web applications with all functionality you need support most current Internet standards: IMAP, FTP, POP, XML, WDDX, LDAP, NIS, and SNMP can be compiled as stand-alone script interpreter, and handle simple system administration tasks .