===== CS 328 - recorded for Week 9 Lecture 1 - recorded for 2024-03-18 ===== ===== TODAY WE WILL ===== * announcements * APPLICATION TIER - start intro to PHP * PHP basics! * prep for next class ===== * Should be working on Homework 7 - 1st attempts due by 11:59 pm on Sunday, March 24 * Remember there will also be a recorded for Wednesday, March 20, and an individual-or-pair lab exercise due by 11:59 pm on Friday, March 22 * I will also send a class e-mail when the zyBooks PHP chapter is available! ===== INTRO to PHP ===== * Goal: a useful subset of PHP so you can finish writing a full n-tier web app that uses a database! * now, PHP stands for "PHP Hypertext Processor" * yes, that's a recursive acronym... * PHP can be embedded within HTML * Much of its syntax is borrowed/based/inspired by C, Java, and Perl plus some sort-of-unique-to-PHP features; PHP's goal: to allow web developers to write dynamically-generated web pages. * IT IS EXECUTED ON THE APPLICATION TIER!!!!!!! (not on the data tier! not on the client tier!) * PHP document has the suffix .php * client tier requests a document with the suffix .php, the web server passes that request (and any associated additional information TO the PHP Preprocessor running also on the application tier; the PHP Preprocessor executes the PHP in the named document, and sends the document that results back to the web server, and the web server sends it to the client, to the client's browser, and the browser displays the result. * THE PHP NEVER GOES to THE CLIENT TIER!!!! ...just its executed result is sent!! ===== some PHP basics! ===== * PHP is often embedded within HTML documents (but you give any file containing PHP that suffix .php so it will be handed over to the PHP Preprocessor to execute!!) * OK, there are several styles of PHP tags, here are the TWO that are allowed in CS 328: * the classic, regular, usual PHP tag: <?php ... ?> * contains PHP statements * these statements are executed by the PHP Preprocessor * PHP expression tag: <?= ... ?> * contains a SINGLE PHP *expression* (a small bit that has a value, something that could be on the right-hand-side of an assignment statement, for example!) * this expression will be evaluated by the PHP Preprocessor and its value will replace this expression tag in the resulting document ===== CS 328 CLASS STYLE: ===== * we will end PHP statements with a SEMICOLON we will *NOT* end PHP *expressions* with a semicolon! * Stepp et al style (Web Programming Step-by-Step) recommended avoiding print and echo statements, instead interleaving HTML that is always the same (boilerplate) with PHP that determines which parts are displayed * that will be CS 328 required style: avoiding use of print/echo statements in PHP ===== PHP comments ===== * three kinds! /* multi-line comment! */ // single line comment! # single line comment! * THESE NEEEEEEED to be within PHP tags!!!! (or they will NOT be treated as PHP comments!!!!!) ===== PHP basic types ===== * BUT note: PHP is a scripting language, it is NOT strongly-typed! * four scalar data types: integer, float, string, boolean int integer - what you expect 0 42 -4 float also called double - the same type in PHP (?!) - real numbers with fractional parts 4.5 -0.33 bool boolean - true false <-- but not case sensitive * many values are treated as true or false if they appear in a boolean context * for example, these are all considered to be false: false 0 0.0 -0.0 "" "0" null ...and a few others; string - write a string literal in single OR double quotes 'moo' "moo" array - indexed array - indexed by integers associative array - indexed by other-type keys null - also a type! that has one possible value: null ...and several others...! * three fun facts: * the function var_dump(desired_variable) outputs the data type of the given desired_variable * a set of functions is_blah (is_int, is_float, several others) expect one argument and returns a bool value based on whether its argument is of that type * gettype expects one argument and returns a string whose contents are the name of the argument's type ===== TWO ways to "execute" PHP ===== * classic way: embed it in a document with the suffix .php and request that document from a browser (so that the web server asks the PHP Preprocessor to execute the PHP in that document and send back the result) * also has as Command Line Interface (CLI) - can run PHP from the command line on the application tier, using the php command and the desired .php file That is, you can run a php command from an nrs-projects terminal/bash shell. php desired_file.php * SURROUND the PHP statements and/or expressions in ANY .php file with the appropriate PHP tag! * even if it contains JUST a PHP function definition, for example! ===== PHP variables ===== * naming: STARTS with a $, followed by at least one character * just set them and use them! * assignment operator: = $welcome = "Hello!"; // now variable $welcome has the value "Hello!" $welcome = 3; // now variable $welcome has the value 3 // (yes, you can assign values of any type // to a variable...!) ===== a few notes about PHP compound expressions ===== * function calls are like you expect: funct_name(arg, arg, ... arg) * + - * / ...are the usual addition, subtraction, multiplication, division; * looks like has ++ -- += *= -= /= * > >= < <= != -- these comparators work like you expect BUT! PHP has both == and === * == is true if its lhs and rhs are of equivalent values (even if they are not the same type) "equal regardless of type" * === is true if its lhs and are equivalent values AND the same type * also has: !== (not equal to OR not the same type) * logical and and or: && and || or * BUT the operator precedence of and or are DIFFERENT than && || * zyBook: makes it sound like && and || are preferred; ===== our first PHP function ===== * they are USEFUL for writing debuggable and maintainable PHP documents! * syntax: function desired_funct(desired_param_name, desired_param2, ...) { // function code // put a return statement if the function returns something } * return statement is optional (but put it IF your function is to return something) ===== CS 328 CLASS STYLE: ===== * PHP functions to be called/used in the body of a document should be declared in the head element of that document (if they are defined in another file, you should include them in the head element -- more on that soon) ===== server-side includes ===== * YES you can grab stuff from another file and include it within your PHP document; that's called a "server-side include" * FOUR functions that PHP provides for this -- require require_once include include_once * the modern PHP difference: include, include_once throws a WARNING if the file to be included is not found require, require_once throw a fatal ERROR if the file to be included is not found * *_once : will include that file at most once even if this is called more than once in the document * what can you include? FOR example: * BIIIG chunks of HTML boilerplate code, or boilerplate just be being used in multiple pages * functions you are using in more than one document * see how require_once is used to bring in square's function definition from square.php and 328footer.html's footer element in 328lect09-1-play2.php