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

mcis ua
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

PHP Training 2003 Chapter 2 Language Basics

MCIS/UA

slide-2
SLIDE 2

PHP Basics

  • PHP applications should have a .php

extension.

  • All statements end with a semicolon
slide-3
SLIDE 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”]

slide-4
SLIDE 4

PHP Basics

  • Whitespace and Line Breaks are ignored

(for the most part)

<?php print “testing”; ?> <?php print “testing”; ?> <?php print “test ing”; ?>

slide-5
SLIDE 5

Comments

  • 3 styles of comments
  • Shell-style
  • C++ style
  • C style
slide-6
SLIDE 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>

slide-7
SLIDE 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>

slide-8
SLIDE 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
slide-9
SLIDE 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>

slide-10
SLIDE 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>

slide-11
SLIDE 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

slide-12
SLIDE 12

Single-quoted String Escape Sequences

\’ - single quote \\ - backslash $x = 29; print 'Kent\'s age: \n $x'; Kent's age: \n $x

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 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,

  • r resources.
slide-16
SLIDE 16

Variables

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

slide-17
SLIDE 17

Variables

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

slide-18
SLIDE 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];

slide-19
SLIDE 19

Arrays (Indexed)

$name[0] = “Kent Covert”; $name[1] = 5; $name[2] = false; print $name[1]; 5

slide-20
SLIDE 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’];

slide-21
SLIDE 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

slide-22
SLIDE 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];

slide-23
SLIDE 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

slide-24
SLIDE 24

Mixed Arrays

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

slide-25
SLIDE 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

slide-26
SLIDE 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

slide-27
SLIDE 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

slide-28
SLIDE 28

Questions?

slide-29
SLIDE 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/

slide-30
SLIDE 30

Arithmetic Operators

+ - addition

  • - subtraction

* - multiplication / - division % - modulus 10 % 6 4

slide-31
SLIDE 31

Arithmetic Operators

++ - increment by 1

  • - - decrement by 1

$x = 5; print $x; $x++; print $x; 5 6 $x = 5; print $x++; print $x; 5 6 $x = 5; print ++$x; print $x; 6 6

slide-32
SLIDE 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

slide-33
SLIDE 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

slide-34
SLIDE 34

Assignment Operators

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

slide-35
SLIDE 35

Assignment Operators

+= - addition assignment $x += 5; $x = $x + 5;

slide-36
SLIDE 36

Assignment Operators

+= - addition assignment

  • = - subtraction assignment

*= - multiplication assignment /= - division assignment %= - modulus assignment

slide-37
SLIDE 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)) ...

slide-38
SLIDE 38

Logical Operators

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

slide-39
SLIDE 39

String Operators

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

slide-40
SLIDE 40

Bitwise Operators

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

slide-41
SLIDE 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

slide-42
SLIDE 42

Casting Operators

(int) or (integer)

integers

(float) or (real)

floating points

(string)

strings

(bool) or (boolean)

booleans

(array)

arrays

(object)

  • bjects
slide-43
SLIDE 43
  • Misc. Operators

@ - error suppression $x = 2 / 0; $x = @(2 / 0);

slide-44
SLIDE 44
  • Misc. Operators

` - (backtick) - execute code $listing = `ls -l /tmp`;

slide-45
SLIDE 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; }

slide-46
SLIDE 46

Questions?