0% found this document useful (0 votes)
18 views6 pages

Data Types in PHP

The document outlines the various built-in data types in PHP, including String, Integer, Float, Boolean, Array, Object, NULL, Resource, Callable, and Iterable, each with examples. It also describes the different categories of operators in PHP, such as Arithmetic, Assignment, Comparison, Logical, String, Array, and Ternary operators, along with their functions. Understanding these data types and operators is crucial for effective PHP programming.

Uploaded by

Vicky Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Data Types in PHP

The document outlines the various built-in data types in PHP, including String, Integer, Float, Boolean, Array, Object, NULL, Resource, Callable, and Iterable, each with examples. It also describes the different categories of operators in PHP, such as Arithmetic, Assignment, Comparison, Logical, String, Array, and Ternary operators, along with their functions. Understanding these data types and operators is crucial for effective PHP programming.

Uploaded by

Vicky Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DATA TYPES IN PHP

In PHP, there are several built-in data types that you can use to store and manipulate
different kinds of information. Here are the commonly used data types in PHP:

1. String: A string is a sequence of characters. In PHP, you can define a string using single
quotes ('') or double quotes ("").

For example:
$name = "John Doe";

2. Integer: An integer represents whole numbers without a decimal point. In PHP, integers
can be positive or negative.

For example:
$age = 25;

3. Float: A float represents numbers with decimal points. Also known as floating-point
numbers, they can be used to represent both small and large fractional values.

For example:
$pi = 3.14;

4. Boolean: A boolean data type represents either true or false. It is often used in
conditional statements and logical operations.

For example:
$isStudent = true;

5. Array: An array is a data structure that can store multiple values in a single variable.
PHP arrays can hold values of different data types, and each value is associated with a
unique key or index.

For example:
$favoriteColors = array("red", "blue", "green");

6. Object: An object is an instance of a class. It can store data (properties) and perform
actions (methods) defined within the class. Objects are useful for modeling complex
entities and implementing object-oriented programming concepts.
For example:

class Person {
public $name;
public $age;
}

$person = new Person();


$person->name = "John Doe";
$person->age = 25;

7. NULL: NULL represents a variable with no value assigned to it. It is commonly used to
indicate the absence of a value or to reset a variable.

For example:
$isNull = null;

8. Resource: A resource is a special variable that holds a reference to an external resource,


such as a database connection or file handle. It is used to interact with resources outside
of PHP, and its behavior depends on the resource type.

For example:
$databaseConnection = mysqli_connect("localhost", "username", "password",
"database");

9. Callable: A callable data type represents a variable that can be called as a function. It
can be a regular function, an anonymous function, or a method of an object. Callables
are often used as arguments for higher-order functions or callback functions.

For example:
function sayHello() {
echo "Hello!";
}

$functionName = "sayHello";
$functionName(); // Outputs "Hello!"
10. Iterable: An iterable is a data type that represents a collection of values that can be
looped over. It includes arrays and objects that implement the Iterator interface or can
be traversed using the "foreach" loop. Iterables are useful for iterating through sets of
data.

For example:
$students = ["Alice", "Bob", "Charlie"];
foreach ($students as $student) {
echo $student;
}
These are the main data types in PHP. Understanding and working with these data types is
fundamental to developing PHP applications.
OPERATORS IN PHP
Operators in PHP are symbols or keywords that perform specific operations on one or more
operands (values or variables) and produce a result. PHP supports a wide range of
operators, which can be categorized into several types:

Arithmetic Operators:

Addition (+): Adds two operands together.


Subtraction (-): Subtracts the second operand from the first.
Multiplication (*): Multiplies two operands.
Division (/): Divides the first operand by the second.
Modulus (%): Returns the remainder of the division.
Increment (++) and Decrement (--): Increases or decreases the value of an operand by 1.

Assignment Operators:

Assignment (=): Assigns a value to a variable.


Addition assignment (+=): Adds the right operand to the left operand and assigns the result
to the left operand.
Subtraction assignment (-=): Subtracts the right operand from the left operand and assigns
the result to the left operand.
Multiplication assignment (*=): Multiplies the left operand by the right operand and assigns
the result to the left operand.
Division assignment (/=): Divides the left operand by the right operand and assigns the
result to the left operand.
Modulus assignment (%=): Computes the modulus of the left operand with the right
operand and assigns the result to the left operand.

Comparison Operators:

Equal to (==): Checks if two operands are equal.


Identical to (===): Checks if two operands are equal and of the same data type.
Not equal to (!=): Checks if two operands are not equal.
Not identical to (!==): Checks if two operands are not equal or not of the same data type.
Greater than (>): Checks if the left operand is greater than the right operand.
Less than (<): Checks if the left operand is less than the right operand.
Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right
operand.
Less than or equal to (<=): Checks if the left operand is less than or equal to the right
operand.

Logical Operators:

And (&&): Returns true if both operands are true.


Or (||): Returns true if at least one of the operands is true.
Not (!): Returns the negation of the operand, true becomes false and vice versa.
String Operators:

Concatenation (dot, .): Concatenates two strings together.

Array Operators:

Union (+): Combines the arrays, preserving keys.


Equality (==): Checks if two arrays have the same key/value pairs.
Identity (===): Checks if two arrays are identical (same key/value pairs with the same
order).
Ternary Operator:

Ternary (? :): Provides a shorthand way to write if-else statements.


These are some of the most commonly used operators in PHP. Understanding and utilizing
operators is essential for performing calculations, comparisons, and logical operations
within PHP code.

You might also like