W3Schools Tutorial

Most of these examples are from W3Schools

This won't look like anything but HTML in a browser. PHP renders HTML on the server side. That HTML is then sent as the responds to a request from a client (i.e.: browser). Open this w3tutorial.php file in a browser to see the PHP code. You can only download PHP source code if the PHP source is made available to you. You can download this source from Dan's GitHub

So far in this file, this has all been written as HTML.

Basic PHP

This is PHP.

If you open this in a browser and view source, you won't see any PHP in the source code. All the PHP is executed on the server to create HTML, and only HTML is served.

The the following 3 lines look the same in HTML, but were created diffently in PHP:

Developer is using 8.2.28

Developer is using 8.2.28

Developer is using 8.2.28

PHP Data Types

The var_dump() function returns info about variables.

int(5)
string(9) "Developer"
float(3.14)
bool(true)
array(3) {
  [0]=>
  int(2)
  [1]=>
  int(3)
  [2]=>
  int(56)
}
NULL
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2025-11-26 21:23:53.527673"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}
resource(2) of type (stream)
    

Cookies and Sessions

PHP can track user interactions.

PDO: PHP Data Object

We'll be using the PDO object in CSCI 331. The try/catch construct is typical for this.

The following code tries connecting to a database that probably isn't set up (unless you create one with these same credentials, or alter this PHP code to use one that you have set up.) So if there's no success in connecting, the catch statement runs, and the die() function runs with an argument explaing the problem.

Connection failed: SQLSTATE[HY000] [2002] No such file or directory