Data Types

In programming languages, a data type is a category that defines the type of data being represented. Each piece of data has a different data type, such as numeric data, character data, and others. The common data types in programming are known as primitive data types (meaning they are the most frequently encountered types in programming and are also the oldest types available today). C/C++ is a pioneering programming language for primitive data types and has inspired the creation of other programming languages. Here are the basic data types that can be used in C:

  • Boolean: A logical data type that contains Boolean expressions (True/1 and False/0).
  • Integer: A data type that holds whole numbers.
  • Floating Point: A data type that holds decimal numbers.
  • String: A data type that holds characters.
  • List/Array: A data type that contains a collection of data with multiple entries. An array can only contain a list of data of the same type.

Numbers & Digits

PHP recognizes several types of numbers, including integers and floating-point numbers. Integer and floating-point data types can be further divided into several new types based on the range of values they can hold.

Integer Data Types in PHP:

Integer

Integer same as long, Integer has a storage size of 4 bytes (32 bits), allowing it to store values in the range of -2,147,483,648 to 2,147,483,647 or 0 to 4,294,967,295.

Floating Point Data Types in C/C++:

Float

float has a storage size of 4 bytes (32 bits), allowing it to store values in the range of 1.2E-38 to 3.4E+38.

Double

double has a storage size of 8 bytes (64 bits), allowing it to store values in the range of 2.3E-308 to 1.7E+308.

Here’s an example of applying numeric data types in PHP:

Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$variable_float = 10.54;
$variable_int = 1234;
$variable_double = 23230204.5;

var_dump($variable_float);
$variableInt = gettype($variable_int);
$variableDouble = gettype($variable_double);

echo("$variableInt\n");
echo("$variableDouble\n");
?>

Output:

1
2
3
float(10.54)
integer
double

Boolean Expressions

A Boolean expression is an expression that returns a value of True or False, using relational operators/comparison operators, as well as logical operators. Additionally, Boolean expressions can also utilize membership operators and identity operators in certain cases. In PHP, Boolean values can be represented with false as False and true as True.

List/Array

A list/array is a collection that contains multiple data items. Generally, lists have an index (an order within the collection of data) that starts from 0. To declare a list, create square brackets “[]” after the variable name to declare the list, with each item separated by commas “,”.

Basic Example of Using a List:

1
2
3
4
5
6
<?php
$thisList = [25, 50, 75, 100];

echo("$thisList[0]\n");
echo("$thisList[3]\n");
?>

String

In PHP, a string is represented as a list/array of characters (character data type). If you try to execute commands on a variable of string data type, the results will be the same as those produced in a list, as shown in the example below:

1
2
3
4
5
6
7
8
9
<?php
$thisString1 = "Ini adalah String";
echo("$thisString1[0]\n");
echo("$thisString1\n");

$thisString2 = "Ini adalah String";
echo("$thisString2[5]\n");
echo("$thisString2\n");
?>

Variable

A variable is a place for storing data. If we compare it to something, a variable is like a box. This box has attributes such as the name of the box, notes on the type of contents it holds, and the contents themselves. Therefore, we can conclude that a variable has a name, a type/data type for its contents, and the contents themselves.

In programming languages, variables generally fall into two common types: immutable/constant variables and mutable variables.

To declare a mutable variable in PHP, you need to write “$” in front of the variable name, followed by assigning a value to the variable using the equals sign (=) as the assignment operator.

Example of Declaring a Mutable Variable in C/C++:

1
2
3
4
$int_thisFive = 5;
$str_ThisName = "Your Name";
$flt_thisFivePointSeven = 5.7;
$tf_thisStatus = false;

For declaring an immutable variable, you need to write “const” in front of the variable name then you should name the variable without “$” and you should name the variable in all capital letters.

Example of Declaring an Immutable/Constant Variable in PHP:

1
2
3
4
5
<?php
const SALAH = 0;
const FIVE = 5;
const PI = 3.14;
?>

Variable data types can also be converted to other data types, but you must ensure compatibility between the original data type and the target data type. For instance, if you want to convert a numeric integer data type to a numeric float data type, this can be done by considering the value range of the original integer type before conversion and the float type you want to convert to.

Be cautious when converting variable data types and always consider the compatibility of data types and the storage space of each type.

Example of Data Type Conversion:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$thisInt = 15;

echo("Number $thisInt is still of integer type\n");

var_dump($thisInt);

$thisFloat = (float) $thisInt + 0.0;

printf("Number $thisFloat is now of float type\n");

var_dump($thisFloat);
?>

