Forgot your password?

PHP Basics For Beginners - Hello World

By Edited Oct 26, 2016 0 0

What is Hello World?

You are probably wondering, what exactly is “Hello World”.  Hello World is the concept of taking a new programming language, and learning how to make the program print the text “Hello World”.  The action is simple enough, but learning it provides a new student of the language with the fundamentals to build much larger and more complex apps. 

Implementing Hello World in PHP

Before we can make an application that prints the “Hello World” text, we need to understand a few basics. 

PHP is a programming language that works with HTML (Hyper Text Markup Language).  HTML is the language of the web.  Almost every website on the internet starts in HTML.  Things like PHP and Javascript work with HTML code.  Before we can understand PHP, we need to understand some simple HTML basics.

Every HTML page has a basic structure:

  1. <html>
  2. <body>
  3. ...Enter Page Content Here
  4. </body>
  5. </html>

In the above example, there are two sets of HTML tags.  The outer set of tags is <html></html> and the inner set is <body></body>.  There are many more tags but for this example, these will suffice.  

We could cheat and produce a Hello World page without evening using PHP, and just HTML!  Take a look at the following:

  1. <html>
  2. <body>
  3. Hello World
  4. </body>
  5. </html>

The above example is simple example of Hello World for HTML, but the goal of this article is to examine PHP, not HTML but we can use this as a starting point.

PHP also uses tags to enclose the PHP code.  A sample of PHP code would look like:

  1. <?php
  2. /* PHP code here */
  3. ?>

In the example above, please take note of the green text.  This is one type of PHP comment.  A comment is text only seen by the programmer that describes what lines of code do.  As applications become more complex, signficant amounts of commenting are recommended.  See the examples below of different types of PHP comments:

  1. <?php
  2. //Comment Type #1
  3. /* Comment Type #2 */
  4. /* Comment
  5.   Type
  6.   #3
  7.   Multiple Lines
  8.   */
  9. ?>

So, lets say we want to have a sample of PHP code that prints “Hello World”.  We need to know the PHP command for printing text, which is simple ‘print’.  See the next example:

  1. <?php
  2. print “Hello World”;
  3. ?>

This block of code, working in conjunction with the HTML example above will achieve our goal, printing Hello World.  Noticed that the text to be printed must be enclosed within quotation marks.  Also notice the semi-colon at the end indicating the end of a line.  See a complete example below:

  1. <html>
  2. <body>
  3. <?php
  4. print “Hello World”;
  5. ?>
  6. </body>
  7. </html>

There is still one problem.  We originally named the file index.html.  If the file contains any PHP code, then it needs to be named with a .php extension instead of .html.  So we would name this index.html.

There are many more functions and tools within PHP.  Check out future articles that build upon the basics covered here.



Add a new comment - No HTML
You must be logged in and verified to post a comment. Please log in or sign up to comment.

Explore InfoBarrel

Auto Business & Money Entertainment Environment Health History Home & Garden InfoBarrel University Lifestyle Sports Technology Travel & Places
© Copyright 2008 - 2016 by Hinzie Media Inc. Terms of Service Privacy Policy XML Sitemap

Follow IB Technology