PHP Program to Print Diamond Shape Star Pattern Last Updated : 17 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Printing a diamond-shaped star pattern is a classic programming exercise that involves nested loops to control the structure of the pattern. In this article, we will explore different approaches to creating a PHP program that prints a diamond-shaped star pattern. Using Nested LoopsThe basic method involves using nested loops to control the number of spaces and stars in each row. The nested loop prints spaces and stars for each row, creating a diamond-shaped pattern. The number of rows determines the size of the diamond. PHP <?php function printDiamond($rows) { for ($i = 1; $i <= $rows; $i++) { // Print spaces for ($j = 1; $j <= $rows - $i; $j++) { echo " "; } // Print stars for ($k = 1; $k <= 2 * $i - 1; $k++) { echo "*"; } echo "\n"; } for ($i = $rows - 1; $i >= 1; $i--) { // Print spaces for ($j = 1; $j <= $rows - $i; $j++) { echo " "; } // Print stars for ($k = 1; $k <= 2 * $i - 1; $k++) { echo "*"; } echo "\n"; } } // Driver code $row = 5; printDiamond($row); ?> Output: * *** ***** ******* ********* ******* ***** *** *Using Single Loop and String ManipulationAn alternative approach involves using a single loop and string manipulation to create each row. PHP <?php function printDiamond($rows) { for ($i = 1; $i <= $rows; $i++) { echo str_repeat(" ", $rows - $i); echo str_repeat("*", 2 * $i - 1) . "\n"; } for ($i = $rows - 1; $i >= 1; $i--) { echo str_repeat(" ", $rows - $i); echo str_repeat("*", 2 * $i - 1) . "\n"; } } // Driver code $row = 5; printDiamond($row); ?> Output: * *** ***** ******* ********* ******* ***** *** * Comment More infoAdvertise with us Next Article PHP Program to Print Diamond Shape Star Pattern V vkash8574 Follow Improve Article Tags : PHP Geeks Premier League 2023 Similar Reads PHP str_pad to print string patterns str_pad: Pad a string to a certain length with another string. Syntax:- str_pad (input, pad_length, pad_string_value, pad_type) It returns the padded string. Parameters Description input:-The input string. pad_length:-If the value of pad_length is negative, less than, or equal to the length of the i 1 min read Program to Print Inverted Right Half Pyramid Pattern (Star Pattern) Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Ou 3 min read Diamond Shapes: Lesson for Kids Diamond Shapes: Lesson for Kids: Children start learning about shapes and figures at a very young age. Even babies recognize shapes through toys like blocks or picture books. As they grow, they become familiar with the names of different shapes. One shape that captures their interest is the diamond. 7 min read Java Program to Print Diamond Shape Star Pattern In this article, we are going to learn how to print diamond shape star patterns in Java. Illustration: Input: number = 7 Output: * *** ***** ******* ********* *********** ************* *********** ********* ******* ***** *** * Methods: When it comes to pattern printing we do opt for standard ways of 6 min read Java Program to Print Square Star Pattern Here, we will implement a Java program to print the square star pattern. We will print the square star pattern with diagonals and without diagonals. Example: ********************** * * * * * * * * * * * * ********************** Approach: Step 1: Input number of rows and columns. Step 2: For rows of 4 min read C Program To Print Diamond Pattern The Diamond Pattern is a symmetrical shape where the number of characters increases up to the centre and then decreases forming a diamond-like structure. It can be visualized as a full pyramid and an inverted full pyramid joined by their bases. In this article, we will learn how to print the Diamond 4 min read PHP Program to Print matrix in snake pattern Given an n x n matrix in the given matrix, you have to print the elements of the matrix in the snake pattern.Examples:Â Input: mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output: 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input: mat[][] = { {1, 2, 3}, {4, 2 min read Java Program to Print Pyramid Star Pattern This article will guide you through the process of printing a Pyramid star pattern in Java. 1. Simple pyramid pattern Java import java.io.*; // Java code to demonstrate Pyramid star patterns public class GeeksForGeeks { // Function to demonstrate printing pattern public static void PyramidStar(int n 5 min read C Program To Print Hollow Diamond Pattern The Hollow Diamond Pattern is a variation of the diamond pattern where only the edges of the diamond are filled with characters and the inside remains empty. This creates a hollow effect in the shape of a diamond. In this article, we will learn how to print the Hollow Diamond Pattern using C program 4 min read Like