Check whether $_POST-value is empty
isset()
will return true if the variable has been initialised. If you have a form field with its name
value set to userName
, when that form is submitted the value will always be "set", although there may not be any data in it.
Instead, trim()
the string and test its length
if("" == trim($_POST['userName'])){
$username = 'Anonymous';
}
Check if a POST value is empty
Use !empty()
instead of isset();
<?php
if (!empty($_POST["username"]) && !empty($_POST["submit"])) {
echo "filled";
} else {
echo "not filled";
}
Check if $_POST exists
if( isset($_POST['fromPerson']) )
{
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}
PHP Correctly checking if a post variable is empty
This link may be useful: https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
Use the empty
function to check if a variable contents something:
<?php
if (isset($_POST['var']) && !empty($_POST['var'])) {
echo $_POST['var'];
}
else {
echo 'Please ensure you have entered your details';
}
?>
How can I check in PHP if a $_POST is empty or zero?
You could check for the string "0"
(all $_POST
variables are strings or arrays of strings):
if ($_POST['NumAthletes_Ratings'] > 0) {
echo 'POST Parameter is set to ' . $_POST['NumAthletes_Ratings'];
} elseif ($_POST['NumAthletes_Ratings'] === '0') {
echo 'POST Parameter is set to 0';
} else {
echo 'POST Parameter is empty';
}
If $_POST is empty function
I think what you are looking for is something like:
if(!empty($_POST['foo'])) {
echo "a sentence".$_POST['foo']." with something in the middle.";
}
This will check that the value is NOT empty, however empty means a lot of things in PHP so you may need to be more specific. For example you may want to check simple if its set:
if(isset($_POST['foo'])) {
echo "a sentence".$_POST['foo']." with something in the middle.";
}
Related Topics
Check If PHP-Page Is Accessed from an iOS Device
How to Install/Enable the PHP Phar Extension
How to Run Cronjobs More Often Than Once Per Minute
Call a C Program from PHP and Read Program Output
Composer Installing: the JSON Extension Is Missing
More Concise Way to Check to See If an Array Contains Only Numbers (Integers)
Mechanisms for Tracking Db Schema Changes
How to Capture PHP Output into a Variable
Replace Multiple Newlines, Tabs, and Spaces