So now lets move on to some data type knowledge in PHP.
PHP basically supports 8 Data types namely being :
In PHP we can assign integers in 10 based(decimal), 16 based(hexadecimal), 8 based(octal) notation which can have + or - sign at start.
General syntax is :
<?php
eg:-
{
function do_foo()
{
echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo();
PHP basically supports 8 Data types namely being :
- Boolean
- Integer
- Float
- String
- Array
- Object
- Resource
- Null
The Array and Object are known as Compound Data Types and Resource and Null are known as Special Data Types whereas remaining 4 are primitive scalar data types.
We shall see each one of them one by one
Boolean
This is the easiest data type and needs no introduction. It can either have True or False value.
Special to note is that both True and False are Case Sensitive.
<?php
$foo=True;
echo $foo;
?>
would simply produce 1 as Output.
Integers
Integer can range from {....,-1,0,1,2,...}
In PHP we can assign integers in 10 based(decimal), 16 based(hexadecimal), 8 based(octal) notation which can have + or - sign at start.
<?php
$a1=12 ; //decimal number
$a2=-123; //negative number
$a3= 0123; //octal based number
$a4= 0x1A; //hexadecimal based number
echo $a1." ".$a2." ".$a3." ".$a4;
?>
This gives Output as under
Floating Point Numbers
They can be specified as below
<?php
$a1=1.23;
$a2=1.2e3;
$a3=6E-12;
?>
The size of float depends on the platform we use.(Google it for more Info :) )
Strings
As we all knows string is sequence of characters. PHP has no bound for strings.
There are 3 ways to represent Strings in PHP
1. Single Quotes : This is the most commonly used methodology. But here to specify a literal one will need it to be preceded by backslash (\).
Eg echo 'OK :\\*';
would give OK :\* as output .
2. Double Quotes : This is another most common used string displaying methodology here PHP understand more special characters like \n, \r etc.
>>>More Difference and information on Both Single and Double Quotes could be found here : Details
3. Heredoc : Its another way to delimit strings having syntax as <<< .
General syntax is :
<?php
<<< Identifier String Identifier;
?>
eg:-
<?php
echo <<<EOT
My name is ....
EOT;
?>
This would give My name is .... as output .
More Info on all Delimetering String Formats could be found here : Details
Arrays
An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. In PHP unlike all other languages we can have different data type values stored in arrays.
We would discuss about PHP arrays in detail later.
Objects
To initialize an object we use the new statement so that we can instantiate the object to variable.
Eg:
<?php
class foo{
function do_foo()
{
echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo();
?>
Resource
A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions
The following is a list of few functions which create, use or destroy PHP resources.
fbsql_db_query()-Selects a database and executes a query on it.
ftp_connect()-opens an FTP connection to the specified host .
imap_open()-Open an IMAP stream to a mailbox.
ftp_connect()-opens an FTP connection to the specified host .
imap_open()-Open an IMAP stream to a mailbox.
NULL
It simply represents that a variable has no value.
Eg:
<?php
$make=NULL;
echo $make;
?>
The output on browser would be nothing just blank.
No comments:
Post a Comment