how do i force a variable to be int

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

how do i force a variable to be int

Post by pedrotuga »

Code: Select all

int $half=mysql_num_rows($result)/2;
can anybody help me please... i want to use the $half so i can know when am in the midle of the output so i can insert another line on a html table...

the above code generates this error...

Code: Select all

Parse error: parse error, unexpected T_VARIABLE in...
the manual doesnt say so much about variable types

thx in advance
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

You were VERY close.

Code: Select all

(int) $half=mysql_num_rows($result)/2;
(int) casts the variable to an int.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Re: how do i force a variable to be int

Post by bdlang »

pedrotuga wrote: the manual doesnt say so much about variable types
PHP Manual: Types
PHP Manual: Integers
PHP Manual: Type Juggling

You can cast type using (int), e.g.

Code: Select all

$half=mysql_num_rows($result);
$output= (int) $half / 2;
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

thank you everybody
i still have some C habits
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

What are you going to do if mysql reurns an odd number?
Post Reply