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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / PHP / php Convert Degrees to Radians with php deg2rad() Function

php Convert Degrees to Radians with php deg2rad() Function

February 5, 2022 Leave a Comment

To convert degrees to radians for use in trigonometric functions in php, the easiest way is with the php deg2rad() function.

$radians = deg2rad(60)

The php collection of math functions has many powerful functions which make performing certain calculations in php very easy.

One such calculation which is very easy to perform in php is converting degrees to radians.

We can convert degrees to radians easily with the php deg2rad() function.

To do so, we need to pass any number to the deg2rad() function.

Below are a few examples of how to use the deg2rad() function to convert different angles, in degrees, to radians with in php.

echo deg2rad(0);
echo deg2rad(30);
echo deg2rad(60);
echo deg2rad(90);

// Output:
0
0.5235987755983
1.0471975511966
1.5707963267949

If you’d like to go the other way, converting radians to degrees, you can use the php rad2deg function.

Converting Degrees to Radians Without rad2deg() Function in php

Converting degrees to radians is a very easy formula. To convert degrees to radians, all we need to do is multiply the degrees by pi divided by 180.

We can convert degrees to radians without the help of the math module easily in php.

Below is a user-defined function which will convert degrees to radians for us in our php code.

function degrees_to_radians($degrees):
  return $degrees * (pi()/180);
}

Let’s test the function to verify that we get the same results as the deg2rad() php function.

function degrees_to_radians($degrees):
  return $degrees * (pi()/180);
}

echo degrees_to_radians(0);
echo degrees_to_radians(30);
echo degrees_to_radians(60);
echo degrees_to_radians(90);

// Output:
0
0.5235987755983
1.0471975511966
1.5707963267949

As you can compare for yourself to the example above, we get the same results as using deg2rad()

Hopefully this post was helpful for you to learn how to convert degrees to radians in php with and without the deg2rad() php function.

Other Articles You'll Also Like:

  • 1.  Check if String Starts with Given String in php with str_starts_with()
  • 2.  php var_dump() Function – Print Information about Variable Type and Value
  • 3.  Remove Duplicates from Array in php with array_unique()
  • 4.  php preg_match_all – Get All Matches of Pattern in String
  • 5.  Check if Variable Exists in php with isset() Function
  • 6.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 7.  Get Word Count of String in php with str_word_count() Function
  • 8.  Remove Last Character from String Using php
  • 9.  php strlen() – Get the Length of String Variable in php
  • 10.  php json_encode() Function – Get JSON Representation of Variables in php

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