PHP Math,PHP Constants,PHP Operators

 PHP has a set of math functions that allows you to perform mathematical tasks on numbers.


PHP pi() Function

The pi() function returns the value of PI:

Example

<?php
echo(pi()); // returns 3.1415926535898
?>
Try it Yourself »

PHP min() and max() Functions

The min() and max() functions can be used to find the lowest or highest value in a list of arguments:

Example

<?php
echo(min(01503020, -8, -200));  // returns -200
echo(max(01503020, -8, -200));  // returns 150
?>
Try it Yourself »

PHP abs() Function

The abs() function returns the absolute (positive) value of a number:

Example

<?php
echo(abs(-6.7));  // returns 6.7
?>
Try it Yourself »

PHP sqrt() Function

The sqrt() function returns the square root of a number:

Example

<?php
echo(sqrt(64));  // returns 8
?>
Try it Yourself »

PHP round() Function

The round() function rounds a floating-point number to its nearest integer:

Example

<?php
echo(round(0.60));  // returns 1
echo(round(0.49));  // returns 0
?>
Try it Yourself »

Random Numbers

The rand() function generates a random number:

Example

<?php
echo(rand());
?>
Try it Yourself »

To get more control over the random number, you can add the optional min and max parameters to specify the lowest integer and the highest integer to be returned.

For example, if you want a random integer between 10 and 100 (inclusive), use rand(10, 100):

Example

<?php
echo(rand(10100));
?>
Try it Yourself »

Constants are like variables except that once they are defined they cannot be changed or undefined.


PHP Constants

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

Note: Unlike variables, constants are automatically global across the entire script.


Create a PHP Constant

To create a constant, use the define() function.

Syntax

define(namevaluecase-insensitive)

Parameters:

  • name: Specifies the name of the constant
  • value: Specifies the value of the constant
  • case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

Example

Create a constant with a case-sensitive name:

<?php
define("GREETING""Welcome to W3Schools.com!");
echo GREETING;
?>
Try it Yourself »

Example

Create a constant with a case-insensitive name:

<?php
define("GREETING""Welcome to W3Schools.com!", true);
echo greeting;
?>
Try it Yourself »

PHP Constant Arrays

In PHP7, you can create an Array constant using the define() function.

Example

Create an Array constant:

<?php
define("cars", [
  "Alfa Romeo",
  "BMW",
  "Toyota"
]);
echo cars[0];
?>
Try it Yourself »

Constants are Global

Constants are automatically global and can be used across the entire script.

Example

This example uses a constant inside a function, even if it is defined outside the function:

<?php
define("GREETING""Welcome to W3Schools.com!");

function myTest() {
  echo GREETING;
}
 
myTest();
?>

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

OperatorNameExampleResultShow it
+Addition$x + $ySum of $x and $yTry it »
-Subtraction$x - $yDifference of $x and $yTry it »
*Multiplication$x * $yProduct of $x and $yTry it »
/Division$x / $yQuotient of $x and $yTry it »
%Modulus$x % $yRemainder of $x divided by $yTry it »
**Exponentiation$x ** $yResult of raising $x to the $y'th powerTry it »

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

AssignmentSame as...DescriptionShow it
x = yx = yThe left operand gets set to the value of the expression on the rightTry it »
x += yx = x + yAdditionTry it »
x -= yx = x - ySubtractionTry it »
x *= yx = x * yMultiplicationTry it »
x /= yx = x / yDivisionTry it »
x %= yx = x % yModulusTry it »


PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

OperatorNameExampleResultShow it
==Equal$x == $yReturns true if $x is equal to $yTry it »
===Identical$x === $yReturns true if $x is equal to $y, and they are of the same typeTry it »
!=Not equal$x != $yReturns true if $x is not equal to $yTry it »
<>Not equal$x <> $yReturns true if $x is not equal to $yTry it »
!==Not identical$x !== $yReturns true if $x is not equal to $y, or they are not of the same typeTry it »
>Greater than$x > $yReturns true if $x is greater than $yTry it »
<Less than$x < $yReturns true if $x is less than $yTry it »
>=Greater than or equal to$x >= $yReturns true if $x is greater than or equal to $yTry it »
<=Less than or equal to$x <= $yReturns true if $x is less than or equal to $yTry it »
<=>Spaceship$x <=> $yReturns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.Try it »

PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable's value.

The PHP decrement operators are used to decrement a variable's value.

OperatorNameDescriptionShow it
++$xPre-incrementIncrements $x by one, then returns $xTry it »
$x++Post-incrementReturns $x, then increments $x by oneTry it »
--$xPre-decrementDecrements $x by one, then returns $xTry it »
$x--Post-decrementReturns $x, then decrements $x by oneTry it »

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

OperatorNameExampleResultShow it
andAnd$x and $yTrue if both $x and $y are trueTry it »
orOr$x or $yTrue if either $x or $y is trueTry it »
xorXor$x xor $yTrue if either $x or $y is true, but not bothTry it »
&&And$x && $yTrue if both $x and $y are trueTry it »
||Or$x || $yTrue if either $x or $y is trueTry it »
!Not!$xTrue if $x is not trueTry it »

PHP String Operators

PHP has two operators that are specially designed for strings.

OperatorNameExampleResultShow it
.Concatenation$txt1 . $txt2Concatenation of $txt1 and $txt2Try it »
.=Concatenation assignment$txt1 .= $txt2Appends $txt2 to $txt1Try it »

PHP Array Operators

The PHP array operators are used to compare arrays.

OperatorNameExampleResultShow it
+Union$x + $yUnion of $x and $yTry it »
==Equality$x == $yReturns true if $x and $y have the same key/value pairsTry it »
===Identity$x === $yReturns true if $x and $y have the same key/value pairs in the same order and of the same typesTry it »
!=Inequality$x != $yReturns true if $x is not equal to $yTry it »
<>Inequality$x <> $yReturns true if $x is not equal to $yTry it »
!==Non-identity$x !== $yReturns true if $x is not identical to $yTry it »

PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions:

OperatorNameExampleResultShow it
?:Ternary$x = expr1 ? expr2 : expr3Returns the value of $x.
The value of $x is expr2 if expr1 = TRUE.
The value of $x is expr3 if expr1 = FALSE
Try it »
??Null coalescing$x = expr1 ?? expr2Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Introduced in PHP 7
Try it »

Post a Comment

0 Comments