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.
- 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:
- The first number will be in a variable called $a.
- The second number is stored in the variable $b.
- 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:
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.
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:
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:
- The variable $weightVehicle You have the weight of the vehicle to check.
- The variable $weightAllowed contains the maximum weight allowed for the vehicle in question.
- 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:
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:
Solution of exercise 3 of if and else in php
- The entered message will be stored in $message.
- If the user has entered Hello, We must display a message on the screen that says: Very good, user
- 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:
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:
Possible solutions of exercise 4 on if and else
The entered text will be stored in a variable $link.
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:
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 !=.
Another solution benefiting from the use of if and elsemore efficient and faster to program:
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:
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