PHP Tips & Tutorials

PHP: Divide and Split an Array into Chunks (Sections) or Several Sub-arrays

A big array is awkward. To divide the big array and split it into smaller sub-arrays or children arrays by size, you can use the PHP function array_chunk():

$sections = array_chunk(array('k', 'l', 'm', 'n', 'o'), 2);

To slice $sections into smaller arrays by the size of 2, and $sections will look like this:

    [0] => Array
            [0] => k
            [1] => l

    [1] => Array
            [0] => m
            [1] => n

    [2] => Array
            [0] => o

PHP Tips & Tutorials

PHP: Change Array Key Case – All Array Keys to Lowercase or Uppercase

While you can easily fabricate your own functions to do this element by element, you can simply resort to the php function array_change_key_case() to do it for you:

$new_array = array_change_key_case($old_array, CASE_UPPER);

Changes all the keys in $old_array to upper case and

$new_array = array_change_key_case($old_array, CASE_LOWER);

Changes all the keys in $old_array to lower case.

PHP Tips & Tutorials

PHP: Create an Array

Array is probably the simplest compound data type every language supports. It’s basically a set of ordered values (text strings, numbers or booleans and even a child array). You can create an array in PHP by:

$companies = array('Microsoft', 'Google', 'IBM', 'Apple');

Now array $companies contains 4 text string values. You can append one more by:

$companies[] = 'Dell';

Or append yet another element that’s itself an array:

$companies[] = array('Wikipedia', 'Skype');

You can use PHP function print_r() to display the contents of an array:


And the contents of array $companies looks like:

    [0] => Microsoft
    [1] => Google
    [2] => IBM
    [3] => Apple
    [4] => Dell
    [5] => Array
            [0] => Wikipedia
            [1] => Skype


Very intuitive. It’s basically a structured storage of a number (maybe a large number) of atomic values.

Also note that each and every of the values are marked with a unique index across the set which can be used to identify the element for change or retrieval. For example, to get the 4th company whose index is 3, you can do this:

echo $companies[3];

And the PHP would print out:


Another approach for creating PHP arrays is to create one element a time. For instance, you will generate a series of values and then assign each of them to an additional element in the array:

