How do you define constant pi with the value 3.142 in php?
Show Using Constants in Your Computer ProgramUsing 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 IntroductionConstants 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 Syntaxdefine ( string $name , mixed $value [, bool $case_insensitive = FALSE ] ) : bool parameters
Return ValueFunction returns TRUE if definition is sucessful, else FALSE is returned ExampleFollowing example shows use of define() function to define constants magic constantsPHP 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
Following example demonstrates some of magic constants ExampleLive Demo mymethod(); ?> OutputFollowing result will be displayed Line no: 2 file name : C:\xampp\php\testscript.php directory name: C:\xampp\php myclass myclass::mymethod
Updated on 19-Sep-2020 15:00:15
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.
|