• 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
  • VBA
  • About
You are here: Home / PHP / php Square Root with sqrt Function

php Square Root with sqrt Function

December 15, 2021 Leave a Comment

To find the square root of a number in php, the easiest way is with the php sqrt() function.

echo sqrt(4); // Output: 2

Finding the square root of a number in php is easy. We can find the square root of both integers and floats using the php sqrt() function.

In mathematics, the square root of a number a square root of a number x is a number y such that y^2 = x.

In php, we can calculate the square root of positive numbers including 0, but for negative numbers, the sqrt() function returns NAN.

echo sqrt(4); // Output: 2
echo sqrt(16); // Output: 4
echo sqrt(2); // Output: 1.4142135623731
echo sqrt(-2); // Output: NAN

Finding the Square Root of Positive Number in php

In php, we can use the sqrt() function to find the square root of a positive number. The return value will also be of type float.

echo sqrt(9); // Output: 3
echo sqrt(25); // Output: 5
echo sqrt(7); // Output: 2.6457513110646

Finding the Square Root of a Negative Number in php

In php, if you pass a negative number to the sqrt() function, you will get NAN.

To calculate the square root of a negative number in php, we can use the php absolute value function abs() and then calculate the square root of the absolute value.

echo sqrt(-91.1237); // Output: NAN
echo sqrt(abs(-91.1237)); // Output: 9.5458734540114  (Not exactly correct though, see note below)
echo sqrt(abs(91.1237)); // Output: 9.5458734540114

One thing to note here is that the square root of a negative number is not equal to the square root of a postive number – we still need to consider imaginary numbers.

Implementing imaginary numbers is beyond the scope of this article. Mark Baker has put together a wonderful package for complex numbers in php that you can use for working with imaginary numbers.

Hopefully this article has been helpful for you to understand how you can find the absolute value of a number in using php.

Other Articles You'll Also Like:

  • 1.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 2.  php is_numeric – Check if Variable is a Number
  • 3.  php print_r() Function- Get Information about Variables in php
  • 4.  php json_encode() Function – Get JSON Representation of Variables in php
  • 5.  php pi – Get Value of pi Using php pi() Function
  • 6.  php acos – Find Arccosine and Inverse Cosine of Number
  • 7.  php unset – How to Destroy Variables in php
  • 8.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 9.  In PHP isset Method is Used For Checking if Variable is Defined
  • 10.  Check if Variable Exists in php with isset() Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy