# Primitive Data Types

Updated on Dec 22, 2021 by [Alan Morel](/content/alanmorel/index.html)

## Table of Contents

1. [Integer](/content/classes/php/primitive-data-types#integer/index.html)
2. [String](/content/classes/php/primitive-data-types#string/index.html)
3. [Floating Point Number](/content/classes/php/primitive-data-types#floating-point-number/index.html)
4. [Boolean](/content/classes/php/primitive-data-types#boolean/index.html)
5. [Null](/content/classes/php/primitive-data-types#null/index.html)

**Primitive data types** refers to the type of data that can be held in a variable or constant. They are called primitive because the data type is fundamental to the language and cannot be broken down any further. This includes strings of text, numbers, booleans, and more. Let's go over each one and see how they're used.

## Integer

**Integers** are numbers that don't contain a decimal point, or whole numbers. They can also be positive or negative numbers. Here's how to declare them:

```php
<?php
    $red = 4;
    $blue = -6;

echo($red);
    echo($blue);
?>
```

```html
4
-6
```

## String

**Strings** are a sequence of characters that ultimately make up text of some sort. Strings themselves can contain numbers in them and are declared by enclosing your text with either single or double quotes:

```php
<?php
    $string1 = 'I have 9 pieces of gum.';
    $string2 = 'My favorite genre of music is hip hop.';

echo($string1);
    echo($string2);
?>
```

```html
I have 9 pieces of gum.
My favorite genre of music is hip hop.
```

### Concatenation

You can **concatenate**, or combine, strings together by using the concatenation operator, like so:

```php
<?php
    $string1 = 'I have 9 pieces of gum.';
    $string2 = 'My favorite genre of music is hip hop.';
    $string3 = $string1 . ' ' . $string2;
    echo($string3);
?>
```

```html
I have 9 pieces of gum. My favorite genre of music is hip hop.
```

`$string3` was a new string made from two existing strings that were combined thanks to concatenation. On that note, [PHP](/content/classes/php/index.html) provides a number of built-in functions to work with strings. Let's explore the more popular ones.

### strlen

To get the length of a string, that is, how many characters make up the entire string, use the `strlen` function, which stands for string length.

```php
<?php
    $string = 'This is an example string.';

echo(strlen($string));
?>
```

```html
26
```

### str_word_count

The `str_word_count` function is used to count how many words are in a string. Here it is used on the same string as before:

```php
<?php
    $string = 'This is an example string.';

echo(str_word_count($string));
?>
```

```html
5
```

### str_replace

The `str_replace` function is used to replace all occurrences of the first string you pass in as a parameter with the second string you pass in as a parameter. Let's look at an example:

```php
<?php
    $string = 'Fast cars are better than slow cars.';

echo(str_replace('cars', 'trains', $string));
?>
```

```html
Fast trains are better than slow trains.
```

We replaced both occurrences of `cars` with `trains` to get our new string.

### strrev

The `strrev` function reverses the order of the string of characters:

```php
<?php
    $string = 'Fast cars are better than slow cars.';

echo(strrev($string));
?>
```

```html
.srac wols naht retteb era srac tsaF
```

### strtolower

The `strtolower` function takes a string and lowercases it entirely.

```php
<?php
    $string = 'Fast cars are better than slow cars.';

echo(strtolower($string));
?>
```

```html
fast cars are better than slow cars.
```

### strtoupper

The `strtoupper` function takes a string and uppercases it entirely.

```php
<?php
    $string = 'Fast cars are better than slow cars.';

echo(strtoupper($string));
?>
```

```html
FAST CARS ARE BETTER THAN SLOW CARS.
```

### str_repeat

The `str_repeat` function takes a string and repeats it as many times as you want. The first parameter is the string you want repeated and the second parameter is the number of times you want it to be repeated.

```php
<?php
    $string = 'ha';

echo(str_repeat($string, 5));
?>
```

```html
hahahahaha
```

## Floating Point Number

Unlike integers, which are whole numbers, **floating point numbers** are decimals or fractional numbers. They otherwise aren't too different from integers and can be initialized the same way:

```php
<?php
    $decimal = 3.14159;

echo($decimal);
?>
```

```html
3.14159
```

## Boolean

**Booleans** are a very simple data type that only have two valid values, `true` or `false`.

```php
<?php
    $hungry = true;

echo($hungry);
?>
```

```html
1
```

## Null

The **null** value is strange. It actually represents nothing at all. You can assign `NULL` to a variable, but it literally means that it is empty.

```php
<?php
    $strange = NULL;

echo($strange);
?>
```

```html

```

Nothing appears because `$strange` has no real value assigned to it. The assignment we just did is equivalent to if we declare a variable but not assign it any value, like so:

```php
<?php
    $strange;

echo($strange);
?>
```

```html

```
