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 “testing”; PRINT “testing”; Print “testing”; • Variables are case sensitive $_SERVER[”HTTP_USER_AGENT”] $_server[”HTTP_USER_AGENT”] $_Server[”HTTP_USER_AGENT”]
PHP Basics • Whitespace and Line Breaks are ignored (for the most part) <?php print “testing”; ?> <?php print “testing”; ?> <?php print “test ing”; ?>
Comments • 3 styles of comments • Shell-style • C++ style • C style
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>
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>
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
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>
Comments <html> <head> <title>my title</title> </head> <body> <p>before code</p> <!-- <p>in php code</p> --> <p>after code</p> </body> </html>
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
Single-quoted String Escape Sequences \’ - single quote \\ - backslash $x = 29; print 'Kent\'s age: \n $x'; Kent's age: \n $x
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
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
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.
Variables $name $name1 $name_1 $2nd_address Invalid - variable names must start with a letter or underscore $NAME1 $first_name $firstName
Variables $x = 5; $x = 3.14; $x = “305 Hoyt Hall”; $x = 5 > 3; $x = true; $x = array(3, 5, 10); $x = new person();
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];
Arrays (Indexed) $name[0] = “Kent Covert”; $name[1] = 5; $name[2] = false; print $name[1]; 5
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’];
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
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];
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
Mixed Arrays $person[’covertka’][’dept’][0] = “MCIS”; $person[’covertka’][’dept’][1] = “Music”; print $name[’covertka’][’dept’][1]; Music
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
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
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
Questions?
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/
Arithmetic Operators + - addition - - subtraction * - multiplication / - division % - modulus 10 % 6 � 4
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
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
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
Assignment Operators = - simple assignment $x = 5; $x = $y = 5; if ($x = 5) ... if ($fp = fopen("test.txt", "r")) ...
Assignment Operators += - addition assignment $x += 5; $x = $x + 5;
Assignment Operators += - addition assignment -= - subtraction assignment *= - multiplication assignment /= - division assignment %= - modulus assignment
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)) ...
Logical Operators if ($x > 0 && $y/$x < 10) ... $result = fopen($filename) or exit();
String Operators . - string concatentation $firstName = “Kent”; $lastName = “Covert”; $fullName = $firstName . “ “ . $lastName; print $fullName; .= - concationation assignment $fullName = “Kent”; $fullName .= “ “; $fullName .= “Covert”; print $fullName;
Bitwise Operators ~ - negation & - bitwise AND | - bitwise OR ^ - bitwise XOR << - shift left >> - shift right &= - bitwise AND assignment |= - bitwise OR assignment ^= - bitwise XOR assignment
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
Casting Operators integers (int) or (integer) floating points (float) or (real) strings (string) booleans (bool) or (boolean) arrays (array) objects (object)
Misc. Operators @ - error suppression $x = 2 / 0; $x = @(2 / 0);
Misc. Operators ` - (backtick) - execute code $listing = `ls -l /tmp`;
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; }
Questions?
Recommend
More recommend