Returning a value froma function

Discussion in 'PHP' started by mz906, Jun 25, 2008.

  1. #1
    I would like to return a value from a function how can this be done? with the following limitations:
    1. function is in another file
    2. i do not want to call the function

    example:

    
    // this would be in my functions file
    <?php function car(){
        $car_name = 'honda';
        return $car_name;
    };?>
    
    // page file
    <p><?php print $car_name; ?></p>
    
    Code (markup):
    I am aware that I can do this:
    
    <?php
    	$my_val = my_function(5,6,8);
    	print $my_val;
    ?> 
    
    Code (markup):
    but to me the above method seems redundant
     
    mz906, Jun 25, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    1. Its ok if your function is in another file,, just include() or require() it.
    2. Well theres a lot of method on how to do this, maybe sometimes you need to be redundant.

    well i guess you need to do it in OOP method, with global variables.
     
    bartolay13, Jun 25, 2008 IP
  3. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #3
    You obviously have to call the function at some point, or it will be never run. Why not just put the code in the global view?

    
    <?php
    
    function car()
    {
        $car_name = 'honda';
        return $car_name;
    }
    
    // Either::
    echo car();
    
    // Or:
    echo ($var = car());
    
    // Or:
    $var = car();
    echo $var;
    
    // Or
    function global_car()
    {
        $car_name = 'honda';
        global $car_name;
        return $car_name;
    }
    
    global_car();
    echo $car_name;
    
    // Or:
    
        $car_name = 'honda';
    // Now we always have it, problem solved.
    
    ?>
    
    PHP:
     
    Danltn, Jun 26, 2008 IP
  4. mz906

    mz906 Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hmmm, sorry i don't think i was clear on my limitiations...

    1. i am aware that the file needs to be included()
    2. i am aware that i can use this method
    
    function global_car()
    {
        $car_name = 'honda';
        global $car_name;
        return $car_name;
    }
    
    global_car();
    echo $car_name;
    
    Code (markup):
    (this is what i'm actually currently doing)

    in a nutshell I'm trying to use vairables the same way that DRUPAL, WordPress and other CMS' use them. for example in drupal you can just type:
    
    <?php print $content; ?>
    
    Code (markup):
    with out ever calling the function from within the file.

    i'm trying to slim down my code as much as possible.


    see i'll be re-using this and passing it along to someone else, that doesn't know that much php, and they want a list of variables to be used when they get the code, so they can just type:
    
    <?php
    print $header; 
    print $content;
    print $footer;
    ?>
    
    Code (markup):
    vs.
    
    <?php
    
    $h = header();
    $c = content();
    $f = footer();
    
    print $h;
    print $c;
    print $f;
    ?>
    
    Code (markup):
     
    mz906, Jun 26, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    You're missing the point. You HAVE to call a function either directly or through a callback or it won't run. If you don't want to have to call it put it in the global context.

    You could do

    $var = 'car';
    echo $var();

    But hey, still not what you want. There is nothing wrong with an extra line, just dump it in your includes file.

    It's not redundant at all. I put all the methods I can think of in my original post that don't use other functions.

    Dan
     
    Danltn, Jun 26, 2008 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    Perhaps it's a class you're looking to build;

    
    <?php
    
    class car{
    
      var $car_name;
    
      function print_name(){
         print $this->car_name;
      }
    }
    ?>
    
    PHP:
    On your main page;

    
    <?php
    include 'mycarfile.php';
    
    $car = new car;
    $car->car_name = "Honda";
    
    $car->print_name();
    
    ?>
    
    PHP:
     
    Weirfire, Jun 26, 2008 IP
  7. mz906

    mz906 Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yeah, i currently have the print statements in my include files so when i need to get/print my header i just type

    
    <?php print myHeader(); ?>
    
    Code (markup):
    I guess i still didn't word my question properly, i have my site working, my php functions work, BUT im more curious as to how this is done...

    again, i'm refereing to the CMS' drupal and wordpress they allow the user just to type in

    <?php print $breadcrumbs; ?> or <?php print $footer; ?>

    I know they are highly complex, but I just wanna know how do they acheive this? how is it they can set-up there code in away all where you can type a variable and not call the function, i am aware that there are other fiels involved, so if anyone knows how to set-up php to do this that would help, again...this is more of a question like "Hey how'd they do that?"

    maybe this is more of a question that should be posted in a drupal or wordpress forum? :(
     
    mz906, Jun 26, 2008 IP
  8. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #8
    This happens because the file is parsed by the respective template engine, and the $variables are filled in there.

    Dan
     
    Danltn, Jun 26, 2008 IP
  9. mz906

    mz906 Peon

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    AH, yes, thanks that makes senese :D
     
    mz906, Jun 26, 2008 IP