PHP Basics for Beginners

Channels >> Webmasters >> PHP Basics for Beginners
PHP Basics for Beginners
Posted by Tiff (#1) on 29 June 2012, 3:57 AM EDT in Programming Guides
Note: This guide is targeted primarily towards people who have limited or no prior programming experience.

One of the most popular scripting languages on the web, PHP is easy to learn and conveniently integrated into HTML web pages. It's powerful but flexible and thus can be used to create anything from short simple scripts to complete web frameworks or content management systems. Some examples of reasons you'd want to learn PHP might include:
  • You want to create a site with interactive user features
  • You want to create your own content management system, forums system, etc
  • You own a static website and want to spice it up with some server side scripts
  • You want to modify an existing program written in PHP
  • You just want to know one of the most popular languages on the web!

Your Starter Kit

PHP is a server side language, meaning that it's processed by the web server rather than the browser. This means that if you just open a PHP file in your browser, it won't do anything! To test PHP, you need either a web host that supports PHP or a local development environment set up.

Luckily, lots of free web hosts and even more paid web hosts now a days support PHP, and there are many freely available bundles on the web packaged with everything you need to simulate a web server running PHP. If you're using Windows, I recommend BitNami WAMPStack (WAMP stands for Windows Apache MySQL PHP) or LAMPStack if you're using Linux.

If you set up a local test server on your computer, usually the default folder for your website files is 'htdocs' under the installation folder. This is the folder where you would create you PHP files to test, and the default URL for you to view your results is simply localhost. (Try typing http://localhost/ into your web browser after setting up Apache!)

In addition, while you're coding PHP, using a text editor that has syntax highlighting enabled or an IDE (Integrated Development Environment) will help you a long way by making it easy to read and understand your own or others' code.

Essentially, with syntax highlighting,
//Hello World
echo "Hello World";

PHP Code:
//Hello World
echo "Hello World";

And while the colors may not mean much to you yet, especially if you've never programmed before, syntax highlighting will make it much easier for you to skim programs and see the different parts. For a text editor enhanced for programming, I recommend either Notepad++ or SublimeText.

The Basics of Syntax

PHP can be used interchangeably with raw HTML in a single file. However, PHP code in .html files is not processed by default. The typical file extensions for PHP files are usually .php or .phtml, so make sure your PHP files use these extensions. PHP files in general work like HTML files, only PHP scripts inside of them are processed.

With PHP code being put in the midst of HTML, the web server needs an easy way to tell when you want it to process PHP instead of just HTML or raw text. PHP uses the <?php and ?> tags to do this.

PHP Code:
This is just text 
//Your PHP code goes here
and will not get processed as PHP.

<?php tells the web server to start parsing PHP in a file, and ?> tells the web server to stop. You can also use <? (called the short tag) in place of <?php, but this practice is not recommended because of conflicts with the meaning of the <? tag in XML.

Your First PHP Code!

Alright, by now I'm guessing you got a web server to test PHP ready whether it's local or on a host right? Here, copy and paste this code into a .php file:

PHP Code:
//Hello World script
echo "Hello World!";

Go to this PHP file under your host and see what the page shows. It should say "Hello World!" If it does, congratulations! You just wrote your first PHP script. Now you're ready to take on the world.


Okay, maybe not quite yet. We've got to dissect what you just copied and pasted there to get some things straight. You could have just made your page say "Hello World!" by typing the text directly into the file without the PHP code, but instead you used the PHP function echo.

While here echo doesn't seem to do anything spectacular, it's one of the most useful PHP parts of PHP because it's how PHP ultimately takes the result of your script and turns it into text or HTML or whatever that's seen by the browser!

In addition, notice the quotes around "Hello World!". Even though these were part of the thing you told PHP to echo, you shouldn't have seen any quotes in your final web page. This is because quotes actually mean something to PHP. The program interprets anything inside a set of quotes as a string, that is, data that's text. If you forget to use quotes around random text you want PHP to echo or manipulate or whatever, PHP will try to process that text as part of the script rather than data which will generally break your script.

Finally, at the end of the echo statement, notice the semicolon ( ; ). Semicolons are often forgotten by beginning programmers, making them a common cause of syntax errors. But essentially, a semicolon tells the parser that a statement in the program has ended and it can move on to the next thing. Think of semicolons as being like the periods of programming, only when you forget them, instead of annoying a reader, you break the program.

So let's take a look at our first program again:

PHP Code:
//Hello World script
echo "Hello World!";

Did I forget something?

How about that //Hello World script part?

Try removing it or changing the text after the //! It shouldn't affect your program at all. Lines of programs prefaced by // are called comments, and comments don't change how your program works at all. They're ignored by the parser, but they're also important because they let you put notes in your code to help you to remember why you coded something a certain way or whatever else.


So hey, try messing around with some of the parts of your "Hello World" program and pay attention to what changes break your program and what changes change the output of your program. Experimentation is one of the easiest ways to get used to programming, so have fun!

We'll be talking more about some specifics in depth in a later guide. Stay tuned.
© 2017 PokeDS. Pokémon © 1995-2017 Nintendo/ Pokémon. Icons © LED24.
Page generated in 0.0691 seconds.
Silver Islands Indigo Plateau The Cave of Dragonflies Clash at the Summit