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

nicholas maddren
nicholas maddren
12,793 Points

Using Bootstrap With PHP?

How can I use bootstrap with a PHP file? Will it work exactly the same way as a .html file? I've tried searching but I can't find any answers to this :(

Thanks

3 Answers

geoffrey
geoffrey
28,736 Points

You just have to do the same as the website tutorial shirts4Mike, You can include separate files with include(). It's exactly the same. Except in this case you'll have in the header the stylesheets of bootstraps and etc...

Garrett Reasoner
Garrett Reasoner
913 Points

Hey Nick, You can use HTML inside a PHP file just as if it was only a HTML file. You can place your <?php tags before HTML just make sure to close the PHP tags if you want HTML on the same page, you can put your HTML above your PHP the same way, and if you wanted you can use PHP throughout the entire HTML layout of a page.

PHP is server side code meaning the server hosting the .php file interprets what code is within the <?php ?> tags and then displays the entire page to the browser just as any HTML page or similar depending on what your script does. The browser doesn't handle any PHP.

So to use bootstrap within a php page you could do something like this:

<?php
$example = 'example';
$wellArray = array (
'Arrays are a lot of fun.',
'Bootstrap is an amazing development tool to use with PHP',
'With bootstrap you can quickly code and design beautiful websites'
);
?>
<html>
<head>
<title>PHP in HTML Example</title>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link rel='stylesheet' type='text/css' href='//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>
</head>
<body>
<h1>Short code <?=$example;?></h1>
<? foreach($wellArray as $well) {
 print "<div class='well'>$well</div>";
}
?>
</body>
</html>

You should see something like this:

http://imgur.com/hZ4bCq9

nicholas maddren
nicholas maddren
12,793 Points

Thanks great help! So do I not need to include the html declaration? Just wondering :)

Garrett Reasoner
Garrett Reasoner
913 Points

You should, but you don't absolutely have to -- this example can be copied and pasted and it'll work fine but in a production environment you should.