Use PHP functions in JavaScript

JavaScript phpversion

Return the current PHP version

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
function phpversion () {
    // Return the current PHP version  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/phpversion    // +   original by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: We are using this to get the JavaScript version (since this is JavaScript and we can't get the PHP version anyways)
    // %        note 2: The return value will depend on your client's JavaScript version
    // %        note 3: Uses global: php_js to store environment info
    // *     example 1: phpversion();    // *     returns 1: '1.8'
    var xhtml = true,
        s = {},
        firstScript = {},
        d = this.window.document,        c = 'createElement',
        cn = 'createElementNS',
        xn = 'http://www.w3.org/1999/xhtml',
        g = 'getElementsByTagName',
        gn = 'getElementsByTagNameNS'; 
    // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    // END REDUNDANT
     var getVersion = function (app) {
        var att = '',
            minVers = 0,
            versionString = '',
            temp_jsversion = undefined;        if (this.php_js.jsversion !== undefined) {
            return this.php_js.jsversion;
        }
        while (this.php_js.jsversion === temp_jsversion && minVers < 10) {
            temp_jsversion = '1.' + minVers;            if (gn) {
                firstScript = d[gn](xn, 'script')[0];
            }
            if (!firstScript) {
                firstScript = d[g]('script')[0];                xhtml = false;
            }
            if (d[cn] && xhtml) {
                s = d[cn](xn, 'script');
            } else {                s = d[c]('script');
            }
 
            if (app) { // Check with standard attribute (but not cross-browser standardized value?) as per Mozilla
                att = 'type';                versionString = 'application/javascript;version=1.';
            } else {
                att = 'language'; // non-standard
                versionString = 'JavaScript1.';
            } 
            s.setAttribute(att, versionString + minVers);
            s.appendChild(d.createTextNode("this.php_js.jsversion=" + "'1." + minVers + "'"));
            firstScript.parentNode.insertBefore(s, firstScript);
            s.parentNode.removeChild(s);            minVers++;
        }
        return this.php_js.jsversion;
    };
    getVersion(true);    getVersion(false);
 
    return this.php_js.jsversion;
}
external links: original PHP docs | raw js source

Examples

Running

1
phpversion();

Should return

1
'1.8'

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have phpversion goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
qqq
4 May '11 Permalink

q  <?php
// MobGold Publisher Install Code
// Language: PHP (curl)
// Version: MG-20110322
// Copyright MobGold Ltd, All rights reserved

// Parameters to make MobGold request
$site_id = '02f800ffRZNkRa'; // site_id is required to request ads from MobGold
$version = 'MG-20110322';
$test_mode = 0; // Set 1 for test mode, set 0 for live mode

// Optional parameter
$ad_type = ""; // 0 = both, 1 = text, 2 = banner
$ad_lang = ""; // EN, AR, ZZ, EN, FR, DE, ID, IT, JA, PT, RU, ES, TH, OT

/////////////////////////////////
// Do not edit below this line //
/////////////////////////////////

// This section defines MobGold functions and should be used AS IS.

$protocol = 'http';
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') $protocol = 'https';
$tps = isset( $_SERVER["HTTPS"] ) ? $_SERVER["HTTPS"] : '';
$ua = isset( $_SERVER["HTTP_USER_AGENT"] ) ? $_SERVER["HTTP_USER_AGENT"] : '';
$ua = isset( $_SERVER["HTTP_X_OPERAMINI_PHONE_UA"] ) ? $_SERVER["HTTP_X_OPERAMINI_PHONE_UA"] : $ua;
$ua = isset( $_SERVER["HTTP_X_ORIGINAL_USER_AGENT"] ) ? $_SERVER["HTTP_X_ORIGINAL_USER_AGENT"] : $ua;
$ua = isset( $_SERVER["HTTP_X_DEVICE_USER_AGENT"] ) ? $_SERVER["HTTP_X_DEVICE_USER_AGENT"] : $ua;
$xwp = isset( $_SERVER["HTTP_X_WAP_PROFILE"] ) ? $_SERVER["HTTP_X_WAP_PROFILE"] : '';
$pro = isset( $_SERVER["HTTP_PROFILE"] ) ? $_SERVER["HTTP_PROFILE"] : '';
$xwc = isset( $_SERVER["HTTP_X_WAP_CLIENTID"] ) ? $_SERVER["HTTP_X_WAP_CLIENTID"] : '';
$ipr = isset( $_SERVER["REMOTE_ADDR"] ) ? $_SERVER["REMOTE_ADDR"] : '';
$ipx = isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : '';
$ipc = isset( $_SERVER["HTTP_CLIENT_IP"] ) ? $_SERVER["HTTP_CLIENT_IP"] : '';
$ref = isset( $_SERVER["HTTP_REFERER"] ) ? $_SERVER["HTTP_REFERER"] : '';
$hos = isset( $_SERVER["HTTP_HOST"] ) ? $_SERVER["HTTP_HOST"] : '';
$uri = isset( $_SERVER["REQUEST_URI"] ) ? $_SERVER["REQUEST_URI"] : '';
$acp = isset( $_SERVER["HTTP_ACCEPT"] ) ? $_SERVER["HTTP_ACCEPT"] : '';
$cha = isset( $_SERVER["HTTP_ACCEPT_CHARSET"] ) ? $_SERVER["HTTP_ACCEPT_CHARSET"] : '';
$lan = isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ? $_SERVER["HTTP_ACCEPT_LANGUAGE"] : '';

$mg_params = array(
'ua=' . urlencode($ua),
'xwp=' . urlencode($xwp),
'pro=' . urlencode($pro),
'xwc=' . urlencode($xwc),
'ipr=' . urlencode($ipr),
'ipx=' . urlencode($ipx),
'ipc=' . urlencode($ipc),
'ref=' . urlencode($ref),
'hos=' . urlencode($hos),
'uri=' . urlencode($uri),
'acp=' . urlencode($acp),
'cha=' . urlencode($cha),
'lan=' . urlencode($lan),
'pt=' . urlencode("$protocol://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']),
'sm=' . $site_id,
'ver=' . urlencode($version),
'test=' . $test_mode,
'type=' . $ad_type,
'lang=' . $ad_lang,
);

$post = implode('&', $mg_params);
$request = curl_init();
$request_timeout = 5; // 5 seconds timeout
curl_setopt($request, CURLOPT_URL, 'http://ads.mobgold.com/request.php');
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_TIMEOUT, $request_timeout);
curl_setopt($request, CURLOPT_CONNECTTIMEOUT, $request_timeout);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Connection: Close'));
curl_setopt($request, CURLOPT_POSTFIELDS, $post);
$mg_contents = curl_exec($request);
curl_close($request);

if( null !== $mg_contents )
echo $mg_contents;
?>

Gravatar
lala
1 Apr '11 Permalink

q  lala lala lala lala lala


Contribute a New function

More functions

In this category

assert
assert_options
get_cfg_var
get_defined_constants
get_extension_funcs
get_include_path
get_included_files
get_required_files
getenv
getlastmod
ini_alter
ini_get
ini_get_all
ini_restore
ini_set
php_ini_loaded_file
php_ini_scanned_files
» phpversion
putenv
restore_include_path
set_include_path
set_time_limit
version_compare

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !