Jump to content
dalawh

Fatal error: Cannot redeclare...

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.

Share this post


Link to post
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");} ?>

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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.
Yea. Now I am waiting on the mods :D since they are the ones who usually help me.

Share this post


Link to post
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().

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...