PHP syntax
Before starting programming in any language, the basic syntax and program structure is very important. This chapter introduces you to the basic PHP syntax, including: php tags, comments, print commands, echo commands, and two print and echo commands.
"Escaping to PHP" technique
PHP Parser needs a way to distinguish PHP code from other elements in the page. The technique to do this is known as "Escaping to PHP". There are 4 ways to do this:
Accurate PHP tags
The most popular and effective PHP tag style is:
To use this card, do one or two things to enable PHP to recognize these tags:
Choose the --enable-short-tags configuration option when you are building PHP.
Set short_open_tag settings in the php.ini file to on. This option must be disabled to parse XML with PHP, because the same syntax is used for XML tags.
ASP-style tag
ASP-style tags mimic the tags used by Active Server Pages to outline code blocks. ASP-style tags look like this:
<% .%>
To use an ASP-style tag, you will need to set configuration options in the php.ini file.
HTML script tags
The script tag in HTML looks like this:
Show text on browser - Print command and echo command
In PHP, you can use two print and echo commands to print a certain string screen. For new learners of PHP, these two commands are not much different and you can use any command.
For example:
PHP program example php echo "Example of echo command!
" ; print " Example of print print! " ; ?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
You notice the code above I use
to down the line. If not used
then these two lines of text will be printed together.
In other words, the print and echo commands are used to format the HTML section to display in the browser. You can format anything you want with the HTML in these two commands.
For example:
php echo "
Example command echo in PHP!
" ; //hoặc print "
The print command example in PHP!
" ; ?>
Save the program in a file named test.php in htdocs , then open the browser and type in the address http:/// localhost: 8080 / test.php will result (I minimize the browser):
You should note that the value of the attributes is enclosed in single quotes ('), but if to display quotation marks (""), you must use the forward slash () in front. Similar to the line break (with
), title and title with tags h1, h2, h3 .
You can also use the print << command
php print <<< EOF "QTM" EOF ; ?>
The difference between the print command and the echo command
In PHP, basically these two commands are quite similar, but sometimes you should also pay attention to the following two different points:
The print command is a function, when executed it returns a result of 1, otherwise it returns the result 0. Therefore, you can assign the result of this print command to a variable, while with the echo command not.
php $QTM = print 'abcd' ; $QTM = echo 'cdef' ; //sai ?>
If you run the above PHP code, it will give an error.
The print command can only be used with one parameter, while the echo command can be used with many parameters.
php echo 'v' , 'i' , 'e' , 't' ; //dung voi 4 tham so echo ( 'j' ),( 'a' ); //dung duoc cho dau ngoac kep tung tham so print 'c' ; //dung print 'k' , 't' ; //sai ?>
Commet in PHP
A comment is a part of the program that only works for the code reader and it will be omitted before displaying the program results. There are 2 types of comments in PHP:
Single-line comments - In general, they are often used for brief explanations or notes regarding internal code. Below is an example of a single-line comment:
php # Day la vi du mot comment # Vi du comment tiep theo // Vi du ve comment don dong khac print "Vi du minh hoa comment don dong" ; ?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Multi-line comment - Used to explain details when needed. This comment form is quite similar in C. This is an example of a multi-line comment.
php /* Ví dụ một comment da dong: Web: TipsMake.com Muc dich: minh hoa comment viet tren nhieu dong Ngon ngu: PHP */ print "Ví dụ minh họa comment đơn dòng" ; ?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
PHP is irrespective of whitespace
Whitespace is what you type without displaying on the screen such as: space, tab mark, or line break (end of a character line).
PHP does not distinguish whitespace, that is, there is no problem about how many whitespace characters you have in a row. A whitespace character is similar to many whitespace characters.
For example, the following PHP commands will assign a sum of 2 + 2 to the equivalent variable $ four:
$four = 2 + 2 ; // cac khoang trang don $four =< tab2 +< tab > 2 ; // khoang trang và tab $four = 2 + 2 ; // vi du viet mot dong code co the viet tren nhieu dong
PHP is case sensitive
PHP is a case sensitive language. You consider the following example:
php $vietjack = 98 ; print ( "Giá trị biến QTM la: $QTM
" ); print ( "Giá trị biến QTM la: $QTM
" ); ?>
Save the program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will produce the result.
PHP commands are expressions that are terminated by semicolons (;)
A command in PHP is any expression that is followed by a semicolon (;). Any valid PHP command sequence that is surrounded by PHP tags is a valid PHP program. Below is a typical PHP command that will assign a string to a variable of $ greeting:
$ greeting = "Chasing the PHP elephant!";
Expressions are combinations of tokens in PHP
The smallest blocks in PHP are tokens (which are indivisible), for example: numbers (3.14159), strings (.two.), Variables ($ two), constants (TRUE), and special words that make up PHP's own syntax such as if, else, while, for .
Parentheses create PHP blocks
Although commands cannot be combined like expressions, you can always place a sequence of commands surrounded by parentheses anywhere to create PHP blocks.
The following PHP commands are equivalent:
if ( 3 == 2 + 1 ) print ( "Hoc PHP tai QTM.
" ); if ( 3 == 2 + 1 ) { print ( "Hoc PHP" ); print ( " tat ca co tai QTM.
" ); }
Run the PHP script from the command prompt
Of course, you can run your PHP script from the command prompt (command prompt). You consider the following content in the test.php file.
php echo "Xin chao World - Xin chao PHP!!!!!" ; ?>
Now run this script as a command prompt as follows:
$ php test.php
It will result:
Hello World - Xin chao PHP !!!!!
Come here, hope you understand the basics of basic PHP syntax.
Follow tutorialspoint
Next article: Turning in PHP
You should read it
- How to add comments to Google Slides
- How to filter malicious comments on social networks
- How to hide comments on Threads, filter inappropriate comments Threads
- Guide to watching and managing video comments on Youtube
- Facebook tested the private comment feature, set the right to read comments
- Comment in C / C ++
- How to turn off comments in Facebook
- How to use comments, notes (comments) in Word
May be interested
- Bin () function in Pythonin python programming, the bin () function converts and returns the binary string of the given integer. so what is the syntax of bin () function, parameters and how to use bin ()? let's find out together.
- Include syntax in AngularJShtml does not support embedding html web pages in html pages. to achieve this function, the following methods can be used:
- All () function in Pythonin python, all () returns true when all iterable elements are true or iterable empty and return false in the remaining cases. how does the function all () have syntax, what parameters do we have, let's find out in this section.
- USER_NAME function in SQL Serverthis article will show you in detail how to use the function to handle the user_name statement in sql server with specific syntax and examples to better visualize and capture the function.
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- ABS function in SQL Serverthis article will show you in detail how to use the abs () handling function in sql server with specific syntax and examples to better visualize and capture functions.
- Basic Java syntaxa java program can be defined as a collection of objects, communicating through calling each other's procedures. now we will look at the class, object, procedure and instance variable / instance variable to consider their meaning.
- ManPage Help in Unixall unix commands come with arbitrary and mandatory functions. it is very common when you forget the full syntax of these commands.
- Syntax and description of search and reference functions in Excelthe following article summarizes the syntax and description of the search and reference functions in excel.
- Summary of functions that handle character strings in Excel, syntax and examplessummary of functions that process character strings in excel, syntax and examples will help you supplement important knowledge and commands with this powerful calculation tool. character string processing functions will help you extract characters anywhere in the text content.