how to return the current line using variable but return the line being used the PHP variable

Asked

Viewed 60 times

-4

For example:

<?php
$linha = __LINE__;

echo "a linha atual é $linha <br>";
//deveria retornar "a linha atual é 4"

echo "a linha atual é $linha <br>";
//deveria retornar "a linha atual é 7"

?>

but in all rows I use the $line variable returns 2. How to return the line the variable is in?

  • 4

    echo "a linha atual é ".__LINE__." <br>";

  • What is the purpose of storing the line number? the comment above solves the problem at first.

  • I need to pick up an error code, and this code will be the line

3 answers

1

The easiest way I found to solve sui problem is through the function debug_backtrace(); it returns various information, but for ease I created a function and every time I call it will return the line in question:

<?php 

function line(){        
    $backtrace = debug_backtrace();
    $line = $backtrace[0]['line'];
    return $line;
}

echo line();

echo line();

Test there and see That’s what you need.

0


I did it and it worked:

<?php
function __CODIGO_ERRO__(){
    $backtrace = debug_backtrace();
    $line = $backtrace[0]['line'];
    return "<div class='box-alert box-alert-top box-alert-erro'>
                <div class='box-alert-container'>
                    <span class='erroCOD' data-erro='".$line."'>Erro na execução da consulta. COD: (".$line.")</span>
                </div>
            </div>";
}

echo __CODIGO_ERRO__();
?>

-1

The way you did it worked, but it doesn’t work that way:

<?php
function line(){
    $backtrace = debug_backtrace();
    $line = $backtrace[0]['line'];
    return $line;
}

define("__CODIGO_ERRO__", "
<div class='box-alert box-alert-top box-alert-erro'>
    <div class='box-alert-container'>
        <span class='erroCOD' data-erro='".line()."'>Erro na execução da consulta. COD: (".line().")</span>
    </div>
</div>
");

echo __CODIGO_ERRO__;
 ?>

He returns that the line is 11, but it should be 16. Is there any way this works?

  • Do you know how functions work and call functions in a programming language? If you call the function to set the constant __CODIGO_ERRO__, the value of the line will refer to that call. Every time you use the constant, it will have the same value (because it is a constant and constant does not usually vary).

  • Ahh vlw, I made it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.