In addition to the two common types mentioned above, there are also types based on usage. Generally, variable usage is divided into two types: local variables and global variables. A global variable is accessible in any program block, while a local variable can only be accessed within a specific program block, such as in a function or class.

We will discuss examples of global and local variables in the next session.

Operator

Arithmetic Operators

Arithmetic operations are part of number processing in a computer for performing calculations. In addition to performing calculations, arithmetic operations can also be used for logical operations. The foundation of performing calculations in computer arithmetic is addition, also known as the adder.

Here are the Arithmetic Operators in the PHP Programming Language.

Operator Symbol
Addition +
Substraction -
Multiplication *
Division /
Remainder of Share %
Power of **

This is was example of the Arithmatics Operation:

1
2
3
4
5
6
echo(3 + 2);echo("\n");
echo(3 - 2);echo("\n");
echo(2 * 8);echo("\n");
echo(10 / 3);echo("\n");
echo(18 % 5);echo("\n");
echo(3 ** 3);echo("\n");

Mathematical operators function works normally in PHP, just like in other programming languages. There are several notes to keep in mind:

  • Multiple variables can be assigned the same value in one statement.
  • Parentheses () are used to group operations that should be performed first.
  • Division of an integer by an integer will round down.
  • An integer will be converted to a floating-point number in operations involving both integers and floating-point numbers.
  • We cannot convert a complex number to a real number (floating-point or integer); only its absolute value can be obtained.

Assignment Operators

As the name suggests, this operator is used to assign values to variables. For example: age = 18 This means the variable age has been assigned the value of 18. In addition to storing or assigning values, you can also perform addition, subtraction, multiplication, division, etc. More details can be found in the following table.

Operator Symbol
Filling =
Addition += or =+
Substraction -= or =-
Multiplication *=
Division /=
Remainder of Share %=
Power of **=
String concenation .=

The Increment and Decrement operators are operators that can add or subtract 1 from a variable. These operators are often used in loops and arithmetic operations. More details can be seen in the table below.

Operator Symbol
Increment ++
Decrement --

Comparison Operators

Comparison operators are operators that compare two values. These are also known as relational operators and are often used to create logic or conditions. Below is a list of Comparison Operators in C/C++:

Operator Symbol
Greater than >
Smaller than <
Equals to == or ===
Not Equals to != or !==
Greater than or Equals to >=
Smaller than or Equals to <=

Logical Operators

Logical operators are operators used to create logic in the programs we write. Logical operators are often referred to as Boolean algebra operators and are typically used to create branching operations in a program. The logical operators include AND, OR, and NOT.

The logical operators consist of:

Operator Symbol
AND Logic && or and
OR Logic || or or
Negation Logic !

Bitwise Operators

As the name suggests, bitwise operators are used to perform bit-level operations on values in a computer. Here are the bitwise operators available in C/C++:

Operator Symbol
Bitwise AND &
Bitwise OR |
Bitwise NOT ~
Bitwise XOR ^
Bitwise Right Shift >>
Bitwise Left Shift <<

Input and Output Functions

Output

The Output function is used to display something, often for showing a value that can be read and seen by the user. Generally, the Output function uses the command echo(). Its usage can be seen in the code below:

Code:

1
echo("This is the echo Output Function\n");

Output:

1
This is the Echo Output Function

What if we want to include variables in the output function? To insert a variable into printf, you need to add a % sign as a data type identifier, whereas with cout, you simply include the variable name.

Here’s an example:

Code:

1
2
3
4
5
6
7
8
9
10
11
$x = 5;
$y = "example";
$z = 5.5;

// Just Variables Without String
echo($x);echo("\n");
echo($z);echo("\n");
echo($y);echo("\n");

// Combining String with Variables
echo("this is $x\n");

Output:

1
2
3
4
5
example
5.5
this is 5

Input

The Input function is used to request values from the user and input them into a program. Generally, the Input function uses the readline() commands. Here’s an example of using input functions in PHP:

Code:

1
2
3
$x = readline("Input Value: ");

echo("this is $x\n");

Output:

1
2
3
Input Value: 5 (Input By User)

this is 5

Package & Library

Packages and Libraries are bundles or groups of many functions and classes (source code) combined into a single unit in the library that can be used and called in the source code you are developing, allowing you to obtain a function without having to repeatedly type the source code. PHP provides packages/libraries for standard operations. For more specific operations, you may need to use functions from other packages. In this practical session, we will learn about arithmetic operations and how to use a package to call trigonometric operation functions, which are provided in PHP’s Math package.

Example of using a Library/Package:

1
2
3
$flt_root = sqrt(9);

echo("this is $flt_root\n");