2

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.

4
  • If $v is 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 $v to be, you may or may not need that line. But it seems that $v is a reasonably flexible value that could be a string or an integer. Commented Jun 13, 2013 at 23:51
  • 2
    It is merely ensuring that when $v is converted to an int, it still looks like $v. So, if $v is 3, intval(3)==3 is true. If $v is 3.1, intval(3.1)==3.1 is false. Obviously, any $v that is not a number will be false. I have a feeling that intval(null)==null will return true, but I haven't checked it. Commented Jun 13, 2013 at 23:52
  • Wouldn't it been wise to use the === strict comparison as a string "0" will be converted to an integer, for accuracy? Commented Jun 13, 2013 at 23:56
  • Almost like JavaScript's parseFloat and parseInt funcions don't you think? Commented Aug 6, 2013 at 3:11

3 Answers 3

1

You are correct in your assumption.

That line is merely a method of checking whether the variable $v is indeed an integer. Because if it is any other data-type the value would differ from that intval() operation.

Sign up to request clarification or add additional context in comments.

Comments

0

It is using intval() just because it's checking for the value entered to be an integer rather than a string wich would not be allowed in your query either for column datatype and for security purpose

intval — Get the integer value of a variable

Comments

0

is_numeric($v) returns true if $v is a number (e.g. 234233) or numeric string (e.g. "234233"), whereas intval($v) == $v returns true if $v is an integer.

The first makes sure $v is a numeric in any way and seconds checks if $v is an integer.

I guess you could drop is_numeric($v)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.