Jump to content

[SOLVED] Always returning zero?


9three

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.