Introduction to PHP PHP is a serverside scripting

  • Slides: 17
Download presentation
Introduction to PHP “PHP is a server-side scripting language designed specifically for the Web.

Introduction to PHP “PHP is a server-side scripting language designed specifically for the Web. Within an HTML page, you can embed PHP code that will be executed each time the page is visited. Your PHP code is interpreted at the Web server and generates HTML or other output that the visitor will see” (“PHP and My. SQL Web Development”, Luke Welling and Laura Thomson, SAMS)

Introduction to PHP p PHP Hypertext Preprocessor. n p Other Names : Personal Home

Introduction to PHP p PHP Hypertext Preprocessor. n p Other Names : Personal Home Page, Professional Home Page Is a server side scripting language. n Capable of generating the HTML pages HTML generates the web page with the static text and images. p However the need evolved for dynamic web based application, mostly involving database usage. p

WEB SERVER Gets Page <HTML> <? php PHP code ? > </HTML> HTTP Request

WEB SERVER Gets Page <HTML> <? php PHP code ? > </HTML> HTTP Request (url) Server response CLIENT Browser creates the web page Hello Interprets the PHP code <HTML> <B>Hello</B> </HTML>

Why PHP? p p . . there are no. of server side scripting available

Why PHP? p p . . there are no. of server side scripting available like ASP, SSJS, JSP…. . PHP involves n n p PHP is n n p simplicity in scripting (. . generally using the database) platform independence. primarily designed for web applications well optimized for the response times needed for web applications Is an open source.

PHP Block PHP code block is embedded within the <? php and ? >

PHP Block PHP code block is embedded within the <? php and ? > tags. p When the server encounters the PHP tags it switches from the HTML to PHP mode. p There are four different ways to embed the PHP code p n n <? php echo(“Some PHP code”); ? > <? echo(“Some PHP code”); ? > <SCRIPT Language=‘php’> echo(“Some PHP code”); </SCRIPT> <% echo(“Some PHP code”); %>

Hello World! (web oriented) <html> <head> <title>My personal Hello World! PHP script</title> </head> <body>

Hello World! (web oriented) <html> <head> <title>My personal Hello World! PHP script</title> </head> <body> <? PHP tag, allow to insert PHP echo “Hello World!”; code. Interpretation by PHP ? > module will substitute the </html> code with code output

Variables (I) To use or assign variable $ must be present before the name

Variables (I) To use or assign variable $ must be present before the name of the variable The assign operator is '=' There is no need to declare the type of the variable the current stored value produces an implicit type-casting of the variable. A variable can be used before to be assigned $A = 1; $B = “ 2”; $C = ($A + $B); // Integer sum $D = $A. $B; // String concatenation echo $C; // prints 3 echo $D; // prints 12

Variables (II) Function isset tests if a variable is assigned or not $A =

Variables (II) Function isset tests if a variable is assigned or not $A = 1; if (isset($A)) print “A isset” if (!isset($B)) print “B is NOT set”; Using $$ $help = “hidden. Var”; $$help = “hidden Value”; echo $$help; // prints hidden Value $$help = 10; $help = $$help * $$help; echo $help; // print 100

PHP Variables (cont. ) p Rich set of functions for working with variable. n

PHP Variables (cont. ) p Rich set of functions for working with variable. n For e. g p gettype, settype, isset, unset, is_int, intval etc

Strings (I) A string is a sequence of chars $string. Test = “this is

Strings (I) A string is a sequence of chars $string. Test = “this is a sequence of chars”; echo $string. Test[0]; output: t echo $string. Test; output: this is a sequence of chars A single quoted strings is displayed “as-is” $age = 37; $string. Test = 'I am $age years old'; // output: I am $age years old $string. Test = “I am $age years old”; // output: I am 37 years old Concatenation $conc = ”is “. ”a “. ”composed “. ”string”; echo $conc; // output: is a composed string $new. Conc = 'Also $conc '. $conc; echo $new. Conc; // output: Also $conc is a composed string

