Skip to content
Kevin Jensen edited this page Aug 4, 2014 · 6 revisions

console::assert

console::assert — If the specified expression is false, the message is written to the console along with a stack trace. If assertion succeeds, nothing is printed.

Description

void console::assert ( bool $exp [, string $msg ] )

Parameters

$exp

The expression to be evaluated.

$msg

The message to be printed if assertion fails.

Return Values

returns nothing

Examples

Example #1

Assert if 1 is greater than 2

<?php
require_once 'console.php';

console::log( 1 > 2, "1 is never greater than 2!" );
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>Page Title</title>
</head>
<body>
	
</body>
</html>

Example #1 would output the following to the browser's console:

▼ index.php:29
  ▼ Assertion failed: 1 is never greater than 2!
    (anonymous function)

Example #2

console::assert can be used for unit testing. The following example was ported from a PHPUnit example

<?php
require_once 'console.php';

$stack = array();
console::assert( count( $stack ) == 0, "$stack size is NOT 0" );

array_push( $stack, "foo" );
console::assert( $stack[count( $stack ) - 1] == "foo", "\$stack[" . count( $stack ) - 1 . "] is not 'foo'" );
console::assert( count( $stack ) == 1, "$stack size is not 1" );

console::assert( array_pop( $stack ) == "foo", "array_pop did not give 'foo'");
console::assert( count( $stack ) == 0, "$stack size is not 0" );
console::assert( count( $stack ) == 1, "$stack size is not 1" );

?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>Page Title</title>
</head>
<body>
	
</body>
</html>
Example #2 Output
▼ index.php:13
  ▼ Assertion failed: Array size is not 1
    (anonymous function)
  • Home
  • About
  • Usage
  • console
    • $messageLog
    • $counters
    • $timers
    • assert
    • clear
    • count
    • debug
    • error
    • export
    • group
    • groupCollapsed
    • groupEnd
    • info
    • log
    • profile
    • profileEnd
    • time
    • timeEnd
    • timeline
    • timelineEnd
    • timeStamp
    • trace
    • var_dump
    • warn
  • Log — the Log class
    • $type
    • $var
    • $file
    • $line
    • __construct

Clone this wiki locally