Jun
12
2013

Getting Started with PHP: Part 2

Continuing the tutorial series on getting started with PHP (see “Getting Started with PHP” for the last batch), today I’ll provide some information on…

  • Including external files
  • Assigning PHP variables and constants
  • Overview of variable and constant usage

Including External Files

PHP allows us to include external files that allow us to decrease the amount of redundant/repeated code used, as well as a security measure for important files such as files containing database connection details, encryption keys, and other sensitive information.

There are 4 primary ways to include files using PHP:

  • include (‘file’);
  • include_once (‘file’);  //used for including files that include files- eliminates repeated occurrences of same file loading multiple times
  • require (‘file’);
  • require_once (‘file’); //used for including files that include files- eliminates repeated occurrences of same file loading multiple times

The primary difference is error handling:

  • include and include_once will generate a warning if an error is detected, but will not stop the page/script from being executed, whereas…
  • require and require_once generates a fatal error, thereby stopping the script entirely

Included external files may contain ANYTHING, which allows us to use constant files throughout websites…

Footer.php:

<div class="footer">(c) 2011 This course | Link | Link | Link | Link</div>

The page:

<html>
................................................
<?php include ('includes/constant/footer.php'); ?>
</html>

Note that there is no “echo” or “print” in play here, as the “include” and “require” functions render the content exactly as though it were already on the page.

This makes actual output of the page above…

<html>
....
<div class="footer">(c) 2011 This course | Link | Link | Link | Link</div>

Similarly, we are able to include files that reference lengthy or complex operations that do not need to be rendered such as this snippet from a database connection file:

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "some_database";

//Connect to MySQL Server
mysql_connect( $dbhost, $dbuser, $dbpass );

//Select Database
mysql_select_db( $dbname ) or die( mysql_error() );
define( "ARTICLES_TBL", "some_articles" );
define( "BASE", "http://".$_SERVER['HTTP_HOST']."/yoursite" );
define( "SITE_ROOT", $_SERVER['DOCUMENT_ROOT']."/yoursite" );

Saving use AT LEAST 13 lines of repeat typing, and the unnecessary modification of (potentially) hundreds of files in the event of a connection detail change!

Assigning PHP Variables and Constants

What?

A PHP variable can be assigned to nearly anything, with the purpose of simplifying repeated calls to operations, functions, or variables.

  • Constants follow a “define( “VARIABLE”, “value for this constant” );” structure, while
  • Variables follow the $variable = “variable”; structure
define( "TITLE", "This is a page title"); //calling "echo TITLE" will output "This is a page title"
$one = 1; //calling "echo $one;" will now output the integer 1
$two = 10/5; //performing mathematical operation, calling "echo $two;" will now output 2
$me = "Bennett Stone";
$onetwo = $one . $two; //joining two constants into a new constant
$sql = mysql_query( "SELECT name FROM some_table WHERE name = '$me'" );

In the last example (“$sql”), we created both a php constant ($sql), and included a PHP constant ($me).

Leave a comment