charlie charlie - 6 months ago 21
PHP Question

get results returned in a function

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);
}
}

Answer

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';
}