Data types

PHP is a dynamically typed language. This means that the variable’s data type is inferred at runtime, and unlike some other programming languages, PHP doesn’t need to precede the variable with the data type.

PHP has ten basic data types:

  • bool (boolean type)
  • int (integers)
  • float (fractional numbers)
  • string (strings)
  • array (arrays)
  • object (objects)
  • callable (functions)
  • mixed (any type)
  • resource (resources)
  • null (no value)

Of these data types, the first four are scalar: bool, int, float, and string. So let’s take a look at them first.

int (integer type)

Represents a signed integer.

$num = -100;
echo $num;

Here, the variable $numrepresents an integer type because it is assigned an integer value.

In addition to decimal integers, PHP also has the ability to use binary, octal, and hexadecimal numbers. Number templates for other systems:

hex : 0[xX][0-9a-fA-F]

octal : 0[0-7]

binary : 0b[01]

For example:

 
<?php
// All decimal numbers are 28
$num_10 = 28; // decimal number
$num_2 = 0b11100; // binary number (28 decimal)
$num_8 = 034; // octal number (28 in decimal)
$num_16 = 0x1C; // hexadecimal number (28 in decimal)
echo "num_10 = $num_10 <br>";
echo "num_2 = $num_2 <br>";
echo "num_8 = $num_8 <br>";
echo "num_16 = $num_16";
?>

A type variable int occupies 32 bits in memory, that is, it can take values ​​from -2 147 483 648 to 2 147 483 647. If the variable receives a numeric value outside this range, then it is treated as a type variable float

float type (floating point numbers)

The size of a floating point number depends on the platform. The maximum possible value is typically 1.8* 10308 with a precision of about 14 decimal digits. For example:

<?php
$a1 = 1.5; 
$a2 = 1.3e4; // 1.3 * 10^4 
$a3 = 6E-8; // 0.00000006
?>

type bool (boolean type)

Boolean variables can take two values: true and false or, in other words true, and false. Most often, boolean values ​​are used in conditional constructs:

<?php
$foo = true;
$boo = false;
?>

type string (strings)

Lines can be used to work with text. Strings come in two types: double-quoted and single-quoted. The type of quotes determines how strings are handled by the interpreter. Thus, variables in double quotes are replaced with values, while variables in single quotes remain unchanged.

<?php
$a=10;
$b=5;
$result = "$a+$b <br>";
echo $result;
$result = '$a+$b';
echo $result;
?>

In this case, we will get the following output:

10+5
$a+$b

In addition to ordinary characters, the string may contain special characters that may be misinterpreted. For example, we need to add a quote to a string:

$text = "Model "Apple II"";

This entry will be wrong. To fix the error, we can combine different types of quotes (‘Model ‘Apple II” or ‘Model ‘Apple III’) or use a slash to enter a quote in a string:

$text = "Model \"Apple II\"";

The special value null

The value null indicates the variable has no value. Using this value is useful when we want to indicate that a variable has no value. For example, if we simply define a variable without initializing it, and then try to use it, then the interpreter will give us a message that the variable is not set:

<?php
$a;
echo $a;
?>

Using null will help avoid this situation. In addition, we will be able to check for the presence of a value and, depending on the results of the check, perform certain actions:

<?php
$a = null;
echo "a = $a";
?>

The null constant is not case-sensitive, so we can write it like this:

$a = NULL;

Dynamic typing

Since PHP is a dynamically typed language, we can assign values ​​of different types to the same variable:

<?php
$id = 123;
echo "<p>id = $id</p>";
$id = "asdfasdf";
echo "<p>id = $id</p>";
?>