Use PHP functions in JavaScript

JavaScript func_get_arg

Get the $arg_num'th argument that was passed to the function

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
30
function func_get_arg (num) {
    // Get the $arg_num'th argument that was passed to the function  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/func_get_arg    // +   original by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: May not work in all JS implementations
    // *     example 1: function tmp_a() {return func_get_arg(1);}
    // *     example 1: tmp_a('a', 'b');
    // *     returns 1: 'a'    if (!arguments.callee.caller) {
        try {
            throw new Error('Either you are using this in a browser which does not support the "caller" property or you are calling this from a global context');
            //return false;
        } catch (e) {            return false;
        }
    }
 
    if (num > arguments.callee.caller.arguments.length - 1) {        try {
            throw new Error('Argument number is greater than the number of arguments actually passed');
            //return false;
        } catch (e2) {
            return false;        }
    }
 
    return arguments.callee.caller.arguments[num];
}
external links: original PHP docs | raw js source

Examples

Running

1
2
function tmp_a() {return func_get_arg(1);}
tmp_a('a', 'b');

Should return

1
'a'

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 func_get_arg 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

No comments yet. Be the first!


Contribute a New function

More functions

In this category

call_user_func
call_user_func_array
create_function
forward_static_call
forward_static_call_array
» func_get_arg
func_get_args
func_num_args
function_exists
get_defined_functions
register_shutdown_function

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 !