Sie sind auf Seite 1von 8

1

Fundamentos de PHP

Laboratorio
Fundamentos de PHP

Mediador: Breyson Meza

Fundamentos de PHP

Variables, constantes y operadores


Implementar los siguientes cdigos: Sintaxis_01.php <html> <head><title>Primer Programa</title></head> <body> <p>Antes de PHP</p> <?php echo "Bienvenidos al curso...!!!"; ?> <p>Despues de PHP</p> </body> </html>

Sintaxis_02.php <?php //definiendo las variables $x="Sistemas UNI"; $y="Bienvenidos"; $z="PHP"; echo $y." a ".$x."- curso: <b>".$z."</b>"; //definiendo constantes echo "<hr>"; define("NOMBRE",'Juan Perez'); echo NOMBRE; //operadores echo "<HR>"; $x=5; $y=3; echo "La suma de ".$x." y ".$y." es:".($x+$y); echo "<BR>"; echo "La resta de $x y $y es:".($x-$y); echo '<BR>'; echo 'El producto de $x y $y es:'.($x*$y); echo "<BR>"; echo $x++."<BR>";

Mediador: Breyson Meza

Fundamentos de PHP

echo $x."<BR>"; echo --$y."<BR>"; echo "<A href=\"http://www.google.com\">google</A>"; echo "<BR>"; echo "<A href='http://www.yahoo.com'>yahoo</A>"; //operadores de relacin echo "<HR>"; $x=6; $y=7; echo $x==$y; echo "<BR>"; echo $x!=$y; $z=$x<$y; //imprimiendo la descripcin de la variable con var_dump() var_dump($z); $z=$x>$y; var_dump($z); //operadores lgicos $p=3; $q=5; $r=($p<$q)||($p>$q)||($p==$q); $t=!($p==$q); var_dump($r); var_dump($t); //agregando contenido dinmico echo "<HR>"; echo date('y-m-d')." -- ".date('Y-M-D H:i')."<BR>"; echo __FILE__."<BR>"; echo dirname(__FILE__)."<BR>"; echo "<b>".rand(1,6)."</b>"; //Convertir tipos $x="123.45"; var_dump($x); $y=(float)$x; var_dump($y); $z=(int)$y; var_dump($z); //Variables de tipo Variable echo "<HR>"; $varname="var1"; $$varname=5; echo $var1; /* las variables solo tienen vida en esta pagina si queremos mantener sus valores a lo largo de la navegacin(otras pginas), se debe usar: -sesiones, cookies, formularios, etc. */ echo "<br>fin<br>";//la ms comn print("fin<br>");//para dar formatos(nmeros) die("fin");//imprime y termina la ejecucin echo "sigue...."; ?>

Mediador: Breyson Meza

Fundamentos de PHP

Control de flujos
Instruccin ifelse
Sintaxis_03.php <?php if($x){ echo "variable con valor<br>"; }else{ echo "Variable sin valor<br>"; } $x=20; if($x){ echo "variable con valor<br>"; }else{ echo "Variable sin valor<br>"; } //manejando los mensajes if(isset($y)){//pregunta si la variable tiene valor echo "variable con valor<br>"; }else{ echo "Variable sin valor<br>"; } $z=0;// o $z=null if($z){//pregunta si la variable tiene valor echo "variable con valor<br>"; }else{ echo "Variable sin valor<br>"; } //validacin $dato="hola"; $dato=array("hola","que tal","como te va"); if(!isset($dato)){//pregunta si tiene valor die("Datos no enviado...!!!"); }elseif(!is_array($dato)){//pregunta si es arreglo die("el dato no es un arreglo...!!!"); }else{ var_dump($dato); } ?>

Sintaxis_04.php <?php $accion=rand(1,4); switch ($accion){ case 1: $label="Insertar"; break; case 2: $label="Eliminar"; break; case 3: Mediador: Breyson Meza

Fundamentos de PHP

$label="Actualizar"; break; default: $label="Consultar"; break; } echo $accion; ?> <input type="button" value="No dinamico"> <input type="button" value="<?php echo $label?>">

Iteradores
Sintaxis_05.php <?php define("ROOT",__FILE__); $long=strlen(ROOT); $pos=0; $bs="\\"; echo ROOT."<br>"; echo $bs."<hr>"; $aux=0; while($pos<$long){ $x=substr(ROOT,$pos,1); if($x==$bs){ $carpeta=substr(ROOT,$aux,($pos-$aux)); echo $carpeta."<br>"; $aux=$pos; } $pos++; } ?>

