=====
CS 328 - Week 10 Lecture 1 - 2026-03-30
=====
=====
TODAY WE WILL
=====
* announcements
* string concatenation in PHP
* how to get PHP errors in the browser
* writing your own PHP functions
* whirlwind tour of using PHP and OCI (Oracle Call Interface)
to allow PHP on the application tier to make requests
from the data tier, in this case from an Oracle DBMS
* prep for next class
=====
* should be working on Homework 8!
* deadline: 11:59 pm on Friday, April 3
* START now, NOT later in the week!
* submit files OFTEN, THROUGHOUT the week!
* Should also start reading and working through the activities
in zyBooks Chapter 5 - PHP Fundamentals
* IT IS advising time for SUMMER 2026/FALL 2026!
* preregistration for FALL 2026 begins at your
registration appointment in your student center
(sometime between April 13 - April 24)
=====
SIDE NOTE: if you are considering taking Humboldt courses in SUMMER 2026
=====
* registration for SUMMER 2026 opens for ***EVERYONE*** on APRIL 13!!!!
* from:
https://www.humboldt.edu/student-financial-services/summer-term-2026
"For the 2026 Summer term, the university is guaranteeing 3 units,
if enrolled in 6 or more units, will be covered for all continuing
matriculated undergraduate students. This will cover 3 units of
the tuition charge and does not include coverage of mandatory
campus-based fees or the additional non-resident tuition if
applicable."
* see the above link for more information!
=====
string concatenation in PHP
=====
* PHP gets its operator for this from Perl!
...and uses a . (<-- period) as the concatenation operator!
* you WILL get an error if you try to use + to append two
strings in PHP
* for example:
<p> <?= "a" . "b" . "c" ?> </p>
|
| when the PHP is executed, this becomes:
v
<p> abc </p>
* see 328lect10-1.php
=====
how to request that error reporting be turned on
for a particular PHP document
=====
* nrs-projects has PHP error messages turned off by default
(that is, in the php.ini file for nrs-projects, there is a setting that
keeps PHP error messages from being included in a PHP document's executed
results that are sent to the client tier's browser)
* (it is not considered good practice to include them in a production
setting...)
* BUT, they can be very useful in debugging! and during development!
SO -- happily, nrs-projects IS set up so that individual PHP documents
can enable error messages to be included in their response;
* we'll put this in the head element,
say after the meta element for charset:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
* now, PHP error messages should be enabled for inclusion in a PHP
document's result
BUT NOTE!!!!
* certain PHP syntax errors still result in the PHP engine
sending no result --
(not unlike how a syntax error can keep a C++ compiler from
giving you an executable result...)
running your PHP from the nrs-projects command
line will *sometimes* give you a parse-level error message:
[nrs-projects]$ php desired_php.php
=====
PHP Variable Interpolation in strings
=====
* variable interpolation: means you can write a variable
within certain kinds of PHP string literals,
and that variable will be replaced by its value
in that string
* if you want it,
here's one way to get it:
IF you write a PHP variable in a DOUBLE-QUOTED
string, you'll get this -- the variable will be replaced
by its value
AND: if you write a PHP variable in a SINGLE-QUOTED
string, you WON'T get this; that variable name will
NOT be replaced by its value
* for example:
<?php
$looky = 13;
?>
<p> <?= "what is this: $looky" ?> </p>
|
| when the PHP is executed, this becomes:
v
<p> what is this: 13 </p>
<p> <?= 'what is this: $looky' ?> </p>
|
| when the PHP is executed, this becomes:
v
<p> what is this: $looky </p>
* Also - because it is sometimes useful!! -
in a double-quoted string, you can optionally use
CURLY BRACES around a variable name to "ensure
proper variable interpolation" when you happen to
want to surround a variable's value with NON-blank characters!
* that is, the { } make EXPLICIT where the variable name begins
and ends
* for example:
<p> <?= "0{$looky}0" ?> </p>
|
| when the PHP is executed, this becomes:
v
<p> 0130 </p>
=====
how to write and use a PHP function
(esp. one in its own file)
=====
* because sometimes you want to abstract common
actions into functions!
* THING TO REMEMBER PART 1:
* if this is in its own file, or with
other functions in its own file,
standard practice is to give this file a suffix .php
* it is REQUIRED that a function definition be
within a regular PHP tag in this separate file!
<?php
...
?>
* THING TO REMEMBER PART 2:
the function syntax will mostly look like you
know from C++, EXCEPT we are in world of
loose typing, so:
* instead of a return type, the function header
begins with the word function
* and the list of parameters after the function
name are written as PHP variables, and not given
data types
<?php
function my_fun_name( $param1, ... $param_n )
{
statement;
...
statement;
... can have return; or return expr; ...
}
* return statement IS optional
* BUT if a return statement is reached,
that DOES end the function call at that point
* see square.php
and see ow 328lect10-1.php uses:
<?php
require_once("square.php");
?>
...in its head element to copy the square function's definition
there,
and then you can call the square function as desired in PHP tags
within the body element
=====
STARTING our discussion of connecting to Oracle from PHP
=====
* PHP on the application tier is able to connect to the
data tier to make requests of the data tier
* there are MANY packages out there for letting PHP do this
with MANY different DBMSs
* OCI, Oracle Call Interface, is one such package
that happens to work with Humboldt's PHP on nrs-projects
and Humboldt's Oracle student database on cedar
* what are the steps for this?
* in OCI, you first need to establish a connection between
the PHP Engine and an Oracle DBMS
You can do this with OCI's oci_connect function
oci_connect is a PHP function that is part of OCI;
it expects 5 arguments, and if successful returns a
connection object
* a string username
* a string password
* a string connection string
* a string encoding
* an integer session mode
* [we talked about a few gory details about how student
Oracle accounts are set up on campus...]
* (and because the number of connections to the Oracle db
on campus is FINITE,
class style is to explicitly CLOSE your connection,
using:
oci_close with your connection object as its argument:
oci_close($conn);
...as soon as you are done with the connection,
BEFORE completing the PHP's response!!!!!!!!!!!!!!!!
* Wednesday: we'll walk through how PHP on nrs-projects
can connect to Humboldt's Oracle student database