Example code:

<?php
function myFunction()
{
echo 'The function ran successfully.';
}

echo '<a href="xxxxx">Link to myFunction</a>';

?>

What do I put in place of xxxxx? I want the act of clicking the hyperlink to run myFunction().

Thanks in advance!

    I suppose you could do something like this:

    if ($_GET['run_func'] == 'yes') {
        myFunction();
    } else {
        echo '<a href="xxxxx?run_func=yes">Link to myFunction</a>';
    }
    
    function myFunction() 
    {
        echo 'The function ran successfully.';
    }
      <?php
      
      ///// functions /////
      
      function myFirst(){
          echo 'The First ran successfully.';
      }
      
      function mySecond(){
          echo 'The Second ran successfully.';
      }
      
      ///// START /////
      
      ?>
      <html><body>
      
      <?php
      
      if (isset($_GET['run'])) $linkchoice=$_GET['run'];
      else $linkchoice='';
      
      switch($linkchoice){
      
      case 'first' :
          myFirst();
          break;
      
      case 'second' :
          mySecond();
          break;
      
      default :
          echo 'no run';
      
      }
      
      ?>
      <hr>
      <a href="?run=first">Link to First</a>
      <br>
      <a href="?run=second">Link to Second</a>
      <br>
      <a href="?run=0">Refresh No run</a>
      
      </body></html>

      A devlopement based on Installer example in post above.
      Added so now there is 3 options in this page.
      I dont think there is much to explain.
      switch test if is 'first' or 'second'
      default is executed if is any other value

      I tested code at my php server and it works.
      🙂

        One question is why would you want to link to a function? Normally you would 'call' the funtion and provide a paramater(s) if any. What is the point? Why not have all your function in a file called functions and as an example this would have just one.

        <?php
        include("functions.php");//comment this out to test
        //here is the actual function that is unseen from the include
        function myFunction()//
        {
        echo 'The function ran successfully.';
        }//and below is the script of the page
        echo "<h2>I am going to call my function to see what happens with the include!</h2>";
        echo "<span style='color:green'><h1>";
        myFunction();
        echo "</h1></span>The above function call should be green and means it worked.";
        echo "<br />Wasn't that neato?";
        ?>
          Houdini wrote:

          One question is why would you want to link to a function?

          The "point" is that I want functions to run when users click hyperlinks. Instead of loading another separate php script, or passing URL parameters, I'd rather just call the function directly. This way, for small applications, I can do everything from within one php script, without having to pass a URL parameter like "index.php?fnc=do_something."

            JLHeidecker wrote:

            The "point" is that I want functions to run when users click hyperlinks.

            as you already have seen by running my script
            this is exactly what my script does:
            - run different functions when a user click a hyperlinks

            if you have any questions about this, just ask
            but think is very straight forwardcand easy code to understand

            <?php
            
            ///// functions /////
            
            function myFirst(){
                echo 'The First ran successfully.';
            }
            
            function mySecond(){
                echo 'The Second ran successfully.';
            }
            
            ///// START /////
            
            ?>
            <html><body>
            
            <?php
            
            if (isset($_GET['run'])) $linkchoice=$_GET['run'];
            else $linkchoice='';
            
            switch($linkchoice){
            
            case 'first' :
                myFirst();
                break;
            
            case 'second' :
                mySecond();
                break;
            
            default :
                echo 'no run';
            
            }
            
            ?>
            <hr>
            <a href="?run=first">Link to First</a>
            <br>
            <a href="?run=second">Link to Second</a>
            <br>
            <a href="?run=0">Refresh No run</a>
            
            </body></html>
              halojoy wrote:

              as you already have seen by running my script
              this is exactly what my script does:
              - run different functions when a user click a hyperlinks

              if you have any questions about this, just ask
              but think is very straight forwardcand easy code to understand

              Hi, thanks Halojoy. My previous post was responding to Houdini, who was asking why I would want to call a function from a hyperlink.

              Yes, of course your code works, there are many versions of that solution. It's what just about any developer would typically do.

              I was originally hoping there was a way to call a function directly from the hyperlink within the same php script, thus eliminating the need to reload the script, and have a test for the URL parameter.

              But after thinking, I realize that is not what I really want. I think I'm going to go back to passing URL parameters! 🙂

                5 years later

                For those who may actually need to do this, it is really quite simple:

                echo('<a href="' . YourFunction() . '">Run your function</a>');
                  RationalRabbit;10971652 wrote:

                  For those who may actually need to do this, it is really quite simple:

                  echo('<a href="' . YourFunction() . '">Run your function</a>');

                  Either you've misunderstood the topic, or you really misunderstand how PHP works. In your code, YourFunction() will be executed and it's output will be placed in the 'href' attribute of the HTML code.

                  It will not simply wait for the user to click the hyperlink and then execute the PHP function 'YourFunction'.

                    You are, of course, right. Not much thinking going on on my part this morning. (he said embarrassingly)

                      10 months later

                      Hey,
                      this thread has really Helped me out!

                      1 Question:

                      I'm using halojoy's code (works great!)
                      Would it be possible to pass a variable along with the call?
                      Without getting into the details, this would give me some more functionality.

                      Example:
                      Instead of:

                      <a href="?run=second">Link to Second</a>

                      Do something like:

                      <a href="?run=second($example_variable)">Link to Second</a>

                      ....I'm relatively new to PHP, so learning it as I go...
                      Thanks

                        <a href="?run=second&arg=stuff_here">Link to Second</a>
                        
                        ...
                        
                        case 'second' :
                            mySecond($_GET['args']);
                            break;

                          Thanks for the quick response Derokorian!
                          ...now, can you see why this isn't working?

                          <?php
                          
                          function myFirst(){
                              echo 'The First ran successfully.';
                          }
                          function mySecond(){
                              echo 'The Second ran successfully.';
                          }
                          function myThird($samplePassVariable){
                          	echo $samplePassVariable;
                          }
                          ?>
                          
                          
                          <?php
                          if (isset($_GET['run'])) $linkchoice=$_GET['run'];
                          else $linkchoice='';
                          
                          switch($linkchoice){
                          	case 'first' :
                          		myFirst();
                          		break;
                          
                          case 'second' :
                          	mySecond();
                          	break;
                          
                          case 'third' :
                          	myThird($_GET['args']);
                          	break;
                          
                          default :
                          	echo 'no run';
                          }
                          ?>
                          
                          <hr>
                          <a href="?run=first">Link to First</a>
                          <br>
                          <a href="?run=second">Link to Second</a>
                          <br>
                          <a href="?run=third&arg="Sample string to send">Link to Third</a>
                          <br>
                          <a href="?run=0">Refresh No run</a> 
                          

                            Because "arg" is not the same thing as "args".

                              Also look at the href attribute... href="?run=third&arg="Sample string to send" which says href="?run=third&arg=" because it will stop at the quote! A solution would be to A) not use quotes in the query string and/or (most likely and) 😎 use url_encode() on the string you want to put there. Example:

                              <hr> 
                              <a href="?run=first">Link to First</a> 
                              <br> 
                              <a href="?run=second">Link to Second</a> 
                              <br> 
                              <a href="?run=third&arg=<?php echo url_encode("Sample string to send"); ?>">Link to Third</a> 
                              <br> 
                              <a href="?run=0">Refresh No run</a>

                                Wonderful!

                                ...Now if I wanted to send an array, or a list of 5 variables, what would that look like?

                                Right now I'm creating the links in a loop, that recurses through a multidimensional array... but I'm having trouble passing more then one variable to the function. Any advice?

                                  One way to pass an array of values would be to append multiple instances of 'paramName[]' (where 'paramName' is any name you'd like to give the query parameter, of course). In other words, this query string:

                                  ?foo[]=one&foo[]=two&foo[]=three

                                  would then make $_GET['foo'] look like this:

                                  array(3) {
                                    [0]=>
                                    string(3) "one"
                                    [1]=>
                                    string(3) "two"
                                    [2]=>
                                    string(5) "three"
                                  }
                                    8 days later

                                    Thanks a lot guys! I really appreciate the help!

                                      a year later

                                      Hi,

                                      Sorry to resurrect such an old thread...

                                      I'm trying to run a function to alter the post_type in my wordpress query string (or to add a post type altogether). The function I'm using comes from here: http://www.addedbytes.com/blog/code/php-querystring-functions/

                                      Based on Halojoy's example, I've written this:

                                      <?php //add query to URL
                                      function add_querystring_var($url, $key, $value) {
                                          $url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
                                          $url = substr($url, 0, -1);
                                          if (strpos($url, '?') === false) {
                                              return ($url . '?' . $key . '=' . $value);
                                          } else {
                                              return ($url . '&' . $key . '=' . $value);
                                          }
                                      }
                                      ?>
                                      <?php 
                                      
                                      if (isset($_GET['run'])) $linkchoice=$_GET['run']; 
                                      else $linkchoice=''; 
                                      
                                      switch($linkchoice){ 
                                      
                                      case 'showAll' : 
                                          add_querystring_var($url, 'post_type', '');
                                      	break; 
                                      
                                      case 'showAnimation' : 
                                          add_querystring_var($url, 'post_type', 'animation');
                                          break; 
                                      
                                      case 'showPeople' : 
                                          add_querystring_var($url, 'post_type', 'people'); 
                                          break; 
                                      
                                      case 'showStudios' : 
                                          add_querystring_var($url, 'post_type', 'studios');
                                          break; 
                                      
                                      case 'showResources' : 
                                          add_querystring_var($url, 'post_type', 'resources'); 
                                          break; 
                                      
                                      case 'showWiki' :
                                      	add_querystring_var($url, 'post_type', 'wiki');
                                      	break;
                                      default : ;
                                      } 
                                      
                                      ?> 
                                      
                                      <h2>Show... </h2>
                                      <a href="?run=showAll">All</a>
                                      <a href="?run=showAnimation">Animation</a>
                                      <a href="?run=showPeople">People</a>
                                      <a href="?run=showStudios">Studios</a>
                                      <a href="?run=showResources">Resources</a>
                                      <a href="?run=showWiki">Wiki</a>

                                      When I click any of these links, they take me to "mywebsite.com/?run=showX" - which in turn directs me to my front page. I think this means that the function is not running, and I'm wondering if anyone has any suggestions as to why. I've tried testing without the function, and with just echoes indicating that a function has ran, instead - and these haven't done anything. I am still directed to the url indicated in the link and nothing beyond that seems to happen. Have I set the function to run incorrectly?

                                      Thanks in advance!

                                        Write a Reply...