Calling a function in a PHP file

Discussion in 'PHP' started by webboy, Mar 8, 2011.

  1. #1
    Hi

    is this possible to call a function within a php file

    as i wish to call a function within the wordpres function.php file from the command prompt some thing like

    > php function.php hello2

    can this be done ?

    eg
    function.php =

    function hello1 () {

    echo 'hello';

    }

    function hello2 () {

    echo 'hello';

    }
     
    webboy, Mar 8, 2011 IP
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
  3. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Way over my head and that did not make any sense at all
     
    webboy, Mar 8, 2011 IP
  4. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #4
    can you be a bit more elaborate on what you're trying to achieve? why would you want to call a PHP function from command prompt?
     
    Grit., Mar 8, 2011 IP
  5. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    To the question as i have the shell access to run the script , i dont wish to run the function over the web server plus i see what the problems will be from standard out
     
    webboy, Mar 8, 2011 IP
  6. ferostive

    ferostive Active Member

    Messages:
    162
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    include("function.php");
    hello2 ();
    
    PHP:
     
    ferostive, Mar 9, 2011 IP
  7. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    we are taking about the shell command line here not inside a include script .
     
    webboy, Mar 9, 2011 IP
  8. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #8
    Not tested, but you get the idea. Use func_get_arg to get the function name and call it like $functioname().

    func_get_arg(0) is the filename, then starts the options. You can use --option-name syntax if you want as well (like shell scripts do).

    
    <?php
    
    if (isset(func_get_arg(1)) && is_function(func_get_args(1))) {
        {func_get_args(1)}();
        // Pass variable params as well if you want, but check for them
        // {func_get_args(1)}(func_get_args(2));
    } else {
        die('Invalid function call');
    }
    
    
    function hello() { 
        echo 'Hello';
    }
    
    function hello2()
    {
        echo 'Hello 2';
    }
    
    
    PHP:
     
    ThePHPMaster, Mar 10, 2011 IP
  9. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    hmm nice i will give that a go and test it out

    Thanks
     
    webboy, Mar 11, 2011 IP
  10. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #10
    require(); is always best solution how to include important files ;)
     
    G3n3s!s, Mar 11, 2011 IP
  11. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    (G3).. read the full post ass this is not over the web-sever its via the command prompt shell and it wordpress so we are to calling any other file but-in in the function one
     
    webboy, Mar 11, 2011 IP