0

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

Here is my code:

$value = $_GET['value'];
$fullname = "John Doe";
$email = "johndoe@email.com";

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

  • It will always echo the last two lines, because you're not using brackets, see [`PHP if-manual`](http://php.net/manual/en/control-structures.if.php). You also assign `fullname` to `$value`, because you only use one equal sign (`=`) which assign, instead of two (`==`) which compares. – Qirel Dec 22 '15 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. – donfontaine12 Dec 23 '15 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](http://stackoverflow.com/questions/17981723/how-to-write-a-php-ternary-operator) – Qirel Dec 23 '15 at 14:44

3 Answers3

2

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...).

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • 1
    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. – donfontaine12 Dec 23 '15 at 10:53
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;
}
marklark
  • 860
  • 1
  • 8
  • 18
  • 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. – donfontaine12 Dec 23 '15 at 10:55
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 = "johndoe@email.com";

echo "value: " . $value;

if ($value = "fullname") {
    echo "fullname: " . $fullname;
} elseif($value = "email") {
    echo "email: " . $email;
} else {
    echo "fullname: " . $fullname;
    echo "<br>";
    echo "email: " . $email;
}
3ocene
  • 2,102
  • 1
  • 15
  • 30
  • 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. – donfontaine12 Dec 23 '15 at 10:55