Aristokrat
Aristokrat

Reputation: 43

Can I use variables from included files?

Is it possible to calculate percentage from values inside two included php files?

Something like that:

$percentage = (dash.php/dash1.php)*100;
echo $percentage

Where dash.php prints 1600 and dash1.php prints 1200.

dash.php:

$rezultat = "SELECT sum(vrednost) as vrednost FROM vrednosti WHERE username = '$username' AND cas between '".date("Y-m-01")."' AND '".date("Y-m-31 23:59:59")."' ";
$result = mysqli_query($link, $rezultat) or die (mysqli_error($link));

$sestevek = mysqli_fetch_object($result);

//This would normally print 1600
echo number_format($sestevek->vrednost, 2, ",", "");

Upvotes: 1

Views: 32

Answers (2)

frankle
frankle

Reputation: 141

Instead of echo number_format($sestevek->vrednost, 2, ",", ""); you want to assign it to a variable like so:

$dash = number_format($sestevek->vrednost, 2, ",", "");

Do the same in dash1.php but assign to something like dash1:

$dash1 = number_format($sestevek->vrednost, 2, ",", "");

Then in the file that includes dash and dash1:

include 'dash.php';
include 'dash1.php';

Now you can use your variables $dash, $dash1 as if you were inside dash.php or dash1.php.

$percentage = ($dash/$dash1)*100;

Having said that: I think you should look at putting the code into a function of some sort as including the files and using the variables could lead to clashes and confusion later on. It would be much safer to code like this:

$dash = getDashValue();
$dash1 = getDash1Value();

Hope that makes sense :)

Upvotes: 2

norlesh
norlesh

Reputation: 1861

Assuming you wanted to do it from inside a third script (as opposed to doing it from the command line) you could call each script using the php exec function and then do what ever you needed with those results.

Upvotes: 0

Related Questions