doubledee Posted June 2, 2012 Share Posted June 2, 2012 I'm having a problem with my function when there is no Birth Year. In the Member's Profile I have this... <dd> " . getAgeRange($birthYear) . "</dd> And then here is the Function... function getAgeRange($birthYear){ /** * Returns Age-Range label * * Takes an Estimated Age (e.g. 31) and converts to Age-Range (e.g. 30-something). * * @param integer $birthYear * @return string */ $currYear = date("Y"); $estimatedAge = ($currYear - $birthYear - 1); if ($estimatedAge < 20){ $ageRange = 'Youth'; }else if ($estimatedAge < 30){ $ageRange = '20-something'; }else if ($estimatedAge < 40){ $ageRange = '30-something'; }else if ($estimatedAge < 50){ $ageRange = '40-something'; }else if ($estimatedAge < 60){ $ageRange = '50-something'; }else if ($estimatedAge < 70){ $ageRange = '60-something'; }else{ $ageRange = 'Senior'; } // if (!isset($birthYear)){ if (is_null($birthYear)){ $ageRange = 'unknown'; } return $ageRange; }//End of getAgeRange Whether I use !isset() or is_null() I keep getting 'Senior' when a person creates a new Account which would have a NULL in the birthYear field. What am I doing wrong? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263546-problem-with-function-and-null-birth-year/ Share on other sites More sharing options...
silkfire Posted June 2, 2012 Share Posted June 2, 2012 if (!isset($birthYear)) {} should be the first line in that function, not last. But it's best to use "if empty($birthYear)" instead, because '' is still a variable and it's set. 0 and '' (empty string) evaluate TRUE for empty(). Quote Link to comment https://forums.phpfreaks.com/topic/263546-problem-with-function-and-null-birth-year/#findComment-1350640 Share on other sites More sharing options...
doubledee Posted June 2, 2012 Author Share Posted June 2, 2012 if (!isset($birthYear)) {} should be the first line in that function, not last. But it's best to use "if empty($birthYear)" instead, because '' is still a variable and it's set. 0 and '' (empty string) evaluate TRUE for empty(). Okay, lots going on here... 1.) I get $birthYear from the Member table in my database. When a User registers, I don't ask for their Birth-Year up front, as I figure it might scare them off. As such, $birthYear will always start off as NULL. 2.) I forgot I did this, but I actually had this line of code in another place in my "profile.php" script... $birthYear = (isset($birthYear) ? $birthYear : 0); Regardless of my Function, should I have this in my code? Should NULL's be erased out, or are they not evil necessarily? 3.) So, I commented out the line above to allow NULL's to exist, and I added this to my Function... function getAgeRange($birthYear=1970){ ...which should have fired off this but didn't... }else if ($estimatedAge < 50){ $ageRange = '40-something'; Why didn't that work? If $birthYear is NULL in the database, should the default of 1970 kick in? If I add this line inside the Function then it fires that condition off... //$birthYear=1970; Hope I am making some sense?! Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263546-problem-with-function-and-null-birth-year/#findComment-1350659 Share on other sites More sharing options...
silkfire Posted June 2, 2012 Share Posted June 2, 2012 Look, Debbie. NULL values from a database are transformed into empty strings ('') upon retrieval. Also, '' is converted to 0 when using arithmetics. This line right here $estimatedAge = ($currYear - $birthYear - 1); is interpreted like this by PHP: $estimatedAge = 2012 - 0 - 1; Which in turn is 2011. If a person is 2011 years old no wonder he's senior! Quote Link to comment https://forums.phpfreaks.com/topic/263546-problem-with-function-and-null-birth-year/#findComment-1350662 Share on other sites More sharing options...
silkfire Posted June 2, 2012 Share Posted June 2, 2012 By the way, the reason that didn't trigger was because you're still supplying a birth year, in this case an empty string. If you call your function with no arguments, THEN the default value is 1970: echo getAgeRange(); Quote Link to comment https://forums.phpfreaks.com/topic/263546-problem-with-function-and-null-birth-year/#findComment-1350663 Share on other sites More sharing options...
doubledee Posted June 3, 2012 Author Share Posted June 3, 2012 Silkfire, So please help me put all of this together... 1.) Should I have this in my script calling the Function... $birthYear = (isset($birthYear) ? $birthYear : 0); Or is letting a NULL come through from my database okay? 2.) Should I have some default parameter like this... function getAgeRange($birthYear=''){ (Actually, that one doesn't work because it comes up as "Senior" and I wanted "unavailable".) 3.) What is the best and safest way to have "undefined" be returned by the Function is the User has not entered a Birth Year yet? There appear to be several ways to do things, and I'm not sure which would be the best and safest? Here is what I have... function getAgeRange($birthYear){ /** * Returns Age-Range label * * Takes an Estimated Age (e.g. 31) and converts to Age-Range (e.g. 30-something). * * @param integer $birthYear * @return string */ $currYear = date("Y"); $estimatedAge = ($currYear - $birthYear - 1); if ($estimatedAge < 20){ $ageRange = 'Youth'; }else if ($estimatedAge < 30){ $ageRange = '20-something'; }else if ($estimatedAge < 40){ $ageRange = '30-something'; }else if ($estimatedAge < 50){ $ageRange = '40-something'; }else if ($estimatedAge < 60){ $ageRange = '50-something'; }else if ($estimatedAge < 70){ $ageRange = '60-something'; }else{ $ageRange = 'Senior'; } if (is_null($birthYear)){ $ageRange = 'unknown'; } return $ageRange; }//End of getAgeRange Since I'm a newbie, I always fear some extreme value like a "0" or NULL will blow up my code because I failed to think of when something like could happen... Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263546-problem-with-function-and-null-birth-year/#findComment-1350697 Share on other sites More sharing options...
silkfire Posted June 3, 2012 Share Posted June 3, 2012 Start by removing: if (is_null($birthYear)){ $ageRange = 'unknown'; } from the end of the function. Then at the start of the function, add: if (empty($birthYear)) return 'unknown'; Piece a pie! Quote Link to comment https://forums.phpfreaks.com/topic/263546-problem-with-function-and-null-birth-year/#findComment-1350698 Share on other sites More sharing options...
doubledee Posted June 3, 2012 Author Share Posted June 3, 2012 Start by removing: if (is_null($birthYear)){ $ageRange = 'unknown'; } from the end of the function. Then at the start of the function, add: if (empty($birthYear)) return 'unknown'; Piece a pie! Looks like this might be safer... function getAgeRange($birthYear=NULL){ /** * Returns Age-Range label * * Takes an Estimated Age (e.g. 31) and converts to Age-Range (e.g. 30-something). * * @param integer $birthYear * @return string */ if (empty($birthYear)){ return 'unknown'; } $currYear = date("Y"); $estimatedAge = ($currYear - $birthYear - 1); if ($estimatedAge < 20){ $ageRange = 'Youth'; }else if ($estimatedAge < 30){ $ageRange = '20-something'; }else if ($estimatedAge < 40){ $ageRange = '30-something'; }else if ($estimatedAge < 50){ $ageRange = '40-something'; }else if ($estimatedAge < 60){ $ageRange = '50-something'; }else if ($estimatedAge < 70){ $ageRange = '60-something'; }else{ $ageRange = 'Senior'; } return $ageRange; }//End of getAgeRange Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263546-problem-with-function-and-null-birth-year/#findComment-1350709 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.