PHP Tutorials

Install LAMP on Ubuntu 12.04 (Linux, Apache, MySQL, PHP)

In this tutorial we will look at how to install Apache, MySQL, and PHP on an Ubuntu 12.04 system using the command line. This set of packages together is called a LAMP server or a LAMP stack. The LAMP stack is the most common form of software stack used on web servers today.

Apache is used to serve web pages, PHP works with Apache to enable you to create dynamic web pages, and MySQL works with PHP to enable it to communicate with a database backend. Note that any of these packages can be installed separately but for running a dynamic website, they are usually installed together.

Compare dates using PHP

In this tutorial, we will use a PHP date compare method to determine which of two (or more) dates is earlier or later. This is extremely handy in various applications where we want to know which of a set of dates came first, for example, in a scheduling or ordering application.

Install GD on Ubuntu Linux

In this tutorial, we will look at how to install the GD image processing library for PHP5 on Ubuntu Linux. The GD library is a very popular module which allows dynamic image processing from within PHP.

The process for installing GD for use with PHP5 on Ubuntu outlined below works for Ubuntu 10.04, Ubuntu 10.10, Ubuntu 11.04, Ubuntu 11.10, Ubuntu 12.04, and on Debian as well.

To install the GD library, run the following command with superuser privileges:

apt-get install php5-gd

Use PHP to redirect to another page

PHP redirects are useful as they allow you to transparently send a user to a different page. For example, you could want to redirect a user to the home page after they have successfully logged out. There is the option of using meta refresh or some Javascript in your markup to perform the redirection, but the process is smoother and more liquid when it is done on the server side.

PHP Convert String to Int

In this PHP tutorial, you will learn how to convert a PHP string to int. Depending on what calculation you will be doing with a variable, it may be important that it is of the particular type of integer.

For example, suppose you were processing (from a submitted form) a vote which was from 1 to 10; you would ideally want this data stored in a numeric variable rather than as a string which the $_GET or $_POST array would provide it to you as.

Converting a string to int in PHP is quite easy using casting as we will see below.

PHP set timezone

PHP comes with a dedicated configuration setting for the timezone that it is supposed to use. On both Windows and Linux this setting usually comes blank and PHP defaults to using the system timezone when doing calculations using the date function for example.

On both Windows and Linux systems, the configuration to set the timezone is in the main php.ini file.

PHP Array Size

PHP array size is important when we will be manipulating the contents of our array. By virtue of us using an array to store our variables in the first place, it usually means that we plan to operate on them as a unit.

Since we usually use for loops to process arrays, we need to know the array size so that we can know when to end the loop.

The following tutorial on the PHP array size explains.

PHP Random - Generate random Numbers and Strings

PHP random numbers and strings are easily generated using the PHP rand function. This function can be made to generate pseudo-random numbers within certain boundaries based on the arguments passed to it. Have a look at our PHP random tutorial below.

Note: We might use the term "random" throughout this tutorial, but kindly note that what the 'rand' function generates is pseudo-random numbers.

PHP Mail Function - Sending E-mail with PHP

The PHP mail function is what we use if we want to send email using a PHP script. This function is appropriately named mail, and when invoked with the right parameters, it allows us to send an email fairly easily.

For the PHP mail function to work properly, PHP itself must be configured to use a mail transfer agent. We will look more at that below. Right now let's get into the syntax of the PHP mail function.

The syntax for the simplest form of the PHP mail function is as follows:


Delete directory recursively using PHP

In this PHP tutorial, we will learn how to use PHP to delete a directory and any files which may be contained in the directory. The rmdir command is used to tell PHP to remove a directory, but this only works on empty directories. If we want to remove a directory which contains subdirectories (or subfolders if you like) and files in these subdirectories we need to write our own function which gets a bit more involved.