Primitive Data Types

Primitive Data Types
udemy2 placeholder image

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
        $red = 4;
        $blue = -6;

        echo($red);
        echo($blue);
    ?>
	
	
    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
        $string1 = 'I have 9 pieces of gum.';
        $string2 = 'My favorite genre of music is hip hop.';

        echo($string1);
        echo($string2);
    ?>
	
	
    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
        $string1 = 'I have 9 pieces of gum.';
        $string2 = 'My favorite genre of music is hip hop.';
        $string3 = $string1 . ' ' . $string2;
        echo($string3);
    ?>
	
	
    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
        $string = 'This is an example string.';

        echo(strlen($string));
    ?>
	
	
    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
        $string = 'This is an example string.';

        echo(str_word_count($string));
    ?>
	
	
    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
        $string = 'Fast cars are better than slow cars.';

        echo(str_replace('cars', 'trains', $string));
    ?>
	
	
    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
        $string = 'Fast cars are better than slow cars.';

        echo(strrev($string));
    ?>
	
	
    .srac wols naht retteb era srac tsaF
	

strtolower

The strtolower function takes a string and lowercases it entirely.

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

        echo(strtolower($string));
    ?>
	
	
    fast cars are better than slow cars.
	

strtoupper

The strtoupper function takes a string and uppercases it entirely.

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

        echo(strtoupper($string));
    ?>
	
	
    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
        $string = 'ha';

        echo(str_repeat($string, 5));
    ?>
	
	
    hahahahaha
	
1and1 placeholder image

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
        $decimal = 3.1459;

        echo($decimal);
    ?>
	
	
    3.1459
	

Boolean

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

	
    <?php
        $hungry = true;

        echo($hungry);
    ?>
	
	
    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
        $strange = NULL;

        echo($strange);
    ?>
	
	

	

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
        $strange;

        echo($strange);
    ?>