在本教程中,我们将学习如何在 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>'; } ?>
输出:
示例#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>'; } ?>
输出:
示例#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>'; } ?>
输出:
示例#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'; } ?>
输出:
示例#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>'; } ?>
输出:
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=$i; $j--) //loop to print stars { echo '*'; } echo '<br>'; } ?>
Output:
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中文网其他相关文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。