PHP syntax

Admin   PHP   72  2020-09-08 16:45:35

Before you start programming in any language, the basic syntax and program structure is a very important part. This chapter introduces you to basic PHP syntax, including: php tag, comment, print command, echo command, and comparison of print and echo commands .

 "Escaping to PHP"

PHP Parser needs a way to distinguish PHP code from other elements on the page. The technique for doing this is known as "Escaping to PHP". There are 4 ways to do this:

Legitimate PHP tag

The most effective and commonly used PHP tag styles are:

<?php...?>

If you use this style, you can be sure that your tags will always be interpreted correctly.

Brief opening tag (SGML-style)

These tags look like this:

<?...?>

To use this tag you do 1 or 2 of the following to enable PHP to recognize these tags:

Choose the --enable-short-tags configuration option when you are building PHP.

PHP syntax

PHP syntax

Set the short_open_tag setting 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

The ASP-style tag mimics the tags used by Active Server Pages to sketch blocks of code. The ASP-style tags look like this:

<%...%>

To use the ASP-style tag, you will need to set configuration options in the php.ini file.

HTML script tag

The script tag in HTML looks like this:

<script language="PHP">...</script>

Display text in the browser - print command and echo command
In PHP, you can use the print and echo commands to print to the screen a certain string. For those who are new to PHP, these two commands are not much different and you can use any command.

For example:

<html>
   
   <head>
      <title>PHP program example</title>
   </head>
   <body>
       <?php
         echo "Example echo command! <br>";
   
         print "Example illustrates the print command!";
       ?>
   </body>
</html>   

Save the above program in a file called test.php in htdocs , then open a browser and type http://localhost:8080/test.php will produce the following result:

PHP print and echo command

You notice in the above code that you use <br> to break the line. If <br> is not used , these two lines of text will be printed together.

In other words, the print command and the echo command are used to format the HTML portion for display in the browser. You can format anything you want with HTML in these two commands.

For example:

<?php
   echo "<p align='center'><font color='blue' size='25px'>PHP echo command example!</font></p>";
   // Or
   print "<p align='center'><font color='red' size='25px'>PHP print example!</font></p>";
?>

Save the above program in a file called test.php in htdocs , then open a browser and type http: // localhost: 8080 / test.php will produce the results (I minimize the browser):

PHP print and echo command

Note that the attribute values ​​are enclosed in single quotes ( ' ), if to display double quotes ( "" ), you must use a forward slash ( \ ). Similar to line breaks (with <br> ), headers and headers with tags h1, h2, h3 ...

Alternatively you can also use the print <<< EOF . This command will print any character after the EOF to the browser, whatever it is.

<?php
   print <<<EOF
   "Learn PHP basic and advanced!!!"
   EOF;
?>

What is the difference between the print command and the echo command?

In PHP, basically these two commands are quite similar, but sometimes you should pay attention to the following two differences:

The print command is a function, when executed it returns 1, otherwise it returns 0. Therefore, you can assign the result of this print command to a variable, while with the echo command. not.

<?php
   $hello = print 'abcd';
   $work = echo 'cdef'; // False
?>

If you run the above PHP paragraph will give an error is Parse error: syntax error, unexpected 'echo' ...

The print command can only be used with one parameter, while the echo command can be used with multiple parameters.

<?php
   echo 'v','i','e','t';
   echo ('j'),('a');    
 
   print 'c';        
   print 'k','t';   
?>

Commet in PHP

A comment is a part of the program that is only available to the reader of the code and it will be omitted before displaying the program's results. There are two types of comments in PHP:

Single-line comments - In general, they are often used for brief explanations or notes regarding internal code. Here is an example of a single line comment:

<?php
   # Here is an example of a comment
   # Second comment example
   
   //Another single line comment example
   print "Example illustrating single line comment";
?>

Save the above program in a file called test.php in htdocs , then open a browser and type http: // localhost: 8080 / test.php will produce the following result:

Comment in PHP

Multiline Comment - Used for detailed explanations when needed. This type of comment is quite similar to that in C. Here's an example of a multi-line comment.

<?php
   /* Example illustrating single line comment
   */
   
   print "Example illustrating single line comment";
?>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost:8080/test.php will produce the following result:

Comment in PHP

PHP is space-insensitive
Spaces are things you type that are not displayed on the screen such as spaces, tabs, or line breaks (end of a line of characters).

PHP is whitespace insensitive, that is, it doesn't matter how many whitespace characters you have in a row. A whitespace character is similar to many whitespace characters.

For example, the following PHP statements will assign the sum of 2 + 2 to the variable $ four as equal:

$four = 2 + 2; 
$four <tab>=<tab2<tab>+<tab>2 ;
$four =
2+

2;

PHP is case sensitive

PHP is a case sensitive language. Consider the following example:

<html>
   <body>
      
      <?php
         $hello = 98;
         print("The hello variable value is: $hello <br>");
         print("The hello variable value is: $hello <br>");
      ?>
      
   </body>
</html>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost:8080/test.php will produce the following result:

Case sensitive in PHP

Commands in PHP are expressions terminated by a semicolon (;)

A statement in PHP is any expression followed by a comma (;). Any valid PHP script surrounded by PHP tags is a valid PHP program. Here is a featured PHP command that will assign a string of characters to a variable, $ greeting:

$greeting = "Welcome to PHP!";

Expressions are combinations of tokens in PHP

The smallest blocks in PHP are tokens (which cannot be divided any more), e.g. numbers (3.14159), strings (.two.), Variables ($ two), constants (TRUE), and special words that make up the syntax of PHP such as if, else, while, for, ...

The braces create PHP blocks

Although statements cannot be combined like expressions, you can always place a series of statements surrounded by braces anywhere to create PHP blocks.

The following PHP commands are equivalent:

if (3 == 2 + 1)
   print("Welcome to PHP<br>");
   
if (3 == 2 + 1)
{
   print("PHP");
   print(" Welcome to PHP<br>");
}

Run PHP script from command prompt

Of course, you can run your PHP script from the command prompt. Consider the following content in the file test.php.

<?php
   echo "Hello World - Hello PHP!!!!!";
?>

Now, run this script as a command prompt like so:

$php test.php

It will produce:

Hello World - Hello PHP!!!!!

By now, hopefully you have a basic understanding of PHP syntax.