PHP 中的星形圖案

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

在本教學中,我們將學習如何在 PHP 中實作星形模式。在 PHP 中列印不同的圖案簡單易學。擁有 C 或 C++ 等其他程式語言的知識會更好。我們可以列印金字塔三角形圖案、星形圖案、數位圖案等。我們將在本教程中從許多不同的圖案中學習它們。我們使用 for 迴圈來列印這些圖案。我們也可以使用 foreach 迴圈和巢狀 for 迴圈來列印這些模式。在這些嵌套循環中,我們將使用外部循環和內循環來列印特​​定圖案的星星。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

PHP 中的星形圖案範例

以下是 PHP 中星形圖案的 6 個範例:

  • 這是 PHP 中的一個簡單的星形圖案。我們將使用兩個循環,外循環和內循環。外循環用於行,內循環用於列。值為 I 的外部 for 迴圈從 0 開始迭代 5 次,以值 5 結束,因為我們需要 5 行。外部 for 迴圈代表模式的行。
  • 接下來,內部 for 迴圈也從 0 開始迭代 5 次,以小於 5 的值結束。內部 for 迴圈表示模式的欄位。
  • 依照for迴圈的語法,外層迴圈從0開始,檢查條件是否小於5,如果是,則進入迴圈內部。 for 迴圈內部又是一個針對列的 for 迴圈,其中變數 j 初始化為 0,該迴圈將在第一行印出 star(*),並將循環直到條件 $j
  • $j 值大於 5 後,控制項會跳出循環,遊標將進入下一行。現在 i 的值增加了 1,這表示該值為 2,j for 迴圈將再次循環 5 次並列印 5 顆星。
  • 這將重複 5 次,直到 i 的值大於 5。一旦值大於 5,程式將停止執行並列印所需的輸出。

範例#1

代碼:

<?php
//example to demonstrate star pattern-1
for($i=0; $i<5; $i++) {
for($j=0; $j<5; $j++)
{
echo '*';
}
echo '<br>';
}
?>
登入後複製

輸出:

PHP 中的星形圖案

範例#2

在此範例中,i for 迴圈迭代 5 次,對於 i 的每個值,內部 j for 迴圈將迭代並列印為星號 *。 j for 迴圈用於列印星星。當i的初始值為1時,將列印一顆星。接下來,對於值 2,將列印一行兩顆星,對於值 3,將列印三顆星,一直持續到值不大於 5。

代碼:

<?php
//example to demonstrate star pattern-2
for($i=1; $i<=5; $i++) {
for($j=1; $j<=$i; $j++)
{
echo '*';
}
echo '<br>';
}
?>
登入後複製

輸出:

PHP 中的星形圖案

範例 #3

在此範例中,i for 迴圈將循環 5 次,因為我們需要 5 行。 j for 迴圈用於根據 i 的值列印 *。因為我們第一次希望第一行有 5 顆星,所以對於 i 的第一個值為 1 的情況,j 循環將列印 5 顆星。接下來,對於值 2,j 循環將列印 4 顆星,對於下一個值 3,j 循環將列印 3 次,依此類推。一旦 i 的條件大於 5,這將停止並列印所需的輸出。

代碼:

<?php
//example to demonstrate star pattern-3
for($i=1; $i<=5; $i++) {
for($j=5; $j>=$i; $j--)
{
echo '*';
}
echo '<br>';
}
?>
登入後複製

輸出:

PHP 中的星形圖案

範例#4

在此範例中,值 I for 迴圈迭代了 5 次,因為星形圖案中的行數為 5。同樣在此範例中,我們使用 j for 迴圈來列印空格,新的 k for 迴圈為用於列印星星*.

代碼:

<?php
//example to demonstrate star pattern-4
for($i=1; $i<=5; $i++) {
for($j=4; $j>=$i; $j--)  //loop to print spaces
{
echo '  ';
}
for($k=1; $k<=$i; $k++)  //loop to print stars
{
echo '*';
}
echo '<br>';
}
?>
登入後複製

輸出:

PHP 中的星形圖案

範例#5

在此範例中,使用了三個循環,第一個循環用於計算行數,第二個循環用於列印空格,第三個循環用於列印星星。定義的兩個循環都依賴 i 的值。

代碼:

<?php
//example to demonstrate star pattern-5
for($i=1; $i<=6; $i++) {
for($j=1; $j<=$i; $j++) //loop to print spaces
{
echo '  ';
}
for($k=5; $k>=$i; $k--) //loop to print stars
{
echo '*';
}
echo '<br>';
}
?>
登入後複製

輸出:

PHP 中的星形圖案

Example #6

In this example, there is a combination of two stars patterns upper triangle and lower triangle. These triangles are already explained in the previous examples and for this, we use three loops, one for the number of rows, second for the printing of spaces and third for the printing of stars and this loop is repeated again with different initial values of i and j along with different conditions for the next half of the triangle pattern.

Code:

<?php
//example to demonstrate star pattern-5
// this loop prints the upper half of the star pattern
for($i=1; $i<=5; $i++) {
for($j=1; $j<=$i; $j++) //loop to print spaces
{
echo '*';
}
echo '<br>';
}
// this loop prints the lower half of the pattern
for($i=1; $i<=5; $i++) {
for($j=4; $j>=$i; $j--) //loop to print stars
{
echo '*';
}
echo '<br>';
}
?>
登入後複製

Output:

PHP 中的星形圖案

Conclusion

In this article, star patterns in PHP are explained. We have seen different forms of star patterns. These patterns are with an explanation of how the condition works, how the looping works to print the stars as desired.

以上是PHP 中的星形圖案的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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