I need help with php:
I am developing a certain web application with which I need a certain condition to be met for the rest of the code to be executed. For now I have developed the following, although I find it very cumbersome to see:
if ($message != 'Mensaje') {
echo '¡El mensaje está mal escrito!';
} else {
echo '¡El texto del mensaje es correcto!';
// Aquí hay otras 800 líneas de código.
}
As you can see from the annotation, there is a lot of code in the else
. When these types of statements accumulate, the code becomes more and more "dirty", which does not interest me.
Is there a way to not have to use else
? Namely:
if ($message != 'Mensaje') {
echo '¡El mensaje está mal escrito!';
// Sentencia que pone fin al código, tal y como lo hace un require; cuando suelta un error.
}
echo '¡El texto del mensaje es correcto!';
// Aquí hay otras 800 líneas de código.
In this way, the first code and the second would give the same results with the exception that the second is much cleaner than the first. Until now I had searched for information about require
, but apparently it only works if it is about checking the existence of certain files. Is there a way to get the same results as with require
using variables?
A greeting and good programming.