Search code examples
phpsubstrstrlen

What is the purpose of strlen($query)-2;


I got this code from google and it fulfills my requirement, but I don't understand the meaning of this line:

substr($query,0,strlen($query)-2)

Could somebody explain it to me?

<?php
function insert($tablename, $parameter_order, $values)
{
    $query = "insert into $tablename (";
    foreach($parameter_order as $po)
    {
        $query .= $po.', ';
    }
    $query = substr($query,0,strlen($query)-2).') values (';
foreach($values as $v)
{
$query .= "'$v', ";
}
$query = substr($query,0,strlen($query)-2).');';
    return $this->makeQuery($query);
}
?>

Solution

  • This is what those functions do exactly:

    substr() is used to generate a sub-string of specified length from another string.

    strlen() will return the length of the provided string.

    Code substr($query,0,strlen($query)-2) removes comma and space from foreach Loop.