note 125317 added to language.operators.comparison

From: Date: Tue, 01 Sep 2020 14:45:54 +0000
Subject: note 125317 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-215054@lists.php.net to get a copy of this message
So I couldn't find an answer readily anywhere about what happens when using the spaceship operator to sort data that contains null values in the mix so I wrote a test to prove that they are handled as expected and sorted to the top/bottom. $int_test = [8,4,2,null,5,null,12,384]; $str_test = ["foo", "bar", null, null, "zoo", "tam"]; $flt_test = [null, 1.54, 200.2222, 100.1, null, 55.55, 55.56, 55.54]; $icomp = function ($a, $b) { $t1 = intval($a); $t2 = intval($b); return $t1 <=> $t2; }; $scomp = function ($a, $b) { $t1 = strval($a); $t2 = strval($b); return $t1 <=> $t2; }; $fcomp = function ($a, $b) { $t1 = floatval($a); $t2 = floatval($b); return $t1 <=> $t2; }; usort($int_test, $icomp); // [null,null,2,4,5,8,12,384] usort($str_test, $scomp); // [null,null,"bar","foo","tam","zoo"] usort($flt_test, $fcomp); // [null,null,1.54,55.54,55.55,55.56,100.1,200.2222] ---- Server IP: 45.112.84.4 Probable Submitter: 103.51.167.36 (proxied: 97.124.37.214) ---- Manual Page -- http://php.net/manual/en/language.operators.comparison.php Edit -- https://master.php.net/note/edit/125317 Del: integrated -- https://master.php.net/note/delete/125317/integrated Del: useless -- https://master.php.net/note/delete/125317/useless Del: bad code -- https://master.php.net/note/delete/125317/bad+code Del: spam -- https://master.php.net/note/delete/125317/spam Del: non-english -- https://master.php.net/note/delete/125317/non-english Del: in docs -- https://master.php.net/note/delete/125317/in+docs Del: other reasons-- https://master.php.net/note/delete/125317 Reject -- https://master.php.net/note/reject/125317 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215054) next »