Misc Function in PHP: Part 1

Introduction

In this article I describe the PHP miscelaneous functions connection_abroted, connection_status, constant, define and defined, so the following function were paced in the Miscellaneous category because none of the other categories seemed to fit.

PHP connection_abroted() Function

The PHP miscelaneous connection_aborted function is used to check whether the client is disconnected and it returns 1 (one) if the client was disconnected otherwise it returns 0 (zero).

Syntax

connection_aborted()

Example

An example of the
function is:

<?php
function
checkAbort()
  {
 
if (connection_aborted())
 
error_log ("Script $GLOBALS[SCRIPT_NAME]" .
 
"$GLOBALS[SERVER_NAME] was aborted by the user.");
  }

register_shutdown_function
("checkAbort");
?>


PHP connection_status() Function

The PHP miscelaneous connection_status function returns a connection status bit field and the possible return values are:

  • 0-CONNECTION_NORMAL
  • 1-CONNECTION_ABORTED
  • 2-CONNECTION_TIMEOUT
  • 3-CONECTION_ABROTED & CONNECTION_TIMEOUT

Syntax

connection_status()

Example

An example of the
function is:

<?php

switch (connection_status ())

{

case CONNECTION_NORMAL:

  $data = 'Now Connection is in a normal state';

  break;

case CONNECTION_ABORTED:

  $data = 'Connection aborted';

  break;

case CONNECTION_TIMEOUT:

  $data = 'Connection timed out';

  break;

case (CONNECTION_ABORTED & CONNECTION_TIMEOUT):

  $data = 'Connection aborted and timed out';

  break;

default:

  $data = 'Unknown';

  break;

}

echo $data;

?>

Output

connection-status-function-in-php.jpg

PHP constant() Function

The PHP miscelaneous constant function returns the value of a constant or say it returns the value of a constant on success or NULL if the constant is not defined.

Syntax

constant(constant)

Parameter in constant function

The parameter of the function is:

Parameter Description
constant It specifies the name of the constant to check.

Example

<?php

define("C-ShapCorner","Welcome at MCN Solution!");

echo constant("C-ShapCorner");

?>

Output


constant-function-in-php.jpg

PHP define() Function

The PHP miscelaneous define function defines the name of a constant and it returns TRUE on success or FALSE on failure.

Syntax

define(name, value,CaseInsensitive)

Parameter in define function

The parameters of the function are:

Parameter Description
name It specifies the name of the constant.
value It specifies the value of the constant.
CaseInsensitive It specifies whether the constant name should be case-insensitive.

Example

<?php

define("C-ShapCorner","Welcome at MCN Solution!");

echo constant("C-ShapCorner");

?>

Output

define-function-in-php.jpg

PHP defined() Function

The PHP miscelaneous defined function checks whether a constant exists and it returns true if the constant exists or false on failure.

Syntax

defined(name)

Parameter in defined function

The parameter of the function is:

Parameter Description
name It specifies the name of the constant to check.

Example

<?php

define("C-ShapCorner","Welcome at MCN Solution!");

echo defined("C-ShapCorner");

?>

Output

defined-function-in-php.jpg


Similar Articles