Saturday 21 July 2012

PHP Language Structure

DATA TYPES

Data types in PHP are generally divided into two categories namely scalar and composite. A scalar variable contains only one value at a time and PHP supports four scalar types:
  1. boolean – can either be true or false.
  2. int – signed (can be positive or negative) numeric integer value.
  3. float – signed floating point value.
  4. string – collection of characters.
Compound data types are essentially containers of other data and PHP supports two types:

  1. Arrays – containers of groups of other data elements.
  2. Objects – containers of both data and code. They are the basis of object-oriented programming and will be discussed in a separate tutorial.
Other data types that PHP supports are the following:
  1. NULL – indicates that a variable has no value either because it has not yet been assigned a value or because it has been explicitly set to NULL.
  2. resource – Used to indicate external resources that are not used natively by PHP. For example, a database. In order to test which data type a variable has been defined as, PHP makes available a set of functions. The is_* set of functions can be used for just this. For example, is_bool() can used to test whether a given variable is a boolean. Here are some of the functions used to test for different data types:
  • is_bool();
  • is_null();
  • is_int();
  • is_string();
  • is_double();
  • is_array();
  • is_numeric();
  • is_resource();

VARIABLES

A variable is a storage container that can contain any type of data including integers, floating point numbers, strings, boolean values, arrays and objects. PHP is loosely typed and this means that the data type of a variable is determined when the data is assigned to the variable. For example, if the number 2 is assigned to a variable the variable will take on the data type of an integer or if the sentence "PHP is cool!" is assigned to a variable then the data type will automatically revert to that of a string.
Variables are fundamental to any programming language and without them you would be forced to hard-code each value you would want to use in your scripts. The syntax used to define a variable is the dollar($) sign followed by an identifier name. Variable names may only consist of letters, numbers and underscores and must start with a number or underscore. They are also case-sensitive.
The scope of a variable only extends as far as the script or function in which it resides. That is to say that if a variable
$test = "test"
is defined in scriptA.php it cannot be used in scriptB.php. So how do I create a variables that are available outside the scope of the script or function in which it resides? You can define a variable as global which means that if two scripts are connected to each other the variable will be available from either script and that the variable will be available outside of a function is it is so declared. This will be discussed in more detail in the tutorial on functions.
Also, PHP provides a set of predefined Superglobals. The Superglobals are always present, and their values are available across all scripts and functions. We will discuss these in more detail in a later tutorial.

CONSTANTS

Unlike variables, constants are used to define values that must remain unchanged. Constants can contain only scalar values and are, like variables, case-sensitive. They follow the same naming requirements as variables with the exception of the leading $ sign. It is, however, considered best practice to define constants using upper-case names.
To define a constant the PHP define() function is used and once a constant is defined it will remain unchanged unless the define() function is used to re-declare the same constant with a different value.
define(‘EMAIL’, ‘test@example.com’);
echo EMAIL; // displays test@example.com

OPERATORS

Operators are the symbols used to manipulate the data that is stored in variables and there are many types of operators in PHP. As a matter of interest, an operand is the value that an operator interacts on in an expression. The most commonly used operators are:
  1. Assignment Operators
  2. Arithmetic Operators - Perform arithmetic operations. They perform the same functions as arithmetic but the modulus
  3. String Concatenation Operators
  4. Combined Assignment Operators
  5. Comparison Operators – Compare two operands to each other and return a boolean value.
  6. Logical Operators – Test combinations of boolean values
More videos and tutorials available from tutormeonline.co.za and Tutormeonline's Youtube Channel

No comments:

Post a Comment

Please feel free to comment. No backlinks will be displayed.