Difference between bindParam and bindValue in PHP

Both bindParam() and bindValue() are built-in PHP PDO methods used to bind variables to placeholders in prepared SQL statements. While they appear similar, they handle variable binding differently − one binds by reference, the other by value.

Key Differences

Sr. No. Key bindParam() bindValue()
1 Binding Type Binds by reference − uses variable's current value at execution Binds by value − captures value at bind time
2 When Value is Read At execute() time At bindValue() call time
3 Variable Changes Reflects changes made to variable after binding Does not reflect changes − uses original value
4 Parameter Type Requires a variable (not literal values) Accepts variables or literal values
5 Use Case When variable value may change before execution When value is fixed at bind time

Example: bindParam() vs bindValue()

Here's a practical example demonstrating the difference ?

<?php
// Simulated PDO prepared statement (database connection required)
$stmt = $pdo->prepare("INSERT INTO users (name, age) VALUES (:name, :age)");

// Initial values
$name = "John";
$age = 25;

// Using bindParam() - binds by reference
$stmt->bindParam(':name', $name);
$stmt->bindParam(':age', $age);

// Change values after binding
$name = "Alice";
$age = 30;

$stmt->execute(); // Executes with Alice, 30

// Reset for bindValue example
$name = "John";
$age = 25;

$stmt->bindValue(':name', $name);
$stmt->bindValue(':age', $age);

// Change values after binding
$name = "Bob";
$age = 35;

$stmt->execute(); // Executes with John, 25
?>

Practical Demonstration

This standalone example shows the behavior difference ?

<?php
// Simulate PDO behavior
class MockPDOStatement {
    private $params = [];
    private $values = [];
    
    public function bindParam($param, &$variable) {
        $this->params[$param] = &$variable;
    }
    
    public function bindValue($param, $value) {
        $this->values[$param] = $value;
    }
    
    public function execute() {
        echo "bindParam results:<br>";
        foreach($this->params as $key => $value) {
            echo "$key = $value<br>";
        }
        
        echo "\nbindValue results:<br>";
        foreach($this->values as $key => $value) {
            echo "$key = $value<br>";
        }
    }
}

$stmt = new MockPDOStatement();

$name = "Original";
$age = 20;

// Bind parameters
$stmt->bindParam(':name1', $name);
$stmt->bindValue(':name2', $name);

// Change variable after binding
$name = "Modified";

$stmt->execute();
?>
bindParam results:
:name1 = Modified

bindValue results:
:name2 = Original

When to Use Each Method

Use bindParam() when:

  • Variable values may change between binding and execution
  • You want dynamic behavior based on variable state
  • Implementing loops with changing parameters

Use bindValue() when:

  • You have fixed values or want to bind literals directly
  • Parameter values won't change after binding
  • You prefer explicit value assignment

Conclusion

bindParam() binds variables by reference and reads their current value at execution time, while bindValue() captures the value immediately when called. Choose bindParam() for dynamic scenarios and bindValue() for fixed values.

Updated on: 2026-03-15T08:54:25+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements