Tuesday, February 10, 2015

RHT: PHP 101: A Basic PHP Tutorial for Novice Programmers

by Robert Half Technology
February 10, 2015


Learning PHP is part of the process of meeting the minimum requirements for many web developer jobs.

If you’re just starting out in the programming world, consider learning PHP.
The January 2015 TIOBE index ranks it sixth among programming languages, and the Robert Half Technology 2015 Salary Guidereports that PHP development skills can bump the starting salary for many tech jobs by 9 percent.

Learning PHP is part of the process of meeting the minimum requirements for many web developer jobs. Additionally, PHP (along with two other languages, Perl and Python) represents the “P” in LAMP — another skill set that can give a boost to starting compensation for many technology roles, according to the Salary Guide. The acronym stands for:
  • Linux (operating system)
  • Apache HTTP Server (web server)
  • MySQL (database management system)
  • PHP, Perl or Python (programming languages)
These technologies are behind millions of websites, including Facebook, Twitter and WordPress, so knowledge of PHP is likely to give any budding developer an edge.

Another PHP plus is that it’s easy to learn due to its logical syntax, and the command functions clearly describe what they do. You can also download this open-source language for free, and it runs on all major operating systems. great resource for those who are looking to start a consulting careeror new business and have limited time and money to develop a dynamic website, and HTML can’t provide the functionality they need.

PHP can be used to write command-line scripts or desktop applications. The PHP tutorial below shows four ways to create a “Hello World” web page, with PHP functioning as a server-sided coding language.
Ready for a PHP tutorial?
You’ll find four simple PHP programs below. The code samples in this PHP tutorial are based on those found in this W3Schools.com chapter. They demonstrate how you can use PHP to display text to a user using two language constructs: echo (which can output one or more strings) and print(which can only output one string). In the first two examples, you’ll see the PHP code snippet followed by the output that would be rendered in a web browser.

The first example shows two ways to provide user-facing text with echo: including it as a single parameter or in a series of parameters separated by commas. Separating parameters serves no real purpose in this example, but it’s good to see, as it can be quite useful when you learn to work withstring functions.

Code snippet 1

<?php echo "Hello world!<br>"; echo "This", " string", " was", " made", " with multiple parameters."; ?> Code


Browser output:

Hello world!

This string was made with multiple parameters. The next example shows how you can output text using print and a little HTML code (to italicize text) within PHP.

Code snippet 2

<?php print "<i>PHP is fun!</i>"; print "Hello world!<br>"; print "No parameters were involved in making this string."; ?>


Browser output:

PHP is fun!

Hello world!

No parameters were involved in making this string.

The final two examples in this PHP tutorial illustrate how to use an array to pass a variable value to produce slightly different content via these two language constructs. The variables in the array are unnamed, which means this is an indexed array. In the echo and print statements looking for a value from the array, the number indicates which value to pull in.

Code snippet 3

<?php $txt1=" Hello world!"; $colors=array("Red","Purple","Orange"); echo $txt1; echo "<br>"; echo "My favorite colors are " . {$colors[0]}", " . {$colors[1]} "and " . {$colors[2]} "."; ?>


Browser output:

My favorite colors are Red, Purple and Orange.

Now see if you can determine the favorite color below, based on what you learned in snippet 3.

Code snippet 4

<?php $txt1=" Hello world!"; $colors=array("Red","Purple","Orange"); print $txt1; print "<br>"; print "My favorite color is" {$colors[2]} "."; ?>


If you guessed “Orange,” you read it right.
Interested in PHP?
This PHP tutorial gives you a basic introduction, but it’s just a start. The real power of using PHP comes when you discover how it can work with complementary technologies. To learn that, you’ll need training. Bulk up your PHP knowledge by:
  • Working through tutorials and the official manual at PHP.net
  • Setting up your own PHP coding environment and writing your own code
  • Finding a mentor to work with or seeking feedback in tech forums as you advance your skills
Are you familiar with PHP? Share your experiences in the comments. To learn what starting salaries leading employers are offering for candidates who know PHP, check out Robert Half Technology’s 2015 IT Salary Guide.


— Robert Half Technology

With more than 100 locations worldwide, Robert Half Technology is a leading provider of technology professionals for initiatives ranging from web development and multiplatform systems integration to network security and technical support. Visit our website at www.rht.com.

No comments:

Post a Comment