In our previous tutorial, we covered the basics of defining variables and working with strings. In this tutorial we are going to introduce a few more concepts that will help us manipulate strings and variables.
NULL Value
Sometimes you will no longer have a need for a variable, or you will want to specify a variable's properties without assigning an actual value to it. Both of these situations can take advantage of the null property. Null, much like the English words nil or naught, is used when a variable exists but holds no value. Testing if a variable is null can signal that a certain task has yet to be performed on the variable or that something has changed elsewhere in your program which resulted in the variable losing its value.
Setting a variable to the null type is easy.
Code Example:
Note the lack of quotes. Setting the variable equal to a string with the word "null" is NOT the same as setting a null variable. If you try to print out a null variable, no value will appear on the screen.
PHP Functions
Functional programming led one of the early shifts in computer software design. Functions simplify your program by eliminating redundant code and grouping together code that performs a given task. We are going to explore all of the properties of functions in a later tutorial, but now is a good time to dip our toes in the water.
A function, like a variable, is normally given a name that represents the function's task. Our program will "call" a function, and the function will then perform its task. Sometimes the function will require information input, and we can pass this information to the function through the function's parameters. Often times the parameters will be variables, but this is not a requirement. Sometimes a function will perform a task on its own and we're done with it, and other times the function will return information back to us. This returned information can be saved into a variable, or we can simply ignore it.
Here are two examples of PHP functions:
Code Example:
The first function above, named substr, has two parameters and returns a value, which is assigned into the $newString variable. In this example, the parameters are a string with the word "Hello," and the number 1. We will examine substr later in this tutorial.
The second function is ob_start. This function returns a true or false value, but since we have no use for this information, we will not assign the return value into a variable. The ob_start function will also accept up to three parameters, but since we have no use for them in this example, we can call the function without sending it any additional information.