Jump to content

typecast question


Ninjakreborn

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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.