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
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

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; 
}

Your code looks more readable, I think this will show where the error happened soon. More user friendly code
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

dhrosti wrote:Does php not treat empty variables as false? if it does you can strip out all the "!empty()"s
No, it treats them as empty.
Post Reply