PHP if elseif conditions

Asked

Viewed 51 times

-1

I’m developing a php application and want to determine if a certain word will receive the letter "a" or the letter "o".

I was doing with if and elseif, but I don’t know why it’s not working.

I will put an example that I did to demonstrate my problem, this is not the code of my application, but it is an example that explains well my problem.

$type = "";
$texto1 = "carr";
$texto2 = "cas";

if($texto1){
$type = "o";
}elseif($texto2){
$type = "a";
}
echo $texto1.$type . '<br>';
echo $texto1.$type;
  • if ($texto1), what you hope to verify with this?

  • 1

    ola, vc cannot use $texto1 as boolean condition, you would have to do something like this: if($texto1 == "Carr") string comparing string int comparing int

1 answer

0

The builder if has the return of type booleano, that is, test whether the condition is verdadeira or falsa. Thus, your code is testing whether the variable $texto1 is true, and since it is not empty, the return will always be true.

Take a look at the control structures of php:

https://secure.php.net/manual/en/language.control-structures.php

Browser other questions tagged

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