1

I have the function get_price_data(); which gives "0,11\u20ac" as return value.

The "0,11" is not static. I want to do some maths with "0,11" from the string below.

My Code shown down below does not work how i want it to. Has someone any idea how i can complete this task ? I am very new to php.

<?php 
function get_price_data() {
    $json = file_get_contents("http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=Chroma%202%20Case");
    $decode = json_decode($json,1);
    echo $decode['median_price'];
}

$string=get_price_data();
$string2 = str_replace("\u20ac","",$string);
echo $string2 * 1000 - 120;
?>
7
  • is 0,11 two values(eg. 0 & 11) or one(eg. 0.11)? also are they supposed to be integers? Commented Mar 3, 2016 at 22:46
  • in germany 0,11 is like your 0.11 ... Sry should have said that before. So this is one value. I dont know if you need to convert that it to an integer. Commented Mar 3, 2016 at 22:54
  • echo floatval(str_replace(',','.',$string)); returns 0.11 (where $string="0,11\u20ac"). This will work as long as your number in $string is followed by some non-numeric value (such as €) Commented Mar 3, 2016 at 23:19
  • @KorreyD it does not work for me. I tried exactly this .. CTRL + C and it just returns "0,11€0"... Commented Mar 3, 2016 at 23:28
  • hmm floatval(str_replace(',','.',"0,11\u20ac")); could not possibly return "0,11€0" because "0,11€0" is a string. worse case if it was "not working" floatval() would return a 0 Commented Mar 3, 2016 at 23:31

2 Answers 2

1
<?php 
function get_price_data() {
    $json = file_get_contents("http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=Chroma%202%20Case");
    $decode = json_decode($json,1);
    return $decode['median_price'];  // return here instead of echo.
}

You are somewhat correct that this return is necessary because this is a function. A return statement is not specifically required in a PHP function (functions will return null by default if there is no explicit return value.) The reason you need it to return is that you are using its returned value in this statement:

$string=get_price_data();

With echo instead of return, $string will be set to null here, and any subsequent operations on it will obviously not do what you intended.

If you change your function to return the value of $decode['median_price'], then $string=get_price_data(); will assign 0,11€ to $string, and then your replacements and calculations should work as expected.

$string=str_replace(array('€','\u20ac'),'',$string);
$string = str_replace(",",".",$string);  // replace , with . as noted by mertizci
echo $string * 1000 - 120;
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Because of the comma in the changed price value, PHP cant do math things on it. You should change the comma with dot too.

Your code should be (edited):

    <?php  
$string=get_price_data();
$string=str_replace(array('€','\u20ac'),'',$string);
$string = str_replace(",",".",$string);  
echo $string * 1000 - 120;
    ?>

4 Comments

Thank you for your answer but this does not work for me. It will give you "0,11€-120".. thats not what i wanted :D
did it work for you ? i also updated the Code above. For me your new Code does not work either ... just copy and pasted it.
@LaurinM.Whats the returned result ?
@LaurinM.Edited again, can you try now with new code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.