Jump to content

Getting the line number (not the ++ approach)


ted_chou12

Recommended Posts

Try this code, I think it will explain.

[code]<?php //this is line #1
echo "This is line #".__LINE__."<br>"; //this is line #2
////this is line #3
echo "This is line #".__LINE__."<br>"; //this is line #4
//this is line #5
for($i=0; $i<=3; $i++) //this is line #6
echo "i = ".$i.", this is line #".__LINE__."<br>"; //this is line #7
//this is line #8
foo(); //this is line #9
//this is line #10
echo "This is line #".__LINE__."<br>"; //this is line #11
//this is line #12
function foo() //this is line #13
{ //this is line #14
  echo "This is line #".__LINE__."<br>"; //this is line #15
} //this is line #16
//this is line #17
//this is line #18 ?>[/code]

The __LINE__ constant, simply holds the number of the current line.

Orio.
Link to comment
Share on other sites

question on the same topic... say i have this function...

[code]
<?
function fatal_error($string=""){
$string = 'You have an Error on line #'.__LINE__.'<br><pre>'.highlight_string($string);
if(mysql_errno()!=0) $string.= '<hr>'.mysql_errno().': '.mysql_error().'<hr>';
$string .= '</pre>';
die("Fatal Error: $string");
}
fatal_error();
?>
[/code]
how can i get that set to 8 instead of 3?
Link to comment
Share on other sites

You could put the __LINE__ in the fatal_error();

[code]<?php
<?
function fatal_error($line, $string){
$string = 'You have an Error on line #'.$line.'<br><pre>'.highlight_string($string);
if(mysql_errno()!=0) $string.= '<hr>'.mysql_errno().': '.mysql_error().'<hr>';
$string .= '</pre>';
die("Fatal Error: $string");
}
fatal_error(__LINE__, $error_string);
?>[/code]
Link to comment
Share on other sites

You need to pass to the function the line where the error occurred. Chnage the function to:
[code]<?php
function fatal_error($line,$string=""){
$string = 'You have an Error on line #'.$line.'<br><pre>'.highlight_string($string);
if(mysql_errno()!=0) $string.= '<hr>'.mysql_errno().': '.mysql_error().'<hr>';
$string .= '</pre>';
die("Fatal Error: $string");
}
fatal_error(__LINE__,'Error string');
?>[/code]

Ken
Link to comment
Share on other sites

Pass the line number to the function:

[code]<?php

function fatal_error($line, $string="")
{
$string = 'You have an Error on line #'.$line.'<br><pre>'.highlight_string($string);
if(mysql_errno()!=0) $string.= '<hr>'.mysql_errno().': '.mysql_error().'<hr>';
$string .= '</pre>';
die("Fatal Error: $string");
}
fatal_error(__LINE__);

?>[/code]

Orio.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.