php.ini Error Settings

Error settings in php.ini tell PHP interpreter what kind of errors should be reported and where those should be reported. You may enable these settings in your development environment and disable few in your production environment since useful information error reports provide can also be sensitive information that outsiders shouldn’t see.

display_errors 

Default value of this setting is ‘On’. It tells PHP interpreter that if it finds a type of error mentioned in error_reporting setting then add it to the output of the script. This basically means to show the errors in web browser.

display_startup_errors 

Default value of this setting is ‘Off’. This tells whether to display errors that occur in PHP’s startup sequence. PHP manual recommends turning this ‘On’ only in your development environment as an aid for debugging.

log_errors 

Default value of this setting is ‘Off’. This setting tells whether errors should be logged in web server’s error log file. When you finish your application and put it live, it’s a good practice that you turn ‘Off’ display_errors and turn ‘On’ log_errors in your production server’s php.ini file.

This prevents users seeing any sensitive information that can go with error reports and let you still see them via web server’s error log. But in development, you would rather like to see errors on the web browser and would set the settings other way around.

error_reporting 

This setting tells what type of errors should be displayed and/or logged. There are constants that can be given as values to this setting and there is single constant (E_ALL) that represents all error types except one (E_STRICT). E_STRICT error type was added in PHP5 and is not that common. According to PHP manual, idea of introducing this type is to encourage you to use latest methods of coding. Thus it can warn you about using deprecated functions.

Default value of this setting is like below.

Above it instructs to discard errors fall into notices category. For an example, if you used an undefined variable in an echo() statement, PHP generates a notice. This can be a useful feature in debugging. Think that you defined a variable as $name but mistyped it in the echo() statement as $nmae then PHP interpreter would let you know it. So, make sure you enable notices in your development environment by using just E_ALL as below.

As mentioned above, E_ALL doesn’t include E_STRICT. So, if you want to instruct PHP interpreter to report all possible types of errors then you can set the setting as below.

From PHP version 6, E_ALL will also include E_STRICT. These constants have corresponding integer values. PHP manual instructs to use those integer values when you set error levels in anything other than php.ini like Apache httpd.conf.

HTML Date Select Menus Using PHP range()

Here is an easy way to create HTML select menus for Month/Day/Year.

Create three arrays containing the range of values for days, months, years.  Then loop over each array generating the required option tags for each select menu.

 

Create a Quick Random PIN using PHP

The quickest method for generating a unique PIN number of 10 characters in length.

We are taking the smallest part of micro-time (milliseconds), encrypting it with MD5, then shuffling the string itself. This creates a very random blend of letters and numbers.

 

PHP5 bug fix when force downloading .doc .docx files

There appears to be a problem with PHP5 when performing a forced download of a WORD file having the extension .doc or .docx from a web page. Adding ob_clean() and flush() prior to reading the file seems to have resolved this problem. Add the following lines before reading the file from disk.

 

Process PHP within HTML Markup Pages

The way to execute PHP on a .html page is to modify your .htaccess file. This file may be hidden, so depending upon your FTP program you may have to modify some settings to see it. Then you just need to add this line for .html:

Or for .htm

If you only plan on including the PHP on one page, it is better to setup this way:

This code will only make the PHP executable on the yourpage.html file, and not on all of your HTML pages.

Include PHP Classes Auto-Magically

This has to be one of my favorite time savers.

If you’re an experienced PHP programmer you separate your common methods (aka: functions) into Class files.  Over time, this can become an organizational nightmare if your application became large.

There is an easy fix for this using PHP’s spl_autoload_register.  This not so well known function is called each time a Class is instantiated.  We can register our own functions to process the name of the Class being called, and easily include that Class in real-time.

Using this approach could not be any simpler.

Assuming you have included the SPL_AUTOLOADER scripts first, instantiating myClass() will auto-magically load the Class file myClass.php from the defined directory set in the myAutoloader() function.

It’s so simple you’ll ask yourself why you have not used this in the past.

Strip UTF-8 characters invalid for use in XML markup

When you put UTF-8 encoded strings into an XML document you should remember that not all UTF-8 characters are accepted in an XML document http://www.w3.org/TR/REC-xml/#charsets

You should strip away the invalid characters, else you’ll have an XML fatal parsing error tossed during parsing.

It’s rather easy to accomplish this using the following function.

 

Using PHP Context Streams to FTP files onto another server.

Here I’m going to show you how to use PHP streams to connect to an FTP server, create a new file and place content into this file. We also need an additional stream context telling PHP to overwrite the file if it exists.  A common stumble I see in scripts like this all the time.

You could also assign to the $content value the entirety of a file on your server using file_get_content() or perhaps a generated CVS or JSON or XML data file.

Auto-Loading PHP Script Widgets

The architecture for an application I created for a client required a method to auto-load a set of widgets (aka: individual PHP scripts) located in the “widgets” directory on the server.  This solution worked perfectly.

Of course, you can enhance this script by refactoring it into a function and adding additional filters to allow only certain types of files to load, like those prefixed with “widget-“.

Create Table Columns from an Array of Data using PHP

Recently I needed to take a list of URL’s from a database and present them in an HTML table.  I needed to split the list into an equal number of list items per column in the table.

The PHP script function below accepts a single array of items which could be URL’s, words, phrases, or some other text and a value for the number of columns your table should contain.