Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Basics 1 ICS213, 1 / 2011 Dr. Seung Hwan Kang 1.

Similar presentations


Presentation on theme: "PHP Basics 1 ICS213, 1 / 2011 Dr. Seung Hwan Kang 1."— Presentation transcript:

1 PHP Basics 1 ICS213, 1 / 2011 Dr. Seung Hwan Kang 1

2 Outline Introduction to PHP Comments Variables Type Type Casting Constants Operators Control Structures Misc. Functions 2

3 What is PHP? PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Its syntax draws upon C, Java, and Perl, and is easy to learn. The main goal of the language is to allow web developers to write dynamically generated web pages quickly, but you can do much more with PHP. 3

4 There are three main areas where PHP scripts are used. Server-side scripting. Command line scripting. Writing desktop applications. The following databases are currently supported: IBM DB2, Informix, MySQL, ODBC, Oracle, PostgresSQL, SQLite, Sybase, etc., PHP Data Objects (PDO) LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM, SAX, XML, XSLT, etc., 4 What can PHP do?

5 Getting Started PHP is installed on the Apache HTTP Server (httpd) All files ending in.php are handled by PHP. e.g. index.php, helloworld.php Server parses files based on extensions Returns plain HTML, no code Think of these PHP-enabled files as simple HTML files with a whole new family of magical tags that let you do all sorts of things. 5

6 Open and close tags: <?php echo "Hello World!"; ?> 6 Hello World

7 Hello World (cont’d) Example <?php echo "Hello World!"; ?> 7 helloworld.php

8 <?php // $expression = TRUE; if ($expression) { ?> This is true. <?php } else { ?> This is false. <?php } ?> 8 escape.php Escaping from HTML

9 Comment <?php // one-line comment /* * multi line comment */ echo "Hello World!"; ?> 9

10  In PHP, you create a variable with a dollar sign ($) and some text.  Usually the text will be something descriptive of what it is going to hold. <?php $name = " Web Programming " ; $code = ' ICS213 ' ; $credit = 3; $semester = " 1 / 2011 " ; ?> 10 Variable

11 Output one or more strings <?php echo ("Hello World"); echo "Hello World"; echo "This spans\nmultiple lines. The newlines will be\ noutput as well."; echo "Escaping characters is done \"Like this\"."; ?> 11 Echo, Print

12 Echo, Print (cont’d) <?php $foo = "foobar"; $bar = "barbaz"; echo "foo is $foo"; // foo is foobar echo 'foo is $foo'; // foo is $foo echo $foo; // foobar echo $foo,$bar; // foobarbarbaz echo "foo is ". $foo; // foo is foobar // preferred ?> 12 echo.php

13 13 Reserved Word

14 Booleans Integers Floating point numbers Strings Arrays Objects Resources NULL 14 Types

15  It can be either TRUE or FALSE <?php $foo = TRUE; ?> 15 booleans.php Boolean

16  An integer is a number of the set Z = {..., -2, -1, 0, 1, 2,...} <?php $int1 = 1234; // decimal number $int2 = -123; // a negative number $int3 = 0123; // octal number $int4 = 0x1A; // hexadecimal number ?> 16 Integer

17  also known as "floats", "doubles", or "real numbers" 17 Float pointing numbers floats.php

18  A string is series of characters <?php echo 'this is a simple string'; // Outputs: "I'll be back" echo '"I\'ll be back"'; // Outputs: Variables do not $expand $either echo 'Variables do not $expand $either'; ?> 18 String

19 . String catenation is indicated with a period (.) <?php $str1 = 'Hello'; // Outputs: Hello World PHP! echo "$str1 World". " PHP!"; ?> 19 string.php

20  Simple array <?php $arr = array(1,2,3,4,5); echo $arr[3]; ?> 20 // 4 array1.php Array

21 Array (cont’d)  array( key => value,... ) // key may only be an integer or string // value may be any value of any type <?php $arr = array('foo' => 'bar', 12 => true); echo $arr['foo']; // bar echo $arr[12]; // 1 ?> 21 array2.php

22 Array (cont’d) <?php // This array is the same as... $arr1 = array(5 => 43, 32, 56, 'b' => 12); //...this array $arr2 = array(5 => 43, 6 => 32, 7 => 56, 'b' => 12); ?> 22 array3.php

23  To create a new object, use the new statement to instantiate a class: <?php class Robot { public $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function __toString() { return $this->name; } $bender1 = new Robot(); $bender1->setName("Bender 1"); echo $bender1->__toString(); ?> 23 object.php Object

24 The special NULL value represents a variable with no value. <?php $var = NULL; ?> 24 null.php NULL

25 (int), (integer) - cast to integer (bool), (boolean) - cast to boolean (float), (double), (real) - cast to float (string) - cast to string (binary) - cast to binary string (PHP 6) (array) - cast to array (object) - cast to object (unset) - cast to NULL (PHP 5) 25 Type Casting

26 <?php $foo = 10; // $foo is an integer $str = "$foo"; // $str is a string $fst = (string) $foo; // $fst is also a string // This prints out that "they are the same" if ($fst === $str) { echo "they are the same"; } ?> 26 casting.php

27 Constant  A constant is case-sensitive by default <?php // a valid constant name define('HOST', 'localhost'); echo HOST; // localhost ?> 27 config.php

28 Arithmetic Operators Assignment Operator Comparison Operators Incrementing/Decrementing Operators Logical Operators String Operators Type Operators 28 Operators

29 PHP supports the usual operators supported by the C/C++/Java family 29 Arithmetic Operators

30 The assignment operator (=) used in C/C++/Java are supported in PHP 30 Assignment Operator

31 ExampleNameResult $a == $bEqualTRUE if $a is equal to $b. $a === $bIdentical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4) $a != $bNot equalTRUE if $a is not equal to $b. $a <> $bNot equalTRUE if $a is not equal to $b. $a !== $bNot identical TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4) $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 >= $b Greater than or equal to TRUE if $a is greater than or equal to $b. 31 Comparison Operators

