Jump to content

[SOLVED] Array Error?


kaveman50

Recommended Posts

Does anyone know what's wrong with this array?It says that there is an error on line 19.Basically, I'm trying to make it so that a user enters a name, and it spits out if their name is in the list.  If it's not, it'll say "Not found."

<form method="post" action=""> Name: <input type="text" name="num1" size="10"/><br /><br /><input type="Submit" name="Submit" value="Look up name" /></form>

<?

$name=$_POST['num1'];$array=array('Al', 'David' ,'Sarah' ,'Don' ,'Ed' ,'Bob' ,'Frank', 'Hank', 'Jim' ,'Ben');  for($i=0; $i<=9; $i++){

if ($array[$i]==$name){echo "Found";}else[echo "Not found";]}?>

Link to comment
https://forums.phpfreaks.com/topic/179264-solved-array-error/
Share on other sites

You're using square brackets where you should be using curly ones in the else statement. There's also no need for a loop, just use in_array()

 

Ex:

if(in_array($name, $array))
{
     echo "Found";
}
else
{
     echo "Not Found";
}

 

This can also be shortened to (using the Ternary Operator):

echo (in_array($name, $array)) ? 'Found' : 'Not Found';

 

To post code use [ code ] or [ php ] tags (without the spaces). If you don't know what I'm talking about press quote on my post to see what it looks like.

Link to comment
https://forums.phpfreaks.com/topic/179264-solved-array-error/#findComment-945793
Share on other sites

Now it's saying line 35?

<form method="post" action="">
Name: <input type="text" name="num1" size="10"/>
<br />
<br />
<input type="Submit" name="Submit" value="Look up name" />
</form>



<?

$name=$_POST['num1'];
$array=array('Al', 'Bob', 'Carl','Don' ,'Eric' ,'Fred' ,'Greg', 'Hank', 'Irene' ,'Judy');

for($i=0; $i<=9; $i++){

if(in_array($name, $array))
{
     echo "Found";
}
else
{
     echo "Not Found";
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/179264-solved-array-error/#findComment-945805
Share on other sites

 

 

<form method="post" action="">
Name: <input type="text" name="num1" size="10"/>
<br />
<br />
<input type="Submit" name="Submit" value="Look up name" />
</form>



<?

$name=$_POST['num1'];
$array=array('Al', 'Bob', 'Carl','Don' ,'Eric' ,'Fred' ,'Greg', 'Hank', 'Irene' ,'Judy');

c

if(in_array($name, $array))
{
     echo "Found";
}
else
{
     echo "Not Found";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/179264-solved-array-error/#findComment-945816
Share on other sites

I don't think it's going to help but it just says

 

Parse error: syntax error, unexpected T_IF in /Applications/XAMPP/xamppfiles/htdocs/webalizer/search_array.php on line 17

of course it helps .. without it, it becomes a guessing game.  the error states the exact issue at hand, 'unexpected T_IF'.  make sure to always post error messages exactly as seen on screen to get more direct assistance.

 

it's like going into the doctors, saying 'fix me doc', and when asked what the error is, you say, 'it won't help'.

Link to comment
https://forums.phpfreaks.com/topic/179264-solved-array-error/#findComment-945822
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.