Conditions and Loops in PHP

Rating: 4.0Blog-star
Views: 2777
by Ravindra Savaram
Last modified: November 23rd 2021




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


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



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


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


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.



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:



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 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



Be the first to catch the latest happenings of technology with us.

About Author

NameRavindra Savaram
Author Bio


Ravindra Savaram is a Content Lead at 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.