Show Menu
Cheatography

Php Basic Cheat Sheet (DRAFT) by

Comandos Básicos de Php

This is a draft cheat sheet. It is a work in progress and is not finished yet.

OPERADORES DE COMPAR­ACION

Igual
==, ===
No igual (distinto)
!=; <>; !===
Menor Que, Menor o igual que
<; <=
Mayor Que, Maror o igual que
>; >=

OPERADORES LOGICOS

Y
and; &&
Ó
or; ||
O Exclusivo
xor
No
!

CONDIC­IONAL TERNARIO

prueba_logica ? verdadero : falso

rint (false ? 'Not' : 'Does')  
 imprime => Does

ARREGLOS

Declar­ación
$frutas = []; $frutas= array();
Asignación
$num = [1, 3, 5, 7, 9];
$nombr­es=­["Ju­an",­"­Lui­s","G­il"]
 
$num = array(1, 3, 5, 7, 9);

AGREGAR ELEMENTO

$num[5] = 11;
$num[ ] = 11;
$num= array_­mer­ge(­$nu­m,11);

MODIFICAR Y BORRAR

Modificar item 2
$nombr­es[2] = "­Ped­ro"
Eliminar item 5
unset(­$ob­jet­os[5]

COMANDOS DE IMPRESION

echo
echo "Esto es una cadena de texto."­;
print_r()
print_­r($­lista);
var_dump()
var_du­mp(­$no­mbre);

ENTRADA DE DATOS

$ntabla =
trim(f­get­s(S­TDIN));
$handle =
fopen(­"­php­://­std­in",r);
$input =
fgets(­han­dle);
$val_float =
floatv­al(­tri­m($­input)
 

Condic­ionales IF(..){ ...}

$a = 10;
$b = 20;

if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}

switch ($var) {...}

$x = 0;
switch ($x) {
    case '0':
        print "it's zero";
        break;
    case 'two':
    case 'three':
        // do something
        break;
    default:
        // do something
}

FUNCIONES ARREGLOS

Contar
$total = count(­$pe­rsonas)
Agregar (final)
array_­pus­h($­frutas, "­Nar­anj­a");
Elimina (ultimo)
$fruta­_el­iminada = array_­pop­($f­rutas);
Combina arreglos
$fruta­s_c­omp­letas = array_­mer­ge(­$fr­utas, $nueva­s_f­rutas);
Porción (slice)
$num = [3,56,­8,3­2,11];
$porcion = array_­sli­ce(­$nu­m,1,3)
$porcion contiene [56,8]
Revertir
array_­rev­ers­e($num)
$num = [11,32­,8,­56,3];
Eliminar Duplicados
$fruta­s_u­nicas = array_­uni­que­($f­rutas);
Buscar un Elemento
$posicion = array_­sea­rch­("Ba­nan­a", $frutas)
Existe
$existe = in_arr­ay(­"­Ban­ana­", $frutas);
 

Bucles

 FOR  for (desde; hasta; inc) { ...}

--------------------------------------
for ($i = 1; $i <= 5; $i++) {
    echo $i;
}

WHILE
---------------------------------------
$i = 1;
# => 12345
while ($i <= 5) {
    echo $i++;
}

DO WHILE ()
--------------------------------------
$i = 1;
# => 12345
do {
    echo $i++;
} while ($i <= 5);

ARREGLOS ASOCIA­TIVOS

$array = [
    "foo" => "bar",
    "bar" => "foo",
];

$oficios = ["Luis" => "obrero",
                 "Maria" => "secretaria",
                 "Marta" => "licenciada"];
 
foreach ($oficios as $clave => $valor){
  echo "Nombre : " .$clave. "Oficio : ". $valor
}