What is PHP Data Type and Variable’s Data Type | PHP Gurukul

What is PHP Data Type?

All data stored in PHP variables fall into one of eight basic categories, known as data types. A variable’s data type determines what operations can be carried out on the variable’s data, as well as the amount of memory needed to hold the data.

Types of Data Types in PHP
  • Booleans
  • Integers
  • Doubles/Float
  • Strings
  • NULL
  • Arrays
  • Objects
  • Resources
Booleans

This data type can only hold two types of values, which include TRUE and FALSE. There are situations that demand expressions to be evaluated as boolean giving the result of TRUE and FALSE. While performing such evaluations, the following rules must be kept in mind-

  • If the expression evaluates to a number, which is zero, then the value of the expression is FALSE. Any other number is considered non-zero and is evaluated as TRUE.
  • If the expression evaluates to a string, then the Boolean value of the expression is TRUE if it evaluates to a valid string. On the other hand, if the string is empty, then the expression is evaluated to FALSE.
  • Any expression that evaluates to the value NULL has an equivalent Boolean value of FALSE.
  • If an expression evaluates to an array, then the Boolean value of the expression is TRUE if the array has elements. On the other hand, if the value of the expression is an array with no elements, its Boolean value is FALSE. Considering the case of objects, if any of the member variables of the objects have a value assigned to it, then the object evaluates to the Boolean value TRUE, else it is evaluated as FALSE.
  • Any resource that is valid for the PHP system is evaluated as TRUE.
  • The use of doubles as Booleans must be avoided.
Integers

All the whole numbers are included in Integers. For example, any number like 6758 or 54 is an integer. The syntax or PHP expression for assigning a value to an integer is given below-

It is important to mention that the largest integer that can be held by a system is 2,147,483,647.

Click : https://phpgurukul.com/php-data-types/

Rules for Integer:

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

All floating-point number that is actually just whole numbers followed by a decimal point and fractional part fall under this data type.

Output:
float(1234)
float(1.234)

Strings

Any combination of characters is referred to as a string. All string operations are supported in PHP.

A String can hold numbers, letters, and special characters and it can be as large a up to 2 GB.

Output:
Welcome to PHPGurukul
Welcome to PHPGurukul

NULL

This data type can store only one value and is usually used for initializing the variable. The one value that is stored is NULL. Although conventionally capital letters are used for writing NULL, it can also be written as ‘null’ and the system will recognize it as the same. Therefore, the following statement is valid-

There are two important things to note about NULL. First, NULL always evaluates to FALSE when a Boolean equivalent is to be determined.

Secondly, if NULL is tested against IsSet() function, it always evaluates to FALSE.

Arrays

A collection of data of the same type can also be called an array. In fact, arrays can also be visualized as a number of variables of the same type, which are referred by the same name and are distinguished by the index at which they are present.

Output:
array(3) {[0]=>string(4)”Blue”[1]=>string(3)”Red”[2]=>string(5)”Blue”}

Objects

Programmers can define classes or templates, which when instantiated create objects. A class is typically composed of data values and functions. An object of that class can access all the functions of the class and possesses a set of data members of itself, in accordance with the data members defined for the class.

Output:
object(welcome)#1(1){[“str”]=>string(21)”Welcome to PHPGurukul”}

Resources

There may be some programming scenarios that require you to refer to resources that are not available within PHP. An example of such a programming scenario is a database connection. In order to hold such references, special is required, which are called resources.


PHP Gurukul

Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.

Website : https://phpgurukul.com

Publicado en Default Category en noviembre 06 at 12:04
Comentarios (0)
No login
Inicie sesión o regístrese para enviar su comentario
Cookies on De Gente Vakana.
This site uses cookies to store your information on your computer.