Forum Moderators: coopster

Message Too Old, No Replies

Fatal error: Cannot redeclare

         

Darylt

12:47 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



I have a php script that calculates the age of a person, later on in the table I want to do exactly the same for their dog.

After that works I will be looking to use a repeat region to show multiple people and dogs however when I add the scrip for the dog and the handler I get the following error.

Fatal error: Cannot redeclare

Is there a way that I can 'stop' the script so that it can run again?

Or any other way that I can make this work?

rob7591

12:52 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



You need to post some code so we can see what you're redeclaring.

Darylt

1:04 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



<?php

// Example Age Input
$DateOfBirth=$row_member_profile['handler_dob'];

// Calculate Age Using Function:
$Age= DetermineAgeFromDOB ($DateOfBirth);

// Display Results
echo $Age;

// Define the function
function DetermineAgeFromDOB ($YYYYMMDD_In)
{
// Parse Birthday Input Into Local Variables
// Assumes Input In Form: YYYYMMDD
$yIn=substr($YYYYMMDD_In, 0, 4);
$mIn=substr($YYYYMMDD_In, 4, 2);
$dIn=substr($YYYYMMDD_In, 6, 2);

// Calculate Differences Between Birthday And Now
// By Subtracting Birthday From Current Date
$ddiff = date("d") - $dIn;
$mdiff = date("m") - $mIn;
$ydiff = date("Y") - $yIn;

// Check If Birthday Month Has Been Reached
if ($mdiff < 0)
{
// Birthday Month Not Reached
// Subtract 1 Year From Age
$ydiff--;
} elseif ($mdiff==0)
{
// Birthday Month Currently
// Check If BirthdayDay Passed
if ($ddiff < 0)
{
//Birthday Not Reached
// Subtract 1 Year From Age
$ydiff--;
}
}
return $ydiff;
}
?>

omoutop

2:13 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thh error "Fatal error: Cannot redeclare" appears when you try to declare a specific function 2 or more times.
Check your included files... perhaps you include your function 2 times.

Darylt

4:38 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



I have done because I want to convert a date of birth to an age in two places.

How can I do this without using the above code twice?

eelixduppy

6:19 pm on Jul 28, 2008 (gmt 0)



>> to convert a date of birth to an age in two places.

If you are including a function that calculates the DOB already, then you don't need to redefine the function to use it again, you just need to simply call the function with the correct parameters. You should not need to rewrite the same code twice. Try removed your new function declaration and see if things are still working for you.

Darylt

6:53 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



I have now put the table that I wish the age to show up in into a repeat region.

I now get the error and nothing else is displayed after it.

If I remove the code that converts the date to an age everything works fine.

eelixduppy

7:33 pm on Jul 28, 2008 (gmt 0)



>>everything works fine.

So everything is good now?

Darylt

8:42 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



I have now managed to get it to work by including the code as a function.

Thanks for everyones patience

eelixduppy

3:49 am on Jul 29, 2008 (gmt 0)



Cool, thought so. Glad it's working now :)