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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:00:27+00:00 2026-06-02T07:00:27+00:00

I am new to PHP and I’m trying to convert a simple Javascript function

  • 0

I am new to PHP and I’m trying to convert a simple Javascript function into a PHP function. So far I know to only convert the variables, I need help with the sort function and array filter. Thanks for any help!

var closeRoll = function( number ) {
  var rollArray = [36, 48, 60, 72];
  var closest = rollArray.filter(function(ele){
    return ele - number >= 0
  }).sort()[0];
  return closest;   
};

My half conversion:

function closeRoll( $number ) { 
  $rollArray = Array(36, 48, 60, 72);
  $closest = array_filter( $rollArray, function temp( $rollArray ) {
    return $rollArray - $number >= 0;
  }); //.sort()[0];
  return $closest;   
};
  • 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-02T07:00:32+00:00Added an answer on June 2, 2026 at 7:00 am

    Scoping does not work quite like JavaScript; you can’t simply refer to $number inside your callback function, because it does not have a scope chain.

    You either have to make it global (not recommended but simple):

    <?php
    function doFilter($element) { // this could still be anonymous, but doesn't have to be
       return $element - $GLOBALS['number'] >= 0;
    }
    
    function closeRoll( $number ) { 
       $GLOBALS['number'] = $number;
       $rollArray = Array(36, 48, 60, 72);
       $closest = array_filter( $rollArray, 'doFilter' );
       return $closest;   
    };
    ?>
    

    Or you could utilize the use operator (an elegant solution):

    <?php
    
    function closeRoll( $number ) { 
       $rollArray = Array(36, 48, 60, 72);
       $closest = array_filter( $rollArray, function( $element ) use ($number) {
         return $element - $number >= 0;
       });
       return $closest;   
    };
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm about to start a new PHP project and I'm going to need to
I have old.htm and new.htm on a directory, and I need to load file.php
I'm trying to write a php script that will generate a variety of new
I'm trying to work with Symfony2 with the new PHP 5.4 and its built-in
I'm using the new PHP SDK 3.0 and I use the new getLoginUrl() function
I am trying to pass an array as an argument to the new php
I'm new to PHP. I'm developing a new PHP website. My website folder structure
Has anyone ever modified the post-new.php file in their WordPress installation? I want to
To put it simply i am a fairly new PHP coder and i was

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.