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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:04:06+00:00 2026-05-29T07:04:06+00:00

I’ve been fighting with this PHP-snippet for some hours and I can’t figure it

  • 0

I’ve been fighting with this PHP-snippet for some hours and I can’t figure it out…
How does PHP determines the order when to pass the variables in this snippet:

function myfunction ($left, $right) {
    echo $left;
    echo '<br>';
    echo $right;

}

$a = array ('one', 'two', 'three', 'four');

usort($a, 'myfunction');

In this case, the variables are passed like this:

$left = 'two'   - $right = 'one';
$left = 'four'  - $right = 'two';
$left = 'three' - $right = 'two';
$left = 'four'  - $right = 'three';

But how is this sequence determined?!? I’ve been cracking my head and couldn’t find any logic…

  • 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-29T07:04:07+00:00Added an answer on May 29, 2026 at 7:04 am

    usort uses the quicksort algorithm. You’ll see that if you change your function to return one of the expected values (-1, 0, 1), the results will be different:

    function myfunction ($left, $right) {
        echo $left, ', ';
        echo $right, "<br>";
        if ($left == $right) {
            return 0;
        }
        return ($left  < $right) ? -1 : 1;
    
    }
    
    $a = array (1, 2, 3, 4);
    
    usort($a, 'myfunction');
    
    /* output:
    2, 1
    2, 3
    4, 2
    3, 2
    4, 3
    */
    
    
    /* output when null is returned, as your function does:
    2, 1
    4, 2
    3, 2
    4, 3
    */
    

    From the article:

    Quicksort first divides a large list into two smaller sub-lists: the
    low elements and the high elements. Quicksort can then recursively
    sort the sub-lists.

    The steps are:

    1. Pick an element, called a pivot, from the list.
    2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater
      than the pivot come after it (equal values can go either way). After
      this partitioning, the pivot is in its final position. This is called
      the partition operation.
    3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.