0% found this document useful (0 votes)
3 views

php data types

The document provides definitions and examples of PHP data types, including Integers, Floats, Strings, Booleans, Arrays, Objects, NULL, Resources, Callable, and Iterable. Each data type is explained with a brief definition and a corresponding PHP code example. This serves as a foundational guide for understanding PHP data types.

Uploaded by

dhanushbro50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

php data types

The document provides definitions and examples of PHP data types, including Integers, Floats, Strings, Booleans, Arrays, Objects, NULL, Resources, Callable, and Iterable. Each data type is explained with a brief definition and a corresponding PHP code example. This serves as a foundational guide for understanding PHP data types.

Uploaded by

dhanushbro50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

php data type definsion and example

Sure, here are the definitions and examples of PHP data types:
1. Integers:
Definition: Integers are whole numbers without any decimal points. They can be posi
tive or negative.
Example:

Php Copy

$age = 25;
echo $age; // Output: 25

2. Floats (Doubles):
Definition: Floats are numbers with a decimal point.
Example:

Php Copy

$price = 9.99;
echo $price; // Output: 9.99

3. Strings:
Definition: Strings are sequences of characters enclosed in single or double quotes.
Example:

Php Copy

$greeting = "Hello, world!";


echo $greeting; // Output: Hello, world!

4. Booleans:
Definition: Booleans represent two possible states: TRUE or FALSE .
Example:

Php Copy

$is_admin = true;
if ($is_admin) {
echo "User is an admin.";
} else {
echo "User is not an admin.";
}
// Output: User is an admin.

5. Arrays:
Definition: Arrays are collections of values indexed by keys. They can be indexed or a
ssociative.
Examples:
Indexed Array:

Php Copy

$fruits = array("Apple", "Banana", "Cherry");


echo $fruits[1]; // Output: Banana

Associative Array:

Php Copy

$person = array("name" => "John", "age" => 30, "city" => "New York");
echo $person["name"]; // Output: John

6. Objects:
Definition: Objects are instances of classes that can contain properties and methods.
Example:

Php Copy

class Car {
public $make;
public $model;

public function describe() {


return "This car is a " . $this->make . " " . $this->model . ".";
}
}

$myCar = new Car();


$myCar->make = "Toyota";
$myCar->model = "Corolla";
echo $myCar->describe(); // Output: This car is a Toyota Corolla.

7. NULL:
Definition: NULL represents a variable with no value.
Example:

Php Copy

$var = NULL;
if (is_null($var)) {
echo "The variable is NULL.";
}
// Output: The variable is NULL.

8. Resources:
Definition: Resources are special variables holding references to external resources li
ke database connections or file handles.
Example:

Php Copy

$file = fopen("example.txt", "r");


if ($file) {
echo "File opened successfully.";
fclose($file);
}
// Output: File opened successfully.

9. Callable:
Definition: Callable represents a value that can be called as a function, such as a func
tion name, class method, or anonymous function.
Example:

Php Copy

function greet() {
return "Hello!";
}

$callback = "greet";
echo $callback(); // Output: Hello!

10. Iterable:
Definition: Iterable represents any value that can be looped through with a foreach
loop.
Example:

Php Copy
function getIterable() {
return array(1, 2, 3);
}

foreach (getIterable() as $value) {


echo $value . " ";
}
// Output: 1 2 3

These definitions and examples should give you a solid understanding of PHP data types. If y
ou have any further questions or need more examples, feel free to ask!

You might also like