All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP While Loop Multiple Conditions

Last Updated : Mar 11, 2024

PHP While Loop Multiple Conditions

In this tutorial we will show you the solution of PHP while loop multiple conditions and we will understand is it possible to write multiple conditions in while if not then with is its alternative. Let us understand this example given below.

As, the answer is YES. Yes, we can use multiple conditions in while loop using php.

Step By Step Guide On PHP While Loop Multiple Conditions :-

There is no any need to add extra code or use of nested loop for multiple condition. For better understanding, let us the codes given below.

<!DOCTYPE html>
      <html>
        <head>
          <title> php tutorials </title>
   </head>
 <body>
<?php
while ((condition) && (condition))
{
// use || instead of OR
// use && instead of AND
// use ! instead of NOT
 /* the main reason why loop is not working is most of developer use
while(condition)||(condition)
this is wrong way to write condition instead of this
while((condition)&&(condition))
this must be used.
*/
}
?>
 </body>
      </html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as we can see that inside body we use our basic php structure and create while loop using <?php ?>. The content inside while loop is of your choice.
  6. Here, as you see that we give some assumptions that is Use || instead of OR, Use && instead of AND, Use ! instead of NOT
  7. Here are more condition except this. So, we have to use formats instead of text that is provided by php.
  8. After this the main reason why developer fail to use while loop is they write one condition inside the while loop braces whereas another is written outside loop as specified above.
  9. We are able to use as much multiple conditions as we want. We can also use conditions inside condition, but the format must be right. This concept is same used in do while loop. We hope that you understand codes properly.
  10. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how we can use while loop for multiple condition inside php.

I hope this tutorial on PHP while loop multiple conditions helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