IF and ELSE conditional exercises php for beginners

First problem bulletin to learn php. These exercises are to work on control structures, specifically the IF and ELSE conditionals. Beginner and intermediate level exercises.

The step-by-step solutions of these exercises, I will publish in another post that I will link to at the end of this post. I recommend you try them, spend at least some time thinking about them, and then check out the solution provided by me. Think that, in most problems, there is not only one solution.

All the exercises presented come from a hypothesis where I indicate what the initial situation is, what variables you should use to solve the problem and the possible values ​​they may have. This means that you should only focus on using the resources (variables) that I will indicate and on programming the solution for the specified case. If you want to test your script, you must have given a value of your choice to the variables that I specify in each problem.

Content

  • Conditional exercises for beginners
    • Exercise 1 php conditionals.
    • Exercise 2 of php conditionals
    • Exercise 3 of if and else in php
    • Version 2 of exercise 3. Handicap:
    • Exercise 4 on if and else
    • Version 2 of exercise 4. Handicap:
  • Intermediate level conditional exercises
    • Exercise 5 php of if and else
    • Exercise 6 of if and else in php
    • Exercise 7 of if and else in php
  • Extra: Theory to solve these exercises
  • Solutions of the exercises

Conditional exercises for beginners

Exercise 1 php conditionals.

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.

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).

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.

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.

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.

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).

Intermediate level conditional exercises

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.

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 and OR.
  3. The messages can be personalized by you, the important thing is that you print them on the screen.

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.

Extra: Theory to solve these exercises

  • 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 exercises

Soon you will be able to find the solutions of the proposed exercises in the following links:

  • Exercise solutions for beginners
  • Intermediate level exercises solutions

Leave a Reply