Programming Fundamentals - PHP QuickStart Guide (2015)

PHP QuickStart Guide (2015)

Chapter 1: Programming Fundamentals

There are several things you'll need to understand about programming in order to get the greatest benefit from this book. Programming languages differ greatly, but they all share certain traits, and those characteristics are the basis for knowing how to create a program.

This chapter is an introduction to some of the terminology and concepts associated with programming. You'll find many of the terms described in this chapter in the glossary; however, a good grasp of what they mean now will make the following chapters much easier to follow. Let's look at some programming basics.

Constructs

The term “construct” as a noun refers to any assembly of parts of a language that matches the rules of syntax of that language. In English, for instance, the word “a” is a construct. We can use it as-is, because it's a recognizable article of speech.

Constructs in programming languages are basically the same. They may be used independently of other programming elements and are sometimes incorrectly referred to as “commands.” It's important to know about constructs because they sometimes don't follow the same rules as other program elements. The “echo” construct is an example we'll use often in our code examples. It causes whatever follows it to be output immediately, normally to the computer screen.

Statements

A statement is the smallest part of a programming language. It's comparable to a sentence in a spoken language, in that it needs to meet certain criteria dictated by the programming language. Statements may identify elements of a program or contain instructions for the computer. A statement may be considered a line of code; however, it's important to realize that PHP statements may span more than one line on a page. By the same token, multiple PHP statements can be written on the same line of a page.

Functions

A function in a program is a sequence of statements grouped and named with an identifier so that it can be executed from anywhere within the program. By creating functions, a programmer provides an easy way to perform operations without the need to repeat the statements.

Functions may require the input of values when executed. For instance, a program may contain a function that performs math operations on values that must be entered each time the function is executed or “called.” In other cases, these values may be parameters limiting the operations of the function. This input is often referred to as “passing” the values. In PHP, values are passed within parentheses immediately following the name of the function.

Most programming languages, including PHP, include a number of predefined functions and allow for user-defined functions as well.

Constants and Variables

One of the ways we optimize computer programs is by storing values in computer memory for use in various routines within the programs. By storing a value as a block of data, we can use it repeatedly in a program without the need to find its value each time. These “reusable” values are normally stored in one of two ways, as a constant or as a variable.

As the terms imply, the main difference between the two is whether the value of the data can be changed within the program. As you've probably guessed, the value of constants cannot be changed, while variables can be acted on by elements of the program to change the data. Constants are most often used for fixed values that might need to be changed periodically because of some external influence, such as a tax rate. They may also be used to store long strings of text or complex numbers in a way that makes them easier to insert into the program code.

PHP includes several predefined constants, most of which are used for configuring core installations and troubleshooting programming problems.

Note: When a variable is stored, extra space is allocated in computer memory to allow for changes. Since constants can't be changed by the program, only the amount of memory necessary for the original data is used. The data for a constant will also be optimized on the computer for faster access. This saves resources and computing time, also known as “overhead,” which is the chief reason to use constants when feasible.

PHP constants are created and assigned a value by means of a predefined function:define. Constant names are case-sensitive. In other words, “CONSTANT,” “Constant” and “constant” would describe three separate names.

Note: While there is no rule governing the case used for constant names, the accepted standard is to use all uppercase letters in a constant name, as “CONSTANT.” This makes the element clearly identifiable.

PHP Variables are created and assigned their initial values with a statement called a declaration. Variable names are also case-sensitive. While any combination of letter case can be used in a variable name, it's good practice to be consistent throughout your programs. All variable names must begin with the $” character.

Each programming language has its own set of rules for declaring variables and constants, as well as the types of data allowed. In many programming languages, the data type must also be declared. PHP, however, automatically assigns a type to variable and constant data.

Note: Names for constants, variables, functions and other elements are considered “labels.” Programming languages have rules that determine how valid labels are created. In PHP, labels may contain:

-Any number of the letters A-Z
-Any mix of uppercase and lowercase letters

-Any number of underscore (_) characters

The first character in the label may not be a number; it must begin with a letter or underscore.

Scope

The scope of a variable, constant or function defines where and how it can be used within a program. Programs, as a rule, have two scopes: local and global. An element with a local scope can only be used within the function where it's declared. Global elements can be used in other areas of the program.

Note: PHP constants always have a global scope.

PHP provides an additional scope: static. A variable declared with a static scope has a local scope, but also retains its value when the program flow leaves that scope. For example, a static variable whose value was changed the last time the function containing it was executed, it will have the new value if and when that function is called again.

Arrays

Arrays are special variables that store collections of values and associate a key or index with each value stored. Arrays can also be stored as values within arrays. By utilizing arrays, multiple values can be manipulated as a unit. An individual value within an array can be accessed by way of its assigned key or numeric index.

PHP arrays can be created with thearray() construct. Several predefined arrays also exist within PHP for handing server processes and other actions that may generate a large number of values that can be used in PHP operations.

Operators

If you remember math class, you'll have a basic understanding of what operators are. They're the characters that perform the operations on the elements of a problem. In programming, however, operators can perform many other operations. Operators are used to manipulate text, perform logical operations, assign values and much more, in addition to performing all of the math functions you're familiar with. PHP operators can be separated into at least 12 groups for different purposes.

Programming operators, like mathematical operators, have an order of preference. Unlike mathematical operators, however, the preference of a PHP operator in a program doesn't designate order of operations, but how operators are grouped. Complex operations are beyond the scope of this book, but it's important to know that relying on the familiar mathematical order of operations may yield inconsistent results. Parentheses can and should be used to specify the order of operators in complex operations.

Operators can also be categorized by their associativity, which determines whether they apply to values on the left, right, or none at all (non-associative). This property also affects grouping, rather than order.

A table of operators showing their preference and associativity can be found in Appendix A.

Keywords

Programming keywords are words that serve a particular purpose in a programming language. They are similar to functions and are often incorrectly referred to as operators or statements, but actually belong in a class of their own and are usually used as building blocks for other program structures. The words “if,” “else” and “elsif” are examples common to several programming languages, including PHP.

Using PHP keywords as variable names should be avoided to prevent confusion, and using them as names for constants, functions, classes or methods is not allowed and will cause program errors.

A list of PHP keywords is available under Appendix A.

Expressions

Here's another term you may recognize from math class. An expression is a way to state a value. In other words, you're expressing a value in a particular way. For example, the numeral 3 is an expression of the integer value 3. It can also be expressed as 2 + 1, 14 – 12, 3 * 1, 3/1, “The number of years since my 3-year-old was born,” and in countless other ways.

That means that almost anything that makes up a program might be considered an expression:

- A function will return a value.
- A declaration assigns a value.
- Evaluating a formula returns a value.
- A comparison evaluates to TRUE or FALSE.

Therefore, all of the above are expressions.

Almost everything you write in a PHP program will be an expression. Evaluating those expressions is what makes the program work.

Control Structures

Constructs and statements that control the program flow are referred to as control structures. A control structure may specify conditions (conditional) or simply tell the program to do something (instructional). In most instances, a control structure will determine what happens next by analyzing the data provided to it and applying predetermined rules. These are the elements that help make decisions and perform actions like looping through a series of statements.

PHP includes a very useful and easily understood set of control structures. We'll be using some of them in code examples later.