Req #19621 [Com]: Math needs a "sign()" function
Edit report at https://bugs.php.net/bug.php?id=19621&edit=1 ID: 19621 Comment by: php at yopmail dot com Reported by:bill at softky dot com Summary:Math needs a "sign()" function Status: Closed Type: Feature/Change Request Package:Feature/Change Request Operating System: Mandrake linux PHP Version:4.2.0 Block user comment: N Private report: N New Comment: sign is very usefull in usort function exemple : "John","age"=>20], ["name"=>"Jack","age"=>30], ["name"=>"Paul","age"=>25], ] usort($people,function($a, $b){ return sign($a['age'] - $b['age']); }); ?> Previous Comments: [2011-09-26 14:30:22] tom at kera dot name Patrick, adding a new library function _absolutely_ breaks BC. For example: Do you want every script that declares a function `sign` to suddenly not work any more? How would this *not* be a compatibility issue? [2011-04-09 16:51:11] bogdan at moongate dot ro Seconded. I've always rolled my own in PHP, but this is typically used in small loops executed a lot, so moving this logic into the PHP core would probably make a difference. If you're concerned sign() might already be used by various existing scripts, why not implement is as the more math-like sgn()? [2011-02-25 10:14:51] patrick at ibuildings dot nl I also searched for a sign() function and ended up here. But I disagree with Andrey's arguments not to include it into PHP. Since when is it a policy to *not* include a function, simply because it does not exist in a number of other languages? Doesn't PHP have its own 'vision'? Secondly, a new function should not break BC. As Bill stated, it would complement the abs() function and make the Math list a more complete list, even though it's a simple function. It seems a better idea to me to add "sign()" then let's say "goto". [2002-10-03 02:32:28] and...@php.net Quick search showed that there is no well know scripting language that has such function. C++/C# has this but they are not scripting languages. The following code does the same. Also we have to think about BC(backward compatibility) with older scripts. function sign($x){ return (int)((abs($x)-$x)? -1:$x>0); } Thank you for you suggestion. [2002-09-26 13:52:07] bill at softky dot com The wonderful math-function list is missing a very important and simple function: sign(). The is the comlpementof abs(), and together with abs() allows you to separate the magnitude and sign of a number, e.g. for graphing purposes (it's hard to graph a negative number of pixels, and displaying money as "$-99" looks dumb). It's a one-line function, so I've already written my own, but it really ought to be built-in. Thanks! -- Edit this bug report at https://bugs.php.net/bug.php?id=19621&edit=1
Req #46240 [Com]: Build in foreach else support
Edit report at https://bugs.php.net/bug.php?id=46240&edit=1 ID: 46240 Comment by: php at yopmail dot com Reported by:kjarli at gmail dot com Summary:Build in foreach else support Status: Duplicate Type: Feature/Change Request Package:Scripting Engine problem Operating System: * PHP Version:5.2.6 Block user comment: N Private report: N New Comment: foreach(){}else{} is a good id but perhaps need to be improved : this simple example can't be converted to foreachElse (due to ) : --- if(count($data)>0){ echo ""; foreach($data as $item){ echo "$item"; echo ""; } else { echo "No think to see :("; } --- Previous Comments: [2012-09-22 09:30:38] ni...@php.net Closing as duplicate of https://bugs.php.net/bug.php?id=26411. [2011-12-22 20:41:46] jason at valdron dot ca I completely disagree about the onFail section. The foreach else would be useful if there is no item. As a shortcut to if(count($elements) == 0). [2011-07-12 12:13:44] dinumarina at yahoo dot com Opposing general consensus, I think adding such feature would be semantically ambiguous. The foreach function does not fail, it has types it can handle (assoc array, object) and that it can't handle (scalar). A function can always return a result that may be valid for foreach but not the expected format, so I think data sanitizing is best done dilligently. This is just a lazy-man's hack for a non-issue. For the function completion status (onFail), there is already such a construct: try {throw()} catch(){} which already supersets the imagined onFail implementation. Only thing I would hindsight: it would have been SOOO nice if php had a false/null/na result convention, as well as an error/exception convention and it would actually stick by it. Each module signals completion, computability, singular cases and errors as it well pleases. [2011-02-18 01:20:24] ijrbiz at gmail dot com Highly agreed with adding a foreach :: else statement, this request would be very practical for improved coding structure and follows logical language syntax nicely. foreach ($items as $item) { echo $item; // Manage each item, ... } else { echo 'No items present.'; // Manage no items found, ... } [2011-02-04 02:30:00] pedro at worcel dot com foreachelse seems ok to me. Onfail is... weird. :) The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=46240 -- Edit this bug report at https://bugs.php.net/bug.php?id=46240&edit=1
Bug #51184 [Com]: DateInterval has incorrect days property on windows
Edit report at https://bugs.php.net/bug.php?id=51184&edit=1 ID: 51184 Comment by: test dot php at yopmail dot com Reported by:s...@php.net Summary:DateInterval has incorrect days property on windows Status: Wont fix Type: Bug Package:Date/time related Operating System: Windows PHP Version:5.3.2 Assigned To:pajoye Block user comment: N Private report: N New Comment: Still have this bug on the 5.3.5 version more than 2 years later. It is not acceptable to have a "won't fix" decision on a major bug like this that is affecting so many people. Thank you in advance, Previous Comments: [2012-02-06 21:45:11] asdf at asdf dot com Your function can be changed to accept Datetime Objects for Strings this way. Also now calculates datetime difference from dt1 to dt2 rather then the other way around. private function daysdiff($dt1, $dt2, $timeZone = 'America/Chicago') { $tZone = new DateTimeZone($timeZone); if(is_string($dt1)) { $dt1 = new DateTime($dt1, $tZone); } if(is_string($dt2)) { $dt2 = new DateTime($dt2, $tZone); } // use the DateTime datediff function IF we have a non-buggy version // there is a bug in many Windows implementations that diff() always returns 6015 if( $dt1->diff($dt1)->format("%a") != 6015 ) { return $dt1->diff($dt2)->format("%a"); } // else let's use our own method $y1 = $dt1->format('Y'); $y2 = $dt2->format('Y'); $z1 = $dt1->format('z'); $z2 = $dt2->format('z'); $diff = intval($y2 * 365.2425 + $z2) - intval($y1 * 365.2425 + $z1); return $diff; } [2012-02-06 21:25:02] asdf at asdf dot com Also have this problem. A full year since last modified. No fixes yet? I agree with the other use who said there should be more documentation on this error. I am sure lots of us spent lots of time before we found this thread trying to figure out why diff() does not output the correct result. Right here would be a great place to put some documentation on the 6015 error: http://us3.php.net/manual/en/datetime.diff.php [2012-01-29 05:38:54] alabi10 at yahoo dot com The fix submitted by fbast...@yahoo.com on 2011-10-16 16:13 UTC solved the problem for me on Windows 7 running WAMP on localhost and php 5.3.0 [2011-11-12 15:08:05] iskeen at barebrush dot com I am surprised that the number of days between two dates problem is not made clear right up front in the documentation. It took me 3 hours to find this page and I was trying all different things to make it work. I suspected a problem when the three different sets of dates I was using all came out with the same answer, but when I finally used %a and they all came out with 6015, I knew, finally, that there is a problem. The fact of the problem should be made very clear right up front. [2011-10-16 16:13:05] fbastage at yahoo dot com Here's a reasonably close substitute (run result through abs() if you don't want potentially negative numbers): // $dt1 and $dt2 can be any valid date string that DateTime accepts // the time zone shouldn't affect anything (since $dt1 and $dt2 use same zone), // but you can override the default function daysdiff($dt1, $dt2, $timeZone = 'America/Chicago') { $tZone = new DateTimeZone($timeZone); $dt1 = new DateTime($dt1, $tZone); $dt2 = new DateTime($dt2, $tZone); // use the DateTime datediff function IF we have a non-buggy version // there is a bug in many Windows implementations that diff() always returns // 6015 if( $dt1->diff($dt1)->format("%a") != 6015 ) { return $dt1->diff($dt2)->format("%a"); } // else let's use our own method $y1 = $dt1->format('Y'); $y2 = $dt2->format('Y'); $z1 = $dt1->format('z'); $z2 = $dt2->format('z'); $diff = intval($y1 * 365.2425 + $z1) - intval($y2 * 365.2425 + $z2); return $diff; } The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=51184 -- Edit this bug report at https://bugs.php.net/bug.php?id=51184&edit=1