Implementation method of printing left and right arrow patterns in PHP (code example)

藏色散人
Release: 2023-04-05 17:02:02
Original
2954 people have browsed it

Implementation method of printing left and right arrow patterns in PHP (code example)

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:

Implementation method of printing left and right arrow patterns in PHP (code 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 = &#39; &#39;; 
    $st = &#39;*&#39;; 
  
    // 用于打印上半部
    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);
Copy after login

Output:

Implementation method of printing left and right arrow patterns in PHP (code example)

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!