PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




assert

Name

assert — Checks whether an assertion is false.

Synopsis

bool assert(assertion);
mixed assertion: Boolean expression to check or code to evaluate

Returns

FALSE if assertion fails

Description

This is similar to the C function of the same name. A piece of code is evaluated by the function; if FALSE is returned from the function, a warning is generated indicating the file and line number where the assertion failed. This function should only be used to debug code or return values from functions.

You can modify the output and behavior of this function by using the assert_options() function or by modifying the php.ini file.

assert() callbacks are particularly useful for building automated test suites because they allow you to easily capture the code passed to the assertion, along with information on where the assertion was made. While this information can be captured via other methods, using assertions makes it much faster and easier. The callback function should accept three arguments. The first argument contains the file in which the assertion failed. The second argument contains the line on which the assertion failed. The third argument contains the expression that failed (if any); literal values such as 1 or "two" are not passed via this argument. A simple callback function can be found in the example for assert_options() .

When using this function with a string, be sure to enclose the string in quotes as shown in the example.

Another good function to use for testing output and code is the eval() function in the Miscellaneous Functions module.

Version

Existing since version 4.0

See also

See also: assert_options() , eval() (Miscellaneous Functions)

Example

Example 989. Use assert() to automate a test for a user function

<?php
// Use assert_options() to set how assert behaves
// Enable assert
assert_options (ASSERT_ACTIVE, 1);

// Turn on warnings for assert
assert_options (ASSERT_WARNING, 1);

// Display errors in the asserted code
assert_options (ASSERT_QUIET_EVAL, 0);

// Calculate a tax, rebate, tariff and exhange rate on a dollar amount
function calculate_tax ($amount, $tax, $rebate, $tariff, $exchange) {
   return $exchange * ($amount + $tariff - ($amount * $tax) - $rebate);
}

// Test our function call against a series of outputs
// distributed by a standards body as test data

// Doing this kind of testing lets us be more sure that our 
// algorithm matches the standards body's algorithm

assert ("calculate_tax (100, 0.08, 12, 0, 1.52) == 121.60;");
assert ("calculate_tax (100, 0.0625, 0, 0.2, 1.52) == 142.80;");
assert ("calculate_tax (100, 0, 0, 25, 1.52) == 190;");
assert ("calculate_tax (100, 0.15, 22, 13, 1.52) == 115.52;");
assert ("calculate_tax (100, 0.11, 10, 19, 1.52) == 148.96;");
?>

// Should output
Warning: Assertion "calculate_tax (100, 0.0625, 0, 0.2, 1.52) == 142.80;" failed in f:/xitami/pub/test/test.php on line 20



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.