DEV Community

Mateusz Jasiński
Mateusz Jasiński

Posted on • Updated on

PHP 0 to hero pt.3 - PHP basics

Hi, this intro won't be too long. Finally, in the third part we'll start learning PHP

Today we will be creating a simple script for checking user's age, based on their input

When we have this explained and our environment is set, off we go.

Table of contents

This article won't be too long - today we will learn how to

  1. Display text and HTML in PHP
  2. Use $_GET superglobal and basics of form handling
  3. Write basic conditional statements with if-else construct

So, maybe let's start with executing code

Executing code in PHP

Do you remember the code from pt.1 of this course? This one

<?php
echo "Hello World";
?>
Enter fullscreen mode Exit fullscreen mode

And it displays Hello World- but first we see this - <?php

What does it mean? It's the opening of php tag.

PHP will only get executed if it's inside <?php ?> tags - every file with PHP code has them inside.

There can be multiple PHP tags inside one file

And if we only have PHP code inside file (So no additional HTML) you can omit closing tag (?>)

What does it mean? It means that code from above can also be rewritten as

<?php
echo "Hello World";
Enter fullscreen mode Exit fullscreen mode

(Of course, only if this is only thing in the file)

By the way, in older codes you can see tags written like this <? ?>. These are short tags. For portability reasons, you shouldn't use them. Some servers have this function disabled by default - so your code won't work

So, let's create a new folder inside htdocs called age-checker,
add index.php file inside it and open it in your code editor

At first - I created a simple form

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Age checker</title>
</head>
<body>
    <h1>Are you old enough to access this page?</h1>
    <form action="" method="GET">
        <input type="number" name="age" id="" placeholder="Enter your age">
        <button type="submit">Check age</button>
    </form>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

If you don't understand content - check my recent article from this series

Now, after we run it - we should see this

Result of our HTML

(Note: Remember, that to execute PHP you need to have your environment running and head to http://localhost/[path_to_your_file] to execute it)

Okay, form is present - but what about it's logic?

Let's write it. Add php tags (<?php ?>) after form

<?php

?>
Enter fullscreen mode Exit fullscreen mode

Now inside it, we need to check if we got something from the form

if(!isset($_GET['age'])){
   exit();
}
Enter fullscreen mode Exit fullscreen mode

These lines mean "If $_GET['age'] is not set (!), finish the script"

What is this - $_GET['age']?

That's how we get data from GET form - inside single quotes we place name of the field. But about it, later in this course

And let's look at the if too - basic structure looks like this

if(condition){
    code
}
Enter fullscreen mode Exit fullscreen mode

Similar to other programming languages - this code means

If the condition is true, execute code

When we are sure, that code won't be executed. Let's start writing our conditions

First - If user isn't and adult (is less than 18 years old) display proper message

if($_GET['age'] < 18){
            echo "<p>Access denied - You are too young to visit this website</p>";
}
Enter fullscreen mode Exit fullscreen mode

We should look at the condition?

$_GET['age'] < 18
Enter fullscreen mode Exit fullscreen mode

Do you remember comparing 2 numbers in math? That's what we do here
That piece of code can be read as

Is $_GET['age'] smaller than 18? If yes, display <p>Access denied - You are too young to visit this website</p>

But, what if we actually meet that condition? Let's code it too

Delete closing brace, and add

}else{
    echo "<p>Access granted - Welcome";
}
Enter fullscreen mode Exit fullscreen mode

Now, we added a alternative. If that condition isn't met - execute this code. Let's add one more thing.

As developers we have to make our code as foolproof as possible. Someone may try to send us text instead of the number

To check if something is a number we use is_numeric function

Don't worry, if you can't understand something - I'll talk more about types of data in the next. But let's go back of the code

In between those 2 ifs, add one more

if(!is_numeric($_GET['age'])){
    echo "<p>Your age must be a number</p>";
    exit();
}
Enter fullscreen mode Exit fullscreen mode

What is this exclamation mark (!)? It's negation.
is_numeric gives us true or false

And !is_numeric($_GET['age']) means

Is $_GET['age'] not numeric?

This ! means not

And, that's it. The end of this article

Afterword

Congratulations, you have successfully wrote and (hopefully) understood PHP code.

We finally got to the point, where we write PHP. But don't worry, now we will be writing PHP 99% of the time

Yes, I know. It was a bit delayed. But here you have it

Thanks for reading, leave your feedback in the comments and that's it

See you in next articles. Pt.4 will be published next thursday

Top comments (0)