0

I thought this would have been simple enough using strpos, so I must be oblivious to a horribly obvious mistake. I have this code here:

<div class="text" style="text-align:center;border:2px solid #bbb;font-size:12px;padding:0.2%;">
    <?php
        $root = realpath($_SERVER['DOCUMENT_ROOT']);
        include "$root/scripts/display.php";
    ?>
</div>

Within the included file is a simple array that displays a random string from it:

$data=array("random string","something","something else","and so on");
$info=$data[rand(0,count($data)-1)];
echo $info;

What I want to do is to display an image that is relevant to a few of the random strings that are echoed. I thought I could do this with an if check for strpos as, for example "random" in $info, such that:

...
    ?>
</div>
<?php if(strpos($info,"random") !== false){?>
    <img src=...>
<?php } ?>

Problem is, this displays the image, even if "random" was not echoed.

I have a feeling this might have something to do with $info being checked, right?

2
  • That conditional looks fine, there must be some other issue. Commented Apr 9, 2014 at 11:41
  • I'll have to dig through my other codes, then. I'll see what I can come up with later in the day. Commented Apr 9, 2014 at 11:42

1 Answer 1

1

can you please check with the following if it solves you problem

$data=array("1","2","3","4","5");
   $info = array_rand($data, count($data));
   print_r($info);

    <?php if(in_array("4", $info)){?>
        <img src=...>
    <?php } ?>
Sign up to request clarification or add additional context in comments.

4 Comments

Maybe you could also explain or add a link of what in_array() does.
Sorry, this answer is wrong. $info is not an array, but a string.
I made an edit to my question. I think it was confusing because I'm trying to look for a specific word in something that is output by the randomiser, not the entire output itself.
you can have a look at us3.php.net/in_array. If you do not understand then I will tell you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.