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

A simple way to output symmetrical rhombus using PHP

WBOY
Release: 2016-07-30 13:29:30
Original
2412 people have browsed it

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.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template