IF IF IF how to do? PHP IF with syntax error

Asked

Viewed 170 times

-5

In the code, there are several conditions, according to the choices of the user. The system is simple, just checks whether or not the user can receive unemployment insurance. I’m following the chart:

Tabela seguro

My code is this:

<?php   

$tx = 2;
            /* Exibe o resultado caso a variável possua um valor diferente de null */
            if ($tx !== null): 

                if($pedido == primeira):

                    if($meses >= 12 && $meses <= 23):
                        $parcela = "4";
                    elseif($meses > 23):
                        $parcela = "5"; 

                elseif($pedido == segunda):

                    if($meses >= 9 && $meses <= 11):
                        $parcela = "3";
                    elseif($meses > 11 && $meses < 24):
                        $parcela = "4"; 
                    elseif($meses > 24):
                        $parcela = "5";

                    endif;    
                endif;

            endif;   

You’re making the mistake:

Parse error: syntax error, unexpected end of file in > C:\wamp\www\extudando.esy.es\teste.php on line 24

and there’s nothing on line 24

I also know that the syntax must be wrong (like it works, but it is gambiarra), but I could not think of a more "correct" way. How could I fix this? Or the only way would be IF IF IF ? And I can’t correct the error

  • Substitute $meses => 12 for $meses >= 12. This happens because => is used to assign a value to an Array index, so PHP accuses the error.

  • endif wave ; to close. It is advisable to review the syntax of the language whenever you have doubts.

  • After editing... Missing a variable in && <=23, the correct is && $variavel <=23.

  • $parcela = also can not stay that way.

  • I fixed that same Isac ! I completed, and corrected the order of =< .... but now gives the error:

  • Parse error: syntax error, Unexpected '<' in C: wamp www extudando.esy.es rh_seg-des.php on line 62

  • 1

    @Flavia The correct is <= and not =<. I recommend reading http://php.net/manual/en/language.operators.comparison.php

  • I corrected it, recreated it, yet there’s some error in the syntax, and I don’t know what I’m doing wrong

  • @Flavia there are two errors in her - new - code. 1) It is necessary to involve the words primeira and segunda by quotation marks or adding the $ and turn them into variables; 2) Missing a endif; before elseif($pedido == segunda):

Show 4 more comments

2 answers

0

  • Missing one endif see comment in code
  • first and second, should be in single quotes
  • You have to decide what happens when number of months equals 24

        if ($tx !== null): 
    
            if($pedido == 'primeira'):
    
                if($meses >= 12 && $meses <= 23):
                    $parcela = "4";
                elseif($meses > 23):
                    $parcela = "5"; 
                //faltando no seu código
                endif;
    
            elseif($pedido == 'segunda'):
    
                if($meses >= 9 && $meses <= 11):
                    $parcela = "3";
                //deve ser <=24 ?
                elseif($meses > 11 && $meses < 24):
                    $parcela = "4"; 
                //ou deve ser >=24 ?
                elseif($meses > 24):
                    $parcela = "5";
                endif; 
    
            endif;
    
        endif;
    

0

  1. In the first installment is missing the endif
if($pedido == primeira):
  if($meses >= 12 && $meses <= 23):
     $parcela = "4";
  elseif($meses > 23):
     $parcela = "5"; 
 endif; // aqui
  1. In this case you expect the user to write 'first' or 'second'? I suggest you put a menu with numbers indicating the options: 1- First Installment 2- Second installment. The user will need to enter only the number for the option, not the whole word. In addition, it is necessary to add simple quotes under conditions, as the user input is in string format

if($pedido == 'primeira') or if($pedido == '1')

Browser other questions tagged

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