Solutions first bulletin exercises IF and ELSE intermediate level

Let’s go there with the solutions to the intermediate level exercises of the first bulletin of conditional exercises on the web.

This is the second part of the solutions to the conditional exercises. If you would like to see first the unresolved exercises or the solutions of the beginner level exercises visit:

  • php exercises on php conditionals
  • Solution php exercises for beginners about conditionals

Content

  • Theory used to solve these exercises intermediate level
  • Solutions of the Conditional Exercises intermediate level
    • Solution Exercise 5 php of if and else
    • Solution of Exercise 6 of if and else in php
    • Exercise 7 of if and else in php

Theory used to solve these exercises intermediate level

To solve these exercises you should know the following theory concepts:

  • How they work and how variables are used in php
  • Arithmetic operations in php with variables
  • php control structures
  • if and else conditionals in php
  • php strlen() function

Solutions of the Conditional Exercises intermediate level

Solution Exercise 5 php of if and else

A blog client has a problem: his readers can leave comments on the web, but they must be less than 150 characters and, often, when they write, they tend to go too long. You must program a small script with the help of the strlen() function that displays an error message on the screen if it goes too far:

  1. The variable $comment contains the text of the message.
  2. The strlen() function returns the length of a text: $chars = strlen($comment);
  3. If the user enters a text of allowed length, it must be indicated and otherwise an error will be displayed such as: The maximum length of the comments is 150, your comment instead has XX characters.

To solve this exercise and as indicated, you must use the PHP function strlen, so the first thing I’m going to do is calculate the length of the inserted comment:

<?php
$total_letras = strlen($comentario);

Now that I have the number of letters in a variable, I can condition the messages that will be displayed on the screen depending on their value:

if ( $total_letras <= 150 ) {
   echo 'Tu mensaje es correcto! Gracias por comentar en la web!';
} else {
  echo 'La longitud máxima de los comentarios es de 150 caracteres, tu comentario en cambio tiene ' . $total_letras . ' caracteres';
}
?>

And that’s it, thanks to the elimination mechanism offered by the ELSE I could avoid the comparison $chars > 150.

Solution of Exercise 6 of if and else in php

In this exercise we are going to solve a problem that arose on a website with special content, limited to non-retired adults. The user is shown a message on the screen and a field to enter her age. We, in php, must program an algorithm that solves the problem:

  • If the age entered is that of a minor, we must indicate that access is prohibited
  • If the age is above 65 we will notify you that the content, unfortunately, is not for retirees.
  • Finally, if the age is between 18 and 65 we will welcome you to the web with a message: welcome to the web www.srcodigofuente.com/adultos-no-jubilados.
Exercise help:
  1. The age of the user is given in the variable $ageUser.
  2. You must use several conditions in the same IF In order to solve it, you must help yourself AND or OR.
  3. The messages can be personalized by you, the important thing is that you print them on the screen.

How do they tell me that the age is accessible to me thanks to the value of the variable $userage, I will use this variable to perform all the necessary comparisons. Depending on the condition that is fulfilled, I will show the correct message.

There is a clue in the help of the exercise and at the same time a handicap that forces us to use a certain strategy, and that is that it indicates that we must use the AND or OR sentences. So, I have to introduce several conditions in a IF.

The first thing I am going to rule out minor users:

<?php
if ( $edadUsuario < 18 ) {
   echo "Lo sentimos, el contenido de esta web no es accesible a menores de edad";
}

With this IF I have solved the first case that can occur. Now I am missing two possibilities: that it is the age of a retiree or correct (between 18 and 65). For this I will propose two possible solutions:

if ( $edadUsuario >= 18 && $edadUsuario < 65 ){
   echo 'bienvenido a la web www.srcodigofuente.com/adultos-no-jubilados';
} 
if ( $edadUsuario > 65 ) {
  echo 'Lo sentimos, el contenido de esta web está restringido a jubilados';
}
?>

In this solution in which I have opted for two IFs, I have resolved the interval from 18 to 65 thanks to the statement AND(&&). In the following solution I am going to forget about this logical operator (the AND) and I am going to nest IF and ELSE To solve the same problem:

<?php
if ( $edadUsuario < 18 ) {
   echo "Lo sentimos, el contenido de esta web no es accesible a menores de edad";
} else {
   if ( $edadUsuario < 65 ) {
      echo 'bienvenido a la web www.srcodigofuente.com/adultos-no-jubilados';
   } else {
      echo 'Lo sentimos, el contenido de esta web está restringido a jubilados';
   }
}
?>

In this proposal I have taken advantage of the implicit condition of the else (that the age will be greater than 18) and inside it I have only had to check if the age is below 65 (not retired and greater than 18), and finally with a ELSE of discard I solve the problem that the age is the same or is over 65 (he is retired).

Exercise 7 of if and else in php

In a web application for a flight agency they require our help, they need a small script that prevents a pilot from having more flight hours than the recommended ones. We must display an error message in case the hours have been exceeded or, otherwise, increase the number of hours given in the variable with the pilot’s flight hours. The initial data for the exercise are:

  1. We receive the total flight hours of the pilot in $TotalHours.
  2. We will be able to know the flight hours to be added with $hoursflight.
  3. The maximum flight hours allowed to the same pilot will come in the variable $maxHoras.

This exercise is somewhat different from the rest, although not more difficult, since in some cases we will have to modify the value of a variable.

The first thing is to divide the exercise into possible cases:

  1. The pilot’s flight hours plus the new hours do not exceed the maximum hours allowed.
  2. The pilot’s flight hours plus the new ones exceed the flight hours allowed.

Now I know, that for case 1 I will definitely have to add the new hours, and for case 2 I will have to display an error message:

<?php
if ( ( $horasTotales + $horasVuelo ) < $maxHoras ) {
   $horasTotales = $horasTotales + $horasVuelo;
} else {
   echo 'Error, el piloto no puede volar más de ' . $maxHoras . ' horas de vuelo.';
}
?>

In the first IF I have introduced the concept of arithmetic operation, something new so far in conditionals on the web. The PHP interpreter recognizes the arithmetic operations, whatever they are, and performs them before proceeding to the comparison (in this case before comparing with the value of $maxHours.

Finally, helping me from the implicit condition that includes the else, that is, the opposite of ifI have resolved the possibility that the total hours are greater than those allowed.

Leave a Reply