Repair Bad Credit - Marketing - Babb Fest - Mobile Phones - MPAA

PDA

View Full Version : php operators not working correctly for me. Can you spot what I am doing wrong?


exodus
Jan 1st 2008, 3:49 pm
What am I doing wrong?

$rs['count'] is set to "468732" in a function that is returns an array.

$max_results = 25;
$top = $rs['count'];
echo ($top + 1)."<br />";
$numpages = $top / 25;
echo $top. " / ".$max_results." = ".$numpages;
exit;

Output:
1
469190 / 25 = 0

I do not understand what is wrong.

lv211
Jan 1st 2008, 5:16 pm
You're script works when I take out the $top = $rs['count'];

I think I had a similar problem in a script I wrote in perl. I used sprintf. I think it might have to do with the type of variable you are trying to pass.

This works for me.

check out the script here. www.wineryfinder.net/dude.php

$max_results = 25;
$top = 5;
echo ($top + 1)."<br />";
$numpages = $top / 25;
echo $top. " / ".$max_results." = ".$numpages;

exodus
Jan 1st 2008, 7:09 pm
Count is an array. The array assigns it's as an string even though I type cast it as an int. Still shouldn't php change it to int when doing math operations? Instead for some reason it is assigned the var to be a zero instead of the number. Which is wierd, because there is no white spaces in the string and should be read by php like a integer.

Maybe it is just something wrong with my install of php?

PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable $var , $var becomes a string. If you then assign an integer value to $var , it becomes an integer.

Kaizoku
Jan 1st 2008, 7:31 pm
Do this

$top = intval($rs['count']);

This will process the array as an integer if it is not already.

To check if it's an integer do this.

print_r($rs);

This will print all keys, indexes and values of the array $rs;

azizny
Jan 1st 2008, 8:27 pm
$top = intval($rs['count']);
if($top > 0){
$max_results = 25;
echo ($top + 1)."<br />";
$numpages = $top / 25;
echo $top. " / ".$max_results." = ".$numpages;
exit;
}


Peace,

exodus
Jan 1st 2008, 9:33 pm
I found the error. For some reason the count var had xml stuff in it and firefox wasn't showing it. I feel stupid. I changed up the rss scrape routine to do the [1] instead of [0] and now it is grabbing only the ints.