• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Internet
    • Browsers
    • Cloud Computing
    • Online Video
    • Search Engines
    • Web 2.0
  • Mobile
    • Android Apps
    • Apps
  • Software
  • Funny Stuff
  • Social Networks
  • Web Tools

Daily Bits

  • Home
  • About
  • Archives
  • Contact
  • Advertise

Getting Started With PHP – Part 1: Hello World!

November 16, 2007 by Brandon 8 Comments

PHP has become one of the most popular languages for programming on the web. Many popular software packages such as WordPress, Gallery, and ZenCart are built using PHP. If you are planning on using any of these, or doing any sort of web programming, it is useful to at least be familiar with PHP.

This is the first in a series of articles on getting started with PHP. I will first explain the basics of what you need to write and run programs in PHP, and then I will walk you through writing your first PHP program.

The PHP Runtime

The first thing you need to start writing PHP programs is the PHP runtime. The PHP runtime is what will interpret your script when a page is requested, and output the results as HTML to the web browser. Most hosting companies already have PHP installed, so you can probably skip this step.

However, if you are hosting the site on your own web server, you will need to download the PHP runtime here. Depending on the type of web server you have (Apache, IIS, OS X Server), the installation instructions can vary pretty widely, so I will just direct you to the official installation instructions.

The Basic Elements of a PHP Script

Now that you’re ready to start writing your first PHP script, let’s go over the basic elements that are involved:

  • There are a number of specialized PHP editors available, including Komodo, PHPDesigner, and Zend Studio, although I prefer to use a simple text editor like TextPad.
  • PHP pages should have a .php file extension.
  • Inside of your page, PHP script blocks must be enclosed in <?php ?> tags.
  • Regular HTML can be embedded inside of your PHP page, but must be located outside of any PHP script blocks.

Writing Your First Program

Now that we are familiar with the basic elements of a PHP script, we are ready to write our first PHP program. The Hello World! program has long been the standard first program when learning a new programming language, as it performs the most basic of tasks (it displays “Hello World!” on the screen). Let’s take a look at what the Hello World! program looks like in PHP:

<?php
echo "Hello World!";
?>

Pretty simple, huh? Let’s take a closer look at exactly what’s going on here:

  • The echo function simply displays to the screen whatever follows it. There is also a print function that behaves similarly, but echo is actually a little bit faster, so I usually like to use it instead.
  • Since “Hello World!” is a literal string of characters, it must be enclosed in quotes. If I wanted to display the number 1, I could have done this instead: echo 1;
  • The line ends with a semicolon. Every line in a PHP script must end in a semicolon. This is what tells the PHP runtime that the current command is complete. Otherwise, the PHP runtime will try to combine it with the command on the next line, and you will likely get a runtime error.

For Next Time

After reading this article, you should be able to write a basic PHP program that displays “Hello World!” in the browser. The next article in this series will focus on one of the major building blocks needed for writing more interesting programs: variables.

You can already read Getting Started with PHP: Part 2 – Variables

Filed Under: General Tagged With: basics, hellow, part, php, Programming, start

Reader Interactions

Comments

  1. Cannabis Junction says

    November 19, 2007 at 6:51 pm

    Thanks for creating this Article and series. It surely will be useful.

  2. Cannabis Junction says

    November 19, 2007 at 6:59 pm

    i have a question.
    I’ve noticed that php is somewhat similar to C++
    Could the code above be written as a single line of text ?

    ie

  3. Daniel says

    November 19, 2007 at 8:46 pm

    Cannabis, your comment got broken due to the code you pasted.

    I believe you were asking if the code could be written like this:

    <?php echo “Hello World!”; ?>

    Yes you can write the code on a single line, the breaks are just used to facilitate when you need to tweak things.

  4. Brandon says

    November 19, 2007 at 10:41 pm

    Cannabis, thanks for the comment. Yes, you can push everything into a single line. I had just put the line breaks in for readability. In fact, you can combine multiple commands in a single line, such as:

    <?php echo “Hello “; echo “World”; echo “!”; ?>

  5. Submit Comfort says

    December 1, 2007 at 6:04 pm

    I will highly recommend you to start with “easyphp” or “XAMPP” to execute PHP/mySQL on your local machine. Just google them to download, they are free!

  6. lamib says

    January 25, 2008 at 3:31 pm

    I prefer Notepad++: it also has syntax highlighting for other languages when I have to touch something else than PHP.

  7. Stas says

    February 16, 2010 at 3:05 pm

    I suggest to use Codelobster PHP Edition as free PHP editor.

Trackbacks

  1. Getting Started with PHP: Part 2 - Variables says:
    November 23, 2007 at 10:50 am

    […] PHP. In part 1 of the series, I covered the basic building blocks of a PHP script, and then showed how to write a simple Hello World! program in PHP. In this article, I will cover one of the most basic tools for creating more interesting programs: […]

Leave a Reply Cancel reply

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

Primary Sidebar

Privacy Policy

Copyright © 2021 ยท News Pro On Genesis Framework ยท WordPress ยท Log in