PHP using empty, isset or is_null in a ternary - php

My PHP is rusty and I'm struggling to figure this out. I'm working with forum software (SMF) and can't really tell the value of the variable I'm trying to check.
All members are assigned a post_group e.g Senior Member
Some users are assigned a special group e.g Moderator.
I want to set the variable $memberType to the group if there is one, if not the post_group.
E.g
User is Moderator and Senior Member. $memberType = 'Moderator'
User is just Junior Member. $memberType = 'Junior Member'
Here's what I've tried:
$memberType = is_null($message['member']['group']) ? $message['member']['group'] : $message['member']['post_group'];
I also tried empty() and isset() instead of is_null but none of them seem to get me a consistent result.
is_null only applies the post_group, empty() only applies the post_group but weirdly not for my "newbie" class. isset() only applies the post.
Any way I can get a consistent result?

I guess you missed a ! in your question. empty should work in your scenario. Acc to the man page of 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)
So to sum up something like this should work.
$memberType = empty($message['member']['group']) === FALSE ? $message['member']['group'] : $message['member']['post_group'];

What you probably need is :
$memberType = empty($message['member']['group']) ? $message['member']['post_group'] : $message['member']['group'];
Basically is says (pseudocode):
IF $message['member']['group'] IS EMPTY :
$memberType = $message['member']['post_group'];
ELSE :
$memberType = $message['member']['group'];
empty():
The following value 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)

Related

PayPal's tracking id

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)

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.

Categories