In this post we will learn to print "X" star pattern in PHP. Let's see how we make it.

## "X" Star Pattern In PHP

To create this star pattern in PHP we will follow the following steps:

Step 1: In this step we take a variable \$n and give it value 5. Then we take two nested for loop and get this output.

<?php

\$n = 5;
for(\$i = 1; \$i <= \$n; \$i++) {
for(\$j = 1; \$j <= \$n; \$j++) {
echo " * ";
}
echo "<br/>";
}

?> code-box

Output :
*****
*****
*****
*****
*****

Step 2: Now we want star pattern like forward slash "\" . For this, we take if condition and place the condition \$i == \$j. After this we get this output.

<?php

\$n = 5;
for(\$i = 1; \$i <= \$n; \$i++) {
for(\$j = 1; \$j <= \$n; \$j++) {
if(\$i == \$j) {
echo " * ";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br/>";
}

?> code-box

Output :
*
*
*
*
*

Step 3: Now in this step we want star pattern like backward slash "/" . For this, we use this formula \$i + \$j = \$n +1 and then we get "X" star pattern as output.

1 2 3 4 5
1 |* 0 0 0 *
2 |0 * 0 0 *
3 |0 0 * 0 0        =     \$i + \$j = \$n +1
4 |0 * 0 0 *
5 |* 0 0 0 *

<?php

\$n = 5;
for(\$i = 1; \$i <= \$n; \$i++) {
for(\$j = 1; \$j <= \$n; \$j++) {
if(\$i == \$j || (\$i+\$j == \$n + 1)) {

echo " * ";
} else {
echo "&nbsp;&nbsp;&nbsp;";
}
}
echo "<br/>";
}

?> code-box

Output :
*           *
*     *
*
*     *
*           *

## Summary

In this post we learn how to print "X" star pattern in PHP. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts.