PHP 8 Code Syntax
For learning PHP, you should have basic knowledge of HTML and CSS.
PHP is a Hybrid language. It has both procedural and object-oriented features. It takes most of the syntax from other languages such as C, Perl, Shell, Java. PHP Syntax is very simple and easy to use. We can write PHP code in an HTML file.
Types of PHP tags -
|PHP Standard Tags||
|These are more compatible, portable and mostly used tags.|
|PHP Short Tags||
|It requires shorthand tags or short_open_tag to be ON on php.ini setting. We generally avoid using these tags because this creates a problem in XML parsing. As XML has the same syntax, so they have a major drawback of conflicting with XML headers.|
|PHP Script Tags||
|These tags are removed from PHP 7. These are less portable and generally not recommended.|
|PHP ASP Tags||
|It requires to ON the asp tags in the php.ini configuration.|
All the PHP Code is written between the start and end tags.
The PHP processor parses the code between these tags. Everything outside of these tags are ignored by the PHP Parser.
We can ignore the closing tag if the file contains only PHP Code.
PHP File Extension
The PHP file extension is ".PHP".
Example - index.php, introduction.php, aboutus.php
- Like other languages, PHP script is made up of statements, variables, functions, loop structure and so on.
- Each PHP statement is terminated with a semicolon.
Like- echo 'Hello World';
- PHP use '=' as the assignment operator.
Like- $var = 'Dog';
- PHP displays a statement by using ECHO and PRINT.
Like - echo $var;
print "Hello World";
- PHP code ignores whitespace between statements.
Adding PHP to HTMLThis is our basic first program. Open a new file in your preferred editor and type -
<html> <head> <title>PHP Basic Example</title> </head> <body> <?php echo 'Hello World'; ?> </body> </html>
Let us save the above script as sample.php and start the server installed on your machine, suppose the webserver is running at -
Output of the above code is - Hello World.