Introduction to PHP

coordinatedcapableSoftware and s/w Development

Nov 4, 2013 (3 years and 8 months ago)

90 views

Krerk Piromsopa.

Introduction to PHP

Krerk Piromsopa.

Department of Computer Engineering.

Chulalongkorn University

Krerk Piromsopa.

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

Krerk Piromsopa.

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

Krerk Piromsopa.

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!!)

Krerk Piromsopa.

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 .." %>

Krerk Piromsopa.

PHP Feature.


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” ?>


Krerk Piromsopa.

User define function

<?

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);

?>


Krerk Piromsopa.

User define function (Cont)

<?

function
cook($time,$food="chicken");

{


echo "Cook $food in $time
mins";

}


cook(10);

cook(10,"fish" );

?>

<?

$boil=“cook”;


$boil(100,”water”);

?>

Krerk Piromsopa.

Library


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");

?>


Krerk Piromsopa.

Variable from form (POST)

<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"];

?>

Krerk Piromsopa.

Calling external program

<?

$list = `ls`;


echo "<pre> $list </pre>";


?>


See


system()


passthru()


exec()


popen()


escapeshellcmd().

Krerk Piromsopa.

Header, Cookie, and session.


header('WWW
-
Authenticate: Negotiate');



setcookie ("TestCookie", "", time()
-

3600, "/~rasmus/",
".utoronto.ca", 1);



$_SESSION['count'] = 0;



Krerk Piromsopa.

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'));

?>


Krerk Piromsopa.

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.

Krerk Piromsopa.

Reference


http://www.devnetwork.net/


http://www.php.net/


http://pear.php.net/


http://www.phpadvisory.com/