Php

How to Create Triangle in PHP

How to create a triangle in php, This is a very common question for beginner and intermediate-level programmers. I will share a script that will generate a triangle using PHP.

This is a very common type of question in interviews. The programming languages can vary but the question will same as how to generate a triangle.

We’ll cover the following topics here:

  • Create a right angle using a number.
  • Create a right angle using *.
  • Create a triangle using *.

Sample code in php to generate Right Angle:

The Below php code will use to create a right angle from 1- 9.

<?php
for ($i=0; $i<=9; $i++) {  
 for($j=1; $j<=$i; $j++) {  
 echo $i ."  ";  
  } 
  echo "\n";
}

Output:

Related Post

PHP Star Triangle Program Example

The below code are creating right angle using star(*).

<?php
for($i=0;$i<10;$i++){  
 for($j=1;$j<$i;$j++){  
  echo "* ";  
 }  
echo "\n";  
}

Output:

PHP Number Triangle Program in PHP

The Below PHP code will use to create a Triangle from 1- 10.

for ($i=9; $i&gt;=1; $i--) {  
  for($j=1; $j&lt;=$i; $j++) {  
     echo ' ';  
  }
  $j--;
for ($k=1; $k&lt;=(10-$j); $k++) {  
    echo " # ";   
}  
echo "\n";
}

Output:


You can download source code and Demo from below link.

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories