MC Press Online

Saturday, Jun 24th

Last updateFri, 23 Jun 2017 1pm

You are here: Home ARTICLES Programming Scripting PHP: Variables, Arrays, and Functions, Part 2

Programming / Scripting

PHP: Variables, Arrays, and Functions, Part 2

Support MC Press - Visit Our Sponsors


Flexible Input, Dazzling Output with IBM i



Click for this Month's

Bookstore Special Deals

Love the idea of putting data in two-dimensional arrays? Well, the next step is figuring out how to get it out. But before you do that, there's one little thing.


In our last visit, we talked about arrays. We talked about the difference between numeric and associative arrays. We talked about how to load and unload data from them. And we talked about how to load data into two-dimensional associative arrays, which more or less mimic file records. Let's face it, we just couldn't shut up about them. But we delayed the discussion of pulling data out of those two-dimensional associative arrays until later. You might, of course, think that "later" is now, but unfortunately, before we do that, there's one other little thing that we should cover.


I've talked a lot so far about using PHP, and I've said more than once that PHP code can be embedded in HTML. But I haven't really talked about how that works.


First, let's ask ourselves, when do we use PHP? We use PHP whenever we need to do something that we can't do in straight HTML, like access file data. And, since HTML doesn't really have array support, this includes holding and manipulating data that we either get from a file or plan to write out to a file. (There are other times that we use PHP, but that's the use we're looking at now.) What this means is that we'll do a PHP statement and then display the results, using HTML, on the screen.


The question is, how do we combine PHP and HTML so that we can execute a command in PHP and then display it using HTML?


The first thing we have to do is tell the browser that we're about to do some PHP commands (remember, neither PHP nor HTML is compiled; it is instead interpreted by the browser engine). We do this by enclosing anything that uses PHP with the <?php and ?> tags. Then, within those tags, we can mix PHP (echo or print, for example) and HTML (h2 or ul, for another example) tags.




      echo "<h1>This is the table header</h1>";




This is a trivial example, of course, but it gives you the idea. Note the beginning and ending PHP tags and then the fact that we use echo to display things on the screen. Note also that the HTML statement is in quotes (things in an echo are) and that there's a semicolon at the end, which is normal for a PHP statement. Pretty straightforward, but make sure you get the syntax.

Echo vs. Print

There are two ways to take data that you've played with in PHP and throw it onto a Web page: echo and print. In this article, I'm pretty much going to use echo, but print is just as valid. Are they the same? No, not quite, and here are the differences that may cause you to use one over the other at times.


First and least important, there is a speed difference. Echo is faster than print (because the print has a return value associated with it), and to some people that's important. In one spot, I thought I saw a 30% difference; in another, it was called "irrelevant." With the speed of today's processors, I don't worry too much about the speed of an individual instruction, but for some people it's very important. You make the call.


Second, print is sort of like a function, whereas echo is just an operator. That means that print can be used in more complex functions and can return a value to a variable. For example, let's consider the use of print in a statement like this:


$var1 = print "ABC";


The value of $var1 would be 1 after completion of this task. Consequently, you could use this variable (and the print operator) in more complicated statements. Because it can be used in such statements, print has a spot in the precedence table, although it's low on the table hierarchy. Bottom line, if you need to evaluate the results of your display statement, use print.


Third, you can attach multiple parameters to an echo, but only one to a print. This means that a single echo statement can print out multiple things (fields, lines, etc.). You have to be careful how you set it up; the parms are separated by commas, no parentheses.


            echo "First line", "<br>", "Second line";


This renders as such:


First line

Second line


At the same time, while print will allow only one parm, print will allow concatenation of multiple values (as will echo).


$name = "Dave";

      print "My Name is " . $name . ".";


This renders like so:


My Name is Dave.


For more information on echo and print, see the PHP Manual or Google PHP echo vs. print.

Skipping Lines

One of the annoying but probably quite logical things about HTML is that just starting a new line of text in the text editor does not mean you're starting a new line on the browser. To do that, you need to use an HTML break tag (<br>) as shown below.


            echo "First Line" . "<br>" . "Second Line";


This renders like so:


           First Line

     Second Line

Combining PHP and HTML Tags

Using just HTML, you can't specify a variable value in a text paragraph. But using PHP you can do that. Suppose we wanted to build a sentence kind of thing that included data and text.


One of the most important constructs is using the period or the comma (which we talked about above) to allow PHP and HTML elements to be used on the same line. In this case, the HTML elements are enclosed in quotes, the PHP elements are not, and the period symbol (or comma for echo only) is used to separate them. You can have a space before or after the period/comma for readability or not. It's up to you.


For example, if we want to code up a line that combines both HTML text and a PHP variable, it could look like this:


      $variable1 = "Start";

      $variable2 = "End";

echo "First variable is " . $variable1 . " and the second is " . $variable2 . ".";


This would render like this:


First variable is Start and the second is End.


Or perhaps you want to put some formatting into it:


           echo "<h3>" . "My first name is " . "<em>" . $variable1 . "</em>" . "?" . "</h3>";


This would render this way:


            My first name is Dave?


I know, looks kind of confusing to begin with, but it's really pretty simple once you play with it for awhile. Take it apart element by element and see what's in there.


What's important here is that generally it doesn't matter whether you use echo or print, although if you want to evaluate the result of your display, then you have to use print. And it doesn't matter if you separate things with commas or periods, although if you're using print, you can't use commas to separate.


What does matter is that with a little practice you'll be able to print out mixed variables and text with either operator and even be able to apply formatting to what you put out there, something that is indispensible if you want to have nice-looking data moved from your arrays to a Web page. And that brings us back to where we started: How do you pick up the data in a two- (or more) dimensional array and display it on the page? But for the details on that, you'll have to wait for the next installment.


David Shirey

Dave Shirey is president of Shirey Consulting Services (, providing technical and business consulting services for the IBM i world. Among the services provided are IBM i technical support, including application design and programming services, ERP installation and support, and EDI setup and maintenance. With experience in a wide range of industries (food and beverage to electronics to hard manufacturing to drugs (the legal kind) to medical devices to fulfillment houses) and a wide range of business sizes served (from very large, like Fresh Express, to much smaller, like Labconco), SCS has the knowledge and experience to assist with your technical or business issues. You may contact Dave by email at or phone at 616 304 2466.