I'm checking to make sure all incoming $_POST values are not empty. is using isEmpty the best method. This is a second check prior to db entry, I'm already using a jquery form validate
if (!isEmpty($_POST['add']) || ($_POST['add']['type'] == 2 && !isEmpty($_POST['cpl']))):
// insert into db
else:
header("Location: /?req=register");
exit();
endif;
try this
if (isset($_POST['add']) AND $_POST['add'] != '')
you can chcek in many way using also NULL
Be careful, as empty (no such thing as isEmpty) considers some values as empty.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
You can just use if($_POST['add'] <> '') ...
you can use isset to see whether the variable is set or not. This will help in radio and checkbox if you have not selected.
(isset($_POST['add']) && $_POST['add'] != '')
You can also use !empty($_POST['add'])
Related
How I can identify in PHP if my current URL contains this text special_offer=12.
For example, if my domain URL is http://www.example.com/newproducts.html?special_offer=12 then echo something.
Alternatively, the domain can be http://example.com/all-products.html?at=93&special_offer=12
This text will always be the same: special_offer=12.
You can access the Query parameters using the $_GET superglobal, In this case:
if(isset($_GET['special_offer']) && $_GET['special_offer'] == 12){
echo "Something";
}
if (isset($_GET['special_offer'] {
do something...
}
Write var_dump($_GET);
to see how superglobal $_GET works
Just by checking the $_GET var ?
if(!empty($_GET["special_offer"]) && $_GET["special_offer"] ==12 ){
//You're code here
}
You Can Access To Query Parameter Using Get Like This:
if(isset($_GET['special_offer'] == 12)){
//echo " ECHO YOUR CODE HERE ";
}
OR
NOTE: Avoid To Use isset() and USE !empty() ..Because isset() accept the null value also to store , So avoid to use it !!
if(!empty($_GET['special_offer']) && $_GET['special_offer'] == 12){
//echo " ECHO YOUR CODE HERE ";
}
NOTE:
Empty checks if the variable is set and if it is it checks it for null, "", 0, etc
1 Isset just checks if is it set, it could be anything not null
2 With empty, the following things are considered empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
Reference: http://php.net/manual/en/function.empty.php
Sometimes PayPal breaks, and it doesn't return a tracking_id. At the same time, it completes the payment on it's own servers. This is a problem for me, because even thought the payment was successful, my server ends up with a blank tracking_id column in the database table, which causes me lots of issues
If this were to happen again, will the following if statement be enough to capture such an event?
if( !isset($trackingId) || empty($trackingId) || $trackingId == null || $trackingId == "" ) {
// something is wrong
} else {
// continue as normal
}
Use empty.
Determine whether a variable is considered to be empty. A variable is
considered empty if it does not exist or if its value equals FALSE.
empty() does not generate a warning if the variable does not exist.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
In PHP, What is the best way to check whether a string variable is empty or null?
I currently have the following:
if($variable == null || $variable == "")
exit;
And wondered if there was a more minimalistic way of doing this?
Thanks in advance
Use empty() see http://php.net/manual/en/function.empty.php
if(empty($myvariable))
exit();
Empty returns true if the variable is empty or null, full details below
From the doc
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty (return true):
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
if (empty($variable))
exit;
You could also use is_null() if you wanted to explicitly test whether or not the variable was set to the value null.
http://www.php.net/manual/en/function.empty.php
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
For checking whether a variable is null or empty please refer the below code snippet
if(is_null($variable) || empty($variable))
{
exit;
}
For documentation about is_null and empty functions please refer the below urls
http://php.net/manual/en/function.is-null.php
http://www.php.net/manual/en/function.empty.php
I want to check if the app parameter exists in the URL, but has no value.
Example:
my_url.php?app
I tried isset() and empty(), but don’t work. I’ve seen it done before and I forgot how.
Empty is correct. You want to use both is set and empty together
if(isset($_GET['app']) && !empty($_GET['app'])){
echo "App = ".$_GET['app'];
} else {
echo "App is empty";
}
empty should be working (if(empty($_GET[var]))...) as it checks the following:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
Here are your alternatives:
is_null - Finds whether a variable is NULL
if(is_null($_GET[var])) ...
defined - Checks whether a given named constant exists
if(defined($_GET[var])) ...
if( isset($_GET['app']) && $_GET['app'] == "")
{
}
You can simply check that by array_key_exists('param', $_GET);.
Imagine this is your URL: http://example.com/file.php?param. It has the param query parameter, but it has not value. So its value would be null actually.
array_key_exists('param', $_GET); returns true if param exists; returns false if it doesn't exist at all.
I want to prevent an empty value from going into a MySQL database
I'm using following but for some reason it is letting the empty values get through...
if (trim($var)= '' || !isset($var)){
header("Location:page.php?er=novariablel");
}
else {
...insert into database
}
Note, there is a bunch of complicated stuff that sets the value of var which is why I want to have both the ='' and the !isset because either might be the case.
Am I missing something with the or statement, i.e. it evaluates to false if both are true. Or what am I doing wrong?
You're lacking an = for your Equal Comparison Operator inside your if statement. Try:
if (trim($var) == '' || !isset($var)){
Try:
if (!isset($var) || empty(trim($var))){
empty() is a better way to check to see if a variable has no value. Just keep in mind that the following will return true:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
Try:
$var = trim($var);
if (!empty($var)){
//not empty
}
Try
if (strlen(trim($var)))==0
You should also have put a constraint in your database table attribute that it will not accept NULL values. Then your entry would have not been made in the database.
#John this is great advice.
empty(trim($var))
I've been programming for 6 years and I never thought of trimming the variable before checking to see if it's empty.