Jump to content

Getting the line number (not the ++ approach)


ted_chou12

Recommended Posts

because the order of my codes are sorted to different categories, therefore, getting the line number simply by putting a variable like $number=0;
and each loop $number ++; would not work, is there other possible ways to get the line number in which I am reading?
Link to comment
https://forums.phpfreaks.com/topic/34736-getting-the-line-number-not-the-approach/
Share on other sites

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.
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?
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]
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
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.