Last active
January 1, 2023 03:57
-
-
Save HiroNakamura/61198fe8a65e46a20f4a37862636eb47 to your computer and use it in GitHub Desktop.
PHP en ejemplos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Clases en PHP</title> | |
</head> | |
<body> | |
<h1> | |
<?php | |
define(TITULO, "Clases en PHP"); | |
echo TITULO; | |
?> | |
</h1> | |
<hr/> | |
<?php | |
abstract class Abstracta{ | |
abstract function msg(); | |
} | |
trait MyUtilites{ | |
function utilidad(){ | |
echo "Usamos la utilidad de " . $this->nombre . "<br/>"; | |
} | |
} | |
class X extends Abstracta{ | |
private $nombre; | |
use MyUtilites; | |
public function __construct(){ | |
echo "Objeto X creado e inicializado<br/>"; | |
} | |
public function __destruct(){ | |
echo "Objeto X destruido<br/>"; | |
} | |
public function msg(){ | |
return "Hola " . $this->nombre . "<br/>"; | |
} | |
public function setNombre($nombre){ | |
$this->nombre = $nombre; | |
} | |
public function getNombre(){ | |
return $this->nombre; | |
} | |
} | |
interface IDB{ | |
public function conecta(); | |
} | |
class MySQL implements IDB{ | |
public function conecta(){ | |
echo "Nos conectamos a una BD MySQL<br/>"; | |
} | |
} | |
$my_x = new X(); | |
$my_msql = new MySQL; | |
$my_x->setNombre("Roman Robles"); | |
echo "Nombre: ",$my_x->getNombre(),"<br/>"; | |
$my_x->utilidad(); | |
$my_x->msg(); | |
$my_msql->conecta(); | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Clases en PHP</title> | |
</head> | |
<body> | |
<h1>Constantes mágicas</h1> | |
<?php | |
/** | |
* Definicion de clase Informacion | |
*/ | |
final class Informacion{ | |
private $nombre=''; | |
public function __construct(){ | |
echo "Objeto Info creado e inicializado<br/>"; | |
echo "Directorio:",__DIR__,"<br/>"; | |
echo "Archivo: ",__FILE__,"<br/>"; | |
echo "Clase: ",__CLASS__,"<br/>"; | |
echo "Método: ",__METHOD__,"<br/>"; | |
echo "Linea: ",__LINE__,"<br/>"; | |
} | |
public function __destruct(){ | |
echo "Se ha destruido objeto Info<br/>"; | |
} | |
public function getNombre(){ | |
return $this->nombre; | |
} | |
public function setNombre($nombre){ | |
$this->nombre = $nombre; | |
} | |
} | |
$x = "Torquemada"; | |
$info = new Informacion(); | |
$info->setNombre($x); | |
?> | |
<p>Hola, <span> | |
<?php | |
echo $info->getNombre(); | |
?> | |
</span> | |
</p> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
php -S 127.0.0.1:8080 $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html></html><!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>De JS a PHP</title> | |
</head> | |
<body> | |
<h1>De JS a PHP</h1> | |
<p id="prrf"></p> | |
<?php | |
function hola_desde_PHP($nombre){ | |
return $nombre; | |
} | |
?> | |
<script> | |
var nombre = "Soy una variable de JS en PHP."; | |
var variable = "<?=hola_desde_PHP('" + nombre + "');?>"; | |
document.getElementById('prrf').innerText=variable; | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Get datos en PHP</title> | |
</head> | |
<body> | |
<h1>Get datos en PHP</h1> | |
<?php | |
$nombre = "Fernando"; | |
if(strlen($_GET["nombre"]) > 0){ | |
echo "<p>Hola, ",$_GET["nombre"],"</p>"; | |
}else{ | |
echo "<p>Hola, ",$nombre,"<p/>"; | |
} | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Hola mundo</title> | |
</head> | |
<body> | |
<?php | |
$nombre = "Fernando"; | |
$edad = 2021-1981; | |
echo "<h1>Este es el clásico Hola, mundo en PHP!</h1>"; | |
if(9 > 11){ | |
?> | |
<p>Esto aparecerá si 9 > 11</p> | |
<?php | |
}else{ | |
?> | |
<p>Esto aparecerá si 9 < 11</p> | |
<?php | |
} | |
echo "<p>Hola, <span>",$nombre,"</span></p>"; | |
echo "<p>Tienes, <span>",$edad,"</span> años de edad</p>"; | |
echo "<p>Esto se ejecuta en PHP, version: ",PHP_VERSION,"</p>"; | |
echo "<p>El sistema operativo es: ",PHP_OS,"</p>"; | |
echo "<p>Nombre del archivo: ",__FILE__,"</p>"; | |
if(45 > 233 || 23 >155){ | |
?> | |
<p>Esto no es verdadero</p> | |
<?php | |
} else if(true){ | |
?> | |
<p>Esto es verdadero</p> | |
<?php | |
} else{ | |
?> | |
<p>Esto no se cumple</p> | |
<?php | |
} | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('llamado.php'); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "Esto es desde el archivo: 'llamado.php'<br/>"; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require("llamado.php"); | |
require("llamadotwo.php"); | |
echo "<h1><font color='steelgreen'>Programando con PHP</font></h1>"; | |
saludo(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "Desde el archivo: 'llamadotwo.php'<br/>"; | |
function saludo(){ | |
echo "Esto es desde la función: <b>saludo</b><br/>"; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<mascotas> | |
<mascota> | |
<nombre>Caramelo</nombre> | |
<tipo>gato</tipo> | |
</mascota> | |
<mascota> | |
<nombre>Panther</nombre> | |
<tipo>gato</tipo> | |
</mascota> | |
<mascota> | |
<nombre>Soruya</nombre> | |
<tipo>perro</tipo> | |
</mascota> | |
<mascota> | |
<nombre>El Profesor</nombre> | |
<tipo>cuyo</tipo> | |
</mascota> | |
</mascotas> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Se requiere instalar: | |
apt-get install php-xml | |
apt-get search php |grep xml | |
*/ | |
$xml_parser = xml_parser_create(); | |
print $xml_parser; | |
echo "<br/>Verificamos que no sea nulo<br/>"; | |
is_null($xml_parser); | |
echo "Parece que no es nulo<br/>"; | |
$fp = fopen("mascotas.xml","r") or die("Error al abrir archivo."); | |
while($dato = fread($fp,4096)) | |
xml_parse($xml_parser,$data,feof($fp)); | |
fclose($fp); | |
xml_parser_free($xml_parser); | |
echo "Hecho<br/>"; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Sueldo empleado</title> | |
</head> | |
<body> | |
<h1> | |
<?php | |
define("TITULO","Calcular sueldo de empleado"); | |
echo TITULO; | |
?> | |
</h1> | |
<?php | |
function conseguir_aumento($sueldo, $porcentaje){ | |
return ($porcentaje*$sueldo)+$sueldo; | |
} | |
const MAX = 26000.00; | |
$sueldo_base = 12000.00; | |
$sueldo_final = 0.0; | |
$aumento = 25.0/100.0; | |
echo "Sueldo original: $$sueldo_base<br/>"; | |
if($sueldo_base < MAX){ | |
$sueldo_final = conseguir_aumento($sueldo_base,$aumento); | |
} | |
if($sueldo_final > 0){ | |
echo "Sueldo final: $$sueldo_final"; | |
}else{ | |
echo "Sueldo final: $$sueldo_base"; | |
} | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Suma múltiplos</title> | |
</head> | |
<body> | |
<h1>Suma de múltiplos del 1 al 999</h1> | |
<?php | |
define(TAM, 999); | |
$suma_multiplos_tres = 0; | |
$suma_multiplos_cinco = 0; | |
for($i= 1; $i <=TAM ; $i++){ | |
if($i % 3 == 0){ | |
$suma_multiplos_tres+=$i; | |
} | |
if($i % 5 == 0){ | |
$suma_multiplos_cinco+=$i; | |
} | |
} | |
?> | |
<div> | |
<p>Suma múltiplos de 3: <span> | |
<?php | |
echo $suma_multiplos_tres; | |
?> | |
</span> </p> | |
<p>Suma múltiplos de 5: <span> | |
<?php | |
echo $suma_multiplos_cinco; | |
?> | |
</span> </p> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Suma de pares e impares</title> | |
</head> | |
<body> | |
<h1>Suma de pares e impares</h1> | |
<?php | |
define(MAX, 8); | |
$suma_par = 0; | |
$suma_impar = 0; | |
for($cont = 1; $cont<=MAX; $cont++){ | |
if($cont % 2 == 0){ | |
$suma_par+=$cont; | |
}else{ | |
$suma_impar+=$cont; | |
} | |
} | |
echo "Suma de pares: $suma_par<br/>"; | |
echo "Suma de impares: $suma_impar<br/>"; | |
?> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Tarot en PHP</title> | |
</head> | |
<body> | |
<h1> | |
Tarot PHP | |
</h1> | |
<?php | |
$aleatorio = array(1 => 'El mensaje no es la respuesta', 2 => 'Lo que es arriba, es abajo'); | |
$cartas_tarot = array(1 => 'EL MAGO',2 => 'LA PAPISA', 3 => 'LA EMPERATRIZ',4 => 'EL EMPERADOR', 5 => 'EL PAPA', 6 => 'EL ENAMORADO', 7 => 'EL CARRO' , 8 => 'LA JUSTICIA', 9 => 'EL ERMITAÑO', 10 => 'LA RUEDA', 11 => 'LA FUERZA', 12 => 'EL COLGADO', 13 => 'LA MUERTE', 14 => 'LA TEMPLANZA', 15 => 'EL DIABLO', 16 => 'LA TORRE', 17 => 'LA ESTRELLA', 18 => 'LA LUNA', 19 => 'EL SOL', 20 => 'EL JUICIO', 21 => 'EL MUNDO', 22 => 'EL LOCO'); | |
?> | |
<?php | |
echo "<p>",$aleatorio[rand(1, count($aleatorio))],"</p><br/>"; | |
foreach($cartas_tarot as $posicion=>$carta){ | |
echo "<p>El " . $posicion . " es " . $carta,"</p>"; | |
} | |
?> | |
<hr/> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment