Asked  7 Months ago    Answers:  5   Viewed   35 times

If I want to execute a php script, i just point the browser to www.something.com/myscript.php

But if i want to execute a specific function inside myscript.php, is there a way? something like www.something.com/myscript.php.specificFunction

Thanks!

 Answers

31

One quick way is to do things like

something.com/myscript.php?f=your_function_name

then in myscript.php

if(function_exists($_GET['f'])) {
   $_GET['f']();
}

But please, for the love of all kittens, don't abuse this.

Wednesday, March 31, 2021
 
Litty
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
58

You can't call PHP functions directly. However, if the php file is formatted how you displayed here with only two functions you could pass a flag in as a POST field and have a logic block determine which function call based on that flag. It's not ideal but it would work for your purposes. However, this would force the page to refresh and you would probably have to load a different page after the function returns so you may want to simply implement this as an ajax call to avoid the page refresh.

Edit based on your comment (Using jQuery Ajax):

I am using the .ajax() jQuery function. My intentions create one php file that contains multiple functions for different html forms. – tmhenson

Well in this case, you can have your file contain the logic block to "route" the request to the right function and finally return the result.

I'll try to provide an option for what you want to do based on some simple jQuery. Say you have something like this:

Java Script:

$("#button1").click(function(e){ 
    e.preventDefault(); // prevents submit event if button is a submit
    ajax_route('ap'); 
}); 
$("#button2").click(function(e){ 
    e.preventDefault(); // prevents submit event if button is a submit
    ajax_route('se'); 
});

function ajax_route(action_requested){ 
    $.post("ajax_handler.php", {action : action_requested}, function(data){
        if (data.length>0){ 
            alert("Hoorah! Completed the action requested: "+action_requested); 
        } 
    })
}

PHP (inside ajax_handler.php)

<?php
// make sure u have an action to take
if(isset($_POST['action']) 
    switch($_POST['action']){
        case 'ap': addPerson(); break;
        case 'se': sendEmail(); break;
        default: break;
    }
}

function addPerson() {
    //do stuff here 
}

function sendEmail() {
    //do some stuff here 
}
?>
Wednesday, March 31, 2021
 
relipse
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
30

This is how I do it. It works like a charm.

# shell execute PHP
def php(code):
  # open process
  p = Popen(['php'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, close_fds=True)

  # read output
  o = p.communicate(code)[0]

  # kill process
  try:
    os.kill(p.pid, signal.SIGTERM)
  except:
    pass

  # return
  return o

To execute a particular file do this:

width = 100
height = 100

code = """<?php

  include('/path/to/file.php');
  echo start(""" + width + """, """ + height + """);

?>
"""
res = php(code)
Saturday, May 29, 2021
 
Whakkee
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :