Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Building a Media Library in PHP Basic PHP and Including Files

Mphammad Arifi
Mphammad Arifi
147 Points

PHP commonad

to link to others

Mphammad Arifi
Mphammad Arifi
147 Points

how can i link PHP different file into other?

Also, how can I combine PHP with CSS, java, angular and other?

and what's link path for all of them please?

how can I build up a folder directory to create PHP?

Mphammad Arifi
Mphammad Arifi
147 Points

Hi,

1- How can I create a linked path: "href" directory file: creating file for javascript and stylesheet , i can't link stylesheet into java?

2- when i save my work as a index.php is not coming up as local host or internet?

3- i download sublime text and what else do i need to run javascript or PHP?

4- do i need download php or javascript server in order to create directory in to that server?

5- how do i output in to googlechrome automatically?

can anyone Answer all Please?

5 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

In php to include another file you have 4 options:

include Will include a file, if the file is not found or fails to run; the rest of your script will continue with a warning.

<?php

include('example.php');

?>

require Will include a file, if the file is not found or fails to run; the rest of your script will be stopped and fail to load with critical error (very useful if the included file is essential for the rest of the script to function correctly).

<?php

require('example.php');

?>

include_once() and require_once() Functions the same as include and require but will only include the file once regardless of how many times you call the file.

<?php

for ($x = 0; $x <= 10; $x++) {
    include_once('example.php'); // will only be included on the first loop.
    echo "The number is: $x <br>";
} 

?>

A .php file understands HTML so you can include CSS, JS, jQuery, Angular or anything you could normally do in an HTML file for example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <link rel="stylesheet" type="text/css" href="styles/global.css">
        <script type="text/javascript" src="./scripts/jquery-2.1.4.min.js"></script>
</head>
<body>
    <header>
          <?php require_once('header.php'); ?>
    </header>
    <main>
        <div id="wrap">
            <?php
               echo 'This is my main content';
               for ($x = 0; $x <= 10; $x++) {
             ?>
             <div class="contentcell">
                This HTML will be repeated in the PHP for loop and the number is <?php echo $x ?> 
             </div>
             <?php
               }
            ?> 
        </div>
    </main>
    <footer>
        <?php require_once('footer.php'); ?>
    </footer>
</body>
</html>

I do not fully understand your last two questions.

Hope this helps.

Mphammad Arifi
Mphammad Arifi
147 Points

Hi Ashly, my trial is running out soon

is there any way I could ask you please?

1- How can I create a linked path: "href" directory file: creating file for javascript and stylesheet , i can't link stylesheet into java?

2- when i save my work as a index.php is not coming up as local host or internet?

3- i download sublime text and what else do i need to run javascript or PHP?

4- do i need download php or javascript server in order to create directory in to that server?

5- how do i output in to googlechrome automatically?

can anyone Answer all Please?

Codin - Codesmite
Codin - Codesmite
8,600 Points

You should really follow the Web Design Track and PHP Development Track here on Teamtreehouse, almost all of your questions are answered within the first hour of each tracks basics.

https://teamtreehouse.com/tracks/web-design

https://teamtreehouse.com/tracks/php-development

1 - I am assuming you mean Javascript and not Java? (These are two completely different languages). To add a stylesheet to your HTML head using javascript you can use the following script (Should be compatible with all browsers including IE6):

var file = location.pathname.split( "/" ).pop();

var link = document.createElement( "link" );
link.href = file.substr( 0, file.lastIndexOf( "." ) ) + ".css";
link.type = "text/css";
link.rel = "stylesheet";
link.media = "screen,print";

document.getElementsByTagName( "head" )[0].appendChild( link );

You would need to place this before the closing head tag in your HTML, and do not include it in an external Javascript file or you may find when the page first loads it will render unstyled.

Alternatively it would be easier just to include your stylesheet using HTML for example:

<head>
   <link rel="stylesheet" type="text/css" href="styles/global.css">
</head>

2 - You require PHP and a webserver such as Apache installed on the computer/server you are running a .php file on.

3 - Sublime Text is just a text editor, refer to answer 2 for PHP and for Javascript you will require Javascript installed and enabled on the browser you are running your website from.

4 - You can create directories on a server without PHP or Javascript, you requrie PHP and Javascript to run PHP and Javascript (as mentioned in the previous answers).

5 - I'm assuming you mean how to preview your local files in Google Chrome from Sublime Text, you can simply just open your HTML file or PHP file (assuming an Webserver and PHP is installed) with Google Chrome, if it does not open by default in Google Chrome just right click the file and choose "open with" and select Google Chrome.

I sense from your questions you may need to start at the basics of how to design a website before jumping into Javascript and PHP.

I would reccomend the Web Design Track here on Team Treehouse:

https://teamtreehouse.com/tracks/web-design

Mphammad Arifi
Mphammad Arifi
147 Points

Thank you.

but I'm struggling how to build a platform base with all enquiry server download. when i save as index.php the localhost as internet not coming up the rest i'm fine.

My trial running out. i'm already with lynda.com premier subs.. confused between lynda.com and treehouse, they are talking too much, making me confuse i rather follow youtube tutorial.

Mphammad Arifi
Mphammad Arifi
147 Points

Thank you, Ashly for your useful help.

Is there possibly, facebook, twitter or others ... that I could ask you question .As my trial running out

Thank you.