32,000+ SUBSCRIBERS CAN'T GO WRONG !!!

phpans
GET TOP ARTICLES, SNIPPETS, SCRIPTS IN YOUR INBOX FOR FREE!

Delivered by GET EMAIL UPDATES FeedBurner
We always respect your privacy and take protecting it seriously.
We won't Spam You

PHP Data Type

, 0,
PHP Data Type
Every language has the own data type. We should know the data type of any variable example is $a= 100; that means the value of $a is 100 and it is a Integer non-decimal number it could also a decimal number like 100.25 or it could also a text like $a = "PHPAns"; . That means we should identify the data type of a variable. When we set a number like 100 is a value of $a then it means it's an integer, when we set $a = "PHPAns"; that means $a is a string and when we set value like $a = 100.25; that means the value of a is decimal and $x is floating number etc.

PHP has some data types that means PHP variable can take that types data and participate any type of operation. Mainly PHP have two data type php scalar data types and php compound data types. Let's know about these data types.

Scalar data also divide into 4 types.
1. boolean: This data type has two type of value TRUE or FALSE, PHP have many functions these function returns boolean data types. Normally use in conditional statement like if is_bool() is a function to check variable data boolean. example-
<?php
$a = TRUE;
$b = 100;
var_dump(is_bool($a)); // output bool(true)
var_dump(is_bool($b)); // output bool(false)
?>

PHP checks these variables data type and execute results. First it check variable $a value using is_bool() function it returns true cause TRUE is boolean data type and when check $b variable then it returns false cause value of $b is a Integer not boolean. You can check any variable's value using this.

2. integer: Intger is full number means without desimal thats like 2,8,10,44,-23 etc are integer data. You can check integer data using is_int() function , you can also check any variable using is_int() function. If varible value is full number then this function will return TRUE otherwise this function will return FALSE . Full number can be positive or negative number even decimal- 10 based, hexadecimal, octal or binary can be Integer.
Here is some example-
<?php
$x = 1011100; // binary number 92
var_dump($x);
?>

output int(1011100)

3. float: Float is decimal number like 10.2646 , 2.456 etc are float number or, doubles or, real number.
example code-
<?php
$x = 10.2646;
var_dump($x);
?>

output float(10.2646)

4. string: If you use one or more then one character into single quotation or double quotation syntax then it will be called string.
example-
<?php
$a = 'my text';
$a = "my text";
$a = <<<HTML
<p>my text</p>
HTML;
?>


Compound data type have array and object data type. And it has also two more data type resource and NULL.
resource mainly some type of data system like database return is a resource data. And NULL is empty data system, If you declere any variable but not set the value of variable then this variavle will return NULL value.

Type Casting
You can change data type on PHP. The system of convert a data into another data is called type casting. Yous should declare tag into first bracket before data. Tags are,
(int) for Full Number
(bool) for Boolean
(float) for Desimal, Double, Real Number
(string) for Character
(array) For Create Array
(object) For Convert into object
(unset) For NULL a value

Here is an example that $a = 100; in this valriable $a is Integer that means full number and I want to convert it into string
<?php
$a = 100;
$a = (string) 100;
var_dump($a);
?>

Output string(1) "100"
if you remove var_dump from this code then this script will execute as int(100).

Download PDF
Signup & Get $25 Free
Is this article helpful for you?





captcha

  • Connect
  •       
Top
Create an free account and start learning & sharing! Sign Up