Arithmetic Operators

  • PHP Integer (Full Numbers)

An integer is an integer (without commas) that must meet the following conditions:
- Should not contain commas or spaces
- Must not contain decimal points.
- Can take positive or negative values:

<? Php
Sint1 = 42; // positive number
$ int2 = 42; // negative number

PHP Float
- A float is a number that includes a decimal point.

<? Php
$ x = 42,168;
?>

  • PHP Boolean

- A Boolean can take two different states: TRUE or FALSE.

<? Php
$ x = true; $ y = false;
?>

Generally used in condition testing. Many data combinations are used in combination with each other. In this example, string and integer are used together to find the sum of two numbers.


<? Php
$ str = "10";
$ int = 20;
$ amount = $ str + $ int;
echo ($ sum);
// Output 30
?>

PHP will automatically convert each variable to the appropriate data type, depending on its value. This is why the $ str variable is traced as a number in the addition.

  • Declaration of variables

-FPP variables can be declared anywhere in the script. The scope of variables is that part of the script in which variables can be referenced or used. PHP variables can be declared local, global, and static. A variable declared within a function has a local scope, and can only be accessed within that function. For example:

<? Php
$ name = 'Mira';
function getEmri () {
echo $ name:
}
getEmri ():
// Error: Undefined variable: name
?>

This script will produce an error because the name variable $ has a global scope, and is not accessible within the getName () function.

Global variable
-The global word is used to access a global variable within a function.

<? Php
$ name = 'Mira';
function getEmri () {
global $ name;
echo $ name;
}
getEmri ();
// Output 'Mira'
?>

Variable variable
-With PHP, we can use a variable to name another variable. Thus, the value of another variable is treated as the variable name. For example:
<? Php
$ a = 'Hello';
$ Hello = "How are you!";
echo $$ a;
// Output 'How are you!'
?>

$$ a is a variable that uses the value of another variable, $ a, is its name. The value of $ is the same as "Hello". The result of the variable is $ Hello, which holds the value "How are you!".


  • ARITHMETIC OPERATORS


-Arithmetic operators perform arithmetic operations with variables. Arithmetic operators work with numerical values ​​to perform common arithmetic operations.

Operator Name Example
+ Collection $ x + $ y
- Discount $ x - $ y
* Multiplication $ x * $ y
/ Partition $ x / $ y
% Module $ x% $ y

example:
<? Php
$ num1 = 8;
$ num2 = 6;

// collection
echo $ num1 + $ num2; // 14

// Subtraction
echo $ num1 - $ num2; // 2

// multiplication
echo $ num1 * $ num2; // 48

//Division
echo $ num1 / $ num2; //1.33333333333
?>


  • MODULE


-The module operator, presented with the sign%, returns the remainder of the first number with the second number:

>? Php
$ x = 14;
$ y = 3;
echo $ x% $ y; // 2
?>

If comma numbers are used, they will be converted to integers before the operations are performed.

Post a Comment

0 Comments