In this article we will introduce you to an interesting PHP method. That is, use PHP to print out the right arrow pattern and left arrow pattern composed of stars. For example, enter an odd number n to indicate the height and width of the pattern to be printed.
Example:
The PHP code implementation example is as follows:
<?php // rightpattern方法可打印右箭头图案 function rightpattern($n) { // 用于打印上面部分 $c1 = ($n - 1) / 2; // 用于打印下半部 $c2 = floor(3 * $n / 2 - 1); for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { // 检查条件,以打印右箭头 if (($j - $i) == $c1 || ($i + $j) == $c2 || $i == $c1) echo "*"; // 否则打印空格 else echo " "; } echo "\n"; } } // leftpattern方法可打印左箭头图案 function leftpattern($n) { $s = ' '; $st = '*'; // 用于打印上半部 for ($i = ($n - 1) / 2; $i > 0; $i--) { for($j = 0; $j < $i; $j++) echo " "; echo $st."\n"; } // 用于打印中间部分 for ($i = 0; $i < $n; $i++) echo "*"; echo "\n"; // 用于打印下半部 for ($i = 1; $i <= ($n - 1) / 2; $i++) { for($j = 0; $j < $i; $j++) echo " "; echo $st."\n"; } echo "\n"; } // 运行 $n = 9; // 必须是奇数 // 方法调用以打印右箭头 rightpattern($n); echo "\n\n"; // 方法调用以打印左箭头 leftpattern($n);
Output:
Related recommendations: "PHP Tutorial"
This article is about how to print left and right arrow patterns in PHP. It is simple and interesting. , I hope it will be helpful to friends in need!
The above is the detailed content of Implementation method of printing left and right arrow patterns in PHP (code example). For more information, please follow other related articles on the PHP Chinese website!