1 / 76

PHP

PHP. The Basic. Mohammad Al- Samarah. Outline. History of PHP What is PHP? What does PHP code look like? Apache Server. Syntax PHP code . Anatomy of a PHP Script . Data Types. Variables. Constants &Operators. Control Structures. Errors and Error Management. History of PHP.

dinah
Download Presentation

PHP

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 The Basic Mohammad Al-Samarah

  2. Outline • History of PHP • What is PHP? • What does PHP code look like? • Apache Server. • Syntax PHP code . • Anatomy of a PHP Script . • Data Types. • Variables. • 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 language designed 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 .php by convention

  5. What is 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 <?php echo “Welcome in PHP Page “; echo “my name is ali “; ?>

  12. Variables in PHP • 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) • Etc.

  13. Variables in PHP <?php$name ;?> <?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 = “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, <?phpprint( "$name" ); ?>! </strong> </p> </body> </html>

  16. Variable variables • That is variable whose name is contained in another variable. Example : <?php $data = “a"; $$data = “b"; echo $a; // echo ${'a'}; ?>

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

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

  19. General 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 “\\”)

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

  21. Anatomy of a PHP Script • // or # for single line • /* */ for multiline • /** *some info * *this is type of comments */ • Comments

  22. 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

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

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

  25. Math operations <?php // multiplication echo $num1* $num2 . ‘<br>’; // division echo $num1/num2 . ‘<br>’ ; //increment $num1++; $num2--; echo $num1; ?>

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

  27. Arithmetic Operators <?php $a=15; $b=30; $total=$a+$b; Print $total; Print “<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 /=

  28. 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 • ?>

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

  30. Data type

  31. 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 />”; ?>

  32. 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 />" ); ?>

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

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

  35. Logic Operations

  36. Bitwise Operations

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

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

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

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

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

  42. 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: $A =10; $B =$a; $B =20 Echo $a; // 10

  43. Referencing Operators • But we can change the value of variable $a by the reference , that mena connect right hand to left hand , • Ex: $A =10; $B = &$a; $B= 20; Echo $a; // 20

  44. Constant Value <?php $a = 5; // 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 />" ); ?>

  45. 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.

  46. Escaping the Character • <?php • $heading=“\”Computer Science\””; • $heading1=@“Computer Science”; • Print $heading; • Print $heading1; • ?> “Computer Science”

  47. PHP 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).

  48. If ... Else... • If (condition) { Statements; } Else { Statement; } <?php $user = “John”; If($user==“John”) { Print “Hello John.”; } Else { Print “You are not John.”; } ?> Hello John.

  49. if/else if/else <?php $foo = 4; 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; } ?>

  50. 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 />" );

More Related