首页 >后端开发 >php教程 >PHP 中的模式

PHP 中的模式

王林
王林原创
2024-08-29 13:11:271314浏览

什么是 PHP 中的模式编程?在屏幕上打印某种图案是一门编程艺术。这可以是形成模式的一系列数字、字母或特殊字符。最简单的模式示例是斐波那契数列(1、1、2、3、5、8、13、21、34 等)。还有其他在屏幕上设计的图案,比如星星金字塔。所以,基本上,模式编程只是在屏幕上打印一个模式。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

对于本文,我们将使用 PHP 来编码模式。不过别担心。一旦掌握了它的窍门,就会发现语法因语言而异。逻辑总是一样的。

PHP 中的模式示例

  • 在直接进入程序之前,让我们先了解编码模式的基本逻辑。模式总是通过嵌套循环进行编程——主要是嵌套 for 循环。这是因为循环在语法上更容易理解和简洁。
  • 外层循环始终与行数有关。因此,假设您必须打印跨五行的模式,则外循环将始终运行五次。
  • 内循环始终关注每行中的元素数量。因此,假设您必须在第一行打印 1 个星,在第二行打印 2 个星,依此类推,内部循环将控制此逻辑。
  • 根据模式的不同,有时会有多个内循环或三层嵌套循环。此外,还涉及空格和制表符来产生所需的图案。

所以,记住所有这些,现在让我们尝试对模式进行编码。

1.星半金字塔

这是最简单的打印图案。它在后续行中打印越来越多的星星。第一行 1 颗星,第二行 2 颗星,依此类推。

PHP 中的模式

让我们用五行代码来编写这个模式。牢记逻辑,我们的外循环将运行五次。由于每行中的星星数量直接取决于行号,因此我们的内部循环将是外部循环中控制变量的函数。让我们看看如何。

Our outer control variable is i and inner control variable is j.
Outer loop iteration 1 –> i = 0
Inner loop iteration 1 –> <em>j </em>=<em> 0</em>
Print star
Outer loop iteration 2  –> i<em> = 1</em>
Inner loop iteration 1 –> <em>j</em><em> =</em><em> 0</em>
Print Star
Inner loop iteration 2 -> <em>j</em> =<em> 1
</em>Print Star
Outer loop iteration 3  –> <em>i</em> =<em> 2
</em>Inner loop iteration 1 –> <em>j =</em> 0
Print Star
Inner loop iteration 2 -> <em>j = 1
</em>Print Star
Inner loop iteration 3 -> <em>j = 2
</em>Print Star

等等。这就是我们如何根据外循环控制变量来控制内循环。现在让我们看看该计划正在运行。

代码:

