PHP Type Conversions - Code

PHP Strings Previous Next

Data type of a variable is defined as the set of values assigned to a variable and the allowable operations on those values.

Almost all programming languages explicitly include the notion of data type, though the names of these data types differ,

Some common data types used programming languages include those that represent integers, floating point numbers, characters, and dates etc.

A boolean variable is declared as given below.

boolean $variable;

When a variable is declared as follows

$variable = true;

Here the variable becomes a boolean value. Basic data type included in php are:

Boolean: It can hold values either "true" (1) or "false" (0)

Integer: The integer data type is used to specify a numeric value without a fractional component. it will hold values from -2,147483,648 to 2,147,483,647.

Double: The Double data types is used to specify floating point numbers. It will hold values from 1.7E-308 to 1.7E+308.

String: A String is a sequence of characters. A string can be delimited by single quotes or double quotes.

Array:Used to store multiple data values

Object: PHP also support composite data types, such as arrays and objects. Composite data types represent a collection of data, rather than a single value. An object is a compound data type that can contain any numbers of variables and functions.

NULL: It represent a special value called NULL which means that a variable has no value.

Filename: php / type-conversions.php

$net_sal =12000.45; //double
echo gettype(net_sal));
settype($net_sal, "integer"); // set to integer
echo "After type conversion net_sal is"." ".gettype(net_sal);

// Type casting of variables is done to temporarily change a variable 's data type.
$sal = 12000.2; // double
$name = "Sachin"; // string
$grossal = (string) $sal; 
echo gettype($grossal." ".value is ".$grossal);

//Output String value is 12000.2

Running Step.

Type Url - https://localhost/php/type-conversions.php

Tags: PHP Type Conversions PHP Code Programming Language

Was this helpful ?

Related Code