• 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 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.  Get Current Location Latitude Longitude in php
  • 2.  php unset – How to Destroy Variables in php
  • 3.  php array_key_exists() Function – Check if Key Exists in Array
  • 4.  php property_exists() – Check if Property Exists in Class or Object
  • 5.  php preg_match_all – Get All Matches of Pattern in String
  • 6.  How to Parse URLs with php parse_url() Function
  • 7.  php strlen() – Get the Length of String Variable in php
  • 8.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function
  • 9.  php array_shift() – Remove the First Element from Array
  • 10.  php array_map() – Create New Array from Callback 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

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