‘Hello, World’ and Other PHP Basics

Since this is my first column on PHP for Dice, let’s start at the beginning. The common convention in learning any language is to introduce it through a “Hello, World” application.  This application, as you probably know, is designed to give new users the satisfaction of having completed a project while introducing the language.

php-elephantIn PHP, this application couldn’t get more basic and isn’t very interesting. On any PHP page, just type:

<!--?php echo 'Hello, World'; ?>

That’s it. Like I said, not very interesting. You can spice it up a little bit by passing it a variable, but that’s still pretty dull.

<!--?php $name = 'Scott'; echo 'Hello, ' . $name; ?>

The command echo is one of the first functions developers learn when they’re introduced to PHP. By no means is it the only way to get output to the browser, but I thought it might be a good way to take an initial look at the different types of browser output you can produce using commands such as echo and others. So let’s go back to echo and explore it a little bit.

A Little Dissection

In the first example, note that the phrase <!--?php echo 'Hello, World'; ?> is in single quotes.  For the most part, PHP doesn’t care if you use single or double quotes. Personally, I like using single quotes because that means I don’t have to escape the quotes in my HTML, which you can output as easily to the browser as plain text.

<!--?php echo 'Name '; ?>

If you do something like that, you’ll get an error:

<!--?php echo "Name "; ?>

PHP will parse the statement up to the second quotation marks as a string, and won’t know how to parse the rest. You can reverse the internal quotation marks to be single quotes, but to me that makes less sense than just starting with single quotes. That eliminates any concerns about escaping quotes and makes it more likely that your IDE will pick up any syntax highlighting.

There are a lot more things you can do with echo, such as using it in a loop to output data, but it’s still pretty boring. It does its job and there’s just not that much more to describe.

Outputting Arrays

One of the next commands a new developer learns is print_r(). The “_r” here is for “recursive.” This is what you’ll need to use if your output isn’t a text string such as “Hello, World.” Use print_r for dumping arrays to the browser. (There are other commands that can do this too, such as var_dump() and var_export(), but print_r() is a good place to start.

Arrays are written in this syntax, prior to PHP 5.4.x:

$array = array(1, 2, 3);

If you want to output the array to the browser, you would type:

print_r($array);

If you prepend 'echo '<pre>;' and append "echo '</pre>;'" to that, the browser will format your output for you, and you’ll get a nicely displayed array in the browser.

Other commands, such as var_dump(), will give you more information and can be used for debugging. Whether it’s an array or not, var_dump() provides the value type and values for that variable. So if $var = 3.14, and you pass $var into var_dump(), you’ll get float(3.14).

This command will also iterate through arrays and give you the same information. In contrast, var_export() does the same thing but returns parseable PHP code. Obviously, these are commands you don’t want to use in production.

Both of these commands are quite useful and can be used in combination with others, such as die(). If you pass die() the var_dump() of a variable, the command will output the value and value type of the variable, then stop executing code. This is an excellent and simple way to debug your code by finding out what data is in your variables and arrays.

There are other functions that can be used in production, such as printf() and sprintf(), both of which output formatted strings. For instance:

$animal = "cat";
$lives = 9;
$format = "A %s has %d lives.";


// Outputs: A cat has 9 lives.
echo sprintf($format, $animal, $lives);

This could be useful if, say, you wanted to loop through an array of animals and the number of lives they have and output the results to the browser.

Comments

  1. BY RobS says:

    Thanks for the tutorial.
    Can you go back to square “zero” and describe how to implement this? Specifically, how do I find/build a PHP environment where I can run this? Does it require a Linux machine? Can I do it on a Windows machine? Does it require a PHP server?

  2. BY MegaBobTube says:

    Great initiative! Could you embed OOP little-by-little?

  3. BY Scott Hummel says:

    @Megabobtube — I plan on doing some OOP soon.

  4. BY Scott Hummel says:

    @RobS — I suggest WAMP Server. WAMP stands for Windows Apaache MySQL and PHP. That will give you everything you need to get started on PHP on a Windows machine. WAMP is your PHP server.

Post a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>