In this article, you will learn about the basics of PHP syntax and how it can be embedded with HTML tags.
Standard PHP Syntax
Canonical PHP tags
A PHP script starts with the
<?php and ends with the
The PHP delimiter
?> in the following example simply tells the PHP engine to treat the enclosed code block as PHP code, rather than simple HTML.
<?php //PHP code to be execute ?>
<?php echo “Hello, world!”; ?>
PHP short open tags (SGML-style)
Short open tags in PHP starts with
<? and ends with
?>. To use short open tags in PHP, they must be enabled in php.ini file by setting on short_open_tag.
<? echo “Hello, world!”; ?>
The default file extension for PHP files is “.php”
- To save this “.php” file. Create any folder name you need to test PHP files in under the “htdocs” folder or simply click the button below and follow step 4.
Embedding PHP syntax within HTML
PHP files are plain text files with
.php extension. Inside a PHP file you can write HTML like you do in regular HTML pages as well as embed PHP codes for server-side execution.
Example: How to embed PHP with HTML
<!DOCTYPE html> <html> <head> <title>Try to Program</title> </head> <body> <h1>First PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
Note: PHP statements end with a semicolon (;).
What is happening?
?> tags and leave rest of the thing as it is. At the end the web server sends the final output back to your browser which is completely in HTML.
Comments in PHP
A comment is simply text that is ignored by the PHP engine. The purpose of comments is to make the code more readable and also can be used as reminders or descriptions of the purpose of the code written which makes it easy for us to navigate when we see the code after a long time.
Single-line comments in PHP
They are generally used for short explanations or notes relevant to the local code.
<!DOCTYPE html> <html> <body> <?php //This is a comment. Easy to share details # This is a comment too. echo “Hello, World!.’; ?> </body> </html>
Multi-lines comments in PHP
They are generally used to provide pseudocode algorithms and more detailed explanations when necessary.
<!DOCTYPE html> <html> <body> <?php /* This is a comment for multiple purpose Auhtor: james author Purpose: Multi purpose comment */ echo “Hello, World!.”; ?> </body> </html>
Case Sensitivity in PHP
Variables name are case-sensitive in PHP. Case-sensitive as uppercase and lowercase letters.
Here in following example $Action, $acTion and $action are treated as different variables.
<!DOCTYPE HTML> <html> <head> <title>Case Sensitivity in PHP</title> </head> <body> <?php // Assign value to variable $Action ="run"; $acTion ="stop"; $action ="hide"; // Try to print variable value echo"now you must " . $Action . "<br>"; echo "now you must " . $acTion . "<br>"; echo "now you must " . $action . "<br>"; ?> </body> </html>
But, built-in keywords like echo, while, else, if, etc , classes, functions and user-defined functions are not case-sensitive.
<!DOCTYPE HTML> <html> <head> <title>Case Sensitivity in PHP</title> </head> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>"; ?> </body> </html>