IT16
Integrative
Programming
& Technologies 2
Fundamentals of PHP
(Data Types & Operators)
Prepared by: Daryl Factor
PHP Data Types
• PHP is a loosely typed language, meaning you don’t have to declare a
variable’s type explicitly.
• PHP will determine it automatically based on the value assigned.
• Categories: Scalar, Compound, & Special Data Types
Scalar Data Types
• These hold single values (not collections).
• Integer
• Whole numbers (positive, negative, or zero) without a decimal point.
• Range (on 64-bit systems): approximately -9,223,372,036,854,775,808
to +9,223,372,036,854,775,807
• Example:
• Functions: is_int(), intval()
Scalar Data Types
• These hold single values (not collections).
• Float (Double / Real numbers)
• Numbers with a decimal point or in exponential form.
• Example:
• Functions: is_float(), floatval()
Scalar Data Types
• These hold single values (not collections).
• Boolean
• Represents true or false.
• Often used in conditional statements.
• Example:
• Functions: is_bool()
Scalar Data Types
• These hold single values (not collections).
• String
• Sequence of characters enclosed in single or double quotes.
• Double quotes allow variable interpolation and escape sequences.
• Example:
• Functions: strlen(), strtoupper(), substr()
Compound Data Types
• These can hold multiple values.
• Array
• Collection of values (indexed or associative).
• Example:
• Functions: count(), array_push(), array_merge()
Compound Data Types
• These can hold multiple values.
• Object
• Instances of classes.
• Example:
• Functions: is_object(), get_class()
Special Data Types
• NULL
• Represents a variable with no value.
• Example:
• Functions: is_null()
Special Data Types
• Resource
• Special variable holding a reference to an external resource
(e.g., database connection or a file).
• Example:
• Functions: get_resource_type()
PHP Type Casting
• Type casting in PHP means manually converting a variable from one
data type to another.
• PHP is a loosely typed language, which means variables don’t require a
declared type and can change type automatically.
• However, sometimes you may want explicit control over the type of a
variable.. that’s when type casting is used.
Example (without type casting):
Here, PHP performed type juggling automatically. But with type
casting, we can force the type.
Type Casting Syntax
• Type casting in PHP is done by placing the target type in parentheses
before the variable or value:
• General Syntax:
Where type can be:
• (int) or (integer) – Cast to integer
• (bool) or (boolean) – Cast to Boolean
• (float) or (double) or (real) – Cast to floating point
• (string) – Cast to string
• (array) – Cast to array
Rules per Casting Type
• Integer Casting
• Float → Truncated towards zero (no rounding).
• String → Converted to integer if it starts with a number.
• Boolean → true becomes 1, false becomes 0.
• NULL → becomes 0.
Rules per Casting Type
• Boolean Casting
• Rules (Falsy values in PHP):
• 0 (integer)
• 0.0 (float)
• "0" (string containing zero)
• Empty array []
• NULL
• All other values are true.
Rules per Casting Type
• Float (Double/Real) Casting
• Notes:
• Integers become floats (e.g., (float) 5 → 5.0).
• Strings are parsed until a non-numeric character is found.
Rules per Casting Type
• String Casting
• Special Cases:
• true → "1", false → "" (empty string)
• NULL → "“
• Arrays → "Array" (not very useful — better use json_encode())
Rules per Casting Type
• Array Casting
• Notes:
• Scalar values become an array with one element (index 0).
• Objects become an associative array of their properties.
Type Casting vs Type Juggling
• Type Casting → You manually set the type.
• Type Juggling → PHP automatically changes the type depending on the
operation.
Type Casting vs intval(), floatval(), strval()
• You can also convert types using functions:
• Key Differences:
• (int) $var and intval($var) are almost the same, but intval() allows specifying a base
for conversion:
• Type casting is slightly faster than using these functions.
Practical Use Cases
• Sanitizing user input
• Ensuring correct type for calculations
• Forcing boolean checks
PHP Operators
• Arithmetic Operators
• Used for basic mathematical operations on numeric values.
Operator Description Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference
* Multiplication $x * $y Product
/ Division $x / $y Quotient
% Modulus $x % $y Remainder after division
** Exponentiation $x ** $y $x raised to $y
PHP Operators
• Increment / Decrement Operators
• Change a variable’s value by +1 or -1.
• Pre-increment (++$x): Increases first, then returns new value.
• Post-increment ($x++): Returns current value, then increases.
• Pre-decrement (--$x): Decreases first, then returns new value.
• Post-decrement ($x--): Returns current value, then decreases.
PHP Operators
• Assignment Operators
• Assign values to variables.
• They can also perform a calculation and assign in one step.
Operator Example Equivalent
= $x = 5 Assign 5 to $x
+= $x += 3 $x = $x + 3
-= $x -= 2 $x = $x - 2
*= $x *= 4 $x = $x * 4
/= $x /= 5 $x = $x / 5
%= $x %= 3 $x = $x % 3
**= $x **= 3 $x = $x ** 3
PHP Operators
• Comparison Operators
• Compare two values and return a Boolean (true or false).
Operator Description Example Result
== Equal $x == $y true if values are equal
true if values and types
=== Identical $x === $y match
!= or <> Not equal $x != $y true if values differ
true if values or types
!== Not identical $x !== $y differ
> Greater than $x > $y true if left > right
< Less than $x < $y true if left < right
>= Greater or equal $x >= $y true if left ≥ right
<= Less or equal $x <= $y true if left ≤ right
PHP Operators
• Logical Operators
• Combine multiple conditions in decision-making.
PHP Operators
• Ternary Operator
• Short form of an if-else statement.
• Example:
Thank you!