I tried and tried and tried to get this code to work and kept coming up with zilch. So I decided to try it using "for loops" instead and it worked first try. Could somebody tell me why this code is no good?
<?php
$x = $y = 10;
while ($x < 100) {
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
?>
you should reset y=10 inside the first while.
$x = 10;
while ($x < 100) {
$y = 10;
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
You need to reset y before the y loop begins.
While($x < 100){
$y=10; //... rest of code
For loops which loop over an integer that is incremented I would prefer the for-loop:
for ($x=0; $x < 100; $x++) {
for ($y=10; $y<100; $y++) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
}
}
IMHO this is much more readable and it's shorter, too.
Related
I am trying to write a code that is capturing number 3. using while loop as the condition, so that the for statement will be the evaluation condition of $x, of the while loop statement, and by the same time, using if statement to evaluate the value of $x=3 and so it can echo 'three..'; . please enlighten me. thank you
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
if ($x = 3) {
echo 'three..' . "\n";
}
}
$y++;
}
In while loop, you should increment x variable.
If you increment y variable, it will always "true". So it will be endless loop.
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
// code
$x++;
}
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
echo $z;
}
if ($x == 3) {
echo 'three..' . "\n";
}
$x++;
}
I'm supposed to use nested for loops to create this shape: https://imgur.com/a/prh6zwj
This is what I currently have:
<?php
for ($x = 1; $x <= 10; $x++){
for ($y = 1; $y <= 6; $y++){
echo "Y";
}
}
?>
I have no clue what to do.
Thanks in advance!
<?php
$position = 1;
for ($x = 1; $x <= 11; $x++){
for ($y = 1; $y <= 6; $y++){
if ($y == $position) {
echo "Y";
} else {
echo "0";
}
}
if ($x < 6) {
$position++;
} else {
$position--;
}
echo "\n";
}
<?php
$length = 6; // change this to change height width
$pos = 0;
for ($x = 1; $x <= (($length*2)-1); $x++){
if($x <= $length)
{$pos = $pos+1; }
else
{$pos = $pos-1; }
for ($y = 1; $y <= $length; $y++){
if($y == $pos)
echo "Y";
else
echo "O";
}
echo "\n";
}
There are many possible ways to achieve this when I started programming I never cared about the code quality and just focused on the output. I have added two examples to help you understand it better!
<?php
//We have 6 columns & 11 rows so we need two loops one size of 11 and second size of 6
$counter = 1;
for ($i = 1; $i <= 11; $i++){
for ($j = 1; $j <= 6; $j++){
if ($j == $counter) {
echo "Y";
} else {
echo "O";
}
}
if ($i < 6) {
$counter++;
} else {
$counter--;
}
echo "<br/>";
}
echo "**************************** METHOD TWO ****************************";
//Following is not efficient But its also printing the same results
for ($i = 0 ; $i < 66 ; $i++){
if($i == 65)
{
echo "O";
break;
}
if($i % 6 == 0){
echo "<br/>";
}
if($i <= 36)
{
if ($i % 7 == 0){
echo "Y";
}else{
echo "O";
}
}else{
if ($i % 5 == 0){
echo "Y";
}else{
echo "O";
}
}
}
?>
$k=2; // for calculating position from backside
for($i=1;$i<=11;$i++) //for row
{
for($j=1;$j<=6;$j++) //column
{
if($j==$i && $i<=6) //logic for printing "Y" till the end of row
echo "Y";
else if($i>6 && $j==($i-$k)) //logic for printing "Y" in reversal order from the end of row
{
echo "Y";
$k+=2;
}
else
echo "O"; // filling rest places with "O"
}
echo"\n"; // jumping to new Row;
}
Hope you can understand it easily.
This is the code I have. It currently works as is, However I'm experimenting with loops and want to see it can be done with a while loop and how it would be done. With this code I can take 2 input numbers and display them, then point out all odds, add all evens, and add all the squares of the odds.
define ("B","<br/>");
$firstNum = $_POST["firstNum"];
$secondNum = $_POST["secondNum"];
if ($firstNum < $secondNum)
{
$firstNum = true;
}
elseif ($firstNum >= $secondNum)
{
$firstNum = "You didn't listen, dumb dumb!".'<br/>GO BACK';
}
echo "First Number: ".$firstNum."<br/>"."Second Number: ".$secondNum;
echo B;
echo B;
$numbers = array();
$numbers = range($firstNum, $secondNum);
$length = count($numbers);
$odds = array();
$sumSqOdds = 0;
$sumEven = 0;
$j = 0;
for ($x = 0; $x < $length; $x++)
{
if (($numbers[$x] % 2) == 1)
{
$odds[$j] = $numbers[$x];
$sumSqOdds = $sumSqOdds + pow ($numbers[$x], 2);
$j++;
}
else
{
$sumEven = $sumEven + $numbers[$x];
}
}
$x = 0;
$y = 0;
printf("The odd numbers between your integers are: ");
for ($x = 0; $x < $j; $x++)
{
echo $odds[$x];
echo ' ';
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
echo B;
echo B;
printf("The sum of all even numbers between your integers is: ".$sumEven);
echo B;
echo B;
printf("The sum of the square of the odd numbers between your integers is: ".$sumSqOdds);
Here is my while loop but it seems to be infinite...
$numW = array ();
$numW = range ($firstNum, $secondNum);
$lengthW = count ($numW);
$oddsW = array ();
$sumSqOddsW = 0;
$sumEvenW = 0;
$j = 0;
$x = 0;
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++;
$j++;
}
else
{
$sumEvenW = $sumEvenW + $numW[$x];
}
}
$x = 0;
$y = 0;
printf ("The odd numbers between your integers are: ");
while ($x < $j)
{
$x++;
echo $oddsW[$x];
echo "nbsp;";
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
Equivalent loops:
for ($i = 0; $i < 10; $i++) {
echo $i;
}
$i = 0;
while ($i < 10) {
echo $i;
$i++;
}
For a loop to ever finish it has to change one of the two evaluating variables. So either $x, or $lengthW would have to change during iteration. You made an if statment, in the first case you define that X increases by 1, but in the else case you do not change any variable that then has an effect on either $x, or $lengthW
Nor is there any check that sees if the else state has been reached and to catch that by either changing $x or $lengthW in a later iteration.
As such there's an infinite loop as soon as you reach the else case.
The if statement uses the same $x value as the last iteration checking the same position of the $numW, as such nothing has changed since the last iteration and you'll hit the else again, and again, and so on.
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++; //$x is increased by one, and as such, the loop will progress.
// remove this $x++ if you place it outside the if else statement.
$j++;
}
else // reached when ($numW[$x] %2) != 1
{
$sumEvenW = $sumEvenW + $numW[$x];
// No changes to $x or $lengthW as such you'll hit the else again
// this could be solved by either adding $x++; here.
}
// or by adding $x++; here
// (if you do add it here, remove it in the if case above,
// or you risk increasing it by 2 every iteration
}
this is what i want.
123456
23456
3456
456
56
6
Hi, i have trouble with this loop.
<?php
for ($x = 7; $x >= 1; $x--) {
for ($y = 7; $y > $x; $y--) {
echo "  ";
}
$s = 7;
while ($s < $x) {
$f++;
$s--;
}
for ($f=1; $f < 7; $f++) {
echo "$f";
}
echo "<br>";
}
?>
this is what i got. I want to get the $f work but it is ignoring it.
You can make it simpler than you did.
for($x = 1; $x <= 6; $x++) {
for($y = 1; $y <=6; $y++){
if($x > $y)
echo "  ";
else
echo $y;
}
echo "<br>";
}
With x you control the lines and with y the columns. If the lines is greater than the column you print the spaces, and if not, the number.
So I have a list with variables (auto-generated), something like:
$won3 = 1;
$time3 = 4;
$won6 = 0;
$time6 = 5;
$won4 = 0;
$time4 = 5;
(...)
but with many more variables. Now I want to make a table with all the variables, so I used a for-loop, but $won1 has to be the first in the table, then $won2 etc...
But how can I recall this $won1 in a for-loop? I tried:
for ($X = 0, $X < $Y, $X++){
echo '$won'.$X;
}
but this does not do the job. Anyone knows how I can solve this?
Thanks in advance.
First... In for loops you can't use ,
for ($X = 0, $X < $Y, $X++){
Try this:
for ($X = 0; $X < $Y; $X++){
.
And you have to choices...
for ($X = 0; $X < $Y; $X++){
$var = 'won' . $X;
echo $$var;
echo ${'won' . $X};
}