How to use if/Else within a function?

Asked

Viewed 1,735 times

1

I’m trying to create a simple function, to write the title according to the gender selection on the form, but I must be doing something wrong:

$sexo = empty($_POST['sexo']) ? "[sexo]" : $_POST['sexo'];

function mudasexo($sexo) {

    if ($sexo == "masculino") {
            echo "O senhor";
        }
    else {
        echo "A senhora";
    }
}

Hence I wanted to use this function within HTML texts with: <?php mudasexo() ?> but it is not working. It does not accuse any error when threshing, but the page doesn’t even open.

  • 1

    In my test appeared "The Lady"

  • 1

    The problem seems not to be the function, if call her as it is there works.

  • @rray Can it be because this file where I put the function is included via include in one file, which is then included in another? Now looking right at the debug here I found this PHP error Fatal error: Cannot redeclare mudasexo() (previously declared in arq1.php:90) in arq1.php on line 98...

  • 1

    Just delete the version of the function that is in the wrong place.

  • 1

    This @rray! @gustavox, enable error Reporting

  • No, the function is declared only once. It’s like this, when the form runs it calls a file (saida.php, which is html/php), and at the end of this file has a include that calls another file (saida2php - which tbm is an html/php file), and this one that calls just another script (and this is where the function is, and this script only acts on the output 2php file). @rray

  • Then the output file.php (the first) opens only half, when it starts the 2 no longer opens... Only remove the function...

  • I thought it had to do with when the if/Else, but I think it has to do with the way using include and such... thanks anyway to all who helped... Hugs.

  • 1

    you call for include or include_once ?

  • Ah @rray, by include (but I changed to include_once now hehe), but when I read your comment I went to look and I was calling the script file on both pages (left and left 2)... calling only on the first worked! thanks^^

Show 5 more comments

3 answers

5


I would do different, I see several conceptual errors in the function, but the only problem that prevents it from working is the function call in HTML, the function expects a parameter, you are not passing any arguments. I think this is your argument:

<?php mudasexo(empty($_POST['sexo']) ? "[sexo]" : $_POST['sexo']) ?>

I put in the Github for future reference.

Even with the problem, if nothing is appearing then there are problems in other parts of the code.


According to the comments, the real problem was in the function statement but still worth rethinking it.

  • So the problem I think is because this file where I put the function is included via include in one file, which is then included in another... now looking right the debug here I found this error PHP Fatal error: Cannot redeclare mudasexo() (previously declared in arq1.php:90) in arq1.php on line 98...

  • Yes, in this case you missed passing the seedling function parameter($Cod)

  • So, but the point is that even if I don’t call the function, that just puts it in the script file, it already gives the problem. If I pull the job, it’s normal...

  • I thought it had to do with the if/Else, but I think it has to do with the way using include and such... thanks anyway to all who helped...

  • The problem is that I was calling the script file twice... Thanks again to everyone.

  • The problem of not opening was with the include of the script being called twice even, but it also had this but not being passing the argument, so I marked your answer as accepted (but I used only mudasexo($sexo) because it already had the variable declared). Btw, could talk something about conceptual errors and/or make an example of what you would do?

  • 2

    The function should return a value instead of printing something, so it separates the task of selecting what to print with. It treats the masculino as one thing and all the rest as another. You’re not comparing something that can only have two states. The function name is weird, not only by the expression used, but you’re not changing the sex of anything inside it. And so on...

  • kkkk is, the name just got weird :-) O masculino is the value of a select, who only has two options, but when I made a elseif with $sexo == "feminino" I ended up finding it redundant and left only the else same... But then from what you said it would be right to do something like if ($sexo == "masculino") { Return true;}e daí embaixo um outro ifif ($sex == true) { echo...}`?

  • 1

    That’s it, you are trusting that the page that is something you have no control will send the data straight. That’s a mistake. If you return true will print true that’s not what you want. And never put in a if one $variavel == true, Now this is redundant.

  • But in the variable declaration I’m using the empty, which in case makes this data check... it makes no difference? In case then it would be better to do the elseif for a female and a else with Return false? Or better... ask another question? :)

  • That only checks if it is empty, nothing else. Something else can come. And worse, you check and create a third state, which you claim does not exist. Your code guarantees that it exists.

  • Aff now got scared. : -) I’ll think about it and then see if you’ve created another more specific question. Thanks for now!

Show 7 more comments

3

You can do it this way:

function mudarSexo($sexo){

    switch($sexo){
        default: 
            $mudanca = 'Sexo não selecionado';
            break;

        case 'masculino':
            $mudanca = 'Senhor';
            break;

        case 'feminino':
            $mudanca = 'Senhora';
            break;      
    }

    return $mudanca;
}

echo mudarSexo("masculino"); // Imprime -> Senhor
echo mudarSexo("feminino"); // Imprime -> Senhora
echo mudarSexo(""); // Imprime -> Sexo não selecionado

0

I have a form , individual, appears one and for legal person appears another. It’s in html, I don’t know how to fit the if and Else in the middle of it. I even tried but it’s giving error.

the individual example:

Juridical Person Pessoa Física
        <?php
        <if value == (pessoa física)>
        <br>


        <div class="row">
            <div class="col">
                <label>CPF</label>  
                <input  name="cpf" type="text" placeholder="Digite o CPF" class="form-control input-md" required>
            </div>
            <br>
            <div class="col">
                <label>E-mail</label>  
                <input  name="email" type="email" placeholder="Digite o seu E-amil" class="form-control input-md" required>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <label for="inputPassword3">Data e Hora</label>
                <input type="date" class="form-control" name="data" placeholder="Data">
            </div>
            <br>
            <div class="col">
                <label>Nome</label>  
                <input name="nome" type="text" placeholder="Digite o Nome do Respnsável Legal" class="form-control input-md" required>
            </div>
            </div>
            <br>
            <div class="row">
                <div class="col">
                    <label>Telefone</label>  
                    <input  name="tel" type="text" placeholder="Digite o seu Telefone" class="form-control input-md" required>
                </div>
                <div class="col">
                    <label >Bairro</label>  
                    <input  name="endereco" type="text" placeholder="Digite o Bairro" class="form-control input-md" required>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col">
                    <label>Rua</label>  
                    <input name="rua" type="text" placeholder="Digite o nome da rua" class="form-control input-md" required>
                </div>
                <div class="col">
                    <label>CEP</label>  
                    <input name="cep" type="text" placeholder="Digite o cep" class="form-control input-md" required>
                </div>
            </div> 
            <br><br><br><br>
            <div class= "offset-md-6 col-md-12">
                <button type="submit" class="btn btn-dark col-md-1">Cadastrar</button>
            </div>
    </div>
</div>

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/bootstrap-datetimepicker.min.js"></script>
    <script src="js/locales/bootstrap-datetimepicker.pt-BR.js"></script>
    <script type="text/javascript"</script>}
    }

Browser other questions tagged

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