Home Backend Development PHP Tutorial A simple way to output symmetrical rhombus using PHP

A simple way to output symmetrical rhombus using PHP

Jul 30, 2016 pm 01:29 PM
echo for line lines quot

For example, to output a simple diamond shape or star tower, first the outermost layer controls the line number, then the inner loop. The inner loop has two first loop controls, and then the second loop controls the stars. The number, which controls the space, is the total number of lines minus the current number of lines for($space=1; $space<=$lines-$line; ++$space) and then the star loop is for($star= 1; $star<=2*$line-1; ++$star)

The following code is part of the simplest output rhombus

//$line 表示行号
//$space = $lines-$line;//每行的前导空格
//$star = 2*$line - 1;//m每行的星星数量

//外层循环控制行号
for($line=1; $line<=$lines; ++$line) {
	//先输出前导空格,控制空格的数量
	for($space=1; $space<=$lines-$line; ++$space) {
		echo &#39;+&#39;;
	}
	//再输出星星,控制星星的数量
	for($star=1; $star<=2*$line-1; ++$star) {
		echo &#39;*&#39;;
	}
	//输出换行
	echo "<br />";
}<br></p>
<p>When it comes to symmetrical graphics, most of them involve number axes, such as input and output Read the sporadic + as shown and replace it with spaces</p>
<p>First of all, the general method is to first make a ++ loop to output the upper half of the layer, and then make a -- loop to output the lower half of the layer</p>
<p>For example, </p>
<pre name="code">$lines=12;
for ($line=1; $line <=$lines ; $line++) { 
	for ($j=1; $j <= $lines - $line ; $j++) { 
		echo "+";
	}
	for ($i=0; $i < 2*$line-1; $i++) { 
		if ($i == 0 || $i == 2*$line-2) {
			echo "*";
		}else{
			echo "+";
		};
	}
	echo "</br>";
}
$lines=12;
for ($line=$lines-1; $line >=1 ; $line--) { 
	for ($j=1; $j <= $lines - $line ; $j++) { 
		echo "+";
	}
	for ($i=0; $i < 2*$line-1; $i++) { 
		if ($i == 0 || $i == 2*$line-2 || $line == $lines) {
			echo "*";
		}else{
			echo "+";
		};
	}
	echo "</br>";
}
Copy after login
and then there are It’s a clever way to use the number line

//123454321
//-4-3- 2-101234(number line)
//4321012 3 4 (absolute value)

Think of achieving the effect of 1 2 3 4 5 4 3 2 1

First put -4-3-2-1012 34 Use the absolute value function to convert

and then it is 432101234

The corresponding first one is added. They are all equal to the same book. For example, this one is equal to 5

The corresponding pseudo code

for($i=-4; $i<=4; ++$i) {
	echo $i, &#39;+&#39;;
	echo abs($i), &#39;+&#39;;
	echo 5-abs($i);

	echo &#39;<br>';
}
Copy after login

According to this idea, first of all, the outermost loop controls the number axis, that is to say, it goes from negative numbers to positive numbers. for($i=-($lines-1); $i<=$lines-1; ++$i)

Then subtract the absolute value of the above axis from the total number of lines $line = $lines - abs($i);

Then the code below remains unchanged

for($i=-($lines-1); $i<=$lines-1; ++$i) {
	//计算$line
	$line = $lines - abs($i);
	//先输出前导空格,控制空格的数量
	for($space=1; $space<=$lines-$line; ++$space) {
		echo '+';
	}
	//再输出星星,控制星星的数量
	for($star=1; $star<=2*$line-1; ++$star) {
		//判断应该输出星星还是空格
		if($star==1 || $star==2*$line-1) {
			echo '*';
		} else {
			echo '+';
		}
	}
	//输出换行
	echo "
"; }

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces a simple way to output a symmetrical rhombus using PHP, including all aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
17 ways to solve the kernel_security_check_failure blue screen 17 ways to solve the kernel_security_check_failure blue screen Feb 12, 2024 pm 08:51 PM

Kernelsecuritycheckfailure (kernel check failure) is a relatively common type of stop code. However, no matter what the reason is, the blue screen error causes many users to be very distressed. Let this site carefully introduce 17 types to users. Solution. 17 solutions to kernel_security_check_failure blue screen Method 1: Remove all external devices When any external device you are using is incompatible with your version of Windows, the Kernelsecuritycheckfailure blue screen error may occur. To do this, you need to unplug all external devices before trying to restart your computer.

How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer Feb 13, 2024 pm 12:30 PM

Can Win10 skype be uninstalled? This is a question that many users want to know, because many users find that this application is included in the default program on their computers, and they are worried that deleting it will affect the operation of the system. Let this website help users Let’s take a closer look at how to uninstall Skype for Business in Win10. How to uninstall Skype for Business in Win10 1. Click the Windows icon on the computer desktop, and then click the settings icon to enter. 2. Click "Apply". 3. Enter "Skype" in the search box and click to select the found result. 4. Click "Uninstall". 5

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

How to use for to find the factorial of n in JavaScript How to use for to find the factorial of n in JavaScript Dec 08, 2021 pm 06:04 PM

How to use for to find n factorial: 1. Use the "for (var i=1;i<=n;i++){}" statement to control the loop traversal range to "1~n"; 2. In the loop body, use "cj *=i" Multiply the numbers from 1 to n, and assign the product to the variable cj; 3. After the loop ends, the value of the variable cj is the factorial of n, and then output it.

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

See all articles