Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Functions and commands in PHP

Wednesday, June 11th, 2014 by Servage

figure2

The Difference Between The Echo And Print Commands

So far you have seen the echo command used in a number of different ways to output text from the server to your browser. In some cases, a string literal has been output, in others, strings have first been concatenated or variables have been evaluated. I’ve also shown output spread over multiple lines.

But there is also an alternative to echo that you can use: print. The two commands are quite similar to each other, but print is an actual function that takes a single parameter, whereas echo is a PHP language construct.

By and large, the echo command will be a tad faster than print in general text output, because—not being a function—it doesn’t set a return value.

On the other hand, because it isn’t a function, echo cannot be used as part of a more complex expression, whereas print can. Here’s an example to output whether the value of a variable is TRUE or FALSE using print—something you could not perform in the same manner with echo, because it would display a “Parse error” message:

$b ? print "TRUE" : print "FALSE";

The question mark is simply a way of interrogating whether variable $b is TRUE or FALSE. Whichever command is on the left of the following colon is executed if $b is TRUE, whereas the command to the right is executed if $b is FALSE.

Generally, though, the examples in this article use echo, and I recommend that you do so as well until you reach such a point in your PHP development that you discover the need for using print.

Functions

Functions are used to separate out sections of code that perform a particular task. For example, maybe you  need to look up a date often and return it in a certain format. That would be a good example to use a function for. The code doing it might be only three lines long, but if you have to paste it into your program a dozen times, you’re making your program unnecessarily large and complex, unless you use a function. And if you decide to change the data format later, putting it in a function means you’ll have to change it in only one place.

Placing such code into a function not only shortens your source code and makes it more readable, it also adds extra functionality (pun intended), because functions can be passed parameters to make them perform differently. They can also return values to the calling code.

To create a function, declare it in the manner .

A simple function declaration

<?php
function longdate($timestamp)
{
return date("l F jS Y", $timestamp);
}
?>

This function takes a Unix timestamp (an integer number representing a date and time based on the number of seconds since 00:00 AM on January 1, 1970) as its input and then calls the PHP date function with the correct format string to return a date in the format Monday August 1st 2016. Any number of parameters can be passed between the initial parentheses; we have chosen to accept just one. The curly braces enclose all the code that is executed when you later call the function.

Sources for further reading

Functions and commands in PHP, 5.0 out of 5 based on 1 rating
Categories: Guides & Tutorials, Software & Webapps

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.