Bifalgorithm post · Computer science one · Type Juggling

How does PHP handle different variable types in arithmetic expressions?

How does PHP handle different variable types in arithmetic expressions? (Computer science one · Type Juggling) — "The expectations of an arithmetic expression…

Ideas in this flowchart

  • Comportement des expressions arithmétiques PHP selon les types des variables
  • Avec des entiers ou des nombres flottants, le résultat attendu est la somme, le produit, etc.
  • Le typage dynamique de PHP permet à une expression arithmétique de recevoir des valeurs de types variés.
  • Une valeur booléenne true est traitée comme 1 et false comme 0.
  • Un tableau utilisé dans une expression arithmétique provoque généralement une erreur fatale.
  • Un objet non nul est traité comme 1, tandis que null est traité comme 0.
  • PHP autorise toutefois l’addition de deux tableaux, ce qui produit leur union.
Feed/

How does PHP handle different variable types in arithmetic expressions?

No cover

Channel

Computer science one

1 page

"The expectations of an arithmetic expression involving two variables that are either integers or floating point numbers are straightforward. We expect the sum/product/etc. as a result. However, since PHP is dynamically typed, a variable involved in an arithmetic expression could be anything, including a Boolean, object, array, or string. When a Boolean is involved in an arithmetic expression, true is treated as 1 and false is treated as zero. If an array is involved in an expression, it is usually a fatal error. Non-null objects are treated as 1 and null is treated as 0. PHP does allow you to “add” two arrays together which results in their union."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.