Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialŁukasz Czuliński
8,646 PointsAdding two variables returning 0. Not sure what's happening.
This is probably something painfully simple that I'm missing here. My browser correctly displays the $pStake and $pCash echo as seen below, but for some reason $thisPlayer is displaying 0. What am I doing wrong here?
$pStake = totalStakes('seat1');
echo "<br>";
echo $pStake;
echo "<br>";
$pCash = ctp('seat1');
echo "<br>";
echo $pCash;
echo "<br>";
$thisPlayer = $pStake - $pCash;
echo "<br>";
echo $thisPlayer;
echo "<br>";
?>
Which is giving me an output of:
479.25
36.29
0
Instead of:
479.25
36.29
442.96
4 Answers
Shawn Gregory
Courses Plus Student 40,672 PointsLukasz,
After looking at your functions, I realize that you are echoing out from within the function instead of returning. Get rid of the echo $total from both functions and, instead, put return $total after the while loop in the functions. The way you have it now, the echoing of the totals are coming from within the function and are not getting assigned to $pStake and $pCash. Once you change both functions the script should start working.
Cheers!
Shawn Gregory
Courses Plus Student 40,672 PointsHello,
I don't know why this would fix it but you can give this a try:
$thisPlayer = intval($pStake) - intval($pCash);
This just makes sure that the numbers you had outputted before are in fact numbers. Try this and check to see if you still get a 0.
Cheers!
Łukasz Czuliński
8,646 PointsThanks for the reply. I didn't know about intval().
Unfortunately it's still returning 0. So bizarre.
Shawn Gregory
Courses Plus Student 40,672 PointsLukasz,
I copied your code and (since I didn't have the functions) put the values that the functions return and tried the script out and it gave the proper response which is telling me that it has something to do with your functions. Do you think you can provide me with the functions so I can check your syntax. As far as what you are providing here, it all works as intended.
Cheers!
Łukasz Czuliński
8,646 PointsYeah, I tried the same thing so you are right about it likely being in the functions.
Here they are. PHP noob here so I'm expecting it to be something obvious!
BTW, how do you get the nicer formatting? I did ```php with three more backticks at the bottom and get that ugly grey wall of code.
function totalStakes($player){
global $con;
$stakeQuery = 'SELECT SUM(stake) FROM tally WHERE stakeholders LIKE "%'.$player.'%"';
$query = mysqli_query($con, $stakeQuery);
while ($row = $query->fetch_assoc()) {
$total = $row['SUM(stake)'];
echo $total;
}
}
function ctp($player){
global $con;
$stakeQuery = 'SELECT SUM(payout) FROM tally WHERE stakeholders LIKE "%'.$player.'%"';
$query = mysqli_query($con, $stakeQuery);
while ($row = $query->fetch_assoc()) {
$total = $row['SUM(stakeholders)'];
echo $total;
}
}
And my original variable declarations to keep scrolling to a minimum.
$pStake = totalStakes('seat1');
echo "<br>";
echo $pStake;
echo "<br>";
$pCash = ctp('seat1');
echo "<br>";
echo $pCash;
echo "<br>";
$thisPlayer = $pStake - $pCash;
echo "<br>";
echo $thisPlayer;
echo "<br>";
Łukasz Czuliński
8,646 PointsŁukasz Czuliński
8,646 PointsAhhhhh yes, of course! Completely logical. It was staring me right in the face despite going over it a million times. Thanks so much for all the help. It works perfectly now.