Here's my code, fetch_data.php returns a float (an exchange rate). The first echo will return the float that I need (0.65 something) but the second one will return an integer (1)... Why? How do I fix this?
I'm using include because file_get_contents returned PHP for some reason? I don't have a lot of PHP experience and haven't written a line in a month so I'm rusty.
<div class="callout panel">
<p><strong>We sell for:</strong> $<?php
$coin_price = include('fetch_data.php');
$doge = $coin_price;
echo $coin_price;
echo $doge;
?> per 1000</p>
</div>
fetch data
$string = strip_tags($element); //<strong>$484.66</strong>
$string = str_replace('$', '', $string);
$int = $string;
$val = $int * 1.25;
echo $val / 1000;
any help appreciated
You can use
$doge = floatval($coin_price);
Check here : http://www.php.net/manual/en/function.floatval.php
fetch_data.php
$string = strip_tags($element); //<strong>$484.66</strong>
$string = str_replace('$', '', $string);
$int = $string;
$val = $int * 1.25;
$coin_price = $val / 1000;
other php file
<div class="callout panel">
<p><strong>We sell for:</strong>
$<?php
include('fetch_data.php');
echo $coin_price;
$doge = $coin_price;
echo $doge;
?> per 1000</p>
</div>
I modified the code a bit if you want to use include
like you are currently. When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.