If you're programming on a Windows machine, then chances are you probably haven't dabbled much in PHP. So today let's take PHP for a spin using Microsoft's WebMatrix IDE.
Update: 10.16.2017: Microsoft will no longer support WebMatrix after November 1, 2017. Instead, it is recommended that you switch over to Visual Studio Code instead.
WebMatrix is a lightweight IDE that allows for development using .NET Web Forms, Web Pages, PHP sites and more, and it is 100% free and you can download it from right here.
Let's Get Started
So I select the Starter Site template and then enter some admin credentials that I want to use. If I forgot the credentials, it is stored in a config file and I can change it whenever I'd like once the project is set up.
And that's pretty much it for setting up a new PHP site. Running the project now should generate the following web page.
Well that was easy. I have now created a PHP website. Just kidding. Now it's time to add some logic to this beast and see what it's capable of. The IDE has generated the following files for my coding pleasure.
So it looks like I have plenty of CMS like features off the start. I can add new pages, edit them, delete them, add new users, edit users, etc etc. Not bad. But I want to take a dive into the language. So let's create a new database and then try some inserts and updates.
Add A New Database
I'm going to add a new MySql database to my project and create a few test tables.
And that fails. Over and over again. Because apparently, WebMatrix does not yet allow the creation of MySql databases from the IDE. I could however create a database from the outside and then connect to it. But since that's going to take some time to figure out, I'll just work with the default database that gets created with each project.
So here is a quick table that will represent the most basic of blog schemas. Another thing to note, I can't call a function as a default parameter. That's MySql for ya I guess. I added some fake data into the table and will be displaying it onto a php page. Most of the time, with any language pretty much, fetching and adding to a database is one of the things that are required to know and many day to day tasks will revolve around that.
Fetch From A Database
So I'm going to create a new page and call it BlogPosts.php or something and will use that to fetch all of the blog posts that I have added into my database. I have the option of using PDO or MySQLi to handle my database work, and because WebMatrix by default uses MySQLi, then I will be using that.
And because I'm new to PHP it looks like I'll need to create a new objection of type mysqli to get the job done and I'll need to pass in the appropriate authentication parameters.
$databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($databaseConnection->connect_error)
{
die("Database selection failed: " . $databaseConnection->connect_error);
}
And after that I'm ready to start querying some data. To make it as simple as possible, I'm just going to be fetching everything and spitting it back out.
<div class="posts">
<?php
$sql = "SELECT * FROM BlogPost";
$result = $databaseConnection->query($sql);
while($row = $result->fetch_assoc())
{
echo "<div class='post'>";
echo "<div class='title'>" . $row["Title"] . "</div>";
echo "<div class='content'>" . $row["Content"] . "</div>";
echo "</div>";
}
$databaseConnection->close();
?>
</div>
So that wasn't too bad, except that WebMatrix didn't have much of this in intellisense and so Google had to hep me out a bit.
Inserting and Editing the database data is pretty much just more queries, which I won't bore anyone with. Overall, it's pretty much like any other programming language. It offers functions for file manipulation, string manipulation, database operations and everything else in between.
Include And Require
And interesting feature. This is equivalent to User Controls in ASP.NET and WebMatrix will make use of them. I had to follow includes several layers deep to find out how some of the pre-built functionality works. Am I a fan? Not really. Repeatedly including files to copy over functionality seems like something better suited for MasterPages or parent classes. Nevertheless, they're simple and get the job done, so why not.
What I Liked
So there are a few features that I liked about using PHP, so I'll give credit where credit is due. It's fast and lightweight most importantly. Projects have a very small footprint and they can run in multiple types of web servers which makes it super convenient to develop.
File Manipulation
This was probably one of my favorite things in PHP. File manipulation is super simple. In other languages, I almost always have to remember which classes are best suited for reading a file in and there's always about a dozen ways to get to the same place. With PHP, I just did this:
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
So that wasn't too bad and it went better than expected. The overall syntax is pretty simple to get down after a few functions are written and while WebMatrix wasn't the most helpful IDE, it definitely got the job done quickly. I will definitely be tackling future projects in PHP to better get acquainted with it and will do a follow up to this post when that happens.