PHP Developer’s Dictionary IT-SC book 80 Some groups of PHP's syntax, such as functions and classes, revolve around the ability of the programmer to define his own functionality. This could be anything from a piece of code to be reused for repetitive calculations to the creation of actual objects. These language elements are defined in this section of the chapter. class Syntax class name { [variable] [function name (){ }] } Description A class is a set of variables and the name d functions (that operate on the variables ) which make up an object. After the object is defined, new instances can be created using the new constructor. A vehicle object, for instance, could contain certain properties (defined by the variables in a PHP class ), such as a color or the number of doors. Additionally, this vehicle object could move or stop—these actions can be defined by the name d functions in the PHP class . class extends Syntax class new_class extends base_class{ [variable] [function name (){ }] } Description The class extends provides the ability to derive new classes, referenced by new_class , out of an existing base_class . In these instances, all the variables and functions are inherited from the base_class in addition to the ones defined by variable and name d functions. create_function() Syntax PHP Developer’s Dictionary IT-SC book 81 string create_function(string arguments, string code) Description The create_function() function, which was added in PHP 4.0.1, creates an anonymous function and returns a unique name for it. The created function performs the code that is passed and can accept any arguments . Because you might need to create a function with more than one argument, you can pass arguments as a single quote, comma-delimited string. For instance, you could do the following: create_function('$arg1,$arg2', 'echo "You passed in $arg1 $arg2"); function Syntax function name([params]){ code } Description A function enables a programmer to define a reusable piece of code. This code could potentially take an optional number of params and potentially return a result. For instance, if you wanted to check the Celsius or Fahrenheit temperature of a number passed, you could create a function that took the numeric temperature value and the type (Celsius or Fahrenheit). This function could then return the corresponding value based in the other unit of measurement. Defining this as a function enables you to reuse the code over and over for an unlimited number of computations. In PHP3, these functions must be defined before they are used, but in PHP4 this has changed. Additionally, PHP4 adds support for variable-length argument lists. Note In addition to function , PHP also has an old_function , which allows for support of PHP/FI2 syntax. However, this feature has been deprecated and should be avoided. We wanted to mention it only in case you run across the function in some legacy applications. new PHP Developer’s Dictionary IT-SC book 82 Syntax variable = new class Description The new keyword creates a new instance named variable of the class specified. Operators Operators are language elements that enable you to evaluate or impose conditions on items, such as variables, in your program. There are also operators that enable you to perform mathematical functions, incrementing and decrementing, and logical operations. An important part of using operators, which were covered in Chapter 1, "Basic PHP," is understanding precedence. Precedence determines the order in which the operators are performed in a given equation. We all remember from grade school that multiplication and division occur before addition and subtraction, which outlines the precedence of these operators. Table 4.1 lists the operator precedence in PHP. Table 4.1. Operator Precedence in PHP Associativity Operators Non-associative new Right [ Right ! , ~ , ++ , , (int) , (double) , (string) , (array) , (object) , @ Left * , / , % Left + , - , . Left << , >> Non-associative < , <= , > , >= Non- associative == , != , === Left & Left ^ Left | Left && Left || Left ? , : Left = , += , -= , *= , /= , .= , %= , &= , |= , ^= , ~= , <<= , >>= Right print Left and Left xor Left or PHP Developer’s Dictionary IT-SC book 83 Left , Arithmetic The list of arithmetic operators contains the list of mathematical operations that are common in all programming languages. These operators enable you to perform the basic adding, subtracting, division, multiplication, and modulus of numerical (and sometimes string) values. + (Addition) Syntax num1 + num2 Description The + operator is used when you want to add num1 to num2 . These values are numerical. - (Subtraction) Syntax num1 - num2 Description The - operator is used when you want to subtract num2 from num1 . These values are numerical. * (Multiplication) Syntax num1 * num2 Description PHP Developer’s Dictionary IT-SC book 84 The * operator is used when you want to multiply num1 by num2 . These values are numerical. / (Division) Syntax num1 /num2 Description The / operator is used when you want to divide num1 by num2 . These values are numerical. % (Modulus) Syntax num1 % num2 Description The % operator is used to obtain the integer remainder of dividing num1 by num2 . For instance, if num1 =5 and num2 =3, the integer returned from this operation will be 2. These values are numerical. $result = 5 % 3; // returns 2 into $result Assignment Assignment operators are some of the most powerful operators available. They enable you to assign a value, which might be the result of a computation, to a variable. After the value is in a variable, it can be used for later processing. This section of the chapter outlines the assignment operators in PHP and how they can be used. = Syntax variable = num . for an unlimited number of computations. In PHP3 , these functions must be defined before they are used, but in PHP4 this has changed. Additionally, PHP4 adds support for variable-length argument. variables in a PHP class ), such as a color or the number of doors. Additionally, this vehicle object could move or stop—these actions can be defined by the name d functions in the PHP class create_function() Syntax PHP Developer’s Dictionary IT-SC book 81 string create_function(string arguments, string code) Description The create_function() function, which was added in PHP 4.0.1,