How many data types are there in PHP ?

  • PHP data types are used to hold different types of values or data.
  • There are eight primitive data types which are further categorized into three types, they are:
    • Scalar types
    • Compound types
    • Special types

Scalar data types

  • Scalar data types hold only single value and it consists of four data types, they are boolean, integer, float and string.

Boolean

  • Booleans are the simplest data type works like switch and holds only two values TRUE (1) or FALSE (0).
  • It is often used with conditional statements were if the condition is correct, it returns TRUE otherwise FALSE.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%20%0A%20%20%20%20if%20(TRUE)%20%20%0A%20%20%20%20%20%20%20%20echo%20%22This%20condition%20is%20TRUE.%22%3B%20%20%0A%20%20%20%20if%20(FALSE)%20%20%0A%20%20%20%20%20%20%20%20echo%20%22This%20condition%20is%20FALSE.%22%3B%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Integer

  • Integer means numeric data, it can be either positive or negative sign.
  • It holds only whole numbers, like numbers without decimal points or fractional parts.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%20%0A%20%20%20%20%24dec1%20%3D%2034%3B%20%20%0A%20%20%20%20%24oct1%20%3D%200243%3B%20%20%0A%20%20%20%20%24hexa1%20%3D%200×45%3B%20%20%0A%20%20%20%20echo%20%22Decimal%20number%3A%20%22%20.%24dec1.%20%22%3C%2Fbr%3E%22%3B%20%20%0A%20%20%20%20echo%20%22Octal%20number%3A%20%22%20.%24oct1.%20%22%3C%2Fbr%3E%22%3B%20%20%0A%20%20%20%20echo%20%22HexaDecimal%20number%3A%20%22%20.%24hexa1.%20%22%3C%2Fbr%3E%22%3B%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Float

  • A floating-point number is a number with a decimal point and unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%20%0A%20%20%20%20%24n1%20%3D%2019.34%3B%20%20%0A%20%20%20%20%24n2%20%3D%2054.472%3B%20%20%0A%20%20%20%20%24sum%20%3D%20%24n1%20%2B%20%24n2%3B%20%20%0A%20%20%20%20echo%20%22Addition%20of%20floating%20numbers%3A%20%22%20.%24sum%3B%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

String

  • String is a non-numeric data type which holds letters or any alphabets, numbers, and even special characters.
  • Its value must be enclosed either within single quotes or in double quotes and both are treated differently.

Sample  Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%20%0A%20%20%20%20%24company%20%3D%20%22Wikitechy%22%3B%20%20%0A%20%20%20%20%2F%2Fboth%20single%20and%20double%20quote%20statements%20will%20treat%20different%20%20%0A%20%20%20%20echo%20%22Welcome%20to%20%24company%22%3B%20%20%0A%20%20%20%20echo%20%22%3C%2Fbr%3E%22%3B%20%20%0A%20%20%20%20echo%20%22Welcome%20to%20%24company’%3B%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Compound data type

  • Compound data type can hold multiple and it consists of two data types, they are array and object.

Array

  • In single variable an array canstore multiple values of same data type.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%C2%A0%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%24bikes%C2%A0%3D%C2%A0array%C2%A0(%22Royal%C2%A0Enfield%22%2C%C2%A0%22Yamaha%22%2C%C2%A0%22KTM%22)%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0var_dump(%24bikes)%3B%C2%A0%C2%A0%C2%A0%2F%2Fthe%C2%A0var_dump()%C2%A0function%C2%A0returns%C2%A0the%C2%A0datatype%C2%A0and%C2%A0values%C2%A0%0A%0A%C2%A0%C2%A0%20%C2%A0echo%C2%A0%22%3C%2Fbr%3E%22%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0echo%C2%A0%22Array%C2%A0Element1%3A%C2%A0%24bikes%5B0%5D%C2%A0%3C%2Fbr%3E%22%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0echo%C2%A0%22Array%C2%A0Element2%3A%C2%A0%24bikes%5B1%5D%C2%A0%3C%2Fbr%3E%22%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0echo%C2%A0%22Array%C2%A0Element3%3A%C2%A0%24bikes%5B2%5D%C2%A0%3C%2Fbr%3E%22%3B%C2%A0%C2%A0%0A%0A%3F%3E%C2%A0%C2%A0″ message=”” highlight=”” provider=”manual”/]

Output

Object

  • Objects are the instances of user-defined classes that can store both functions and values.
  • It must be explicitly declared.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%20%0A%20%20%20%20%20class%20bike%20%7B%20%20%0A%20%20%20%20%20%20%20%20%20%20function%20model()%20%7B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24model_name%20%3D%20%22Jawa%22%3B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20echo%20%22Bike%20Model%3A%20%22%20.%24model_name%3B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%0A%20%20%20%20%20%7D%20%20%0A%20%20%20%20%20%24obj%20%3D%20new%20bike()%3B%20%20%0A%20%20%20%20%20%24obj%20-%3E%20model()%3B%20%20%0A%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Special Data Type

  • Special data types consist of two types, they are Null and resource data type.

Null

  • Null is a special data type that has only one value is NULL and there is a convention of writing it in capital letters as it is case sensitive.
  • In special data type NULL defined a variable with no value.

Sample Code

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%20%0A%20%20%20%20%20%24nl%20%3D%20NULL%3B%20%20%0A%20%20%20%20echo%20%24nl%3B%20%20%20%2F%2Fit%20will%20not%20give%20any%20output%20%20%0A%20%20%20%20%3F%3E%20%20%0A” message=”” highlight=”” provider=”manual”/]

Output

Resource:

  • In PHP resources are not the exact data type and basically these are used to references to external PHP resources or store some small function calls.

Leave a Reply

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

You May Also Like