0% found this document useful (0 votes)
6 views9 pages

Solved Exercises PHP (1) Control Structures

The document contains a series of PHP exercises focused on basic programming concepts such as loops, conditionals, and functions. Each exercise includes a specific task, such as displaying floor and door numbers, checking age eligibility, and generating Fibonacci series. The exercises aim to enhance understanding of PHP syntax and logic through practical coding challenges.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Solved Exercises PHP (1) Control Structures

The document contains a series of PHP exercises focused on basic programming concepts such as loops, conditionals, and functions. Each exercise includes a specific task, such as displaying floor and door numbers, checking age eligibility, and generating Fibonacci series. The exercises aim to enhance understanding of PHP syntax and logic through practical coding challenges.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Deployment of Web Applications

Basic PHP exercises (1)

Exercise 1. Nested loops, display the floor and door numbers of a block. For example:

Floor 1–door 1
Floor 1–door 2
Thus until reaching Floor 5–door 4

For($i=1;$i<=5;$i++)

For($b=1;$b<=4;$b++){

Echo 'floor' . $i . ': door' . $b . '.<br>';

//This is NOT I say 'floor'.$i. ': door'.$b. '.<br>;}

Exercise 2. Using the function mt_rand(x,y), create a script that checks if a person is in
age of working or not, remember that the working age is from 16 to 65 years old. It will show by
screen "The user is of working age" or conversely "The user is NOT of working age".

age=mt_rand(1,100);

if(($age>=16)&&($age<=65))

{ $age years<br>The user is of working age

else

{ echo "$age years<br>The user is not of working age";

Exercise 3. Create a script that checks if the user is of legal age. (use the function
mt_rand(x,y) to obtain the age

age=random number between 1 and 100;

if($age>=18)

{
Implementation of Web Applications

echo $age." Is of legal age";

else{

$age. Is a minor.

Exercise 4. Check the age every 10 years, from 0 to 100. Show a message.
indicating in which range the user is. Use the IF control structure. (use mt_rand(x,y)).

age=mt_rand(1,100);

if($age>=1 && $age <=10)

the user is

} else if($age >= 11 && $age <= 20)

the user is

}else if($age>=21 && $age <=30)

the user is

}else if($age>=31 && $age <=40)

the user is

}else if($age>=41 && $age <=50)

the user is

}else if($age>=51 && $age <=60)

the user is

}else if($age>=61 && $age <=70)

the user is

}else if($age>=71 && $age <=80)


Deployment of Web Applications

the user is

}else if($age>=81 && $age <=90)

the user is

} else if ($age >= 91 && $age <= 100)

the user is

Exercise 5. Create a script that, based on three integer variables called $a, $b, and $c, displays
display the value of the largest of them. If, for example, we assign the values 15, 94, and 73 to the
the highest value among 14, 94, and 73
es: 94

//Exercise 5
$a=mt_rand(1,100);
$b=mt_rand(1,100);
//$c=mt_rand(1,100);
$b=70;
$c=70;
They do not take into account whether two numbers are equal.
if($a>$b && $a>$c){
</br>The largest number between
}
if($b>$a && $b>$c){
</br>The largest number between
}
if($c>$b && $c>$a){
<br>The largest number among
}

They check that two numbers are equal and the third is smaller.
if ($a==$c && $b<$c)
{
</br>The largest number between
}

if ($a==$b && $c<$a)


{
</br>The largest number between
}
Deployment of Web Applications

if ($b==$c && $a<$c)


{
The largest number between
}

Exercise 6. Create a script that converts pesetas to euros from 50 to 1000 inclusive.
included, jumping in increments of 50. Use a FOR loop to carry out the exercise, the exchange rate
it will be 166,386.

Exercise 7. Repeat the previous loop using WHILE.

Exercise 8. Display on screen all the even numbers between 1 and 1000.
rows of 5 numbers, 10 numbers, 15 numbers...

//Exercise 8
$tope=5;
for ($i=1;$i<=1000;$i++)
{
for($j=1;$j<=$tope*2;$j++)
{
if($i%2==0&&$i<=1000)
{
echo $i,;
}
$i++;
}

$tope+=5;
</br>
}//End of the 1st FOR

// // if($i==$tope)
// // {
// // echo '</br>';
// // }

$tope=5;

2nd Version, Daniel Martín

