<<

. 36
( 132 .)



>>

Object buffering allows you to cache instances of object in a region of memory,
making them easily accessible across multiple sessions. Quite a few object-buffering
functions exist. We used very few of them.
188 Part II: Working with PHP

OB_START() This function starts the buffer.

void ob_start(void)

FLUSH() This function clears the buffer.

void flush(void)

OB_END_CLEAN(), OB_END_FLUSH()

void ob_end_clean (void)

void ob_end_flush (void)

These two functions both turn off output buffering, but ob_end_flush() sends
the contents of the buffer to output first, whereas ob_end_clean() deletes the con-
tents of the buffer without sending them.

BUFFERING FUNCTIONS NOT USED IN THIS BOOK
Check the online manual for some more sophisticated buffering functions.

Information functions
These functions will give you information about the environment in which you are
working.

phpinfo()
int phpinfo ([int option])

This is your guide to all that is available in your PHP environment. Use it. Use it.
Use it. And then take it off your system. There™s no point in letting crackers get a
look at the specifics of your system.
The option argument specifies what you want information about, such as
installed modules. Have a look at the manual page (http://php.net/phpinfo) for
a list of legal option values.

phpversion()

string phpversion (void)

This function returns only the version of PHP you are using.
Chapter 6: PHP™s Built-in Functions 189

php_sapi_name()

string php_sapi_name (void)

This function returns a string indicating the type of Server Application
Programming Interface (SAPI) interface that exists in the current configuration. For
example, if the CGI version of PHP is running, the string will be cgi.

extension_loaded()

bool extension_loaded (string extensionname)

This function returns TRUE if the input PHP extension is loaded, and FALSE if it
is not.

ini_get()
string ini_get (string varname)

This function returns the value of a PHP configuration option.

ini_set()

string ini_set (string varname, string newvalue)

This function assigns a new value to a PHP configuration option. Refer to the
PHP online documentation for information on which options can be controlled by
this function.



Summary
As you™ve seen, PHP has more functions than you will be able to commit to mem-
ory anytime soon. It can seem intimidating, but the quantity and quality of these
functions are what make PHP such a great language. Most anything you need to do
can be done quickly and painlessly.
At first you may need to study and play with the functions in order to get them
to work. But in time it will get a lot easier. You™ll be making use of more and more
functions, and keeping your scripts more compact and easier to read.
Chapter 7

Writing Organized and
Readable Code
IN THIS CHAPTER

— Keeping your code tidy

— Understanding the power and convenience of functions

— Using object-oriented code

— Learning the importance of comments



THIS a run-through of the preferred ways to present and orga-
CHAPTER PRESENTS
nize code. Along the way you will see how to construct functions and classes in
PHP. By the end of this chapter you should have a good idea of how write efficient,
readable applications in PHP, and you should be ready to dive into the applications
in Parts III and IV of this book.



Indenting
If you have done coding in any language, this point should be pretty obvious. But
it is an important point and, therefore, deserves some mention. In the type of cod-
ing needed for Web applications, following a few indenting rules can help make
your life a little easier.


How far should you indent? Some feel that each level of code should be
indented by three spaces. Others, like us, think a single tab is the way to go. If
you use spaces, it is possible that your code will look terrible in a different
text editor (maybe the one used by your co-worker). We believe tabs are a
better choice anyway, but some people have the opposite opinion with the
same motivation. So really, what™s important is consistency.



191
192 Part II: Working with PHP


Code blocks
The most obvious use of indenting comes in differentiating blocks of code. For
instance, it is fairly typical to have an if block within a while loop:

$i = 0;
while ($i < 100)
{
$i++;
if ($i < 50 )
{
echo “Within the first 49.”;
}
else
{
echo “Between 50 and 99.”;
}
}

As you can see in this PHP code, each block is delimited by curly braces ({});
this goes for both while loops and if blocks. When a block is entered with an
opening curly brace, the next line should be indented. Each line following at the
same level of execution should be indented at the same level. Additional nested
blocks should be indented another level.
Looking at the preceding brief snippet of code, it is easy enough to see that it
contains three distinct blocks. This might not seem like such a big deal with a small
bit of code like this, but as scripts get longer, and levels of nesting get deeper, you
will see how important it can become. We™re not going to belabor this point because
it should be pretty clear. But, for a quick example, we present the previous code
without indents. Note that it works just fine ” PHP doesn™t care if you don™t write
your code neatly. But imagine coming back to this a month after you wrote it and
having to troubleshoot or add code. Life is a lot easier if you can easily find the
block that needs work.

$i=0;
while ($i < 100)
{
$i++;
if ($i < 50 )
{
echo “Within the first 49.”;
}
else
{
Chapter 7: Writing Organized and Readable Code 193

echo “Between 50 and 99.”;
}
}

If you like, you can even run everything together, like this:

$i=0; while ($i < 100) {$i++; if ($i < 50 ) { echo “Within the
first 49.”; } else { echo “Between 50 and 99.”; } }

The interpreter truly does not care. Indeed, you™ll sometimes see code generated
by other programs that looks that way. Such organization is obviously a nightmare
to analyze and maintain.
You can also omit some of the braces, like so:

$i = 0;
while ($i < 100)
{
$i++;
if ($i < 50 )
echo “Within the first 49.”;
else
echo “Between 50 and 99.”;
}

This is an easy way make your code a little more compact, and in some ways
easier to read, if ” and let™s stress that if ” you™re writing simple if-else statements
like this one. Remember, though, indenting alone does not a code block make. This
code:

$i = 0;
while ($i < 100)
{
$i++;
if ($i < 50 )
echo “Within the first 49.”;
else
echo “Between 50 and 99.”;
echo “ (which is getting up there)”;
}

will not treat those last two echo statements the same, even though it looks like
it might. In fact, the preceding code is equivalent to the following:

$i = 0;
while ($i < 100)
194 Part II: Working with PHP

{
$i++;
if ($i < 50 )
{
echo “Within the first 49.”;
}
else
{
echo “Between 50 and 99.”;
}
echo “ (which is getting up there)”;
}

When in doubt, use braces.


Are you getting a parse error you can™t identify? Make sure you have an iden-
tical number of opening and closing curly braces and parentheses. If you
have, for example, five closing curly braces in a page and only three opening
ones, you haven™t opened at least two of your code blocks. Most code
editors ” which are really just text editors with a few extra features for
programming ” enable you to park your cursor on a single brace and either
use a hotkey combination to find its match or highlight the block of code it
marks. If the matching brace or marked region isn™t what you expect, you
know how to advance further down the road to bug-free code.



Function calls
Indenting code should not stop at code blocks. Often you need to use nested func-
tion calls or complex variables that take up several lines. You will be much happier
in your coding life if you use indents in these situations. Take a look at the follow-
ing, which is borrowed from the Catalog application:

$file_ext = strtolower(
substr(
$file
, strrpos($file,”.”)
)
);

The purpose of this code is pretty simple: It takes the name of a file and assigns
its extension (the characters following the final dot (.)) to $file_ext. It takes three
separate built-in PHP functions to get this done. PHP executes the innermost level
Chapter 7: Writing Organized and Readable Code 195

first. There, strrpos() finds the numeric position of the final dot. For example, for
the string myfile.jpg it would return 6. Then the substr() function returns only

<<

. 36
( 132 .)



>>