I have this piece of code that I am studying but don't see the purpose of a certain line.
public function insertRecords($table, $data){
//setup some variables for fields and values
$fields = "";
$values = "";
//populate them
foreach($data as $f => $v){
$fields .= "`$f`,";
$values .= (is_numeric($v) && (intval($v) == $v)) ? $v . "," : "'$v',";
}
//remove our trailing ,
$fields = substr($fields, 0, -1);
//remove our trailing ,
$values = substr($values, 0, -1);
$insert = "INSERT INTO $table ({$fields}) values({$values})";
//echo $insert
$this->executeQuery($insert);
return true;
}
I don't see the purpose of:
intval($v) == $v))
In the ternary operator. What I understand is, if the integer value of $v is the same as $v do blah. Of course the integer value of $v is going to be equal to $v. It's the current value in the current iteration. Is my understanding incorrect?
I already know that if intval() doesn't return a integer it defaults to a string in the ternary operator.
$vis an integer, don't wrap it in quotes. If it's not (it's a string), wrap it in quotes. Depending on what you expect the value of$vto be, you may or may not need that line. But it seems that$vis a reasonably flexible value that could be a string or an integer.