PHP PHP Server-Side Scripting Language

PHP is an open source server-side scripting language widely used in the development of web pages. PHP is used to dynamically build web pages, create login systems, upload files, send emails, and other processes that require interaction with web and database servers. See additional details about PHP history and usage. The PHP Group website is a good reference for PHP.


PHP Environment Setup wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24-32b.exe

My PHP environment was set up with WampServer 2.2e. See the Apache page for details about installing the WAMP package of software and configuring a virtual host for the testing website. See the Eclipse page for information about using Eclipse with Zend's PDT plugin for an IDE for PHP development. Also see the Dreamweaver page for using it as a PHP IDE.


PHP Configuration C:\wamp\bin\apache\apache2.2.22\bin\php.ini
PHP is like buying a new car in that there are many options you can choose. The php.ini file is where you enable/disable the various PHP options. There are also configuration settings in PHP which are strongly recommended for security reasons, such as disabling the Register Globals and Magic Quotes. Before configuring PHP, check the location of the "Loaded Configuration File" from the PHP (phpinfo) installation page to be sure you are modifying the correct version of the file. My PHP configuration file is located at C:\wamp\bin\apache\apache2.2.22\bin\php.ini.

The table below lists recommended PHP configurations.

Recommended PHP Configurations
Directive Recommended Value Default Value Description
allow_url_fopen On On
Whether to allow the treatment of URLs (like http:// or ftp://) as files
allow_url_include Off Off
Whether to allow include/require to open URLs (like http:// or ftp://) as files.
display_errors Development = On
Production = Off
On
Directive controls whether or not and where PHP will output errors, notices and warnings too. Error output is very useful during development, but it could be very dangerous in production environments.
error_reporting Development = E_ALL | E_STRICT
Production = E_ALL & ~E_DEPRECATED
E_ALL
Directive informs PHP of which errors, warnings and notices you would like it to take action.
file_uploads On On
Whether to allow HTTP file uploads
magic_quotes_gpc Off On
Off is recommended for security reasons.
register_globals Off Off
Off is recommended for security reasons.
register_long_arrays Off On
Determines whether the deprecated long $HTTP_*_VARS type predefined variables are registered by PHP or not.
safe_mode Off Off
Deprecated - the PHP safe mode is an attempt to solve the shared-server security problem.
short_open_tag Off On
This directive determines whether or not PHP will recognize code between <? and ?> tags It's been recommended for several years that you use full <?php and ?> tag.
upload_tmp_dir "c:/wamp/tmp"
"c:/wamp/tmp"
Temporary directory for HTTP uploaded files (will use system default if not specified).


Coding PHP Classes Object Oriented PHP Development
  1. Create a new PHP file (DateUtility.php) that contains the class (DateUtility). Inside the class code a function to get the current time.

    <?php 
       class DateUtility {
          public function getCurrentTime()
          {
             date_default_timezone_set("America/Chicago");
             echo "Current time is: ";
             echo date("g: i"); 
          }
       }
    ?>

  2. Go to the file where you wish to use the class. Code an Include statement for the class at the top of the file.

    <?php include("_include/DateUtility.php"); ?>

  3. In the file where you wish to use the class, insert the code to instantiate the object and call the class function.

    <?php  
    // Create an instance of object.
    $dateutil = new DateUtility();
    // Call object function.
    $dateutil->getCurrentTime();
    ?>
MySQL PHP Coding API Extension and Coding Example

For PHP, there are two versions of the MySQL API extensions mysql and mysqli (mysql improved). It is strongly recommended that you use the mysqli extension if using MySQL version 4.1.3 or later. mysqli improvements include an object-oriented interface and support for prepared statements and transactions. Below is a simple script for connecting to a MySQL database using the mysqli extension.

<?php

  //Get Data Thru URL
if (isset($_GET["sql"])) {
$sql = $_GET["sql"];
// Decode Data Passed Thru URL
$sql = urldecode($sql);
} else {
die("Error: The SQL query was not passed in.");
}
echo "<h3>Search: $sql</h3>"; // Find Server OS and Set DB Connection Parameters $sys = strtoupper(PHP_OS); if(substr($sys,0,3) == "WIN") { $con = mysqli_connect("localhost","root","","dbName"); } else { $con = mysqli_connect("pdb3.your-hosting.net","1218369_photos","password","dbName"); } // Check DB Connection Success if (mysqli_connect_errno($con)) { die (mysqli_connect_error()); } // Run Query Against DB $result = mysqli_query($con,$sql) or die(mysqli_error($con)); // Return DB Row in an Array while($row = mysqli_fetch_array($result)){ $fileName = trim($row['Path']); $title = trim($row['Title']); $description = trim($row['Description']); $comment = trim($row['Comment']); $comment2 = trim($row['Comment2']); $picture = substr($fileName,7); echo "<div style='overflow: hidden; float:left; clear: right; margin: 0 10px 10px 0; height: 330px'>"; echo "<img src=$fileName ;height='250'; width='333'><br>"; echo "<table style='border-collapse: collapse; cell-padding: 5; border: 0; border-spacing : 0; padding: 0; margin: 0'>"; if ( (isset ($title)) && ($title != "") ) { echo "<tr><td style='border:0; padding: 0 10px 0 5px; '><b>Title:</b></td>" . "<td style='border:0; padding: 0 10px 0 5px; margin: 0'><b>" . $row['Title'] . "</b></td></tr>"; } if ( (isset ($description)) && ($description != "") ) { echo "<tr><td style='border:0; padding: 0 10px 0 5px; margin: 0'>Description:</td>" . "<td style='border:0; padding: 0 10px 0 5px; margin: 0'>" . $description . "</td></tr>"; } if ( (isset ($comment)) && ($comment != "") ) { echo "<tr><td style='border:0; padding: 0 10px 0 5px; margin: 0'>Comment:</td>" . "<td style='border:0; padding: 0 10px 0 5px; margin: 0'>" . $comment . "</td></tr>"; } echo "<tr><td style='border:0; padding: 0 10px 0 5px; margin: 0'>Picture:</td>" . "<td style='border:0; padding: 0 10px 0 5px; margin: 0'>" . $picture . "</td></tr>"; echo "</table></div>"; } // Close DB Connection mysqli_close($con); ?>


PHP Code Segments Example PHP Code
  1. Current Time

    <?php 
       date_default_timezone_set("America/Chicago");
       echo "Current time is: ";
       echo date("g: i"); 
    ?>

Top | Environment | Configuration | Classes | Code