CHAPTER 9

image

A PHP Primer for Drupal

PHP is the programming language used in Drupal. As you may already know, Drupal uses a LAMP stack. The “P” in the word LAMP stands for PHP (the first letters stand for Linux, Apache, and MySQL). PHP is the programming language that does most of the work to make Drupal what it is—it is the guts of Drupal. This chapter teaches you the basics of the PHP programming language. It serves as a great primer for users new to the PHP language, and for users who have dabbled in PHP already, it serves as a good review of PHP. You need to understand this basic building block of Drupal if you want to become an expert in the Drupal content management system. This chapter gets you started.

Image Note  In presenting PHP to you for the first time, my first thought is to remind you once again that this book’s aim is to teach you to fish. In this chapter I do my best to make sure you understand the basic concepts of PHP. Importantly, I also show you where to learn more about the details. A programmer’s career involves quite a lot of “googling” when it comes to PHP syntax or how to do X, Y, or Z. Fortunately, the open source programming community—and the programming community in general—is generous about sharing code, examples, and troubleshooting tips.

Verifying Your Installation of PHP

First, make sure PHP is installed on your computer. To do this, open Terminal and type php –v at the command prompt. Your screen should display information about which PHP version you’re using. Following is the output on my machine, which is running PHP version 5.36:

james-louis-barnetts-macbook-pro-2:js barnettech (master)$ php -v
PHP 5.4.10 (cli) (built: Jan 21 2013 15:12:32)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.0-komodo, Copyright (c) 2002-2012, by Derick Rethans

If PHP is not installed, refer to Appendix A of this book for install instructions. The appendix includes directions for how to install Drupal. And if Drupal is working on your machine, then PHP is installed as well.

Image Note  If PHP is not installed, your install will differ depending on what operating system you have. Macs come with PHP installed, although I personally installed a different version called MAMP. If you google “MAMP,” you’ll easily find MAMP, which comes with an installer. If you use Windows, I’ve had a good experience with the Zend Server Community Edition: www.zend.com/en/products/server/downloads?src=greybar. There are many other LAMP installers out there for Macs, Windows, and Linux—all easily findable with a quick google for “LAMP Mac” (or whatever operating system you prefer).

Serving Up a Hello World Web Page with PHP

In this section you write your first bit of PHP. With this simple bit of code, you learn to use PHP to create a Hello World web page. As you may recall, the Hello World page in a programming language simply shows how to print out the words “hello world” in the programming language.

  1. In your code editor, create a new file and call it hello_world.php.
  2. In that file add the following simple line of code:
    <?php echo 'HELLO WORLD!!!!!'; ?>
  3. Now, at the command line (make sure you’re in the same directory as your PHP script or you’ll have to use absolute or relative paths to reference your PHP script), type the following:
    php hello_world.php

    After you type the preceding line, the phrase “HELLO WORLD!!!!!” gets outputted to the terminal. Figure 9-1 shows what this output looks like on my screen (quite a simple program still).

    9781430264668_Fig09-01.jpg

    Figure 9-1. Running the hello_world.php script on the command line

  4. Put the file hello_world.php in your “docroot” folder; you will navigate to http://localhost/hello_world.php. Now, bring up this extra simple PHP file in a web browser.

Recall from Chapter 4 that your Drupal docroot directory is the base, or “home,” directory of your Drupal installation—it’s where all your Drupal files and folders reside. To serve up PHP pages for a web browser, you’ll want to put your files within this docroot folder. Here’s where to find your docroot directory.

  • If you change the configuration (in the Apache configuration file), you can really put it most anywhere. But out of the box, when you install LAMP, there will be a default place for the docroot. Often the name of the folder will be htdocs. If you use LAMP out of the box on a Mac, it will be in your Sites directory, which is within your user’s home directory. For example, on my Mac it’s here: /Users/barnettech/Sites. If you used Acquia Dev Desktop, the path would also be in your Sites directory; the path on my Windows machine is C:UsersJames BarnettSitesacquia-drupal.
  • In Windows, you usually find the docroot by navigating to the Program Files directory, then find where Apache is installed, and then within the Apache directory you’ll find the htdocs directory, which is the docroot.
  • DAMP, the Acquia Drupal installer for Windows, puts the Drupal docroot within C:/Users/your_username/Sites/acquia-drupal.

