Trying to figure out a decent way to get a multiple while loops to work properly.
What I have been using is:
$x = "0";
$y = "0";
while($x <= "7"){
while($y <= "7"){
echo $x . "-" . $y ."<br />";
$y++;
}
$x++;
}
Trying to get that to display:
1-1
1-2
1-3
...
1-7
2-1
2-2
etc
Anyone able to provide a quick snippet?
You need to reset y after it exits the while loop:
$x = "0";
$y = "0";
while($x <= "7"){
while($y <= "7"){
echo $x . "-" . $y ."<br />";
$y++;
}
$y = "1";
$x++;
}
You should initialize $y to 1 inside the first while loop.
$x = "0";
while($x <= "7") {
$y = "1";
while($y <= "7"){
echo $x . "-" . $y ."<br />";
$y++;
}
$x++;
}
Better approach is to use for loop to do this.
for ($x=1; $x<=7; $x++) {
for ($y=1; $y<=7; $y++) {
echo $x . "-" . $y ."<br />";
}
}
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 want to print number from 1 to 12 in matrix form and expected output is:
1 5 9
2 6 10
3 7 11
4 8 12
code:
<?php
for ($i=1; $i<=12; $i++)
{
for($j=1;$j<=$i;$j++)
{
echo $i." ";
}
echo "<br/>";
}
?>
I have got wrong output. So, How can I get expected output as I mention above? Please help me.
Thank You
Some "magic" code):
foreach (range(1,4) as $num) {
echo implode(' ', range($num,12,4)) . '<br />';
}
Version with for:
for ($i = 1; $i <= 4; $i++) {
for ($j = $i; $j <= 12; $j +=4) {
echo $j . ' ';
}
echo '<br />';
}
$z=0;
for ($x = 1; $x <= 4; $x++)
{
echo " $x ";
$z=$x;
for ($y = 1; $y <= 2; $y++)
{
$z=$z+4;
echo " $z ";
}
echo "\n";
}
$maxRow = 4;
$maxColumn = 3;
for ($row = 1; $row <= $maxRow; $row++)
{
for ($column = 1; $column <= $maxColumn; $column++) {
$number = $row + 4*($column-1);
echo $number." ";
}
echo "<br/>";
}
should work
foreach (range(1, 4) as $res) {
echo implode(' ', range($res, 12, 4));
echo "<br>";
}
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.
I have to make a diamond-shaped asterisk using for loop, inside a table. It has to have "blank" <td> spaces before and after the asterisks to move and make it look centered, so it looks like a diamond. How do I do that? (I used PHP inside an HTML code.)
Code without the <tr> and <td> tags, it looked like a diamond because it was center aligned:
<center>
<?php
echo "<table border = 1>";
// loop for the pyramid
for($i = 1; $i <= 10; $i += 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
echo "</table>";
?>
</center>
Code with the <tr> and <td> tags, need "spaces" for it to look like it's center aligned:
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
for($i = 1; $i <= 10; $i += 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "<br />";
echo "</tr>";
}
echo "</table>";
?>
Please help!
Here is new Code with your solution. I have added logic to put blank td forward and backward to *
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
$max = $initAmount = 10;
for($i = 1; $i <= $initAmount; $i += 2) {
$max = $max -2;
$halfTD = (int)$max/2;
echo "<tr>";
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
$max = $initAmount = 10;
for($i = 7; $i >= 1; $i -= 2) {
$max = $max -2;
$diff = $initAmount - $max;
$blankTd = $diff/2;
echo "<tr>";
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</table>";
?>
I used the code below without using a table to make a diamond shape.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
I used the code below.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
<?php
for($i=0;$i<=5;$i++){
for($j=5;$j>=$i;$j--){
echo ' ';
}
for($k=0;$k<=$i;$k++){
echo '*';
}
echo '<br />';
}
for($i=0;$i<=4;$i++){
for($k=0;$k<=$i+1;$k++){
echo ' ';
}
for($j=4;$j>=$i;$j--){
echo '*';
}
echo '<br />';
}
?>
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.