
Hi there. At first, PHP comment lines might seem unnecessary, but they’re actually very important. You realize their value when you revisit your code years later. I know you're probably thinking "Sounds nice, but is it really needed?" — I can almost hear you!
Why Are Comment Lines Used in PHP?- To highlight certain parts while writing code;
- To easily recall what the code does when you look back at it later;
- To organize and improve your code structure;
- To help others understand your code more easily when shared;
- To include copyright and license information in your code;
- And for people like me, to disable some lines after seeing their output :)
You can use them for all these purposes — it's completely up to you. Let’s learn and use them together.
In PHP, you can add comments using #, // for single lines and /**/ for multiple lines. Let me explain these briefly.
Lines starting with # and // are comment lines. Anything you write after these symbols won’t be treated as code. However, PHP will try to parse it if these markers are missing, and will throw an error. Their effect ends once the line ends. For longer comments, you can use /**/.
With /**/, you can write as much as you like between the stars. For example:
[php]<?php /* system_xxxx.php Copyright (c) 2018 Netopsiyon All rights reserved. */ [/php]These lines are comments. They don't have any functional impact. But yes, they look a bit messy here. Let’s style them a bit and add some license info to make them prettier.
[php]<?php /* * system_xxx.php * * Copyright (c) 2018 Netopsiyon * * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */[/php]Looks better now, right? :) This is completely up to your imagination. But if you’re dealing with TSE, documentation, or certification, you’ll want to format it more properly. How? Look below:
[php]<?php ############################################################ # Project Name: # Creation Date: # Revision Date: # Revision No: # Prepared By: # Checked and Approved By: # MD5SUM: # File Name: # Purpose: #############################################################[/php]If you don’t include this kind of block, you'll have a hard time getting certified. I got this template from someone visiting for a TSE audit :) Of course, this isn’t all — you may also need to prepare documents for Alpha and Beta tests, etc. If you need help in that area, consider consulting a trademark or patent firm. That’s not really my area :)
These kinds of comment blocks are usually placed at the top of the file.
Earlier I said something like “And for people like me, to disable some lines after seeing their output” — now let me explain that.
Let’s say we wrote an SQL query like this:
[php]$sql='SELECT * FROM lisanslar WHERE kimlikkodu=''.$_POST['kimlik'].'' and tarih=''.$_POST['tarih'].''';[/php]Right after that, we echo the query to see if it's written correctly and whether the POST data is arriving properly:
[php]$sql='SELECT * FROM lisanslar WHERE kimlikkodu=''.$_POST['kimlik'].'' and tarih=''.$_POST['tarih'].'''; echo $sql;[/php]You can copy-paste this output into phpMyAdmin or MySQL Workbench to test the query. Then, simply comment out the echo $sql; line:
[php]$sql='SELECT * FROM lisanslar WHERE kimlikkodu=''.$_POST['kimlik'].'' and tarih=''.$_POST['tarih'].'''; //echo $sql;[/php]Now the SQL won't print to the screen anymore. Once you're done with the file, you can remove the lines you've disabled. Because anything inside comment markers won't execute. Or you can do the same using /* */ like so:
[php]$row=mysqli_fetch_array($sql); echo '<pre>'; print_r($row); echo '</pre>'; [/php]You checked the output, everything looks good. Let’s now disable those lines:
[php]$row=mysqli_fetch_array($sql); /* echo '<pre>'; print_r($row); echo '</pre>'; */ [/php]REMEMBER: Anything written inside comment blocks won’t show up when the page is requested via a browser.
I think this should be enough for now. If you have questions, feel free to drop them in the comments!
Related Articles
