Basic data types in PHP. Examples open

Basic data types in PHP. Examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
$string - "text";//string
$number - 123;//number
$number_float = 123.45;// floating point number
$bool = true;// true or false
$array = ['one', 'two']//Any data array

Type Object. In PHP, the object type is used to create objects that are instances of classes. Objects allow you to combine properties (variables) and methods (functions) associated with a certain entity or concept.

class Person {
    public $name;
    public $age;
} 

Type collable. In PHP, the callable record type is used to work with functions that can be called like normal functions. This type allows you to pass functions as arguments, store them in variables and call them when necessary.

$sum = function($a, $b) {
    return $a + $b;
};

$result = $sum(2, 3);
echo $result; // Return: 5 

In PHP, the mixed record type is used to indicate that a variable or function can accept values of various data types. This allows you to work with data flexibly, without explicitly specifying a specific type.

function processValue(mixed $value) {
    if (is_string($value)) {
        echo "String: " . $value;
    } elseif (is_int($value)) {
        echo "Number: " . $value;
    } elseif (is_bool($value)) {
        echo "Bolian: " . ($value ? 'true' : 'false');
    } else {
        echo "Неизвестный тип данных";
    }
}

processValue("Hello"); // Return: string: 'Hello'
processValue(42); // Return: Number: 42
processValue(true); // Return: Bolian: true
//...

Resource: in PHP, the resource type is used to represent external resources, such as files, databases, network connections, etc. Resources are special variables that contain a link to an external resource managed within PHP.

// Open the file in reading mode
$file = fopen("example.txt", "r");

// Read data from the file
$data = fread($file, filesize("example.txt"));

// Close the file
fclose($file);

NULL: in PHP, the NULL record type is used to represent variables that have no value or have not been initialized. It indicates the absence of any specific meaning.

$name = null;
if ($name === null) {
echo “No name”;
} else {
echo “Name: ” . $name;
}

In this example, the $name variable is initialized to NULL, which means that the name value is not specified. Then, with the help of a conditional operator, it is checked whether the value of the variable is NULL. If the value is NULL, the message “Name not specified” is displayed. Otherwise, if the value is not NULL, a message with the name is displayed.

The NULL type allows you to explicitly specify that the variable has no value. This can be useful, for example, when the variable has not yet been initialized, the value has been deleted, or it is necessary to specify the absence of a specific value.

How to see the type of php variable?

var_dump($varriable);//Let's check if Spike is in the $user array
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation