Jump to: navigation, search

Php in array

From w3cyberlearnings

Contents

PHP function in_array

This function checks a value within an array. If the value exists in the array, it returns TRUE else returns FALSE.

Syntax in_array

  • value: value to search within the array
  • array: Array input
  • strict (optional): when set to TRUE the functions check string and numeric differently (i.e '30' != 30)
in_array(value, array, strict);

Example 1

<?php

$name = array('Ella', 'Amelia', 'Liam', 'Melanie');

if(in_array('Ella',$name)){
	echo 'Ella <br/>';
}
else {
	echo 'No Ella <br/>';
}
?>

Output

Ella

Example 2: Set the strict to TRUE

<?php

$name = array('30', 'Ella', 'Amelia', 'Liam', 'Melanie');

if (in_array(30, $name, 1)) {
	echo '30 number Exist <br/>';
} else {
	echo 'No Ella <br/>';
}
?>

Output

No Ella

Example 3

<?php

$name = array('30', 'Ella', 'Amelia', 'Liam', 'Melanie');

if (in_array(30, $name)) {
	echo '30 number Exist <br/>';
} else {
	echo 'No Ella <br/>';
}
?>

Output

30 number Exist

Related Links


Navigation
Web
SQL
MISC
References