简体   繁体   中英

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".

(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 "";

In fact the method isset will check is the value is null.

Then it is simply a ternary operator, returning the value of the variable if it is not empty and an empty string if the value of the var is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM