PHP Archives

How to install ZEND Framework for use with MAMP

If you have MAMP installed and working on your Mac, it’s easy to get started with ZEND Framework development. I’ll show you how in this article. Download the framework from here: http://framework.zend.com/downloads/latest Choose the full version without ZEND Server (not necessary as we’re using MAMP). Unpack the download and put it somewhere safe. I’m adding … Read more

How to send an email in PHP

Many complex things are extremely simple in PHP – sending mail is one of them. Here’s how: // components for our email $recepients = ‘you@somewhere.com’; $subject = ‘Hello from PHP’; $message = ‘This is a test mail.’; // let’s send it $success = mail($recepients, $subject, $message); if ($success) { echo ‘Mail added to outbox’; } … Read more

How to test if a Shell Command can be executed in PHP

Before we execute a shell command from PHP it’s a good idea to test if the server will respond to it. We can do this by making use of the empty() function. The following example consists of a helper function you can call before executing the command in question. Then we call it with the … Read more

How to read the contents of a file into a string in PHP

file_get_contents() can help us here. It reads a file and stores it in a string. Can be used with local files, as well as online content: $myLocalFile = file_get_contents(‘myfile.html’); $myOnlineFile = file_get_contents(‘http://wpguru.co.uk’); echo myOnline File; // would display the actual website This is different from the file() function which reads the contents of a file … Read more

How to replace text inside a string in PHP

If we have a string and would like to replace a portion of it, we can use the str_replace() function in PHP. It works like this: str_replace (‘whatToReplace’, ‘theReplacement’, ‘originalText’); It’s easy to remember… but I always get confused when I look this up in the manual. Here’s an example: $originalText = “Now is the … Read more

How to convert a timestamp into a readable date in PHP

A UNIX timestamp is something like a 10 digit (or less) integer number which represents the elapsed seconds since the 1st of January 1970 (also known as the Unix Epoch). It’s a very accurate representation of time, but not necessarily something us humans easily understand. A Unix Timestamp looks something like 1367805780. We can use … Read more

How to find the directory of your WordPress Theme in PHP

To get the directory of the current theme (or child theme) you can use get_stylesheet_directory_uri(). Here’s how to use it. Let’s assume that your WordPress installation lives in http://demo.com, and that your theme is located in a folder named “my-super-theme”. We can assume then that the full URL that points at http://demo.com/wp-content/themes/my-super-theme/ The URL will … Read more