Jump to content

Fatal error: Cannot redeclare...


dalawh

Recommended Posts

I been trying ti figure this out for a few hours now and I can't seem to figure it out. Hopefully, someone will be able to solve my dilemma. The error I get is "Fatal error: Cannot redeclare getDate() in C:\xampp\htdocs\dalawh\date.php on line 4" test.php

<?phprequire 'date.php';$data=$last.' | '.$first.' | '.getDate().' | '.$_SERVER['SCRIPT_FILENAME'];echo $data;?>

date.php

<?phpfunction getDate(){echo date("l, M j, Y g:i:s A T");}?>

When I comment out "require 'date.php'", everything works fine, but of course getDate() returns nothing. So I figured it had to do with that line. I thought it may have been the echo in date.php, so I tried return, but I still got the same error.

Link to comment
Share on other sites

Guest So Called

I've pored over your code and I cannot see any flaw. Without mounting the code on my own server I have no idea. Combine the two into one script and tell us what happens:

<?php//require 'date.php';$data=$last.' | '.$first.' | '.getDate().' | '.$_SERVER['SCRIPT_FILENAME'];echo $data; function getDate(){echo date("l, M j, Y g:i:s A T");} ?>

Link to comment
Share on other sites

I've pored over your code and I cannot see any flaw. Without mounting the code on my own server I have no idea. Combine the two into one script and tell us what happens:
<?php//require 'date.php';$data=$last.' | '.$first.' | '.getDate().' | '.$_SERVER['SCRIPT_FILENAME'];echo $data; function getDate(){echo date("l, M j, Y g:i:s A T");} ?>

Yea, I could not figure it out. It is a pain. The reason that I am not combining them into one is because I am using the getDate() multiple times throughout multiple files.
Link to comment
Share on other sites

Guest So Called

No, you don't understand. Just try it as an experiment.. Whether or not it works you will learn more about your problem. This is not the final version. It's just a probe to understand more why your code does not work. Maybe you will gain an idea how to solve your problem after the experiment has been made.

Link to comment
Share on other sites

No, you don't understand. Just try it as an experiment.. Whether or not it works you will learn more about your problem. This is not the final version. It's just a probe to understand more why your code does not work. Maybe you will gain an idea how to solve your problem after the experiment has been made.
Does not seem to work. When I just echo the date, it works fine, but when I turn it into a function and call it, I get an error.
Link to comment
Share on other sites

Aren't you suppose to be returning the date instead of echo'ing?

function getDate(){  return date("l, M j, Y g:i:s A T");}

Edited by Err
Link to comment
Share on other sites

Ah yeah we realized that within minutes of each other. So now you know.
Yup :D I wish notepad++ had something built in that tells me that that function was already declared, but I guess I should of figured it from the error I got.
Link to comment
Share on other sites

Guest So Called

I should have figured it out from your error report, but it took me quite some time. In the end that's the only thing it could have been. PHP was telling us both that right from the beginning.

Link to comment
Share on other sites

Hope this isn't too complicated to understand. I even color coded it :D Let's say that in test.php, I require date.php and I use getDateInfo(). After that, I require misc.php and use funcOne() and funcTwo().In date.php, I have the function, getDateInfo().In misc.php, I have two functions: funcOne() and funcTwo(). In funcOne() and funcTwo(), I have getDateInfo(). My question is, do I have to require date.php inside funcOne() and funcTwo() to use the getDateInfo() or it isn't required because I already called it in test.php?

Link to comment
Share on other sites

Your color coding is so distracting that I can't understand your question.
I removed the colors :DLet's say that in test.php, I require date.php and I use getDateInfo(). After that, I require misc.php and use funcOne() and funcTwo().In date.php, I have the function, getDateInfo().In misc.php, I have two functions: funcOne() and funcTwo(). In funcOne() and funcTwo(), I have getDateInfo().My question is, do I have to require date.php inside funcOne() and funcTwo() to use the getDateInfo() or it isn't required because I already called it in test.php?
Link to comment
Share on other sites

Guest So Called

Sorry, my eyes are crossing. I think I'll have to pack it in tonight and hope you'll get some more useful replies from others.

Link to comment
Share on other sites

As long as you've included a file once, it is available for the rest of the script, no matter how many other files you include. To prevent a file being included multiple times accidentally, use include_once() or require_once().

Link to comment
Share on other sites

As long as you've included a file once, it is available for the rest of the script, no matter how many other files you include. To prevent a file being included multiple times accidentally, use include_once() or require_once().
Oh okay. require_once isn't even necessary for this case. I get an error when I use multiple require of the same file.
Link to comment
Share on other sites

what error are you refering? require_once() does not thro error if you accidently include same file it will just not include it second time and ignore it. though it throw error when file path does not match

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...