What are some best practices for formatting strings from arrays in PHP for use in WHERE conditions in SQL queries?

When formatting strings from arrays for use in WHERE conditions in SQL queries in PHP, it is important to properly handle escaping special characters to prevent SQL injection attacks. One common approach is to use prepared statements with placeholders and bind the array values to the placeholders. This ensures that the values are properly escaped and sanitized before being used in the query.

// Example of formatting strings from an array for WHERE conditions in SQL queries

// Sample array of values
$values = ['John', 'Doe', '25'];

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare the SQL query with placeholders
$stmt = $pdo->prepare("SELECT * FROM users WHERE first_name = ? AND last_name = ? AND age = ?");

// Bind the array values to the placeholders
$stmt->execute($values);

// Fetch the results
$results = $stmt->fetchAll();

// Output the results
foreach ($results as $row) {
    echo $row['first_name'] . ' ' . $row['last_name'] . ' - Age: ' . $row['age'] . '<br>';
}