Sesion 03 - Características Del Lenguaje PHP
-
Upload
sistemasunica -
Category
Documents
-
view
866 -
download
0
Transcript of Sesion 03 - Características Del Lenguaje PHP
![Page 1: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/1.jpg)
Lenguaje PHP
FACULTAD DE INGENIERIA DE SISTEMAS – F.I.S. - UNICA
Ing. Erwin Peña CasasMag. Ingeniería de Sistemas
Características del Lenguaje
![Page 2: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/2.jpg)
Inserción código PHP
Los script basados en PHP se insertan en el código HTML encerrados entre los caracteres “<?” y “?>”
<?
Echo “Prueba de código PHP”;
?>
Toda línea en PHP, termina en un punto y coma “;”, como se puede apreciar en el ejemplo.
![Page 3: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/3.jpg)
Otras Formas
Forma 1
<?PHP
Sentencias
?>
Forma 2
<SCRIPT LANGUAJE=“PHP”>
Sentencias
</SCRIPT>
![Page 4: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/4.jpg)
Comentarios
Permiten, documentar secciones de código, permitiendo un mejor entendimiento:
Delimitadores:// ó # desde el punto de inserción hasta el fin de línea.
/* y */ puede afectar a varias líneas
/*este es un comentario */
//este es otro comentario
![Page 5: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/5.jpg)
Variables
En PHP, un dato se puede almacenar de dos formas: Temporal y Permanente
El almacenamiento temporal se da con el uso de las variables.
El almacenamiento permanente se da con el almacenamiento del dato en un tabla de una base de datos.
![Page 6: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/6.jpg)
Ámbito de las Variables
Global a nivel del fichero actual y los ficheros incluidos.
Dentro de una función son locales a la función.
Las variables se usarán en la operaciones y cálculos de los procesos, una vez validados se almacenarán en un tabla o se imprimirá en pantalla.
![Page 7: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/7.jpg)
Declaración de Variables
Toda variable inicia con una letra (obligatoriamente) a la misma se le antepone el símbolo “$”.
La asignación de un dato a una variable se da por medio del operador “=“.
Ejemplos
$facultad=“FIME”, $precio=125, $ruc=“20452514037”
Mal definidas
$123=“prueba”, $edad-alumno=50
![Page 8: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/8.jpg)
PHP no necesita que se declaren la variables, por que una variable puede tener en un determinado momento un valor y en otro momento otro valor.
<?
$nombre=“Juan Carlos”;
Echo “hola “.$nombre.”<br>”;
$nombre=1200;
Echo $nombre;
?>
![Page 9: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/9.jpg)
Concatenación
Para unir concatenar (unir) dos expresiones, debe usarse un punto “.”, entre las dos expresiones.
<?
$nombre=“Juan Carlos”;
Echo “hola “.$nombre;
?>
![Page 10: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/10.jpg)
Ejemplos (Determine su resultado)
<?
$valor=7;
Echo “Tu Numero es : “.$valor;
?>
<?
$valor=“7”;
Echo 3+$valor; ¿Qué resultado es?
?>
![Page 11: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/11.jpg)
Tipos de datos mas Comunes
Entero, en decimal, octal o hexadecimal
$valor=1200;
Punto Flotante $valor=2.252e5;
Array $nombres[2]=“Juan”;
String $Producto=“Monitor”;
La variables lógicas o booleanas no existen;
Falso = nulo, cero, arreglo o cadena vacía
Verdadero = lo opuesto a Falso
![Page 12: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/12.jpg)
Determinando tipo de variable
Gettype.- determina el tipo de variable usada
<?
$producto=“Teclado”;
$precio=48;
echo gettype($producto);
echo “<br>”;
echo gettype($precio);
?>
![Page 13: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/13.jpg)
CONSTANTES
Las constantes almacenan valores fijos que no cambiarán, no llevan el signo “$”
No pueden contener Arreglos, redefinirlas, borrarlasUna constante se define con la orden “define”.<?
define(“PI”,3.1416);define(“BIENVENIDA”,”Bienvenidos a mi Web Site”);$areacirculo =PI*25;echo BIENVENIDA;echo “Resultado Area del Circulo : “.$areacirculo;
?>
![Page 14: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/14.jpg)
Por convencionalismo, se escribe la constantes con MAYUSCULAS (se diferencia mayúscula de minúsculas).
<?
define(“BIENVENIDA”,”Bienvenido a mi Web Site”);
$Nombre=“Juan Carlos”;
echo $nombre.BIENVENIDA;
?>
Cual es el error????
![Page 15: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/15.jpg)
OPERADORES
Matemáticos
suma “+” $a + $b
resta “-” $a - $b
multiplicacion “*” $a * $b
division “/” $a / $b
modulo/residuo “%” $a % $b
![Page 16: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/16.jpg)
Autoincremento/Autodecremento
Asumiendo $a=5
Pre-incremento echo ++$a; incrementa a en 1, luego imprime $a R =
Post-incremento echo $a++; imprime a luego aumenta 1 R=
Por equivalencia como sería autodecremento??
![Page 17: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/17.jpg)
Operadores Lógicos
Y lógico $a y $b
O lógico $a or $b
Negación !$a
O Exclusivo $a xor $b (verdadero si $a o $b es verdadero; pero no ambas).
![Page 18: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/18.jpg)
Asignación, Igualdad e Identidad
Asignación $a=$b asigna el valor de $b a $a
Igualdad $a==$b compara $a con $b
Identidad $a===$b
compara $a con $b, y verifica si el dato es del mismo tipo
![Page 19: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/19.jpg)
$a=“10”;
$b=10;
If ($a==$b){
echo “Son iguales los valores”;}
Else{
echo “Son diferentes los valores”;
}
Cual es el resultado???
![Page 20: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/20.jpg)
$a=“10”;
$b=10;
If ($a===$b){
echo “Son iguales los valores”;}
Else{
echo “Son diferentes los valores”;
}
Cual es el resultado???
![Page 21: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/21.jpg)
Operadores Relacionales
Mayor que $a > $b
Menor que $a < $b
Diferente $a != $b
Por analogía cuales serán:
Mayor o igual que ??
Menor o igual que ??
![Page 22: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/22.jpg)
Dado los valores para $a=8 y $b=5, que valores debe dar los siguiente comparaciones:
$a!=$b
$a<>$b
$a !==$b
$a<$b
$a>$b
$a<=$b
$a>=$b
$a==$b
$a===$b
![Page 23: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/23.jpg)
Atajos en Asignación
Asumiendo $a=7
$a +=3 ; Rpta :10
$a -=3 ; Rpta : 4
$a *=3 ; Rpta :21
$a /=3 ; Rpta :2.3
$a %=3 ; Rpta :1
$a ^=3 ; Rpta :343
$a .=3 ; Rpta :73 Concatenación
![Page 24: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/24.jpg)
VARIABLES EXTERNAS
Formularios HTML(GET y POST cuando se envía un formulario a un script PHP, las variables de dicho formulario pasan a estar automáticamente disponibles en el script gracias a PHP.
Estructura:
print $_POST[‘variable’];
print $_GET[‘variable’];
![Page 25: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/25.jpg)
<html><head><title>Prueba de PHP</title></head><body><form name=form1 method=get action=ejemplo5.php>Nombre :<input name=nombre type=text><br>Edad :<input name=edad type=text><br><input type=submit name=submit value=Enviar><br></form><br><br><?if($_GET['nombre']=="erwin")
echo "bienvenido ".$_POST['nombre'];else
echo "No se encontro el Cliente";?></body></html>
![Page 26: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/26.jpg)
<html>
<body><form method = "POST" action = "buscador.php">
<strong>Ingresa tu nombre:</strong>
<input type="text" name="buscar" size="20"><br><br>
<input type="submit" value="Buscar">
</form>
</body>
</html>
Evalúa cadena vacía – buscar.html
![Page 27: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/27.jpg)
<?if (!isset($buscar)){ echo "Debe especificar un nombre a buscar"; echo "<p>Debe especificar un nombre a buscar</p> \n";echo "<p><a href=buscar.html>Volver</p> \n"; echo "</html></body> \n"; exit; } ?>
buscador.php
![Page 28: Sesion 03 - Características Del Lenguaje PHP](https://reader031.fdocument.pub/reader031/viewer/2022012913/5571f26349795947648c8531/html5/thumbnails/28.jpg)
CREAR UN FORMULARIO CON POST