While PHP is not well-suited to every website, there are several benefits to using PHP on the back end of your site. PHP is open-source, meaning there is a lot of support and community discussion, and it is free to use. Figuring out how to create a PHP website is simple with so many resources available.
Content management systems such as WordPress use this popular language. Experts estimate WordPress powers about 27 percent of the Internet, and those aren’t the only sites using PHP coding.
Other benefits to using PHP include that all the web servers support it, and it delivers content much faster than other options, reducing load times. If you’re convinced PHP is the right choice for your website, here are several tips to get you started on how to create a PHP website and help you make the most of the transition.
1. Start With OOP
Before you dive right into PHP and trying to learn a new coding language, gain a background in OOP, which stands for object-oriented programming — the foundation of PHP. By learning the basics of OOP, you begin to understand PHP, and can then move on to more advanced topics. Take an online course, such as Object-Oriented PHP for Beginners, at Tuts+.
2. Install PHP
As with most new things, your first step is to begin working with PHP. The more you work with it, the better you’ll understand the language. Check to see if your web hosting company allows PHP. Most already have the latest version of PHP installed, meaning you can install software such as WordPress and work within it, as well as integrating databases with it. Look for MySQL as an option, for example. If you’re unsure of the installation process, or your server doesn’t offer one-click installation, talk to your server about what you need to do.
3. Learn Basic Commands
Once you have your platform installed, learn about some basic PHP commands that will call up what you want on your page. Understand that you will be embedding PHP commands into your HTML documents. You have to learn the syntax of PHP, which isn’t much different from HTML. You basically have to spell out what you want to happen when someone lands on your web page. The University of Florida spells out how the syntax works and some basic commands to get you started for free.
4. Avoid Relative Paths
If you’ve spent any time coding, you know there are relative paths and root paths. Relative paths point the user to a folder on the server. Example:
You can run into issues if the parent directory is not the working directory, which returns errors for the user. Instead, define the root path:
define(‘ROOT’ , ‘/home/var/specificfolder;
require_once(ROOT . ‘…/home/page.php);
This process is also helpful if you move directories later, as you can redefine your root, rather than recoding everything.
5. Automate Copyright Year
One of the main reasons people switch to PHP is the ability to automate processes. However, you might still manually go in and rework your copyright year in your site’s footer. Luckily for you, it’s a simple matter of PHP coding to automate this to update to the current year.
© <?php echo date(“Y”); ?>
If you want, you can add a start date and a dash and then automate this year’s date. The key is that you won’t have to keep going in and changing your copyright year every time the calendar year changes.
6. Change Graphics by Season
Have you ever noticed how some websites use different graphics based on the current season? There is a simple way to automate this process, too. It does require some planning for the graphics you’d like to use and more involved coding. You can grab the code from CSS Tricks. Just plug in the paths that point to your graphics.
Want to Become a Design Expert?
Join over 700 subscribers!
Sign up for weekly design news sent right to your inbox!
7. Create a Master Configuration File
Instead of randomizing your database connection settings — this makes it more difficult to find and alter them later — it’s better to create a single, master configuration file and then call upon it in your PHP scripts.
This also makes it much easier to make changes and updates later. You don’t have to sort through a massive database of code — you can refer to a single file. It’s also incredibly helpful when you need to use constants and functions across multiple scripts.
8. Always Sanitize Database Inputs
Learn the following function and use it every time you pull data into your database: mysql_real_escape_string. What it does is take any string or dataset and cleanses it to ensure you’re getting the raw data. When used alongside the htmlspecialchars function, you can be sure that special HTML characters are preserved.
This allows you to keep your database safe — especially from SQL injections — and also prevents cross-site scripting attacks, which can happen if you include user-submitted HTML fields.
9. Find a Good Code Editor
You’ll want a code editor that helps you learn coding and points out mistakes you’re making along the way. You could just use the free Notepad that comes with Windows, of course, but you may not work in a Windows environment, or you may want more feedback. Invest in an integrated development environment for PHP, such as PHPStorm. It will give you feedback as your code your first PHP scripts.
10. Learn Ternary Expression
Learning PHP isn’t easy and takes many months of study to fully master. One area you’ll want to study in depth is ternary expression, also known as if-else code blocks. They give scenarios for when a visitor lands on your page — such as actions that happen or do not happen, based on given circumstances. You’ll have if statements and variables and also use true/false statements to tell your page how to behave.
11. Use a Framework
Writing code from scratch is time-consuming and leaves a lot of room for error. That is where frameworks come into play, which are sections of pre-written code you can adapt for your specific needs.
A framework is basically a platform, so WordPress is a framework and a content management system at the same time on which you can build a website.
12. Turn Error Reporting On
When you begin a new project, turn error reporting on. It will allow you to see any mistakes in syntax in real time and fix them as you go, before they become bigger issues. Error reporting should continue until you’re ready to publish the site.
13. Use Built-In Functions
PHP features built-in functions that will help you code some of the most common elements in website design. A function is a block of statements you likely use over and over in creating a project. They don’t pull up on page load, but during specific functions, such as clicking on a link. Using built-in functions saves precious coding time.
14. Use Your Admin Tool
Your server likely has an admin tool installed to help you use PHP easily. Often, this is phpMyAdmin. Using phpMyAdmin speeds up anything you need to do on the back end of the database, such as exporting SQL files or running SQL queries.
15. Add a PHP Exception Handler
If your PHP script has an error, your site visitor might get a fatal error code. This is off-putting to site visitors, so you should add code that handles an exception in your PHP script. You can find an example of one you can copy and use at CodexWorld.
16. Practice and Be Patient
The key to learning PHP so you can create and maintain websites is to practice coding as much as you can. Be patient with yourself as you learn the ins and outs of this coding language. PHP isn’t an easy language to learn, and just when you think you understand the finer details, there is something new to master.
17. Save Your Favorite Snippets
Throughout the course of your projects — and career even — you’ll be working with a lot of identical code and functions. Instead of typing it all out, piece by piece from scratch, it makes a lot more sense to preserve it in some way, so you can just call upon it later. It’s similar to a framework, but you’re working with custom codes you’ve written.
Many applications and tools allow you to do this, including Snippet, Snippely, Snipplr, and Code Collector. The web and cloud-based ones are the best because they allow you to store your favorite snippets in an accessible space online you can access anywhere. For instance, you can save a code reference while programming on a work terminal, and access it when you get home on your own computer.
Some IDEs also include support for code snippets — Dreamweaver and Eclipse come to mind. However, you can also create your own directory, organized in whatever form or fashion you desire.
The point is just to store the snippets you frequently use because it will save you a lot of time — like, a lot, a lot.
18. Find a Good Balance with Commentary
Even though you’ll be using a standard language, it’s essential to understand code writing is as unique as drawing, conventional writing and other creative practices. Often, it can be so unique that you might not understand what you were trying to do when you return. That’s exactly why it’s necessary to document your work by commenting out your code. All languages allow you to include ignored comments, and PHP is no exception.
The trick is finding a good balance. You don’t need to comment on every single line. Well-written code often speaks for itself. But when you’re dealing with more complicated functions and code segments, it’s essential to leave at least a brief explanation.
19. Connect With Other Developers
It doesn’t matter how experienced you are, there are still plenty of opportunities to learn from your peers. Join a PHP-oriented community, or dive into a PHP oriented sub-community on an existing social platform. The PHP subreddit is a great example.
When you get stuck, you can ask for help or advice. You can provide support for other developers, which helps hone your own skills. You can also open discourse about PHP and your work, in general, to gain valuable insights from the community.
You don’t exist in a bubble, which is — believe it or not — a good thing!
How to Create a PHP Website
You don’t have to wait until you fully know PHP before creating a PHP website. Using a framework allows you to get the site in place. You can then learn PHP as you go, which is one of the best ways to learn because it allows you to put principles into practice.
This article was originally published on 9/11/2018 and updated on 3/13/2019.Buffer