Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP The Basic. Outline  History of PHP  What is PHP?  What does PHP code look like?  Apache Server.  Syntax PHP code.  Anatomy of a PHP Script.

Similar presentations


Presentation on theme: "PHP The Basic. Outline  History of PHP  What is PHP?  What does PHP code look like?  Apache Server.  Syntax PHP code.  Anatomy of a PHP Script."— Presentation transcript:

1 PHP The Basic

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 a server-side scripting language 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 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 : Short Style: Script Style: ASP Style:

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

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

15 Variables A simple PHP document Welcome to PHP, !

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 ”; echo ‘ Hello world’; ?>

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

19 General Example 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 “\\”) <?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=25 echo ‘5x5=$foo’;// Outputs 5x5=$foo ?>

20 Concatenation Use a period to join strings into one. <?php $string1=“Hello”; $string2=“PHP”; $string3=$string1. “ ”. $string2; Print $string3; ?> <?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 :whi le,func tion,fo r) You cant break apart varible names and function names (e.g:$var name,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. ‘ ’; //subtraction echo $num1 - $num2. ‘ ’; // multiplication ?>

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

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

27 Arithmetic Operators $a - $b // subtraction $a * $b// multiplication $a / $b// division $a += 5// $a = $a+5 Also works for *= and /= <?php $a=15; $b=30; $total=$a+$b; Print $total; Print “ $total ”; // total is 45 ?> <?php $a=15; $b=30; $total=$a+$b; Print $total; Print “ $total ”; // total is 45 ?>

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 <?php // declare a string, double and integer $testString = "3.5 seconds"; $testDouble = 79.2; $testInteger = 12; print( $testString ).”is a string ”; print( $testDouble ).”is a double ” print( $testInteger ).”is an integer ”; ?>

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 " ); print( "$testString" ); settype( $testString, "integer" ); print( " as an integer is $testString " ); settype( $testString, "string" ); print( "Converting back to a string results in $testString " ); ?>

33 Data type <?php $data = "98.6 degrees"; print( "Now using type casting instead: As a string - ". (string) $data. " As a double - ". (double) $data. " 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 ExampleNameResult $a == $bEqual TRUE if $a is equal to $b after type juggling. $a === $bIdentical TRUE if $a is equal to $b, and they are of the same type. $a != $bNot equal TRUE if $a is not equal to $b after type juggling. $a <> $bNot equal TRUE if $a is not equal to $b after type juggling. $a !== $bNot identical TRUE if $a is not equal to $b, or they are not of the same type. $a < $bLess thanTRUE if $a is strictly less than $b. $a > $bGreater thanTRUE if $a is strictly greater than $b. $a <= $bLess than or equal toTRUE if $a is less than or equal to $b. $a >= $bGreater than or equal toTRUE if $a is greater than or equal to $b.

36 Bitwise Operations ExampleNameResult A & BAnd Bits that are set in both A and B are set. A | BOr Bits that are set in either A or B are set. A ^ BXor Bits that are set in A or B but not both are set. ~ ANot Bits that are set in A are not set, and vice versa. A << BShift left Shift the bits of A B steps to the left (each step means "multiply by two") A >> BShift right Shift the bits of A B steps to the right (each step means "divide by two")

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

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; ?> <?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 <?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 " ); // define constant VALUE define( "VALUE", 5 ); // add constant VALUE to variable $a $a = $a + VALUE; print( "Variable a after adding constant VALUE is $a " );

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

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

53 For Loops for ($varible = value ;condition;$value assigment ) { Statements; } <?php $count=0; for($count = 0;$count <3,$count++) { Print “hello PHP. ”; ?> hello PHP. hello PHP. hello PHP.

54 Example ( break) <?php /* example 1 */ for ($i = 1; $i <= 10; $i++) { echo $i; } ?>

55 Example ( break) 10) { break; } echo $i; } ?>

56 Example ( break) <?php /* example 3 */ $i = 1; for (; ; ) { if ($i > 10) { break; } echo $i; $i++; } /* example 4 */ for ($i = 1, $j = 0; $i

57 Example (for) "; } ?>

58 Example – table <?php $brush_price = 5; echo " "; echo " Quantity "; echo " Price "; for ( $counter = 10; $counter <= 100; $counter += 10) { echo " "; echo $counter; echo " "; echo $brush_price * $counter; echo " "; } echo " "; ?>

59 Example (nested For)

60 Example ( continue)

61 Example (for..if) '; for($i = 0; $i '. $i. ' '; if(($i + 1) % $rows == 0){ echo ' '; } } echo ' '; ?>

62 While Loops While (condition) { Statements; } <?php $count=0; While($count<3) { Print “hello PHP. ”; $count += 1; // $count = $count + 1; // or // $count++; ?> hello PHP. hello PHP. hello PHP.

63 Example (while switch) <?php $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5 \n"; break 1; case 10: echo "At 10; quitting \n"; break 2; default: break; } } ?>

64 Example (nested while) <?php $i = 0; while ($i++ \n"; while (1) { echo " Middle \n"; while (1) { echo " Inner \n"; continue 3; } echo "This never gets output. \n"; } echo "Neither does this. \n"; } ?>

65 Do …While Loops Do { Statements; } While (condition); <?php $count=0; Do { Print “hello PHP. ”; $count += 1; } While($count<3); ?> hello PHP.hello PHP.hello PHP.

66 Example (do..while) 0); ?>

67 Example (do..while)

68 Switch switch(expression ) { case value: break;. default: break; } <?php $count=0; switch($count) { case 0: Print “hello PHP3. ”; break; case 1: Print “hello PHP4. ”; break; default: Print “hello PHP5. ”; break; ?> hello PHP3

69 Example

70 Example

71 Errors & Error Management

72 Example 1 <?php $name = 'Elijah'; $yearBorn = 1975; $currentYear = 2005; $age = $currentYear - $yearBorn; print ("$name is $age years old."); ?>

73 Example 2 <?php print(" Hello and welcome to my website. "); ?>

74 Example 3

75 Example 4 ( Isset) '; // This is an alternative isset( $value ) AND print( $value ); ?>

76 Example 5 (Goto)


Download ppt "PHP The Basic. Outline  History of PHP  What is PHP?  What does PHP code look like?  Apache Server.  Syntax PHP code.  Anatomy of a PHP Script."

Similar presentations


Ads by Google