. 32
( 132 .)


void echo ( string str1 [, string strN ...] )

Keep in mind that you can mix variables and literal strings within double quotes:

$var = “this string”;
echo “Please print $var”;

This will print Please print this string.
However, within single quotes the string will be treated literally:

$var = “this string”;
echo ˜Please print $var™;

The preceding code will print Please print $var. The concept of mixing vari-
able names and string literals is discussed in greater detail in Chapter 4.

print versus echo. Which should you use? This is very much a matter of
personal preference: Use whichever you think looks better in your script.
There™s only one major difference between the two, and this may influence
your decision. echo can take multiple arguments. That is, with echo, differ-
ent portions can be separated by commas.This will work:
echo “this is part 1”, “this is part 2”;
But this will not:
print “this is part 1”, “this is part 2”;

They didn™t come up here, but these are really important to know about.
166 Part II: Working with PHP


string sprintf (string format [, mixed args])

This function can be used to output a string formatted according to a specified
C-style pattern. The parameters of the pattern are covered in detail in the online
PHP documentation (http://www.php.net/sprintf).

PRINT_R() This function is great for putting to productive use the time you™d oth-
erwise spend pulling your hair out. It prints the entire contents of any variable ”
most notably arrays and objects ” to the screen.

void print_r (mixed expression)

It is invaluable for debugging. We use it frequently when we™re not getting the
results we expect from arrays or objects.

Do not do print_r($GLOBALS).You will create a very large output.

VAR_DUMP() This function behaves like print_r, but gives you a bit more

void var_dump (mixed expression)

In addition to printing out the contents of a variable, it includes the data type ”
including the data type for each element in an array or object. The same caution
given for print_r() applies to var_dump().

Date/time functions
Dealing with PHP and MySQL as a team, you will have to get to know two sets of
date/time functions ” and they are quite different. See Appendix J for detailed cov-
erage of MySQL™s time and date functions.

The following are some date/time functions used in the applications in this book.

DATE() You can use this function and the indicators outlined next to return the
date and time.

string date (string format [, int timestamp])
Chapter 6: PHP™s Built-in Functions 167

If you include a second argument, that time/date value will be formatted as you
prescribe. Otherwise, the current time and date will be used.

The time and date the functions return are based on the time on the server.
You will need to make use of JavaScript to get an idea of the time on the
client™s computer.

Often the second argument will be a product of the mktime() function,
which we discuss next.

You can format the date using any of the indicators in Table 6-1.


Indicator Meaning

am or pm

AM or PM

Swatch Internet time

Day of the month, two digits with leading zeros; 01 to 31

Day of the week, textual, three letters; for example, Fri

Month, textual, long; for example, January

Hour, 12-hour format without leading zeros; 1 to 12

Hour, 24-hour format without leading zeros; 0 to 23

Hour, 12-hour format; 01 to 12

Hour, 24-hour format; 00 to 23

Minutes; 00 to 59

I [capital i ] 1 if Daylight Savings Time, 0 otherwise

Day of the month without leading zeros; 1 to 31

168 Part II: Working with PHP


Indicator Meaning

l (lowercase l) Day of the week, textual, long; for example, Friday
Boolean for whether it is a leap year; 0 or 1

Month; 01 to 12

Month, textual, three letters; for example, Jan

Month without leading zeros; 1 to 12

Seconds; 00 to 59

English ordinal suffix, textual, two characters; for example, th, nd

Number of days in the given month; 28 to 31

Time-zone setting of this machine; for example, MDT

Seconds since the epoch (midnight, January 1, 1970)

Day of the week, numeric; 0 (Sunday) to 6 (Saturday)

Year, four digits; for example, 1999

Year, two digits; for example, 99

Day of the year; 0 to 365

Time-zone offset in seconds; -43200 to 43200

For example, if you want to print the date in the format, June 5, 2003 4:22 pm,
this would do the trick:

echo date(“F d, Y g:i a”);

MKTIME() This function is most useful for calculating valid dates.

int mktime (int hour, int minute, int second, int month, int day,
int year [, int is_dst])

For example, say you have a form that collects a date ” maybe the current month,
day, and year. You want to calculate and set a due date exactly 30 days from the
date submitted.
Chapter 6: PHP™s Built-in Functions 169

$year = 2003;
$month = 5;
$day = 24;
echo date(“l F d, Y”, mktime(0,0,0,$month,$day+30, $year) );

This will output 30 days from May 24, 2000, and will print out Friday June
23, 2000.
Keep in mind that this function enables you to add or subtract dates without
worrying that PHP will return a fictitious result. In the previous example, you could
subtract six from the month value of 5, and PHP would return a meaningful date.
You can add or subtract any number of years, months, or days without worrying
that PHP will return a bad result. For instance, the following is a perfectly accept-
able way to get date information about the last day of 1999:

$year = 2000;
$month = 1;
$day = 1;
echo date(“l F d, Y”, mktime(0,0,0,$month,$day-1, $year) );

This code will let you know that December 31, 1999 was a Friday.
Notice that the preceding code first calculates the timestamp of the date indi-
cated by mktime() and then prints that out using the date function.
If you exclude arguments from the right, those parameters will be retrieved from
the current timestamp. So, to print what the date and time will be in five hours, this
will do the trick:

echo date(“l F d, Y g:i a”, mktime( date(˜H™)+5) );

Note the nesting of functions here. Starting at the innermost function,
date(˜H™) returns the current hour, in 24-hour format. Then five is added to that,
and the timestamp is calculated for five hours in the future. The timestamp is then
formatted using the string indicated.

TIME() This function returns the current time measured in the number of seconds
since the Unix Epoch. The Unix Epoch is the beginning of time in Unix terms ” the
time with timestamp 0. It™s arbitrarily defined as January 1 1970 00:00:00 GMT.

int time(void);

MICROTIME() This function returns the string msec sec where sec is the current
time measured in the number of seconds since the Unix Epoch (0:00:00 January 1,
1970 GMT), and msec is the microseconds part.

string microtime(void);
170 Part II: Working with PHP

This function is only available on operating systems that support the gettime
ofday() system call.
The returned string will look something like 0.12082400 969034581. You can
be reasonably sure that this function will never return the same number twice. It is
often used to seed the random number generator.

A few other time/date functions may prove useful to you. They include several for
printing the current date and time. If you need to know about something specific
that isn™t discussed here, take a look at the manual: http://www.php.net/manual/

File-system functions
PHP has a whole range of functions that enable you to manipulate files and direc-


. 32
( 132 .)