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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:55:29+00:00 2026-05-27T08:55:29+00:00

I was toying around with some PHP today, doing what I normally do when

  • 0

I was toying around with some PHP today, doing what I normally do when I try a language I either don’t know or am rusty in: write a small crappy card game.

The first task was to roll out a sorted deck with all the available cards. Here was my (failed) attempt:

<?php

$sorted_deck = array_map(
    function($suit) {
        $card = function($rank) {
            return array(
                'suit' => $suit
                , 'rank' => $rank
            );
        };
        return
            array_map($card, range(2, 10))
            + array_map($card, array('jack', 'queen', 'king', 'ace'))
            ;
    }
    , array('clubs', 'diamonds', 'hearts', 'spades')
);

?>

I’m not sure if this code is even logically correct; I wrote it in a rush. But even if I did want to check it, I can’t because it fails with the following message: ‘Undefined variable: suit’.

There’s only one place where $suit is used, and that’s within the $card function. Given that the $card function is assigned inside the anonymous function that has $suit as its argument, surely it’s accessible, right? Furthermore, it has to be defined, given that PHP’s own array_map function is the one providing the argument from my array of defined values.

I converted function card($rank) { ... into $card = function($rank) { ... thinking that maybe the former makes it global like Perl’s sub does, thus making the inner variable inaccessible. But the latter doesn’t work either, much to my surprise.

Can anyone give me a nudge in the right direction?

  • 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-27T08:55:29+00:00Added an answer on May 27, 2026 at 8:55 am

    You need to capture the $suit variable using the use keyword for the inner closure, as that variable only exists in the scope of the outer function($suit) closure (as its parameter) and not the inner function($rank) closure:

    function($suit) {
        $card = function($rank) use($suit) {
            return array(
                'suit' => $suit
                , 'rank' => $rank
            );
        };
        return
            array_map($card, range(2, 10))
            + array_map($card, array('jack', 'queen', 'king', 'ace'))
            ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just playing around with some PHP and was wondering what happens when an
This question is kind-of crappy because I try to get around some limitations: Current
So I've looked around to try to find some posts on this and there
I am messing around trying to write a small web crawler. I parse out
So I've been doing some digging around and I've been trying to piece together
so I am trying to drag around some images in a canvas. I am
I'm having some fun trying to get my head around some MVP stuf, as
Here, is my code. Just trying to wrap my head around some of the
I am playing around with some recursion, and trying to count the number of
I'm trying to put some specs around a new rails 3 project I am

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.