Doubt about functions in php

-1

Does a function only work if we call it in some file or will it work anyway? For example, to pass the value from one variable to another in the rendering file, I know I should call the function to give value to the variable. But when I just want to pass a value from one string to another string, for example:

public function conta_teste_profissional($codigo){
    $conexao = Database::getConnection();
    $busca = "SELECT * FROM teste WHERE cod_usuario_profissional = $codigo;";
    $resultado = $conexao->query($busca);
    $retorno = $resultado->fetchAll(PDO::FETCH_ASSOC);

    $tamanho = count($retorno);
    return $tamanho;
}

.

Do I need to call it in some other file or does this function work automatically?

    
asked by anonymous 27.08.2018 / 16:57

2 answers

1

Your question is not very clear, but if the question is "functions are invoked automatically" the answer is no. You should call the function in each part of your code that you want to use it for. Taking your example as a basis

public function conta_teste_profissional($codigo){
    $conexao = Database::getConnection();
    $busca = "SELECT * FROM teste WHERE cod_usuario_profissional = $codigo;";
    $resultado = $conexao->query($busca);
    $retorno = $resultado->fetchAll(PDO::FETCH_ASSOC);

    $tamanho = count($retorno);
    return $tamanho;
}

The above block will only be responsible for declaring the function, if you want to use it, you must call it in the place that is appropriate for you. Example:

conta_teste_profissional(4)
    
27.08.2018 / 17:22
0

Functions, even if declared, are not invoked / started automatically. You should call the function in the part of your code where you need it.

Using your code as an example:

public function conta_teste_profissional($codigo){
    $conexao = Database::getConnection();
    $busca = "SELECT * FROM teste WHERE cod_usuario_profissional = $codigo;";
    $resultado = $conexao->query($busca);
    $retorno = $resultado->fetchAll(PDO::FETCH_ASSOC);

    $tamanho = count($retorno);
    return $tamanho;
}

The function would only be executed if you called it this way:

conta_teste_profissional(10); //Exemplo

You can also call a function declared in another PHP file giving an include in the file in which it is declared.

Here's the INCLUDE handbook: link

    
28.08.2018 / 00:56