php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #63204 Unreadable example code, replacement enclosed
Submitted: 2012-10-03 07:45 UTC Modified: 2013-01-16 22:59 UTC
From: henry at henrysmith dot org Assigned: googleguy (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: henry at henrysmith dot org
New email:
PHP Version: OS:

 

 [2012-10-03 07:45 UTC] henry at henrysmith dot org
Description:
------------
---
From manual page: http://www.php.net/function.array-udiff#refsect1-
function.array-
udiff-examples
---

This is my case for replacing this example code:

It uses a class when there is no need to, and then to make matters worse the 
class is 
named "cr" with no explanation as to what this means. All-lower-case class names 
go 
against prevailing PHP code standards too.

The class only has one single attribute, and it's poorly-named. A name like 
"priv_member" tells us nothing that the bare syntax doesn't already tell us.

The class' constructor is in the old "repeat the class name" style, and accepts 
another 
badly-named parameter: $val. Again, every variable is a "value" so this tells us 
nothing about what's going on.

The static comparator method is called "comp_func_cr". Presumably this means 
"Compute 
function CR". Again, we know it's a function from the syntax, we know it'll 
compute 
something because that's what functions *do*, and we know it's in the "cr" class 
because we're looking at it right now in the "cr" class.

Inside the comparator, usage of whitespace is so randomly varied as to be 
confusing to 
the reader, and thanks to all the long "priv_member" attributes it's difficult 
to see 
what's actually being compared and returned.

The instantiation code that creates arrays of "cr" objects is arranged into 
over-long 
lines, and although it defines keys for the array elements, it is not arranged 
into a 
sensible visual order from 0 to 2.

A proposed replacement is enclosed as the "test script". Its output is enclosed 
as the "actual result". The code executes the same and produces the same output 
as the existing example, but without any of the issues mentioned above.

Test script:
---------------
<?php                                                                                                                    
                                                                                                                         
$one = array(                                                                                                                                          
        '0'   => 23,                                                                                                     
        '0.1' => 9,                                                                                                      
        '0.5' => 12,                                                                                                     
        '1'   => 4,                                                                                                      
        '2'   => -15,                                                                                                    
);                                                                                                                       
                                                                                                                         
$two = array(                                                                                                            
        '0'   => 3,                                                                                                      
        '0.2' => 9,                                                                                                      
        '0.5' => 22,                                                                                                     
        '1'   => 4,                                                                                                      
        '2'   => -15,                                                                                                    
);                                                                                                                       
                                                                                                                         
$result = array_udiff($one, $two, function($a, $b) {                                                                     
        if ($a === $b) {                                                                                                 
                return 0;                                                                                                
        } elseif ($a > $b) {                                                                                             
                return 1;                                                                                                
        } else {                                                                                                         
                return -1;                                                                                               
        }                                                                                                                
});                                                                                                                      
                                                                                                                         
print_r($result);

Actual result:
--------------
Array
(
    [0] => 23
    [0.5] => 12
)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-16 22:58 UTC] googleguy@php.net
Automatic comment from SVN on behalf of googleguy
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=329171
Log: Provided some more practical examples for array_udiff. Also fixes bug #63204.
 [2013-01-16 22:59 UTC] googleguy@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: googleguy
 [2013-01-16 22:59 UTC] googleguy@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 09:00:02 2026 UTC