In this tutorial we will iterate PHP array using the for loop. We will find the length of the array and then print all the values in it.
The following PHP code shows how you can get the array length and then use the for loop to display all the data into the array.
<?php
$loans = array("Home Loan", "Cash Loan", "FHA Loans", "Personal Loans", "Business Loans");
$count=sizeof($loans);
echo ("Total loan types:".$count);
//Now use the for loop to display all the Loan types on browser
for($counter=0;$counter<$count;$counter++){
echo ("<br>".$loans[$counter]);
}
?>
You can also use the while loop to iterate the data of PHP array. In the following code we have achieve the same:
<?php
$counter=0;
while( $counter< $count){
echo ("<br>".$loans[$counter]);
$counter++;
}
?>
Both the code mentioned above will print the following values:
Total loan types: 5
Home Loan
Cash Loan
FHA Loans
Personal Loans
Business Loans
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.