Untitled Document
Untitled Document
PHP is a dynamically-typed language, which means you don't have to declare a variable's
data type explicitly. The type is determined by the value assigned to it. PHP supports the
following primary data types:
1. Scalar Types:
Example Code:
<?php
// Scalar Types
$integer = 10; // Integer
$float = 3.14; // Float
$string = "Hello, PHP!"; // String
$bool = true; // Boolean
// Compound Types
$array = array(1, 2, 3); // Indexed Array
$assocArray = array("name" => "Alice", "age" => 25); // Associative Array
class Person {
public $name;
public $age;
}
$object = new Person(); // Object
$object->name = "Bob";
$object->age = 30;
// Special Types
$nullVar = null; // NULL
● Dynamic Typing: PHP automatically converts variable types based on the assigned
value.
● Type Juggling: PHP can change the type of a variable during execution when
necessary.
● Usage: Understanding these data types is essential for effective PHP programming,
as they determine how data is stored, manipulated, and interacted with in your code.
This answer covers the main data types in PHP with definitions, examples, and key points,
making it a solid response for a 4-mark question.
1) Integer -
- Integers are data types which store only positive and
negative values without any decimal points.
- Syntax -
$var_name = value;
- For Example-
<?php
$x = 5;
$x = 5.5;
3) Strings-
- Strings datatypes are used to store any set of letters and
characters.
- Syntax -
$var_name = “value”;
- For example,
<?php
$x = "Gautam";
echo "X value is:", $x;
?>
- Here, x is a variable which holds the string value Gautam.
4) Boolean -
- Boolean data types are used for binary operations; it has
2 values either “true” or “false”.
- Syntax -
$var_name = true/false;
- For example,
<?php
$x = true;
echo "X value is:", $x;
?>
Here, x is a variable which holds the boolean value true.
1) For Loop -
- It is a type of loop which is used when the user knows
how many iterations are needed.
- The for loop comes with initialization, condition,
increment or decrement.
- Syntax -
for(initialization; condition; increment/decrement) {
// statement
}
- For Example,
<?php
for($i=0; $i<5; $i++) {
echo $i . "<br>";
}
?>
- Here, the for loop will run 5 times from 0 till 4 and print
the values b/w 0 to 4.
2) While loop-
- It is a type of loop which is used till the condition of the
loop is true. This is helpful when the user wants to run
infinite loops and wants to stop at a particular condition.
- The while loop comes only with condition in its structure.
- Syntax-
while(condition) {
// statements
}
- For example,
<?php
$i=0;
while($i<5) {
echo $i . "<br>";
$i++;
}
?>
- Here the while loop will run 5 times from 0 to 4 and print
the values b/w 0 to 4
3) Do while-
- The Do..While loop is similar to a while loop but the only
difference is the condition is checked at the end.
- In this the first iteration will be done without any check.
- Syntax -
do {
//statements
} while(condition);
- For example,
<?php
$i=0;
do {
echo $i . "<br>";
$i++;
} while($i<5);
?>
- Here, the do…while loop will run 5 times from 0 to 4 and
print the values b/w 0 to 4.
4) For each loop-
- The for each loop is similar to for loop but the only
difference is that it is used in iterables like arrays or
objects.
- Syntax-
foreach($iterable as $var_name) {
// statements
}
- For example,
<?php
$arr = [1, 2, 3];
foreach($arr as $x) {
echo $x . "<br>";
}
?>
Class -
- A class is a blueprint or template which is used to create
objects. It defines properties(variables) and methods(function)
that describe the behaviour of the object.
- A class doesn’t hold any actual data but defines a structure to
create multiple instances(objects).
- A class is a fundamental concept of OOP.
Object -
- Objects are instances of class. Which contains the actual data
and can perform any sort of action using properties of the
class.
- Each object has its own property, but every object share
similar structure.