mishke mishke - 5 days ago 5
PHP Question

using loops and if statements in PHP I need to create pattern

In php i need to get this on the screen:

XXXXXXXXXXXXXXX
XOHHHHHHHHHHHHX
XHOHHHHHHHHHHHX
XHHOHHHHHHHHHHX
XHHHOHHHHHHHHHX
XHHHHOHHHHHHHHX
XHHHHHOHHHHHHHX
XHHHHHHOHHHHHHX
XHHHHHHHOHHHHHX
XHHHHHHHHOHHHHX
XHHHHHHHHHOHHHX
XHHHHHHHHHHOHHX
XHHHHHHHHHHHOHX
XHHHHHHHHHHHHOX
XXXXXXXXXXXXXXX


Sofar I have successfully created this:

XXXXXXXXXXXXXXX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XHHHHHHHHHHHHHX
XXXXXXXXXXXXXXX


I don't know how to add character O starting from left top corner to the right bottom corner.

There is a code:

<?php

$x = 15;
$y = 15;

for ($i = 0; $i < $y; $i++)
{
for ($u = 0; $u < $x; $u++) {
if ($u > 0 && $u < $x-1 && $i > 0 && $i < $y-1) {
echo "H";
}
else {
echo "X";
}
}
echo "<br>";
}

Answer

You just create a variable like x and y and if it equals that number you put a o instead of an H. Then, with the <br> you just add 1 to it, as it needs to be extended one each time.

<?php 
$x = 15;
$y = 15;
$o = 0;
for ($i = 0; $i < $y; $i++){
    for ($u = 0; $u < $x; $u++) {
        if ($u > 0 && $u < $x-1 && $i > 0 && $i < $y-1) {
            if($u == $o){
                echo "O";
            }else{
                echo "H";
            }
        }else {
            echo "X";
        }
    }
    $o++;
    echo "<br>";
}
?>
Comments