Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:20:32+00:00 2026-05-16T11:20:32+00:00

In Ruby def my_func(foo,bar,*zim) [foo, bar, zim].collect(&:inspect) end puts my_func(1,2,3,4,5) # 1 # 2

  • 0

In Ruby

def my_func(foo,bar,*zim)
  [foo, bar, zim].collect(&:inspect)
end

puts my_func(1,2,3,4,5)

# 1
# 2
# [3, 4, 5]

In PHP (5.3)

function my_func($foo, $bar, ... ){
  #...
}

What’s the best way to to do this in PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:20:32+00:00Added an answer on May 16, 2026 at 11:20 am

    Try

    • func_get_args — Returns an array comprising a function’s argument list

    PHP Version of your Ruby Snippet

    function my_func($foo, $bar)
    {
        $arguments = func_get_args();
        return array(
            array_shift($arguments),
            array_shift($arguments),
            $arguments
        );
    }
    print_r( my_func(1,2,3,4,5,6) );
    

    or just

    function my_func($foo, $bar)
    {
        return array($foo , $bar , array_slice(func_get_args(), 2));
    }
    

    gives

    Array
    (
        [0] => 1
        [1] => 2
        [2] => Array
            (
                [0] => 3
                [1] => 4
                [2] => 5
                [3] => 6
            )
    )
    

    Note that func_get_args() will return all arguments passed to a function, not just those not in the signature. Also note that any arguments you define in the signature are considered required and PHP will raise a Warning if they are not present.

    If you only want to get the remaining arguments and determine that at runtime, you could use the ReflectionFunction API to read the number of arguments in the signature and array_slice the full list of arguments to contain only the additional ones, e.g.

    function my_func($foo, $bar)
    {
        $rf = new ReflectionFunction(__FUNCTION__);
        $splat = array_slice(func_get_args(), $rf->getNumberOfParameters());
        return array($foo, $bar, $splat);
    }
    

    Why anyone would want that over just using func_get_args() is beyond me, but it would work. More straightforward is accessing the arguments in any of these ways:

    echo $foo;
    echo func_get_arg(0); // same as $foo
    $arguments = func_get_args();
    echo $arguments[0]; // same as $foo too
    

    If you need to document variable function arguments, PHPDoc suggest to use

    /**
     * @param Mixed $foo Required
     * @param Mixed $bar Required
     * @param Mixed, ... Optional Unlimited variable number of arguments
     * @return Array
     */
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my ruby (on rails) class looks like: class Foo def self.method1 someAction end def
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
So I've got a Ruby method like this: def something(variable, &block) .... end And
In Ruby, is there a way to do something like class Foo attr_reader :var_name
In Ruby, when I do something like this: class Foo ... def initialize( var
In Ruby, I'm trying to do the following. def self.stats(since) return Events.find(:all, :select =>
What is the Python equivalent of the following code in Ruby? def loop cont=nil
I have the following Ruby script, in which class Foo includes module Baz, module
def double(a) a*2 end method_object = method(:double) and here's my question, how does this
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.