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:

http://es.php.net/manual/en/language.types.string.php


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
.



Relevant Pages

  • Re: why use the data command parameter collection
    ... cumbersome than using an iterative object oriented approach. ... quote characters or other such input by concatenating strings. ... Parameters Collection. ... Also the approach string concatenation leaves a lot to be desired (see ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Can you dynamically resize an array?
    ... to omit it. ... the i'th item in the list of strings, ... some special syntax. ... TStrings indicates what you want ...
    (comp.lang.pascal.delphi.misc)