PHP Form Validate

Last Updated: June 12, 2021

When you submit data to your server you should validate the data before it is processed.

When you do the validation for HTML forms you have to check for the following things.

Check for empty values

Check for number

Prevent XSS attacks

If you can use a framework like Laravel for PHP development they have the built-in validation functions.

You can use the PHP function htmlspecialchars() to convert HTML escaped code

<?php
$x = 0;

// True because $x is empty
if (empty($a)) {
  echo "Variable 'x' is empty.<br>";
}

// True because $x is set
if (isset($a)) {
  echo "Variable 'x' is set";
}
?> 

// So when you set 0 to $x it is empty
// When you set 1 ro $x it is not empty