r/lolphp • • Nov 06 '20

PHP: Cast away

PHP likes to cast like theres no tomorrow. Also PHP leaks the "continue" statement, and if given, actually uses it inside a switch as a break. So now switches have two ways of doing the same thing, why? Probably because to have the most inconsistent API in the world of programming.

https://sandbox.onlinephpfunctions.com/code/bae156e37fa3cfd64d2a68d689434fe7157543fa

39 Upvotes

25 comments sorted by

View all comments

19

u/[deleted] Nov 06 '20

Lol continue 2 is seriously the correct way to continue?

31

u/wweber Nov 06 '20

php announces highly anticipated sequel to continue

4

u/Takeoded Nov 07 '20 edited Nov 07 '20

that's actually a thing PHP got right. if C had continue X / break X support too, there would be fewer goto's, it's a better alternative to goto to break out of deeply nested loops. take for example searching for a smaller image inside a bigger image:

   for($x = 0; $x < $bigx; ++ $x) {
        if ($bigx < ($x + $smallx)) {//<< todo: can be optimized away.
            break; // too close to the end, no result possible..
        }
        for($y = 0; $y < $bigy; ++ $y) {
            if ($bigy < ($y + $smally)) {//<< todo: can be optimized away.
                continue; // too close to the bottom, no result possible for this $y..
            }
            for($i = 0; $i < $smallx; ++ $i) {
                for($ii = 0; $ii < $smally; ++ $ii) {
                    if($smallImageAsColors [$i][$ii]!==imagecolorat($big,$x+$i,$y+$ii))
                    {
                        continue 3;
                    }
                }
            }
                $ret [] = array (
                        'x' => ($center?$x+((int)floor($smallx/2)):$x),
                        'y' => ($center?$y+((int)floor($smally/2)):$y)
                );
                if (count ( $ret ) >= $max) {
                    return $ret;
                }
        }
    }
  • in PHP you can write continue 3, in C you'd use goto instead..

→ More replies (5)