Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:55:17+00:00 2026-06-13T14:55:17+00:00

I have an array of people that is registered as online in a html

  • 0

I have an array of people that is registered as online in a html file. I am using this so that each can have an image assigned to them. But when checking to see if using name is already in use the in_array function return false and allow the script to continue.

$user = "< img src='default.jpg' />John";
$explode = array("<img src='tress.jpg' />John");

if(in_array($user, $explode))
   {
   //show login script if user exists
   }
   else
    {
     //continue to script
     }

Now the reason this is not working is because the john in the array is not identical to the john in $user. Is there anyway of checking that the name exists in the array? When responding please explain.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:55:20+00:00Added an answer on June 13, 2026 at 2:55 pm

    Instead of asking, “How do I solve this problem?”, you need to start with, “Why am I having this problem?”

    $user = "< img src='default.jpg' />John";
    

    Is < img src='default.jpg' />John a user name? Why are you using it as one? I’m guessing there’s some clever thought behind this like “Well, I always display a user’s image with their name, so I’ll just make the image part of their name. This is going to cause far more problems than it solves. This comes back to a big concept in computer science called separation of concerns. An image is not logically a part of a user name, so don’t store it as one. If you always display them together, you can use functions to display a user’s information in a standard way without making the image part of the user name.

    So first off, remove the image from the name. There are several ways to store this separately.

    I would suggest using a class:

    class User {
        public $name;
        public $imageSource;
    
        // The following functions are optional, but show how a class
        // can be useful.
    
        /**
         * Create a user with the given name and URL to their image
         */
        function __construct($name, $imageSource) {
            $this->name = $name;
            $this->imageSource = $imageSource;
        }
    
        /**
         * Gets the HTML to display a user's image
         */
        function image() {
            return "<img src='". $this->imageSource ."' />";
        }
    
        /**
         * Gets HTML to display to identify a user (including image)
         */
        function display() {
            return $this->image() . $this->name;
        }
    }
    
    $user = new User("john", "default.jpg");
    
    // or without the constructor defined
    //$user = new User();
    //$user->name = "john";
    //$user->imageSource = "default.jpg";
    
    echo $user->display();
    

    You can use an “array” if you want to be a little lazier, but I don’t recommend it in the general case, since you lose the cool features of classes (like those functions):

    $user = array(
       name => "john",
       image => "<img src='default.jpg' />";
    );
    
    echo $user["image"] . $user["name"];
    

    In your database (if you’re using one), make them separate columns and then use one of the above data structures.

    Now that you have this, it’s easy to see if a user name is in a given list using a foreach loop:

    function userNameInList($user, $users) {
        for($users as $current) {
            if($user->name == $current) {
                return true;
            } 
        }
        return false;
    }
    
    $newUser = new User("John", "john.jpg");
    $currentUsers = array("John", "Mary", "Bob");
    if(userNameInList($newUser, $currentUsers) {
        echo "Sorry, user name " . $newUser->name . " is already in use!";
    }
    

    If you’re new to PHP, the normal for loop may be easier to understand:

    function userNameInList($user, $users) {
        for($i = 0; $i < count($users); ++i) {
            $current = $users[$i];
            if($user->name == $current) {
                return true;
            } 
        }
        return false;
    }
    

    Let me know if any of this doesn’t run, I don’t write PHP very often anymore..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of people that looks like this: $people = Array (
I have an array that is produced from people wanting to reserve a time
I have a web app that allows people to register directly or by using
I have an array of arrays that is currently printing each object in the
I have a multidimensional array that stores people. Array ( id93294 => array (
i have the following code: $.each(data.People, function (i, person) { html.push(<img title=' + person.Name
Assume that I have some array of data (a vector to be specific). Can
I have two different arrays. One array, a, for a list of people. My
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.