Home  >  Blog  >   PHP  > 

Conditions and Loops in PHP

Rating: 4
  
 
2858

 

CONSTANT

DEFINE:-

Define is the pre-defined key-word in php. This will take two arguments. The first argument being the constant identifier which identifier its second argument value. We can pass third argument as Boolean to make case in-sensitive

Checkout PHP Interview Questions

CONDITIONS

Conditional statements are used to perform different actions based on different conditions.

IF CONDITION:-

WHILE CONDITION:-

The while loop executes a block of code while a condition is true

OUTPUT:-

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

Related Page: Cookies, Sessions And Buffer In PHP

DO-WHILE CONDITION:- 

The do…while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.

 

Example

The example below defines a loop that starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5:

 

FOR LOOP:-

Loops execute a block of code a specified number of times, or while a specified condition is true. The for loop is used when you know in advance how many times the script should run.

Related Page: Headers, Downloading And File Uploading In PHP

FOR EACH:-

for each is usefull for all non-sequence data of an array. Associative array ,numerical array object data of an array
The general syntax is
foreach (arrayName as arr[value]))
{
echo arr [value];
}
Another syntax is
foreach (arrayname as key=>value)
{
echo key’-‘value         (or)        echo “key-value”;   }

Ex: -foreach.php

 

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

About Author
Remy Sharp
Ravindra Savaram

Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

Recommended Courses

1 /15