Chapter 6: Data Types

Every piece of data in HolyPHP has a type. The type tells the computer what kind of data it is and what operations you can do with it.

Basic Data Types

1. Integer (int)

Whole numbers, positive or negative, no decimal point.

$age = 25;
$temperature = -10;
$bigNumber = 1234567890;

HolyPHP uses 64-bit signed integers.

2. Float (float)

Numbers with decimal points.

$pi = 3.14159;
$price = 19.99;
$temperature = 98.6;

HolyPHP uses 64-bit IEEE floating point numbers.

3. Boolean (bool)

True or false values. Used for making decisions.

$isActive = true;
$isLoggedIn = false;

4. String (string)

Text, enclosed in double quotes " or single quotes '.

$name = "Alice";
$greeting = 'Hello';
$message = "She said \"Hi!\""; // Escaping quotes

5. Array (array)

Lists of values (we'll cover this in detail later).

$fruits = ["apple", "banana", "cherry"];
$numbers = [1, 2, 3, 4, 5];

6. Null

Represents "no value" or empty.

$value = null;

Explicit Type Annotations

While types are usually inferred, you can explicitly declare them when you want:

$count: int = 5;
$price: float = 9.99;
$name: string = "Bob";
$active: bool = true;

Adding explicit types is good practice when you want to be clear about your intent, especially in functions.

Checking Types

HolyPHP has built-in functions to check types:

$x = 42;
var_dump($x);        // Shows type and value

echo gettype($x);    // Returns "integer"

// Type checking functions
if (is_int($x)) { echo "It's an integer"; }
if (is_string($x)) { echo "It's a string"; }
if (is_float($x)) { echo "It's a float"; }
if (is_bool($x)) { echo "It's a boolean"; }
if (is_array($x)) { echo "It's an array"; }
if (is_null($x)) { echo "It's null"; }

Type Conversion (Casting)

Sometimes you need to convert between types:

$numString = "123";
$num = intval($numString);  // Convert to int
echo $num + 1; // 124

$floatNum = floatval("3.14");
echo $floatNum;

$str = strval(123); // Convert to string
echo "Value: " . $str;

Strings and Concatenation

Use the dot . operator to concatenate strings:

$firstName = "John";
$lastName = "Doe";
$fullName = $firstName . " " . $lastName;
echo $fullName; // John Doe
Remember: In HolyPHP, + is for numeric addition, . is for string concatenation (like PHP).
Challenge: Create variables of different types, use var_dump() to inspect them, and practice concatenation with .