Выбрать главу

Including Other Files

Unless you are restricting yourself to the simplest programming ventures, you will want to share code among your scripts at some point. The most basic need for this is to have a standard header and footer for your website, with only the body content changing. However, you might also find yourself with a small set of custom functions you use frequently, and it would be an incredibly bad move to simply copy and paste the functions into each of the scripts that use them.

The most common way to include other files is with the include keyword. Save this script as include1.php:

<?php

 for($i = 10; $i >= 0; $i -= 1) {

  include "echo_i.php";

 }

?>

Then save this script as echo_i.php:

<?php

 echo $i;

?>

If you run include1.php, PHP loops from 10 to 0 and includes echo_i.php each time. For its part, echo_i.php just prints the value of $i, which is a crazy way of performing an otherwise simple operation, but it does demonstrate how included files share data. Note that the include keyword in include1.php is inside a PHP block, but PHP is reopened inside echo_i.php. This is important because PHP exits PHP mode for each new file, so you always have a consistent entry point.

Basic Functions

PHP has a vast number of built-in functions that enable you to manipulate strings, connect to databases, and more. There is not room here to cover even 10% of the functions; for more detailed coverage of functions, check the "Reference" section at the end of this chapter.

Strings

Several important functions are used for working with strings, and there are many less frequently used ones for which there is not enough space here. We look at the most important here, ordered by difficulty — easiest first!

The easiest function is strlen(), which takes a string as its parameter and returns the number of characters in there, like this:

<?php

 $ourstring = "  The Quick Brown Box Jumped Over The Lazy Dog  ";

 echo strlen($ourstring);

?>

We will use that same string in subsequent examples to save space. If you execute that script, it outputs 48 because 48 characters are in the string. Note the two spaces on either side of the text, which pad the 44-character phrase up to 48 characters.

We can fix that padding with the trim() function, which takes a string to trim and returns it with all the whitespace removed from either side. This is a commonly used function because all too often you encounter strings that have an extra new line at the end or a space at the beginning. This cleans it up perfectly.

Using trim(), we can turn the 48-character string into a 44-character string (the same thing, without the extra spaces), like this:

echo trim($ourstring);

Keep in mind that trim() returns the trimmed string, so that it outputs "The Quick Brown Box Jumped Over The Lazy Dog". We can modify it so that trim() passes its return value to strlen() so that the code trims it and then outputs its trimmed length:

echo strlen(trim($ourstring));

PHP always executes the innermost functions first, so the previous code takes $ourstring, passes it through trim(), uses the return value of trim() as the parameter for strlen(), and prints it.

Of course, everyone knows that boxes do not jump over dogs — the usual phrase is "the quick brown fox." Fortunately, there is a function to fix that problem: str_replace(). Note that it has an underscore in it; PHP is inconsistent on this matter, so you really need to memorize the function name.

The str_replace() function takes three parameters: the text to search for, the text to replace it with, and the string you want to work with. When working with search functions, people often talk about needles and haystacks — in this situation, the first parameter is the needle (the thing to find), and the third parameter is the haystack (what you are searching through).

So, we can fix our error and correct box to fox with this code:

echo str_replace("Box", "Fox", $ourstring);

There are two little addendums to make here. First, note that we have specified "Box" as opposed to "box" because that is how it appears in the text. The str_replace() function is a case-sensitive function, which means it does not consider "Box" to be the same as "box". If you want to do a search and replace that is not case sensitive, you can use the stri_replace() function, which works in the same way.

The second addendum is that because we are actually changing only one character (B to F), we need not use a function at all. PHP enables you to read (and change) individual characters of a string by specifying the character position inside braces ({ and }). As with arrays, strings are zero-based, which means in the $ourstring variable $ourstring{0} is T, $ourstring{1} is h, $ourstring{2} is e, and so on. We could use this instead of str_replace(), like this:

<?php

 $ourstring = " The Quick Brown Box Jumped Over The Lazy Dog ";

 $ourstring{18} = "F";

 echo $ourstring;

?>

You can extract part of a string by using the substr() function, which takes a string as its first parameter, a start position as its second parameter, and an optional length as its third parameter. Optional parameters are common in PHP. If you do not provide them, PHP assumes a default value. In this case, if you specify only the first two parameters, PHP copies from the start position to the end of the string. If you specify the third parameter, PHP copies that many characters from the start.

We can write a simple script to print "Lazy Dog  " by setting the start position to 38, which, remembering that PHP starts counting string positions from 0, copies from the 39th character to the end of the string:

echo substr($ourstring, 38);

If we just want to print the word "Lazy", we need to use the optional third parameter to specify the length as 4, like this:

echo substr($ourstring, 38, 4);

The substr() function can also be used with negative second and third parameters. If you specify just parameter one and two and provide a negative number for parameter two, substr() counts backward from the end of the string. Rather than specifying 38 for the second parameter, we can use -10 so that it takes the last 10 characters from the string. Using a negative second parameter and positive third parameter counts backward from the end string and then uses a forward length. We can print "Lazy" by counting 10 characters back from the end and then taking the next four characters forward: