Introduction To PHP

PHP (PHP: Hypertext Preprocessor) is a server-side interpreted scripting language that runs on a web server, which allows dynamic generation of a web page based on the parameters you write into the PHP 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 is most often used in combination with PHP to provide a full server stack (bundle of needed server software) solution for web sites. You’ll need a server stack of your own to get started using this tutorial, either with a web hosting company online, or by using something like XAMPP on your own computer.

Getting acquainted with the basics of PHP…

One of the great things about PHP is how easy it is to mix *plain text content or code with PHP into any PHP web page (usually any file with the file extension “.php”). For instance in this example below, we are running normal *plain text content / web code outside of the PHP tags, 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 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 are strings, variables, arrays, statements, functions, and loops

A string 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 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 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 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 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 has many built-in functions you can use as well, with no need to create your own custom functions. Once you have mastered creating your custom functions, you can categorize / organize similar and related functions you create into classes, but more on that in a future article.

Loops are kind of built like statements or functions (and may contain statements or functions) that are designed to process multiple values found in arrays. Generally a loop 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 – foreach, for, and do / while:

<?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 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 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, 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 is an excellent free FTP client that also works well.

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

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

Tweet about this on Twitter0Share on Facebook0Pin on Pinterest0Share on LinkedIn1Share on Tumblr1Share on StumbleUpon15Share on Reddit0Share on Google+0

Email This Post Email This Post     Print This Post Print This Post     Download PDF Version Download PDF Version

This entry was posted in PHP, Tutorials and tagged , , , , , . Bookmark the permalink.

One Response to "Introduction To PHP"

Leave a Reply

Your email address will not be published. Required fields are marked *


*