Welcome to W3Courses
 Like Us on Facebook

Array

Use in_array function to check if a value exists in an array using PHP

You can use in_array function to check if a value exists in an array:

For example:

<?php

$myname = "David";

$names = array("Paul","Maria","David","Lucy","Ana");

if (in_array($myname, $names))
       echo("Welcome $myname");
else
      echo "Sorry, that is not a valid name";

?>
 

Share

Loop through an Array using Foreach Function in PHP

You can loop through an array using foreach function. So instead of using:

<?php

$names = array("Paul","Maria","David","Lucy","Ana");

$i=0
while($names[$i] == true)
{
     echo("$names[$i]");
     $i++;
}

?>
 


You can be more efficient and use foreach:

<?php

Share