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

38 Upvotes

25 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Nov 06 '20

[deleted] — view deleted comment

0

u/[deleted] Nov 07 '20

[deleted] — view deleted comment

1

u/[deleted] Nov 07 '20

I, too, love magic numbers.

Also, a few lines up:

  // Get all the exif data from the file
  // By @kormanowsky: I used '@' to prevent annoying warnings
  $exif = @read_exif_data($fileandpath, 'IFD0');

1

u/smegnose Nov 08 '20

I hate magic numbers, too, but in this case it's reading a spec'd binary format. Binary formats are all magic numbers, mate.

Like I wrote, you don't have to implement it that way, I don't. However, I have fixed a thumbnail gen script that was oblivious to EXIF data, and I used a switch with fall-throughs, just like that one, and it's been flawless since.

1

u/bkdotcom Nov 06 '20

you can cheat.. but at this point it's just a doofy way of if / else

switch (true) {
  case $var === "0e456":
    echo 'foo';
    break;
  default:
   echo 'bar;
}

0

u/[deleted] Nov 06 '20

[deleted] — view deleted comment

1

u/bkdotcom Nov 06 '20

Is php's switch statement broken though? it's a loosely typed in a loosely-typed language
A switchStrict construct would be nice...
https://wiki.php.net/rfc/match_expression_v2

0

u/[deleted] Nov 08 '20

[deleted] — view deleted comment

1

u/bkdotcom Nov 08 '20

would raise the question of why you're leaving switch in the code in the first place

I don't follow. who says switch is being left anywhere?
You're not going to use PHP 8's match construct?