Solutions of the first newsletter exercises php beginner level

Solutions to the first php exercise bulletin, specifically beginner level ones. Solutions are step-by-step explanations of the programmed algorithm that implements the solution.

Content

  • Conditional exercises for beginners
    • Solutions of exercise 1 conditional php.
    • Solutions for exercise 2 of php conditionals
    • Solution of exercise 3 of if and else in php
    • Solution of Version 2 of exercise 3. Handicap:
    • Possible solutions of exercise 4 on if and else
    • Solution for version 2 of exercise 4. Handicap:
  • Extra: Theory used to solve these exercises for beginners

Conditional exercises for beginners

Solutions of exercise 1 conditional php.

In a web that simulates a calculator, a user can enter two numbers and the web will tell him which one is greater. The approach is the following:

  1. The first number will be in a variable called $a.
  2. The second number is stored in the variable $b.
  3. Helping us from the control structure IF and ELSEwe must show the larger number of the two on the screen, with a message that says: The larger of the numbers is X.

We start from the problem that we have two variables $a and $b, each variable contains a number that is unknown to us, and I must show only the largest of both. To solve this exercise we must consider all the hypotheses, that is, either $a is greater than $bO well $b is greater than $a. The first assumption I am going to verify with a ifwhich is the structure that allows me to make the comparison between the numbers:

#php
if ( $a > $b ){
}
#/php

Thanks to this IF I have made sure that the machine (the code interpreter) will only enter between the 2 braces if $a is greater than $bso now I am going to tell the interpreter to show the message that the exercise asks me for.

#php
if ( $a > $b ){
echo ‘The largest of the numbers is ‘ . $a;
}
#/php

I already have my first hypothesis resolved. For the second case, you could do the same: program another if with the condition $b > $abut since in this problem I only have two possible cases, I am going to benefit from the use of the statement ELSE, which is always attached to an IF, and which tells the machine that its code enclosed in braces will be executed if the condition of the IF is not met. The solution would be like this:

#php
if ( $a > $b ){
echo ‘The largest of the numbers is ‘ . $a;
} else {
echo ‘The largest of the numbers is ‘ . $b;
}
#/php

Solutions for exercise 2 of php conditionals

We must design and program an algorithm that, for the introduction of a vehicle weight, says if it is allowed or not depending on a second value $allowedweight. The split approach is:

  1. The variable $weightVehicle You have the weight of the vehicle to check.
  2. The variable $weightAllowed contains the maximum weight allowed for the vehicle in question.
  3. If the weight of the vehicle is below the maximum weight allowed, we must display a message indicating this. Otherwise, you will also have to notify: The maximum weight of the vehicle is: X (Being X the maximum weight allowed).

Knowing that in the variable $weightVehicle I receive the weight of the vehicle to check, and that in another variable $weightAllowed I have the value of the allowed weight, I am facing an exercise with two possible cases: either the weight is greater than the allowed one, or it is lower.

A solution to this exercise using the two possible conditions would be:

#php
if ( $VehicleWeight > $AllowedWeight ) {
echo ‘The maximum weight of the vehicle is: ‘ . $allowedweight;
}
if ( $VehicleWeight < $AllowedWeight ) {
echo ‘The weight of the vehicle is below the maximum.’;
}
#/php

Although this solution is completely valid and solves the problem perfectly, I have to mention that it is not the most efficient. A more efficient solution, since we are facing a problem with two clear possibilities, would be to take advantage of the use of if and else. The proposed solution would be:

#php
if ( $VehicleWeight > $AllowedWeight ) {
echo ‘The maximum weight of the vehicle is: ‘ . $allowedweight;
} else {
echo ‘The weight of the vehicle is below the maximum.’;
}
#/php

Solution of exercise 3 of if and else in php

An algorithm is requested to be programmed, which, given a text entered by the user, displays a message on the screen. The text entered by the user will be Hello either Bye bye.

  1. The entered message will be stored in $message.
  2. If the user has entered Hello, We must display a message on the screen that says: Very good, user
  3. If instead the message is Bye bye, we will display a message that says: See you soon.

We are facing another problem with two possibilities. In this case we must compare the value of $message with a possible word entered by the user, both texts. In php the texts can be compared without any problems, just like the numbers, at least as in this case that we are not told to take capital letters or special characters into account. The solution with if and else I would be:

#php
if ( $message == ‘Hello’ ) {
echo ‘Very good, user’;
} else {
echo ‘See you soon’;
}
#/php

Solution of Version 2 of exercise 3. Handicap:

The text entered in the message may be wrong. The user usually confuses the keys and will have to be told that the entered text is not recognized.

The solution to this version of the exercise, taking into account a possible wrong introduction of the message, should be solved by nesting if and else:

#php
if ( $message == ‘Hello’ ) {
echo ‘Very good, user’;
} else {
if ( $message == ‘Goodbye’ ) {
echo ‘See you soon’;
} else {
echo ‘The entered text is not recognized’;
}
}
#/php

Possible solutions of exercise 4 on if and else

We are going to program a small script that validates the link text. If they pass us a text that says “here” we will show a message: Be careful, you are annoying a link to a page!. Otherwise we will print: Good! You are doing the SEO world a favor..
  1. The entered text will be stored in a variable $link.
  2. The messages must be shown within each possible case, that is, between the keys of the IF or of ELSE.

This exercise for beginners, very similar to the previous ones, we could solve once more with the use of if or combining if and else:

Solution 1:

#php
if ( $link == ‘here’ ) {
echo ‘Watch out, you’re messing with a link to a page!;
}
if ( $link != ‘here’ ) {
echo ‘Good! You’re doing the SEO world a favor’;
}
#/php

As I have only been told that I can receive two values ​​(either here or any other value), in the first case I look for an exact match and in the second any different value with !=.

Solution 2:

Another solution benefiting from the use of if and elsemore efficient and faster to program:

#php
if ( $link == ‘here’ ) {
echo ‘Watch out, you’re messing with a link to a page!;
} else {
echo ‘Good! You’re doing the SEO world a favor’;
}
#/php

Solution for version 2 of exercise 4. Handicap:

The messages must be stored in a variable instead of showing them directly on the screen, that is, assign the message that touches to a variable (for example $message) and when the checks are finished (if and else) display it on the screen in the normal way (for example with threw out).

This exercise tries to strengthen the use of variables to improve our programming, getting used to assigning values ​​to variables to produce a single output is common and achieves a more organized code. The solution would be like this:

#php
if ( $link == ‘here’ ) {
$message = ‘Watch out, you’re messing with a link to a page!;
} else {
$message = ‘Good! You’re doing the SEO world a favor’;
}
echo $message;
#/php

Extra: Theory used to solve these exercises for beginners

  • 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

Leave a Reply