PHP in HTML
×
Home Courses Community Support
PHP in HTML

A PHP scripting block always starts with ‘<?php’ and ends with ‘?>’. A PHP scripting block can be placed anywhere in the document. On servers with shorthand support enabled you can start a scripting block with ‘<?’ and end with ‘?>’. For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form. A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. First have a look on simple HTML file then we’ll incorporate the PHP inside that.
 

<html>
<head>
<title>Title of my page</title>
</head>
<body>
This is the content of my page.
</body>
</html>


In our first step to learn PHP scripting language we will modify a regular HTML page and we will add to it PHP commands. To create a PHP page using the page above as our model, the first important action is to rename our file. In our case we will name the file "mypage.php". When a file with ".php" extension is placed in the server and the corresponding URL is visited, the server will process the file before sending it to the client, so that all PHP commands will be processed. We may just change the name of our file as proposed and save it to our server (without any additional changes), and when visiting the corresponding URL the result will be exactly the same (because there is nothing to be processed in the page-no PHP code is there).


Below, we have an example of a simple PHP script which sends the text "Hello World" to the browser:
 

<html>
<head>
<title>Title of my page</title>
</head>
<body>
This is the content of my page.
<?php
echo"Hello World! Do you like it? ”;
?>
</body>
</html>


When visiting the page, the server will process the page to execute all PHP commands in the page, which will be located within   ‘<?php’ opening tag and ‘?>’ closing tags. In our case “echo" command will write to the resulting page the text contained between brackets ("Do you like it?"). Often, instead of echo command, print command is used to output string. At the end of the line we will add ";" (other way we will get an error). We may use "print" command instead of "echo", and the result will be identical. And we may also include all the php commands in one line by placing the code bellow in our page.
 

<?php
print "Hello World! Do you like it?";
?>

 

Note:

  • Between opening and closing tags we may include several lines with executable commands.
  • Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.
  • The file must have a .php extension. If the file has an .html extension, the PHP code will not be executed.
  • In this page we will added PHP scripting language codes to our regular HTML page, after it we will learn how to write texts and how to concatenate them and write some special characters, and we will start using variables.

 

 

← Preview

First PHP Program

Next →

Introducing Variables

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App