Arrays (I) Groups a set of variables, every element stored into an array as

Arrays (I) Groups a set of variables, every element stored into an array as an associated key (index to retrieve the element) $books = array( 0=>”php manual”, 1=>”perl manual”, 2=>”C manual”); $books = array( 0=>”php manual”, ”perl manual”, ”C manual”); $books = array (“php manual”, ”perl manual”, ”C manual”); echo $books[2]; output: C manual Arrays with PHP are associative $books = array( “php manual”=>1, ”perl manual”=>1, ”C manual”=>1); // HASH echo $books[“perl manual”]; output: 1 $books[“lisp manual”] = 1; // Add a new element

Arrays (II) Working on an arrays $books = array( ”php manual”, ”perl manual”, ”C

Arrays (II) Working on an arrays $books = array( ”php manual”, ”perl manual”, ”C manual”); Common loop for ($i=0; $i < count($books); $i++) print ($i+1). ”-st book of my library: $books[$i]”; each $books = array( “php manual”=>1, ”perl manual”=>2, ”C manual”=>3); while ($item = each( $books )) // Retrieve items one by one print $item[“value”]. ”-st book of my library: ”. $item[“key”]; // each retrieve an array of two elements with key and value of current element each and list while ( list($value, $key) = each( $books )) print “$value-st book of my library: $key”; // list collect the two element retrieved by each and store them in two different // variables

Arrays (II) each and list while ( list($value, $key) = each( $books )) print

Arrays (II) each and list while ( list($value, $key) = each( $books )) print “$value-st book of my library: $key”; // list collect the two element retrieved by each and store them in two different // variables

Arrays (III) Multidimensional arrays $books = array(“title”=>“php manual”, ”editor”=>”X”, ”author”=>”A”), array(“title”=>“perl manual”, ”editor”=>”Y”, ”author”=>”B”),

Arrays (III) Multidimensional arrays $books = array(“title”=>“php manual”, ”editor”=>”X”, ”author”=>”A”), array(“title”=>“perl manual”, ”editor”=>”Y”, ”author”=>”B”), array(“title=>“C manual”, ”editor”=>”Z”, author=>”C”)); Common loop for ($i=0; $i < count($books); $i++ ) print “$i-st book, title: ”. $books[$i][“title”]. ” author: “. $books[$i][“author”]. “ editor: “. $books[$i][“editor”]; // Add. ”n” for text new page or “. <BR>” for HTML new page; Use list and each for ($i=0; $i < count($books); $i++) { print “$i-st book is: “; while ( list($key, $value) = each( $books[$i] )) print “$key: $value ”; print “<BR>”; // or “n” }

Arrays (cont. ) p $ncststaff = array (“dake” => array(“amrish”, “lakshana”, “venkat”), “spc” =>

Arrays (cont. ) p $ncststaff = array (“dake” => array(“amrish”, “lakshana”, “venkat”), “spc” => array(“narayan”, “murali”, “prasad”)); § creates a two dimensional array. p Sorting Functions n n sort() : sorts the elements in the numeric and alphabetical order. rsort() : sorts the elements in the reverse order. asort() : sorts the elements in the array without changing the indices. ksort() : sorts the arrays by key.

PHP Statements p IF statement if (<condition>) { //php code goes here } else

PHP Statements p IF statement if (<condition>) { //php code goes here } else { //php code goes here } p Alternative Syntax if(<condition>) : //html code goes here else : //html code goes here endif;

PHP Statements (cont. ) p For loop for($i=0; $i < 10; $++i) { echo(“the

PHP Statements (cont. ) p For loop for($i=0; $i < 10; $++i) { echo(“the value is : ”. $i); n } Alternative Syntax for($i=0; $i < 10; $++i) : // html code goes here endfor; While loop p Do-While loop p