![]() |
In this article, we’ll learn to print a success message using the PHP. so basically we have to show the success messages on the screen. we can assume that the operation is done and now we need to print the success message. so we will learn some methods for this. These are the following methods: Table of Content Using echo or print statementsThis method directly displaying the success message by utilizing echo or print statements in PHP. The echo statement outputs the value of the $successMessage variable, which holds the success message, within the HTML markup. Consequently, the success message will appear in the browser if the $operationSuccessful variable is set to true. Example: This example shows the printing of success message using echo statement.
Output <div class="success-message"> Operation was successful!</div> Using a PHP variable in HTMLYou can store the success message in a PHP variable and then display it within your HTML markup. The PHP variable $operationSuccessful is assigned the value true, signifying that the operation has been completed successfully. Within the if block, the success message contained in the $successMessage variable is displayed inside a <div> element with the class “success-message”. As $operationSuccessful is true, the code inside the if block is executed, resulting in the success message “Operation was successful!” being displayed within the styled ‘<div>’ element. Example: This example shows the printing of success message using a PHP variable in HTML.
Output <div class="success-message">Operation was successful!</div> Using printf() functionThese functions enable string formatting with variables. Placeholders can be used in your success message and subsequently replaced with actual values through these functions. The PHP variable $operationSuccessful is set to true, signifying that the operation has been completed successfully. Inside the if block, the printf() function is used to format the success message. The %s placeholder in the $successMessage variable is replaced with the string ‘was’ using the printf() function. The message “Operation was successful!” is subsequently displayed within a <div> element assigned the class “success-message”. Example: This example shows the printing of success message using printf().
Output <div class="success-message">Operation was successful!</div> Using session variablesYou can save the success message in a session variable and display it on subsequent pages or upon page reloads. these are the steps to do that:
Example: This example shows the printing of success message using session variables.
Output <!-- On another page or after page reload --> <div class="success-message">Operation was successful!</div> Note: To use session in any page, we need to start the session on top of the page using the session_start(); |
Reffered: https://www.geeksforgeeks.org
Web Technologies |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |