Data Types in PHP 5

PHP Data types

PHP variables can be used to hold or store data of any kind.Data of different form can be used to perform differents tasks.

Supported PHP Data types  are

  • String
  • Integer
  • Float also refered to as Boolean
  • Array
  • Object
  • NULL
  • Resource

PHP String

String a group of characters eg”Programming in java”

A string in PHP can also refer to any text that has been eclosed inside two or one quote i.e double  “” or single ‘ quotes.

Example:


<?php
$text1= "I love Programming in PHP";
$text2 = 'I love Blogging';

echo $text1;
echo "<br>";
echo $text2;
?>

SEE ALSO: PHP echo and print

PHP Integer

Data type is integer if its non-decimal.Ranges from -2,147,483,648 and 2,147,483,647.

Facts about Integers

1.Any integer should have one digit or more

2.No decimal point should be contained in integer

3.Integer can be + or – i.e -4 and 4

4.There are three formats for integers,These are (10-based)=decimal

16-based=hexadecimal and 8 based prefix=octal

PHP function  var_dump() when used returns the data type and value held by the variable

Example

</strong>

&lt;?php
$num1 = 4583;
var_dump($num1);
?&gt;

Output will be int(4583)

PHP Float

Float is a PHP Data type with a decimal point.Its a number which is in form of exponential.

For example ,Below code returns they type of data type num1 is by using function var_dump()

&lt;?php
$num1= 34.375;
var_dump($num1);
?&gt;

PHP Boolean

This data type only represents two states i.e true or false
These data types are used only to do conditional testing.Returned  by most PHP if else statements and while

PHP Array

Arrays are used to hold more than one value.
Example below is $animal array with all kind of animals.Using PHP var_dump() will output array values and the data type.
For Example:

&lt;?php
$animals = array("lions","cow","goat");
var_dump($animals);
?&gt;

PHP Object

Objects are data types that have  information and data  for processing that data.
Object are declared by using the class keyword.A class has both properties and methods

&lt;?php
class Animal {
function Animal() {
$this-&gt;name = “All eat";
}
}

// create a new object
$dog = new Animal();

// output object properties
echo $dog-&gt;name;
?&gt;

PHP NULL

This is a type of data type that can only have one value and that is NULL.

All variables created  that have no value when declared are assigned value NULL and if Variable has been assigned,it can be emptied using NULL

Example

&lt;?php
$text1 = “I love Programming In PHP";
$text1 = null;
var_dump($x);
?&gt;

PHP Resource

Resource are not real data types.Reference to function are stored in resource.A database call is example of resource data type

See My Next Lesson

PHP Strings

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com