Basics of PHP – Module 2

Data Types available in PHP

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined): It holds only single value. There are 4 scalar data types in PHP.
    1. boolean
    2. integer
    3. float
    4. string
  2. Compound Types (user-defined): It can hold multiple values. There are 2 compound data types in PHP.
  3. array
  4. object

Special Types: There are 2 special data types in PHP.

  1. resource
  2. NULL

Note: Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource.

Null is a special data type that has only one value: NULL.

Variables in PHP

Variables are the identifier of the memory location, which used to save data temporarily for later use in the program. The purpose of variables is to store data in memory for later use.

We can make any variable types, like numbers, text strings, and arrays. All variables in PHP start with a $ dollar sign, and the value can be assigned by using the = assignment operator.

PHP Variables are case sensitive, variables always defined with a $, and they must start with an underscore or a letter (no number).

In PHP, unlike other languages, variables do not have to be declared before assigning a value.

You also don't require to declare data types, because PHP automatically converts variable to data types depends upon its value.

Constants in PHP

Constants are like a variable, except that their value never changes during execution once defined.

The constant value is immutable.

Constant within a script is accessible to any area; however, they can be scalar values.

Constant names are case-sensitive, and they also follow the same naming requirements like variables, except the leading $.

It is considered best practice to define constants using only upper-case names.

PHP define() function defines a constant.

Types of Operators in PHP

PHP offers various types of operators having different functioning capabilities.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Concatenation Operators

Comments in PHP

Single Line Comments

// (Single line comment)

Multi Line Comments

/* (Multi line comment) */