1

With the piece of PHP code I have been working on, I wanted to return only:

value1
value2

of an array.

However when I run this code, it returns the following:

value1
value2
value2
value2
value2

This is my code:

<?php
    $inputname = array("value1", "value2", "value3", "value4", "value5");
    foreach($inputname AS $i){
        if($i == "value1" || $i = "value2")
        {
            echo($i."<br />");
        }
    }
?>

Am I overlooking something?

4 Answers 4

4

Add extra = before value2:

if($i == "value1" || $i == "value2")
                        ^

otherwise you assign value2 to every variable $i that is not value1.

Sign up to request clarification or add additional context in comments.

Comments

1
if($i == "value1" || $i = "value2")

Suppose to be

if($i == "value1" || $i == "value2")

Comments

1

you need to set the $i = to $i ==

if($i == "value1" || $i == "value2")

//edit: to slow :c

1 Comment

Ah thanks, didn't expect that, I didn't even see that.
0

You need to check if($i == "value1" || $i == "value2") instead of the if($i == "value1" || $i = "value2")

Comments

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.