Re: using __LINE__ in error reporting

El 22/01/2010 12:58, bill escribió:
I thought I could use the magic constant __LINE__ in error reporting
such as:

if (error_condition) fReportError("error condition on line __LINE__")

but what I get is the literal "__LINE__" and the magic constant is not set.

If I use:
$line = __LINE__
if (error_condition) fReportError("error condition on line $line")

it works just fine, but that is cumbersome.

PHP parses double quoted strings looking for variables but it won't replace constants. Variables start with dollar sign so it's easy to spot them in your strings but constants have no special syntax. Code like this...

echo "Hello, world";

.... would misbehave if you inadvertently do this:

define('world', 'Earth');

Every time you create a constant you'd need to look for literal matches in all your application and escape them!

Is there a way to include the line number in an error report simply ?

You can use the dot operator, that is used to concatenate strings:

fReportError('error condition on line ' . __LINE__)

or sprintf():

fReportError(sprintf('error condition on line %s', __LINE__))

String handling is pretty basic syntax, I recommend you read this:

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor satinado: