PHP Coding Building Blocks
To write programs in PHP that do something useful, you'll need to understand blocks of reusable code called functions or methods, and then how to temporarily store information that cannot be executed in variables.
We talk about evaluations, which are basically things that allow your code to make intelligent decisions based on mathematical principles and user input. Since you haven't done any programming, we understand that variables are a new concept.
A variable stores a value, such as the text string "Hello World!" or the integer value 1. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again for the entire life of the variable, which can be frustrating and tedious.
In PHP, you define a variable with the following form: $variable_name = value;
Pay very close attention to some key elements in the form of variables. The dollar sign ($) must always fill the first space of your variable. The first character after the dollar sign must be a letter or underscore. It can't under any circumstances be a number; otherwise, your code will not execute, so watch those typos!
- PHP variables may only be composed of alphanumeric characters and underscores; for example, a-z, A-Z, 0-9, and _.
- Variables in PHP are case sensitive. This means that $variable_name and $Variable_Name are different.
- Variables with more than one word should be separated with underscores; for example, $test_variable.
- Variables can be assigned values by using the equals sign (=).
- Always end with a semicolon (;) to complete the assignment of the variable.
To access the value of a variable that's already been assigned, simply specify the dollar sign ($) followed by the variable name, and use it as you would the value of the variable in your code.
You don't necessarily have to clean up your variables when your program finishes. They're temporary, since PHP automatically cleans them up when you're done using them. Sort of like how Microsoft Word creates a temp file of your document, so when you close the document, the temp file deletes itself.
Variables all store certain types of data. PHP automatically picks a data variable based on the value assigned. These data types include strings, numbers, and more complex types such as arrays. We'll discuss arrays later.
What's important to know is that unless you have a reason to care about the data type, PHP handles all of the details, so you need not worry about that. However, it's good practice to learn about data types.
In situations where a specific type of data is required, such as the mathematical division operation, PHP attempts to convert the data types automatically. If you have a string with a single "2," it will be converted to an integer value of 2. This conversion is nearly always exactly what you want PHP to do and makes coding seamless for you.
PHP helps keep your code organized by making sure that, if you use code that someone else wrote (and you very likely will), the names of the variables in your code don't clash with other previously written variable names.
For example, if you're using a variable called $name that has a value of Bill, and you use someone else's code that also has a variable called $name but uses it to keep track of the filename log.txt, your value could get overwritten.
Your code's value for $name of Bill will be replaced by log.txt, and your code will say "Hello log.txt" instead of "Hello Bill", which would be a big problem. To solve this problem, PHP organizes code into functions. Functions allow you to group a chunk of code together and execute that code by its name.
To keep variables in your code separate from variables in functions, PHP provides separate storage of variables within each function. This separate storage space means that the scope, or where a variable's value can be accessed, is the local storage of the function.
Global variables allow you to cross the boundary between separate functions to access a variable's value. The global statement specifies that you want the variable to be the same variable everywhere, or globally. Global variables should be used sparingly, since it's easy to accidentally modify a variable by mistake. This kind of error can be very difficult to locate.
Additionally, when we discuss functions in detail, you'll learn that you can send in values to functions when you call them and get values returned from them when they're done. That all boils down to the fact that you really don't have to use global variables. If you want to use a variable in a specific function without losing the value each time the function ends, but you don't want to use a global variable, you would use a static variable.
Static variables provide a variable that isn't destroyed when a function ends. You can use the static variable value again the next time you call the function. The easiest way to think about this is to realize that the variable is a global to just that function. A static keyword is used to dictate that the variable you're working with is static.
PHP uses special variables called super globals to provide information about the PHP script's environment. These variables don't need to be declared as global; they are automatically available and provide important information beyond the script's environment, such as values from a user input.
Since PHP 4.01, the super globals are defined in arrays. The older super global variables such as those starting with $HTTP_* that were not in arrays still exist, but their use is not recommended as they are less secure. This variable is especially useful, as it can be used to call the current script again when processing a form.
Super global variables provide a convenient way to access information about a script's environment from server settings to user inputted data. Now that you've got a handle on variables and scope, we can talk about what types of information variables hold.
Variables can hold more than just numbers. They can hold characters and strings, or an ordered list of characters. A string can be used directly in a function call or it can be stored in a variable. Strings are flexible.
You can even insert variables into string definitions when using double quotes to start and end your string. Using a single quote to start and end your string does not allow a variable to be placed in the string.
Tab, newline, and carriage returns are all examples of extra, yet ignorable, whitespace. If you are writing to a file, a valuable tool is an escaped character. One downside of using the apostrophe to start and end a string is that you can't include a variable. This leads us to be careful about using HTML markup or any other string that includes quotes.
PHP has functions to compare strings that aren't exactly alike. For example, you may want to consider "Bill" to be the same as "BILL," ignoring the case of the string. Use strcmp (string1, string2) to compare two strings including the case.
The return value is 0 if the two strings have the same text. Any nonzero value indicates they are not the same. Use strcasecmp (string1, string2) to compare two strings without comparing the case. The return value is 0 if the two strings have the same text. Any nonzero value indicates they are not the same.
Concatenation combines one or more text strings and variables. When performing this combination, you save yourself the hassle of creating numerous echo statements, or in other words, you build up a string and use it. If you combine a string with another data type, such as a number, the result is also a string.