Sample PHP Scripts
This page lists some sample standalone PHP scripts illustrating core language principles, as well as some PHP scripts that have associated HTML and other files. There are several groupings: core language, system and web info, get and post, forms and their submission and processing, file-handling, cookies, sessions, general database, database table creation, a simple music database, and database-based user authentication. Many of these sample files are modified and updated versions of examples from the various editions of a text on PHP, MySQL and Apache by Julie Meloni. Note that sometimes the first link will not run the script, for various reasons such as the access required or what it might do, and there will be a message that you should make a copy of the script for your own account and run it there.
When clicking on any of the "text" links below, you should see the
text of the associated .php file, which will run and show you its
output if you click on it.
PHP Includes
-
include.php [ text ] Illustrates the PHP include()
function.
-
require.php [ text ] Illustrates the PHP require()
function.
-
include_with_error.php
[ text ] Illustrates the PHP include()
function behavior when an error is encountered.
-
require_with_error.php
[ text ] Illustrates the PHP require()
function when an error is encountered.
Core Language Group
-
first.php [ text ] The PHP version of the classic
"Hello, world!" program.
-
error1.php [ text ] Generates a parse error
because of a missing semi-colon.
-
error2.php [ text ] Generates a parse error
because of nested double quotes.
-
error3.php [ text ] Generates a fatal error
because of a call to an undefined function.
-
error4.php [ text ] This example used to generate a
notice error because of an undefined variable
but with PHP 8 many "notice errors" have been coverted to "warning errors", including this one See the text of this example for a list.
-
error5.php [ text ] Generates a
warning error because of a non-existent include file.
-
comments.php [
text ] Illustrates PHP comments.
-
printvars.php [
text ] Displays values of
several PHP variables.
-
heredoc.php [ text ] Illustrates PHP "heredoc"
construct.
-
assignment.php [
text ] Illustrates PHP
assignment operations.
-
arithmetic.php [
text ] Illustrates PHP
arithmetic operations.
-
comparison.php [
text ] Illustrates PHP
comparison operations.
-
logical.php [ text ] Illustrates PHP logical operations.
-
arrays.php [ text ] Illustrates PHP arrays.
-
explode_implode.php [
text ] Illustrates the
"exploding" of a string into an array of strings, and the
"imploding" of an array of strings into a single string.
-
regular_expressions.php
[ text ] Illustrates
the use of the PHP
preg_match()
function to test a
regular expression.
System and Web Info Group
-
phpinfo.php
[ text ] Displays a lot
of information about your PHP installation. A good script to run
to test your installation, but not necessarily one you want every
visitor to be able to run.
-
remote_address.php
[ text ] Finds
and displays the IP address of your browser location.
-
user_agent.php [
text ] Identifies
and displays more details than you really wanted to know about
the browser you are using.
-
browser_match.php
[ text ]
Identifies and displays just the name of the browser you are
using.
-
server_id.php [
text ] Shows the
username under which the web server is running.
-
test_SERVER.php [
text ] Shows the
values of a number of global constants.
Get and Post Group
-
test_get.html
A form for entering data to illustrate the GET method for submitting form data. [ text of test_get.php,
the PHP script that processes the form data submitted. ]
-
test_post.html
A form for entering data to illustrate the POST method for submitting form data. [ text of test_post.php,
the PHP script that processes the form data. ]
-
simple_get.php [
text ] A simple program to illustrate the GET method of passing values to a PHP script at the end of the URL that acesses the script file. To use this script you need to understand the GET method, how values are sent via a URL, and you also need to know that the two (integer) values you want to send are for the variables
$first
and $second
.
Simple Form Input, Submission and Processing Group
-
calculate.html
A form to enter data for calculation.
[ text of calculate.php,
the PHP script that performs the calculation. ]
-
string_manipulations.html
[ text ] Permits the user to enter some text and choose what to do with that text from several
alternative actions.
-
redirect.html [
text ] Permits the user to choose a web site from a dropdown list, and then redirects the user to that site when the user clicks the Go button.
-
email_feedback.html [
text ] Permits the user to fill out a simple feedback form and have the form data e-mailed to a recipient.
-
email_feedback_allinone.php
[ text ] Same functionality as the two files in the previous example, but now encapsulated in a single file.
File-Handling Group
The scripts in this group are all related to file-handling in PHP.
You should not try to run them here. Instead, download the files to
your own account and run them there. In order to run through this
sequence of scripts successfully, you should do the following:
In the same directory where this group of files is located, issue
the following Linux commands:
mkdir files
chmod 777 files
-
list_files.php
[ text ] Displays
an unordered list of all files in the same directory as the
script itself.
-
create_file.php
[ text ] Creates
an empty file called
my_data.txt
in the subdirectory
called files
, which is located in the same directory
as the script itself. This script simply overwrites any file with
the same name in the same location.
-
create_file_check_first.php
[ text ]
Performs the same action as the previous script, except that it
checks first for the existence of a file with the same name in
the same location and does not create a new file if such a file
already exists.
-
delete_file.php
[ text ] Deletes
any file called
my_data.txt
from the subdirectory
called files
, if that subdirectory is located in the
same directory as the script itself. [Exercise: Try calling this
script without altering the file files/my_data.txt
in any way, and try deleting a second instance of the file after
editing it. In this latter case do a long listing of the file
before and after the editing and note carefully what you see.]
-
write_data_to_file.php
[ text ]
Writes the following three lines, plus another line containing the date when the file was written, to the file
files/my_data.txt
in the current directory:
Check it out!
I've created a new file and
stuck all this text into it!
-
read_data_from_file.php
[ text ]
Reads and displays the contents of
files/my_data.txt
, with a single-line preamble. For
example, if the file contains the contents put there by the
previous script, the output of this script would be the following four lines, plus a line showing the date when the file was written:
The file files/my_data.txt contains:
Check it out!
I've created a new file and
stuck all this text into it!
-
mail_file_contents.php
[ text ]
Mails the contents of
files/my_data.txt
to an e-mail
address "hard-wired" into the script.
-
copy_file.php [
text ] Makes a copy
of
files/my_data.txt
and calls it
files/my_data.bak
, which could serve as a backup for
the original file.
-
rename_file.php
[ text ] Renames
the file
files/my_data.bak
to
files/my_data.txt
, which could be regarded as
restoring the file from backup. [Exercise: Try this script with
and without editing the files/my_data.bak
file, and
observe what happens in each case. Compare this exercise with the
one involving the delete.php
script above.]
-
upload_file.html
[ text of PHP script to process the form data in the accompanying HTML file ] Permits the user to upload a file from the client machine to a
files
directory on the server.
General Database Group
The scripts in this group are all database-related. They illustrate
connecting to an existing database, trying (unsuccessfullyl) to create a new database, trying (unsuccessfully) to remove (or "drop") a database, as well as displaying all accessible databases and their tables.
-
db_connect.php [
text ] Connects to a
particular database, but doesn't do anything.
-
db_create.php
[ text ] Connects to one
database and tries to create another database, but fails because
the user does not have permission to do so.
-
db_drop.php [
text ] Connects to a database and tries to "drop" (or delete) another database, but fails because that database does not exist.
-
db_listdb.php
[ text ] Lists the
databases to which a particular MySQL user has access.
-
db_listtables.php
[ text ] Lists all
the tables in all the databases to which a particular MySQL user
has access.
Database Table Creation Group
The three files in this group allow you to create a table in your
database. The HTML file will show you the form and you can fill it
out but the script to which it is connected will not run here. You
must copy all three files to your own account and set them up with
your own username and password and database for them to work
properly.
-
get_table_name_and_size.html
Provides a form for the user to enter the name and number of
fields in a table to be created.
-
define_table_fields.php
[ text
] Receives the data from the form in the previous file and allows
the user to choose the name and data type for each field in the
table.
-
create_table.php
[ text
] Actually creates the table developed by the previous two files.
A One-Table Music Database Group
The scripts in this group add records to, and retrieve information
from, a one-table database contain information related to a music
library. You should use the three files in the Database Table
Creation Group above to create the table. Then you can use the
files here to put records in the table and retrieve information
from the table via a menu that lets you select how you would like
to see the data organized. The HTML files here will show you the
relevant forms but the associated scripts will not run here. You
must copy them to your own account and set them up properly there
for them to perform their various tasks.
-
my_music.txt A
brief description of the files in the single table that makes up
the
my_music
database.
-
add_record.html [
text ] The form and
script that add a new record to the music database.
-
selection_menu.html
The menu that allows the user to choose how the music information
is to be displayed.
-
select_by_artist.php [
text ] Shows the musical selections in the database ordered by artist name.
-
select_by_date_acquired.php
[ text ] Shows the musical selections in the database ordered by date acquired, with the most recent first.
-
select_by_id.php [
text ] Shows the musical selections in the database ordered by id number, which will be the order in which the items were entered into the database.
-
select_by_title.php [
text ] Shows the musical selections in the database ordered by title.
Cookies Group
The scripts in this group are all cookie-related. A cookie is a
small piece of text sent from the server to the browser, to be
stored on the browser, and later retrieved and used by that server
when that browser comes back to that server.
-
set_cookie.php [
text ] Sets a cookie called
"name" with value "Porter" to last for two minutes.
-
get_cookie.php [
text ] Gets the cookie
called "name" with value "Porter" and uses it to welcome the user
"Porter" back to the site.
-
get_cookie_with_test.php
[ text ] Gets the
cookie called "name" with value "Porter" and uses it to welcome
the user "Porter" back to the site, but also tests to see if
there is a cookie, and if not issues a welcome to the arriving
"stranger".
-
unset_cookie.php [
text ] Deletes (or
"unsets") the cookie called "name" with value "Porter".
-
bad_cookie.php [
text ] Illustrates that a
cookie must be the first thing sent from a web page.
Sessions Group
The scripts in this group are all session-related. A session is a
mechanism used to pass information from one page to another as a
user browses from one page to another on your site. This is a way
of overcoming the "stateless" nature of web pages that "have no
memory".
-
session_start.php [
text ] Starts a PHP
"session".
-
count_visits.php [
text ] Updates a visit counter in the
$_SESSION
array and reports the result.
-
session_remove.php [
text ] First removes all components from the current
$_SESSION
array, and then removes the session itself.
-
set_preferences.php
[ text ] Permits the user to choose the font family and size to use for displaying the page.
-
change_preferences.php
[ text ] Permits the use to change the font family and size preferences set by the previous script.
Database User Authentication Group
The scripts in this group are all related to user authentication by
checking whether a user is "registered" in a database. The scripts
must be copied to your own account and set up to work there, since they will not run here.
-
auth_users.txt
A brief description of the files in the single table that makes
up the
auth_users
database.
-
add_user.html
The form for adding a user to the
auth_users
database.
-
add_user.php
[ text ] The script for adding a user to the
auth_users
database.
-
login1.html The login form for authenticating a user in the
auth_users
database.
-
login1.php [
text ] The script for authenticating a user in the
auth_users
database.
-
login2.html The login form for authenticating a user in the
auth_users
database, and setting a cookie which is checked when the user tries to access either of the following "secret" scripts.
-
login2.php [
text ] The login script for authenticating a user in the
auth_users
database, and setting a cookie which is checked when the user tries to access either of the following "secret" scripts.
-
secretPage.php
[ text ] A script that checks whether a cookie is set before proceeding.
-
secretA.php [
text ] The first script that tests whether a cookie is set before proceeding.
-
secretB.php [
text
] The second script that checks whether a cookie is set before
proceeding.