Miscellaneous Functions in PHP: Part 2

Introduction

In this article I describe the PHP Miscellaneous functions die, eval, exit, get_browser and highlight_file. To learn some other Miscellaneous functions, go to:

  1. Misc Function in PHP: Part 1

PHP die() Function

The PHP Miscellaneous die function prints a message and exits the current script; it is the same as the exit function.

Syntax

die(message)

Parameter of the die function

The parameter of the function is:

Parameter Description
message It specifies the message.

Example

An example of the
function is:

<?
php
$siteurl="http://www.c-sharpcorner.com/";

fopen
($siteurl,"r")
or
die("Unable to open the $siteurl");
?>

Output


die-function-in-php.gif

PHP eval() Function

The PHP Miscellaneous eval function evaluates a string as PHP code.

Syntax

eval(phpcode)

Parameter in eval function

The parameter of the function is:

Parameter Description
phpcode It specifies the PHP code to evaluated.

Example

An example of the
function is:

<?
php
$sitename = "C-sharpcorner";
$ext = ".";
$domainName="com";
$str = 'This is $sitename $ext $domainName!';

echo
$str. "<br />";
eval
("\$str = \"$str\";");
echo
$str;
?>


Output


eval-function-in-php.gif

PHP exit() Function

The PHP Miscellaneous exit function prints a message and terminate the current script and it returns no value.

Syntax

exit(message)

Parameter of the exit function

The parameter of the function is:

Parameter Description
message It specifies the message or status number to write before exiting the script.

Example

An example of the
function is:

<?php

$siteurl="http://www.c-sharpcorner.com/";

fopen($siteurl,"r")

or exit("Unable to open the $siteurl");

?>


Output

exit-function-in-php.gif

PHP get_browser() Function

The PHP Miscellaneous get_browser function determine what the user's browser is capable of and it returns the information as an object array that will contain various data elements representing, for instance, the browser major and minor version number and id string.

Syntax

get_browser(UserAgent, returnArray)

Parameter of the get_browser function

The parameters of the function are:

Parameter Description
UserAgent It specifies the name of an HTTP user agent.
returnArray If you set this parameter to TRUE, the function will return an array instead of an object.

Example

An example of the
function is: 

<?php

echo $_SERVER['HTTP_USER_AGENT'] . "<br /><br />";

$browser = get_browser(null,true);

echo "<pre>";

print_r($browser);

?>

Output


get-browser-function-in-php.gif

PHP highlight_file() Function

The PHP Miscellaneous highlight_file function is used to output a file with the PHP syntax highlighted and it returns the highlighted code as a string instated of printing it out, if you set it to true otherwise it will return true on success or false on failure.

Syntax

 

highlight_file(filename,return)

Parameters of the highlight_file function

The parameters of the function are:

Parameter Description
filename It specifies the file to display.
return If this parameter is set to true then this function will return the highlighted code as a string.

Example

An example of the
function is:

<html>

<body>

<?php

highlight_file("highlightedfile.php");

?>

</body>

</html>


Output

highlight-file-function-in-php.gif

Up Next
    Ebook Download
    View all
    Learn
    View all