Type casting in php javatpoint

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

PHP Data Types: Scalar Types

It holds only single value. There are 4 scalar data types in PHP.

  1. boolean
  2. integer
  3. float
  4. string

PHP Data Types: Compound Types

It can hold multiple values. There are 2 compound data types in PHP.

  1. array
  2. object

PHP Data Types: Special Types

There are 2 special data types in PHP.

  1. resource
  2. NULL

PHP Boolean

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

Example:

Output:

PHP Integer

Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.

Rules for integer:

  • An integer can be either positive or negative.
  • An integer must not contain decimal point.
  • Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).
  • The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.

Example:

Output:

Decimal number: 34
Octal number: 163
HexaDecimal number: 69

PHP Float

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

Example:

Output:

Addition of floating numbers: 73.812

PHP String

A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.

String values must be enclosed either within single quotes or in double quotes. But both are treated differently. To clarify this, see the example below:

Example:

Output:

Hello Javatpoint
Hello $company

PHP Array

An array is a compound data type. It can store multiple values of same data type in a single variable.

Example:

Output:

array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" }
Array Element1: Royal Enfield
Array Element2: Yamaha
Array Element3: KTM

You will learn more about array in later chapters of this tutorial.

PHP object

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

Example:

Output:

Bike Model: Royal Enfield

This is an advanced topic of PHP, which we will discuss later in detail.

PHP Resource

Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource.

This is an advanced topic of PHP, so we will discuss it later in detail with examples.

PHP Null

Null is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as it is case sensitive.

The special type of data type NULL defined a variable with no value.

Example:

Output:


Summary: in this tutorial, you’ll learn about the PHP type casting that allows you to convert a value of a type to another.

Introduction to the PHP type casting

Type casting allows you to convert a value of one type to another. To cast a value, you use the following type casting operators:

Cast OperatorsConversion
(array) Array
(bool) or (boolean) Boolean
(int) or (integer) Integer
(object) Object
(real), (double), or (float) Float
(string) String

Let’s take some examples of using the type casting operators.

Cast to an integer

To cast a value to an integer, you use the (int) type casting operator.

The (int) operator casts a float to an integer. It’ll round the result towards zero. For example:

echo (int)12.5 . '
'
; // 12 echo (int)12.1 . '
'
; // 12 echo (int)12.9 . '
'
; // 12 echo (int)-12.9 . '
'
; // -12

Code language: PHP (php)

Suppose you have a string and want to cast it as an integer:

$message = 'Hi'; $num = (int) $message; echo $num; // 0

Code language: PHP (php)

The result may not be what you expected.

If a string is numeric or leading numeric, then the (int) will cast it to the corresponding integer value. Otherwise, the (int) cast the string to zero. For example:

$amount = (int)'100 USD'; echo $amount; // 100

Code language: PHP (php)

In this example, the (int) operator casts the string '100 USD' as an integer.

Note that the (int) operator casts null to zero (0). For example:

$qty = null; echo (int)$qty; // 0

Code language: PHP (php)

Cast to a float

To cast a value to a float, you use the (float) operator. For example:

$amount = (float)100; echo $amount; // 100

Code language: PHP (php)

Cast to a string

To cast a value to a string, you use the (string) operator.

The following example uses the (string) operator to cast the number 100 to a string:

$amount = 100; echo (string)$amount . " USD"; // 100 USD

Code language: PHP (php)

You don’t need to use the (string) operator in this case because PHP has a feature called type juggling that implicitly converts the integer to a string:

$amount = 100; echo $amount . ' USD'; // 100 USD

Code language: PHP (php)

The (string) operator converts the true value to the string "1" and false value to the empty string (“”). For example:

$is_user_logged_in = true; echo (string)$is_user_logged_in; // 1

Code language: PHP (php)

Output:

1

Code language: PHP (php)

The (string) operator casts null to an empty string.

The (string) cast an array to the "Array" string. For example:

$numbers = [1,2,3]; $str = (string) $numbers; echo $str; // Array

Code language: PHP (php)

And you’ll get a warning that you’re attempting to convert an array to a string.

Warning: Array to string conversion in ...

Code language: PHP (php)

Summary

  • PHP type casting allows you to convert a value from one type to another.
  • Use a type casting operator to cast a value to the desired type.

Did you find this tutorial useful?

What is the type casting in PHP?

PHP type casting allows you to convert a value from one type to another. Use a type casting operator to cast a value to the desired type.

What is type casting explain with example?

Typecasting, or type conversion, is a method of changing an entity from one data type to another. It is used in computer programming to ensure variables are correctly processed by a function. An example of typecasting is converting an integer to a string.

What is type casting in PHP w3schools?

Type casting is when you assign a value of one data type to another type.

What is type casting in OOP?

Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size. byte -> short -> char -> int -> long -> float -> double.