<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 0; $i < $num; $i++)
{
// inner loop handles number of columns
for($j = 0; $j <= $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>

输出:

PHP 中的模式

2.星半金字塔 – 镜像

这与星半金字塔类似,只不过星星是右对齐的。

PHP 中的模式

为了实现正确的缩进,我们将使用空格,然后打印星号。因此,会有两个内部循环——一个控制空格的数量,另一个控制星星的数量。

注意: 请记住一件事,k 循环中的空格数是双倍空格。这是因为我们还随星星一起打印了一个空格。这为我们的图案提供了完整的外观,而不是拥挤的印花。当我们打印完整的金字塔时,我们将利用这一点。

代码:

<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 0; $i < $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i+1; $k-- )
{
// Print stars
echo "  ";
}
// inner loop handles number of stars
for($j = 0; $j <= $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>

输出:

PHP 中的模式

3.星半金字塔 – 倒置

对于这个金字塔图案,星星的数量随着每条新线而不断减少。第一行有 5 颗星,第二行有 4 颗星,依此类推。

PHP 中的模式

记住逻辑,我们知道外循环必须控制行数,内循环必须控制星数。这个逻辑是无法改变的。尽管如此,可以改变的是我们如何开始循环,增加或减少顺序。这意味着我们可以从 0 到 5 循环,也可以从 5 到 0 按降序循环。因此,对于像这样的倒置模式,我们知道第一行的星星数量更多。所以,我们选择减少订单循环。

代码:

<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = $num; $i > 0; $i--){
// inner loop handles number of stars
for($j = 0; $j < $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>

输出:

PHP 中的模式

4. Star Half Pyramid– Inverted Mirrored

This pattern is an indented inverted half pyramid. The number of stars decreases with each line and stars are right-aligned.

PHP 中的模式

I believe by now you would be able to guess the logic for this one.

Code:

<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = $num; $i > 0; $i--)
{
// inner loop handles indentation
for($k = $i; $k < $num; $k++ )
{
// Print stars
echo "  ";
}
// inner loop handles number of stars
for($j = 0; $j < $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>

Output:

PHP 中的模式

5. Star Full Pyramid

This pattern prints the full pyramid. Or in other words, it prints a triangle of stars on the screen.

PHP 中的模式

This pattern is essentially a combination of Half pyramid and its mirror. Although there is a slight twist when we code it. Revisit the Note in Mirrored Half Pyramid. Remember, how we used double spacing to give a finished look to our pattern? Here we would use single spacing so that the stars are alternately aligned in odd and even number of rows, giving us a true triangular pattern.

Code:

<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 0; $i < $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i+1; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 0; $j <= $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>

Output:

PHP 中的模式

6. Star Diamond

This pattern prints a complete diamond shape on the screen. The number of stars increases until the maximum defined and then decrease back to 1, giving us a full diamond shape.

PHP 中的模式

To print this pattern, we would need to divide the pattern into two halves. The upper half – which prints the increasing number of stars. The lower half– which prints the decreasing number of stars. To print both the halves, we would use two outer loops and corresponding inner loops.

Code:

<?php
function print_pattern($num)
{
// The Upper Half Pattern
// Outer loop handles number of rows
for ($i = 0; $i < $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i+1; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 0; $j <= $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
// The Lower Half Pattern
// Outer loop handles number of rows
for ($i = $num-1; $i > 0; $i--)
{
// inner loop handles indentation
for($k = $num-1; $k >= $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 0; $j < $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>

Output:

PHP 中的模式

7. Number Pattern

For this number pattern, we will print the numbers in relation to the row number. Thus, digit 1 would be printed once, 2 twice, 3 thrice and so on.

PHP 中的模式

If you would have followed this tutorial line by line, by now you must have understood very well the working of nested loops to print patterns. This pattern also follows the same logic. Instead of stars, we print numbers. Now you ask how do we get the numbers? The answer is- simply through our control variables i and j.

Code:

<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 1; $i <= $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 1; $j <= $i; $j++ )
{
// Print numbers
echo $i." ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>

Output:

PHP 中的模式

8. Character Pattern

In this pattern, we would print the alphabets ABCDE in a pattern. Starting with A, the subsequent rows would introduce a new alphabet sandwiched between the previous alphabets.

PHP 中的模式

The only trick in this pattern is to get the characters from our control variables. We do this by leveraging the ASCII value of the characters. The ASCII value of A to Z is 65 to 90. So, we calculate the ASCII value in each iteration and print the corresponding character. The chr() function in PHP is used to print a character from the ASCII code.

Code:

<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 1; $i <= $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i; $k-- )
{
// Print spaces
echo "  ";
}
// inner loop handles number of stars
for($j = 1; $j <= $i; $j++ )
{
// Print characters
echo chr(64+$j)." ";
}
for($j = $i-1; $j >= 1; $j-- )
{
// Print characters
echo chr(64+$j)." ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>

Output:

PHP 中的模式

Print for full alphabets and the pattern looks pretty cool.

PHP 中的模式

9. The Binary Hourglass– Bonus Pattern

This pattern is a dynamic pattern that prints the hourglass relative to the time elapsed, not an actual calculation though. For e.g., if one hour has elapsed, it will print one line of 0s in the upper half and one line of 1s in the lower half.

Code:

<?php
function print_pattern($num, $hour)
{
// Outer loop handles number of rows
for ($i = $num; $i > 0; $i--)
{
// inner loop handles indentation
for($k = $num; $k > $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 0; $j < $i; $j++ )
{
// Print characters
if($num-$i < $hour)
echo "0 ";
else
echo "1 ";
}
// go to new line after each row pattern is printed
echo "\n";
}
for ($i = 1; $i < $num; $i++)
{
// inner loop handles indentation
for($k = $num-1; $k > $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 0; $j <= $i; $j++ )
{
// Print characters
if($num-$i <= $hour)
echo "1 ";else
echo "0 ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 8;
$hour = 3;
print_pattern($num, $hour);
?>

Output: 1 hour has elapsed.

PHP 中的模式

Output: 2 hours have elapsed.

PHP 中的模式

Output: 3 hours have elapsed.

PHP 中的模式

And so on.

Conclusion

There is a lot to play with patterns. It’s all about keeping the code logic in mind. Once you get the code logic completely understood, there is no pattern you can’t print.

以上是PHP 中的模式的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn