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 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
Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.