Jump to content

Problem with Function and Null Birth Year


doubledee

Recommended Posts

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

 

 

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().

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?! :shrug:

 

Thanks,

 

 

Debbie

 

 

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!

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

 

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.