Introduction to PHP, Using PHP and HTML on the same page

PHP from scratch, variables, operators, conditional structure and loops

In this tutorial, you will learn

How to code PHP and use variables and data, operators, conditional structure (If), and loops. We use PHP inside the HTML codes and try to use features provided by PHP to create dynamic HTML codes and we will know different between using PHP and not using it. If you already know these things, please go to the end of this page and see the next tutorial.

What you should know before starting?

You should know how to create a PHP file in your PC (windows for example) and run it in a local server on our own computer. If you don't know these things, please go back and read our "Start learning PHP from the very beginning" tutorial.

Let's get started

In our previous tutorial, we created a PHP file named "index.php" and ran it. Open that file in editor (notepad++) and change its code like below:

<html>
<head>
<title>PHP Programming</title>  
</head>
<body>
<p>Hello PHP!</p>
<?php
$a = '<p>Lets Start…</p>';
echo $a;
?>
</body>
</html>

Now, run this file using its address in the browser. I recommend using Chrome.

http://127.0.0.1/start/index.php

Hello PHP

Let explain what we did in the above code. We injected a piece of PHP code into the HTML codes. In line 7, we used "<?php". This means PHP codes started from here. In line 10, we used "?>". This means PHP codes ended here. So, to write PHP codes, you need to write them like this:

<?php
...
?>

Anything placed between these symbols, read and processed as PHP. So, using this method, we can use PHP codes inside the HTML codes.

In line 8, we defined a variable named "a" and we equated it with a text, "Lets Start...". Variable is like a container that we can put something in it and use it somewhere else. To define a variable in PHP, we need to use "$" and then the variable name. All PHP variables are created this way. There are different types of variables in the PHP language. For example, in one variable, we can add text and in another, we can add numbers. Fortunately, in PHP, unlike many other programming languages, we don't need to specify the type of variable in advance, and as soon as we assign a value to a variable, PHP itself determines the type of the variable based on that value. Now, what is the use case of this variable? We could show this text like the previous text "Hello PHP!". Let's look at another example.
Open "index.php" again and change it like this:

<html>
<head>
<title>PHP Programming</title>  
</head>
<body>
<p>Hello PHP!</p>
<?php
$a = '<p>Lets do math!</p>';
$b = 1;
$c = 2;
echo $a;
echo $b+$c;
?>
</body>
</html>

Execute the file.

Math in PHP

Let explain what we did in the above code.

In line 8, we changed the variable "a" a little bit (changed the text). Then in lines 9 and 10, we created 2 new variables named "b" and "c" and put a number in each of them. Then in line 12 we asked PHP to add the values in these two variables together. So PHP added the number "1" to "2" and showed the result which is the same as "3". The variable is useful here. Could you do this without using PHP and with simple HTML? Certainly not. We can easily see the difference between using PHP or not using it in this example. This way we can perform mathematical operations in PHP. Operations such as subtraction can also be implemented with the "-" operator. There are many other operators in PHP, but you don't have to learn them! Just know that you can do anything! Then when you need to do something in a project, just do a Google search for it. For example, search: "How can use Multiplication in PHP?"
You can easily see that you have to use "*" to multiply two numbers in PHP. I call this method "the art of searching!". In my opinion, this is the key to progress in programming. Let me explain this important point further.

The art of searching!

