PHP 中的模式

王林
發布: 2024-08-29 13:11:27
原創
1263 人瀏覽過

什麼是 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中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!