iniciación php 5. ejemplos
DESCRIPTION
Resolución de ejemplo de los bloques I y II del curso de introducción a PHP.TRANSCRIPT
![Page 1: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/1.jpg)
Curso de Introducción a PHP 5 Página 1 de 26
Introducción a PHP 5
Ejemplos y ejercicios
![Page 2: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/2.jpg)
Curso de Introducción a PHP 5 Página 2 de 26
Dado un número y una opción a elegir entre: “uno”,
“cien” y “mil”, imprimir la expresión decimal equivalente a
calcular el tanto por uno, tanto por ciento y tanto por mil.
Ejemplo 01
![Page 3: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/3.jpg)
Curso de Introducción a PHP 5 Página 3 de 26
<? php
// DATOS PARA LOS CÁLCULOS
$number = 56;
$operation = “uno”;
// RESULTADO
echo ejemplo01($number, $operation);
Ejemplo 01
![Page 4: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/4.jpg)
Curso de Introducción a PHP 5 Página 4 de 26
Ejemplo 01function ejemplo01 ($variable, $operation) {
$result = '';
$correct = true;
$operators = array(“uno”, “cien”, “mil”);
if ( !is_numeric($variable) ) {
$result = “El primer parámetro ha de ser un número”;
$correct = false;
}
if ( !in_array($operation, $operators) ) {
$result = “El segundo parámetro no es correcto”.
$correct = false;
}
if ( $correct ) {
$value = 0;
switch($operation) {
case “uno”: $value = $variable; break;
case “cien”: $value = $variable / 100; break;
case “mil”: $value = $variable / 1000; break;
}
$result = “El resultado del tanto por $operation de
$variable es $value.”;
}
return $result;
}
![Page 5: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/5.jpg)
Curso de Introducción a PHP 5
Ejemplo 02
Página 5 de 26
Imprimir una tabla compuesta por 16 flas y 16 columnas.
Cada elemento de la tabla será el resultado de multiplicar
el índice de la fla y columna que ocupa dicha celda.
![Page 6: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/6.jpg)
Curso de Introducción a PHP 5 Página 6 de 26
<? php
// DATOS PARA LOS CÁLCULOS
$dimension = 16;
// RESULTADO
echo ejemplo02($number);
Ejemplo 02
![Page 7: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/7.jpg)
Curso de Introducción a PHP 5 Página 7 de 26
Ejemplo 02function ejemplo02 ($variable) {
$result = “”;
if ( !is_numeric($variable) ) {
$result = “El parámetro dado ha de ser un número”;
} else {
$result .= “<table>”;
for( $i = 0; $i < $variable ; $i++ ) {
$result .= “<tr>”;
for( $j = 0; $j < $variable ; $j++ ) {
$result .= “<td>” . $i * $j . “</td>”;
}
$result .= “</tr>”;
}
$result .= “</table>”;
}
return $result;
}
![Page 8: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/8.jpg)
Curso de Introducción a PHP 5
Ejemplo 03
Página 8 de 26
Modifcar el ejercicio anterior, imprimiendo la cadena “*”
en las celdas cuyo dato sea divisible el número de flas
dadas.
![Page 9: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/9.jpg)
Curso de Introducción a PHP 5 Página 9 de 26
<? php
// DATOS PARA LOS CÁLCULOS
$dimension = 16;
// RESULTADO
echo ejemplo03($number);
Ejemplo 03
![Page 10: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/10.jpg)
Curso de Introducción a PHP 5 Página 10 de 26
Ejemplo 03function ejemplo02 ($variable) {
$result = “”;
if ( !is_numeric($variable) ) {
$result = “El parámetro dado ha de ser un número”;
} else {
$result .= “<table>”;
for( $i = 0; $i < $variable ; $i++ ) {
$result .= “<tr>”;
for( $j = 0; $j < $variable ; $j++ ) {
$result .= “<td>” . format($i * $j, $variable) . “</td>”;
}
$result .= “</tr>”;
}
$result .= “</table>”;
}
return $result;
}
function format( $data, $divisor ) {
return ($result % divisor == 0) ? “*” : $data;
}
![Page 11: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/11.jpg)
Curso de Introducción a PHP 5
Ejemplo 04
Página 11 de 26
Dado un número, imprimir su factorial.
![Page 12: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/12.jpg)
Curso de Introducción a PHP 5 Página 12 de 26
<? php
// DATOS PARA LOS CÁLCULOS
$number = 84;
// RESULTADO
echo ejemplo04($number);
Ejemplo 04
![Page 13: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/13.jpg)
Curso de Introducción a PHP 5 Página 13 de 26
Ejemplo 04function ejemplo04 ($variable) {
$result = 1;
if ( !is_numeric($variable) ) {
$result = “El parámetro dado ha de ser un número”;
} elseif ($variable != 0 ) {
$data = range( 1, $variable);
foreach( $data as $item ) {
$result *= $item;
}
}
return $result;
}
![Page 14: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/14.jpg)
Curso de Introducción a PHP 5
Ejemplo 05
Página 14 de 26
Dado un texto, imprimir el número de palabras que
contiene.
![Page 15: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/15.jpg)
Curso de Introducción a PHP 5 Página 15 de 26
<? php
// DATOS PARA LOS CÁLCULOS
$doc = <<<DATA Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only fve centuries, but also the leap into electronic typesetting, remaining. DATA;
// RESULTADO
echo ejemplo05($doc);
Ejemplo 05
![Page 16: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/16.jpg)
Curso de Introducción a PHP 5 Página 16 de 26
function ejemplo05($variable) {
$size = sizeof(explode(“ “, $variable));
return “El texto contiene $size palabras.”
}
Ejemplo 05
![Page 17: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/17.jpg)
Curso de Introducción a PHP 5
Ejemplo 06
Página 17 de 26
Dado un texto, imprimir el número de letras que posee.
(No contar los signos de puntuación)
![Page 18: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/18.jpg)
Curso de Introducción a PHP 5 Página 18 de 26
<? php
// DATOS PARA LOS CÁLCULOS
$doc = <<<DATA Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only fve centuries, but also the leap into electronic typesetting, remaining. DATA;
// RESULTADO
echo ejemplo06($doc);
Ejemplo 06
![Page 19: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/19.jpg)
Curso de Introducción a PHP 5 Página 19 de 26
function ejemplo06($variable) {
$marks = array( “,”, “.”, “;”, “ “ );
$counter = 0;
for ( $i = 0; $i < strlen( $variable ); $i++ ) {
If ( !in_array( $variable[ $i ] ) ) {
$counter++;
}
}
return “ Existen $counter letras.”
}
Ejemplo 06
![Page 20: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/20.jpg)
Curso de Introducción a PHP 5
Ejemplo 07
Página 20 de 26
Construir una función que valide correos electrónicos.
![Page 21: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/21.jpg)
Curso de Introducción a PHP 5 Página 21 de 26
<? php
// DATOS PARA LOS CÁLCULOS
$mail = “[email protected]”;
// RESULTADO
echo ejemplo07($mail);
Ejemplo 07
![Page 22: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/22.jpg)
Curso de Introducción a PHP 5 Página 22 de 26
function ejemplo07($variable)
{ $expresion = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$';
return ( ereg( $expresion, $variable) ) : “El email es correcto” : “El email es erróneo”;
}
Ejemplo 07
![Page 23: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/23.jpg)
Curso de Introducción a PHP 5 Página 23 de 26
Preguntas
![Page 24: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/24.jpg)
Curso de Introducción a PHP 5 Página 24 de 26
Los ejercicios redactados a continuación,
deberán ser entregados para la obtención de
una mejor califcación.
Entregar la solución de los mismos en un documento con
formato PDF, enviar a [email protected].
![Page 25: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/25.jpg)
Curso de Introducción a PHP 5
Ejercicio 01
Página 25 de 26
Dado un número en formato numérico comprendido
entre 1 y 99, imprimirlo en formato texto.
Construir un formulario para la recepción de datos e
implementar la validación de los mismos.
![Page 26: Iniciación PHP 5. Ejemplos](https://reader033.vdocuments.pub/reader033/viewer/2022050922/557ae40dd8b42a85648b5434/html5/thumbnails/26.jpg)
Curso de Introducción a PHP 5
Ejercicio 02
Página 26 de 26
Dado un día, mes y año, imprimir el día de la semana al
que pertenece.
El mes se introducirá en formato texto.
Construir un formulario para la recepción de datos e
implementar la validación de los mismos.