Array help, looping through array to check for content.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Array help, looping through array to check for content.

Post by akimm »

I am trying to help a user on here, I've made this array, and a function that takes the value of $_POST['name'] and checks to see if it exists within an array. Somewhere along the line I am screwing up. As I said this is for someone else, just trying to help

test1_array.php

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
$users_array = array(
'bob',
'lisa'
);
function users_array() {
$value = $_POST['name'];
   if (!is_array($users_array)) {
	echo "this is not an array";
   return false;
   } else {
   while (in_array($value)) {
	   echo "user found";
			break;
	       return true;
       }
echo "you're not in there";
   break;
   return false;
		}
return users_array();
			}
?>
the form

Code: Select all

<form method="POST" action="test1_array.php">
<h1>your name </h1>
<input type="text" name="name">
<input type="submit" value="submit this" name="submit">
</form>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

while (in_array($value))
why while?
in_array($value)
What is this supposed to do?
function users_array() {
$value = $_POST['name'];
if (!is_array($users_array)) {
please read http://www.php.net/manual/en/language.v ... .scope.php


why not simply

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);

$users_array = array('bob','lisa');
echo in_array($_POST['name'], $users_array) ? 'true':'false';
?>
?
Post Reply