Advertisement
Tech

How to Comment and Execute Your PHP Scripts on Linux

This article, among other things will show you how to comment code. You should learn the habit of writing comments in your code now! Commenting allows you to look at your code at a later date and understand what you were getting your code to do. After we have commented your code we’ll execute it!

By Berry van der Linden
Desk Tech
Reading time 3 min read
Word count 538
Linux Computing Linux support
How to Comment and Execute Your PHP Scripts on Linux
Advertisement
Quick Take

This article, among other things will show you how to comment code. You should learn the habit of writing comments in your code now! Commenting allows you to look at your code at a later date and understand what you were getting your code to do. After we have commented your code we’ll execute it!

On this page

/* Comments */

PHP comments are like HTML comments. PHP comments are indicated by certain characters, when the PHP code is parsed the text between the commenting characters will not be executed.

You should always comment your code! It is a way of documenting your code that will allow you to understand what your code does! Do you think you will remember what all your code does a few months from now?

Advertisement

To distinguish comments from code use either, // at the start of each line, or enclose the section of test you want as a comment with /* and */

For example:

Advertisement

// This is a comment. Below,

// is another comment.

Advertisement

echo ‘I will be output in the browser’;

/* I am a comment that

Advertisement

is spread across lines */

?>

Advertisement

Getting Your Hands Dirty!

No doubt you have probably skimmed through the above text and are eager/apathetic (you choose) to get coding some PHP scripts!

Begin by writing the following code in an empty .php file. Write what you like inside the quotation marks. Echo outputs what is inside the quotation marks to the web browser when you execute the script:

Advertisement

Advertisement

<?php

echo “This is my first script”;

Advertisement

?>

Advertisement

Save the file, and give it the suffix .php, for example, filename.php. If you’ve followed the instructions and installed a LAMP server, you need to save the file in the Apache server’s root folder, which, on a LAMP server is /var/www/.

Advertisement

Execute!

Now you need to open the file in your browser. Because your server is running locally on your machine, you have to enter the localhost URL followed by the name of your file.

Type “https://localhost” into the address bar of your browser and ta-daa! You’ll see the contents of your root folder. To open your script type the local host address followed by the name of your file, “https://localhost/filename.php” this will run your script and display the output in your browser!

Advertisement

If you have saved the file inside a sub-directory in the /var/www/ such as, /var/www/tutorials simply add the folder to the URL:

https://localhost/tutorials/filename.php

Advertisement

Well done! You just programmed and executed your first PHP script! You can now rest your weary head and fingers.

See You Later!

Finally, you got to play with some PHP code all by yourself! /* Awesome! */ In the next article we will take a look at variables, data types and maybe a brief look at some forms, if you are lucky!

Advertisement

This post is part of the series: A Guide to Linux Programming with PHP

In this series of articles I’ll be teaching you the basics of using PHP, and showing you why I think it’s the best language for web development. There are no pre-requisites for your level of knowledge, other than that you can understand basic HTML and are willing to learn.

  1. A Guide to Linux Programming with PHP
  2. A Guide to Linux Programming with PHP - Commenting and Executing Code
  3. A Guide to Linux Programming with PHP - Variables & Data Types
  4. A Guide to Linux Programming with PHP - HTML Forms and PHP
  5. A Guide to Linux Programming with PHP - Arrays
  6. A Guide to Linux Programming with PHP - Associative and Multidimensional Arrays
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux support
Advertisement