How To resolve !empty function issue in php 5.4 - php

I used !empty function multiple places.
But It not woring on php 5.4.
It giving error can not use return type context.
So I create a function for checking null values in php.
function not_empty($value) {
if(trim($value)==null or trim($value) == '0' or trim($value)=="0"){
return false;
}
else{
return true;
}
}
So Can I use This Instead of !empty in my project.

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)
so no way you can consider your statement to be anywhere close to !empty in php
What you are doing is equivalent only to first condition i.e., empty string.
and also null too. But not false and 0.
I hope u got it

Related

PHP conditional error testing for value

Is $var==1 the proper syntax for testing whether $var has a value?
I ask, because I'm getting a failure on a simple conditional.
Here's the code that seems to be failing:
if ($slcustom31==1) {$page="http://www.mydomain.com/members/page_Alpha.php";}
I assume that there's something wrong with the syntax, $var==1 .
++++++++
Here's the entire conditional, if that makes things clearer:
//condition 1
if ($slcustom31==1) {$page="http://www.mydomain.com/members/page_Alpha.php";}
//condition2
elseif ($slcustom31!=1 AND $slcustom29!="") {$page="http://www.mydomain.com/members/".$slcustom29;}
//condition3
else {$page="http://www.mydomain.com/members/page_Beta.php";}
sl_redirecttourl($page);
SPECIFIC SYMPTOM:
Script ignores condition1 and condition2, defaults to condition3.
++++
And if it helps, here's the entire (short) script:
$groupswithaccess="somegroup";
require_once("../slpw/sitelokpw.php");
require_once("../slpw/sitelokapi.php"); //gets value being tested by conditional
if ($slcustom31==1) {$page="http://www.mydomain.com/members/page_Alpha.php";}
elseif ($slcustom31!=1 AND $slcustom29!="") {$page="http://www.mydomain.com/members/".$slcustom29;}//not finished, but started
else {$page="http://www.mydomain.com/members/page_Beta.php";}
sl_redirecttourl($page);
Can someone tell me what I should do differently?
Thanks!
if($var ==1){
//returns true only if var is 1
}
if(isset($var)){
//returns true if you currently have any value stored as the variable $var
}
No, you want to look at isset or empty.
To check if a variable has a value use:
empty($var);
If it's 'empty' it will return TRUE, and if not will return FALSE.
This is what's 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; (a variable declared, but without a value)
Fore more info see: http://php.net/manual/en/function.empty.php

What is the most bulletproof way of checking a variable is not null or empty?

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

How do I check if a $_GET parameter exists but has no value?

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.

PHP/MYSQL: check if variable is empty

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.

Should I use null to see if a variable is empty in PHP?

Which is better to use when I use _GET['something here'] for a variable to check if it is empty or not
if (isset($_GET['url']) != '') {
//do stuff with it
}
OR
if (isset($_GET['url']) != NULL) {
//do stuff with it
}
'' or null or something else?
Please don't call this over optimizing or micro optimizing, I am simply looking for best practices, thank you
Use empty() - it actually first test of the variable exists before testing whether something is in it.
[Edit: isset() returns only TRUE or FALSE, so both of the statements above work equally well]
You should do the following to be sure that the value both exists and isn't empty
if(isset($_POST[myField]) && $_POST[myField] != "") {
Do my PHP code
}
PHP can be a little painful when debugging blank/missing/empty value checks. You can use empty() or isset(), but remember the cases where empty returns true. It's highly liberal with what it considers empty.
Returns FALSE if var has a non-empty and non-zero value.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
isset is more conservative in that it only checks for variable's existence and a NULL value. From the documentation:
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

Categories