• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php pi – Get Value of pi Using php pi() Function

php pi – Get Value of pi Using php pi() Function

January 28, 2022 Leave a Comment

To get the value of pi in php, the easiest way is use the php pi() function. pi() returns the value 3.1415926535898.

echo pi(); // 3.1415926535898

You can also use the php constant M_PI

echo M_PI; // 3.1415926535898

In php, we can easily get the value of pi for use in trigonometry with the php math functions.

For example, let’s say we want to find the sine of a number. The php sin() function takes a number in radians and then finds the sine of the number.

To find the sine of a number, we can use pi to make our lives easy.

To get the value of pi in your php code, you just call the pi() function as shown below.

echo pi(); // 3.1415926535898

Then, if you want to use it like in our example with finding the sine of a number, you can use the pi constant in the following way.

echo sin(pi()/3); // 0.8660254037844386

pi Related Constants You Can Use in php

In php, you can also use the following constants to get the value of pi without using the php pi() function.

These constants are faster than calling the pi() function.

echo M_PI;      // 3.14159265358979323846   pi
echo M_PI_2;    // 1.57079632679489661923    pi/2
echo M_PI_4;    // 0.78539816339744830962    pi/4
echo M_1_PI;    // 0.31830988618379067154    1/pi
echo M_2_PI;    // 0.63661977236758134308    2/pi
echo M_SQRTPI   // 1.77245385090551602729    sqrt(pi)
echo M_2_SQRTPI // 1.12837916709551257390    2/sqrt(pi)

Hopefully this article has been helpful for you to understand how to find the value of pi in php.

Other Articles You'll Also Like:

  • 1.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 2.  php print_r() Function- Get Information about Variables in php
  • 3.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 4.  php Rename File – Renaming Files and Directories Easily
  • 5.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 6.  php floor – Round Down and Find the Floor of a Number in php
  • 7.  php array_merge() – Merge One or More Arrays Together by Appending
  • 8.  Remove Last Character from String Using php
  • 9.  php Absolute Value with abs Function
  • 10.  Using strtoupper() in php to Convert String to Uppercase

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy