The goal of this tutorial is to provide a comprehensive understanding of PHP syntax and tags. We will discuss the standard rules for writing PHP scripts and how to embed PHP in an HTML document.
By the end of this tutorial, you will be able to:
Basic knowledge of HTML is required. Familiarity with programming concepts would be beneficial but not necessary.
A PHP script can be placed anywhere in the document and starts with <?php
and ends with ?>
.
<?php
// PHP code goes here
?>
In PHP, all keywords, classes, functions, and user-defined functions are NOT case-sensitive.
<?php
echo "Hello, world!";
ECHO "Hello, world!";
EcHo "Hello, world!";
?>
All three echo statements above are legal and will print "Hello, world!".
Comments in PHP are similar to other programming languages. Comments can be single-line (// or #) or multi-line (/.../).
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a
multi-line
comment
*/
?>
PHP tags are the markers that tell where the PHP code begins and ends. There are four different pairs of opening and closing tags which can be used in PHP.
<?php //...?> Standard PHP tags.
<? //...?> Short open tags (only available if enabled in php.ini file).
<script language="php"> //... </script> Script tags.
<% //... %> ASP-style tags (only available if enabled in php.ini file).
It's recommended to use standard PHP tags as they always work regardless of the configuration settings.
<?php
echo "Hello, World!";
?>
In this code, <?php
is the opening tag for PHP code and ?>
is the closing tag. echo
is a language construct that outputs one or more strings. The string "Hello, World!" is printed when this script is run.
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to My Homepage</h1>
<?php
echo "Hello, World!";
?>
</body>
</html>
In this example, PHP code is embedded within an HTML document. When the script is run, it will display a webpage with the heading "Welcome to my Homepage" and the text "Hello, World!".
In this tutorial, we've learned about PHP syntax and tags. We've seen how to write PHP scripts and embed them in HTML. We've also discussed the case sensitivity in PHP and how to write comments in PHP.
Remember, practice is key in mastering any programming language. Happy coding!