How can one properly format and display code in a PHP forum to make it easier for others to provide assistance?

Explanation: I am trying to connect to a MySQL database using PHP and I keep getting a connection error. Here is the correct way to establish a connection. Code snippet:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>