Sintaxis_06.php <table> <tr><th>n</th><th>n^3</th><th>Aleatorio</th></tr> <?php for($i=1;$i<101;$i++){ if($i%2==0){ $color="#FFFF99"; }else{ $color="#FF99FF"; } ?> <tr bgcolor="<?php echo $color?>"> <td><?php echo $i?></td> <td><?php echo pow($i,3)?></td> <td><?php echo rand(1,6)?></td> </tr> <?php

Mediador: Breyson Meza

Fundamentos de PHP

} ?> </table>

Sintaxis_07.php <select name=""> <?php for($i=1;$i<=31;$i++){ ?> <option><?php echo $i?> </option> <?php } ?> </select> <hr> <?php echo "<select multiple>"; $anho=date('Y');//$anho=2012; while($anho>=1980){ echo "<option>".$anho; $anho--; } echo "</select>";

Arreglos
Sintaxis_08.php <?php //definiendo arreglo del tipo Vector(Matriz unidimens) $paises=array("Peru","Brasil","Argentina","Uruguay"); var_dump($paises); //accediendo a un valor echo $paises[2]; //cantidad de elementos: echo "<p>Cantidad de elementos: ".count($paises)."</p>"; foreach($paises as $value){ echo $value."<br>"; } $i=1; foreach($paises as $value){ $tablaPuntuacion[]=$value." ".$i++; } $tablaPuntuacion[]="Chile"; var_dump($tablaPuntuacion); echo "<pre>"; print_r($tablaPuntuacion);

Mediador: Breyson Meza

Fundamentos de PHP

echo "</pre>"; ?>

Sintaxis_09.php <?php //definiendo un vector asociativo $estadios=array("Universitario"=>"Monumental", "Alianza"=>"Matute", "Barcelona"=>"Campo Nuevo", "BocaJr"=>"Bombonera", "Cienciano"=>"Garcilazo"); var_dump($estadios); //recorriendo foreach($estadios as $k=>$v){ echo "El estadio de ".$k." es : ".$v."<br>"; } //ordenar arreglos asort($estadios);//arsort por el valor var_dump($estadios); echo "<hr>"; krsort($estadios);//krsort por la llave var_dump($estadios); ?>

Sintaxis_10.php <?php //definiendo matrices (bidimensionales) $datos["carlos"]=array("clave"=>md5('carlitos'),"estado"=>'Activo',"rol"=>"Administrador"); $datos["jose"]=array("clave"=>md5('pp'),"estado"=>'Activo',"rol"=>"Lector"); $datos["maria"]=array("clave"=>md5('maria01'), "estado"=>'Inactivo', "rol"=>"Editor"); var_dump($datos); foreach($datos as $k=>$v){ //filas echo $k.":"; foreach($v as $kv=>$vv){//columnas echo "<b>".$kv."</b>=".$vv." "; } echo "<br>"; } //alternativamente: $datos["carlos"]=array(md5('carlitos'),'Activo',"Administrador"); $datos["jose"]=array(md5('pp'),'Activo',"Lector"); $datos["maria"]=array(md5('maria01'),'Inactivo',"Editor"); echo "<hr>"; foreach($datos as $k=>$v){ list($c,$e,$r)=$v; echo "<b>".$k."</b>: (".$c."-".$e."-".$r.")<br>"; } ?>

Mediador: Breyson Meza

Fundamentos de PHP

Cadenas
Sintaxis_11.php <?php //manejo de cadenas $x="sistemasuni"; print($x); var_dump($x); ?> <table> <tr> <td>Posicion</td> <td><?php echo "strpos('$x','uni')"?></td> <td><?php echo strpos($x,'uni')?></td> </tr> <tr> <td>Extraccion</td> <td><?php echo "substr('$x',8,3)"?></td> <td><?php echo substr($x,8,3)?></td> </tr> <tr> <td>Reemplazar</td> <td><?php echo "str_replace('electronica','sistemas','$x')"?></td> <td><?php echo str_replace('sistemas','electronica',$x)?></td> </tr> <tr> <td>Espacios</td> <td><?php echo "trim(' PHP Developper ').'$x'"?></td> <td><?php echo trim(' PHP Developper ').$x?></td> </tr> <tr> <td>Tamao</td> <td><?php echo "strlen('$x')"?></td> <td><?php echo strlen($x)?></td> </tr> <tr> <td>Convertir Mayus/Minus</td> <td> <?php echo "ucwords('$x')<br>strtoupper('$x')<br>strtolower('ABC')"; ?> </td> <td> <?php echo ucwords($x)."<br>".strtoupper($x)."<br>".strtolower('ABC'); ?> </td> </tr> </table>

Mediador: Breyson Meza

Das könnte Ihnen auch gefallen