$numbers = array(); // creating an empty PHP array
for ($i = 1; $i <= 100; $i++) {
	$numbers[] = $i;

As you may have got it, the array $numbers now contains all the integers from 1 to 100:

    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [99] => 100

You can use array_fill() to form an array with the same values and range() to fill an array with a series of consecutive values.

Apache Web Server Tutorials & Tips HTTP Tips & Tutorials PHP Tips & Tutorials

Apache, PHP: Get Client Browser HTTP Request Headers Information

Every HTTP requests made by any client web browsers to an Apache should conform to the HTTP specification and provide certain set of headers information for the server to parse and understand. Useful headers information that can be retrieved in PHP by function apache_request_headers() includes:

  1. User-Agent: Operating System, browser and its version number, …
  2. Accept-Language: Requesting client language
  3. Accept-Charset: Character set of the client
  4. …

To get an array of the above client request headers information, just use apache_request_headers() function:

$headers = apache_request_headers();

And you’ll get:

    [Host] => localhost
    [User-Agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009042316 Firefox/3.0.10 GTB5
    [Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [Accept-Language] => en-us,en;q=0.5
    [Accept-Encoding] => gzip,deflate
    [Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
    [Keep-Alive] => 300
    [Connection] => keep-alive
    [Cache-Control] => max-age=0

Similarly, you can use apache_response_headers() to get the HTTP response headers information sent from your server to the client.

Apache Web Server Tutorials & Tips PHP Tips & Tutorials

Apache, PHP: Function to Get and Return PHP Version Number and Apache Version

To get the PHP version as well as the Apache version of the current host build, you will need the PHP function apache_get_version():

$ver = apache_get_version();

Sample output:

Apache/2.2.6 (Win32) PHP/5.2.5

Which returns a string containing the Apache version number (2.2.6) as well as that of PHP (5.2.5).

You can also get the PHP version by:

echo phpversion();
Apache Web Server Tutorials & Tips PHP Tips & Tutorials

Apache, PHP: Get Apache Enabled Modules in PHP Dynamically and Detect if a Apache Module is Installed

Sometimes you will need to detect if a certain Apache module is installed dynamically from PHP so as to determine for proper actions to take. For example, if you write inherent functionalities for optional SEO friendly URLs, you will want to know if the client host has the famous Apache module mod_rewrite installed and enabled, or you know it’ll fail.

To get a list (more precisely, an array) of enabled modules in the current Apache installation, run in PHP:

$amods = apache_get_modules();

Let’s display the list:

// output
    [0] => core
    [1] => mod_win32
    [2] => mpm_winnt
    [3] => http_core
    [4] => mod_so
    [5] => mod_actions
    [6] => mod_alias
    [7] => mod_asis
    [8] => mod_auth_basic
    [9] => mod_authn_default
    [10] => mod_authn_file
    [11] => mod_authz_default
    [12] => mod_authz_groupfile
    [13] => mod_authz_host
    [14] => mod_authz_user
    [15] => mod_autoindex
    [16] => mod_cern_meta
    [17] => mod_cgi
    [18] => mod_dir
    [19] => mod_env
    [20] => mod_imagemap
    [21] => mod_include
    [22] => mod_isapi
    [23] => mod_log_config
    [24] => mod_mime
    [25] => mod_negotiation
    [26] => mod_rewrite
    [27] => mod_setenvif
    [28] => mod_userdir
    [29] => mod_php5

You can then use in_array() function to check if a certain Apache module is installed and enabled.

CSS & HTML Tips PHP Tips & Tutorials

PHP: Difference between htmlspecialchars() and htmlentities() Functions

2 functions exist in PHP to convert special characters to HTML entities (a kind of representations defined as in XML for web client such as browsers to recognize and render as special characters such as a spade: â™ , which can be represented as &#9824; in HTML), namely htmlspecialchars() and htmlentities().

But what’s the difference?

The obvious part is htmlspecialchars() only convert 5 special characters that happen to be HTML specific:

  1. &‘ (ampersand) becomes ‘&amp;’
  2. "‘ (double quote) becomes ‘&quot;’ when ENT_NOQUOTES is not set.
  3. ‘ (single quote) becomes ‘&#039;’ only when ENT_QUOTES is set.
  4. <‘ (less than) becomes ‘&lt;’
  5. >‘ (greater than) becomes ‘&gt;’

As you can recognize, all the 5 special characters are HTML reserved ones, so htmlspecialchars() is mostly used at preventing user web client from treating these characters as part of HTML constructs.

On the other hand, htmlentites() tries its best to convert all applicable characters to HTML entity representations including the 5 HTML specific language constructs.

PHP Tips & Tutorials

PHP: Absolute Value Function abs()

A number’s absolute value is the distance from the origin of the axis to itself. In PHP, you can get the absolute value of any numeric value by the help of abs() function:

$a = abs(-19.7); // $a = 19.7
Information Security PHP Tips & Tutorials

PHP: open_basedir in php.ini to Restrict and Limit PHP File Accesses to a Certain Directory

The open_basedir directive in php.ini limits PHP file accesses (such as file opening, writing and deleting) within a designated directory such as /home/www/public_html so that it doesn’t endanger the rest of the system in any way. With proper Apache permissions and PHP installed as an Apache module, PHP inherits whatever privileges Apache has. As Apache is usually endowed with very limited permission in the form of a ‘nobody’ or ‘www-data’ group, there’s actually no need for open_basedir.

So it’s actually turned off by default. Controversies are raised about whether to use it or not. While it’s good to have extra confinement of what your public PHP scripts can access and do, it’d also make your applications reliable on it for file system security.

To modify the value of this directive and restrict php directory access, just find php.ini and locate the line:

;open_basedir = 

And change it to minimum directory access your PHP applications need such as the web documents root:

open_basedir = '/home/www/public_html'
Linux Server Administration Tips PHP Tips & Tutorials

Where is php.ini located?

Well it depends on the Linux distribution you are using, the version of php and the way you install it with Apache web server. Php.ini may be here:


Or here:


Or here:


Anyway, you can always find any file named php.ini in this manner

find / -name php.ini

The simplest yet most powerful usage of the renowned find command. By its help I was able to locate the php.ini on my Ubuntu 9.04 Apache2 and PHP5:


Use phpinfo() to find out

Write a simple PHP file with the following content:

<?php phpinfo();

And upload it to your server and access it in your web browser to get a page like this:

PHP configuration file php.ini path
PHP configuration file php.ini path