What are some key rules and guidelines to keep in mind when using PHP?

When using PHP, it is important to follow certain rules and guidelines to ensure code quality and maintainability. Some key rules to keep in mind include using meaningful variable names, indenting code properly for readability, commenting code to explain its purpose, avoiding global variables, and sanitizing user input to prevent security vulnerabilities. Example:

// Example of using meaningful variable names and proper indentation
$firstName = "John";
$lastName = "Doe";
$age = 30;

// Example of commenting code to explain its purpose
// Calculate the total cost including tax
$totalCost = $subtotal + ($subtotal * $taxRate);

// Example of avoiding global variables
function calculateTotalCost($subtotal, $taxRate) {
    $totalCost = $subtotal + ($subtotal * $taxRate);
    return $totalCost;
}

// Example of sanitizing user input
$username = $_POST['username'];
$sanitizedUsername = filter_var($username, FILTER_SANITIZE_STRING);