How to make comments in PHP

Author: Sara Rhodes
Date Of Creation: 16 February 2021
Update Date: 1 July 2024
Anonim
Create a custom comment section using PHP - PHP tutorial
Video: Create a custom comment section using PHP - PHP tutorial

Content

A comment is a kind of note that can be used to explain the purpose and meaning of a code snippet. Working in PHP, you will have several options for writing comments that have come from the old well-known programming languages: by entering single-line or multi-line C comments. You can use comments to keep a piece of code from working, or just to keep records.

Steps

Part 1 of 2: Styles

  1. 1 Single line comments for short posts. If you need to leave a short comment, use the one-line comment code. The comment will only extend to the end of a line or the end of a block of code. Such comments only work within PHP tags and will only be read if placed in HTML.

    ? php // This is the standard (C ++) way of creating a single line comment # You can also use this Unix style to create a single line comment?> var13 ->

  2. 2 Use multi-line comments to write long comments or test code. Multi-line comments are very useful for writing long explanations and for preventing a segment of code from being processed. Read the Usage section for some tips on using multi-line comments.

    ? php / * Here's how to format a multi-line comment. All text up to the end of the tag will be included in the comment * / / * Some people like to include * at the beginning of each line * extra bullets. This will improve the * readability of large comments, although it is not required. * /?> var13 ->

Part 2 of 2: Using

  1. 1 Use comments to leave notes on code health. You don't have to do this for every line of code, as other programmers can easily figure out well-written code. Comments are useful when your code is performing non-standard or not obvious functions.

    // Create a curl request $ session = curl_init ($ request); // Tell curl to use HTTP POST curl_setopt ($ session, CURLOPT_POST, true);

  2. 2 Leave comments so you don't forget what you did with the code. As you work on a project, comments will keep you from forgetting where you left off. Leave comments for code that is not working correctly or that you have not finished yet.

    // Before moving on to the next step, double-check the output of this program echo "Hello World!";

  3. 3 Leave a comment for the code you plan to share. If you plan to collaborate with other programmers or are going to make your code publicly available, comments will help others understand how your code works and what needs to be fixed.

    / * Is there a better way to accomplish this? * / Gender: input type = "radio" name = "gender"? Php if (isset ($ gender) && $ gender == "female") echo "checked"; ?> value = "female"> Female input type = "radio" name = "gender"? php if (isset ($ gender) && $ gender == "male") echo "checked";?> value = "male" > Male

  4. 4 Leave comments to undo specific blocks of code. This is quite useful when you are testing your code and want to stop a piece of it from running. Anything included in the comment mark will be ignored when the page starts.

    ? php echo "/ * Hello * / World!"; / * When the above code is run, the word "Hello" will not be reflected * /?> Var13 ->

  5. 5 Be careful when commenting out large blocks of code. The commenting function will end when the first completion mark is active, so if there is a multi-line comment inside the code that you have already commented, it will only stretch to the beginning of the original comment.

    ? php / * echo "Hello World!"; / * This comment will ruin everything * / * /?> Var13 ->

    ? php / * echo "Hello World!"; // This comment will be fine * /?> Var13 ->

  6. 6 Use comments to create pseudo recordings. You can use some creative code formats to create a code entry right inside it. This is pretty useful for open source projects.

    ? php // ============= // HEADER // ============== // ------------- // Subtitle // ------------- / * Section name * / # Entries can be written here # The second part can be written here / * * Use this to explain * why would you need multiple lines * or even several points of explanation * /?> var13 ->

Tips

  • HTML and PHP comments are different from each other, so when working with scripts (a combination of HTML and PHP), make sure to use the correct syntax.
  • For example, the following code contains an HTML comment, but it still executes the PHP code. If you put an HTML comment inside a PHP tag, it will result in an error.
    • ! - div id = "example">? php echo ’hello’; ?> var13 -> / div> ->