0

URL

x.php?foo=100

x.php

$x = $_GET['foo'];

if ($x = 100) {
   echo "yeah";    
}else{
   echo "no";
}

My code doesnt work, where is error?

2
  • 2
    @Jleagle I assume because the question gets asked like ten times a day. I must say this is one of the better ones though. Commented Oct 21, 2011 at 9:53
  • Ilya, for long term, you should read this: PHP Operators Commented Oct 21, 2011 at 11:54

6 Answers 6

3

You are using a single equal mark (=) instead of two in the IF statement (if ($x = 100) {).

A single equal mark will set the value 100 into $x, and then evaluate the IF statement with it - which evaluates to true in PHP.

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

Comments

3
if ($x == "100")

or

if (intval($x) === 100)

Comments

3
if ($x = 100) {

sets $x to 100 and evaluates the result. You want:

if ($x == '100') {

Comments

3

It is because you are using an assignment statement instead of checking for equality in the line: if ($x = 100) Try if ($x == 100) instead.

Comments

1

you are not comparing value of x, you are saying that x is equal to 100

try

if($x == 100) { echo "yeah"; }

Comments

1

first check with isset($_REQUEST['foo']) or isset($_GET['foo'])

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.