Home  >  Article  >  Backend Development  >  PHP realizes printing hollow diamond

PHP realizes printing hollow diamond

小云云
小云云Original
2018-03-21 15:30:473551browse

This article mainly shares with you how to print hollow diamonds in PHP, mainly in the form of code. I hope it can help you.

<?php
	function star($num)
	{
		if($num>0)
		{
			
			for($i=0; $i<$num; $i++)
			{
				//左上
				if($i<1)
				{
					for($j=0; $j<($num-$i-1); $j++)
					{
						echo " ";
					}
					echo "*<br/><br/>";
				}
				else
				{
					
					for($j=0; $j<($num-$i-1); $j++)
					{
						echo " ";
					}
					echo "*";
					//右上
					for($j=0; $j<(2*$i-1); $j++)
					{
						echo " ";
					}
					echo "*<br/><br/>";
				}
	
			}
			
			for($i=1; $i<$num; $i++)
			{
				//左下
				if($i>0)
				{
					for($j=0; $j<$i; $j++)
					{
						echo " ";
					}
					echo "*";
				}
				//右下
				if($i>0 && $i<($num-1))
				{
					for($j=0; $j<2*($num-$i)-3; $j++)
					{
						echo " ";
					}
					echo "*<br/><br/>";
				}

			}
		}
	}
	star(3);
?>

Related recommendations:

php output hollow diamond php7 php environment construction php from entry to proficiency

The above is the detailed content of PHP realizes printing hollow diamond. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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