Consider the case where an unfriendly user enters something like
<scriptx>for(;😉 alertx('Gotcha!')</scriptx>
(I typed scriptx instead of script and alertx instead of alert just to make sure there would be no problems).
If your script does this
$data = $_POST['data'];
echo $data; // This is bad
Then the browser would present an endless seris of alert boxes.
Let's assume that the data field should never have html tags in it. Maybe its for a person's name. In this case, we can use strip_tags to make the scripts tags go away forever.
$data = strip_tags($_POST['data']);
echo $data // This is safe
But lets say that your web site is about scripting and you want people to be able to enter tags without that tags being executed. In this case, use htmlspecialchars() or htmlentities() to convert the < and > to $lt ; and > ;. This prevents the browser from actiually executing the script.
$data = $_POST['data'];
echo htmlspecialchars($data); // This is safe