PHP for Beginners

PHP is a scripting language which has risen in popularity to become the number one choice for serving Websites today. Getting started with PHP is easier than you might think. This article explains the first few steps towards understanding PHP.

Earn with Bitconnect!Earn with Bitconnect!

Many of the most popular online publishing solutions such as WordPress, Drupal and Joomla are built with PHP. Understanding PHP will allow you to extend these platforms and customize them in numerous ways.

If you intend on working as an online programmer, knowledge of PHP is highly recommended. You will find a larger community and support network for PHP (and therefore jobs) than any other competing server-side scripting language.

PHP can also be used as a general purpose scripting language as well. Once you know PHP you can leverage this knowledge and take advantage of using PHP from the command line or even build desktop applications with Graphical User Interfaces using PHP-GTK.

Learn Html First

Before you learn PHP it would be a good idea to first learn HTML if you are not already familiar with it. Html is very easy to learn, but learning it goes beyond the scope of this particular article.

Below is a simple Html code snippet. We will be adding some PHP to it in a moment. Enter the snippet into a text editor and save it as phpfun.html. Open the file in your Web Browser and you will see the expected output of “PHP is fun!” appear on your screen.

<!doctype html>
<html lang="en">
<head>
  <title>Learning PHP</title>
  <meta charset="utf-8">
</head>
<body>
PHP is fun!
</body>
</html>

Rename the file extension and insert some PHP

PHP is often embedded into Html documents. Most Web Servers are not configured to process PHP found inside of Html files by default. Because of this we must rename the file to phpfun.php. Doing so informs your server that this document contains PHP and is not merely a static Html file.

Below is a slightly modified version of the Html snippet above. It commands PHP to output “PHP is fun!” and demonstrates how simple it is to add PHP to an existing Html document.

<!doctype html>
<html lang="en">
<head>
  <title>Learning PHP</title>
  <meta charset="utf-8">
</head>
<body>
<?php echo 'PHP is fun!';?>
</body>
</html>

You will notice that the PHP code is inserted into a pair of angle brackets much like an Html tag. The PHP “tag” begins with “<?php” and then closes with “?>“. The actual PHP code is in the middle.

<?php echo 'PHP is fun!';?>

Using PHP’s echo command, the script above simply outputs the exact same thing as the Html document did before.

You can intermix Html and PHP as much as you require in your document. Simply add more PHP tags containing PHP code where you need to.

Below is an example of how multiple PHP tags can exist in a document. It also demonstrates how PHP is capable of outputting Html by creating the web page’s title tags and content for us.

PHP is capable of generating a variety of different kinds of output. Some examples include plain text, Html, Javascript and CSS. It doesn’t stop there however. PHP can also generate SVG data, Images, CSV files, PDFs and much more.
<!doctype html>
<html lang="en">
<head>
  <?php echo '<title>Learning PHP</title>'; ?>
  <meta charset="utf-8">
</head>
<body>
<?php echo 'PHP is fun!';?>
</body>
</html>
Earn with Bitconnect!Earn with Bitconnect!

Upload the file to your Server and test it

Just as you would with an Html file, you need to upload your new PHP file to a Web Server in order to have access to it from the Internet. You cannot view the file locally using your Web Browser and expect it to parse the PHP. This is a job for a Web Server.

Using your favorite FTP program, upload the phpfun.php document to a convenient location on your Web Server. An example location would be in a subdirectory off your Web Server’s document root such as /test/phpfun.php.

Once the file has been uploaded, enter the URL to your new PHP file into your Web Browser just like you were accessing an Html document on your server. For example :

http://yoursitedomain.com/test/phpfun.php

If all went well you should see the Web Page generated by PHP. It should match the output of the Html file you created and viewed in your Web Browser locally.

Documentation

Perhaps one of the greatest things about PHP is the official Documentation available online (or as a download). Be sure to check out the Quick Reference Tips page for information on integrating the documentation into your Web Browser and other applications.

The documentation contains many questions, answers and code snippets posted from users from around the world. After several years of contributions, this has amounted to a treasure trove of information regarding PHP.

A larger example of a PHP Script

The PHP tag can be opened (<?php) and closed (?>) on different lines to allow you to write larger scripts spanning multiple lines. The example below demonstrates how this appears.

A variable is a placeholder for a value. The variable entitled $name is assigned the value of “Paige”. The first echo statement prints the variable ($name) and the second echo follows it with more text. The end result displays Paige loves PHP!

<!doctype html>
<html lang="en">
<head>
  <title>Learning PHP</title>
  <meta charset="utf-8">
</head>
<body>
<?php

  // This is a comment.
  $name = "Paige";
  echo $name;
  echo ' loves PHP!';

?>
</body>
</html>

Also introduced in the code is a single line comment. These are merely notes that can assist the programmer in understanding how the code is written. Anything following “//” to the end of the current line will be ignored by the PHP Parser.

If you require multiple lines for a particular comment, you can use a different form of commenting which uses the character pairs “/*” and “*/” to indicate the beginning and end of a comment. The fact that the comment may span multiple lines does not matter. Everything between the start and stop indicators will be ignored. This form of commenting still functions fine on a single line.

/* This is an example of a comment
   that spans more than a single line.
   It can in fact span any number of lines. */

Summary

This article (PHP for Beginners) has touched on some of the fundamental understandings needed to get started as a PHP Programmer. You have learned how to embed PHP into Html documents, how to comment your code and even generate rudimentary output using variables.

Earn with Bitconnect!Earn with Bitconnect!

Speak Your Mind

*

Read previous post:
8 Bit Character Editor

Today we have the luxury of beautifully anti-aliased fonts on high resolution displays. Several years ago computers had blocky fonts...

Close