Jump to content
dardime

store echo value on a variable

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!

Share this post


Link to post
Share on other sites

Uh, Which line is not working as it is supposed to? Your question is a bit unusual and nothing in your code is.

Share this post


Link to post
Share on other sites

Hi Thank you for your reply.

 

Here is the code im fetching but it is empty

$str = $row_product["rrp"];

If i will put a static value it will work

echo "80";

Share this post


Link to post
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);

Share this post


Link to post
Share on other sites

Hi

There is a value into it. I fetch it from db i think i dont need to show that anymore

echo $row_product["rrp"];

The value of that is 80.

Edited by dardime

Share this post


Link to post
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.

Share this post


Link to post
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>';

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×

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.