Search code examples
phpget

Testfor PHP $_GET value using if statement


I want to testfor the value of a GET variable using an if statement. Here is the code I have:

if(isset($_GET["v"]) == "test1") {
echo 'It worked: test1 loaded';
}

if(isset($_GET["v"]) == "test2") {
echo 'It worked: test2 loaded';
}

and my url is https://www.example.com/video?v=test1

I want to be able to change the content that loads on page depending on the info in the url. Similar to how YouTube videos are all on the same "watch" file, but have a $_Get query afterwords that defines which video to load: www.youtube.com/watch?v=mNZLhuW8vk

But it doesn't work, so I'm assuming that my if statement is messed up.


Solution

  • isset() returns true or false which does not equal to "test1", to get the desired behaviour you need to evaluate the actual value of $_GET['v'] like this:

    if(isset($_GET["v"]) && $_GET['v'] == "test1") {
        echo 'It worked: test1 loaded';
    }
    
    if(isset($_GET["v"]) && $_GET['v'] == "test2") {
        echo 'It worked: test2 loaded';
    }