Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Php. 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."— Presentation transcript:

1 php

2 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

3 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"

4 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 With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.

5 Why PHP? PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP supports a wide range of databases PHP is free. Download it from the official PHP resource: www.php.netwww.php.net PHP is easy to learn and runs efficiently on the server side

6 Install PHP What Do I Need? To start using PHP, you can: Find a web host with PHP and MySQL support Install a web server on your own PC, and then install PHP and MySQL

7 Go to http://www.apachefriends.org/en/xampp-windows.html Download XAMPP Windows 1.8.3 installer

8 PHP 5 Syntax My first PHP page

9 Comments in PHP

10 PHP Case Sensitivity In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are case-insensitive. In the example below, all three echo statements below are legal (and equal): "; echo "Hello World! "; EcHo "Hello World! "; ?>

11 However; in PHP, all variables are case-sensitive. "; echo "My house is ". $COLOR. " "; echo "My boat is ". $coLOR. " "; ?>

12 PHP 5 Variables Variables are "containers" for storing information Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case sensitive ($y and $Y are two different variables)

13 "; echo $x; echo " "; echo $y; ?>

14 PHP Variables Scope In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes: local global static

15 Local and Global Scope A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

16 Test variables inside the function: "; echo "Variable x is: $x"; echo " "; echo "Variable y is: $y"; } myTest(); echo " Test variables outside the function: "; echo "Variable x is: $x"; echo " "; echo "Variable y is: $y"; ?>

17 PHP The global Keyword

18

19 PHP The static Keyword Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

20 "; myTest(); echo " "; myTest(); echo " "; myTest(); echo " "; myTest(); ?>

21 PHP 5 echo and print Statements In PHP there is two basic ways to get output: echo and print. There are some difference between echo and print: echo - can output one or more strings print - can only output one string, and returns always 1

22 The PHP echo Statement Display Strings PHP is fun! "; echo "Hello world! "; echo "I'm about to learn PHP! "; echo "This", " string", " was", " made", " with multiple parameters."; ?>

23 Display Variables "; echo "Study PHP at $txt2"; echo " "; echo "My car is a {$cars[0]}"; ?>

24 The PHP print Statement Display Strings PHP is fun! "; print "Hello world! "; print "I'm about to learn PHP!"; ?>

25 Display Variables "; print "Study PHP at $txt2"; print " "; print "My car is a {$cars[0]}"; ?>

26 PHP Data Types String, Integer, Floating point numbers, Boolean, Array, Object, NULL.

27 PHP Strings "; $x = 'Hello world!'; echo $x; ?>

28 PHP Integers An integer is a number without decimals. Rules for integers: An integer must have at least one digit (0-9) An integer cannot contain comma or blanks An integer must not have a decimal point An integer can be either positive or negative Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)

29 "; $x = -345; // negative number var_dump($x); echo " "; $x = 0x8C; // hexadecimal number var_dump($x); echo " "; $x = 047; // octal number var_dump($x); ?>

30 PHP Floating Point Numbers "; $x = 2.4e3; var_dump($x); echo " "; $x = 8E-5; var_dump($x); ?>

31 PHP Booleans Booleans can be either TRUE or FALSE. var x=true; var y=false; Booleans are often used in conditional testing.

32 PHP Arrays

33 PHP Objects An object is a data type which stores data and information on how to process that data. In PHP, an object must be explicitly declared. First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods. We then define the data type in the object class, and then we use the data type in instances of that class:

34 color = $color; } function what_color() { return $this->color; } } function print_vars($obj) { foreach (get_object_vars($obj) as $prop => $val) { echo "\t$prop = $val\n"; } } // instantiate one object $herbie = new Car("white"); // show herbie properties echo "\herbie: Properties\n"; print_vars($herbie); ?>

35 PHP NULL Value

36 PHP String Functions The PHP strlen() function

37 The PHP strpos() function

38 For a complete reference of all string functions go to http://www.w3schools.com/php/php_ref_stri ng.asp http://www.w3schools.com/php/php_ref_stri ng.asp

39 PHP Constants A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.

40 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. Default is false.

41 "; // will not output the value of the constant echo greeting; ?>

42 "; // will also output the value of the constant echo greeting; ?>

43 PHP Operators OperatorNameExampleResult +Addition$x + $ySum of $x and $y -Subtraction$x - $yDifference of $x and $y *Multiplication$x * $yProduct of $x and $y /Division$x / $yQuotient of $x and $y %Modulus$x % $yRemainder of $x divided by $y PHP Arithmetic Operators

44 AssignmentSame as...Description x = y The left operand gets set to the value of the expression on the right x += yx = x + yAddition x -= yx = x - ySubtraction x *= yx = x * yMultiplication x /= yx = x / yDivision x %= yx = x % yModulus PHP Assignment Operators The PHP assignment operators is used to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

