Edgar Huckert: PHP
Some basic tests in PHP
As I forget frequently the basic features of PHP
I have written a standalone test program
that contains the most essential features:
- basic output ("echo ...")
- loops (for and foreach)
- arrays
- string handling: concatenation
- handling of multibyte strings
- character encoding
- simple conditions (if statement)
- directory handling
Note that the multibyte functions contained in this test program
must be installed separately (sudo apt install php7.4-mbstring).
I had to name this file "PHPTest.txt" instead of "PHPTest.php". When
having the extension ".php" it would be executed directly as
soon as the link is activated - you would only
see the results (outputs) of the program, not the program source text. You
can run this on the command line by saying:
php PHPTest.php
A PHP client (I used PHP client V7.4) must be installed as
this program is used outside the WEB(HTML context.
PHP is great for embedding in HTML pages. I use it frequently
for entering values in my MYSQL databases, for submitting SQL queries and for
getting results from the databases
on my WEB server - but this is not shown in this sample file.
Working with files in PHP
File IO in PHP is very similar to file IO in the standard C library.
I have written here a small
PHP program that shows the
basic functions for file handling. The file reads
a simple ASCII text file (the content is not important)
and writes out MD5-encrypted lines. As in the standard C library you
will find here built-in functions like:
- fopen()
- fclose()
- fgets()
- feof()
The program also demonstrates how
user defined functions and exceptions can be used in PHP.
This program is designed for
usage by a PHP client (not for inclusion in a HTML page). It
runs on the command line. Here again the extension of the source
file should be changed
to ".php" before executing the program.
WEB Pages and database access
Many simple database applications in the WEB context
can be designed as a two phase process:
- a simple HTML data entry form written in HTML or PHP
- a PHP script that accepts data from the form, executes
a SQL operation and sends back results
To demonstrate web access and data base access (in my case MySQL) I have
prepared two files:
- voc_entry1.html:
a HTML form page (data entry form) that sends data fields to a WEB server
- voc_entry2.php: a HTML page containing a PHP script. The PHP script
in the file inserts the transmitted data in a MySQL database table and
sends back a positive or negative result message.
Here is a screen dump for the data entry form voc_entry1.html:
The PHP script voc_entry2.php is called
in the FORM METHOD... tag in HTML form voc_entry1.html.
The PHP script follows these steps:
- get the data from the transmitted fields
- open the database connection
- check for double entry
- insert the new record
- send back a success or failure message
The field contents I need to fill table vocabulary
are in the associative array _POST[] - a standard
mechanism offered by PHP.
Here is the very simple SQL script that creates the table
I use in this sample:
CREATE TABLE `vocabulary` (
`word1` varchar(40) DEFAULT NULL,
`translation` varchar(40) DEFAULT NULL,
`language` varchar(3) NULL,
`comment` varchar(40) DEFAULT NULL,
`id` int(11) DEFAULT NULL
) DEFAULT CHARSET=utf8;
Contact
Copyright for all images, texts and software on this page: Dr. E. Huckert
If you want to contact me: this is my
mail address