Grow your CSS skills. Land your dream job.

Return contents of a php file in a function

  • # June 17, 2012 at 9:25 am

    Hi

    I’m trying to output the whole contents of a php file in a function.
    I have this code,

    function tep_pass_forgot() {

    $file = file_get_contents(FILENAME_PASSWORD_FORGOTTEN);

    return $file;
    }

    FILENAME_PASSWORD_FORGOTTEN is defined as password_forgotten.php.

    But this outputs me this:

    add_session('login', SUCCESS_PASSWORD_SENT, 'success'); tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL')); } else { $messageStack->add('password_forgotten', TEXT_NO_EMAIL_ADDRESS_FOUND); } } $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_LOGIN, '', 'SSL')); $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_PASSWORD_FORGOTTEN, '', 'SSL')); require(DIR_WS_INCLUDES . 'template_top.php'); ?>
    size('password_forgotten') > 0) { echo $messageStack->output('password_forgotten'); } ?>

    This looks like a section from my file but I want it to output the same way it would if i opened the file from the url.

    Any ideas how to make this work to show the output of the php file?

    # June 17, 2012 at 10:25 am

    Can’t you use INCLUDE for this?

    include password_forgotten.php;
    # June 17, 2012 at 12:06 pm

    Include wouldn’t work if he wanted to get the output as opposed to the actual php file’s content. The question isn’t clear as to which it is.

    If you want the actual file contents, use include. If you want the output as would be seen in a browser querying the script, well, you just need a more intelligent way of producing output. Try to break down how the script works out what to produce at the end, and make sure that the output is ultimately produced by a single function.

    Using an OO approach is a good way to be doing this from the start, but if you have a more “procedural” approach, it’s not hard to move your logic into functions instead of them being top level.

    # June 17, 2012 at 12:45 pm

    @BenWalker

    Yes I want to output as would be seen in a browser so the include won’t work.
    I’m not that good in writing php, can you give me a more specific idea with a code example. Would I use a completely different code or do I need to add on to what i have?

    # June 17, 2012 at 4:00 pm

    Not sure what’s the difference (maybe I just misunderstand what you want). Do you want to show the contents of the PHP file before it’s parsed (the unprocessed source code) or the output that the file gives after it’s parsed (the result)?

    Calling the file straight to the URL would result in parsed, processed code, wouldn’t it? I thought that’s what you wanted, which is why I suggested include (wouldn’t that show the output of the processed PHP file?)

    # June 17, 2012 at 4:06 pm

    @Sneff

    I tried the include statement you suggested but it outputs nothing (blank).

    I want to show the result of the contents in the php file, just like you said.

    # June 17, 2012 at 5:25 pm

    Include pulls the php code, not the output, into the script.

    This is a really basic example of what I’m getting at. Let’s say you have a script called show_table.php. show_table.php is a page that contains a table with data in it. The table is an object that is manipulated by other scripts, as it is connected to a database. The table might, therefore, have its own class, contained in MyTable.php:

    < ?php
    class MyTable
    {
    var $output; // output string

    function ConstructTable()
    {
    $this->output = "
    Header OneHeader Two
    Column OneColumn Two
    ";
    }

    function Output()
    {
    return $this->output;
    }
    }
    ?>

    show_table.php, when displaying the table, will also take advantage of a header and footer page. If the header and footer are always the same, then their codes can be placed in separate files (header.php and footer.php) and simply included, since no processing needs to take place. show_table.php therefore probably looks like this:

    < ?php
    include( "header.php" );
    include( "MyTable.php" );
    $myTable = new MyTable();
    $myTable->ConstructTable();
    echo $myTable->Output();
    include( "footer.php" );
    ?>

    If you then needed to include the same table in any other page, you can now include it in exactly the same way. In the same vein, the MyTable class could have other functions than ConstructTable and Output (maybe editing, deleting, etc) that show_table.php doesn’t use, but a new php file called screw_with_data.php does. Do you see?

    Compartmentalise your code so it is reusable. The problem you are having stems from your code’s structure.

    # June 17, 2012 at 6:17 pm

    Sorry — the above post used “$this->output += …” when it should have just been “$this->output = …”. I have edited the post, but apologies to anyone who may have actually tried it out. I wrote it on the page, so hadn’t actually tested it.

    @Al3ks
    If you want to see a complete example of what I’m trying to describe, I’ve written the code up into a working module with some annotations.

    show_table.zip

    # June 18, 2012 at 10:31 am

    include can also have a return result and be assigned to a variable.

    included_file.php

    < ?php
    $str = "Hello, world";
    return $str;

    your_script.php

    < ?php

    $output = my_inc_func();
    print $output; // prints "Hello, world"

    function my_inc_func(){
    $str = include( "included_file.php" );
    return $str;
    }
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".