Skip to content

Instantly share code, notes, and snippets.

@AllenEllis
Last active February 15, 2019 19:39
Show Gist options
  • Save AllenEllis/fe8ee666b0868be66a1bb7144c19dfa2 to your computer and use it in GitHub Desktop.
Save AllenEllis/fe8ee666b0868be66a1bb7144c19dfa2 to your computer and use it in GitHub Desktop.

Revisions

  1. AllenEllis revised this gist Feb 15, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion php-include.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ Contents of `index.html`:
    <p>Contact info here</p>
    </footer>
    </body>

    </html>
    ```

    To seperate this out, we should consider which code we want to duplicate across every page, and move those over to separate files. We can make a new directory to hold those.
    @@ -67,4 +67,5 @@ New file: `templates/footer.php`
    <p>Contact info here</p>
    </footer>
    </body>
    </html>
    ```
  2. AllenEllis revised this gist Feb 15, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions php-include.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ For PHP code to execute, two things have to happen:
    1. The filename needs to end in `.php`
    2. The PHP code needs to exist between an opening PHP tag `<?php` and, optionally, a closing PHP tag `?>`

    ## Code before
    ## Original code

    Contents of `index.html`:
    ```html
    @@ -37,7 +37,7 @@ templates/
    - footer.php
    ```

    ## Code after
    ## Revised code
    The contents of `index.php` change to:
    ```php
    <?php
  3. AllenEllis created this gist Feb 15, 2019.
    70 changes: 70 additions & 0 deletions php-include.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    # PHP: introduction to `include();`

    For PHP code to execute, two things have to happen:
    1. The filename needs to end in `.php`
    2. The PHP code needs to exist between an opening PHP tag `<?php` and, optionally, a closing PHP tag `?>`

    ## Code before

    Contents of `index.html`:
    ```html
    <html>
    <head>
    <!-- header content here -->
    </head>
    <body>
    <h1>Page name</h1>
    <p>Page content</p>
    <footer>
    <p>Contact info here</p>
    </footer>
    </body>

    ```

    To seperate this out, we should consider which code we want to duplicate across every page, and move those over to separate files. We can make a new directory to hold those.

    ##### Before: directory structure
    ```
    index.html
    ```

    ##### After: directory structure
    ```
    index.php
    templates/
    - header.php
    - footer.php
    ```

    ## Code after
    The contents of `index.php` change to:
    ```php
    <?php
    include("templates/header.php");
    ?>
    <h1>Page name</h1>
    <p>Page content</p>

    <?php
    include("templates/footer.php");
    ?>

    ```

    New file: `templates/header.php`
    ```php
    <html>
    <head>
    <!-- header content here -->
    </head>
    <body>
    ```

    New file: `templates/footer.php`
    ```php
    <footer>
    <p>Contact info here</p>
    </footer>
    </body>
    ```