What is PHP
PHP is the back-end language in website programming. It is said that it is easier to learn than other languages not only because of its simple syntax, but also with fewer constraints.
PHP is used to create many different web applications, including WordPress, thanks to easy integration with databases such as MySQL.
Difference between HTML / Javascript and PHP
While PHP is used to create “dynamic web” pages, HTML is often used as the markup of “static” web pages.
With these two terms, “static web” is a website with constant content, while “dynamic web” can be used to display content that changes based on a number of conditions, such as access rights. , browsing time … etc.
Simple examples such as the number of comments or interactions on Facebook, these numbers change over time and depending on the article, have been extracted by PHP, and displayed as HTML.
PHP can extract HTML used to display on the user interface, but more specifically, the ability to customize and embed PHP code between HTML elements in the interface file.
As a result, developers don’t need to separate HTML and PHP files, why split into two when we can use 2 in 1, right?
Another indispensable component of the common website is Javascript, which also plays a role of customizing and changing the interface.
PHP is called server-side language, Javascript is called client-side language.
The reason is that the PHP code is executed at the server, as soon as the request is executed and before the interface is returned to the user, while Javascript must wait to be loaded when the interface has been returned.
How PHP can be used
PHP is often used on a variety of platforms, ranging from sweeping to small components on web applications, from creating forums, forms, and shopping carts.
For example, users can enter the amount of goods they want to buy when paying online, and PHP can be used to handle this parameter.
Some code rules of PHP
The file extension used is .php
The default files are treated as PHP code with the file extension defined as “.php”. As mentioned above, PHP can be integrated into the following HTML code, for example a file named “index.php”
1 2 3 4 5 6 7 8 9 | <html> <body> *** mã PHP *** </body> </html> |
Where to place the PHP code
The PHP code is placed between the preamble on line 1 and the ending in line 3 as follows
1 2 3 4 | <?php echo ‘Hello World’; ?> |
The above code will display the word “Hello World” on the screen
Put semicolons at the end of each line of code
PHP specifies that each line of code needs to have a semicolon
1 2 3 4 5 | <?php echo ‘Message 1’; echo ‘Message 2’; ?> |
In the above example, the words “Message 1” and “Message 2” will be displayed on the screen one by one, you can see that each echo line ends with a semicolon.
How to comment code
Comment lines are comments that support reading code.
Writing comment code is not always required, but writing them can help with future reviews.
There are 3 ways to write comments in PHP code as follows:
- Type “//” at the beginning of the line
- Type “#” at the beginning of the line
- Or enclose the comment between “/ ” and “ /”
1 2 3 4 5 6 7 8 9 10 | <?php echo ‘Hello World’; // Ghi chú 1 # Ghi chú 2 /* Ghi chú 3 này có nhiều dòng */ ?> |
Basic syntax of PHP
Here are some basic syntax of PHP
Declare variable
When writing software, there are reusable information strings that are stored as variables.
Variables can be used to store most types of data, from lengthy text strings, to numbers, true false (Boolean), etc.
However, PHP has a number of predefined variables that cannot be overridden, such as $ GLOBALS, or $ _GET.
For example, we have the following code:
1 2 3 4 5 | <?php $name = ‘Songoku’; echo $name; ?> |
Then, the text “Songoku” will be displayed on the interface, because it is stored as a string into the variable “$ name”, the variable “$ name” can then be changed according to the application’s logic.
There are some rules for defining variables in PHP as follows:
- Starts with dollar characters ($)
- But the characters allowed for variable names include az, AZ, 0-9 and _
- Cannot name variables starting with digits 0-9
- Japanese can also be used as a variable name, but should not be so
Variable names are not too strict, however, they should be set clearly, understandably and properly the nature of the variable.
Operators
Operators in PHP are symbols that can be used in calculations.
There are arithmetic operators like addition and subtraction, division, operators to compare, or operators for inference …
For example, we have the following code:
1 2 3 4 5 | <?php $sum = 2 + 3; echo “2 + 3 = $sum”; ?> |
The text “2 + 3 = 5” will be displayed on the interface.
If statement / while statement
The “if” code is usually used to check if the next part has a valid value (TRUE) or not, and will execute the code inside if it meets the above condition.
1 2 3 4 5 6 7 8 9 | <?php $condition = 3; If ($condition === 3) { echo “Thỏa điều kiện”; } else { echo “Không thỏa điều kiện”; } ?> |
Thus, the text “Condition” will be displayed for the value of the variable “$ condition” equal to 3.
The while code is used to run code repeatedly, until the while condition is no longer satisfied.
1 2 3 4 5 6 7 8 | <?php $increment = 1; while ($increment <= 5) { echo “Chạy 5 lần”; $increment++; } ?> |
The text “Run 5 times” will display exactly 5 times on the screen, until the variable “$ increment” is increased by 1 after 5 loops and the final value after 5 increases is greater than 5.
Function declaration
Function in PHP is a combination of many code grouped and can be executed completely when the function is called.
For example, we have the following sum function:
1 2 3 4 5 6 | <?php function sum ($a, $b) { return $a + $b; } ?> |
When calling the “sum” function as follows, we get the sum of 2 numbers is 5 and is displayed in the interface:
1 2 3 4 | <?php echo sum(2, 3); ?> |
summary
When it comes to programming, people often think it’s something only experts can do, but using PHP is not as difficult as you think. Hopefully this article will help you get an easy start with getting started with programming in general, PHP in particular.