Jump to content

spence911

Members
  • Posts

    38
  • Joined

  • Last visited

Everything posted by spence911

  1. Please ignore the code in my first post. There was a mis-type. Code should read as follows: <?php function multipli(){ global $num; $num = $_POST['fnumber']*$_POST['snumber']; return $num; } echo $num; ?> The line I corrected is this one. It should be: $num = $_POST['fnumber']*$_POST['snumber']; NOT $num3 = $_POST['fnumber']*$_POST['snumber'];
  2. Ok gotcha. I removed the line $num; and I'm still getting the error. I am trying to find the value of $_POST['fnumber'] multiplied by $_POST['snumber']. As far as I know declaring a global variable from within a function will make the variable available outside this function. So why is $num not recognized?
  3. This is a snippet from a simple script that multiplies integers entered into 2 fields on a form (fnumber and snumber). I'm getting an error message that says "Notice: Undefined variable: num". Is this not the proper way to declare a global variable? <?php $num; function multipli(){ global $num; $num3 = $_POST['fnumber']*$_POST['snumber']; return $num; } echo $num; ?>
  4. Yes I did finally get it to work thanks to the edits by Ch0cu3. Awesome script btw, very practical, definitely gonna enjoy it.
  5. Awesome! I think this is a great example to wrap my head around image mapping with. Much appreciated.
  6. I just removed both lines and still not working. All I'm getting is a box with a missing image :[
  7. I was really looking forward to playing with that code Barand. I created a pigeon.php, dropped that image tag into a seperate file, saved it and when i opened it the output it generated was just an empty frame with no image.
  8. @Barand. The code is actually very useful in displaying the map. As is - It outputs a series of 10 dots across the y-axis of the map even if there are no values inside the quotations.
  9. This script is an exercise that i got from a text book. It simulates the path of a homing pigeon as it flies from its starting point to its home. The script is easy to comprehend. The only problem i have is the block of code that displays the map as I've highlighted below. <?php $mapsize = 10; // Position the home and the pigeon do{ $homeX = rand (0, $mapsize-1); $homeY = rand (0, $mapsize-1); $pigeonX = rand(0, $mapsize-1); $pigeonY = rand(0, $mapsize-1); } while ((abs($homeX-$pigeonX) < $mapsize/2) && (abs($homeY-$pigeonY) < $mapsize/2)); do{ // Move the pigeon closer to home if ($pigeonX < $homeX) $pigeonX++; elseif ($pigeonX > $homeX) $pigeonX--; if ($pigeonY < $homeY) $pigeonY++; elseif ($pigeonY > $homeY) $pigeonY--; // Display the current map echo '<div class="map" style="width: ' . $mapsize . 'em;"><pre>'; for ($y = 0; $y < $mapsize; $y++){ for ($x = 0; $x < $mapsize; $x++){ if ($x == $homeX AND $y == $homeY){ echo '<span class ="home">+</span>'; // Home } elseif ($x == $pigeonX && $y == $pigeonY) { echo '<span class ="pigeon">%</span>'; // Pigeon } else { echo '<span class = "empty">.</span>'; //Empty square } echo ($x != $mapsize - 1) ? " " : " "; } echo "\n"; } echo "</pre></div>\n"; } while ($pigeonX != $homeX || $pigeonY != $homeY); ?> </body> </html> How does this line affect the map? I have never come across the "?" and ":" operators, moreover the use of echo in this fashion. echo ($x != $mapsize - 1) ? " " : " ";
  10. Thanks a mil Barand, Google turned up nothing solid on explaining the usage and context of while (true).
  11. Aaah, finally starting to make sense. I overlooked the fact that only the code within the curly brackets will loop. Thank you both. Especially kicken for pointing that out. But please explain the meaning of $count = 0; while(true){ because that's what threw me off. I was under the impression that it meant: while the value of $count is equal to 0, execute the code after the curly brace.
  12. I'm a beginner currently going through php loops. I pretty much understand the fundamentals of loops but i can't figure out why this piece of code. <?php $count = 0; while ( true ) { $count++; echo "I ’ ve counted to: $count <br />"; if ( $count == 10 ) break; } ?> results in this output: I ’ ve counted to: 1 I ’ ve counted to: 2 I ’ ve counted to: 3 I ’ ve counted to: 4 I ’ ve counted to: 5 I ’ ve counted to: 6 I ’ ve counted to: 7 I ’ ve counted to: 8 I ’ ve counted to: 9 I ’ ve counted to: 10 If a loop goes back to the starting point after successful completion, why isn't the value of the variable $count equal to 1 each time, thereby outputting "I've counted to: 1 over and over.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.