[HELP] While loops

Everything PHP-related! Discuss scripts, host challenges, request a script, or just ask for plain old help.

Moderators: IRC Operators, Support Team

User avatar
Hobold
User
User
Posts: 60
Joined: Sun Feb 10, 2008 3:33 am
Location: Tubarão, Brazil
Contact:

[HELP] While loops

Postby Hobold » Wed Oct 08, 2008 10:36 pm

Well I'm trying to compact my scripts and make them simplier. A way I used to do in mSL was by using while loops, but I don't know how would they work in php. Let me try to explain:

I have:

Code: Select all

$var = $x1 * $y1;
echo $var;
$var = $x2 * $y2;
echo $var;
$var = $x3 * $y3;
echo $var;
$var = $x4 * $y4;
echo $var;
$var = $x5 * $y5;
echo $var;


I wanted to make it like:

Code: Select all

$count = "1";
$max = "5"
while ( $count <= $max ) {
$var = $x($count) * $y($count); // example: $var = $x1 * $y1
echo $var;
$count++
}


----------

I have loads of scripts like this and I have no idea on how to make them with while loops, which I use a lot on mSL.
Image Image
Manacit2
User
User
Posts: 442
Joined: Fri Nov 24, 2006 2:39 am

Re: [HELP] While loops

Postby Manacit2 » Wed Oct 08, 2008 11:34 pm

Hobold wrote:Well I'm trying to compact my scripts and make them simplier. A way I used to do in mSL was by using while loops, but I don't know how would they work in php. Let me try to explain:

I have:

Code: Select all

$var = $x1 * $y1;
echo $var;
$var = $x2 * $y2;
echo $var;
$var = $x3 * $y3;
echo $var;
$var = $x4 * $y4;
echo $var;
$var = $x5 * $y5;
echo $var;


I wanted to make it like:

Code: Select all

$count = "1";
$max = "5"
while ( $count <= $max ) {
$var = $x($count) * $y($count); // example: $var = $x1 * $y1
echo $var;
$count++
}


----------

I have loads of scripts like this and I have no idea on how to make them with while loops, which I use a lot on mSL.


use arrays, instead of having $y1, use $y[1] and $y[2] etc
that way you can do $y[$count]
ImageImage
may not be right, but it is a lot of the time!
User avatar
JoshR
User
User
Posts: 716
Joined: Tue Jan 01, 2008 7:32 pm
Location: #mSL
Contact:

Postby JoshR » Thu Oct 09, 2008 2:14 pm

Code: Select all

<?php
  //sets $x and $y as an array to be able to call upon them like $x[0] for the first array item and the same for $y
  $x = array(N,N,N,N,N); $y = array(N,N,N,N,N);
  //sets your starting and ending numbers for the while loop
  $count = "0"; $end = "4";
  //begins the while loop
  while ($count<=$end)
    {
      //does the multiplication
      $var = $x[$count] * $y[$count];
      echo $var;
      $count++
    }
?>

I believe that is what you're looking to do.

Return to “PHP”

Who is online

Users browsing this forum: No registered users and 1 guest