To learn any programming language, there are many training courses in the form of books, videos, articles on the website, and more. In most of these tutorials, at every step, they go into the details of that section and try to teach you everything. For example, when it comes to PHP operators, all existing PHP operators will be taught you at the same step with examples. I think this method is NOT good! Why? Because it's true that you learn everything there, but first, your teaching time becomes very long and you may get tired along the way. Another problem is that you learn everything, but 6 months later, when you want to use it, you forget it! When you can search for something, why keep it in mind?
My teaching and learning method is that I search, learn, and use everything when I need it. Of course, I'll forget about it in a few months, but I can search again whenever I need to! With this method, firstly, you learn different parts very quickly and go further, secondly, you do not involve your mind in boring details, and finally, you learn and use the details with the "knowledge of the day" when needed by searching. Programming languages are constantly changing and updating, and if you save something in your mind, it may not be usable for a few more months and new methods have replaced it.
You may not believe it, but I don't even know trivial things like writing a Do… While loop in PHP! I just know there is such a thing and it can be used. Then, when I need such a mechanism, by knowing that such a thing exists, I search for it and use it in less than 30 seconds in my own project.
In the same way, I have written the most complex applications and used them without any problems. The only important thing is that you should know that these mechanisms exist and can be used. In the example code above, you learned that in PHP you can use math. That's enough! Now, if someday you need to add two decimal numbers and round up the result to 2 decimal places, you should know that you can use mathematics in PHP. So you're looking for: How to add and round up two numbers to 2 decimal places in PHP

Go back to your "index.php" and change it like this:

<html>
<head>
<title>PHP Programming</title>  
</head>
<body>
<p>Hello PHP!</p>
<?php
$a = '<p>Lets use conditional statement!</p>';
$b = 6;
echo $a;
if ($b > 4) {
echo '<p>b is greater than 4</p>';	
} else {
echo '<p>b is smaller than 4</p>';		
}
?>
</body>
</html>

Execute the file.

Operators in PHP

Let see what happened in the code above. In line 9, we set the numerical value "6" to the variable "b". Then in line 11, we told to PHP that if the value of "b" is greater than "4", show the text "b is greater than 4", but if it is smaller, show the text "b is smaller than 4". Since we set the value of the variable "b" to be "6", the smart PHP realized that this number was greater than 4. So it showed us the right text. Now change the value of the variable b and set it to 2 and refresh the page once more.

Operators in PHP

Again, the smart PHP realized that number 2 was less than 4 and showed us the correct text. This structure is called a conditional structure in PHP and is one of the most widely used structures in all programming languages. For example, when you buy from a website, these are conditional structures that determine if you have made a successful payment. If you have made a payment, you will be told that the payment was successful. Else, it tells you your payment was not successful!

Open the "index.php" file again and change its code as shown below.

<html>
<head>
<title>PHP Programming</title>  
</head>
<body>
<p>Hello PHP!</p>
<?php
$a = '<p>Lets use Loops!</p>';
echo $a;
for ($i=0; $i<10; $i++) {
echo '<p>The i is '.$i.' now!<p>';	
}
?>
</body>
</html>

Execute the file.

For loop in PHP

Now I explain the code above. In line 10, we told to PHP that for the variable "i", which is 0, as long as it is less than 10, add a number to it each time, and show us a text each time which tell us what number exist in "i" at that moment.

In fact, we repeated this HTML code:

<p>The i is X now!<p>

10 times, dynamically which each time the "X" is different.

This structure is called the loop, which exists in all programming languages and is very widely used. Once again, pay attention to this code and try to change the part that says as long as "i" is less than 10. For example, put 5 instead and see the result. With loops, you can repeat a recipe over and over again. For example, 10 people have sent you a message on Facebook. Facebook uses loops to create your received message page and show you 10 messages. It may be a little complicated, but for now, know that in PHP you can repeat a command in a certain number. So wherever you need to repeat something, you need to know that the loops are waiting for you. In future tutorials, you will see so many loops and you will learn everything about them.

 

Challenge yourself

  1. Try using PHP to divide one number by another. Don't forget the art of searching!
  2. Create a text variable and a numeric variable and try to add them together. What will happen? Why?
  3. Try to write a conditional structure in which you check if a number is less than 4, then show a message, if it is greater than 4, then show another message, and if it is equal to 4, then show a different message. Then try it with different numbers.
  4. Write a loop in which a number, let say 4, add it 10 times to itself, and each time show result.

What’s next?

Create a shopping cart application with PHP

It may seem strange to you, but with the same things we have learned so far, we can write an online store with PHP! We learn everything we need using the "art of searching". This store may be simple, but all professional stores are the same and have been coded in the same way, with just a few more details...

Read next

Any Question?

Captcha