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

  • SEARCH
  • Home
  • 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

is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end
In Ruby, I can define a method foo=(bar) : irb(main):001:0> def foo=(bar) irb(main):002:1> p
Taken from Programming ruby 1.9 book: def my_while(cond, &body) while cond.call body.call end end
I've wrote two ruby files for test test.rb: #!/usr/bin/ruby def foo(bar) bar['key'] = 'value'
So I create a class in ruby: class User def initialize end end Now
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
I usually edit RUBY files in VIM. I want the methods(def...end) to fold. Could
My env: ruby-1.9.2-preview3; rails-3.0.0.beta3 class PostFather < ActiveRecord::Base def self.inherited(subclass) end end class Post
In my migration I have: def up MyModel.destroy_all MyModel.create!({:id=>1,:name=>'foo'}) MyModel.create!({:id=>2,:name=>'fooBar'}) MyModel.create!({:id=>3,:name=>'fooNull'}) end because I
I have the following Ruby code: module MyModule class MyClass def self.my_method end end

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.