• 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 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 Trig – Using Trigonometric Functions in php for Trigonometry
  • 2.  php var_dump() Function – Print Information about Variable Type and Value
  • 3.  php array_merge() – Merge One or More Arrays Together by Appending
  • 4.  Using strtoupper() in php to Convert String to Uppercase
  • 5.  Get Current Location Latitude Longitude in php
  • 6.  What is the Difference Between unset() and unlink() in php?
  • 7.  php sleep() Function – Pause Execution of Code in php
  • 8.  preg_replace php – Use Regex to Search and Replace
  • 9.  How to Create an Empty Array in php
  • 10.  Get Current Month of Date 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