Category: PHP Toggle Comment Threads | Keyboard Shortcuts

  • Jay Versluis 6:35 pm on February 15, 2014 Permalink | Reply
    Tags: , ,   

    Categories: PHP ( 26 )

    How to install ZEND Framework for use with MAMP 

    ZEND_logoIf 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:

    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 mine to my Documents directory. I’ll also rename my folder to something like “ZendFramework” without the version number.

    To access it from anywhere on our machine we’ll create an alias named “zf”. zf is a shell script provided by the framework that we’ll need throughout our development journey with ZEND. Let’s to this in a Terminal session:

    alias zf="/Users/you/Documents/ZendFramework/bin/'

    Replace the path with your own. Notice the call to /bin/ which is the “real” shell script. Our alias has just made this universal and accessible without having to mess with our shell path.

    Verify that it’s working with this:

    zf show version
    Zend Framework Version: 1.12.3

    Read the full article

    • nashivor 6:05 am on February 17, 2014 Permalink | Reply

      Installation Process of Zend Famework using Mamp define in this blog step by step..

  • Jay Versluis 5:54 pm on January 16, 2014 Permalink | Reply

    Categories: PHP ( 26 )

    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 = '';
    $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';
    } else {
    	echo 'That did not work so well';

    The mail function will add the message to the out queue, so the test will not show if the message has actually been sent.

    To avoid really long single line emails (i.e. entire message on one line) we can use the wordwrap() function, causing an automatic wrap to the next line if more than 70 characters are in a single row:

    $message = wordwrap($message, 70, "\r\n"); 

    All tips courtesy of the PHP Manual Pages:

  • Jay Versluis 5:00 pm on January 16, 2014 Permalink | Reply  
    Categories: PHP ( 26 )

    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 shell command we intend to use, before executing the command for real. We’re using ‘uname -a’ here as an example that will generate output and takes a parameter:

    // helper function
    function checkShellCommand($command) {
        $returnValue = shell_exec("$command");
    	if(empty($returnValue)) {
    		return false;
    	} else {
    		return true;
    // test the shell command you'd like to use
    if (!checkShellCommand('uname -a')) {
        print 'This command cannot be executed.';
    } else {
        echo shell_exec('uname -a');

    • Lionel Péramo 9:01 am on December 7, 2014 Permalink | Reply

      function checkShellCommand($command) { return !empty(shell_exec(“$command”)); }
      It’s shorter and …I wonder myself if it’s really needed to have a function for that.

  • Jay Versluis 4:15 pm on January 16, 2014 Permalink | Reply  
    Categories: PHP, Windows ( 26 )

    How to test if your server is running Windows from PHP 

    If we’re executing shell commands via PHP we need to know if the server is running Windows or Linux. Thanks to a magic constant in PHP we can find out like this:

    echo PHP_OS;

    This will give us a single value like

    • Linux
    • Darwin
    • Windows
    • WINNT

    With that info at hand, we can write a function that delivers a boolean result (courtesy of the PHP manual):

    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        echo 'This server is running Windows!';
    } else {
        echo 'This server is NOT running Windows!';

    This takes the output of PHP_OS and looks at the first three characters turned into upper case. If those are identical to WIN it’s Windows – and if not we assume it’s a Linux flavour.

    If you need a more detailed information about your environment consider using php_uname():

    echo php_uname();

    This will give you the above, plus server name, kernel version, release and local server time.

  • Jay Versluis 4:50 pm on November 15, 2013 Permalink | Reply  
    Categories: PHP ( 26 )

    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('');
    echo myOnline File; // would display the actual website

    This is different from the file() function which reads the contents of a file into an array.

  • Jay Versluis 4:35 pm on November 15, 2013 Permalink | Reply
    Tags: str_replace   

    Categories: PHP ( 26 )

    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 time when xxxx should be replaced.";
    $replacement = str_replace('xxxx', 'Something', $originalText);
    echo $replacement;

    This works well if there’s only one occurrence that can be replaced. If you had ‘xxxx’ twice in the original text, both xxxx’s would be replaced by Something:

    $originalText = "Now is the time when xxxx should be replaced with xxxx.";
    $replacement = str_replace('xxxx', 'Something', $originalText);
    echo $replacement;

    This function is cAsE SenSitiVe – you can also use str_ireplace() which will ignore case.

    You can also do it with Regular Expressions (shudders):

  • Jay Versluis 1:30 pm on November 15, 2013 Permalink | Reply
    Tags: date, timestamp   

    Categories: PHP ( 26 )

    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 PHP’s date() function to turn it into something more readable, such as 05/05/2013, or 5th of May, 2013 at 10:03pm. Here’s how:

    	$theTimestamp = 1367805780;
    	echo date('m/d/Y', $theTimestamp);
    	// result: 05/05/2013

    The first parameter tells date() how to format its output – and you can get extremely creative with it:

    	$theTimestamp = 1367805780;
    	echo date('l jS \of F Y h:i:sa', $theTimestamp);
    	// result: Sunday 5th of May 2013 10:03:00pm

    The slightly eerie looking combination of letters and numbers is made up of “real” printable characters (such as the “of” in “5th of May”) and special switches that represent each date component.

    For a full reference and plenty of examples have a look at the PHP Manual:

  • Jay Versluis 9:34 pm on October 9, 2013 Permalink | Reply  
    Categories: PHP, WordPress ( 26 )

    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, and that your theme is located in a folder named “my-super-theme”. We can assume then that the full URL that points at

    The URL will of course be different for every user of your theme, so you can’t hard code this. Instead, you can use this handy function:

    $myThemeDirectory = get_stylesheet_directory_uri();

    This will give you the same URL as above. You can also link to files inside your theme’s directory, for example:

    $myImage = get_stylesheet_directory_uri() . '/images/my-super-image.jpg';

    get_stylesheet_directory_uri() will point to the directory of your theme or your child theme. If you’re using a child theme and want to explicitly link to the parent theme’s directory, you can use get_template_directory_uri().

    From the WordPress Codex:

  • Jay Versluis 4:47 pm on October 9, 2013 Permalink | Reply  
    Categories: PHP ( 26 )

    How to fix “unexpected T_FUNCTION” error in PHP 

    I tested one of my plugins yesterday and was shocked to see a nasty error message:


    What was going on? I could have sworn this worked fine last time I tried! Then I remembered that I changed my PHP Version in MAMP, from 5.5.3 back to 5.2.17. And exactly therein lies the problem, because the code that was amiss was something like this:

    $zendash_updates = function ($a) {
    	return null;

    This is what’s known as an anonymous function in JavaScript, and it didn’t exist in PHP until Version 5.3.

    If we wanted to be backward compatible, we can fix this by writing a function, then adding its output to the variable. It’s the same thing really, just a bit more code:

    function zendash_update_helper($a) {
    	return null;
    $zendash_updates = zendash_update_helper();

    Here’s a thread with an other example:

  • Jay Versluis 6:34 pm on December 29, 2011 Permalink | Reply
    Tags: , , ,   

    Categories: Linux, PHP ( 96 )

    What’s the full path to php in CentOS? 

    The path to PHP is


    This is good to know if you need to setup a cron job which triggers a PHP file. Calling it from a web browser directly is not a problem, but if you have to call it from the command line or as a scheduled task you need to call it with

    /usr/bin/php yourfile.php

    You can also use wget or cURL but that’s often not reliable.

    If your PHP file gives you an output (usually to the browser screen), your server will send you an email. If you;d rather this didn’t happen, direct it to nowhere like so:

    /usr/bin/php yourfile.php > /dev/null 2>&1

compose new post
next post/next comment
previous post/previous comment
show/hide comments
go to top
go to login
show/hide help
shift + esc