Jump to content

store echo value on a variable


dardime
Go to solution Solved by Jacques1,

Recommended Posts

Hi I need help to store echo value in php and store on a variable but i get empty result. Thank you 

 <?php
		ob_start();
		$str = $row_product["rrp"];
		echo $str;
		$value = ob_get_contents();		
		$percentToGet = 10;
		$percentInDecimal = $percentToGet / 100;
		$result= $value * $percentInDecimal;
		ob_end_clean();
		echo $result-$value;
?>

Thank you!

Link to comment
Share on other sites

First, the code doesn't make any sense. Why on earth do you echo the content of a variable and capture the output in another variable? Why not just use the first variable?

 

Secondly, you need to learn how to do basic debugging. Obviously your $row_product doesn't contain the data you think it does. So check how it actually looks like:

var_dump($row_product);
Link to comment
Share on other sites

Then what is even the problem?

 

Forget about this crazy output buffering stuff and just use the variables:

$percentage = 0.1;
$result = (1.0 - $percentage) * $row_product["rrp"];

echo 'The result is '.$result.'<br>';

This is basic programming.

 

Your math also seems to be off. When you calculate $result - $value, you get a negative value. I'm fairly sure you want the opposite direction, i. e. 90% of the original value.

Link to comment
Share on other sites

Hi Thank you for the reply. I know its basic programming but WHY it outputs 0 (zero)?

 

 

wbumq8.jpg

 

If i use this code below it will work correctly:

$percentage = 0.1;
$result = (1.0 - $percentage) * 80;

echo 'The result is '.$result.'<br>';
Link to comment
Share on other sites

  • Solution

Do you not understand that there's a difference between the number 80 and the string “$80”? Can't you see the dollar sign? This will fudge up any calculations, because PHP cannot convert that into a meaningful number (so it converts it to 0).

 

You need to repair your database. The price must be stored in a numeric column (e. g. DECIMAL) without any currency symbols. If the currency is always the same, you don't need to store it at all. If there are different currencies, I recommend you store them in an additional column and then add the currency symbol in the application.

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.