There are some basic rules and information we need to know before we start coding in PHP, before we move on to the next lessons or examples. Like what we need to do to start coding PHP, what we need to do after we start, and what we need to do after we finish.
When starting and finishing PHP code
In order for the codes we will write to be found and processed by the server, we need to put “<?php” at the beginning and “?>” at the end of the PHP codes.
Everything we write between these two tags is processed by the server as PHP code.PHP Code:
<?php
- //php codes are written here
- ?>
Semicolon
In PHP we have to put a semicolon (;) after each command (with some exceptions and parts).
This is because PHP takes all the code in one line and processes it. It understands that by dividing the code you wrote with semicolons, it moves on to the next command.
We can perceive this semicolon event as the soldiers saying "okay" when their conversation is over in their radio conversations. The term "ok", denoting the end of the command being said here, is a semicolon in PHP.
Displaying the results on the screen
While working in PHP, none of the codes we create or write are sent to the person entering the page. In other words, no one can see the codes we write, they can only see the values we want to print out of the codes.
To print out a value or result, we use the echo or print statement, but most often the echo statement is used. Sample;
Demo: https://www.jdoodle.com/a/5yAnPHP Code:
<?php
- echo "Hello World";
- ?>
When this code runs, only the text Hello World comes out.
If we do not use echo or print statements in our work, visitors to the page may encounter a blank page.
Echo literally means to reflect. In other words, it reflects the result of our work in PHP to the users through the codes.
Adding comments or notes inside the codes
In the long and dense codes we write, we sometimes need to add some explanations so that we or the people we share our codes with can understand the codes more easily. These notes we will add are not visible when we run the codes and do not affect the codes.
There are 3 different ways to add notes inside the code in PHP. Two of them are one-line annotation and the other one is multiple-line annotation.
To add a single line note with a hash (#) sign, it is sufficient to put one hash (#) sign per line. What we write after this mark is perceived as a note, not just that line.
We can perform the same event by using two slash (//) signs as the square sign works.
To add notes on more than one line, we start the note range with a slash and an asterisk (/*). Where we want the note range to end, we put an asterisk and a slash (*/) on the contrary. (/* not */) Example usages;
With these basics, you can move on to the next lessons and start messing things up.PHP Code:
<?php
- # a one-line comment
- // this is also a one line comment
- /* if it is
- more than one
- one line comment
- */
- ?>