LewisJWright LewisJWright - 6 months ago 13
PHP Question

What does this code actually do?

I have recently been employed at an office where they use a lot of php in their work, most of my development background is HTML, CSS, Jquery, Wordpress and Angularjs, I have an idea behind the logic of some of php but was just wondering if anyone can enlighten me as to what this code below actually means/does?

return (isset($rs[0][0]) ? $rs[0][0] : "");


It is located within this function that calls the database and returns values.

function get_temp($table, $field){
global $db;
$sql="select $field from $table";
$rs=$db->select($sql);
return (isset($rs[0][0]) ? $rs[0][0] : "");
}


I feel that is selecting one value from an array within an array but I cannot find any sources to confirm this so was hoping someone on here could help me out or at least point me in the right direction if I am wrong. The reason I believe this is the case is because if I pass the
$field
variable more than one result it will always only return the first one, if this is the case it would also be helpful to me if someone could suggest a way to get all of the results, whenever I simply try:

return $rs


It simply returns "Array".

mmm mmm
Answer
(isset($rs[0][0]) ? $rs[0][0] : "");

This is a ternary operator. It does a check if $rs[0][0] is set. If yes it will make the function return $rs[0][0]'s value, if not it will return an empty string.

You could translate it to an if statement like this:

if (isset($rs[0][0])) {
    return $rs[0][0];
}
return "";