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

PHP realizes printing hollow diamond

小云云
Release: 2023-03-21 21:24:02
Original
3623 people have browsed it

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);
?>
Copy after login

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!

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