Finally, in the third part we start learning PHP
Today we will be creating a simple script for checking user’s age, based on their input
With this explained and our environment set, off we go.
Table of contents
Today we will learn how to
- Display text and HTML in PHP
- Use
$_GET
superglobal and basics of form handling - Write basic conditional statements with
if-else
construct
So, maybe let’s start with executing code
Executing code in PHP
Do you remember part 1 of this course? We’ve wrote this code
<?php
echo "Hello World";
?>
And it displays Hello World
- but first we see <?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 in one file
And if we only have PHP code inside file (So no additional HTML) you can omit closing tag (?>
)
What does it mean? That code from above can also be rewritten as
<?php
echo "Hello World";
By the way, in older code you can find <? ?>
instead of <?php ?>
.
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
Now, to our code - 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>
(If you don’t understand content - go back to the previous part)
Now, after we run it - we should see this
(Note: Remember, that to use PHP you need to have your environment running and head to http://localhost/[path_to_your_file]
to execute it)
Okay, form is present
We can write our age in there, but how to check it?
PHP introduction
Let’s write the logic. Add php tags (<?php ?>
) after form
<?php
?>
Now inside it, we need to check if we got something from the form
if(!isset($_GET['age'])){
exit();
}
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
And let’s look at the if too - basic structure looks like this
if(condition){
code
}
Similar to other programming languages - this code means
If the condition is true, execute code
When we are sure, we have data to operate on - 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>";
}
Have a closer look at the condition
$_GET['age'] < 18
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?
But, what if we actually meet that condition? Let’s code it as well
Delete closing bracket, and add
}else{
echo "<p>Access granted - Welcome";
}
Now, we added an alternative. If 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 number
To check if something is a number we use is_numeric
function
In between those 2 ifs, add one more
if(!is_numeric($_GET['age'])){
echo "<p>Your age must be a number</p>";
exit();
}
Now, we have time to talk about that exclamation mark - !
symbolizes negation
So, while is_numeric($_GET['age'])
would mean
Is
$_GET['age']
numeric?
!is_numeric($_GET['age'])
means
Is
$_GET['age']
not numeric?
This !
represents not.
If we pass numeric value, is_numeric
will return true
.
If something is not true, it is false - and this will be returned value from !is_numeric($_GET['age'])
Congrats, we’ve done it - you have successfully wrote your first code in PHP
Our overall code looks like this
<!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>
<?php
if(!isset($_GET['age'])){
exit();
}
if(!is_numeric($_GET['age'])){
echo "<p>Your age must be a number</p>";
exit();
}
if($_GET['age'] < 18){
echo "<p>Access denied - You are too young to visit this website</p>";
}else{
echo "<p>Access granted - Welcome, this is only for adult users";
}
?>
</body>
</html>
Afterword
When everything has been configured - We’ve done our first PHP task
Since now, learning PHP will be our only focus here. No additional configs or other stuff
Thanks for reading, leave your feedback in the comments and that’s it
See you in next articles