PHP Array Contains

Check if value is in array and outputs Yes or No

$names = array( 'Bob', 'Jim', 'Mark' );
echo 'In Array? ';
if (in_array(‘foo’, $names))
   echo 'Yes';
   echo 'No';


  1. Jimbo
    Permalink to comment#


  2. Dan

    Nice! Exactly what I was looking for.
    I found it helpful.

  3. Marcius
    Permalink to comment#

    Tks man! I was looking for it!

  4. Ammon
    Permalink to comment#

    Had an AHA moment when i tried this out for a project i was working on

  5. Dominik
    Permalink to comment#

    Thanks, you saved me some work

  6. Mark

    So simple :) Thanks!

  7. Torey
    Permalink to comment#

    Thanks a bunch! Much better solution than I would have done otherwise.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.