


How to print a solid and hollow diamond shape with side length N_PHP tutorial
How to print a solid and hollow diamond with side length N in php
This article mainly introduces how to print a solid and hollow diamond with side length N in php Methods and examples analyze the techniques of drawing graphics using PHP loop statements. Friends in need can refer to it
The example in this article describes how PHP prints a solid and hollow diamond shape with side length N. Share it with everyone for your reference. The specific analysis is as follows:
Solid diamond calculation method:
$n: side length
$i: current line, starting from 0
$rows: Total number of rows
Upper part
Number of preceding spaces =$n-$i-1
Number of characters=$i*2+1
Lower part
Number of preceding spaces =$i-$n+1
Number of characters=($rows-$i)*2-1
Use str_pad to reduce for/while loops
The code is as follows:
* Print solid diamond
* @param int $n side length, default 5
* @param String $s characters displayed, default*
* @return String
*/
function solidDiamond($n=5, $s='*'){
$str = '';
// Calculate the total number of rows
$rows = $n*2-1;
// Loop to calculate * for each row
for($i=0; $i<$rows; $i++){
if($i<$n){ // Upper
$str .= str_pad('', ($n-$i-1), ' '). str_pad('', $i*2+1, $s)."rn";
}else{ // Lower part
$str .= str_pad('', ($i-$n+1), ' '). str_pad('', ($rows-$i)*2-1, $s). "rn";
}
}
return $str;
}
echo '
echo solidDiamond(5);
echo '
The code is as follows:
***
*****
*******
*********
*******
*****
***
*
Hollow diamond calculation method:
$n: side length
$i: current line, starting from 0
$rows: Total number of rows
Upper part
Number of preceding spaces =$n-$i-1
Number of empty spaces =$i*2+1-2
Number of characters = $i*2+1 - number of empty spaces
Lower part
Number of preceding spaces =$i-$n+1
Number of empty spaces = ($rows-$i)*2-1-2
Number of characters = ($rows-$i)*2-1 - Number of empty spaces
The code is as follows:
* Print hollow diamond
* @param int $n side length, default 5
* @param String $s characters displayed, default*
* @return String
*/
function hollowDiamond($n=5, $s='*'){
$str = '';
// Calculate the total number of rows
$rows = $n*2-1;
// Loop to calculate * for each row
for($i=0; $i<$rows; $i++){
if($i<$n){ // Upper
$tmp = $i*2+1;
$str .= str_pad('', ($n-$i-1), ' '). str_pad(str_pad('', $tmp-2, ' ', STR_PAD_BOTH), $tmp, $s, STR_PAD_BOTH). "rn";
}else{ // Lower part
$tmp = ($rows-$i)*2-1;
$str .= str_pad('', ($i-$n+1), ' '). str_pad(str_pad('', $tmp-2, ' ', STR_PAD_BOTH), $tmp, $s, STR_PAD_BOTH). "rn";
}
}
return $str;
}
echo '
echo hollowDiamond(5);
echo '
The code is as follows:
* *
* *
* *
* *
* *
* *
* *
*
I hope this article will be helpful to everyone’s PHP programming design.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