32 Incrementing/Decrementing Operators ExampleNameEffect ++$aPre-incrementIncrements $a by one, then returns $a. $a++Post-incrementReturns $a, then increments $a by one. --$aPre-decrementDecrements $a by one, then returns $a. $a--Post-decrementReturns $a, then decrements $a by one. 32

33 ExampleNameResult $a and $bAndTRUE if both $a and $b are TRUE. $a or $bOrTRUE if either $a or $b is TRUE. $a xor $bXorTRUE if either $a or $b is TRUE, but not both. ! $aNotTRUE if $a is not TRUE. $a && $bAndTRUE if both $a and $b are TRUE. $a || $bOrTRUE if either $a or $b is TRUE. 33 Logical Operators

34 ExampleNameResult $a = "Hello "; $b = $a. "World!"; concatenation operator“Hello World!” $a = "Hello "; $a.= "World!"; concatenating assignment operator “Hello World!” 34 String Operators

35 Type Operators instanceof is used to determine whether a PHP variable is an instantiated object of a certain class: <?php class ParentClass { } class MyClass extends ParentClass { } $a = new MyClass; var_dump($a instanceof MyClass); // bool (true) var_dump($a instanceof ParentClass); // bool (true) ?> 35 type_op.php

36 If, else, else if while do-while for foreach break continue switch require include 36 Control Structures

37 <?php $a = 8; $b = 8; if ($a > $b) { echo "a is bigger than b"; } else { echo "a is NOT bigger than b"; } ?> 37 if

38  while (expr) statement <?php $i = 1; while ($i < 10){ echo $i++; } ?> 38 while.php while

39  while loops is guaranteed to run the first iteration of a do-while loop <?php $i = 0; do { echo $i; } while ($i > 0); ?> 39 dowhile.php do-while

40  for (expr1; expr2; expr3) statement <?php for ($i = 1; $i <= 10; $i++) { echo $i; } ?> 40 for.php for

41 foreach  foreach (array_expression as $value) statement it loops over the array given by array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element)  foreach (array_expression as $key => $value) statement The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop. 41

42 foreach (cont’d)  foreach (array_expression as $value) statement <?php $arr = array('one', 'two', 'three'); foreach ($arr as $value) { echo "Value: $value \n"; } ?> 42 foreach1.php

43 foreach (cont’d)  foreach (array_expression as $key => $value) statement <?php $arr = array('one', 'two', 'three'); foreach ($arr as $key => $value) { echo "Value: $value \n"; } ?> 43 foreach2.php

44  break ends execution of the current for, foreach, while, do-while or switch structure. <?php $arr = array('one', 'two', 'stop', 'three'); while (list(, $val) = each($arr)) { if ($val == 'stop') { break; } echo "$val \n"; } ?> 44 break1.php break

45 break (cont’d) <?php $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5 \n"; break 1; // Exit only the switch. case 10: echo "At 10; quitting \n"; break 2; // Exit the switch and // the while. default: break; } ?> 45 break2.php

46 continue <?php /* The continue keyword can skip division by zero: */ $i = 5; while ($i > -2) { $i--; if ($i == 0) { continue; } echo $i. " "; } ?> 46 continue.php

47 switch <?php $i = 'orange'; switch ($i) { case 'apple': echo "i is apple"; break; case 'orange': echo "i is orange"; break; case 'cake': echo "i is cake"; break; } ?> 47 switch.php

48  include() statement includes and evaluates the specified file. When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward  include_once() // hi.php <?php echo "Hi, I'm a PHP script!"; ?> // world.php <?php include 'hi.php'; ?> 48 hi.php, world.php include

49 require() is identical to include() except upon failure it will also produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue. require_once() 49 hi.php, world2.php require

50 HTML 5 & CSS 3 50 template.php nav.php section.php footer.php header.php article.php <?php // template.php include_once 'header.php'; … include_once 'footer.php'; ?>

51 Misc. Functions constantReturns the value of a constant defineDefines a named constant. See Constant dieEquivalent to exit() exitOutput a message and terminate the current script highlight_fileSyntax highlighting of a file highlight_stringSyntax highlighting of a string 51 config,php, highlight_string.php Try config.phps

52 Reference PHP Manual, http://www.php.net/manual/en/index.php


Download ppt "PHP Basics 1 ICS213, 1 / 2011 Dr. Seung Hwan Kang 1."

Similar presentations


Ads by Google