All TalkersCode Topics

Follow TalkersCode On Social Media

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

PHP Array Length For Loop

Last Updated : Mar 11, 2024

PHP Array Length For Loop

In this tutorial we will show you the solution of PHP array length for loop, today we will see how to use arrays in php and get value using for loop.

Array is mostly used to store values of same datatype. As for example if we want to add 100 names that store them different variable we use on array to store them.

As storage, we can also retrieve data from array and that is possible using for loop.

This full concept is also used in case of database. And today we will understand about length of array using for loop.

Step By Step Guide On PHP Array Length For Loop :-

Here, as we know that to create for loop. We have to initiate for loop, then have to apply a condition and last concept is to increment that.

Now, with the help of below codes we will understand how to get array length using for loop.

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
$v = [1, 3, 5, 4, 3, 0, 0, 1, 1];
    $count = 0;
        for ($i = 0; $i < v.length; $i++){
                            $count++;
        }
      console.log ($count);
?>
</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, first of all as we can see that we use basic php structure. And inside this <?php is our opening tag of php and ?> is our closing php.
  6. As, we see that we create an array with name $v and inside this we create an array. And we use another variable that is $i and sets its value to 0.
  7. Now, as next we see that we use for loop. And inside this we initialize a variable. And sets a condition that if I exceed than length of array then it became false.
  8. Now, we increment count inside this for loop. As the length of array finishes the count increment to the length of array. And at end of this php, we just prints the value. And show the output.
  9. 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 to get length of array using for loop in php.

I hope this tutorial on PHP array length for loop 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 🡪