首頁  >  文章  >  後端開發  >  PHP 取得方法

PHP 取得方法

WBOY
WBOY原創
2024-08-29 13:01:56875瀏覽

PHP 程式語言的 Get 方法對於從指定/特定資源請求資料非常有幫助。它是從特定資源請求資料的 HTTP 請求方法之一。 HTTP 通常的工作方式就像伺服器和客戶端之間的請求-回應協定一樣。客戶端瀏覽器通常會向特定伺服器提交/傳送一些 HTTP 請求,但伺服器會向客戶端傳回特定回應。此請求/回應包含有關特定請求的一些狀態資訊。這個請求過程可以來自 PHP 程式語言的 GET 方法。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

$_GET["name1"]

PHP GET 方法如何運作?

通常,PHP 程式語言的 GET 方法是一種不安全類型的方法,因為它會在 URL 空間/網址列空間上顯示大部分資訊。使用 GET 方法僅限於資料發送,但 GET METHOD 有助於快速發送資料。它也是關聯數組變數之一,將透過一些 URL 參數傳遞到目前特定腳本,這些參數只不過是查詢字串。該數組將請求查詢字串。我們可以透過從指定或特定的來源/資源請求資料來使 GET 方法在正常的 PHP 程式中運作。

GET 方法也可以透過使用者發送一些附加到特定頁面請求的編碼資訊來運作。該頁面現在將是由「?」分隔的編碼資訊。 URL 網址列空格處的字元。 GET 方法將產生一個非常長的字串,該字串出現在特定瀏覽器的伺服器日誌中的 location: 框中。發送長度限制為 1024 個字元。如果我們使用 GET 方法傳遞一些安全資訊(例如密碼),那麼它將被發送到伺服器,因此最好不要使用一些重要且安全的資訊/資訊。 GET 方法不能用於將一些二進位資料(如文字文件、影像等)傳送到自己的伺服器/其他伺服器。可以藉助 GET METHOD 概念所傳送的 QUERY_STRING 環境變數來存取資料。要存取發送的資料/訊息,大多數時候都會使用 $_GET。

在 PHP 中實作 GET 方法的範例

以下是 PHP GET 方法的方法:

範例#1

這是使用 PHP 程式語言的 GET 方法的範例。這裡首先使用error_reporting()函數來處理一次錯誤。然後 x1 和 y1 變數與其中的 GET METHOD f1 和 s1 值一起使用。然後建立 z1 變數來對 x1 和 y1 變數值求和。然後echo語句就是顯示z1變數值。然後在表單方法中使用GET方法。表單

內部標籤用於建立具有特定列和行的表。傳遞 f1 和 s1 值以取得 x1 和 y1 變數的資訊。然後 z1 變數值將被建立並顯示在瀏覽器結果的頂部。查看輸出以了解輸入值之前和之後的結果如何。

代碼:

<html>
<head>
<title>get_browser1</title>
<?php
error_reporting(1);
$x1=$_GET['f1'];
$y1=$_GET['s1'];
$z1=$x1+$y1;
echo "Sum of the two numbers = ".$z1;
?>
</head>
<body bgcolor="skyblue">
<form method="GET" >
<table border="2" bgcolor="green">
<tr>
<td>Enter the first number for input :: </td>
<td><input type="text" name="f1"/></td>
</tr>
<tr>
<td>Enter the second number for input :: </td>
<td><input type="text" name="s1"/></td>
</tr>
<tr align="center">
<td colspan="3" >
<input type="submit" value="+"/></td>
</tr>
</table>
</form>
</body>
</html>

輸出:

1_1:

PHP 取得方法

1_2:

PHP 取得方法

範例#2

在下面的 GET METHOD 概念範例中,使用者必須在文字方塊中輸入名稱。輸入名稱並點擊“提交輸入名稱”後即可完成。您將在輸入框上方看到輸入的名稱/單字的輸出,然後您將像以前一樣再次看到正常的輸入框。由於 PHP 程式語言的 GET 方法,使用者甚至可以檢查 URL 內部的輸入。這裡首先 echo 語句與 GET METHOD 呼叫一起使用,但內部的字串名稱/值沒有值。因此我們將使用 FORM 方法將值傳遞給它。然後使用該表以便更好地理解輸入輸入名稱。然後輸入並點擊提交按鈕後,名稱就會出現在頂部。

代碼:

<html>
<head>
<?php
echo $_GET['n1'];
?>
<title>get_browser2 pavankumar sake</title></head>
<body bgcolor="sky color">
<form method="GET">
<table border="2" bgcolor="green">
<tr>
<td>Enter the name for input :: </td>
<td><input type="text" name="n1"/></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="show the input name"/></td>
</tr>
</table>
</form>
</body>
</html>

輸出:

PHP 取得方法

Example #3

This is the example of implementing the GET METHOD to show the name and the age which is entered in the user boxes. Here I entered “pavankumar sake” as name value and “23” as the age value. Here at first, inside of the PHP tags name1 and age1 are used inside, and then using the echo statement they will be printed but those values got from the FORM METHOD below. Check out the output 3_1 and output 3_2 to understand the concept better.

Code:

<?php
if( $_GET['name1'] || $_GET['age1'] ) {
echo "Welcome ". $_GET['name1']. "<br />";
echo "You are ". $_GET['age1']. " years old.";
exit();
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "GET">
Name :: <input type = "text" name = "name1" />
Age :: <input type = "text" name = "age1" />
<input type = "submit" />
</form>
</body>
</html>

Output:

3_1:

PHP 取得方法

3_2:

PHP 取得方法

Advantages of PHP GET Method

Since some data which will be sent by the GET METHOD of the PHP language will be displayed in/inside of the specific URL, it will only be possible in order to bookmarking the page which is with the specific query values of the string/strings. The GET METHOD will help the specific method requests can/should be cached and the Get Method requests remains in our browser history. The Get Method requests can/will be bookmarked.

Conclusion

I hope you learnt what is the definition of PHP Get Method along with its syntax and explanation, How the PHP Get Method works along with the various examples and explanation of those examples, Advantages of the Get Method etc. to under the Get Method concept better.

Recommended Article

This is a guide to the PHP GET Method. Here we discuss the introduction, syntax, and working of Get Method in PHP along with examples and advantages. You can also go through our other suggested articles to learn more –

  1. Abstract Class in PHP
  2. Socket Programming in PHP
  3. PHP Frameworks

以上是PHP 取得方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn