IF and ELSEIF conditional exercises php beginner level

Second bulletin of problems to learn php. These exercises are to work on control structures, specifically the IF and ELSE IF conditionals. Exercises for beginners, intermediate and advanced level.

Although this newsletter contains exercises for beginners, it is more complex than the first newsletter.

I will publish the step-by-step solutions of these exercises in another post that I will link at the end of this publication. 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 problems for beginners
    • Exercise 1 IF and ELSE IF php.
    • Exercise 2 IF and ELSE IF with php
    • Exercise 3 IF and ELSE IF
  • Intermediate level conditional problems
    • Exercise 1 conditionals
    • Exercise 2 conditionals
    • Academic exercise 3 on conditionals
  • test your php code
  • Extra: Theory to solve these exercises
  • Solutions of the exercises

Conditional problems for beginners

Exercise 1 IF and ELSE IF php.

A Spanish services website has a small problem, it wants us to indicate the day of the week on its home page, a simple task if it weren’t for the fact that the server provides us with the day in English through the date() function.

Exercise help:

  1. The function date() with the parameter ‘D’ returns the first three letters of the day of the week in English: Mon, Tue, Wed, Thu, Fri, Sat, Sun.
  2. with this little code $english_day = date(‘D’); we will have the day in the variable $day_english.
  3. Show the day in Spanish on the screen with the message: “The day of the week is: XXXX”.

Exercise 2 IF and ELSE IF with php

Carry out the previous exercise but showing the day of the week from Monday to Friday, and for Saturday and Sunday show the message: Weekend.

Exercise 3 IF and ELSE IF

The online store store.srcodesource.es You want to make an improvement in the code of your website. You need the web, depending on the amount of the basket, to show one message or another to the user. Specifically, you want:

  • If the purchase is less than 30 euros, a message will be shown in bold saying: ‘Buy more or we will charge you the abusive 30 euros of shipping costs’.
  • If the purchase is more than 30 euros but less than 90, we must show a number indicating how much is left to reach 90 euros and have free shipping costs. Example: ‘With only €33.50 more you can have free shipping costs!!!’
  • If the purchase reaches the €90 we will indicate a message in bold: ‘Shipping costs included’.

For this problem we have the following data:

  1. The total amount of the shopping cart comes in a variable $total_purchase with a positive decimal number. Example: 33.55.
  2. Messages in bold must use the tag #lt;strong#gt;#lt;\strong#gt;

Intermediate level conditional problems

Exercise 1 conditionals

The online store store.srcodesource.es You have asked us for an improvement to your website. You need a treatment of customer basket information that meets the following requirements:

  1. If the customer’s purchase is less than 19 euros, they may require 2 things:
    1. If the products are for pets, a message will be displayed: “Unable to send”.
    2. If the products are clothing, the following message will be displayed: “Shipping costs are 9 euros”.
  2. If the purchase has an amount between 19 and 40 euros, the message will be indicated: “Shipping costs are 9 euros”.
  3. If the purchase exceeds 40 euros, we must indicate a message that the shipping costs are free.
  4. If the purchase exceeds 200 euros, we must display a message with a discount code: CODESC33.

For this problem we have the following data:

  1. The total amount of the shopping cart comes in a variable $total_purchase with a positive decimal number. Example: 33.55.
  2. in variable $purchase_type a text comes to us that can be ‘pets’ either ‘clothes’.
  3. We must fill a variable $shipping_price for each of the above possibilities.

Exercise 2 conditionals

We must create a script that tells if a word is a palindrome, that is, it is the same to read it from beginning to end than from end to beginning. To get around it we will use the php function strrev().

Academic exercise 3 on conditionals

We must create a script that indicates which is the largest of 4 numbers, that is, we receive four integer numbers and we must display a message with the largest of the four. The variables with the four numbers will be $a, $b, $c and $d.

test your php code

Once your proposals have been programmed, the best thing you can do is test them. Give a value to the variables that I indicate in the approach of each exercise and test your script in the browser, with the url of your php script. For example: https://127.0.0.1:8080/exercise/exercise1.php.

If errors appear, the best thing you can do is check that you have not forgotten any semicolons, parentheses, keys or dollars. If you are not able to solve it, look at the error messages that appear, they usually indicate the line and the specific problem.

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
  • if and else if conditionals in php
  • Conditional Switch
  • php strrev() 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