-1

I just want to make a GET request in php and here is my requirement,

URL: http://localhost/test.php?firstName=Sagar&secondName=Gautam

Expected Output: Hi Sagar Gautam

URL: http://localhost/test.php

Expected Output: Hi Guest

Here is the code

test.php

<?php 
    $firstName = $_GET['firstName'];
    $secondName = $_GET['secondName'];

    $response = '';

    if($firstName!='' && $secondName!='')
    {
        $response = "Hi ".$firstName.' '.$secondName;
    }
    else
    {
        $response = "Hi Guest";
    }

    echo $response;
?>

Above code doesn't work but when I change empty string to null in if statement like this

if($firstName!=null && $secondName!=null)

then everything works perfectly.

I've already visited this question: In PHP, what is the differences between NULL and setting a string to equal 2 single quotes but don't get idea why above code not work with empty string.

If somebody explain whats going wrong, it will be great.

7
  • '' and null are different because '' still has a value. firstName and secondName parameters are assigned a blank, whereas when there are no parameters at all, they are set as null Commented Aug 17, 2018 at 3:24
  • @alimbaronia When no parameter are sent in request then those values are null or empty string ? Commented Aug 17, 2018 at 3:25
  • they are considered null Commented Aug 17, 2018 at 3:25
  • Which means if route like localhost/test.php?firstName=&secondName= will work for the above code ? Commented Aug 17, 2018 at 3:26
  • You can do if(($firstName!=null || $firstname=='')&& ($secondName!=null || $secondName=='')) to catch both cases Commented Aug 17, 2018 at 3:28

2 Answers 2

0

It's better to check isset() the $_GET variables

$firstName = isset($_GET['firstName']) ? $_GET['firstName'] : '';
$secondName = isset($_GET['secondName']) ?  $_GET['firstName']: '';

We always get the not null value.

Just a small note: isset will take care null case for you too. isset($null_value) -> false

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

6 Comments

I'm sorry to say this but... it is not, actually better. There is no difference. Checking for equivalence against empty string or null makes no difference. BTW, OP's code works perfectly already. try it.
If directly gets $firstName = $_GET['firstName'];, it will throw error" Undefined index: firstName when there is no paramter firstName.
oh, right, the notice. ok, fair enough. maybe you can mention the new null coalesce operator $firstName = $_GET['firstName'] ?? '';.
And from here: stackoverflow.com/questions/624922/…. $y = null; isset($y) -> false echo $y -> ""
Thanks, I am fairly aware of these, I was not thinking of the notice for undefined index. In this case, as OP was already using that, they did not seem to care about the notice anyway. That being said, that's not really what they asked...
|
0

Try This

if($firstName!=='' && $secondName!=='')

Hope This Help

1 Comment

Because If you use != for the operator it will check only if $a or $b is not equal in this case $firstName is containing null not ' ' so that's why it return false but when you use !== it will also check the type so it will return True CMIIW

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.