函数返回错误响应

I've got an issue with a function where it's returning data from the first if() statement as opposed to the secondary if() statement in a function, please see below.

function training($type,$tid)
{   
if($type = "id") {
    $query = mysql_query("SELECT * FROM trainroster WHERE DATE = CURDATE()");
    if (mysql_num_rows($query) == 1) {
            $array = mysql_fetch_array($query);
            $id=$array['TID'];  
            }
    else { $id = "1"; }
 return $id;
}
else if($type = "topic"){
    $query = mysql_query("SELECT * FROM trainroster WHERE TID='$tid'");
    $array = mysql_fetch_array($query);

    $topic = $array['TOPIC'];
    return $topic;
}
}

Which is being called like this:

$training = $connection->training("topic",$row['TID']);

When the function is called it's returning the $id as opposed to the $topic even though I'm setting the $type variable to "topic".

Any help greatly appreciated, cheers!

use '==' instead of '=' to compare

'=' is an assigning operator, if you use it to compare, it will always return true. That's why you are getting id always.

if($type == "id") {
....
if($type == "topic"){
....

Look at your if statements:

if($type = "id")

if($type = "topic")

You're assigning, not comparing. They should be:

if($type == "id")

if($type == "topic")

if($type = "id") assigns the string "id" to $type and returns "id" which is true.

As mentioned by Harish Singh you have to write if($type == "id").

A way to prevent this error is to write if("id" == $type) which results in an error if you forget one "=".