fbpx

Introduction to PHP

PHP is a great language to learn for making websites. It’s also very useful for system administration tasks too, but in this tutorial we’re going to look at it for creating websites. A lot of popular software is written using PHP. WordPress, the CMS that runs the majority of websites on the Internet, is written in PHP along with all the addons available for it. WordPress themes also utilize PHP. Learning PHP is an incredibly valuable skill. Here’s some of what you can do with PHP:

  • PHP can generate dynamic page content
  • PHP can create, open, read, write, delete, and close files on the server
  • PHP can collect form data
  • PHP can send and receive cookies
  • PHP can add, delete, modify data in your database
  • PHP can be used to control user-access
  • PHP can encrypt data

With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.

Introduction to PHP

Like any programming language, we’re going to get started with a simple “Hello World” script. Unlike JavaScript, PHP is a server-side script. That means that the language is processed on the server and is then sent to the client. This means that any system you want to run PHP on needs to have PHP installed. This is why all of our shared hosting servers have PHP installed by default. If you want to develop PHP locally, you’ll need to have a local server with PHP like XAMPP.

In your web directory, create a file called index.php. All PHP files need to have the .php extension. This tells the server to execute PHP on the file.

In the index.php file, type the following:

<?php
echo "Hello World!";

?>

and save the file.

The <?php is the start of where PHP code begins. Every PHP file needs to have this. echo is a command that echos out the text given. Notice that the line ends in a semicolon. Every PHP statement needs to end with a semicolon as this tells PHP the end of that statement. This allows you to write all your PHP code on a single line if you wanted, but don’t do this! It makes your code hard to read and debugging difficult. Finally, we end the file with ?> which tells PHP anything after this, it doesn’t need to process.

Now if you open your browser to where this page is located, you should see “Hello World!” printed in your browser’s default font.

Speaking of readable code, comments help make code readable. You can add several different types of comments to your code.

// This is a single line comment that starts with two forward-slashes.
# This is a single line comment that starts with a octothrorp (or "pound sign" or "hashtag" for younger generations)

Additionally, you can have multi-line comment:

/* This is a multi-line comment.
That means you can have multiple lines that are ignored by PHP.
You can use this for longer explanations.
Don't forget to close it when you're done! */

Add a single line comment to your code. Execute your page in your browser. Right click and view source. Do you see your comment?

You shouldn’t! That’s because PHP ignores comments. When it generates the output, all comments are removed.

Expanding on PHP

Now you know the basics! Time to expand! Go to W3Schools and follow their PHP tutorial.

Practice what you learn here with our NVMe web hosting!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.