- Dragon Frugal Designs - https://dragonfrugal.com -

Introduction To PHP

PHP (PHP: Hypertext Preprocessor) [1] is a server-side [2] interpreted scripting language [3] that runs on a web server, which allows dynamic generation of a web page based on the parameters you write into the PHP [4] programming. It has been around since the mid 1990s, and is usually installed on just about any modern web server these days. The database language MySQL [5] is most often used in combination with PHP [4] to provide a full server stack [6] (bundle of needed server software) solution for web sites. You’ll need a server stack [6] of your own to get started using this tutorial, either with a web hosting company [7] online, or by using something like XAMPP [8] on your own computer.

Getting acquainted with the basics of PHP…

One of the great things about PHP [4] is how easy it is to mix *plain text [9] content or code with PHP [4] into any PHP [4] web page (usually any file with the file extension “.php”). For instance in this example below, we are running normal *plain text [9] content / web code outside of the PHP tags [10], and mixing it with PHP-rendered output:


Hello world, this is normal plain text, and

<?php 
echo ' this is PHP-rendered text using the echo construct.';
?>

This will output onto a web page looking like this:

Hello world, this is normal plain text, and this is PHP-rendered text using the echo construct.

Pretty cool…so in this way we can inject PHP dynamic content and code into any section of a web page, and use normal *plain text [9] outside of the PHP tags:

plain text here <?php // PHP code here... ?> plain text here

Strings, variables, and arrays are the data. Statements, functions, and loops process that data.

Six of the most basic and necessary PHP expressions [11] are strings [12], variables [13], arrays [14], statements [15], functions [16], and loops [17]

A string [18] is simply a line (or multiple lines) of text characters:

<?php
echo 'This is a string in PHP... A string can be on one line or multiple lines.';
?>

A variable [13] is simply a way to store strings for later use in the programming:

<?php 

$my_variable_name = 'This is a string in PHP... A string can be on one line or multiple lines.'; 

echo $my_variable_name; /* Printing to the web page a string stored in a variable beforehand. */ 

$my_variable_name = 'This is a different string stored in the same variable, replacing the old value.'; 

echo $my_variable_name; /* Printing to the web page the -NEW- string variable, after we changed it. */

?>

An array [14] is basically a way to store multiple variables inside one object (like a neatly-ordered “basket” of variables you might say…in fact, arrays can even be stored within other arrays):

<?php

$my_array_name = array('');  /* A new blank array */

$my_array_name = array(
                       'value 1',
                       'value 2',
                       'value 3',
                       'value 4'
                       );  /* Adding values to the array */

echo $my_array_name[0];  /* Print the first value to a web page (always starts with 0 for the key, not 1) */

echo '<br />';  /* OPTIONIAL : Print a line break to print everything after on a new line...copy and paste after each echo below if desired */

echo $my_array_name[1];  /* Print the second value to a web page */

echo $my_array_name[3];  /* Print the fourth value to a web page */

$my_array_name[2] = 'my new value';  /* Changing the value of the third item in the array */

echo $my_array_name[2];   /* Print the third value to a web page */

$my_array_name = array(
                       'my_key_name_1' => 'value 1',
                       'my_key_name_2' => 'value 2',
                       'my_key_name_3' => 'value 3',
                       'my_key_name_4' => 'value 4'
                       );  /* Changing the array to use your own key names (instead of 0 upward) */

echo $my_array_name['my_key_name_1'];  /* Print the new first value to a web page */

echo $my_array_name['my_key_name_2'];  /* Print the new second value to a web page */

echo $my_array_name['my_key_name_4'];  /* Print the new fourth value to a web page */

?>

Statements [15] are how you control what happens under different conditions:

<?php

$my_variable_name = '99'; /* Defining our variable  */

