Do I need to sanitize it or can I just leave it safely in the if statement without checking it?
That depends on the level of quality you would like to achieve. It looks like that you access the $_GET
array on a key that you don't test for existence.
This can trigger a notice like "Notice: Undefined index: id" which contains the filename as well.
Depending on server configuration, this message might be given to everybody requesting the website in question not providing the id
URL query parameter.
Next to information offered to a potential attacker, spreading notices makes it harder for you to achieve a certain level of quality for your overall script, e.g. if you decide to turn notices and errors into feedback for maintaining and improving the script.
Can hackers penetrate through this?
This information can be used to further penetrate your site then.
From the code you've shown you don't make further use of the variable / value and as far as I can say, I don't see an additional issue next to information exposure.
It's always subject what you would like to achieve, security is nothing static. See Defensive programming, Secure input and output handling and Postel's law.