$t=0;
$s=1;
for ($i=0;$i<=1000;$i++)
{
if($i%2==0){
echo " $i,";
Implementation of Web Applications

$t++;
}

if($t%(5*$s)==0){
<br>
$t=0;
$s++;
$i++;//Added
}
} // End of the 1st FOR

Exercise 9. Create a script that, based on two integer variables $a and $b, tells me if a is greater than,
less than or equal to b. (uses mt_rand)

$a=mt_rand(1, 10);

$b=mt_rand(1, 10);

if($a > $b){

$a is greater than $b

if($a < $b){

$a is less than $b

if($a == $b){

$a is equal to $b

Exercise 10. Using a FOR loop, calculate the sum of all even integers that exist.
between 1 and 1000.

$sum=0;

for($i=0;$i<=1000;$i++)

if($i%2==0)

$suma=$suma+$i;

}
Deployment of Web Applications

echo "$suma";

Exercise 11. Do the same exercise as before, but in this case using a while loop.

Exercise 12. Using the IF-ELSEIF control structure, create a script that displays a greeting.
customized according to the time of day.

$date=date("H");
random(0,23); // This is to see that it works
if ($fecha>=8 & $fecha<12)
{
Good morning, it is:
}
else if ($date >= 12 & $date < 22)
{
Good afternoon, it is:
}
else echo "Good evening, it is: " . $date;

Exercise 13. Show the Fibonacci series from the number 1 to 10000. Remember that the series
Fibonacci works by adding the two previous values.

1, 1, 2, 3, 5, 8, 13 ...

$n1=1;
$n2=0;
for ($i=0;$i<=1000;$i++)
{
$suma=$n1+$n2;
$n1=$n2;
$n2 = $sum;
echo $sum." ";
}

Exercise 14. Display on screen a 10 by 10 table with the numbers from 1 to 100.

$cont=0;
echo "<table>";
for($i=0;$i<10;$i++)
{
echo "<tr>" ;
for ($j=0;$j<10;$j++)
{
<td>
$cont++;
echo $cont;
</td>
Web Application Deployment

}
</tr>
}
</table>
<br>
<br>
<br>

//Version David P.
define('TAM',10);
ECHO "<table border=1>";
$n=1;
for ($n1=1;$n1<=TAM;$n1++)
{
IF ($n1%2==0)
echo "<tr bgcolor=#bdc3d6>;"
else
echo "<tr>";
for ($n2=1; $n2<=SIZE; $n2++)
{
<td>
$n=$n+1;
}
</tr>
}
</table>

Exercise 15. Create the following pyramid:

+
++
+++
++++
+++++
++++
+++
++
+

for($i=1;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
+
}
<br>
}
for($i=5;$i>=1;$i--)
{
for($j=1;$j<=$i;$j++)
{
Web Applications Implementation

+
}
<br>
}

Exercise 16. Design an algorithm that takes three numbers and finds out if one of them
they are the sum of the other two.

<form action="ejer16.php" method="post">


<input type="number" id="campo1" name="campo1" placeholders="Inserta un dato"/>
<input type="number" id="campo2" name="campo2" placeholders="Inserta un dato"/>
<input type="number" id="campo3" name="campo3" placeholders="Inserta un dato"/>
Submit
</form>

//forloopfortable
if(isset($_POST["campo1"])){

$CampoTexto = $_POST["campo1"];
$CampoTexto2 = $_POST['campo2'];
$CampoTexto3 = $_POST['campo3'];
$aux=$CampoTexto2+$CampoTexto3;
$aux1=$TextField+$TextField3;
$aux2 = $CampoTexto2 + $CampoTexto;
if($TextField==$aux){
$aux is the sum of $CampoTexto2 and $CampoTexto3
}
if($CampoTexto2==$aux1){
$aux1 is the sum of $CampoTexto and $CampoTexto3
}
if ($CampoTexto3 == $aux2) {
$aux2 is the sum of $CampoTexto2 and $CampoTexto.
}
}
?>

Exercise 17. Create a script that sums the first 1000 numbers.

$sum=0;
for($i=0;$i<=1000;$i++)
{
$sum=$sum+$i;
Web Application Deployment

}
The sum is $suma

Exercise 18. Design an algorithm that calculates the sum of the first 20 even numbers and the
product of the first 20 odd numbers simultaneously.

$pares=0;
$impares=1;
for($i=2;$i<=40;$i++)
{
if($i%2==0)
{
$pares = $pares + $i;
}
elseif($i%2!=0)
{
$odds=$odds*$i;
}
}
echo $pairs;
<br>
echo $impares;

You might also like