=====
CS 328 - Week 9 Lecture 1 - 2026-03-23
=====
=====
TODAY WE WILL
=====
* announcements
* finally reaching the APPLICATION tier --
start intro to PHP
* intro to PHP basics
* prep for next class
=====
* should be working on Homework 7!
* deadline: 11:59 pm on Friday, March 27
* submit files EARLY and OFTEN!
* coming soon: zyBooks Chapter 5 on PHP fundamentals
* will send a class e-mail when linked from Canvas
=====
INTRO to PHP
=====
* goal: a useful subset to let you get started building some
small web applications that use Humboldt's Oracle student
database, with and without sessions,
and including appropriate security measures
* PHP -
* created by Rasmus Lerdorf in 1994
* originally stood for:
Personal Home Page
later changed (!!) to stand for:
PHP: Hypertext Preprocessor
* yup, a recursive acronym...!
* much of its syntax was borrowed from C, Java, and Perl,
with some unique PHP-specific features thrown in...
* its goal: to allow web developers to write dynamically-generated
HTML documents
* PHP is free to use,
ships with the Apache web server,
is one of NUMEROUS choices for application-tier programming
You can embed PHP within HTML
BUT: the PHP is executed ON THE APPLICATION TIER!!!
====
* typically, you put your PHP --
whether it is one or a set of PHP functions
or PHP embedded within an HTML document -- in a file
on the application tier whose suffix is .php
* typical flow is like this:
1. client tier/browser requests a document with suffix .php
2. web server on the application tier
SEES the request ends with .php,
and directs that request -- ALONG with any accompanying data,
such as name=value pairs from a form submission --
to the PHP Engine ALSO running on the application tier
3. The PHP Engine executes the PHP in the requested
.php document, possibly using the accompanying data,
ON THE APPLICATION TIER
4. ...and returns the RESULTS of that execution (which now contains
NO PHP) TO the web server --
5. ...and the web server sends that result to the client browser
6. ...and the client browser executes the HTML/CSS/client-side JavaScript
in that result
* the client tier sees NO PHP!!!!!
it just sees the RESULTS from the application tier's EXECUTING
the PHP the client tier requested,
because PHP is executed on the APPLICATION tier!
====
* on a terminal/shell running on the application tier,
php -version
...will let you see the version of PHP you are running
* see the posted PHP references page!
* NOTE that the PHP home page: https://www.php.net/
and the PHP manual: https://www.php.net/manual/en/
are USEFUL references!
* NOTE: we are going to be following the PHP style from
"Web Programming Step by Step", 2nd edition,
in which one AVOIDS print/echo statements
* instead, you hard-code "constant" excerpts,
and jump "in" and "out" of PHP for the dynamic parts;
* (helps avoid errors from quoting and \n and etc.!)
* (so, while you will practice with echo/print statements
in the PHP zyBooks activities,
you are NOT to use echo/print statements in any other
CS 328 lab exercises, homeworks, or exams!!)
=====
PHP tags - PHP regular tags and PHP expression tags
=====
* these are the TWO kinds of PHP tags we will be using in
CS 328
========
* regular PHP tag:
========
<?php
PHP_statement;
PHP_statement;
...
PHP_statement;
?>
* these PHP statements are executed by the PHP Engine
and you may or may not SEE anything directly from these
statements in the result, depending on the statements
* note: PHP statements are terminated by a semicolon
========
* PHP expression tag:
========
<?= single_PHP_expression ?>
* you should embed a SINGLE PHP expression
(can be a simple or compound expression --
an expression that has a value)
* CS 328 class style: as an expression, it should NOT
be ended with a semicolon
(strictly speaking, ending a PHP expression with a semicolon
makes it a statement - PHP Engine might not throw an error
if you put a statement in a PHP expression tag, but it will
be considered as violating CS 328 class style standards)
* the PHP expression is evaluated,
and the value of the PHP expression is INSERTED in PLACE
of this expression tag IN the PHP Engine's result
* CS 328 class style: ALL of your PHP should be embedded in either
PHP regular tags or PHP expression tags
=====
PHP comments
=====
/* I am a
multi-line
comment */
// I am a single-line comment
# I am also a single-line comment
* BUT all of these DO need to be WITHIN PHP tags!
=====
some PHP basic types
=====
* PHP is loosely typed -- every expression has a type,
but a variable's type will be type of its value...
* four basic scalar (single-valued) data types:
integer
string
float
boolean
* two basic non-scalar data types:
array
object
* and several others -- for example, two other types are:
* null <-- yes, it is a type, and ALSO a value...!
* the null type only has one value, null!
* (and it is not case sensitive -- so can also
be NULL, null, Null...)
* resource -
"a special variable holding a reference to an external resource"
such as handles to opened files, database connections,
etc.
* int is typically used to mean the integer type
* you write int literals like you probably expect:
14 236 -5
* float and double and real are all the same datatype
in PHP --
the real size is platform dependent (I think)
* bool is typically used to mean the boolean type
* true and false are bool values
* but they are not case sensitive -- True, FALSE, etc.
* when printed or combined with other values,
true is treated as 1,
false is treated as 0? maybe treated as a blank...?!
(see how output in 328lect09-1.php...!)
* if you use a non-boolean expression in a boolean
context, PHP will TRY to treat it as a boolean;
that is, in such a context, these are ALL considered
to be false:
* the bool expression false
* the int 0
* the float values 0.0 -0.0
* the empty strings "" ''
* the strings "0" '0'
* an array with zero elements (with no elements)
* the unit type NULL (including unset variables)
* and a few others...
* but, generally, every other value is considered to be true,
including resource values and NAN <- not-a-number
* string - can be in single or double quotes
* BUT!! there are some interesting DIFFERENCES between
the values of single-quoted and double-quoted string expressions
if there are VARIABLES in that string!!
* array - can be an indexed array
(indexed by ints)
or an associative array
(indexed by other-type keys, such as strings)
* object - encapsulation of state and behavior
* PHP does support classes
=====
compound expressions
=====
* PHP has a variety of compound expression types,
built using infix operators and prefix operators
and function calls and yes we have ++ and -- also
* (and a few more options as well,
with some distinctively-PHP twists)
* function call - syntax is probably what you expect
funct_name(arg_expr, arg_expr, ...)
some built-in functions:
* date(format_str_arg) will return the current date formatted
according to the given format string
* var_dump(desired_variable) returns nothing, and has the
side effect of printing information about its argument,
including its type and value
* is_blah (is_int, is_float, is_bool, others)
return a bool letting you know if its argument is of that type
* gettype expects one argument and returns a string
whose contents are the name of the type of its argument
=====
PHP variables
=====
* naming rules:
* start with a $
followed by a character (CANNOT be a digit)
followed by 0 or more letters, digits, or underscores
(that is, note: a digit cannot follow the $ for a variable...)
* declaring rules: just use the variable
* you can ASSIGN to a variable with the assignment operator, =
* once you create a variable, its scope is the rest of that PHP document --
you can use it in ANY of the PHP tags from when it is created
until the end of that .php file
* zyBooks Section 5.2:
"Variable names are case-sensitive,
but functions and keywords are not" <--- ?!!?