Conociendo php (201009)
-
Upload
antonio-kobashikawa-carrasco -
Category
Education
-
view
1.760 -
download
1
description
Transcript of Conociendo php (201009)
![Page 1: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/1.jpg)
Conociendo PHP
Introducción a uno de los lenguajes de programación web más usados en Internet
Antonio Kobashikawa [email protected]
@akobashikawa
![Page 2: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/2.jpg)
Expositor
• Desarrollo Web• Programación
![Page 3: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/3.jpg)
Audiencia
• Web• Programación
![Page 4: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/4.jpg)
Esta presentación
• La idea PHPo Páginas web dinámicas
• Instalando PHP• Programando con PHP
o Patroneso Frameworks
![Page 5: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/5.jpg)
La idea
![Page 6: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/6.jpg)
El ciclo web
navegador servidor websolicitud
respuesta
1
2
3
4
proceso
presentación
![Page 7: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/7.jpg)
¿Qué es una página web?
Archivo de texto con indicaciones para mostrar el contenido
<html><body> <h1>Hola</h1> <p>¡Hola Mundo!</p> <img src="h.gif"/></body></html>
Hola¡Hola Mundo!
HTMLpresentación
![Page 8: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/8.jpg)
Página dinámica
Página web con contenido generado por algún proceso.
HTML proceso
<html><body> ...</body></html>
![Page 9: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/9.jpg)
Estilo CGIHTML Perl
![Page 10: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/10.jpg)
Estilo SSIHTML PHP
![Page 11: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/11.jpg)
¿Qué es PHP?
lenguaje de programación para generar contenido
web
<html><body> ...</body></html>
HTML PHP
![Page 12: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/12.jpg)
Proceso PHP
servidorweb
.html
.php
HTML
HTML
El contenido del documento .html es pasado tal cual a la respuesta HTML
Un documento dinámico (como .php) es procesado para obtener HTML
![Page 13: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/13.jpg)
servidorweb
en el proceso, un documento dinámico puede involucrar diversos recursos
base de datos email archivos gráficos etc
Proceso PHP
![Page 14: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/14.jpg)
¿Por qué PHP?
• Es ampliamente soportado por los servicios de hosting
• Es libre• Varios sites importantes se han escrito en
PHP (Wikipedia , YouTube, Yahoo, Facebook, WordPress…)
• Varios paquetes importantes están escritos en PHP (WordPress, OS Commerce, Joomla, Drupal…)
• Es simple, directo, flexible…• Ha evolucionado para soportar programación
orientada a objetos
![Page 15: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/15.jpg)
Instalación
![Page 16: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/16.jpg)
instalando PHP
AMP
instalarPHP
instalarApache
Apache+ módulo PHP
instalarMySQL
configurarApache
configurarPHP
configurarPHP
En la instalación manual hay que bajar cada componente, instalarlo y configurarlo.
![Page 17: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/17.jpg)
LAMP, WAMP, …• Típicamente, PHP se
instala para trabajar junto con Apache y MySQL
• Cuando la instalación aparece en Linux, se la llama LAMP
• Cuando la instalación aparece en Windows, se la llama WAMP
• Y así por el estilo…
A M P
A M P
A M P
L
W
M
![Page 18: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/18.jpg)
XAMPP• Hay varios proyectos
que difunden paquetes que contienen esas instalaciones típicas.
• Usarlos puede facilitar la instalación, y el día a día con el entorno de desarrollo.
• XAMPP es uno de tales paquetes. No sólo provee PHP, sino también Perl y Python y está disponible tanto para Linux, como para Windows, Mac OS X y Solaris
![Page 19: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/19.jpg)
Editores, IDEs• Igual que con HTML,
CSS, Javascript y otros lenguajes web, es posible trabajar usando un simple editor de texto, como Notepad, aunque contar con un entorno de desarrollo puede facilitar mucho las cosas.
• Hay editores de texto muy útiles para programación, como Notepad++, jEdit, …
• Y entornos de desarrollo muy buenos, como Dreamweaver, Aptana, Eclipse, Zend Studio…
![Page 20: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/20.jpg)
Editores, IDEs• Estos son los que suelo
usar• Notepad, si no hay nada
más a la mano• Notepad++ es
excelente, ligero, con FTP y una buena utilidad para convertir Unicode
• Dreamweaver es excelente para el diseño HTML y además tiene buen soporte para PHP
• Aptana esta basada en Eclipse, tiene mucho mejor soporte para PHP, HTML, Javascript…
• Nano, cuando estoy en una consola Linux
![Page 21: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/21.jpg)
Empezando
El panel de control de XAMPP, desde donde podemos iniciar y detener sus servicios
![Page 22: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/22.jpg)
Programando
![Page 23: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/23.jpg)
Programando
lo básico
![Page 24: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/24.jpg)
¡Hola Mundo!
hola_mundo.php ¡Hola Mundo!
![Page 25: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/25.jpg)
¡Hola Mundo!
• El código php se escribe entre etiquetas <?php y ?>
• Para escribir algo en la página, podemos usar echo
• Las cadenas se escriben entre apóstrofes 'así', ó entre comillas "así"
• El nombre del archivo tiene la extensión php
<?phpecho '¡Hola Mundo!';?>
hola_mundo.php
![Page 26: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/26.jpg)
¡Hola Mundo!
<?phpecho '¡Hola Mundo!';?>
hola_mundo.php
htdocs
![Page 27: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/27.jpg)
…¡Hola Mundo!
htdocs/index.php
htdocs
http://localhost/index.php
htdocs/hola_mundo.php http://localhost/hola_mundo.php
![Page 28: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/28.jpg)
…¡Hola Mundo!
unicode guardado como ASCII
unicode guardado como UTF-8 En Notepad++ es fácil comprobar y cambiar el encode
Si usamos unicode, tener cuidado de guardar el archivo en UTF-8 (sin BOM)
![Page 29: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/29.jpg)
…¡Hola Mundo!
<?phpecho '¡Hola Mundo!';?>
¡Hola Mundo!
servidorweb
navegador
http://localhost/hola_mundo.php
.phpHTML
![Page 30: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/30.jpg)
…¡Hola Mundo!
• Un documento php es básicamente html con código inserto
• Los bloques php se reemplazan por su resultado
<html><body>
<h1><?php echo '¡Hola Mundo!';?></h1>
</body></html>
hola_mundo.php
![Page 31: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/31.jpg)
…¡Hola Mundo!
<html><body>
<h1>¡Hola Mundo!</h1>
</body></html>
![Page 32: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/32.jpg)
Cuadrados
cuadrados.php
1 12 43 9…10 100
![Page 33: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/33.jpg)
Cuadrados
<html><body> <h1>Cuadrados</h1> <table border="1"> <?php for ($i=1; $i<=10; $i++) { ?> <tr> <td><?php echo $i;?></td> <td><?php echo $i*$i;?></td> </tr> <?php } ?> </table></body></html>
cuadrados.php
![Page 34: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/34.jpg)
…Cuadrados<html><body> <h1>Cuadrados</h1> <table border="1"> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>2</td> <td>4</td> </tr> … <tr> <td>10</td> <td>100</td> </tr> </table></body></html>
![Page 35: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/35.jpg)
Programando
formularios
![Page 36: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/36.jpg)
Hola
hola_form.php
¡Hola Antonio!
hola.php
Antonio
OK
![Page 37: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/37.jpg)
Hola
<html><body> <form action="hola.php" method="post"> Nombre: <input type="text" name="nombre"/> <input type="submit" value="OK"/> </form></body></html>
hola_form.php
![Page 38: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/38.jpg)
…Hola
<html><body><h1> <?php echo '¡Hola '.$_POST['nombre'].'!';?></h1></body></html>
hola.php
![Page 39: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/39.jpg)
…Hola
<?phpecho '¡Hola '.$_POST['nombre'].'!';?>
¡Hola Antonio!
servidorweb
navegadoraction=hola.php
hola.phpHTML
nombre=Antonio
<form>...</form><form>...</form>
http://localhost/hola_form.php
hola_form.php
![Page 40: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/40.jpg)
Hola
¡Hola Antonio!
hola.php
Antonio
OK
![Page 41: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/41.jpg)
…Hola
<html><body> <?php $nombre = isset($_POST['nombre'])?$_POST['nombre']:''; ?> <form action="hola.php" method="post"> Nombre: <input type="text" name="nombre" value="<?php echo $nombre;?>"/> <input type="submit" value="OK"> </form> <?php if ($nombre) { echo '<h1>¡Hola '.$nombre.'!</h1>'; }?></body></html>
hola.php versión de una página
![Page 42: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/42.jpg)
…Hola
<?php$nombre = ...;?>
<form>...</form>
<?phpif ($nombre) { echo '<h1>¡Hola ' .$nombre.'!</h1>';}?>¡Hola Antonio!
servidorweb
navegadoraction=hola.php
hola.phpHTML
nombre=Antonio
<form>...</form>
http://localhost/hola.php
![Page 43: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/43.jpg)
Programando
un poco más
![Page 44: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/44.jpg)
Bloques básicos
asignación
decisión
repetición
$x = 1+1;
if ($x>0) { ...}
if ($x>0) { ...} else { ...}
switch ($x) { case 1: ... break; ... default ... break;}
for ($i=0; $i<10; $i++) { ...}
while ($i<10) { ...}
do { ...} while ($i<10);
• if• if/else• switch
• for• while• do-while
![Page 45: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/45.jpg)
Arrays( [0]=>2 [1]=>3 [2]=>1 [3]=>'Hola')
'Hola'
231'Hola'
$x = 1;$a = array(2, 3, $x, 'Hola');
a[3]
foreach ($a as $item) { echo $item.'<br/>';}
foreach ($a as $indice=>$item) { echo $indice.': '.$item.'<br/>';}
0: 21: 32: 13: 'Hola'
![Page 46: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/46.jpg)
Funciones
function hola_mundo() { echo '¡Hola Mundo!';}
function hola($x) { echo '¡Hola '.$x.'!';}
hola_mundo();
hola('Mundo');
function saludo($y) { return '¡Hola '.$y.'!';}function hola($x) { echo saludo($x);}
hola('Mundo');
¡Hola Mundo!
![Page 47: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/47.jpg)
Clases
class Hola() { function saludo($y) { return '¡Hola '.$y.'!'; } function hola($x) { echo $this->saludo($x); }}
$h = new Hola();$h->hola('Mundo');
¡Hola Mundo!
![Page 48: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/48.jpg)
include
echo '<h1>Conociendo PHP</h1>';
cabecera.php
echo 'Hola Mundo';
_hola_mundo.php
<html><body> <?php include 'cabecera.php';?> <?php include '_hola_mundo.php';?></body></html>
hola_mundo.php
![Page 49: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/49.jpg)
require
function hola() { return '¡Hola Mundo!';}
funciones.php
echo hola();
_hola_mundo.php
<html><body> <?php require('funciones.php');?> <?php include '_hola_mundo.php';?></body></html>
hola_mundo.php
![Page 50: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/50.jpg)
include & require• include() y require() son similares, la diferencia
es que si no existe el archivo a incluir, include() emitirá una alarma, mientras que require() terminará el programa.
• Un mismo archivo se puede incluir más de una vez.
• Para prevenir duplicaciones en las inclusiones existen include_once() y require_once().
![Page 51: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/51.jpg)
Frameworks• Un framework es una forma de
organizar el trabajo.• Luego de un tiempo desarrollando,
uno empieza a notar ciertos patrones de organización. Aún sin proponérselo, va surgiendo un framework.
• Algunos frameworks muy difundidos en la comunidad PHP son CakePHP, CodeIgniter, Zend, Symphony…
• Un framework estandarizado obliga a seguir ciertas convenciones de organización, pero pone a nuestra disposición código ampliamente probado y optimizado.
![Page 52: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/52.jpg)
Frameworks• Drupal es un CMS construido usado
un framework propio.• El framework de Drupal es muy
extensible y tiene una comunidad muy activa.
• Próximamente el framework se separaría del CMS, pero aún en el nivel actual permite crear aplicaciones más o menos generales.
![Page 53: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/53.jpg)
Enlaces
PHP site http://php.net
W3Schools – PHP http://www.w3schools.com/PHP/default.asp
CakePHP http://cakephp.org
CodeIgniter http://codeigniter.com
Zend Framework http://framework.zend.com
Symphony http://www.symfony-project.org
Drupal http://drupal.org
Coquette Icons http://dryicons.com/free-icons/preview/coquette-icons-set/
![Page 54: Conociendo php (201009)](https://reader033.fdocumento.com/reader033/viewer/2022061214/549b2bbab4795924098b46e4/html5/thumbnails/54.jpg)
Gracias por su atención
preguntas,comentarios
Introducción a uno de los lenguajes de programación web más usados en Internet
Conociendo PHP
Antonio Kobashikawa [email protected]@akobashikawa