ted_chou12 Posted January 18, 2007 Share Posted January 18, 2007 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? Quote Link to comment Share on other sites More sharing options...
Orio Posted January 18, 2007 Share Posted January 18, 2007 You can use the __LINE__ magic constant. Read more [url=http://www.php.net/manual/en/language.constants.predefined.php]here[/url].Orio. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 18, 2007 Author Share Posted January 18, 2007 thanks orio, i think that is what i am looking for, but how and in what function do i put this "__LINE__" in?ThanksTed Quote Link to comment Share on other sites More sharing options...
trq Posted January 18, 2007 Share Posted January 18, 2007 Did you read the link provided? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 18, 2007 Author Share Posted January 18, 2007 yes i did. Quote Link to comment Share on other sites More sharing options...
trq Posted January 18, 2007 Share Posted January 18, 2007 I don't think __LINE__will be of much use to you as it referes to the line numbers of the current script.Post some code and a better discription of what it is exactly your trying to do. Quote Link to comment Share on other sites More sharing options...
Orio Posted January 18, 2007 Share Posted January 18, 2007 Try this code, I think it will explain.[code]<?php //this is line #1echo "This is line #".__LINE__."<br>"; //this is line #2////this is line #3echo "This is line #".__LINE__."<br>"; //this is line #4//this is line #5for($i=0; $i<=3; $i++) //this is line #6 echo "i = ".$i.", this is line #".__LINE__."<br>"; //this is line #7//this is line #8foo(); //this is line #9//this is line #10echo "This is line #".__LINE__."<br>"; //this is line #11//this is line #12function 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. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 18, 2007 Author Share Posted January 18, 2007 thanks...Ted Quote Link to comment Share on other sites More sharing options...
taith Posted January 18, 2007 Share Posted January 18, 2007 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? Quote Link to comment Share on other sites More sharing options...
Philip Posted January 18, 2007 Share Posted January 18, 2007 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] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 18, 2007 Share Posted January 18, 2007 You need to pass to the function the line where the error occurred. Chnage the function to:[code]<?phpfunction 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 Quote Link to comment Share on other sites More sharing options...
Orio Posted January 18, 2007 Share Posted January 18, 2007 Pass the line number to the function:[code]<?phpfunction 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. Quote Link to comment Share on other sites More sharing options...
taith Posted January 18, 2007 Share Posted January 18, 2007 aah... perfect... makes sence :-D thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.