150 likes | 154 Views
Learn about PHP, a popular scripting language for web development, with insights on its capabilities, advantages, and speed compared to other languages. Explore PHP functions, file manipulation, variable handling, user-defined functions, and more. Discover the simplicity and power of integrating PHP with HTML. Get an overview of PHP's performance benefits and why it is a top choice for server-side scripting. Reference PEAR for reusable PHP components.
E N D
Introduction to PHP Krerk Piromsopa. Department of Computer Engineering. Chulalongkorn University
What is PHP? • PHP: Hypertext Preprocessor • A widely-used Open Source general-purpose scripting language. • Suitable for Web development / embedded into HTML. • Server-Side scripting. • Simple for a newcomer
What can PHP do? • Server-side scripting (CGI or server module) • Command line scripting (Ideal for cron/task schedule) • Client-side GUI applications (PHP-GTK extension) • Usable on all major operating systems and support for most of the web servers • Support a wide range of databases • Support Java objects and CORBA
Why PHP? • Performance Issue ? • The Zdnet did a evaluation and benchmarking of 4 web scripting languages. During benchmarking, the same spec and identical cpu, memory boxes were used. Under identical conditions, it was found that PHP was the fastest - about 3.7 times faster than JSP and about 1.2 times faster than ASP. Read the report at eWeek and mirror-site The benchmark results are - • PHP pumped out about 47 pages/second • Microsoft ASP pumped out about 43 pages/second • Allaire ColdFusion pumped out about 29 pages/second • Sun Java JSP pumped out about 13 pages/second • KISS policy (Keep It Simple Stupid!!)
Escape between PHP and HTML 1. <? echo ("this is the simplest, an SGML processing instruction\n"); ?> 2. <?php echo("if you want to serve XML documents, do like this\n"); ?> 3. <script language="php"> echo ("some editors (like FrontPage) don't like processing instructions"); </script> 4. <% echo ("You may optionally use ASP-style tags"); %> <%= $variable; # This is a shortcut for "<%echo .." %>
require() and include() Using remote files $file = fopen ("http://www.php.net/", "r"); Image Manipulate Handling File Upload PERL Style Regular Expression $string = "This is a test"; echo ereg_replace (" is", " was", $string); Variable Variable <? $home=“test”; $myvar=“home”; echo $$myvar; ?> Image Submit <input type="image" src="image.gif" name="sub"> <?= “$sub_x,$sub_y” ?> PHP Feature.
<? function max($a,$b) { if ($a>$b) $max = $a; else $max = $b; return $max; } ?> <? function swap(&$a,&$b) { $tmp=$a; $a=$b; $b=$tmp; } $x=5; $y=10; swap($x,$y); ?> User define function
<? function cook($time,$food="chicken"); { echo "Cook $food in $time mins"; } cook(10); cook(10,"fish" ); ?> <? $boil=“cook”; $boil(100,”water”); ?> User define function (Cont)
File “common.inc” <? function Msgbox($msg,$title); echo "<P><B>$title</B></P>\n"; echo "<P>$msg</P>\n"; ?> File “sample.inc” <? require ("common.inc"); echo "Call to Msgbox"; Msgbox("Hi!!!","Greeting"); ?> Library
<form method=post> <input type=text name="myvar"> </form> <?php // Global GPC echo $myvar; // PHP 3.0.2 echo $HTTP_POST_VARS["myvar"]; // PHP 4.2.0 echo $_POST["myvar"]; ?> Variable from form (POST)
<? $list = `ls`; echo "<pre> $list </pre>"; ?> See system() passthru() exec() popen() escapeshellcmd(). Calling external program
Header, Cookie, and session. • header('WWW-Authenticate: Negotiate'); • setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1); • $_SESSION['count'] = 0;
Java Class Support <?php // get instance of Java class java.lang.System in PHP $system = new Java('java.lang.System'); // demonstrate property access print 'Java version='.$system->getProperty('java.version').' <br>'; print 'Java vendor=' .$system->getProperty('java.vendor').' <br>'; print 'OS='.$system->getProperty('os.name').' '. $system->getProperty('os.version').' on '. $system->getProperty('os.arch').' <br>'; // java.util.Date example $formatter = new Java('java.text.SimpleDateFormat', "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"); print $formatter->format(new Java('java.util.Date')); ?>
What is PEAR? • "The fleshy pome, or fruit, of a rosaceous tree (Pyrus communis), cultivated in many varieties in temperate climates." • PEAR is a framework and distribution system for reusable PHP components.
Reference • http://www.devnetwork.net/ • http://www.php.net/ • http://pear.php.net/ • http://www.phpadvisory.com/