Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

PHP functions in JavaScript

Saturday, May 5th, 2012 by Servage

If you’re very familiar with PHP and also use JavaScript, you might find it confusing, yet annoying that some great functions aren’t available in both languages. phpjs.org is a project were you can find JavaScript functions that replicate PHP functions. This is really nice to add missing functionality to JavaScript, which you are used to under PHP.

Example

I’ve picked a random example, namely the array_pop function which is available in PHP and pops and element of the end of an array. This functionality isn’t available in JavaScript, so it’s great you can get it form phpjs:

function array_pop (inputArr) {
    // Pops an element off the end of the array  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/array_pop    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)    // +   input by: Theriault
    // %        note 1: While IE (and other browsers) support iterating an object's
    // %        note 1: own properties in order, if one attempts to add back properties
    // %        note 1: in IE, they may end up in their former position due to their position
    // %        note 1: being retained. So use of this function with "associative arrays"    // %        note 1: (objects) may lead to unexpected behavior in an IE environment if
    // %        note 1: you add back properties with the same keys that you removed
    // *     example 1: array_pop([0,1,2]);
    // *     returns 1: 2
    // *     example 2: data = {firstName: 'Kevin', surName: 'van Zonneveld'};    // *     example 2: lastElem = array_pop(data);
    // *     returns 2: 'van Zonneveld'
    // *     results 2: data == {firstName: 'Kevin'}
    var key = '',
        lastKey = ''; 
    if (inputArr.hasOwnProperty('length')) {
        // Indexed
        if (!inputArr.length) {
            // Done popping, are we?            return null;
        }
        return inputArr.pop();
    } else {
        // Associative        for (key in inputArr) {
            if (inputArr.hasOwnProperty(key)) {
                lastKey = key;
            }
        }        if (lastKey) {
            var tmp = inputArr[lastKey];
            delete(inputArr[lastKey]);
            return tmp;
        } else {            return null;
        }
    }
}
PHP functions in JavaScript, 5.0 out of 5 based on 2 ratings
Categories: Tips & Tricks

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.