Skip to content
Pierre Germain edited this page Apr 25, 2024 · 22 revisions

Welcome to the MyPHP wiki!

MINT LAMP Express Install / Test

sudo apt install apache2
sudo apt install mysql-server
sudo apt install php7.0 libapache2-mod-php7.0 php7.0-mysql php7.0-curl php7.0-json 
sudo service apache2 restart
cd /var/www/html/
chown pierre .
mv index.html index2.html
echo '<?php phpinfo(); ?>' > info.php

Go to http://localhost/info.php

Basic Commands: https://manpages.debian.org/stretch/apache2/index.html

Creación de Clases e instanciar Objetos

<?php
class miClase
{
  public function __construct()
  {
    echo "Hola";
  }
}
$miObjeto = new miClase();
?>
  • __construct se ejecuta automáticamente

Variables


    // Variables
    $nombre = "juan"; // String
    $edad = 13; // Entero
    $meses = "15.5"; // Decimales
    
    echo "nombre:".$nombre."<br>";
    
    // Array v1
    $arreglo[0] = $nombre;
    $arreglo[1] = $edad;
    $arreglo[2] = $meses;
    
    echo "edad:".$arreglo[1]."<br>";
    
    // Array v2
    $monedas = array
      (0=> 'peso', 
       1=> 'dolar', 
       2=> 'euro'); 
    echo "peso:".$monedas[0]."<br>";
     
   }
}

Operadores



    // Operadores
    $a = 6;
    $b = 4;
    $c = $a + $b; // suma
    $d = $a - $b; // resta
    $e = $a * $b; // multiplicacion
    $f = $a % $b; // resto
    echo "f:".$f."<br>";

    // Comparación de valores
    if ($a >  $b) {echo "mayor<br>";}
    if ($a >= $b) {echo "mayor o igual<br>";}
    if ($b <  $a) {echo "menor<br>";}
    if ($a <= $a) {echo "menor o igual<br>";}
    if ($a == $a) {echo "igual<br>";}

    // Comparación de tipo de dato
    $aa = 3;
    $bb = '3';
    if ($aa === $bb) {
      echo "si es el mismo tipo de dato"."<br>";
    }
    else {
      echo "no es el mismo tipo de dato"."<br>";
    }

    // OPERADORES LÓGICOS
    // Meter los operadores logicos
    // en parentesis porque el "="
    // tiene preferencia sobre ellos

    // And 
    $a = true;
    $b = false;
    $cc = ($a and $b);
    $cc = ($a & $b);
    if ($cc == true){
      echo "cc: true<br>";
    }
    else {
      echo "cc: false<br>";
   }

    // Or 
    $dd = ($a or $b);
    $dd = ($a || $b);
    if ($dd == true){
      echo "dd: true<br>";
    }
    else {
      echo "dd: false<br>";
   }
    echo "dd:".$c."<br>";


    // Incremento y decremento
    $a++;
    $a--;

Estructuras IF - ELSE


    $a = 10;
    $b = 5;
    if ($a > $b){
    	echo $a." es mayor que ".$b;
    }
    else {
    	echo $a." no es mayor que ".$b;
    }

Trabajar con Arrays

    // Ordenar un Array e imprimirlo
    $alum = array ("Gerard","Juan","Fran","Alba","Ana");
    sort ($alum);
    foreach ($alum as $key => $val){
    	echo "alum [" .$key . "] =". $val . "<br>"; 
    }
     
    // Ordenar Array inversamente
    echo "<hr>";
    rsort ($alum);
    foreach ($alum as $key => $val){
    	echo "alum [" .$key . "] =". $val . "<br>"; 
    }

    // Ordenar Array por índice
    // Uso de Array asociativos
    echo "<hr>";
    $nombres = array(
    	"h" => "Ale",
    	"e" => "Marco",
    	"z" => "Bryan",
    	"a" => "America");

    ksort ($nombres);
    foreach ($nombres as $key => $val){
    	echo "nombres [" .$key . "] =". $val . "<br>"; 
    }
     

Obtener key y valor del primer elemento de un array

$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

reset($array);      // Place pointer on the first element of the array
$value = current($array);  // Get the value of the first element of the array
$key = key($array); // Get the key of the current element of the array

var_dump($key);

Poner key como valor en un array

      // Ensure that the same value is duplicated as key and value of the array.
      $available_countries = array_combine($available_countries, $available_countries);