mcis ua
play

MCIS/UA PHP Training 2003 Chapter 2 Language Basics PHP Basics - PowerPoint PPT Presentation

MCIS/UA PHP Training 2003 Chapter 2 Language Basics PHP Basics PHP applications should have a .php extension. All statements end with a semicolon PHP Basics Commands are case insensitive (but are usually typed in lowercase) print


  1. MCIS/UA PHP Training 2003 Chapter 2 Language Basics

  2. PHP Basics • PHP applications should have a .php extension. • All statements end with a semicolon

  3. PHP Basics • Commands are case insensitive (but are usually typed in lowercase) print “testing”; PRINT “testing”; Print “testing”; • Variables are case sensitive $_SERVER[”HTTP_USER_AGENT”] $_server[”HTTP_USER_AGENT”] $_Server[”HTTP_USER_AGENT”]

  4. PHP Basics • Whitespace and Line Breaks are ignored (for the most part) <?php print “testing”; ?> <?php print “testing”; ?> <?php print “test ing”; ?>

  5. Comments • 3 styles of comments • Shell-style • C++ style • C style

  6. Comments • Shell-style • start with pound sign (#) • continue through end of line or end of PHP section <?php # This is a comment. print “testing”; # another comment # 3rd comment ?> <b>bold text</b>

  7. Comments • C++ style • start with 2 slashes (//) • continue through end of line or end of PHP section <?php // This is a comment. print “testing”; // another comment // 3rd comment ?> <b>bold text</b>

  8. Comments • C style • start with /* and end with */ <?php /* This is a comment. */ print “testing”; /* another comment */ /* 3rd comment ?> <b>bold text</b> */ /* comment1 /* embedded comment */ comment2 */ • useful for disabling a section of code

  9. Comments • HTML comments? <html> <head> <title>my title</title> </head> <body> <p>before code</p> <!-- <?php print "<p>in php code</p>"; ?> --> <p>after code</p> </body> </html>

  10. Comments <html> <head> <title>my title</title> </head> <body> <p>before code</p> <!-- <p>in php code</p> --> <p>after code</p> </body> </html>

  11. Literals Integers 2003, -5, +345 Floats 3.14, -6.789 Scientific 17.0E-3 Hexadecimal 0x3f, 0x3AB4C, 0Xff, 0XFF Single-quoted strings 'This is a string.\n' Double-quoted strings "This is a string.\n" Booleans true, false Null null, NULL

  12. Single-quoted String Escape Sequences \’ - single quote \\ - backslash $x = 29; print 'Kent\'s age: \n $x'; Kent's age: \n $x

  13. Double-quoted String Escape Sequences \n - newline \” - double quotes \’ - single quote \\ - backslash \$ - dollar sign \t - tab \r - carriage return \{ \} - braces \[ \] - brackets \0 thru \777 - ASCII character represented by octal value \x0 thru \xFF - ASCII character represented by hex value

  14. Double-quoted String Escape Sequences $x = 29; print 'Kent\'s age: \n $x'; Kent's age: \n $x print "Kent\'s age: \n $x"; Kent's age: 29

  15. Variables • Start with $, followed by a letter or underscore, followed by any combination of letters, numbers, or underscores • case sensitive • don’t need to be declared • weakly-typed • can store booleans, integers, floating- points, strings, arrays, objects, references, or resources.

  16. Variables $name $name1 $name_1 $2nd_address Invalid - variable names must start with a letter or underscore $NAME1 $first_name $firstName

  17. Variables $x = 5; $x = 3.14; $x = “305 Hoyt Hall”; $x = 5 > 3; $x = true; $x = array(3, 5, 10); $x = new person();

  18. Arrays (Indexed) $name = array(”Kent Covert”, “Tim Kingman”, “John Moose”); $name[0] = “Kent Covert”; $name[1] = “Tim Kingman”; $name[2] = “John Moose”; print $name[1]; Tim Kingman print $name[3];

  19. Arrays (Indexed) $name[0] = “Kent Covert”; $name[1] = 5; $name[2] = false; print $name[1]; 5

  20. Arrays (Associative or Hashes) $name[’covertka’] = “Kent Covert”; $name[’kingmatm’] = “Tim Kingman”; $name[’moosejc’] = “John Moose”; $name = array( ‘covertka’ => “Kent Covert”, ‘kingmatm’ => “Tim Kingman”, ‘moosejc’ => “John Moose”); print $name[’kingmatm’]; Tim Kingman print $name[’tepeds’];

  21. Arrays $name[’covertka’] = “Kent Covert”; $name[’kingmatm’] = “Tim Kingman”; $name[’moosejc’] = “John Moose”; $name[0] = "Dirk Tepe"; print $name[’kingmatm’]; Tim Kingman print $name[0]; Dirk Tepe print $name['0']; Dirk Tepe

  22. Multi-dimensional Arrays $name[0][0] = “Kent Covert”; $name[0][1] = “Tim Kingman”; $name[0][2] = “John Moose”; print $name[0][1]; Tim Kingman print $name[0][3];

  23. Multi-dimensional Arrays $person[’covertka’][’name’] = “Kent Covert”; $person[’covertka’][’dept’] = “MCIS”; $person[’covertka’][’age’] = 29; $person[’moosejc’][’name’] = “John Moose”; print $name[’covertka’][’age’]; 29

  24. Mixed Arrays $person[’covertka’][’dept’][0] = “MCIS”; $person[’covertka’][’dept’][1] = “Music”; print $name[’covertka’][’dept’][1]; Music

  25. Multi-dimensional Arrays??? $name[0][0] = “Kent Covert”; $name[0][1] = “Tim Kingman”; $name[0][2] = “John Moose”; print $name[0][1]; Tim Kingman print $name[0]; Array

  26. Arrays $name[0][0] = “Kent Covert”; $name[0][1] = “Tim Kingman”; $name[0][2] = “John Moose”; $name[0] = array("Kent Covert", "Tim Kingman", "John Moose"); $name[1] = 5; print $name[0][0]; Kent Covert print $name[1]; 5

  27. Arrays $person['covertka']['name'] = "Kent Covert"; $person[’covertka’][’dept’][0] = “MCIS”; $person[’covertka’][’dept’][1] = “Music”; print $person[’covertka’][’dept’][1]; Music print $person['covertka']['name']; Kent Covert

  28. Questions?

  29. Homework #2 Create 2 files: 1) The first file is a webpage that contains a form that prompts the user for name (text field), password (password text field), sex (radio button), and favorite color (popup with red, blue, green, or yellow as choices). The action URL should refer to the second file. 2) The second file should be a php program that displays the data entered in the form (even the password). Form data is stored in the $_REQUEST array. Try out my solution at: http://webdev.admin.muohio.edu/phpapps/covertka/hw2/

  30. Arithmetic Operators + - addition - - subtraction * - multiplication / - division % - modulus 10 % 6 � 4

  31. Arithmetic Operators ++ - increment by 1 -- - decrement by 1 $x = 5; print $x; � 5 $x++; � 6 print $x; $x = 5; print $x++; � 5 � 6 print $x; $x = 5; print ++$x; � 6 print $x; � 6

  32. Comp. Operators == - equals numerically/lexically === - equals numerically/lexically and same type 3 == “3” � true 3 === “3” � false “0” == “0.0” � true “0” === “0.0” � false if (strpos($s, $sub) === false) ... != or <> - not equals !== - not identical

  33. Comparison Operators > - greater than 5 > 3 � true 3 > 5 � false “def” > “abc” � true “abc” > “def” � false < - less than >= - greater than or equal to <= - less than or equal to

  34. Assignment Operators = - simple assignment $x = 5; $x = $y = 5; if ($x = 5) ... if ($fp = fopen("test.txt", "r")) ...

  35. Assignment Operators += - addition assignment $x += 5; $x = $x + 5;

  36. Assignment Operators += - addition assignment -= - subtraction assignment *= - multiplication assignment /= - division assignment %= - modulus assignment

  37. Logical Operators &&, and - logical AND if ($x > 5 && $x < 10) ... if ($x > 5 and $x < 10) ... ||, or - logical OR xor - logical XOR (true if either, but not both) ! - logical NOT if (!($x > 5)) ...

  38. Logical Operators if ($x > 0 && $y/$x < 10) ... $result = fopen($filename) or exit();

  39. String Operators . - string concatentation $firstName = “Kent”; $lastName = “Covert”; $fullName = $firstName . “ “ . $lastName; print $fullName; .= - concationation assignment $fullName = “Kent”; $fullName .= “ “; $fullName .= “Covert”; print $fullName;

  40. Bitwise Operators ~ - negation & - bitwise AND | - bitwise OR ^ - bitwise XOR << - shift left >> - shift right &= - bitwise AND assignment |= - bitwise OR assignment ^= - bitwise XOR assignment

  41. Casting Operators • Casting operators are used to convert from one data type to another print 3.14; 3.14 print (int) 3.14; 3 print 101/4; 25.25 print (int) (101/4); 25

  42. Casting Operators integers (int) or (integer) floating points (float) or (real) strings (string) booleans (bool) or (boolean) arrays (array) objects (object)

  43. Misc. Operators @ - error suppression $x = 2 / 0; $x = @(2 / 0);

  44. Misc. Operators ` - (backtick) - execute code $listing = `ls -l /tmp`;

  45. Misc. Operators ?: - conditional (ternary operator) ( condition ? true-expr : false-expr ) $x = ($y != 0 ? 10/$y : -1); if ($y != 0) { $x = 10/$y; } else { $x = -1; }

  46. Questions?

Recommend


More recommend