This being my first post, I'd just like to say hello to everyone. I look forward to being part of the community and hope I can contribute to it later down the road.
Just to throw out some history, I'm coming from ASP and .NET and am looking forward to adding PHP to my arsenal because its awesome!
Alright, the problem I'm having is this:
The code below is a virtual fish tank. It generates some random coordinates for our fish to move around in. This is a really basic and pointless program but I'm getting myself familiar with PHP syntax.
The problem is that the X, Y coordinates come out to be the same once the program runs its course. Attached is some sample data to show you what I mean.
Using Grid Size: X:100, Y:100
Using Fish Count: [3]
Starting locations: Fish 0 XY=82,82
Starting locations: Fish 1 XY=25,25
Starting locations: Fish 2 XY=28,28
Aquarium Hop #: 0
Fish [0] moves from: 82,82=>2,2;
Fish [1] moves from: 25,25=>98,98;
Fish [2] moves from: 28,28=>61,61;
Aquarium Hop #: 1
Fish [0] moves from: 2,2=>44,44;
Fish [1] moves from: 98,98=>87,87;
Fish [2] moves from: 61,61=>13,13;
Aquarium Hop #: 2
Fish [0] moves from: 44,44=>98,98;
Fish [1] moves from: 87,87=>4,4;
Fish [2] moves from: 13,13=>9,9;
I narrowed the problem down to one line of code and cannot for the life of me figure out what exactly the problem is. Here is the code, I have attached comments around the problematic section.
<HTML>
<Body>
<?php
srand((double)microtime()*1000000);
class objFish
{
var $x;
var $y;
}
echo "Welcome to my Fish Simulation.<br>";
echo "Version 1.0<br>";
echo "------------------------------<br>";
$intMaxAquaHops = 25; //Determines how many aqua loops the simulation goes through.
$intFishCount = 3; //Determines how many fish are simulated.
$intGridSizeX = 100;
$intGridSizeY = 100;
$arrFish = array($intFishCount);
$i = 0;
$j = 0;
/*-- Init Aquarium -- */
echo "Using Grid Size: X:" . $intGridSizeX . ", Y:" . $intGridSizeY . "<BR>";
echo "Using Fish Count: [" . $intFishCount . "]<br><br>";
for($i = 0; $i < $intFishCount; $i++)
{
$arrFish[$i] = new objFish();
$arrFish[$i]->$x = rand(1, $intGridSizeX);
$arrFish[$i]->$y = rand(1, $intGridSizeY);
echo "Starting locations: Fish " . $i . " XY=" . $arrFish[$i]->$x . "," . $arrFish[$i]->$y . "<br>";
}
for($i = 0; $i < $intMaxAquaHops; $i++)
{
echo "<br><br><br><b>Aquarium Hop #: " . $i . "</b><br>";
for($j = 0; $j < $intFishCount; $j++)
{
echo "Fish [" . $j . "] moves from: " . $arrFish[$j]->$x . "," . $arrFish[$j]->$y . "=>";
//----------------------PROBLEM CODE------------
$arrFish[$j]->$x = rand(1, $intGridSizeX); //this line should make X random
//echo $arrFish[$j]->$x . "<----<br>"; //If you echo X at this point it will be ok.
$arrFish[$j]->$y = rand(1, $intGridSizeY); //Problem line, if you echo X after this line of code it will change to Y
//echo $arrFish[$j]->$X . "<----<br>"; //X is now Y after the above line is executed
echo $arrFish[$j]->$x . "," . $arrFish[$j]->$y . ";<br>"; //Writes X,Y as Y, Y
//----------------------END PROBLEM CODE-----------
}
}
?>
</Body>
</HTML>
If you have any other questions lets hear em!
Thanks for your help!