Simple Star Pattern With One Loop With PHP

Simple Star Pattern With One Loop With PHP


Simple Star Pattern With One Loop With PHP


Today we will create a simple star pattern with simple one loop in PHP it is very simple, below is the output you can see.
star pattern PHP


We will use a for loop and an inbuilt string function of PHP.  str_repeat(); Inside the For loop. So let's do this.

First You will need Xamppfor this task If You have already installed then well and good but if you don't know about that then also well and good. Let me explain to you what is Xampp.


It stands for Cross-Platform (X), Apache (A), MariaDB (M), PHP (P) and Perl (P). ...is also cross-platform, which means it works equally well on Linux, Mac, and Windows.

Basically, it is a package which has PHP, Mysql and some other useful software preinstalled So that You don't have to make your hands dirty downloading these tools separately.

 If you are developing any web application on LAMP stack then there might be some situations where you have come across a topic like XAMPP for testing your web app.

If you need a more formal explanation then visit here.

Installing it is not much complicated you can simply download it from here Just hit next button until installation starts. (Note: You can select Your custom installation destination to some other local disk but the default is Local disk: C)


  1. Inside Your local disk C: locate and open xampp folder. 
    xampp1
  2. Inside the xampp folder locate and open htdocs folder.
    xampp2
  3. Now create a new folder inside htdocs and name it anything in my case I will name it as projects.
    xampp3
  4. Now inside projects create 2 new file a.php and star.css (NOTE: You can name them anything you want to but the extension should be same ie; .php and .css)
    xampp4
We are done with the setup now past the below code inside your newly created PHP file in my case a.php



<!DOCTYPE html>
<html>
<head>
 <title>star pattern</title>
 <link rel="stylesheet" href="star.css">
</head>
<body>
 <div class="cont">
  <?php 
   for($i=0;$i<9;$i++){
    echo str_repeat("*", $i);
    echo "<br>";
   }
   ?>
</div>
</body>
</html>

Now inside your CSS file in my case star.css paste the below code.


.cont{
 font-size: 28px;
 margin-left: 100px;
 padding:30px;
 color:black;

}

NOTE: It is very important to understand that that CSS file is not mandatory you can to it without the CSS file, I used it because I want to use it. If you don't want to create a CSS file, fine leave it.

HOW TO RUN THE PROJECT

Now start menu and type xampp and select open xampp Control Panel from the list.
start xampp

 A new window will open like this. Press start on both Apache and MySQL

xampp controllpanel

Now open Your favorite browser I will use firefox and type: 

localhost/projects/a.php

Inside the URL box at the top of the browser.
"projects" is the newly created folder inside which we have stored a.php file. We have to provide the full path after the localhost keyword.

It is the end of this tutorial. If You have any dought Pls don't hesitate to comment below. I will be glad to help you and please show some appreciation for my work by commenting on them if you like it. Thanks.

0 comments :

Post a Comment