Using isset with strings captured from a clicked link, but something weird

Okay, I’m sending strings to my PHP script via a clicked link such as:

https://xxxxxxx.net/[email protected]&user=Heather

Then in the script I used this in the HTML portion of my PHP file:

<?php $mail = $_GET['email2']; ?>
<?php $who3 = $_GET['user']; ?>

And in the PHP section:

  $mail = $_GET['email2'];
  $who3 = $_GET['user'];

I don’t know if both are needed, but, anyway, I can easily echo them using echo $mail for example, so that works, but anything like:

if (empty($mail)) {
if (empty($email2) {

Or a similar “(!isset)” statement seem to be ignored and displays the error if the string has data or not. Basically, if someone did NOT follow the e-mail link, then these values will not be populated, and an error is shown. But it shows the error no matter what!

Help please.

Well, without code, we can not be clear on your problem. Are you using functions to house your code?
If so, you may not be setting the variables as GLOBAL. If you can echo the data and it is there, they using ISSET should work. If you use EMPTY, you have to check it a slightly different way. You would need to see
if the EMPTY value is true, and that can be tricky depending on your server and code layout. Any of these:
IF ((EMPTY($mail)==TRUE ) {
IF ((EMPTY($mail)===TRUE) {
One checks for just empty and the other also checks for NULL’s which are different.

Also, if you use ISSET, this does not check for empty, so sometimes you need to handle it like this:
IF (ISSET($mail) AND TRIM($mail)!="") {
This first checks if it is set and then checks for empty. Empty can sometimes indicate valid data is not
there, but, also the data can be a single space. Depends on what program is calling the script.

Hope that did not mix you up further. I would just create a small test page to see what all these do.
Or, ask here if you have further questions.

1 Like

I did:

}else if ((empty($mail)==true) && ((empty($who3)==true))) {

And it works perfectly! I didn’t even know you could do it that way. lol

Thanks very much!! :smiley:

Hi Ernie,

This type of code is redundant. The if function is a truthy check by default. There is also not a strictly true. True is just true. Strict comparison compares types.

This should simple be written as

if (empty($mail))
1 Like

I’m pretty sure I tried that and it didn’t work. Not sure if I had the syntax wrong or what…

Edit: Okay, that works. Thanks as well! :smiley:

@benanmen, Zoldos, I have found many people argue over this. Let’s be clear.

== means “loosely compared”
=== means “strictly compared”

There are many many times you need to one or the other differently depending of the data.
Here is the link that explains it, but, more likely it will confuse newbies…
PHP.net types-comparisons

1 Like

Okay thanks for the info!

Hi @ErnieAlex, I know exactly what it means as you have correctly posted. That still doesn’t change what I said, there there is no such thing as strictly true. Even the manual points out there is no need to code like the examples you posted. See here…
https://www.php.net/manual/en/language.types.boolean.php

Functionally, there is absolutely no difference between these three statements.

<?php
    if (empty($mail)){
        echo 'True';
    }

    if (empty($mail) == true){
        echo 'True';
    }

    if (empty($mail) === true){
        echo 'True';
    }

HUH? Try this:

$mail=false;

if (empty($mail)){
    echo 'True';
}

if (empty($mail) == true){
    echo 'True';
}

if (empty($mail) === true){
    echo 'True';
}

Not sure what you are confused about. It works EXACTLY as it should.

Empty - Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist OR if its value equals FALSE

https://www.php.net/manual/en/function.empty.php

Seems I had a spelling error… Me bad!

I can’t find my notes on the differences between == and ===, but, I know I had to use === in one instance on a site, but, just can not locate it to see what it pertains to… No biggie… Sorry for the confusion!

1 Like

LOL Crap! Spelling error again… My bad!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service