In this lesson PHP data types We will cover the topic. PHP supports 10 primitive data types: 4 scalar, 4 compound, and 2 special. In this article PHP data types and you can find examples.

Scalar Data Types

String (Text)

PHP data types The first data type we will learn is: String (text). This data type stores one or more characters. It is expressed with single quotes or double quotes. Let's reinforce this with an example.

$string1 = "Bu metin string1 değişkeninin değeridir";
$string2 = 'Bu metin string2 değişkeninin değeridir';

In PHP, text type data can be stored with single quotes or double quotes. However, there are some rules for using single quotes and double quotes. PHP single quotes and double quotes differences We talked about this topic in our previous lesson.

Integer

The Integer data type stores an integer. Quotations are not used when expressing.

$integer = 658;

PHP data types There are some rules for the Integer data type. Let's take a look at these.

PHP Integer Data Type Rules:

  • An integer must contain at least one digit.
  • An integer must not have a decimal point.
  • An integer can be positive or negative.
  • Integers can be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation

Float (Decimal Number)

The float (decimal) data type stores a decimal/fractional number. Quotations are not used when expressing.

$float = 14.45;

boolean

The bool data type stores one of two possible states: true or false. Also known as Boolean.

$bool1 = true;
$bool2 = false;

Hint: Bool data type is mostly used in conditional testing.

Compound Data Types

Array

An array stores multiple values in a single variable. The $fruits variable in the example below is an array.

// Array tanımlamak için 1.ci yöntem
$fruits = array(
'Fruit Name 1' => 'apple', 
'Fruit Name 2' => 'kiwi',
'Fruit Name 3' => 'orange',
);
// Array tanımlamak için 2.ci yöntem
$fruits = [
'Fruit Name 1' => 'apple', 
'Fruit Name 2' => 'kiwi',
'Fruit Name 3' => 'orange',
];

Object

Objects are instances of user-defined classes that can store both values and functions.

class Car {
    function brand(): string {
        $brand_name = 'BMW';
        return 'Araç Markası: ' . $brand_name;
    }
}
$object = new Car();
echo $object->brand(); // Çıktı: Araç Markası: BMW

Callable

Callbacks can be specified with a callable type declaration.

class CallableClass {
	public static function callableMethod() {
		echo 'hello world';
	}
}
function callableFunction(callable $callable) {
	call_user_func($callable);
}
$callable_obj = new CallableClass();
callableFunction([$callable_obj, 'callableMethod']);

Iterable

Iterable is any value that can be looped through a foreach() loop. iterable is a pseudo-type and was introduced in PHP 7.1.

function printIterable(iterable $my_iterable) {
	foreach($my_iterable as $item) {
		echo $item;
	}
}
$arr = array('a', 'b', 'c');
printIterable($arr);

Special Data Types

Null

Null is a special data type that can have only one value: null. A variable of data type null is a variable that does not have a value assigned to it.

$variable = null;

Resource

In PHP, the resource type is a special data type that refers to any external resource. A resource variable, file, database, etc. It serves as a reference to external data sources such as PHP uses related functions to create these resources. For example, the fopen() function opens a disk file and its reference is stored in a resource variable.

$fp = fopen('test.txt', 'w');
echo get_resource_type($fp);

In this lesson PHP data types we learned. You can write comments for questions you have in mind and topics you want help with.