1 / 77

PHP-"PHP Hypertext Preprocessor"

PHP-"PHP Hypertext Preprocessor". Introduction. PHP, or PHP Hypertext Preprocessor, is one of the most popular server side scripting languages for creating dynamic Web pages. PHP was created in 1994 by Rasmus Lerdorf

lchittenden
Download Presentation

PHP-"PHP Hypertext Preprocessor"

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP-"PHP Hypertext Preprocessor"

  2. Introduction • PHP, or PHP Hypertext Preprocessor, is one of the most popular server side scripting languages for creating dynamic Web pages. • PHP was created in 1994 by Rasmus Lerdorf • In 1995, Lerdorf released it as a package called the “Personal Home Page Tools.” PHP 2 featured built-in database support and form handling. • In 1997, PHP 3 was released, featuring a rewritten parser, increased performance and led to an explosion in PHP use. • It is estimated that over six million domains now use PHP. • The release of PHP 4, which features the new Zend Engine and is much faster and more powerful than its predecessor • More information about the Zend engine can be found at www.zend.com

  3. Introduction • PHP, or PHP Hypertext Preprocessor, is one of the most popular server side scripting languages for creating dynamic Web pages. • The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.

  4. Introduction • PHP is an open-source technology that is supported by a large community of users and developers. • Open source software provides developers with access to the software’s source code and free redistribution rights. • PHP is platform independent; implementations exist for all major UNIX, Linux and Windows operating systems. • PHP also provides support for a large number of databases, including MySQL

  5. What is PHP? • PHP is an acronym for "PHP Hypertext Preprocessor" • PHP is a widely-used, open source scripting language • PHP scripts are executed on the server • PHP costs nothing, it is free to download and use

  6. What is a PHP File? • PHP files can contain text, HTML, CSS, JavaScript, and PHP code • PHP code are executed on the server, and the result is returned to the browser as plain HTML • PHP files have extension ".php"

  7. What Can PHP Do? • PHP can generate dynamic page content • PHP can create, open, read, write, and close files on the server • PHP can collect form data • PHP can send and receive cookies • PHP can add, delete, modify data in your database • PHP can restrict users to access some pages on your website • PHP can encrypt data

  8. PHP 5 Syntax • The PHP script is executed on the server, and the plain HTML result is sent back to the browser. • A PHP script can be placed anywhere in the document. • A PHP script starts with <?php and ends with ?>: <?php// PHP code goes here?>

  9. Comments in PHP <!DOCTYPE html><html><body><?php// This is a single line comment# This is also a single line comment/*This is a multiple lines comment blockthat spans over more thanone line*/?></body></html>

  10. PHP  Introduction • PHP scripts are executed on the server. • PHP code is embedded directly into XHTML documents. <!DOCTYPE html><html><body><h1>My first PHP page</h1> <?phpecho “ Hello World !"; ?>  </body></html>

  11. PHP 5 Variables • Variables are "containers" for storing information: • PHP variables are "multitype” • A variable starts with the $ sign, followed by the name of the variable <!DOCTYPE html><html><body><?php$x=5;$y=6;$z=$x+$y;echo $z;?></body></html>

  12. PHP 5 echo and print Statements • In PHP there is two basic ways to get output: echo and print. • echo - can output one or more strings • echo "This ", "string ", "was ", "made ", "with multiple parameters."; • print - can only output one string, and returns always 1 • print $x + $y; • echo is marginally faster compared to print as echo does not return any value.

  13. PHP Data Types • String, Integer, Floating point numbers, Boolean, Array, Object, NULL. • PHP Strings • A string is a sequence of characters, like "Hello world!". • A string can be any text inside quotes. You can use single or double quotes: <?php $x = "Hello world!";echo $x;echo "<br>"; $x = 'Hello world!';echo $x;?>

  14. PHP Data Types • PHP Integers (the PHP var_dump() function returns the data type and value of variables) • float 20.10 • PHP Floating Point Numbers • PHP Booleans <?php $x = 20.10;var_dump($x); <?php $x = 10.365; var_dump($x); $x=true;$y=false; var_dump($x);

  15. Demonstrates the PHP data types • <?php • $testString = "3.5 seconds"; • $testDouble = 79.2; • $testInteger = 12; • ?> • <?php print( $testString ) ?> is a string.<br /> • 24 <?php print( $testDouble ) ?> is a double.<br /> • 25 <?php print( $testInteger ) ?> is an integer.<br /> • <br />

  16. <?php • print( "$testString" ); • settype( $testString, "double" ); • print( " as a double is $testString <br />" ); • print( "$testString" ); • settype( $testString, "integer" ); • print( " as an integer is $testString <br />" ); • settype( $testString, "string" ); • print( "Converting back to a string results in • $testString <br /><br />" );

  17. $data = "98.6 degrees"; • print( "Now using type casting instead: <br /> • As a string - " . (string) $value . • "<br />As a double - " . (double) $value . • "<br />As an integer - " . (integer) $value ); • ?>

  18. Set a PHP Constant • To set a constant, use the define() function - it takes three parameters: • The first parameter defines the name of the constant, • the second parameter defines the value of the constant, • and the optional third parameter specifies whether the constant name should be case-insensitive.

  19. function defineto create a named constant. • <?php • define("name","5"); • echo name+10; • ?> • In PHP, uninitialized variables have the value undef, which evaluates to different values, depending on its context.

  20. PHP Data Types PHP Arrays $second = array( "zero", "one", "two", "three" ); for ( $i = 0; $i < count( $second ); $i++ ) print( "Element $i is $second[$i] <br />" ); $first[ 0 ] = "zero"; $first[ 1 ] = "one"; $first[ 2 ] = "two"; 21 $first[] = "three";

  21. assign values to non-numerical indices $third[ "Harvey" ] = 21; $third[ "Paul" ] = 18; $third[ "Tem" ] = 23; for ( reset( $third ); $element = key( $third ); next( $third ) ) print( "$element is $third[$element] <br />" ); Function reset sets the iterator to the first element of the array. Function key returns the index of the element to which the iterator points, and function next moves the iterator to the next element.

  22. The for loop continues to execute as long as function key returns an index. • The foreach loop is a control structure that is specially designed for iterating through arrays . The syntax for a foreach loop starts with the array to iterate through, followed by the keyword as, followed by the variables to receive the index and the value for each element.

  23. $fourth = array( "January" => "first", "February" => "second", "March" => "third", "April" => "fourth", "May" => "fifth", "June" => "sixth", "July" => "seventh", "August" => "eighth", "September" => "ninth", "October" => "tenth", "November" => "eleventh","December" => "twelfth“ ); // print each element’s name and value foreach ( $fourth as $element => $value ) print( "$element is the $value month <br />" );

  24. PHP Control Stuctures • PHP Conditional Statements • if statement - executes some code only if a specified condition is true • if...else statement - executes some code if a condition is true and another code if the condition is false • if...elseif....else statement - selects one of several blocks of code to be executed • switch statement - selects one of many blocks of code to be executed

  25. PHP Loops • while - loops through a block of code as long as the specified condition is true • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true • for - loops through a block of code a specified number of times • foreach - loops through a block of code for each element in an array <?php $colors = array("red","green","blue","yellow"); foreach ($colors as $value) {   echo "$value <br>";}?>  

  26. PHP User Defined Functions • A function is a block of statements that can be used repeatedly in a program. • A function will not execute immediately when a page loads. • A function will be executed by a call to the function.

  27. A user defined function declaration starts with the word "function": Syntax function functionName() {    code to be executed;}

  28. PHP User Defined Functions <?phpfunction writeMsg(){echo "Hello world!";}writeMsg(); // call the function?>

  29. PHP Function Arguments Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

  30. <?php function familyName($fname) {    echo "$fname Refsnes.<br>";}familyName("Jani");familyName("Hege");familyName("Stale");familyName("Kai Jim");familyName("Borge");?>

  31. The following example has a function with two arguments ($fname and $year): <?phpfunction familyName($fname, $year) {    echo "$fname Refsnes. Born in $year <br>";}familyName("Hege", "1975");familyName("Stale", "1978");familyName("Kai Jim", "1983");?>

  32. PHP Default Argument Value The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument: <?phpfunction setHeight($minheight = 50) {    echo "The height is : $minheight <br>";}setHeight(350);setHeight(); // will use the default value of 50setHeight(135);setHeight(80);?>

  33. PHP Functions - Returning values <?phpfunction sum($x, $y) {    $z = $x + $y;    return $z;}echo "5 + 10 = " . sum(5, 10) . "<br>";echo "7 + 13 = " . sum(7, 13) . "<br>";echo "2 + 4 = " . sum(2, 4);?>

  34. PHP String Functions • The PHP strlen() function • The strlen() function returns the length of a string, in characters. • The PHP strpos() function • The strpos() function is used to search for a specified character or text within a string. <?phpecho strlen("Hello world!");?> <?phpecho strpos("Hello world!","world");?> // ouput 6

  35. Counting Number of Words in a String • The str_word_count() function counts the number of words in a string. <?php $my_str = 'The quick brown fox jumps over the lazy dog.'; // Outputs: 9 echo str_word_count($my_str); ?>

  36. Replacing Text within Strings • The str_replace() replaces all occurrences of the search text within the target string. • Example <?php $my_str = 'If the facts do not fit the theory, change the facts.'; // Display replaced string echo str_replace("facts", "truth", $my_str); ?>

  37. You can optionally pass the fourth argument to the str_replace() function to know how many times the string replacements was performed, like this. <?php $my_str = 'If the facts do not fit the theory, change the facts.'; // Perform string replacement str_replace("facts", "truth", $my_str, $count); // Display number of replacements performed echo "The text was replaced $count times."; ?>

  38. Reversing a String • The strrev() function reverses a string. <?php $my_str = 'You can do anything, but not everything.'; // Display reversed string echo strrev($my_str); ?> • The output of the above code will be: • .gnihtyreve ton tub ,gnihtyna od nac uoY

  39. PHP - Sort Functions For Arrays • sort() - sort arrays in ascending order • rsort() - sort arrays in descending order • asort() - sort associative arrays in ascending order, according to the value • ksort() - sort associative arrays in ascending order, according to the key • arsort() - sort associative arrays in descending order, according to the value • krsort() - sort associative arrays in descending order, according to the key

  40. Sort Array in Ascending Order - sort() <?php$cars = array("Volvo", "BMW", "Toyota");sort($cars);?> <?php$numbers = array(4, 6, 2, 22, 11);sort($numbers);?>

  41. Sort Array in Descending Order - rsort() <?php$cars = array("Volvo", "BMW", "Toyota");rsort($cars);?>

  42. Sort Array (Ascending Order), According to Value - asort() The following example sorts an associative array in ascending order, according to the value: <?php$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");asort($age);?>

  43. OUTPUT Key=Peter, Value=35Key=Ben, Value=37Key=Joe, Value=43

  44. Sort Array (Ascending Order), According to Key - ksort() The following example sorts an associative array in ascending order, according to the key: <?php$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");ksort($age);?>

  45. OUTPUT Key=Ben, Value=37Key=Joe, Value=43Key=Peter, Value=35

  46. Sort Array (Descending Order), According to Value - arsort() The following example sorts an associative array in descending order, according to the value: <?php$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");arsort($age);?>

  47. OUTPUT Key=Joe, Value=43Key=Ben, Value=37Key=Peter, Value=35

  48. Sort Array (Descending Order), According to Key - krsort() The following example sorts an associative array in descending order, according to the key: <?php$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");krsort($age);?>

  49. OUTPUT Key=Peter, Value=35Key=Joe, Value=43Key=Ben, Value=37

More Related