Problem: State and briefly describe the PHP data type. (2010)
OR
How array is declared in PHP? Also explain various types of array with proper example.(2017)
Solve:
PHP supports the following data types:Simple data types:
- String
- Integer
- Float / double
- Boolean
- Array
Compound data types:
- Object
- NULL
- Resource
PHP Integers:
Integers are whole numbers, without a decimal point. Integer can be specified in decimal, hexadecimal, octal etc.
Example:
<?php
echo "PHP Data Type";
echo "<br>"; // for line break
//decimal number
$a = 50;
$b = 10;
$sum = $a+$b;
echo " Output=".$sum;
echo "<br>"; // for line break
echo "<br>";
echo "<br>";
// Negative number
$a = -220;
$b = -20;
$sum = $a+$b;
$sum = $a+$b;
echo " Output=".$sum;
echo "<br>"; // for line break
echo "<br>"; // for line break
echo "<br>";
// Hexadecimal number
$a = 0x2b;
// Hexadecimal number
$a = 0x2b;
echo $a;
echo "<br>"; // for line break
echo "<br>"; // for line break
echo "<br>";
// Octal number
$a = 0321;// Octal number
echo $a;
?>
Screenshots of Source code:
Output:
PHP Data Type
Output=60
Output=-240
43
209
Screenshots of Output:
Example:
<?php
$a = 22.8; //Floating numbers
echo "Float Number a = ".$a;
echo "<br>"; // for line break
$b = 8.3;
echo "Float Number b = ".$b ;
$sum = $a+$b;
echo " Output Float =".$sum;
?>
Screenshots of Source code:
Output:
PHP Floating or Doubles number
Float Number a = 22.8
Float Number b = 8.3
Output Float = 31.1
Screenshots of Output:
<?php
echo "PHP String";
echo "<br>"; // for line break
echo "<br>";
$name = "Tarikul Islam Tufan!"; // String data type
echo "The name of the programmer is".",". $name;
echo "<br>";
// or
$a = 'CSE';
echo "String=".$a;
?>
Screenshots of Source code:
Output:
PHP String
The name of the programmer is
String=CSE
Screenshots of Output:
Example:
<?php
echo "PHP Boolean";
echo "<br>"; // for line break
echo "<br>";
//Assign the value TRUE to a variable
$show_error = TRUE;
echo $show_error;
echo "<br>"; // for line break
// or
if(TRUE)
echo "This condition is True";
if(FALSE)
echo "This condition is not True";
?>
Screenshots of Source code:
Output:
PHP Boolean
1
This condition is True
Screenshots of Output:
There are three types of arrays that you can create. These are:
<?php
echo "PHP Array";
echo "<br>"; // for line break
echo "<br>";
$name = array("Tufan","Tarukul","Basu");
print_r($name); //Printing array structure
echo "<br>"; // for line break
echo "<br>";
//or
echo "$name[0]";
echo "$name[1]";
echo "$name[2]";
?>
Screenshots of Source code:
Output:
PHP Array
Array ( [0] => Tufan => Tarukul [2] => Basu)
TufanTarukulBasu
Screenshots of Output:
Associative Array: An array where each key has its own specific value.In an associative array, the keys assigned to values can be arbitrary and user defined strings. In an associative array uses keys instead of index numbers:
Example:
<?php
echo "PHP Associative Array";
echo "<br>"; // for line break
echo "<br>";
$name = array("Tufan"=>22,"Tarukul"=>25,"Basu"=>30);
print_r($name); //Printing array structure
echo "<br>"; // for line break
echo "<br>";
//or
echo $name['Tufan'];
echo "<br>";
echo $name['Tarukul'];
echo "<br>";
echo $name['Basu'];
?>
Screenshots of Source code:
Output:
PHP Associative Array
Array ( [Tufan] => 22 [Tarukul] => 25 [Basu] => 30)
22
25
30
Screenshots of Output:
Multidimensional array: An array containing one or more arrays within itself.
Example:
<?php
echo "PHP Multidimensional Array";
echo "<br>"; // for line break
echo "<br>";
//Define array
$contacts = array(
array(
"name" => "Tarikul islam tufan",
"email"=> "tufan@gmail.com",
"phone"=> "01765779616",
),
array(
"name" => "Tarikul islam tufan",
"email"=> "tufan@gmail.com",
"phone"=> "01956353863",
),
array(
"name" => "Tarikul islam tufan",
"email"=> "tarikul@gmail.com",
"phone"=> "01956353863",
),
);
// Access values
echo "Tarikul islam tufan`s name is:".$contacts[0]["name"];
echo "<br>";
echo "Tarikul islam tufan`s email is:".$contacts[1]["email"];
echo "<br>";
echo "Tarikul islam tufan`s phone is:".$contacts[2]["phone"];
?>
Screenshots of Source code:
Output:
PHP Multidimensional Array
Tarikul islam tufan`s name is:Tarikul islam tufan
Tarikul islam tufan`s email is:tufan@gmail.com
Tarikul islam tufan`s phone is:01956353863
Screenshots of Output:
In an object array, first we must declare a class of object.For this, we use the class keyword.
Example:
<?php
echo "PHP Object Data";
echo "<br>"; // for line break
echo "<br>";
class car {
function car(){
$this -> model = "BMW";
Screenshots of Source code:
Output:
PHP Data Type
Output=60
Output=-240
43
209
Screenshots of Output:
PHP Floating Numbers or Doubles:
Floating point numbers are decimal or fractional numbers.Example:
<?php
echo "PHP Floating or Doubles number";
echo "<br>"; // for line break
echo "<br>"; $a = 22.8; //Floating numbers
echo "Float Number a = ".$a;
echo "<br>"; // for line break
$b = 8.3;
echo "Float Number b = ".$b ;
$sum = $a+$b;
echo " Output Float =".$sum;
?>
Screenshots of Source code:
Output:
PHP Floating or Doubles number
Float Number a = 22.8
Float Number b = 8.3
Output Float = 31.1
Screenshots of Output:
PHP Strings:
Strings are sequence of characters.<?php
echo "PHP String";
echo "<br>"; // for line break
echo "<br>";
$name = "Tarikul Islam Tufan!"; // String data type
echo "The name of the programmer is".",". $name;
echo "<br>";
// or
$a = 'CSE';
echo "String=".$a;
?>
Screenshots of Source code:
Output:
PHP String
The name of the programmer is
String=CSE
Screenshots of Output:
PHP Boolean:
Booleans are like a switch it has only possible either 1 (true) or 0 (false).Example:
<?php
echo "PHP Boolean";
echo "<br>"; // for line break
echo "<br>";
//Assign the value TRUE to a variable
$show_error = TRUE;
echo $show_error;
echo "<br>"; // for line break
// or
if(TRUE)
echo "This condition is True";
if(FALSE)
echo "This condition is not True";
?>
Screenshots of Source code:
Output:
PHP Boolean
1
This condition is True
Screenshots of Output:
Also explain various types of array with proper example.(2017)
PHP Arrays:
An array stores multiple values in one single variable.There are three types of arrays that you can create. These are:
- Indexed array
- Associative array
- Multidimensional array
Indexed array: An array with a numeric key. In an indexed or numeric array, the indexes are automatically assigned and start with 0, and the values can be any data type.
Example:
echo "PHP Array";
echo "<br>"; // for line break
echo "<br>";
$name = array("Tufan","Tarukul","Basu");
print_r($name); //Printing array structure
echo "<br>"; // for line break
echo "<br>";
//or
echo "$name[0]";
echo "$name[1]";
echo "$name[2]";
?>
Screenshots of Source code:
Output:
PHP Array
Array ( [0] => Tufan => Tarukul [2] => Basu)
TufanTarukulBasu
Screenshots of Output:
Associative Array: An array where each key has its own specific value.In an associative array, the keys assigned to values can be arbitrary and user defined strings. In an associative array uses keys instead of index numbers:
Example:
<?php
echo "PHP Associative Array";
echo "<br>"; // for line break
echo "<br>";
$name = array("Tufan"=>22,"Tarukul"=>25,"Basu"=>30);
print_r($name); //Printing array structure
echo "<br>"; // for line break
echo "<br>";
//or
echo $name['Tufan'];
echo "<br>";
echo $name['Tarukul'];
echo "<br>";
echo $name['Basu'];
?>
Screenshots of Source code:
Output:
PHP Associative Array
Array ( [Tufan] => 22 [Tarukul] => 25 [Basu] => 30)
22
25
30
Screenshots of Output:
Multidimensional array: An array containing one or more arrays within itself.
Example:
<?php
echo "PHP Multidimensional Array";
echo "<br>"; // for line break
echo "<br>";
//Define array
$contacts = array(
array(
"name" => "Tarikul islam tufan",
"email"=> "tufan@gmail.com",
"phone"=> "01765779616",
),
array(
"name" => "Tarikul islam tufan",
"email"=> "tufan@gmail.com",
"phone"=> "01956353863",
),
array(
"name" => "Tarikul islam tufan",
"email"=> "tarikul@gmail.com",
"phone"=> "01956353863",
),
);
// Access values
echo "Tarikul islam tufan`s name is:".$contacts[0]["name"];
echo "<br>";
echo "Tarikul islam tufan`s email is:".$contacts[1]["email"];
echo "<br>";
echo "Tarikul islam tufan`s phone is:".$contacts[2]["phone"];
?>
Screenshots of Source code:
Output:
PHP Multidimensional Array
Tarikul islam tufan`s name is:Tarikul islam tufan
Tarikul islam tufan`s email is:tufan@gmail.com
Tarikul islam tufan`s phone is:01956353863
Screenshots of Output:
PHP Object:
An object is a data type which stores data information on how to process that data.In an object array, first we must declare a class of object.For this, we use the class keyword.
Example:
<?php
echo "PHP Object Data";
echo "<br>"; // for line break
echo "<br>";
class car {
function car(){
$this -> model = "BMW";
}
}
//create an object
$message = new car();
echo $message -> model;?>
Example:
<?php
echo "PHP NULL Data";
echo "<br>"; // for line break
echo "<br>";
echo "No output";
$a = "tufan";
$a = NULL ;
echo $a;
?>
Screenshots of Source code:
Output:
PHP NULL Data
No output
Screenshots of Output:
Example:
<?php
// Open a file for reading
$Book = fopen("note.txt","r");
var_dump($Book);
// Connect to MySQL Database server Required
?>
Screenshots of Source code:
Output:
PHP Object Data
BMW
Screenshots of Output:
PHP NULL Value:
Null is a special data type which can have only one value:- NULL. It is used to represent empty variables in PHP.Example:
<?php
echo "PHP NULL Data";
echo "<br>"; // for line break
echo "<br>";
echo "No output";
$a = "tufan";
$a = NULL ;
echo $a;
?>
Screenshots of Source code:
Output:
PHP NULL Data
No output
Screenshots of Output:
PHP Resource:
The special resource type is not an actual data type. These are basically used to store reference to some function call or to external PHP resources.Example:
<?php
// Open a file for reading
$Book = fopen("note.txt","r");
var_dump($Book);
// Connect to MySQL Database server Required
?>
💬আরো পড়ুন এবং জানুন💬
- Create a password field in HTML form
- Basic Forms | HTML Input
- PHP Calculating GPA
- Write the php code to calculate the display GPA in the web browser
- How to Insert Data Into MySQL Database Table & Retrieving data form database Using PHP
- How to Insert and retrieving data from MySQL database using Program
- Creating a new database & Table in mysql Databases
Post a Comment