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 8015451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:15:46+00:00 2026-06-04T20:15:46+00:00

PHP 5.3 added support for closures, and I was wondering if you can close

  • 0

PHP 5.3 added support for closures, and I was wondering if you can close over a specific array element instead of the entire array.

For example, you can do this:

$a = array('a', 'e', 'i', 'o', 'u');
$f = function($b) use($a){
    echo $a[2].$b;
};
$f('!'); // echos "i!"

But is there a way to only close around $a[2]? Something like this maybe:

$f = function($b) use($a[2] as $c){
    echo $c.$b;
};

This doesn’t work, it gives:

Parse error: syntax error, unexpected ‘[‘, expecting ‘,’ or ‘)’

Obviously, I could do this:

$c = $a[2];
$f = function($b) use($c){
    echo $c.$b;
};

But, I figured there had to be a better way than that. So, is there any way to just close around a specific variable in an array?

  • 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-06-04T20:15:48+00:00Added an answer on June 4, 2026 at 8:15 pm

    As the commentators already mentioned: No.

    At the moment use expects only a T_Variable ($var) or a Reference T_Variable (&$var).
    But there is a patch making the use of as possible like in your example. It can be found on gist.

    Maybe this will be merged into master some time, so that it would be possible in a future release.

    But i really don´t think this is a huge feature 🙂

    So you have to go with your alternate solution in the meantime.

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

Sidebar

Related Questions

am using the application in this link http://www.jeasyui.com/tutorial/app/crud2.php .I have added one more column
In my CMS I've added this code <div><?php include(my_contact_form.php) ?></div> which updates to a
I've just found out that php can be added to css files by adding
I want to configure Kohana 2.x to have links in this format: http://localhost/mysite/web/admin/support instead
it might be a newbie question, but does this PHP 5.4 feature Added class
I've added some php flags in my htaccess in order to change some php
I added a drop down to a PHP contact form which pops up in
I have added a function to functions.php on my theme. function insertAds($content) { $content
I added <script type=text/javasript src=<?=base_url()?>js/jquery-1.7.2.min.js></script> in application/views/templatess/header.php and to post data to Controller using
I have a simple class that I've added to the components folder called mixpanel.php.

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.