dalawh Posted June 23, 2012 Share Posted June 23, 2012 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 More sharing options...
Guest So Called Posted June 23, 2012 Share Posted June 23, 2012 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 More sharing options...
dalawh Posted June 23, 2012 Author Share Posted June 23, 2012 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 More sharing options...
Guest So Called Posted June 23, 2012 Share Posted June 23, 2012 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 More sharing options...
dalawh Posted June 23, 2012 Author Share Posted June 23, 2012 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 More sharing options...
Err Posted June 23, 2012 Share Posted June 23, 2012 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");} Link to comment Share on other sites More sharing options...
dalawh Posted June 23, 2012 Author Share Posted June 23, 2012 I solved it... getdate() is a function that was already built in. It seems it is case insensitive. http://www.php.net/manual/en/function.getdate.php Link to comment Share on other sites More sharing options...
Guest So Called Posted June 23, 2012 Share Posted June 23, 2012 Oops. You stepped on PHP: http://php.net/manua...ion.getdate.php Change your function name. Link to comment Share on other sites More sharing options...
Guest So Called Posted June 23, 2012 Share Posted June 23, 2012 Ah yeah we realized that within minutes of each other. So now you know. Link to comment Share on other sites More sharing options...
dalawh Posted June 23, 2012 Author Share Posted June 23, 2012 Ah yeah we realized that within minutes of each other. So now you know.Yup 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 More sharing options...
Guest So Called Posted June 23, 2012 Share Posted June 23, 2012 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 More sharing options...
dalawh Posted June 23, 2012 Author Share Posted June 23, 2012 Hope this isn't too complicated to understand. I even color coded it 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 More sharing options...
Guest So Called Posted June 23, 2012 Share Posted June 23, 2012 Your color coding is so distracting that I can't understand your question. Link to comment Share on other sites More sharing options...
dalawh Posted June 23, 2012 Author Share Posted June 23, 2012 Your color coding is so distracting that I can't understand your question.I removed the colors 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 More sharing options...
Guest So Called Posted June 23, 2012 Share Posted June 23, 2012 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 More sharing options...
dalawh Posted June 23, 2012 Author Share Posted June 23, 2012 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 since they are the ones who usually help me. Link to comment Share on other sites More sharing options...
Ingolme Posted June 23, 2012 Share Posted June 23, 2012 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 More sharing options...
dalawh Posted June 23, 2012 Author Share Posted June 23, 2012 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 More sharing options...
birbal Posted June 23, 2012 Share Posted June 23, 2012 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.