9three Posted February 17, 2009 Share Posted February 17, 2009 Hey, $x = $goldMatch[0][1]; $y = $percent; $total = ($x * $y) / 2; echo $total; x has stored 968.00 y is determined by the user's input right now being .15 (or 15%) I need to mutiply that and then divide. It always gives me zero. I did: $total = ($x * 2); echo $total; And it gave me zero as well Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/ Share on other sites More sharing options...
Maq Posted February 17, 2009 Share Posted February 17, 2009 I did this, and it echoed "72.6". $x = 968.00; $y = .15; $total = ($x * $y) / 2; echo $total; ?> It's probably because 'y' should be '$y'. If that's not the case, then your variables aren't holding what you said they were, try echoing them out individually to ensure they have correct values. Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764487 Share on other sites More sharing options...
9three Posted February 17, 2009 Author Share Posted February 17, 2009 yes it was a typo here. I did try to echo them out individual and they are storing properly. echo of x: 968.000 echo of y: .150 echo of $total: 0 Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764490 Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Share Posted February 17, 2009 yes it was a typo here. I did try to echo them out individual and they are storing properly. Well ok, what do you have them stored at? Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764491 Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 $total = (($x * $y) / 2); For number operations, I tend to always encapsulate it in parans like above to avoid weird results that may come of it. Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764492 Share on other sites More sharing options...
9three Posted February 17, 2009 Author Share Posted February 17, 2009 $gold = file_get_contents('http://www.site.com/'); preg_match_all('~<td class="td" align="Right">(.+?)</td>~is', $gold, $goldMatch); The value between the td element has '968.00' Still zero when I added the extra () Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764495 Share on other sites More sharing options...
Maq Posted February 17, 2009 Share Posted February 17, 2009 If you don't mind, could you post all the relevant code...? Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764496 Share on other sites More sharing options...
9three Posted February 17, 2009 Author Share Posted February 17, 2009 I think I found the problem. I'm storing the TD element with the integers so maybe thats why it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764502 Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Share Posted February 17, 2009 I think I found the problem. I'm storing the TD element with the integers so maybe thats why it's not working. That's why I wanted to know what data type you were storing them as. Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764505 Share on other sites More sharing options...
Maq Posted February 17, 2009 Share Posted February 17, 2009 I think I found the problem. I'm storing the TD element with the integers so maybe thats why it's not working. That's why I wanted to know what data type you were storing them as. This isn't Java or C++, this is PHP... It's so loosely typed it doesn't matter as long as the data has real numbers. Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764510 Share on other sites More sharing options...
9three Posted February 17, 2009 Author Share Posted February 17, 2009 I'm trying to remove it but it's not replacing the string instead it's just echoing it out like normal: preg_match_all('~<td class="td" align="Right">(.+?)</td>~is', $gold, $goldMatch); echo str_replace('<td class="td" align="Right">(.+?)</td>', '(.+?)', $goldMatch[0][1]); The output is: <td class="td" align="Right">968.00</td> It should just put 968.00 Well if that's not it, then I'm not sure what it could be. Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764512 Share on other sites More sharing options...
Maq Posted February 17, 2009 Share Posted February 17, 2009 yes it was a typo here. I did try to echo them out individual and they are storing properly. echo of x: 968.000 echo of y: .150 echo of $total: 0 I thought you said they were echoing correctly...? You may want to ask this in the Regex section, since this is what your thread has become. Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764521 Share on other sites More sharing options...
9three Posted February 17, 2009 Author Share Posted February 17, 2009 well they do output correctly. if you view the source though, it has HTML. though i dont know if that matters? its the only thing i can think of, of why is not working right. I got it: <?php function gold() { $gold = file_get_contents('http://www.site.com/'); preg_match_all('~<td class="td" align="Right">(.+?)</td>~is', $gold, $goldMatch); $x = str_replace('<td class="td" align="Right">', '', $goldMatch[0][1]); $y = .15; $total = (($x * $y) / .20); echo $total; } gold(); ?> ?> Looks like PHP can't render integers if there is HTML before the integers that are needed. Anything after the integer doesnt matter. Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764523 Share on other sites More sharing options...
Philip Posted February 17, 2009 Share Posted February 17, 2009 I'm trying to remove it but it's not replacing the string instead it's just echoing it out like normal: preg_match_all('~<td class="td" align="Right">(.+?)</td>~is', $gold, $goldMatch); echo str_replace('<td class="td" align="Right">(.+?)</td>', '(.+?)', $goldMatch[0][1]); The output is: <td class="td" align="Right">968.00</td> It should just put 968.00 Well if that's not it, then I'm not sure what it could be. <?php $gold = '<td class="td" align="Right">968.00</td>'; preg_match_all('~<td class="td" align="Right">(.+?)</td>~is', $gold, $goldMatch); echo str_replace('<td class="td" align="Right">(.+?)</td>', '(.+?)', $goldMatch[0][0]); ?> Outputs 968.00 Quote Link to comment https://forums.phpfreaks.com/topic/145620-solved-always-returning-zero/#findComment-764528 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.