N402KC Posted April 14, 2010 Share Posted April 14, 2010 Howdy Yall, Ive spent $200 on these damn PHP books and none of em tell me how to run functions. Only how to write the sons of code. Can yall help me in trying to figure out how i can do this. Here is what i wanna do; For Example; function hello() { echo "Hello World"; } That code is in a page called acars.php. Now when the user loads the page acars.php, nothing happens, but when they go to the url in this format. http://mydomain.com/acars.php/hello , Then it will echo Hello World. Let me know what I can do and how I can do it please. Im startin to loose my patience lol. Thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/198455-run-function-via-url/ Share on other sites More sharing options...
ddubs Posted April 14, 2010 Share Posted April 14, 2010 All that block of code does is define the function and how it operates on data. To run the function you can do: <?php function hello() { echo "Hello World"; } hello(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/198455-run-function-via-url/#findComment-1041357 Share on other sites More sharing options...
N402KC Posted April 14, 2010 Author Share Posted April 14, 2010 I know how to run the function via code. But I want the function to code via the URL. So once the user goes to http://mydomain.com/acars.php/hello , It will run the function and display Hello World Quote Link to comment https://forums.phpfreaks.com/topic/198455-run-function-via-url/#findComment-1041358 Share on other sites More sharing options...
ddubs Posted April 14, 2010 Share Posted April 14, 2010 I know how to run the function via code. But I want the function to code via the URL. So once the user goes to http://mydomain.com/acars.php/hello , It will run the function and display Hello World O i gotcha - first post was a lil unclear You would have the a link like: <a href="./blah.php?run=hello">Say Hello</a> Then when someone clicks it you need to check the array $_GET for the 'run' array key. if(isset($_GET['run']) && $_GET['run'] == 'hello') { hello(); } Read up on how to use $_GET, $_POST and html forms ..etc. Quote Link to comment https://forums.phpfreaks.com/topic/198455-run-function-via-url/#findComment-1041359 Share on other sites More sharing options...
N402KC Posted April 14, 2010 Author Share Posted April 14, 2010 Yeah im aware of it. I really wanted to see if there was a easier way. I did use that way but it was a pain. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/198455-run-function-via-url/#findComment-1041364 Share on other sites More sharing options...
oni-kun Posted April 14, 2010 Share Posted April 14, 2010 Yeah im aware of it. I really wanted to see if there was a easier way. I did use that way but it was a pain. Thanks, Are you serious? If you want to have "file.php/hello" You'll need to use mod_rewrite which will require the $_GET anyway. Quote Link to comment https://forums.phpfreaks.com/topic/198455-run-function-via-url/#findComment-1041365 Share on other sites More sharing options...
Domcsore Posted April 14, 2010 Share Posted April 14, 2010 Agreed with oni-kun the easiest way is to use ddubs solution. Quote Link to comment https://forums.phpfreaks.com/topic/198455-run-function-via-url/#findComment-1041366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.