if ( is_numeric($my_variable_name) == false ) { /* Check for value not being a number */
echo 'The variable is not a number.';
}
elseif ( $my_variable_name == 99 ) { /* Check for value of 99 */
echo 'The variable is equal to 99';
}
elseif ( $my_variable_name >= 99 ) { /* Check for value equal to or greater than 99 */
echo 'The variable is equal to or greater than 99';
}
elseif ( $my_variable_name < 99 ) { /* Check for value less than 99 */
echo 'The variable is less than 99';
}
else {  /* If no conditions above match  */
echo 'The variable is something else.';
}

?>

Functions [16] are a way to run the same code in multiple places in PHP code, while only needing a small amount of code for each instance:

<?php

/* Creating a custom function to check for a numeric value greater than 99  */
function my_function_name($my_local_function_variable_name) {

    if ( is_numeric($my_local_function_variable_name) == false ) { /* Check for value not being a number */
    return 'Value is not a number.';
    }
    elseif ( $my_local_function_variable_name == 99 ) { /* Check for value of 99 */
    return 'Value is 99.';
    }
    elseif ( $my_local_function_variable_name >= 99 ) { /* Check for value equal to or greater than 99 */
    return 'Value is equal to or greater than 99.';
    }
    elseif ( $my_local_function_variable_name < 99 ) { /* Check for value less than 99 */
    return 'Value is less than 99.';
    }
    else {  /* If no conditions above match  */
    return 'Value is something else.';
    }

}

/* Running our custom function on a web page, using a variable   */

$my_variable = 'Test, 123';

echo my_function_name($my_variable); /* Print to the web page whether or not the variable is a number greater than 99 */

?>

PHP [4] has many built-in functions [16] you can use as well, with no need to create your own custom functions [16]. Once you have mastered creating your custom functions [16], you can categorize / organize similar and related functions [16] you create into classes [19], but more on that in a future article.

Loops [17] are kind of built like statements [15] or functions [16] (and may contain statements [15] or functions [16]) that are designed to process multiple values found in arrays [14]. Generally a loop [17] runs through each value in an array until it reaches the end of the array, or a condition is met (like X number of values have been processed, or some other milestone). There are 3 primary forms of loops in PHP [4] – foreach [20], for [21], and do / while [22]:

<?php

/* foreach example */
$my_array_name = array(
                       'value 1',
                       'value 2',
                       'value 3',
                       'value 4'
                       );  /* Adding values to the array */

foreach ( $my_array_name as $array_key => $array_value ) {

echo $array_value . '<br />';  /* Print the array value on the web page */

$array_count = $array_count + 1;  /* Add 1 to count of all array values */

}
echo $array_count . ' Values in array' . '<br />'; /* Print count */
/* ////////////////////// */

/* for example */
for ($my_variable = 0; $my_variable < 10; $my_variable + 1) {

$my_variable = $my_variable + 1; /* Up the variable value by one */
echo $my_variable . '<br />';  /* Print the variable value */

}
/* ////////////////////// */

/* do / while example */
$my_variable = 0;
do {

$my_variable = $my_variable + 1; /* Up the variable value by one */
echo $my_variable . '<br />';  /* Print the variable value */

}
while ($my_variable < 10);
/* ////////////////////// */

?>

Be careful with loops, as you can create an endless loop pretty easily by not making sure the requirement to end the loop is properly met. Only foreach [20] loops are safe from this potential issue.

Time to get your hands dirty.

Hopefully you are armed with enough talent now to copy and paste [23] the above code into your favorite text editor (hover over the code, look in top right corner of code section for the buttons), and do some hands on coding…good luck! 🙂  I highly recommend Komodo Edit [24], a free and very powerful / easy-to-use text editor specifically designed for editing web page code. You’ll also need to upload your code to your web site. FileZilla [25] is an excellent free FTP client that also works well.

*Plain text [9] refers to any content text, or even code like HTML [26], CSS [27], or Javascript [28] which is NOT server-side [2] PHP scripting (rather it is client-side [29] code of some variety).

UPDATE: I was contacted recently by Tiffany at udemy.com [30], who pointed out they also have a beginners PHP Tutorial as well [31]. Looks like they did a nice job covering the basics. 🙂