PHP Fundamentals for beginners

in
Author: 
Luke Tarplin

Php or (Php Hypertext Pre-processor) is a scripting language, it requires a Stack of technologies to process the scripts on a server side and then deliver the resulting HTML output to a browser. It can also be used at the server console to perform actions on the server.

Php Scripts are essentially Server Side HTML, while it’s true they may not actually contain any HTML or send HTML to the browser, they contain script which is interpreted by the Server. For example the following script <?php echo date(“Y”);?> will be processed by the server before being sent to the client. In this case HTML will be delivered in the form of the current Year.
 
Php Files
The Php index files is entitled index.php by default, however this can be altered by making changes to the Server Configuration.

The files which are interpreted are mainly *.php files, however they could also be *.inc files to denote includes. We could also use *.inc.php or *.class.php files to denote includes and classes. We could also use our own file types to be included by *.php files. For example; index.php could be used to include a *.ourfile script.
 
Include Files
These files are used to include script into another file; it can be useful when using many single pages to build a website. For example if we wanted to include a header file or a global site menu into many pages we could use an include file such as; menu.inc.php.
Include files can also be used to store global functions, which many need to be called many times, these functions would then be available to the entire script. For example: api.functions.php could be used to store a function entitled <?php GetPage($id);?>, which could be used to alter the content based on a page id.
 
Classes and Functions
One of the tasks of the Php developer is to ensure that the script is well written and concise, one of the ways this can be done is to avoid and reduce repetition of code.

Classes  are containers, which commonly have methods, they are used to create objects and are useful when performing many related actions or storing data relating to one item. To create a class we can either add the class script into the *.php file we are looking at directly or use a class include. An example class could be:
 
<?php
class bootstrap{
 
      function MakeIncludes(){
            include "../config/settings.inc.php";
            include "db.class.php";
            include "page.class.php";
            include "html_elements.class.php";
            include "general.class.php";
           
            //Make DB Connection
            db_connection(DBHOST,DBUSER,DBPASS,DBX);
      }
     
      function MakeHeader($header = NULL){
            echo "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">";
            echo "<head>";
                  echo $header;
            echo "</head>";
      }
}
?>

Functions
These are containers for script, which can be used more than once, this is a good method to reduce repetition in code, if for example we find ourselves doing the same mathematical calculation over and over again, we can create a function and pass it arguments.

A function argument can be a string, integer, array, object, resource etc. Once passed into a function operation can be performed on these arguments to produce an action or output. A sample function is:
 
<?php
function lowerword($word){
return strtolower($word);

}

There are built in functions built into Php, these can be called from anywhere within the script, these functions can also be extended by installing additional Php extensions. If an unknown or an undefined function is called it will return a: “Fatal error: Call to undefined function” error.

Arrays
An array is a good way to structure or organise data, Arrays have dimensions and the more dimensions an array has the more complex it becomes. It is closely related to Objects and can work in much the same way.

A Php array looks like this:

<?php
$MyArray = array();
$MyArray[0][0][0] = “A”;
$MyArray[0][0][1] = “B”;
?>

The above array will return: Array ( [0] => Array ( [0] => Array ( [0] => A [1] => B ) ) ), here we can see that the value of A and B is stored in the 3rd dimension of this array. Practical uses of arrays include storing database query results, storing configurations and paginating data.

The Php resource for Arrays can be found here
Valid XHTML 1.0 Strict