isset()
This function is used to check whether a variable is set or not. Consider the following example:
if(isset($_POST['submit'])){In this example, check whether the variable $_ POST ['submit'] is set or not.
echo "Variable has been set";
}
else{
echo "Variable not set";
}
empty()
The function empty() is used to check whether a variable has a value or not (null / not null). Consider the following example:
$name="John";In the above example the function empty() is used to check whether the variable $name has a value or not. Previous variable $name has been set to the value "John" ($name="John") then when the script is executed will display the value of variable $name
if(empty($name)){
echo "Variable $name is empty!";
}
else{
echo $name;
}
preg_match()
preg_match often used to validate where its use is combined with Regular Expression. The following is the syntax of preg_match():
preg_match($pattern,$subject);There are two parameters: $pattern and $subject. $pattern is the pattern to search for, as a string; $subject is the input string which will be matched by the pattern of whether it is appropriate or no.
The first example of the use preg_match, making text input validation with pattern allows the use of uppercase, lowercase, spaces and the dot.
if(!preg_match("/^[a-zA-Z.\s]+$/",$_POST['txt'])){In the example script above, regular expression pattern that is used is "/^[a-zA-Z.\s]+$/", then be matched with the input from the user ($_POST ['txt']). If the input pattern in accordance with the regular expression it will display the text "Your entry is match", if it is not appropriate to be displayed text "Please fill the form using uppercase, lowercase, spaces or the dot!".
echo "Please fill the form using uppercase, lowercase, spaces or the dot!";
}
else{
echo "Your entry is match!";
}
Validation Number
Validation numbers only numbers are allowed, for the pattern can use "/^[0-9]+$/". The following example script validation number:
if(!preg_match("/^[0-9]+$/",$_POST['txt'])){Validation Email
echo "Please fill the form using number only!";
}
else{
echo "Your entry is match!";
}
Regular Expression to validate the email is "/^[a-zA-Z0-9._]+@+[a-zA-Z0-9]+[._]+[a-zA-Z0-9.]+$/".
Following the implementation of the email validation script:
if(!preg_match("/^[a-zA-Z0-9._]+@+[a-zA-Z0-9]+[._]+[a-zA-Z0-9.]+$/",$_POST['txt'])){Download Script
echo "your entry is not appropriate as the email format!";
}
else{
echo "Your entry is match!";
}