PHP: Data types
Programs work with different kinds of information: text, numbers, dates, lists, boolean values. Every value in a program has a type.
For example:
- "Hello, World!" is a string (
string) - 7, -198, 0 are integers (
int) - 3.14, -0.01, 1.0 are rational numbers (
float)
What is a data type?
A data type defines:
- which values belong to it;
- which operations can be performed with it.
For example, numbers can be added, divided, and multiplied. Strings, on the other hand, are joined differently: through concatenation. Multiplying a string by a string is meaningless and not allowed:
// Not allowed: 'mom' * 'notepad'A programming language recognizes types. That's why PHP won't let us multiply text by text, but it will let us multiply a number by a number. The presence of types and such restrictions in the language protects programs from accidental errors.
Numbers and strings belong to different types
Unlike strings, numbers don't need to be wrapped in quotes. Here's an example of printing a number:
<?php
print_r(5); // => 5
print_r(-5); // => -5Here's an example of printing a string:
<?php
print_r('5'); // => 5
print_r('-5'); // => -5On the screen the result looks the same, but inside the program these are completely different things:
| Value | Data type |
|---|---|
5 | int (integer) |
'5' | string (string) |
The number 5 and the string '5' are different values, and operations with them work differently. That said, when types don't match, PHP often tries to convert one type into another on its own. How convenient this is and what makes it dangerous we'll look at a bit later in this module.
Integers and rational numbers
PHP distinguishes between two kinds of numbers:
intdenotes integers: -3, 0, 7, 100000floatdenotes real (rational) numbers: 1.0, -3.14, 2.718
This division is due to the peculiarities of how computers are built — we talked about this in the lesson about floating-point numbers.
Example:
<?php
print_r(10.234); // => 10.234
print_r(-0.4); // => -0.4They can hold fractional values and be used in calculations:
<?php
print_r(3.5 + 1.2); // => 4.7
print_r(5.0 / 2.0); // => 2.5
print_r(2.75 - 0.5); // => 2.25Primitive types
Types such as:
- string,
- int (integer),
- float (rational number)
are called primitive: they are built right into the PHP language itself.
PHP primitive types
├── int : integers (7, -3, 0)
├── float : fractional numbers (3.14, -0.5)
├── string : strings ('hello')
└── bool : boolean type (true, false)Besides strings and numbers, PHP has the boolean type bool with the values true and false, as well as the special value null. We'll encounter them in more detail in the future.
There are also composite types: arrays, objects, and others. We'll get to know them later. Moreover, in PHP you can create your own types (for example, classes), but first it's important to get a good grasp of the primitives.
Strings and lines
In English, strings in programming are called strings, while lines of text files are called lines. In Russian there can be confusion, so throughout the lessons we'll say "строка" for the "string" data type and "строчка" for lines in files.
Instructions
The program already defines the current year:
<?php
$currentYear = 2026;Write a program that prints profile data in the following format:
Name: Anna
Birth year: 1994
Age: 32
Rating: 4.7To do this:
- create a variable with the birth year;
- calculate the age based on the current year;
- print the data on the screen with labels on the left.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
PHP: Data types
Programs work with different kinds of information: text, numbers, dates, lists, boolean values. Every value in a program has a type.
For example:
- "Hello, World!" is a string (
string) - 7, -198, 0 are integers (
int) - 3.14, -0.01, 1.0 are rational numbers (
float)
What is a data type?
A data type defines:
- which values belong to it;
- which operations can be performed with it.
For example, numbers can be added, divided, and multiplied. Strings, on the other hand, are joined differently: through concatenation. Multiplying a string by a string is meaningless and not allowed:
// Not allowed: 'mom' * 'notepad'A programming language recognizes types. That's why PHP won't let us multiply text by text, but it will let us multiply a number by a number. The presence of types and such restrictions in the language protects programs from accidental errors.
Numbers and strings belong to different types
Unlike strings, numbers don't need to be wrapped in quotes. Here's an example of printing a number:
<?php
print_r(5); // => 5
print_r(-5); // => -5Here's an example of printing a string:
<?php
print_r('5'); // => 5
print_r('-5'); // => -5On the screen the result looks the same, but inside the program these are completely different things:
| Value | Data type |
|---|---|
5 | int (integer) |
'5' | string (string) |
The number 5 and the string '5' are different values, and operations with them work differently. That said, when types don't match, PHP often tries to convert one type into another on its own. How convenient this is and what makes it dangerous we'll look at a bit later in this module.
Integers and rational numbers
PHP distinguishes between two kinds of numbers:
intdenotes integers: -3, 0, 7, 100000floatdenotes real (rational) numbers: 1.0, -3.14, 2.718
This division is due to the peculiarities of how computers are built — we talked about this in the lesson about floating-point numbers.
Example:
<?php
print_r(10.234); // => 10.234
print_r(-0.4); // => -0.4They can hold fractional values and be used in calculations:
<?php
print_r(3.5 + 1.2); // => 4.7
print_r(5.0 / 2.0); // => 2.5
print_r(2.75 - 0.5); // => 2.25Primitive types
Types such as:
- string,
- int (integer),
- float (rational number)
are called primitive: they are built right into the PHP language itself.
PHP primitive types
├── int : integers (7, -3, 0)
├── float : fractional numbers (3.14, -0.5)
├── string : strings ('hello')
└── bool : boolean type (true, false)Besides strings and numbers, PHP has the boolean type bool with the values true and false, as well as the special value null. We'll encounter them in more detail in the future.
There are also composite types: arrays, objects, and others. We'll get to know them later. Moreover, in PHP you can create your own types (for example, classes), but first it's important to get a good grasp of the primitives.
Strings and lines
In English, strings in programming are called strings, while lines of text files are called lines. In Russian there can be confusion, so throughout the lessons we'll say "строка" for the "string" data type and "строчка" for lines in files.
Instructions
The program already defines the current year:
<?php
$currentYear = 2026;Write a program that prints profile data in the following format:
Name: Anna
Birth year: 1994
Age: 32
Rating: 4.7To do this:
- create a variable with the birth year;
- calculate the age based on the current year;
- print the data on the screen with labels on the left.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
<?php
namespace HexletBasics\DataTypes\PrimitiveDataTypes;
use HexletBasics\Exercise\TestCase;
class SolutionTest extends TestCase
{
public function test()
{
$expected = <<<HERE
Name: Anna
Birth year: 1994
Age: 32
Rating: 4.7
HERE;
$this->assertOutput($expected);
}
}Teacher's solution will be available in:
20:00
