PHP TutorialLesson 11Free preview
PHP Casting
This lesson is part of PHP · All levels
Summary
Casting is a method of manually changing the data type of a value by writing the target type in front of the value, for example (int) or (string). This lesson describes the casting results from text to numbers, including cases where some characters are successfully read. Casting is useful, but it doesn't replace data validation.
Key points
- Casting is written with type in square brackets, example (int) $value
- (int) "42abc" makes 42 because the number section reads
- (int) "abc" makes 0 because there's no number at the beginning
- Casting is temporary and does not change the original type of variable
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.