It's February 2021, and I need to learn a little about PHP, the server-side scripting language that runs on most servers supporting web pages. My motivation for learning PHP was the need to create an HTML form from which I would gather visitor input and then send a message to myself. An example of this would be a comment form on a website. I document this as Using PHP to Send an Email from an HTML Form.
I'm documenting what I learn here for both myself and anyone else who might want an introduction to the language. That's just what I do ...
PHP is an acronym that is oddly self-inclusive, as it stands for "PHP: Hypertext Preprocessor." It runs on the server that supports your web page. Well, it can run there; you'll have to test it or ask to see if it is available to you, but it probably is. Test your own system for PHP support by clicking here.
<?phpand end with
echo "Hello World";
// the double slash starts a comment through end-of-line
# a hash tag does the same thing
/* this notation starts and ends a comment anywhere, even across lines */
$n = 5;
$text_message = "Hello World";
$camelCaseIsOK2 = "whatever...";
globalkeyword in the function (shown later -- we just wanted to let you know it is possible).
static $count = 0;.
PHP supports the following data types:
The echo and print keywords are nearly identical. They both output data to the screen (e.g., typically in the middle your HTML screen presentation). Because of the similarity, we will only discuss echo.
There's not much to discuss! The operand of echo can be straight text, text with HTML tags, or a calculation or other PHP statement. Parentheses are optional. Examples:
- echo("Hello"); // note: a space before the paren is optional
- echo "<h2>My Heading</h2>";
- $n = 5;
echo "The first value is " . $n + "<br>";
$m = 5;
echo "Adding " . $m . " the sum is " . $n + $m . "<br>";
Note the use of the concatenation character (a period surrounded by blanks). Also note our use of "<br>". Without it, no new line break would occur between uses of echo.
There are, of course, lots of built-in string functions. You can consult this PHP String Reference to see all of them. Here are some common examples:
- $mystring = "My name is Fred Brack.";
- echo strlen($mystring); // = 22
- echo str_word_count($mystring); // = 5
- echo strrev($mystring); // = .kcarB derF si eman yM
- echo strtoupper($mystring); // MY NAME IS FRED BRACK.
I know you guess what strtolower does ...
- echo strpos($mystring, "Fred"); // = 11
(1) position counting starts at 0;
(2) the operands are in the reverse order from many other programming languages;
(3) stripos does the same thing case-insensitive
- echo str_replace("Fred", "Kathy", $mystring); // = My name is Kathy Brack.
NOTE that now the operands are in a more logical order ...
And here's how you remove all blanks: str_replace(" ","",$variable);
- echo substr($mystring,11,6); // = Fred B
NOTE that if the third parameter (length) is missing, it defaults to end of string
- echo strstr($mystring, "Fred"); // = Fred Brack.
This returns the string starting with the passed phrase
There are many interesting string functions, including ones that:
PHP offers extensive date and time functions, so we are only going to give one example of the most common format here, and you can consult the w3schools Date and Time Functions Reference page for details. Be sure to click on the date() function by itself on that page to see the myriad of options (like the l M j below, which don't exactly jump out in their meaning)!
date_default_timezone_set("America/New_York"); // this
sets your local time zone
date("l M j\, Y") . " at " . date("h:i a") // = (for example) "Friday Feb 12, 2021 at 11:00 am"
PHP offers a variety of math functions. You can view the complete list at PHP Math Reference. They include functions like:
Some of the miscellaneous functions available are as follows. See the complete list at Miscellaneous PHP Functions.
You can use PHP to initiate an email to yourself or anyone else, with or without a copy list or blind copy list. Typically this would be done after a user fills out a form with requested data. For example, you might wish to use a form for user requests for information from you, to assure yourself of gathering required data. To learn how to do this, see Using PHP to Send an Email from an HTML Form.
I will be adding to this document after I have reason to learn more about PHP myself or I just decide to continue my own learning process, which I will share with you!