How do you define constant pi with the value 3.142 in php?

How do you define constant pi with the value 3.142 in php?

Using Constants in Your Computer Program

Using Constants in C:

The C program below finds the area of a circle. In this program the value of π is defined in two different ways. One is by using using the preprocessor directive '#define' to make 'PI' equal to 3.142857. The other uses the key work 'const' to define a double called 'pi' equal to 22.0/7.0. The difference between these two ways of defining a constant is that the '#define' directive replaces PI with example what follows it in the code prior to compiling that code, and the 'const' keyword stores a value into memory that is called by the program whenever the constant name appears.

Below the source we can see the outputs are not exactly the same. We could make them the same by replacing '#define PI 3.142857' with '#define PI 22.0/7.0. However, if this were a large program where 'PI' appeared often, we would not want to do that. This is because the '#define' directive would cause the program to calculate 22.0/7.0 everywhere 'PI' appears. In this program the constant 'pi' is assigned the value resulting from the calculation of 22.0/7.0. The math to determine this value is performed only once. On the other hand, there may be cases, such as in embedded micro-controller applications where memory is very limited. In such a case, the '#define' directive can be used to save that space in memory.

#include 
#include 

#define PI 3.142857
const double SQUARED = 2.0;
const double pi = 22.0 / 7.0;

int main()
{
    double areaOfCircle;
    double radiusOfCircle = 2.0;

    areaOfCircle = pi*pow(radiusOfCircle, SQUARED);
    printf("The area of the circle is %f\n",areaOfCircle);

    areaOfCircle = PI*pow(radiusOfCircle, SQUARED);
    printf("The area of the circle is %f\n",areaOfCircle);

    return 0;
}

How do you define constant pi with the value 3.142 in php?


Introduction

Constants are represented literally in an assignment expression such as $x=10 or $name="XYZ" where 10 and XYZ are numeric and string constants assigned to variables. In PHP, it is possible to define a constant with a user defined identifier with the help of define() function

Syntax

define ( string $name , mixed $value [, bool $case_insensitive = FALSE ] ) : bool

parameters

Sr.NoParameter & Description
1 name
name of the constant.
2 value
value of the constant may be any scalar value (integer, float, string etc) or array
3 case_insensitive
Constant identifiers are case sensitive by default. If this parameter is set to true, name and NAME are treated similarly

Return Value

Function returns TRUE if definition is sucessful, else FALSE is returned

Example

Following example shows use of define() function to define constants

magic constants

PHP has a large number of predefined constants but most of them will be enabled if corresponding extensions are installed. However, following constants - which are called magic constants - are always available

Name Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file
__DIR__ The directory of the file.
__FUNCTION__ The function name, or {closure} for anonymous functions.
__CLASS__ The class name. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ The trait name. The trait name includes the namespace it was declared in (e.g. Foo\Bar).
__METHOD__ The class method name.
__NAMESPACE__ The name of the current namespace.

Following example demonstrates some of magic constants

Example

 Live Demo

mymethod();
?>

Output

Following result will be displayed

Line no: 2
file name : C:\xampp\php\testscript.php
directory name: C:\xampp\php
myclass
myclass::mymethod

How do you define constant pi with the value 3.142 in php?

Updated on 19-Sep-2020 15:00:15

  • Related Questions & Answers
  • PHP Class Constants
  • PHP Predefined Mathematical Constants
  • Which is faster? Constants, Variables or Variable Arrays in PHP?
  • Difference between C++ string constants and character constants
  • C# TicksPer constants
  • Decimal constants in C#
  • Mathematical Constants in Python
  • Constants in Rust Programming
  • Are there constants in JavaScript?
  • ABAP constants with %_ as prefix
  • What are constants in C++?
  • What are C++ Integer Constants?
  • What are C++ Character Constants?
  • Display File class constants in Java
  • Using constants in ABAP OO method

How do you define constant PI with value 3.142 in PHP?

M_PI : It describes π. Its value is 3.141592. M_2_PI : It describes 2/π..
The pi() function can be used to return the value of π..
M_PI is a named constant which is slightly faster than the pi() function..
pi() function returns the value of pi as float..

How do you define a constant in PHP?

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.

How do you declare a constant variable PI of type double?

constant variables can be declared by using constant keyword. for eg. constant double pi = 3.14 or constant variables can be declared by using Macro.

How can we declare variable and constant in PHP?

A constant can only be defined using define() function. It cannot be defined by any simple assignment. A variable can be defined by simple assignment (=) operator. There is no need to use the dollar ($) sign before constant during the assignment.