Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
PHP PowerPoint Presentation

PHP

206 Vues Download Presentation
Télécharger la présentation

PHP

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. PHP The Basic PHP Mohammad Al-Samarah

  2. Out line • History of PHP • What is PHP? • Why PHP ? • What you need to start using PHP ? • What does PHP code look like? • Syntax PHP code . • Echo Statement • Variables. • Anatomy of a PHP Script . • Data Types. • Constants &Operators. • Control Structures. • Errors and Error Management .

  3. History of PHP • PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was initially developed for HTTP usage logging and server-side form generation in Unix. • PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc. • PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans . • PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. • PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP

  4. What is PHP? • Personal Homepage Tools/Form Interpreter • PHP is a Server-side Scripting Languagedesigned specifically for the Web. • An open source language • PHP code can be embedded within an HTML page, which will be executed each time that page is visited. • Filenames end with .phpby convention

  5. Whatis PHP? (cont’d) • Interpreted language, scripts are parsed at run-time rather than compiled beforehand • Executed on the server-side • Source-code not visible by client • ‘View Source’ in browsers does not display the PHP code • Various built-in functions allow for fast development • Compatible with many popular databases

  6. Why PHP ? • Open source / free software • Cross platform to develop and deploy and to use • Powerful, robust , scalable • Web development specific • Can be object oriented especially version 5 • Large active developer community (20 millions websites • Great documentation in many language www.php.net/docs.php

  7. What you need to start using php ? • Installation • You will need • Web server ( Apache ) • PHP ( version 5.3) • Database ( MySQL 5 ) • Text editor (Notepad) • Web browser (Firefox ) • www.php.net/manual/ en/install.php

  8. What does PHP code look like? • Structurally similar to C/C++ • Supports procedural and object-oriented paradigm (to some degree) • All PHP statements end with a semi-colon • Each PHP script must be enclosed in the reserved PHP tag <?php …?>

  9. Syntax PHP code • Standard Style : <?php …… ?> • Short Style: <? … ?> • Script Style: <SCRIPT LANGUAGE=‘php’> </SCRIPT> • ASP Style: <% echo “Hello World!”; %>

  10. Echo • The PHP command ‘echo’ is used to output the parameters passed to it . • The typical usage for this is to send data to the client’s web-browser • Syntax : void echo (string arg1 [, string argn...]) • In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual function

  11. Echo - Example • <?php echo “ This my first statement in PHP language“; • ?>

  12. Variables • PHP variables must begin with a “$” sign • Case-sensitive ($Foo != $foo != $fOo) • Global and locally-scoped variables • Global variables can be used anywhere • Local variables restricted to a function or class • Certain variable names reserved by PHP • Form variables ($_POST, $_GET) • Server variables ($_SERVER)

  13. Variables <?php$name = “ali”echo( $name); ?>

  14. Variables <?php $name = “Mohamed”; $age = 23; Echo “ My name is $name and I am $age years old”; ?>

  15. Variables • <?php • $name = 'elijah'; • $yearborn = 1975; • $currentyear = 2005; • $age = $currentyear - $yearborn; • echo ("$name is $age years old."); • ?>

  16. Variables <?php$name = “Ali"; // declaration ?> <html> <head> <title>A simple PHP document</title> </head> <body style = "font-size: 2em"> <p> <strong> <!-- print variable name’s value --> Welcome to PHP, <?phpecho( "$name" ); ?>! </strong> </p> </body> </html>

  17. Single & Double Quotes <?php echo “ Hello world <br>”; echo ‘ Hello world’; ?>

  18. Single & Double Quotes <?php $word = ‘ World’; echo “ Hello $word <br>”; echo ‘ Hello $word <br>’; ?>

  19. Anatomy of a PHP Script • // or # for single line • /* */ for multiline • /* this is my comment one this is my comment two this is my comment three */ • Comments

  20. Anatomy of a PHP Script • You cant have any whitespace between <? and php. • You cant break apart keywords (e.g :while,function,fo r) • You cant break apart varible names and function names (e.g:$varname,function f 2) • Whitespace

  21. Anatomy of a PHP Script • Is simply a series of statements' enclosed between two braces : { //some comand } • Code Block

  22. Concatenation • Use a period to join strings into one. • <?php • $string1=“Hello”; • $string2=“PHP”; • $string3=$string1 . “ ” . $string2; • Print $string3; • ?> Hello PHP

  23. Escaping the Character • If the string has a set of double quotation marks that must remain visible, use the \ [backslash] before the quotation marks to ignore and display them. <?php $heading=“\”Computer Science\””; $heading1=@“Computer Science”; echo $heading; echo $heading1; ?> “Computer Science”

  24. Example • <?php • $foo = 25; // Numerical variable$bar = “Hello”; // String variable • echo $bar; // Outputs Hello • echo $foo,$bar; // Outputs 25Hello • echo “5x5=”,$foo; // Outputs 5x5=25 • echo “5x5=$foo”; // Outputs 5x5=25echo ‘5x5=$foo’; // Outputs 5x5=$foo • ?> • Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25 • Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP • This is true for both variables and character escape-sequences (such as “\n” or “\\”)

  25. Data type

  26. Data type <?php // declare a string, double and integer $testString = "3.5 seconds"; $testDouble = 79.2; $testInteger = 12; print( $testString ).”is a string<br/>”; print( $testDouble ).”is a double<br />” print( $testInteger ).”is an integer<br />”; ?>

  27. Data type <?php // call function settype to convert variable // testString to different data types 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 />" ); ?>

  28. Data type <?php $data = "98.6 degrees"; echo( "Now using type casting instead: <br />As a string - " . (string) $data . "<br />As a double - " . (double) $data ."<br />As an integer - " . (integer) $data ); ?>

  29. Data type <?php $a = “ 12.4 abc” echo (int) $a; echo (double) ($a); echo (float) ($a); echo (string) ($a); ?>

  30. Arithmetic Operations - Assignment operators • Syntactical shortcuts • Before being assigned values, variables have value undef - Constants • Named values • define function

  31. Arithmetic Operators <?php $a=15; $b=30; $total=$a+$b; echo $total; echo“<p><h1>$total</h1>”; // total is 45 ?> • $a - $b // subtraction • $a * $b // multiplication • $a / $b // division • $a += 5 // $a = $a+5 Also works for *= and /=

  32. Arithmetic Operators <?php $a =1; echo $a++; // output 1,$a is now equal to 2 echo ++$a; // output 3,$a is now equal to 3 echo --$a; // output 2,$a is now equal to 2 echo $a--; // output 2,$a is now equal to 1 ?>

  33. Arithmetic Operators <?php $a =(int)(‘test’); // $a==0 echo ++$a; ?>

  34. Math operations <?php $num1 = 10; $num2 =20; // addition echo $num1+$mum2 . ‘<br>’; //subtraction echo $num1 - $num2 . ‘<br>’; // multiplication ?>

  35. Math operations <?php // Multiplication echo $num1* $num2 . ‘<br>’; // Division Echo $num1/num2 . ‘<br>’ ; //increment $num1++; $Num2--; Echo $num1; ?>

  36. Example <?php $a = 5; print( "The value of variable a is $a <br />" ); // define constant VALUE define( "VALUE", 5 ); // add constant VALUE to variable $a $a = $a + VALUE; print( "Variable a after adding constant VALUE is $a <br />" );

  37. Example cont.. // multiply variable $a by 2 $a *= 2; print( "Multiplying variable a by 2 yields $a <br />" ); // test if variable $a is less than 50 if ( $a < 50 ) print( "Variable a is less than 50 <br />" ); // add 40 to variable $a $a += 40; print( "Variable a after adding 40 is $a <br />" ); // test if variable $a is 50 or less if ( $a < 51 ) print( "Variable a is still 50 or less<br />" ); // test if variable $a is between 50 and 100, inclusive elseif ( $a < 101 ) print( "Variable a is now between 50 and 100, inclusive<br />" ); else print( "Variable a is now greater than 100<br />" );

  38. Example cont.. // print an uninitialized variable print( "Using a variable before initializing: $nothing <br />" ); // add constant VALUE to an uninitialized variable $test = $num + VALUE; print( "An uninitialized variable plus constant VALUE yields $test <br />" ); // add a string to an integer $str = "3 dollars"; $a += $str; print( "Adding a string to variable a yields $a <br />" ); ?>

  39. Logic Operations

  40. Bitwise Operations

  41. Example cont.. <?php   $x=13;   $y=22;   echo $x & $y;   ?>   <?php   $x=77;   $y=198;   echo $x & $y;   ?> 

  42. Example cont.. <?php   $x=5;   $y=11;   echo $x | $y;   ?> <?php   $x=12;   $y=11;   echo $x ^ $y;   ?>

  43. Example cont.. <?php $x=12;   $y=10;   echo $x & ~ $y;   ?>  <?php $x=8;   $y=3;   echo $x << $y;   ?

  44. Example cont.. <?php $x=12;   $y=10;   echo $x & ~ $y;   ?>  <?php $x=8;   $y=3;   echo $x << $y;   ?

  45. Example cont.. <?php   $x=12;   $y=4;   echo  $x << $y;   ?>  <?php   $x=8;   $y=3;   echo $x >> $y;   ?>

  46. Referencing Operators • We know the assignment operators work by value ,by copy the value to other expression ,if the value in right hand change the value in left is not change . • Ex: <?php $a =10; $b =$a; $b =20 Echo $a; // 10 ?>

  47. Referencing Operators • But we can change the value of variable $a by the reference , that mena connect right hand to left hand , • Example: <?php $a =10; $b = &$a; $b= 20; echo $a; // 20 ?>

  48. Control Structures • Control Structures: Are the structures within a language that allow us to control the flow of execution through a program or script. • Grouped into conditional (branching) structures (e.g. if/else) and repetition structures (e.g. while loops).

  49. If Statement • if (condition) { statements; } else { statement; } • <?php • $user = “jone”; • if($user==“john”) • { • print “hello john.”; • } • else • { • print “you are not john.”; • } • ?> hello john

  50. if/else if/else statement <?php if ($foo == 0) { echo ‘The variable foo is equal to 0’; } else if (($foo > 0) && ($foo <= 5)) { echo ‘The variable foo is between 1 and 5’; } else { echo ‘The variable foo is equal to ‘.$foo; } ?>