Explicit and implict casting

PHP automatically casts strings to integers when doing arithmetic. However, intval($string) and $string + 0 is not the same! The first one works on numbers only, as described under String conversion to numbers in the manual, however, the latter is happy to accept numeric strings which again means that hexadecimal numbers are accepted. Compare print "0xf"+0 to intval("0xf").

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options