Generally, you can access the file using the pattern I just described: http://localhost/hello_world.php. If you use subfolders, it would simply be http://localhost/subfolder_name/hello_world.php. Using MAMP (my Mac version of LAMP), it uses a different port (8888, but the default is usually 80 by the way—and it can be omitted from the URL (uniform resource locator) if using the default). Mine looks like http://localhost:8888/hello_world.php. Because it’s a nonstandard port, I have to specify the port. If you cannot bring up the web page in your browser, it’s probable that Apache is either not installed correctly or hasn’t been started.

So you now have a PHP Hello World page. Now what? Well, recall from this book’s introduction to JavaScript that you learned the concepts of variables and arrays. You’ll also run into loop structures. These components are, in general, the basic building blocks of a programming language. You should feel comfortable with the fact that once you’ve learned one programming language, like JavaScript, the others you learn, like PHP, will be much easier. Time to meet some basic PHP.

Working with Some Basic PHP Code

To help you get comfortable with some PHP basics, this section provides a quick exercise that includes inline documentation, made-up comments that explain along the way what the code is designed to do. In this exercise you try out some basic PHP code, which shows usage of variables and the basic rand function, which is a PHP function that generates a random number.

Image Note  When you work in programming, you often hear that the code is your documentation. Some people say this means that you should just be able to read the code and know what the programmer did. Others (and I am one of them), however, mean that you should be able to read the code to follow along. In this case the author of the code provides comments within the code itself to guide you along, especially for anything tricky or for anything so complex it would suck away your time trying to wrap your brain around it. The code in this chapter takes this approach, including comments throughout that explain what the code is doing.

  1. In your Apache docroot directory, create a new a file called basic_php.php and type the following code:
    <!DOCTYPE html>

    <html>
    <head>
      <style>
        .background-image {
          width:1000px;
          height:500px;
          background:url(ocean.jpg) repeat;
          border:1px solid black;
        }
        .the-text {
          width:900px;
          height:360px;
          margin:30px 50px;
          background-color:#ffffff;
          border:15px solid black;
          opacity:0.6; /* We're playing with the opacity of the background
          image here so the text we put on top of it is more readable. */
          filter:alpha(opacity=60); /* For IE8 and earlier */
        }
        /* We're going to put all the output created with straight HTML
        colored with red text.*/
        .html {
          color: red;
          font-size: 35px;
        }
      /* We're going to put all the output created with PHP colored with
      green text.*/
        .php {
          color: green;
          font-size: 35px;
        }

      </style>

      <title>Basic PHP</title>
    </head>

    <body>
      <div class='background-image'>
      <div class='the-text'>
    <div class='html'>Note how I can mix HTML with PHP programming. After
    this bit of text, which is still within the "HTML mode," I'm going to
    switch into using some PHP, wrapping all my PHP in the PHP tags as seen
    below (red text on this page is in straight HTML and green text that
    you see was done using PHP:</div>
    <?php  // This is my tag signaling we're in "PHP Mode" now.
    // Here below is a variable, an integer. An integer is a numeric value.
    $age = 5;

    // And here below is another variable, a string, which contains
    // letter or numbers.
    // I like the definition of a string on php.about.com
    /*(http://php.about.com/od/programingglossary/qt/What-Is-A-String.htm):
    "A string is a type of data. A string can contain letters, numbers or
    characters. Although a string can contain numbers, it is not a numeric
    value. A string can be short or long."*/

    $name = "Jimmy Bo Bob";

    /* See the PHP docs on how to generate a random number. PHP documentation
    is quite good actually: http://php.net/manual/en/function.rand.php */
    $random_number = rand(1,20);

    echo '<div class="php">' . $name . ' who is ' . $age . ' years old,
    received ' .
    $random_number . ' dollars from the tooth fairy for losing his first tooth!
    </div>';

    ?>  <!--And here is my closing PHP tag. After this line we'll be back
    to using HTML programming. -->
      </div>
      </div>
    </body>
    </html>

Be sure to carefully review the comments in the preceding code, which begin with the characters //, because these comments explain what the various parts of the code are doing. Essentially, this code shows how to add PHP to your HTML. Save your code and display the file in your web browser (see Figure 9-2).

9781430264668_Fig09-02.jpg

Figure 9-2. PHP code example in a web browser

Working with PHP Looping Structures

All programming languages have looping structures, structures allowing you to run one or more lines of code repetitively. This section looks specifically at what I’m referring to as “looping structures” and what they look like in PHP.

Using the foreach Loop

So you have an array with many elements in it. Recall that an array is one element, kind of like a filing cabinet, and it has many drawers. The beauty of an array is all those drawers fit into one variable, which you declare as an array. You learn soon how to pass an array to a function for the function to use the array. It’s very convenient to have to pass just one variable around, rather than 10, 20, or even more variables.

$my_array = array('John', 'Curly', 'Moe');
// Now let's loop through the elements of the array and say hi to each
// member in this array.

foreach($my_array as $person) {
 // The </br> HTML element will force the cursor to the next line –
 // a line break.
  echo 'Hello' . $person . '</br>';
}

In plain English, this code says that for each element of the $my_array array, put each individual element in turn into the $person variable in the foreach statement. The first time the code’s execution goes through this loop, it grabs the first element in the array, which is John, and puts the element into the $person variable. Then, it can operate on the $person variable (which at this point just contains the string John). In this case, it just prints out a greeting to John: Hello John. Then when it hits the closing parenthesis of the foreach structure, it goes to the top and grabs the next element in the array, in this case Curly. Then it runs through the code again within the curly braces of the foreach loop. Now it can operate on the $person variable again, but this time the $person variable contains the string Curly. When the foreach loop runs out of items in the $my_array variable, the foreach loop concludes and processing continues with whatever code comes next.

The output of the preceding code will be:

Hello John
Hello Curly
Hello Moe

Using loops, especially in conjunction with arrays, is as common as breathing in programming languages. This, I find, is the most common looping structure, but there are others, including the while loop.

Using the while Loop

Another looping structure is the while loop (www.w3schools.com/php/php_looping.asp), which looks like as follows:

x=1;
while(x<3) {
  x = x + 1;
  // This can also be written x++; which is just a shorthand way of writing
  //   this same code telling the processor to take x and add 1 to it.
  //    x is an integer in this case.
  echo 'Hello </br>';
}

So the code in the curly brackets will run for as long as x is less than 3. Thus the output will just be

Hello
Hello
Hello

You’ll also see another basic “filing cabinet,” which is used just like an array—it’s called an object. You’ll often have to loop through an object to get at all the elements inside of it.

$my_object->first_name = 'Jimbo';
$my_object->last_name = 'Bob';
echo 'Hello ' . $my_object->first_name . ' ' . $my_object->last_name;

The preceding code will give you the following output:

Hello Jimbo Bob.

The use of echo in PHP tells the system to print the following string to the screen. The "." character concatenates (adds to the output) what follows, which is the variable $my_object->first_name. In this case the output will be Jimbo because that is the value of the variable $my_object->first_name. Then you concatenate (add to the output) a space and then you concatenate (add to the output echoed to the screen) the value of the variable $my_object->last_name, which is set to Bob.

You can also loop through the object in PHP with foreach in PHP. “Looping through the object” refers to the code one at a time going through the object and grabbing each element, as shown here.

$x = 1;
foreach($my_object as $property) {
    echo 'property ' . $x . ' in the object is : '. $property . '</br>';
    $x = $x + 1;
}

The output will be

property 1 in the object is : Jimbo
property 2 in the object is : Bob

Here, the foreach statement steps through (loops through) the object, and the code prints out each element of the object.

Printing to the Screen

As shown in the preceding code, the basic method to print to the screen in PHP is to use the echo or print commands. For instance, consider the following code:

$my_message = "Hello, I hope you are having a good day!";
echo $my_message;

This code will print your message to the screen as output. Likewise, the print command will, like the echo command, print the same output to the screen. Consider the following block of code:

$my_message = "Hello I hope you are having a good day!";
print $my_message;

There you have it—two simple ways to print to the screen: the PHP echo and print commands.

Using If, If Else, and Else If Statements

The if statements and if else statements allow us to conditionally execute some code, which is extremely useful and is used daily by programmers. Take a look at the following code:

$my_name = "James";
if ($my_name == "James") {
  echo 'Hello James';
}
else {
  echo 'Hello ' . $my_name . '. You are not James, but it sure is nice
  to meet you!';
}

In this case the $my_name variable is, in fact, equal to "James", so the output is

Hello James

Consider what would happen if you set the first line in the earlier code to the following:

$my_name = "Rob";

Then it will print out the following:

Hello Rob. You are not James, but it sure is nice to meet you!

Notice that a single equal sign assigns a value to a variable, and a double equal sign checks if the variable is equal to some value. The double or triple equals sign is a comparison operator. Other comparison operators can be seen as follows:

x = 5;
y = 10;
if (x < y) {
  echo 'x is in fact less than y';
}
if (y > x) {
  echo 'y is in fact greater than x';
}

You can also use <= or >= to evaluate if a value is less than or equal to another value or to see if a value is greater than another value or equal to said value.

There is also the elseif statement to consider.

$my_name = "James";
if ($my_name == "James") {
  echo 'Hello James';
}
elseif ($my_name == "Rob") {
  echo 'Hello Rob ' . 'Thanks for drawing the original cover art for
  version 1 of this book!';
}
else {
  echo 'I am not sure who this is really, but thanks for coming to the show,';
}

You can see that this code does an elseif and checks if the value of $my_name is equal to "Rob". You can add as many elseif statements to check for other specific values. Often, if there are a lot of cases to check, then you’ll use a switch statement, covered next.

Switch Statements

A switch statement is just like an if statement. It lets you conditionally execute some code; typically, I use a switch statement when I have more than three cases to check. The following code shows an example:

$name = "James";
// Check the name variable for different values.
switch ($name) {
  // In the case that the name variable is equal to "James" or "Jimmy",
  // execute this code.
  case "James":
  case "Jimmy":
    echo "This is the author of this book.";
    break;
  // In the case that the name variable is equal to "Jack", execute this code.
  case "Jack":
    echo "I actually don't know a Jack. Who are you?";
    break;
  // In the case that the name variable is equal to "Josh", execute this code.
  case "Josh":
    echo "I know a Josh. Yes, that is you. Nice to see you again!";
    break;
  //  In the case that there are no previous matches in the case
  // statement, then for all other cases execute this code.
  default:
    echo "If your name is anything else, well, nice to meet you as well.";
}

As you can see, using a switch statement keeps your code looking cleaner looking than using many if statements. It is a bit more efficient (less typing) and easier to read.

Working with Strings

When working with strings, there are some great commands, such as strlen, strpos, and stripos.

Image Note  Remember that a string can be any number of alphabetic, numeric, or other characters that will not be operated on using math. Strings are usually for humans (not computers) to read; to a computer, a sentence or paragraph is just a string of characters.

strlen

The strlen command will give you the length of the string. Take the following code as an example:

echo 'the length of my last name "Barnett" is ' . strlen("Barnett").;

The output of this code is

The length of my last name "Barnett" is 7.

Here is a link to the PHP documentation for strlen: http://us2.php.net/strlen.

strpos and stripos

The strpos and stripos commands will find the first occurrence of whatever you’re looking for in another given string. Note that stripos is the same as strpos, except that when searching the second string for an instance of the first string, stripos is case insensitive (it will find a match even if the capitalization of the letters doesn’t match); strpos is case sensitive.

Take a look at the following code:

$haystack = 'abcdefghi';
$needle  = 'a';
$pos = strpos($haystack, $needle);
echo '------';
echo 'needle was found at location ' . $pos . ' in the haystack string';
echo '-----';

The output of this code is

: ------needle was found at location 0 in the haystack string-----

Notice that in the preceding code the counting starts with zero. In the larger string abcdefghi, the a character is at position 0, b is at position 1, and c is at position 2.

The following code shows how strpos and stripos can be useful:

$haystack = "This is the string we will be searching";
$pos = stripos($haystack, "STRING");
if ($pos === FALSE) {
  print "The string was not found in the haystack variable ";
}
else {
  print "Found the string at position $pos! ";
}

Running this code produces the following output. Note that the use of forces a line break to be printed to the screen.

Found the string at position 12!

But consider what happens when you change the code to the following:

$haystack = "This is the string we will be searching";
$pos = stripos($haystack, "STRRRING");
if ($pos === FALSE) {
  print "The string was not found in the haystack variable ";
}
else {
  print "Found the string at position $pos! ";
}

In this case the string doesn’t exist, because I obviously spelled the word STRING as STRRRING. In this case, this is the output.

The string was not found in the haystack variable

Notice that the code checked if $pos === FALSE with a triple equals sign. This approach is used because if the position found is at position 0, then 0 within PHP is evaluated as FALSE and 1 is evaluated as TRUE. (Remember, computers think in zeros and ones after all.) So the triple equals sign also checks that you’re comparing the same types of values. In this case you are looking for an actual Boolean value of FALSE. If $pos really is FALSE and not a value of 0, which is an integer, then you don’t want to evaluate to FALSE. The triple equal sign ensures you get an actual Boolean value to evaluate, not an integer.

Using Functions

Functions are a way to package up logic or functionality into reusable blocks. A function to check if a user is a class instructor or teacher’s assistant, for instance, might go into a function and get called over and over again throughout your code. Functions are very useful and very common in PHP programming. Consider the following code, which includes an explanation of what is happening at key points:

// Notice the name of the function is name_check and it takes a variable
// as an argument, $my_name.
function name_check($my_name) {
  if ($my_name == "James") {
    $content = 'Hello James';
  }
  elseif ($my_name == "Rob") {
    $content = 'Hello Rob ' . 'Thanks for drawing the original cover art
    for version 1 of this book!';
  }
  else {
    $content = 'I am not sure who this is really, but thanks for coming
    to the show.';
  }
  return $content;
}

// Notice here we call the name_check function and pass in the
// value ‘Walter’ into the function which becomes the value of
// the $my_name variable in the function.
$return = name_check('Walter');
echo $return;

This code puts the value returned after executing the code in the name_check function into the $return variable. The code then prints out the value of the $return variable:

I am not sure who this is really, but thanks for coming to the show.

You passed the value 'Walter' into the name_check function and into the $my_name variable. It is also possible to pass multiple variables into a PHP function. Consider the following code:

function name_check($my_name, $evaluation) {
  if ($my_name == "James") {
    $content = 'Hello, James';
  }
  elseif ($my_name == "Robert") {
    $content = 'Hello Rob ' . 'Thanks for drawing the original cover
    art for version 1 of this book! ';
    $content .= ' ' . $my_name . ' ' . $evaluation;
  }
  else {
    $content = 'I am not sure who this is really, but thanks for coming
    to the show.';
  }
  // Below we return the value of the $content variable, which will get
  // stored in the $return variable;
  return $content;
}

$return = name_check('Robert', 'is awesome');
echo $return;

In this case you call name_check, put the results into the $return value, and print out the $return variable contents. But this time you pass in two values: 'Robert' and 'is awesome'. These values get sent into the first line of the name_check function:

function name_check($my_name, $evaluation) {

The $my_name variable takes the value of the first value passed in, which is 'Robert', and the $evaluation variable takes on the value of the second value passed in, which is 'is awesome'.

You can also set default values for variables by declaring a function, as follows:

function name_check($my_name = 'John Doe', $evaluation = 'is ok') {

In this case, if you do not set $my_name variable when you call the function, it takes on a default value of 'John Doe', and the $evaluation variable takes on the value 'is ok' if you do not set when the function is called.

By using functions you have to write a block of complex code only once, and then you can easily call the code whenever you like. This approach also provides abstraction so that you and other programmers don’t need to get involved in the complexities of the block of code in a function and can just use said code, making life easier for whomever needs to use that function. Object-oriented code, beyond the scope of this book, is often used primarily to take advantage of this concept. My favorite book about PHP object-oriented code structure is PHP Objects, Patterns, and Practice by Matt Zandstra (Apress, 2005) (www.apress.com/9781590599099). The book does a nice job of making a complex subject easy to read about.

Functions Operating on Arrays

Arrays are extremely useful in PHP, and there are many functions in PHP that allow you to work with arrays. The list of functions is quite long, and you can find the full list at www.php.net. The official site for PHP documentation is www.php.net/manual/en/ref.array.php. The following sections look at a few of my favorites.

array_search

The array_search function searches through an array for a value and returns the key of where it’s found in the array. If the value is the first element in the array, the key will be 0; if it’s the second item in the array, it will return the integer 1; if it’s the third element of the array, it returns the integer value 2. (Remember, the computer often starts counting from 0, not 1.) If the array_search function doesn’t find what it’s looking for in the array, it returns the Boolean value FALSE. Consider the following code:

$array = array('blue', 'purple', 'green', 'red');
$key = array_search('green', $array); // $key = 2;
echo $key;

The output from this small bit of code is the integer 2 because in the list, green is, in fact, at position 2. Notice that you don’t have to define the keys in the following line:

$array = array('blue', 'purple', 'green', 'red');

The first element key by default is 0, then 1, 2, 3, and so on. Each key corresponds with the value in the array; these are known as key/value pairs. You can reference a value by referencing the key. For instance, you can do the following:

$array = array('blue', 'purple', 'green', 'red');
echo $array[3];

This code prints out red to the screen because you printed out the third element of the array (again, counting in an array starts from 0, not 1).

You can also define the names of keys in an array. Look at the following code, which contains an array with these key/value pairs:

$array = array('positon-one' => 'blue', 'position-two' => 'purple',
'position-three' => 'green', 'position-four' => 'red');
echo $array['position-two'];

Running this code prints out purple to the screen. It is useful to define the names of your array’s keys for easier reading and organization of your code. When doing more complex things, you can use variables as the key names, so you can dynamically create arrays. The key names can vary depending on the flow of your code.

You can also put arrays or objects within an array. Think of it as putting a filing cabinet within a filing cabinet. Drupal is known for having enormous arrays, and you’ll often see arrays and objects within arrays, or arrays within objects. For documentation on the array_search command, visit http://www.php.net/manual/en/function.array-search.php.

array_push

The array_push command adds an array element to the end of the array. Take the following code as an example:

$my_array = array("orange", "banana"); array_push($my_array, "apple", "raspberry", "pear");
// print_r prints out whatever is in a variable and it handles printing
// out of arrays and objects better than an echo or print command.
print_r($my_array);

This code prints out the new array with its new elements: apple, raspberry and pear are now added to the end of $my_array. Following is the output:

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
    [4] => pear
)

You can see that the array now has the new fruits appended to the end of the array. The print_r command printed out the array, nicely formatting the array’s key/value pairs. For documentation on the array_push command, visit www.php.net/manual/en/function.array-push.php.

asort, arsort, ksort, and krsort

The assort, arsort, ksort, and krsort commands sort your arrays alphabetically by values or by keys. asort sorts your array’s values; arsort sorts your array’s values in reverse order. ksort sorts your array by the array’s keys in alphabetical order; krsort sorts your array’s keys in reverse alphabetical order.

array_keys and array_values

The commands array_keys and array_values let you put just either the array’s keys or its values alone into an array, which can be useful. Take the following code:

$array = array('position-one' => 'blue', 'position-two' => 'purple',
'position-three' => 'green', 'four' => 'red');
$keys = array_keys($array);
$values = array_values($array);

echo 'The arrays keys are: ';
print_r($keys);
echo 'The arrays values are: ';
print_r($values);

This code returns the following output:

The arrays keys are: Array
(
    [0] => position-one
    [1] => position-two
    [2] => position-three
    [3] => four
)
The arrays values are: Array
(
    [0] => blue
    [1] => purple
    [2] => green
    [3] => red
)

More documentation on array_keys and array_values is available at www.php.net/manual/en/function.array-keys.php and www.php.net/manual/en/function.array-values.php.

TAKING ADVANTAGE OF THE OFFICIAL PHP DOCUMENTATAION

I’ve made reference to the official PHP documentation in the section “Functions Operating on Arrays,” but I want to call attention to the official PHP documentation on www.php.net. I’ve programmed using many different programming languages, and by far I find the PHP community’s documentation to be the best. Generally, each function or concept in PHP has its own page (each PHP function has its own page). More important, there are multiple examples of how to use the function being discussed, so if the first or second examples confuse you, just move on to another example.

Summary

This chapter looked at how to use PHP. After creating a couple of web pages that made use of PHP, you studied some of the key logic behind PHP, which included looping structures and conditional code with if statements and switch statements. You then looked at PHP arrays, objects, string, integers, and Booleans. You looked at comparing strings and integers with ==, ===, <, > and <=, >= operators. In addition, you studied some key PHP functions such as strlen, strpos, and stripos. You looked at the value of packaging up some code into functions, which then can be called over and over again without recoding the same logic. Next, you looked at lots of functions for interacting with arrays: array_search, array_push, asort, arsort, ksort, krsort, array_keys, and array_values. Finally, you took a look at the great http://www.php.net documentation, where you can learn about the many more commands available to you to craft your web and command-line PHP applications. Now that you know some of the basics of PHP, the next chapter puts that knowledge to work, teaching you to add PHP-generated output to your Drupal module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset