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.
''andnullare different because''still has a value.firstNameandsecondNameparameters are assigned a blank, whereas when there are no parameters at all, they are set asnullnullif(($firstName!=null || $firstname=='')&& ($secondName!=null || $secondName==''))to catch both cases