dseguy, French
@dseguy@phpc.social avatar

The code below produces a float, although it seems to produce an integer.

This is due to (int) having higher precedence than multiplication: it is applied to $c, without effect.

https://php-tips.readthedocs.io/en/latest/tips/cast_precedence.html

velkuns,
@velkuns@phpc.social avatar

@dseguy same for ternary operator. That's why explicit things like ( ) is recommended to avoid problems 😅

dseguy,
@dseguy@phpc.social avatar

@velkuns Yes, I can do a long list of those operators... Fun with precedence!

zimzat,
@zimzat@mastodon.social avatar

@dseguy This is why I think cast operators should not have a space after them.

echo (int)$c * 0.12345;

dseguy,
@dseguy@phpc.social avatar

@zimzat It does make it more readable.

paulshryock,
@paulshryock@phpc.social avatar

@dseguy yep, if you wanted an integer, you'd wrap the expression in parenthesis before casting.

$myInt = (int) ($c * 0.12345);

Casting applies to the thing directly after it.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • modclub
  • DreamBathrooms
  • osvaldo12
  • GTA5RPClips
  • ngwrru68w68
  • magazineikmin
  • everett
  • Youngstown
  • slotface
  • rosin
  • mdbf
  • kavyap
  • tacticalgear
  • InstantRegret
  • JUstTest
  • Durango
  • cubers
  • khanakhh
  • ethstaker
  • thenastyranch
  • Leos
  • normalnudes
  • tester
  • cisconetworking
  • anitta
  • megavids
  • provamag3
  • lostlight
  • All magazines