Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

typecast question

Recommended Posts

I have a question about typecasting, forcing a variable to have a specific type.
should I get in the habit of typecasting every single variable I create in working with php to what I want it to be no matter what, or is there a bad reason for doing this. I was thinking of every single time I create a variable, I should typecast it, or is that going to make it hard to change variables on teh fly when I decide to change them some as I go, basically either way when are the best reasons, or purposes where typecasting should be used.

Share this post


Link to post
Share on other sites
The need to typecast in PHP is rare as most of the time the processor will do it for you depending on the context in which the variable is used.

The only occasion I can think of immediately is when rounding results of a division.

If you want to show how many pages of results there are and want to have a whole number (int) instead of a float

[code]$total_records = 25;
$recs_per_page = 10;
$pages = (int) ceil ($total_records / $recs_per_page); [/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.