comment code php code example
Example 1: php comment
Example 2: comment in php
Comments in PHP can be used for several purposes, a very interesting one being that you can generate API documentation directly from them by using PHPDocumentor (http://www.phpdoc.org/).
Therefor one has to use a JavaDoc-like comment syntax (conforms to the DocBook DTD), example:
<?php
?>
Some basic html-like formatting is supported with this (ie <br> tags) to create something of a layout.
Example 3: comment in php
Comments do NOT take up processing power.
So, for all the people who argue that comments are undesired because they take up processing power now have no reason to comment ;)
<?php
echo microtime(), "<br />";
echo microtime(), "<br />";
echo microtime(), "<br />";
echo microtime(), "<br />";
?>
They take up about the same amount of time (about meaning on a repeated testing, sometimes the difference between the control and the test was negative and sometimes positive).
Example 4: php code for comment system
if (isset($_POST['comment_posted'])) {
global $db;
$comment_text = $_POST['comment_text'];
$sql = "INSERT INTO comments (post_id, user_id, body, created_at, updated_at) VALUES (1, " . $user_id . ", '$comment_text', now(), null)";
$result = mysqli_query($db, $sql);
$inserted_id = $db->insert_id;
$res = mysqli_query($db, "SELECT * FROM comments WHERE id=$inserted_id");
$inserted_comment = mysqli_fetch_assoc($res);
if ($result) {
$comment = "<div class='comment clearfix'>
<img src='profile.png' alt='' class='profile_pic'>
<div class='comment-details'>
<span class='comment-name'>" . getUsernameById($inserted_comment['user_id']) . "</span>
<span class='comment-date'>" . date('F j, Y ', strtotime($inserted_comment['created_at'])) . "</span>
<p>" . $inserted_comment['body'] . "</p>
<a class='reply-btn' href='#' data-id='" . $inserted_comment['id'] . "'>reply</a>
</div>
<!-- reply form -->
<form action='post_details.php' class='reply_form clearfix' id='comment_reply_form_" . $inserted_comment['id'] . "' data-id='" . $inserted_comment['id'] . "'>
<textarea class='form-control' name='reply_text' id='reply_text' cols='30' rows='2'></textarea>
<button class='btn btn-primary btn-xs pull-right submit-reply'>Submit reply</button>
</form>
</div>";
$comment_info = array(
'comment' => $comment,
'comments_count' => getCommentsCountByPostId(1)
);
echo json_encode($comment_info);
exit();
} else {
echo "error";
exit();
}
}
if (isset($_POST['reply_posted'])) {
global $db;
$reply_text = $_POST['reply_text'];
$comment_id = $_POST['comment_id'];
$sql = "INSERT INTO replies (user_id, comment_id, body, created_at, updated_at) VALUES (" . $user_id . ", $comment_id, '$reply_text', now(), null)";
$result = mysqli_query($db, $sql);
$inserted_id = $db->insert_id;
$res = mysqli_query($db, "SELECT * FROM replies WHERE id=$inserted_id");
$inserted_reply = mysqli_fetch_assoc($res);
if ($result) {
$reply = "<div class='comment reply clearfix'>
<img src='profile.png' alt='' class='profile_pic'>
<div class='comment-details'>
<span class='comment-name'>" . getUsernameById($inserted_reply['user_id']) . "</span>
<span class='comment-date'>" . date('F j, Y ', strtotime($inserted_reply['created_at'])) . "</span>
<p>" . $inserted_reply['body'] . "</p>
<a class='reply-btn' href='#'>reply</a>
</div>
</div>";
echo $reply;
exit();
} else {
echo "error";
exit();
}
}