I'm new to the **Swift language** and until this point i only worked in **web development.**

I tried to grasp the idea of optionals, but every time *they* present some situation where to use it, I still can't understand why they do so (or I might have a little idea, but still not sure.)

I read several articles about this and maybe I just got too confused about it along the way and got lost.

So let's present some examples. Let's say I have a registration form with a **textfield** for an e-mail. After the form is sent, in PHP I could do something like:

```
$mail = $_POST['mail'];

if($mail == "") { 
    echo "ERROR: The E-mail is empty";
} else {
    // Do something more
}

```

If the textfield is empty, the value of $\_POST['mail'] is an empty string. I can work with that and check if it is empty or not.

Now, if I understand correctly, in swift the value of that textfield wouldn't be an empty string, but actually a nil, right? Is this that kind of a difference why we need optionals? Because in comparison to PHP, in Swift I have to declare the type of that information each time and it doesn't know if that textfield passes strings or numbers?

Let's talk about another example. Lot of people in **swift** just declare a string as an optional, even if they put a value in it, like so:

```
let name:String? = "Gabriel"

```

If it already has a value, why make it an optional then? For me, it is just a normal constant with a value and I don't see the point of wrapping it into an optional.

**If I don't know the name at the declaration, why can't i just type this?**

```
let name:String = ""

```

and after that, I can use an if statement to check if it is empty or not

```
if name == "" {
  print("ERROR: name is empty")
} else {
  //Do something else
}

```

Did I get the idea with the textfield right or not?
Is there something I'm missing?

Thank You for any help! 

**EDIT (What got me understanding the concept):**

What I got confused about the most was the simple fact, that for example a **String can store only "text"** and that if nil was something, it wouldn't be of type String, so therefore **it couldn't be "stored" inside String of course**.

Just in the same way, as I can't store text into an Integer — **I can't "store" nil into a String.**

But with Optional String I can use this **"wrapper"** that actually **CAN** "store" both types: string or nil. And that is what distinguish regular String from Optional String. It is this "wrapper" that has the ability to "hold" both these types until I need to use the actual value. (Now of course I understand, that nil is actually an absence of any value. I just used it in this way to illustrate the point)

Thank You everyone for Your help!