What are common pitfalls when trying to modify PHP templates?

Common pitfalls when trying to modify PHP templates include not properly escaping variables, not using conditional statements correctly, and not understanding the template hierarchy. To avoid these pitfalls, always sanitize and escape user input, use if statements and loops appropriately, and familiarize yourself with how template files are loaded and overridden in WordPress. Example PHP code snippet:

<?php
// Incorrect usage of variable without escaping
$name = $_GET['name'];
echo "<p>Hello, $name!</p>";

// Correct usage of variable with escaping
$name = htmlentities($_GET['name']);
echo "<p>Hello, $name!</p>";

// Incorrect usage of if statement
if ($name = 'John') {
    echo "<p>Hello John!</p>";
}

// Correct usage of if statement
if ($name == 'John') {
    echo "<p>Hello John!</p>";
}

// Incorrect understanding of template hierarchy
// Assuming template file is loaded from parent theme
get_template_part('template-parts/content', 'single');

// Correct understanding of template hierarchy
// Assuming template file is loaded from child theme
get_template_part('template-parts/content', 'single', 'child');
?>