This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if a variable is empty
Simple PHP question:
I have this stement:
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
...//IF PASSWORD IS CORRECT STUFF WILL HAPPEN HERE
}
Somewhere above this statement I use the following line in my JavaScript to set the username as a variable both in my JavaScript and in my PHP:
uservariable = <?php $user = $_POST['user']; print ("\"" . $user . "\"")?>;
What I want to do is add a condition to make sure $user is not null or an empty string (it doesn't have to be any particular value, I just don't want it to be empty. What is the proper way to do this?
I know this is a sill question but I have no experience with PHP. Please advise, Thank you!
Null OR an empty string?
if (!empty($user)) {}
Use empty().
After realizing that $user ~= $_POST['user'] (thanks matt):
var uservariable='<?php
echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
Use empty(). It checks for both empty strings and null.
if (!empty($_POST['user'])) {
// do stuff
}
From the manual:
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)
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
I am working with PHP 5.4 and I am submitting a form via POST request, and I want to check to see if one of the values is null, or populated. However the condition does not appear to resolving to true and I can't figure out what I'm doing wrong.
Here is the code:
$route = $_POST['route'];
echo ("route: " . $route);//this is displaying 'route:null'
if(empty($route))
{
echo ("route2: " . $route);
}
I have also tried to use isset is_null and $route === null $route == null
None seem to work .
I have also tried inserting true into the if statement to make sure that true is resolving correctly, but it does.
If the echo is displaying null then it is not null. It is a string "null" which are two entirely different things.
This, in fact is a common problem when using javascript to post to php. Javascript's null gets converted to string when posting to php, thus passing all checks (empty(), isset(), ...).
The easiest way to see these issues is to do a var_dump($_POST), which will give you exactly what you have received in your post.
In your case you are receiving string "null", which in no way can pass the empty() check. You either need to check if $route=="null" or fix your javascript so it does not send null values.
Fixing javascript is the proper way to go.
The POST variable does not contain anything. Either it does not exist on the form or you have it misspelled.
According to the docs null is empty.
http://us3.php.net/empty
Returns FALSE if var exists and has a non-empty, non-zero value.
Otherwise returns 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; (a variable declared, but without a value)
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.
How about try using (or adding):
if(is_null($route))
or
if(!isset($route))
if( isset($_POST['route']) && !empty($_POST['route']) ) {
$route = $_POST['route'];
echo "Route: $route";
} else {
echo "Route: None specified";
}
Because the $_POST is returning null as a string I had to do this:
$route = $_POST['route'];
echo ("route: " . $route);
if($route == 'null')//surround null in quotes
{
echo ("route2: " . $route);
}
Try This ;)
function convert_null($data)
{
$data = str_replace(null,"",$data);
$data = str_replace("null","",$data);
$data = trim($data);
return $data;
};
convert_null($route);
if(empty($route))
{
echo ("route2: " . $route);
};
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if a variable is empty
Simple PHP question:
I have this stement:
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
...//IF PASSWORD IS CORRECT STUFF WILL HAPPEN HERE
}
Somewhere above this statement I use the following line in my JavaScript to set the username as a variable both in my JavaScript and in my PHP:
uservariable = <?php $user = $_POST['user']; print ("\"" . $user . "\"")?>;
What I want to do is add a condition to make sure $user is not null or an empty string (it doesn't have to be any particular value, I just don't want it to be empty. What is the proper way to do this?
I know this is a sill question but I have no experience with PHP. Please advise, Thank you!
Null OR an empty string?
if (!empty($user)) {}
Use empty().
After realizing that $user ~= $_POST['user'] (thanks matt):
var uservariable='<?php
echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
Use empty(). It checks for both empty strings and null.
if (!empty($_POST['user'])) {
// do stuff
}
From the manual:
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)
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.