1

i have this inside a php function:

$result = new stdClass();
$result->domainname = $domainName;
$result->element = $element;
$result->availability = $availability;

return ($result);

so its returning all of the values in the $result variable

when i do a print_r on the function, the results display like this:

stdClass Object
(
    [domainname] => domain.com
    [element] => 
    [availability] => false
)

i am calling the function with this code:

$domain = domainNameCheck($_GET["domain"].'.'.$_GET["tld"]);

so i tried to get the returned by doing $domain->availability but its not returning the value, example:

if($domain->availability) {
    echo 'yes';
} else {
    echo 'no';
}

am i trying to get the data the incorrect way?

UPDATE

the full function is:

if(!function_exists("domainNameCheck")) {
    function domainNameCheck($domainName, $element) {
        $result = '';

        $client = new IcukApiClient();
        $client->username = "username";
        $client->key = "pass";
        $client->encryption = "SHA-512";

        $req = new IcukApiRequest();
        $req->url = "/domain/availability/" . $domainName;
        $req->method = "GET";

        $res = $client->send($req);
        $availability = 'unknown';
        if ($res->success) {
            $obj = json_decode($res->response);
            $availability =($obj->available) ? 'true' : 'false';
        }
        else {
            $availability = 'unknown';
        }

        $result = new stdClass();
        $result->domainname = $domainName;
        $result->element = $element;
        $result->availability = $availability;

        return ($result);
    }
}
charlie
  • 415
  • 4
  • 35
  • 83
  • Can you be a little more specific and give some example of code which does not work properly ? – Arcesilas May 11 '16 at 21:53
  • That should work so show all the function code as something else must have gone wrong – RiggsFolly May 11 '16 at 21:54
  • Please be aware that if you do `echo $domain->availability;` and its value is `false` like you say it is, then you'll have no output. `false` converted to string is the empty string. – trincot May 11 '16 at 21:55
  • Try this `echo $domain->availability ? 'TRUE' : 'FALSE';` – RiggsFolly May 11 '16 at 21:55
  • We can guess as much as we like, we cannot provide a decent answer without more details... – Arcesilas May 11 '16 at 21:57
  • I don't get it really. – Chay22 May 11 '16 at 21:58
  • @RiggsFolly The question, it was. Actually was confused between the code inside `domainNameCheck()` function. Seeing answer below make it clear now. – Chay22 May 11 '16 at 22:04
  • *"but its not returning the value, example"*: the example you give is a piece of code, not the output you get and how it is different from expectations. Can you clarify? – trincot May 11 '16 at 22:07
  • You are calling a function with 2 required parameters, but passing only one parameter. That should throw a compile error – RiggsFolly May 11 '16 at 22:07
  • when using `if($domain->availability) {` its always returning `true`, meaning `availability` is always true although its telling me its false in the `print_r` – charlie May 11 '16 at 22:12
  • strings are always true unless they are empty. So `if ("false") ` will be considered true by the `if` – trincot May 11 '16 at 22:24

3 Answers3

1

Please note that there are two error/warning messages PHP is giving:

E_WARNING : type 2 -- Missing argument 2 for domainNameCheck()

E_NOTICE : type 8 -- Undefined variable: element

You should fix those errors, and make sure you are informed of errors during development.

Secondly, you have defined your availability as a string by assigning "false", "true", or "unknown". So when you do this:

if($domain->availability) { 

... that will be true for all three values, because strings are true for PHP when converted to boolean (except when empty). To illustrate, this will echo "hello":

if ("false") echo "hello";

So you need to change your test like this:

if($domain->availability === "true") { 

Or, If you want to define $domain->availability as a true boolean, then you need to alter the assignments in your function, like this:

        ....
        $availability = $obj->available; // assuming that is a boolean!
    }
    else {
        $availability = null; // unknown
    }

... and then you can do what you had:

if($domain->availability) { 
Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
1

Your main problem seems to be that you are calling a function with 2 parameters but passing only one parameter

function domainNameCheck($domainName, $element) {}

// called like this (one parameter)
$domain = domainNameCheck($_GET["domain"].'.'.$_GET["tld"]);

This should be generating a compile error!

Also here

if ($res->success) {
    $obj = json_decode($res->response);

    // check what $obj->available is set to
    // it may also be a string and not a boolean

    print_r($obj);

    $availability =($obj->available) ? 'true' : 'false';
}
else {
    $availability = 'unknown';
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • ive already tried and its returning true all the time even thought the `print_r` result shows false – charlie May 11 '16 at 22:06
  • I see you are setting `availability` to a string `true or `false` so I removed the part about echo not working – RiggsFolly May 11 '16 at 22:14
0

Likely because $domain->availability is boolean

To output you can first check whether its true or false and output accordingly

here's a simple example:

if ($domain->availability){
    echo 'Available';
}
else {
    echo 'Not Available';
}
David D
  • 1,269
  • 17
  • 22