Variable can't be empty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Variable can't be empty

Post by iknownothing »

Hey Guys,
I'm struggling for a way to write a clean piece of code, that if a certain variable is not empty, then a certain group of variables cant be empty, otherwise it is an error.

Can anyone help?

Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Start with empty(). After you write some code we can review, post it so we can dive a little deeper.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

so far i have this, and it looks messy..

Code: Select all

$allgood = 0;
	if (!empty($domaincompany)){
		if(!empty($domainexp)){
			if(!empty($domainuser)){
				if(!empty($domainpass)){
					if(!empty($domainregkey)){
						$allgood = $allgood + 1;
					}else { 
						$save_result = "<b>Please fill in ALL Domain categories</b>";}
				}else { 
					$save_result = "<b>Please fill in ALL Domain categories</b>";}
			}else { 
				$save_result = "<b>Please fill in ALL Domain categories</b>";}
		}else { 
			$save_result = "<b>Please fill in ALL Domain categories</b>";}
	}else { 
		$save_result = "<b>Please fill in ALL Domain categories</b>";}
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

iknownothing wrote:

Code: Select all

$allgood = 0;
	if (!empty($domaincompany)){
		if(!empty($domainexp)){
			if(!empty($domainuser)){
				if(!empty($domainpass)){
					if(!empty($domainregkey)){
						$allgood = $allgood + 1;
					}else { 
						$save_result = "<b>Please fill in ALL Domain categories</b>";}
				}else { 
					$save_result = "<b>Please fill in ALL Domain categories</b>";}
			}else { 
				$save_result = "<b>Please fill in ALL Domain categories</b>";}
		}else { 
			$save_result = "<b>Please fill in ALL Domain categories</b>";}
	}else { 
		$save_result = "<b>Please fill in ALL Domain categories</b>";}
instead

Code: Select all

$allgood = 0;
if((!empty($domaincompany)&&(!empty($domainexp)&&(!empty($domainuser)&&(!empty($domainpass)&&(!empty($domainregkey)){
$allgood = $allgood + 1;

}else{

$save_result = "<b>Please fill in ALL Domain categories</b>";

}
:P Hopefully short. Can there be shorter code than this.... Anybody??
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Code: Select all

$allgood = 0;
((!empty($domaincompany)&&(!empty($domainexp)&&(!empty($domainuser)&&(!empty($domainpass)&&(!empty($domainregkey))? $allgood++ :$save_result = "<b>Please fill in ALL Domain Categories</b>" ;
Alteranate version of above short code, untested , but I see still it can be reduced. Come on guys this again a code challenge for shortest code :drunk: :twisted:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Shorter, maybe. Readable, not really.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

hmm not readable :roll: I fail at making code readable, I'm getting bigger expressions, who's coming with perfect , shorter, readable code ?? Anybody??
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

This is ugly because you're using a "group of variables" instead of arrays or objects.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Come on.. I did with whatever the data available. there are no arrays in the above given code. Moreover assigning all of the variables to an array will make the code big... I'm talking about shorter code sterofrog :wink:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

an array would be beneficial.. besides which, chances are this is for a form, so the variables will be in $_POST/$_GET anyway.. thusly:

Code: Select all

if (in_array("", $_POST)) $save_result = "<b>Please fill in ALL Domain Categories</b>";
but explicitly checking indexes would be better.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Think about making this code better, not "shorter". Start with decent data structure.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I don't like that code. It's only testing if the field is completed, it's not actually validating it at all.

Code: Select all

$errors = array();
$strErrorMessage = "";
if(empty($domaincompany)) { $errors['domaincompany'] = "Domain Company"; }
if(empty($domainexp)) { $errors['domainexp'] = "Domain Expiry Date"; }
if(empty($domainuser)) { $errors['domainuser'] = "Domain User"; }
if(empty($domainpass)) { $errors['domainpass'] = "Domain Password"; }
if(empty($domainregkey)) { $errors['domainregkey'] = "Domain Reg Key"; }
if (count($errors)>0) {
    $strErrorMessage = "You must complete the following fields: <br>".implode("<br>",$errors)."<br>";
}
if (!checkValidDate($domainexp)) { $strErrorMessage .= "Domain Expiry Date is not a valid date.<br>"; }
if (!checkValidUser($domainuser)) { $strErrorMessage .= "Domain User is not valid.<br>"; }
if (!checkValidRegKey($domainregkey)) { $strErrorMessage .= "Domain Reg Key is not valid.<br>"; }

if (empty($strErrorMessage)) {
    $allgood = $allgood + 1;
} else {
    $save_result = $strErrorMessage;
}
The reason for putting errors into an associative array is so that in the form you can have something like:

Code: Select all

echo "<label class=\"labelClass".(array_key_exists('domaincompany',$errors)?" errorClass":"")."\">Domain Company</label>";
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

Does php not treat empty variables as false? if it does you can strip out all the "!empty()"s
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

A mix with Jenk's code makes the code better

Code: Select all

$allgood=0;
$list=array($domaincompany,$domainexp,$domainuser,$domainpass,$domainregkey);
      if (in_array("", $list)) $save_result = "<b>Please fill in ALL Domain Categories</b>";
$allgood = $allgood + 1;
So is there any others code better than this??
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

As Feyd said, it's not better, it's just shorter. When you're validating incoming data you need to be as verbose as possible.
Post Reply