PHP Syntax

Hi everyone,

As you already know PHP codes are written in html codes. PHP codes are executed in server and returns back to browser as plain HTML. And you should also know that if you installed IIS like I did your .php files need to be located in C:\inetpub\wwwroot location. So compose your PHP files in that folder. For developing PHP pages you can use one of many text editors(I am using Notepad++) or simple text file and save it with .php extension. I named my php file as php1.php, give a name to yours also. So lets begin..

Basic Syntax

Php scripts always start with <?php and ends with ?> . A php script can be placed anywhere in the document. You can also write php scripts between <? and ?> tags but its not recommended so avoid from writing your scripts between these tags 🙂

Just to remember, lets write the main html tags:

<html>
   <head> </head>
   <body> </body>
</html>

Now lets output some text with php. Our php script will be located between <body> and </body> tags.

<html>
   <head></head>
   <body>
      <?php 
         echo "I am learning PHP";
         ?>
   </body>
</html>

The output for the code above will be I am learning PHP. How it works? On the server side the code between <?php and ?>tags is processed and returns the result to the browser as  I am learning PHP. I want you to see something very interesting. If you have come so far please right click on the page(mine was php1.php) in your browser that has the URL of localhost:/php1.php and click on View page source. You can’t see <?php echo “I am learning PHP”;?>, right? Instead, you see only I am learning PHP. That’s why we call it server side scripting language. Our php codes run on server and we see in our browsers only the result of our php code. Great, huh? 🙂

Your page source shoul look like that:

<html>
   <head></head>
   <body>
      I am learning PHP
   </body>
</html>

By the way, you should learn that 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. And we used echo command to get output text with PHP. We could also use print to do the same but mostly we use echo 🙂

Comments in PHP

We, programmers, write comments to make it clear what we are doing with writing lines of codes. In PHP there are two ways of doing this.

If you want to add a one-line comment, write your comment after //

E.x:

// this is a one-line comment

If you want to add comment block, write your comment between /* and */ 

E.x:

/* This

is a

comment

block */

Now, you know some PHP 🙂 Stay connected and learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *