Call Bootstrap Alert with PHP Function

PHP Bootstrap Alert in a Function

Here is a simple way I call Bootstrap Alerts using a PHP Function.

Here is how you normally create an alert using Bootstrap

Bootstrap Alert

<div class="alert alert-success" role="alert">
  <p>Hello World!</p>

Below is the PHP code that uses a function to generate the Bootstrap Alert

PHP Code

function displayAlert($text, $type) {

echo "<div class=\"alert alert-".$type."\" role=\"alert\">

Now whenever you want to display the alert you simply call the function.

You can change the style of the alert box with the second parameter.

  • success
  • info
  • warning
  • danger

Example of different alert styles


Call PHP function to display Bootstrap alert with customized message

displayAlert("Hello World!", "success");


Sample Output


This is a simple way to generate Bootstrap Alert boxes when you need them without having to type out the code for the alert box.

Hope this helps making your coding easier!