In this small tutorial I am going to teach you how to check if a string is in an array.
In this example, I am going to use names of people, so to define these, we need to make a new array:
Okay, so we have defined the names that we want to have available in the $names variable.
Now, we are going to get the name that is set in the URL Bar.
The code above will _GET the data that is set in the 'name' parameter in the URL Bar, for example:
Now this is the part where we check if the '$name' variable is in the '$names' array.
Now it is time to piece the full code together:
This isn't the most detailed tutorial, but atleast it gets to the point.
Anyway, I hope this helped you.
In this example, I am going to use names of people, so to define these, we need to make a new array:
PHP:
$names = array( 'Mark', 'Eminem', 'God' );
Now, we are going to get the name that is set in the URL Bar.
PHP:
$name = strip_tags( $_GET[ 'name' ] );
Code:
[URL]http://www.site.com/array-script.php?[/URL]name=Mark
PHP:
if( in_array( $name, $names ) )
{
echo $name . ' is in the array!'; //the $name variable has been found in $names
}
else
{
echo $name . ' is not in the array!'; //the $name variable has NOT been found in $names
}
PHP:
<?php
$names = array( 'Mark', 'Eminem', 'God' );
$name = strip_tags( $_GET[ 'name' ] );
if( in_array( $name, $names ) ) //make sure the variable goes before the actual array ($name, $names) instead of ($names, $name)
{
echo $name . ' is in the array!'; //the $name variable has been found in $names
}
else
{
echo $name . ' is not in the array!'; //the $name variable has NOT been found in $names
}
?>
Anyway, I hope this helped you.