Skip to main content

Loops In php

Loops are used to run the same lines of code for multiple times. If we want to execute same code multiple times then we can put it into a loop block to avoid code repetition. Loop structure includes three sections. These are,
  • Initialization – To initialize the first iteration index of the loop.
  • Conditional execution – For each iteration, it checks whether the specified condition is satisfied. If so, then only the code will be executed.
  • Incrementation/decrementation – After processing each iteration, the loop index should be incremented or decremented to process next iteration. This will be continued till the loop condition is satisfied.
PHP supports four types of loop statements. These are, whiledo…whileforand foreach. In this tutorial, we are going to see about each of these types of PHP loop statements with an example. I have added a demo with nested for loop to print star triangle.
view demo

while

The following example code shows a while loop with initialization, conditional expression, and incrementation. In this example, I initialize the loop index $i to 1. The while loop will echo the loop index.  Then, the index will be checked with the while condition to process the code within the loop block. The loop will print the index value and increment it for each iteration. This will be continued till the while loop condition is satisfied.
<?php
$i=1;
while($i <= 3) {
   echo $i;
   $i++;
}
?>

do..while

do..while is same as while loop. It has the same three parts initialization, conditional execution, and incrementation/decrementation. The difference is that it will check the condition after processing an iteration and before go to next iteration. So, in do..while there is an assured guaranty for the execution of the first iteration. I changed the while loop example code with do…while. The code is,
<?php
$i = 1;
do {
   echo $i;
   $i++;
} while (($i > 1) && ($1 <= 3)); 
?>

for

The for loop is so easy to understand and it has a very simple structure to use it in programming. It has the initialization, conditional statement and increments/decrements in a single line as shown in the below syntax.
for(initialization; conditional statement; increments (or) decrements) {
//set of expressions to be execute recursively
}
The while loop example is converted to the for loop to print the loop index for the three iterations.
<?php
for($i=1;$i <= 3;$i++) {
   echo $i;
}
?>

foreach

The foreach loop is used to iterate the array variables to process the array keys and values. The following code snippet shows the syntax of the foreach statement with two types of usage. The first type is used to move the array pointer one step inner level or multidimensional array. The second line type is used iterate an array and process with its key and value pairs for each iteration.
foreach($array as $value_array) {}
(or)
foreach($array as $key_array=>$value_array) {}

Output


Note: Though PHP has various types loop statement, the while loop usage is the optimistic way of programming. Because any type of loop structure will be changed to while at the time of compilation.

Comments

Popular posts from this blog

Create facebook messenger chatbot using PHP

Chatbots are the latest sensation in social media communication channels. These automated chat systems are especially build to receive vistiors on social media chats and provide basic information to the visitors about your business. This information could include event schedules, product information, latest deals, store offers and general information about the brand. Entrepreneurs and brand marketers employ chatbots to handle the bulk of chats queries. This way, a large number of queries could be easily handled with minimum costs. Chatbots help reduces the dependence on human customer service representatives (CSR). These chatbots vet out common queries so that the human CSR cold focus on queries that require processing of multiple information sources. Since chatbots steer all conversation toward a pre-set direction, it is easy and time-efficient to use these chatbots instead of human CSR. In this article, I will create a simple Facebook chatbot that could carry out an

Build chatbot with node js and react js

User Experience is given a lot of attention while building any application these days. More and more brands are leveraging chatbots to service their customers, market their brand, and even sell their products. There are a lot of awesome tools out there which helps in building an intelligent bot very easily like Google’s DialogFlow, Amazon Lex, etc, most of which implement their own Natural Language Processing (NLP) logic. However, in some cases, we don’t really need an intelligent bot. Whenever we have a small application having a limited set of options to choose from, it’s not really necessary to use NLP based tools like Google’s DialogFlow. You need to integrate with them (which is pretty easy though), and you need to make a network call to get the results. Instead, you would want to define your rules locally in those cases. Here we will build a simple chatbot using React Simple Chatbot library and add it to our pizza-builder app using which we can build ou

Now About PHP Info

Have you ever heard about phpinfo(). Yes, this function is used to know about configuration details of PHP installed in our machine. Such detailed information returned by this phpinfo() includes platform information, PHP and server environment, HTTP header information, PHP core details like version and directives status, License information and etc. phpinfo() function has an optional argument. If this function is called with no argument, then will display all information. We can check it by executing the following code. <? php phpinfo (); ?> We can also request specific details to be displayed to the browser by passing available constants to this function. Following list shows such available options. INFO_GENERAL – This will return information about the platform, compiler, architecture, API and etc. INFO_CREDITS – This option provides a hyperlink which shows details about authors, documentation, QA and infrastructure team. INFO_CONFIGURATION – Disp