From 0c07ad18540e48278db201d67536d018751865ac Mon Sep 17 00:00:00 2001 From: Gabriel Hoverman Date: Sun, 31 Mar 2019 13:35:06 -0400 Subject: [PATCH] Added PHP Type Casting to Guide. (#33762) * Added PHP Type Casting to Guide. * Added PHP Do While Loop to Guide. * Delete index.md * fix: capitalize title --- guide/english/php/type-casting/index.md | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 guide/english/php/type-casting/index.md diff --git a/guide/english/php/type-casting/index.md b/guide/english/php/type-casting/index.md new file mode 100644 index 0000000000..e098b36c8e --- /dev/null +++ b/guide/english/php/type-casting/index.md @@ -0,0 +1,60 @@ +--- +title: Type Casting +--- + +## Type Casting in PHP + +In PHP, variable types are not explicitly declared. Instead, a variables type is determined by what value is assigned to that variable. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, $var becomes an integer. + +As you can imagine, this can cause some issues at times. For example, if I assign a variable the value "123", it is a string, but if I assign it 123, it will be an int. + +Sometimes we want to ensure that a variable is of a certain type, and that is where type casting comes in. It allows us to define the type of a variable to ensure we get the correct value from that variable. + +## Syntax + +To cast a variable to a specific type, simply write the type inside parenthesis in front of the variable. + + +``` + +(bool), (boolean) - cast to boolean
+(float), (double), (real) - cast to float
+(string) - cast to string
+(array) - cast to array
+(object) - cast to object
+(unset) - cast to NULL
+ + +For more information, please see [PHP: Type Juggling](http://php.net/manual/en/language.types.type-juggling.php)