Chapter 0:Introduction 0-000.000 - About This Course
Chapter 1:PHP Basics 1-1.1 - Hit the ground running.
1-1.2 - A Simple PHP Program
1-1.3 - Formatting the code
1-1.4 - Comments in PHP
1-1.5 - Storing Data in Variables
1-1.6 - The print() Function
|
Chapter: 1 - PHP Basics
Lesson: 1.6 - The print() Function
By now you probably have some understanding of what the built-in PHP function "print()" does. It prints the data that is passed to the function out to the user's browser (or terminal if operating in a shell environment). Thus, when we submit the first print() command in Figure 1-1.6, what the PHP parser sends to the browser is "Hello Steve" followed by a break tag and a newline character.
Basically, anything in the parenthesis of the statement print() will be output to the browser. You can include variables in line with text, integers or just about anything else.
The print() function is one of the PHP built-in functions that we will use quite a bit. The difference between a built in function is that PHP has already defined the function, we do not have to write a function and then pass data to it. It is enough to simply pass data to the print() function as it is. We will cover other built in functions in the coming lessons as well.
Another popular function, built-in to PHP, is the echo() function. It serves basically the same purpose as the print function. So, for the purposes of this lesson, we will just stick with print(). But, you should know that usually when you see an echo() function, it is likely performing the same operation as a print() function would.
Figure 1-1.6, Using print() (download code here)
(Try the sample code)
Again, you probably noticed that my last print() statement spanned two lines. We did this by closing the quotation, appending a "." after a space and carrying over to the next line beginning with another quotation mark and closing as usual. This is helpful when long statements need to carry over multiple lines or, as in this case, I need to keep the code short for imaging it.
As shown in the code above, I have used the print() function to output text, variables, html tags and even newline characters. So, you can see how versatile the print() function really is. Keep in mind that variables passed through a print function will be parsed before being sent to the browser unless they are passed in single quotes, for example the expression print('$johnny'); would print the text $johnny and not whatever the variable $johnny contains. Just watch your quotation marks in the print statement.
We will also be discussing the print() function in further detail later in the study. This should get you started outputting text and variables to the browser.
|