45 OperatorNameExampleResult.Concatenation$txt1 = "Hello" $txt2 = $txt1. " world!" Now $txt2 contains "Hello world!".=Concatenation assignment $txt1 = "Hello" $txt1.= " world!" Now $txt1 contains "Hello world!" PHP String Operators The example below shows the results of using the string operators:

46 OperatorNameDescription ++$xPre- increment Increments $x by one, then returns $x $x++Post- increment Returns $x, then increments $x by one --$xPre- decrement Decrements $x by one, then returns $x $x--Post- decrement Returns $x, then decrements $x by one PHP Increment / Decrement Operators

47

48 OperatorNameExampleResult ==Equal$x == $yTrue if $x is equal to $y ===Identical$x === $yTrue if $x is equal to $y, and they are of the same type !=Not equal$x != $yTrue if $x is not equal to $y <>Not equal$x <> $yTrue if $x is not equal to $y !==Not identical$x !== $yTrue if $x is not equal to $y, or they are not of the same type >Greater than$x > $yTrue if $x is greater than $y <Less than$x < $yTrue if $x is less than $y >=Greater than or equal to $x >= $yTrue if $x is greater than or equal to $y <=Less than or equal to$x <= $yTrue if $x is less than or equal to $y PHP Comparison Operators The PHP comparison operators are used to compare two values (number or string):

49 "; var_dump($x === $y); // returns false because types are not equal echo " "; var_dump($x != $y); // returns false because values are equal echo " "; var_dump($x !== $y); // returns true because types are not equal echo " "; $a=50; $b=90; var_dump($a > $b); echo " "; var_dump($a

50 OperatorNameExampleResult andAnd$x and $yTrue if both $x and $y are true orOr$x or $yTrue if either $x or $y is true xorXor$x xor $yTrue if either $x or $y is true, but not both &&And$x && $yTrue if both $x and $y are true ||Or$x || $yTrue if either $x or $y is true !Not!$xTrue if $x is not true PHP Logical Operators

51 OperatorNameExampleResult +Union$x + $yUnion of $x and $y (but duplicate keys are not overwritten) ==Equality$x == $yTrue if $x and $y have the same key/value pairs ===Identity$x === $yTrue if $x and $y have the same key/value pairs in the same order and of the same types !=Inequality$x != $yTrue if $x is not equal to $y <>Inequality$x <> $yTrue if $x is not equal to $y !==Non-identity$x !== $yTrue if $x is not identical to $y PHP Array Operators The PHP array operators are used to compare arrays:

52 "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; // union of $x and $y var_dump($z); echo " "; var_dump($x == $y); echo " "; var_dump($x === $y); echo " "; var_dump($x != $y); echo " "; var_dump($x <> $y); echo " "; var_dump($x !== $y); ?>

53 array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" } bool(false) bool(false) bool(true) bool(true) bool(true)

54 PHP Conditional Statements PHP - The if Statement The if statement is used to execute some code only if a specified condition is true. Syntax if (condition) { code to be executed if condition is true; }

55

56 PHP - The if...else Statement Use the if....else statement to execute some code if a condition is true and another code if the condition is false. Syntax if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }

57

58 PHP - The if...elseif....else Statement Use the if....elseif...else statement to select one of several blocks of code to be executed. Syntax if (condition) { code to be executed if condition is true; } elseif (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }

59

60 The PHP switch Statement Use the switch statement to select one of many blocks of code to be executed. Syntax switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break;... default: code to be executed if n is different from all labels; }

61

62 PHP while Loops The PHP while Loop The while loop executes a block of code as long as the specified condition is true. Syntax while (condition is true) { code to be executed; }

63 "; $x++; } ?>

64 The PHP do...while Loop "; $x++; } while ($x

65 The PHP for Loop The for loop is used when you know in advance how many times the script should run. Syntax for (init counter; test counter; increment counter) { code to be executed; }

66 "; } ?>

67 The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax foreach ($array as $value) { code to be executed; } For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

68 "; } ?>

69 PHP Functions PHP User Defined Functions Syntax function functionName() { code to be executed; } A function name can start with a letter or underscore (not a number)

70

71 PHP Function Arguments "; } familyName("Jani"); familyName("Hege"); familyName("Stale"); familyName("Kai Jim"); familyName("Borge"); ?>

72 "; } familyName("Hege","1975"); familyName("Ståle","1978"); familyName("Kai Jim","1983"); ?>

73 PHP Default Argument Value "; } setHeight(350); setHeight(); setHeight(135); setHeight(80); ?>

74 PHP Functions - Returning values "; echo "7 + 13 = ". sum(7,13). " "; echo "2 + 4 = ". sum(2,4); ?>


Download ppt "Php. 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."

Similar presentations


Ads by Google