Jump to content


Photo

typecast question


  • Please log in to reply
2 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 18 April 2006 - 05:38 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 18 April 2006 - 07:20 PM

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

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 18 April 2006 - 08:06 PM

thanks for the help.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users