# 2.4 PHP'de Süper Global Değişkenleri

PHP süper küresel değişkenler, PHP betiği boyunca her yerden erişilebilen özel değişkenlerdir. Bu değişkenler, form verilerini toplamak, oturumları yönetmek, sunucu bilgilerini almak gibi birçok farklı amaç için kullanılır.

## **`$GLOBALS`**

`$GLOBALS` değişkeni, tüm PHP betiğinde tanımlanan tüm değişkenlere global olarak erişmek için kullanılır.

```php
<?php
$x = 10;
$y = 20;

function toplama() {
    $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

toplama();
echo $z; // 30
?>
```

## **`$_SERVER`**

`$_SERVER` değişkeni, başlıklar, yol ve komut dosyası konumu gibi bilgileri içeren bir dizi döndürür.

```php
<?php
echo $_SERVER['PHP_SELF']; // Mevcut betik adı
echo "<br>";
echo $_SERVER['SERVER_NAME']; // Sunucu adı
echo "<br>";
echo $_SERVER['HTTP_HOST']; // Host başlığı
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT']; // Kullanıcı ajan bilgisi
echo "<br>";
echo $_SERVER['SCRIPT_NAME']; // Betik adı
?>
```

## **`$_REQUEST`**

`$_REQUEST` değişkeni, GET, POST ve COOKIE giriş değişkenlerini içeren bir dizi döndürür.

```php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $ad = $_REQUEST['ad'];
    if (empty($ad)) {
        echo "Ad boş!";
    } else {
        echo "Merhaba $ad";
    }
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Ad: <input type="text" name="ad">
  <input type="submit">
</form>
```

## **`$_POST`**

`$_POST` değişkeni, HTTP POST yöntemini kullanarak gönderilen verileri toplamak için kullanılır.

```php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $ad = $_POST['ad'];
    echo "Merhaba $ad";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Ad: <input type="text" name="ad">
  <input type="submit">
</form>
```

## **`$_GET`**

`$_GET` değişkeni, URL'deki sorgu dizisi (query string) üzerinden gönderilen verileri toplamak için kullanılır.

```php
<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
    $ad = $_GET['ad'];
    echo "Merhaba $ad";
}
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Ad: <input type="text" name="ad">
  <input type="submit">
</form>
```

## **`$_FILES`**

`$_FILES` değişkeni, HTTP POST ile yüklenen dosyaları yönetmek için kullanılır.

```php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $dosya = $_FILES['dosya'];
    $hedef_dizin = "uploads/";
    $hedef_dosya = $hedef_dizin . basename($dosya["name"]);

    if (move_uploaded_file($dosya["tmp_name"], $hedef_dosya)) {
        echo "Dosya yüklendi: " . htmlspecialchars($dosya["name"]);
    } else {
        echo "Dosya yüklenirken bir hata oluştu.";
    }
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    Dosya Seç: <input type="file" name="dosya">
    <input type="submit" value="Yükle">
</form>
```

## **`$_ENV`**

`$_ENV` değişkeni, ortam (environment) değişkenlerini içerir. Bu değişkenler, sunucu ortamındaki bilgilere erişmek için kullanılır.

```php
<?php
echo 'Home: ' . $_ENV["HOME"];
echo "<br>";
echo 'Path: ' . $_ENV["PATH"];
?>
```

## **`$_COOKIE`**

`$_COOKIE` değişkeni, HTTP çerezlerini (cookies) toplamak için kullanılır.

```php
<?php
// Çerez ayarla
setcookie("kullanıcı", "John Doe", time() + 3600, "/");

// Çerezi oku
if(isset($_COOKIE["kullanıcı"])) {
    echo "Kullanıcı: " . $_COOKIE["kullanıcı"];
} else {
    echo "Kullanıcı çerezi ayarlanmamış.";
}
?>
```

## **`$_SESSION`**

`$_SESSION` değişkeni, oturum değişkenlerini yönetmek için kullanılır. Oturumlar, kullanıcılar arasında verilerin kalıcı olmasını sağlar.

```php
<?php
// Oturumu başlat
session_start();

// Oturum değişkenlerini ayarla
$_SESSION["kullanıcı"] = "John Doe";
$_SESSION["email"] = "john@example.com";

// Oturum değişkenlerini oku
echo "Kullanıcı: " . $_SESSION["kullanıcı"];
echo "<br>";
echo "Email: " . $_SESSION["email"];
?>
```

> \[!NOTE]
>
> * **`$GLOBALS`**: Tüm global değişkenlere erişim sağlar.
> * **`$_SERVER`**: Sunucu ve yürütme ortamı bilgilerini içerir.
> * **`$_REQUEST`**: GET, POST ve COOKIE değişkenlerini toplar.
> * **`$_POST`**: POST yöntemi ile gönderilen verileri toplar.
> * **`$_GET`**: GET yöntemi ile gönderilen verileri toplar.
> * **`$_FILES`**: Dosya yüklemelerini yönetir.
> * **`$_ENV`**: Ortam değişkenlerini içerir.
> * **`$_COOKIE`**: HTTP çerezlerini toplar.
> * **`$_SESSION`**: Oturum değişkenlerini yönetir


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.yavuzlar.org/egitim/00x0-php-nedir/01x4-phpde-super-global-degiskenleri.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
