PHP: Hot Pages: Basic Code: Custom functions

  1. Variables
  2. Basic Code
  3. Finding errors

PHP allows you to create your own functions as well as use built-in functions like the “date” function. Let’s give impatient people the seconds as well as the hours and minutes of the current time. If someone has visited our page within the last minute, we’ll change the format code to show seconds.

First, create a simple function that returns the time string we’ve been using:

<h1>PHP Time</h1>

<?php

//formatted current time, with seconds for impatient visitors

function currentTime() {

return date('h:i A');

}

$now = currentTime();

?>

There’s no difference here as far as the HTML is concerned. If you reload the page or view the source in the browser, you shouldn’t see anything different, other than potentially the time. This is a very simple function with only one line inside of it. That line returns the current date in the specified format.

PHP can also interact with the browser: it can ask the browser to remember a cookie, and PHP can associate that cookie with other information you choose to save. It does this most easily using sessions. Add this code to the very top of your web page, above every bit of HTML:

<?php

session_start();

?>

<!doctype html>

<html lang="en">

You must start sessions at the very top of your web page. This is because PHP makes requests to the visitor’s browser through HTTP headers, and HTTP headers can’t be sent once any HTML is sent.

We are ready to customize the time depending on how impatient our visitor is. Modify the currentTime function:

//formatted current time, with seconds for impatient visitors

function currentTime() {

$format = 'h:i A';

if (isset($_SESSION['lastVisit'])) {

$lastVisit = $_SESSION['lastVisit'];

if ($lastVisit > time()-60) {

$format = 'h:i:s A';

}

}

$_SESSION['lastVisit'] = time();

return date($format);

}

image 3Reload the page, and then reload it again immediately. The second time you load it, you should see the seconds. If you wait for over a minute to reload the page, the seconds should go away.

In PHP, special variables begin with a dollar sign and an underscore. We’ll see that again when we get to forms. $_SESSION is a special variable that makes it easy to remember your visitors. Once you’ve used session_start you can add values to $_SESSION and look for those values on later visits from that visitor.

Custom functions: Comments

The // marks a comment. PHP ignores comments, so you can write whatever you want after the two slashes. Use comments to remind yourself what this code was meant to do. You should put at least a single comment line above every function and often even above variable names.

Custom functions: Isset

The isset function tells you if a variable exists. For basic variables that may not seem useful—you ought to know if they exist or not—but for the special variables isset is invaluable. With $_SESSION, for example, we often try to remember bits of information from visitors, but we don’t initially know if the visitor has been here before. By using isset to verify that the information exists in $_SESSION before trying to use that information, we can avoid annoying errors.

If the variable exists, isset returns true; otherwise, it returns false.

Custom functions: If

A very important part of any programming is the if statement. This causes the enclosed code to be used only if the statement is “true”. This makes functions like isset that return true or false very useful.

The first time the visitor comes to our page, we haven’t created a session for them. So lastVisit in $_SESSION hasn’t been created either. It doesn’t yet exist, and isset returns false. The lines inside of the if from the left brace to the right brace are ignored.

The code resumes after the closing brace. The time function gets the current time in seconds. We store it in the visitor’s session using the key lastVisit. The second time a visitor comes to our page, they now do have a session and we’ve set lastVisit for them. So we use lastVisit to get the last time they visited.

Then, we have a second if inside the first: if their last visit is less than 60 seconds ago, change the $format variable. More specifically, if the value of time() from the last time they visited is greater than the current value of time() minus 60, that statement is true and the lines between the left and right braces are used.

Custom functions: Time

In Unix and many other server environments, both dates and dates with times are measured in seconds from some arbitrary start time years ago. You won’t normally care when that arbitrary start time is, only how much one time (such as their last visit) differs from another time (such as the current time).

In Unix, there is no difference between dates, such as “January 5, 2012” and times, such as “July 6, 2012 5:15 PM”. They’re all stored as a number of seconds since the system’s arbitrary zero time.

Semicolons and line-endings

In PHP, every line of PHP code ends in either a semicolon, a colon, or a curly bracket. Almost every line ends in a semi-colon.

  1. Variables
  2. Basic Code
  3. Finding errors