PHP Basics

We are going to learn basic syntax to write simple program in PHP and how to execute it. We will also learn some more basics of PHP scripting language in this PHP BASICS tutorial.

PHP scripts are executed on server and the output is sent in the form of plain text to browser.

  • Some basic Concepts:
  • Some basic concepts useful in writing PHP programs are as follows:

    • PHP programs are written in simple text editors like notepad++.
    • These programs contain HTML tags and some PHP code in it or they can contain only PHP logic.
    • When PHP code is embedded in HTML file, it can be written in head section as well as body section using the PHP script tag
      <?php?>

      .

    • If the PHP code is written in a separate file, then that PHP code can be used into the HTML file by linking them to each other.
    • PHP code can be written anywhere in the program using the PHP script tag. i.e.
      <?php?>

      .

    • The PHP script tags starts with
      <?php

      and ends with

      ?>

      . The ending tag (?>) is optional.

    • The PHP code is written in between these start and end tags.
    • PHP programs are saved with .php extension.
    • Each line of code in the program is known as a statement.
    • Every statement in PHP code is terminated with semicolon (;).
    • The ending PHP script tag (?>) also implies a semicolon, so it doesn’t matter if you don’t terminate the last statement of PHP code with a semicolon (Remember the last statement which is before the ending PHP script tag (?>).
  • Writing a simple PHP program:
    • Let us write a simple PHP program that welcomes us to learn PHP.
    • Open your notepad++ document and save it with a name index.php on desktop. While saving select the Save as type as PHP Hypertext Preprocessor file.
    • Write the following code in this index.php file:
    • <!DOCTYPE html>
      <html>
      <head>
      <title>
      	Simple PHP Program
      </title>
      </head>
      <body>
      <h1>PHP</h1>
      
      <?php
      	echo "WELCOME TO PHP COURSE.";
      ?>
      </body>
      </html>
    • Here, the index.php file contains some HTML tags and some PHP code.
    • In the body section we have text PHP written in h1 heading tag.
    • Then we have the PHP code inside the PHP script tag i.e
      <?php?>

      .

    • It contains a statement
      echo "WELCOME TO PHP COURSE.";
    • The word echo is a keyword used to display text on the webpage.
    • The text to be displayed is enclosed in double quotes or single quotes and the statement terminates with a semicolon.
    • The syntax of echo statement is given below:
    • echo “some text”;

      OR

      echo ‘some text’;
  • Executing the program:
    • We all remember that our program index.php is saved on the desktop.
    • Let us see if it prints WELCOME TO PHP COURSE.
    • Run the file index.php with Google Chrome as shown below:
    • run_php_program
      fig 1

    • Go to Run menu and click on Launch in Chrome option in the dropdown menu or else you can simply press Ctrl+Alt+Shift+R shortcut key.
    • The index.php page will open in the browser as shown below:
    • desktop_php_file_output
      fig 2

    • Here, the output shows only the text PHP which was enclosed in the h1 heading tag.
    • The code enclosed inside PHP script tag is not at all executed. We can conclude this because the statement WELCOME TO PHP COURSE is not displayed in the above output.
    • We had learned to install xampp server in the last lesson and we had successfully installed it.
    • Copy the index.php file from desktop.
    • Now in our C drive we have a folder named xampp. Open that folder.
    • Search for htdocs folder in the xampp folder and open it.
    • Create a new folder named Simple_PHP in htdocs folder and paste the copied index.php file inside it.
    • Now open your browser and type localhost/Simple_PHP in the address bar of the browser.
    • Remember the file having name index always gets executed automatically as soon as the folder name containing it is given in the address bar of the browser. In this example we have provided the folder name Simple_PHP which automatically calls index.php.
    • You will see the following output:
    • htdocs_php_file_output
      fig 3

    • Here, you got the PHP code output.
    • This indicates that PHP code files require a server for execution.
  • List of steps to write and execute a PHP program:
    1. Write the program.
    2. Save it with .php extension.
    3. Create a new folder in htdocs folder which is inside xampp folder in C drive.
    4. Copy the program file to be executed and paste it inside the newly created folder inside htdocs folder.
    5. Open the browser and write localhost/newly_created_folder_name in its address bar.
  • Comments in PHP:
    • Comments in any language are the statements that are not read or not executed.
    • These are just for information of the programmer.
    • Comments can be used by the programmer in a program to understand his program after a long period of time. I mean if the programmer had written a program and again wants to review it after 6 months or a year. He can understand the program if the purpose of the code is written in the form of comments.
    • A programmer can also write comments to remember the work he wants to do further in a project.
    • Comments can also be used to test the project. I mean, a programmer can test some part of code by commenting the other.
    • There are two types of comments in PHP.
      1. Single line comment
      2. Multiple line comment
    • The single line comment disables a single line of code. A single line can be commented using double slashes (//) or a hash sign (#).
    • The multiple line comment disables a block of code. Multiple lines of code can be commented using (/*——-*/). The hyphens in between indicates lines of code.
    • The multiple line comment starts with a slash followed by an asterisk (/*) and ends with an asterisk followed by a slash (*/).
    • Let us see an example:
    • Modify the file index.php in the Simple_PHP folder executed before as shown below:
    • Remember the changes have to be done in the copy of file inside htdocs folder.
    • <!DOCTYPE html>
      <html>
      <head>
      <title>
      	Simple PHP Program
      </title>
      </head>
      <body>
      <h1>PHP</h1>
      
      <?php
          echo "WELCOME TO PHP COURSE. <br><br>"; 
      	
          echo ‘This is my first PHP program. <br><br>’;	
      	
          echo "PHP is a server side scripting language.<br>";	
          echo 'Every statement in PHP should terminate with a semicolon.';
      	
      ?>
      </body>
      </html>
    • In the file index.php we have just added some more echo statements.
    • You can add HTML tags like brake tag, horizontal line tag inside the echo statement.
    • The output of the above code is shown below:
    • output_before_comments
      fig 4

    • Now we will apply comments to the PHP code block as shown below:
    • <?php
           //echo "WELCOME TO PHP COURSE.<br><br>";
      	
              echo 'This is my first PHP program. <br><br>';#echo is used to print a statement.
      	
           /*
              echo "PHP is a server side scripting language.<br>";	
      	echo 'Every statement in PHP should terminate with a semicolon.';
           */
      ?>
    • Here, we notice that the first welcome statement is commented using double slash (//) single line comment.
    • Information about the echo statement is provided using a hash (#) single line comment.
    • Next the remaining two statements are commented at a time using the multiple line comment.
    • Now let us see the output:
    • output_after_comments
      fig 5

    • In the above output only the statement This is my first PHP program is shown.
    • This shows that the rest of the commented statements were not executed.

Rest of the basic things in PHP are its variables, data types, keywords, loops, functions etc. We will study each of them in detail in further sections.

Thus we studied how to write and execute a PHP program in this PHP Basics tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -