Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Datatypes Booleans

Ben Os
Ben Os
20,008 Points

What is a "Typecast" in programming in general?

Hampton mentioned the term "Typecast" It is not very clear to me What is a "Typecast" in programming in general? Can you give a primal, very short, most-simple example?

2 Answers

Rasbin Rijal
PLUS
Rasbin Rijal
Courses Plus Student 10,864 Points

Hello Ben,

Type casting is a way to convert a variable from one data type to another data type. Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.

<?php
$foo = 10;   // $foo is an integer
$bar = (boolean) $foo;   // $bar is a boolean
?>

Hope this helps

Brandon McClelland
Brandon McClelland
4,645 Points

Typecasting generally refers to taking one variable type, say a Char, and casting it as another type, maybe an Integer. Typically this is because you have the variable as one type but need to work with it as if it was another type.