0

I want to create a Get Url String with an if statement.

Here is my code:

$value = $_GET['value'];
$fullname = "John Doe";
$email = "[email protected]";

echo "value: " . $value;

if($value = "fullname")
    echo "fullname: " . $fullname;
elseif($value = "email")
    echo "email: " . $email;
else
    echo "fullname: " . $fullname;
    echo "<br>";
    echo "email: " . $email;

It returns both fullname and email each time even if I enter only the fullname or email value. What am I doing wrong? Thanks

3
  • It will always echo the last two lines, because you're not using brackets, see PHP if-manual. You also assign fullname to $value, because you only use one equal sign (=) which assign, instead of two (==) which compares. Commented Dec 22, 2015 at 20:05
  • Thank you. Really appreciate the help. Is there any way to avoid the error message from line 3 ($value = $_GET['value'] without the isset? Every time I implement the isset, no matter the value entered, it always returns both fullname and email. Commented Dec 23, 2015 at 14:18
  • You can use a tenary-operator: $value = (isset($_GET['value']) ? $_GET['value'] : null); - it's like an if/else, just more compact. See How to write a PHP ternary operator Commented Dec 23, 2015 at 14:44

3 Answers 3

3

You are assigning values instead of comparing them:

if($value = "fullname")

This is always true.

You need something like:

if($value === "fullname")

That applies to your elseif as well although right not you will never reach that.

You should also use brackets to mark the blocks that need to be executed in each section if it is more than 1 line (although I would suggest to always do that...).

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

1 Comment

Thank you. Really appreciate the help. Is there any way to avoid the error message from line 3 ($value = $_GET['value'] without the isset? Every time I implement the isset, no matter the value entered, it always returns both fullname and email.
1

Your ending 'else' needs braces to keep the last two lines from being run every time. (And using a single equals sign will always do an assignment operation and be evaluated as 'true')

if($value == "fullname") {
    echo "fullname: " . $fullname;
}
else if($value == "email") {
    echo "email: " . $email;
}
else {
    echo "fullname: " . $fullname;
    echo "<br>";
    echo "email: " . $email;
}

1 Comment

Thank you. Really appreciate the help. Is there any way to avoid the error message from line 3 ($value = $_GET['value'] without the isset? Every time I implement the isset, no matter the value entered, it always returns both fullname and email.
0

A single = is the assignment operator. What you're doing is setting $value to "fullname". What you want is the equals operator == or identical operator === so you end up with

if ($value === "fullname")
  // Do stuff

For more info on operators in php: http://php.net/manual/en/language.operators.comparison.php

Edit: To answer your comment

Thank you. Really appreciate the help. Is there any way to avoid the error message from line 3 ($value = $_GET['value'] without the isset? Every time I implement the isset, no matter the value entered, it always returns both fullname and email

You can do something like:

if ((!isset($fullname) && !isset($email)) || !isset($value)) {
    die("No name or email was included in the request");
}

$value = $_GET['value'];
$fullname = "John Doe";
$email = "[email protected]";

echo "value: " . $value;

if ($value = "fullname") {
    echo "fullname: " . $fullname;
} elseif($value = "email") {
    echo "email: " . $email;
} else {
    echo "fullname: " . $fullname;
    echo "<br>";
    echo "email: " . $email;
}

1 Comment

Thank you. Really appreciate the help. Is there any way to avoid the error message from line 3 ($value = $_GET['value'] without the isset? Every time I implement the isset, no matter the value entered, it always returns both fullname and email.

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.