Open In App

PHP | highlight_string() function

Last Updated : 06 Aug, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The highlight_string() function is an inbuilt function in PHP which is used to highlight the text string. It returns output in HTML text format. Syntax:
highlight_string( $string, $return )
Parameters: This function accepts two parameters as mentioned above and described below:
  • $string: It is the required parameter which specifies the text to be highlighted.
  • $return: It is the optional parameter. If it is True then it provides a highlighted code as a string, instead of printing it on the screen.
Return Value: If it is True then it returns highlighted code as a string, otherwise false on failure. Below programs illustrate the highlight_string() function in PHP. Program 1: php
<?php
highlight_string('Welcome to Geeksforgeeks');
?>
Output:
<code><span style="color: #000000">
<span style="color: #0000BB"><?php phpinfo</span><span style="color: #007700">();
 </span><span style="color: #0000BB">?></span>
</span></code>
Program 2: PHP
<?php
highlight_string('<?php
// PHP Program to add two numbers

function Add( $x, $y)
{
    $sum = $x + $y; 
    
    return $sum;
}

    // Driver Code
    echo Add(15, 32);
?>')
?>
Output:
<code><span style="color: #000000">
<span style="color: #0000BB">
<?php<br /></span><span style="color: #FF8000">
// PHP Program to add two numbers<br /><br />
</span><span style="color: #007700">function </span><span style="color: #0000BB">
Add</span><span style="color: #007700">( </span><span style="color: #0000BB">
$x</span><span style="color: #007700">, </span><span style="color: #0000BB">
$y</span><span style="color: #007700">)<br />{<br />    </span>
<span style="color: #0000BB">$sum </span><span style="color: #007700">= </span>
<span style="color: #0000BB">$x </span><span style="color: #007700">+ </span>
<span style="color: #0000BB">$y</span><span style="color: #007700">; <br />    <br />
    return </span><span style="color: #0000BB">$sum</span><span style="color: #007700">;<br />
}<br /><br />    </span><span style="color: #FF8000">
// Driver Code<br />    </span><span style="color: #007700">echo </span>
<span style="color: #0000BB">Add</span><span style="color: #007700">
(</span><span style="color: #0000BB">15</span><span style="color: #007700">
, </span><span style="color: #0000BB">32</span><span style="color: #007700">
);<br /></span><span style="color: #0000BB">?></span>
</span></code>
Reference: http://php.net/manual/en/function.highlight-string.php

Next Article
Article Tags :

Similar Reads