国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
平方根邏輯
求給定數(shù)的平方根
示例#5
結(jié)論
首頁 后端開發(fā) php教程 PHP 中的平方根

PHP 中的平方根

Aug 29, 2024 pm 01:12 PM
php

計(jì)算其他根,例如數(shù)字的 n 次方根或數(shù)字的立方根,類似地,我們需要在 PHP 中求數(shù)字的平方根。我們通過使用不同的函數(shù)(如 pow()、log() 等)來計(jì)算這些根。

廣告 該類別中的熱門課程 PHP 開發(fā)人員 - 專業(yè)化 | 8 門課程系列 | 3次模擬測試

開始您的免費(fèi)軟件開發(fā)課程

網(wǎng)絡(luò)開發(fā)、編程語言、軟件測試及其他

在 PHP 這樣的編程語言中,與內(nèi)置函數(shù)一起使用時計(jì)算平方根很簡單。這個函數(shù)就是sqrt()。我們還將了解如何在不使用 sqrt() 的情況下查找數(shù)字的平方根,以及如何使用帶有用戶輸入的表單來計(jì)算平方根。

sqrt() 函數(shù)用于計(jì)算給定數(shù)字的平方根。該函數(shù)是 PHP 中使用的內(nèi)置數(shù)學(xué)函數(shù),如 pow()、rand()、is_nan() 等

平方根邏輯

平方根邏輯的語法和描述在下面詳細(xì)解釋,

語法:

sqrt($num)

其中 $num 是傳遞給 sqrt 函數(shù)的單個參數(shù)。

描述:sqrt() 函數(shù)計(jì)算并返回給定數(shù)字的平方根。返回值是float類型。此外,我們對給定函數(shù)有不同類型的輸入數(shù)字,在這些數(shù)字上執(zhí)行平方根函數(shù)并計(jì)算結(jié)果。

這里我們會看到輸入的數(shù)字可以是正數(shù)、負(fù)數(shù)或小數(shù)(浮點(diǎn)數(shù)),也可以是零。正數(shù)返回正數(shù)作為輸出,負(fù)數(shù)返回 NAN(非數(shù)字)作為輸出,十進(jìn)制數(shù)的平方根是浮點(diǎn)數(shù)作為輸出,一的平方根是 1。另外,請記住零的平方根為零。

求給定數(shù)的平方根

給定數(shù)字的平方根如下,

如果輸入數(shù)字是81,則該數(shù)字的平方根將為9。如果輸入數(shù)字為49,則該數(shù)字的平方根將為7,依此類推。

讓我們通過一個例子來學(xué)習(xí):

我們還將學(xué)習(xí)使用不同類型的輸入求平方根。

示例#1

代碼:

<?php
// simple example to find how sqrt() function works on numbers
echo sqrt(16);
echo '<br>';
// output is 4
echo sqrt(7);
echo '<br>';
//output is 2.6457513110646
?>

輸出:

PHP 中的平方根

在上面的程序中,輸出是 4,我們知道 4*4 是 16,因此 16 的平方根是 4。在計(jì)算 7 的平方根時,我們看到小數(shù)點(diǎn)后有很多位,數(shù)字是小數(shù)點(diǎn)后的位數(shù)取決于用戶。

類似于 sqrt 函數(shù),計(jì)算給定數(shù)字的平方根。為了計(jì)算給定數(shù)字的任意根,我們使用 pow() 函數(shù),它代表冪。

示例#2

代碼 :

<?php
// example to calculate any root
echo '<br>'.'Result of? :?? pow(16, 1/2)? ======? '. pow(16, 1/2);
// example to calculate the cube root of 27
echo '<br>'.'Result of? : pow(27, 1/3)? ======? '. pow(27, 1/3);
//example to calculate the fourth root of 12
echo '<br>'.'Result of? : pow(12, 1/4)? ======? '. pow(12, 1/4);
//example to calculate the fifth root of 76
echo '<br>'.'Result of? : pow(76, 1/5)? ======? '. pow(76, 1/5);
//example to calculate the sixth root of 88
echo '<br>'.'Result of? : pow(88, 1/6)? ======? '. pow(88, 1/6);
?>

輸出:

PHP 中的平方根

示例#3

代碼:

<?php
echo '<br>'.'Result of? :?? sqrt(625)? ======? '. sqrt(625);
echo '<br>'.'Result of? :?? sqrt(49)? ======? '. sqrt(49);
echo '<br>'.'Result of? :?? sqrt(-36)? ======? '. sqrt(-36);
echo '<br>'.'Result of? :?? sqrt(0)? ======? '. sqrt(0);
echo '<br>'.'Result of? :?? sqrt(121)? ======? '. sqrt(121);
echo '<br>'.'Result of? :?? sqrt(22)? ======? '. sqrt(22);
echo '<br>'.'Result of? :?? sqrt(12.34)? ======? '. sqrt(12.34);
echo '<br>'.'Result of? :?? sqrt(-16)? ======? '. sqrt(-16);
?>

輸出:

PHP 中的平方根

示例#4

求用戶通過表單輸入的數(shù)字的平方根:在下面的程序中,我們用 PHP 創(chuàng)建了一個程序來計(jì)算用戶通過表單輸入的數(shù)字的平方根。假設(shè)用戶輸入了 16,那么我們可以找到 16 的平方根,并期望結(jié)果為 4,如果用戶輸入 49,我們可以期望結(jié)果為 7,依此類推。

此外,我們還使用內(nèi)置數(shù)學(xué)函數(shù) sqrt() 來求平方根。

代碼:

<!---program to calculate square root of input number using form -->
<html>
<head>
<title>Square root of a number using form</title>
</head>
<body>
<!--- input form with text box --->
<form method="post" action="">
<label>Enter a number</label>
<input type="text" name="input" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])) {
//storing the number in a variable $input
$input = $_POST['input'];
//storing the square root of the number in a variable $ans
$ans = sqrt($input);
//printing the result
echo 'The square root of '.$input.'====='.$ans;
}
?>
</body>
</html>

輸出 – 1:

PHP 中的平方根

輸出 – 2:輸入 100。

PHP 中的平方根

示例#5

不使用內(nèi)置 sqrt() 函數(shù)求數(shù)字的平方根:在下面的程序中,我們在 PHP 中創(chuàng)建了一個程序來計(jì)算數(shù)字的平方根,而不使用內(nèi)置函數(shù)sqrt() 函數(shù)。

代碼:

function squareroot($input)
{
//if the input number is 0 then return 0 as result
if($input == 0) {
return 0;
}
//if the input number is 1 then return 1 as result
if($input == 1) {
return 1;
}
// assigning $input value to a variable $a
$a = $input;
$b = 1;
while($a > $b)
{
// calculating the middle number
$a= ($a + $b)/2;
// dividing the input number with the middle number
$b = $input/$a;
}
return $a;
}
echo '<br>'.'Square root of 0 is '.squareroot(0);
echo '<br>'.'Square root of 20 is '.squareroot(20);
echo '<br>'.'Square root of 49 is '.squareroot(49);
echo '<br>'.'Square root of 81 is '.squareroot(81);
echo '<br>'.'Square root of 1 is '.squareroot(1);

輸出:

PHP 中的平方根

結(jié)論

在本文中,我們學(xué)習(xí)了什么是平方根,以及如何使用和不使用 sqrt()、pow() 等內(nèi)置函數(shù)來計(jì)算平方根。 sqrt() 和 pow() 函數(shù)的作用是什么?如何在程序中使用它來求平方根?我們學(xué)習(xí)了如何對數(shù)字、浮點(diǎn)數(shù)、負(fù)數(shù)等進(jìn)行平方根。我們還學(xué)習(xí)了如何使用表單通過用戶定義的輸入計(jì)算平方根。

以上是PHP 中的平方根的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

我如何了解最新的PHP開發(fā)和最佳實(shí)踐? 我如何了解最新的PHP開發(fā)和最佳實(shí)踐? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

什么是PHP,為什么它用于Web開發(fā)? 什么是PHP,為什么它用于Web開發(fā)? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

如何設(shè)置PHP時區(qū)? 如何設(shè)置PHP時區(qū)? Jun 25, 2025 am 01:00 AM

tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()

我如何驗(yàn)證PHP中的用戶輸入以確保其符合某些標(biāo)準(zhǔn)? 我如何驗(yàn)證PHP中的用戶輸入以確保其符合某些標(biāo)準(zhǔn)? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

什么是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? 什么是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? Jun 22, 2025 am 01:03 AM

thephpfunctionserize()andunSerialize()redustoconvertComplexdatStructDestoresToroStoroStoroSandaBackagagain.1.Serialize()

如何將PHP代碼嵌入HTML文件中? 如何將PHP代碼嵌入HTML文件中? Jun 22, 2025 am 01:00 AM

可以將PHP代碼嵌入HTML文件中,但需確保文件以.php為擴(kuò)展名,以便服務(wù)器能正確解析。使用標(biāo)準(zhǔn)的標(biāo)簽包裹PHP代碼,可在HTML中任意位置插入動態(tài)內(nèi)容。此外,可在同一文件中多次切換PHP與HTML,實(shí)現(xiàn)條件渲染等動態(tài)功能。務(wù)必注意服務(wù)器配置及語法正確性,避免因短標(biāo)簽、引號錯誤或遺漏結(jié)束標(biāo)簽導(dǎo)致問題。

編寫清潔和可維護(hù)的PHP代碼的最佳實(shí)踐是什么? 編寫清潔和可維護(hù)的PHP代碼的最佳實(shí)踐是什么? Jun 24, 2025 am 12:53 AM

寫干凈、易維護(hù)的PHP代碼關(guān)鍵在于清晰命名、遵循標(biāo)準(zhǔn)、合理結(jié)構(gòu)、善用注釋和可測試性。1.使用明確的變量、函數(shù)和類名,如$userData和calculateTotalPrice();2.遵循PSR-12標(biāo)準(zhǔn)統(tǒng)一代碼風(fēng)格;3.按職責(zé)拆分代碼結(jié)構(gòu),使用MVC或Laravel式目錄組織;4.避免面條式代碼,將邏輯拆分為單一職責(zé)的小函數(shù);5.在關(guān)鍵處添加注釋并撰寫接口文檔,明確參數(shù)、返回值和異常;6.提高可測試性,采用依賴注入、減少全局狀態(tài)和靜態(tài)方法。這些做法提升代碼質(zhì)量、協(xié)作效率和后期維護(hù)便利性。

如何使用PHP執(zhí)行SQL查詢? 如何使用PHP執(zhí)行SQL查詢? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles