Skip to content

condicionales

en php el uso de condiconales es importante tambien para el manejo condiciones y de errores en ellos tenemos

php
<?php
// if, else if, else
$age = 50;
 
if ($age < 30)
{
  echo "Your age is less than 30!";
}
elseif ($age > 30 && $age < 40)
{
  echo "Your age is between 30 and 40!";
}
else
{
  echo "Your age is greater than 50!";
}

// conditional ternary
$marks=40;
print ($marks>=40) ? "pass" : "Fail";

// switch
$favourite_site = 'Code';
 
switch ($favourite_site) {
  case 'Business':
    echo "My favourite site is business.tutsplus.com!";
    break;
  case 'Code':
    echo "My favourite site is code.tutsplus.com!";
    break;
  case 'Web Design':
    echo "My favourite site is webdesign.tutsplus.com!";
    break;
  case 'Music':
    echo "My favourite site is music.tutsplus.com!";
    break;
  case 'Photography':
    echo "My favourite site is photography.tutsplus.com!";
    break;
  default:
    echo "I like everything at tutsplus.com!";
}

// try catch
function inverse($x) {
    if (!$x) {
        throw new Exception('División por cero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
} catch (Exception $e) {
    echo 'Excepción capturada: ',  $e->getMessage(), "\n";
} finally {
    echo "Primer finally.\n";
}
?>
<?php
// if, else if, else
$age = 50;
 
if ($age < 30)
{
  echo "Your age is less than 30!";
}
elseif ($age > 30 && $age < 40)
{
  echo "Your age is between 30 and 40!";
}
else
{
  echo "Your age is greater than 50!";
}

// conditional ternary
$marks=40;
print ($marks>=40) ? "pass" : "Fail";

// switch
$favourite_site = 'Code';
 
switch ($favourite_site) {
  case 'Business':
    echo "My favourite site is business.tutsplus.com!";
    break;
  case 'Code':
    echo "My favourite site is code.tutsplus.com!";
    break;
  case 'Web Design':
    echo "My favourite site is webdesign.tutsplus.com!";
    break;
  case 'Music':
    echo "My favourite site is music.tutsplus.com!";
    break;
  case 'Photography':
    echo "My favourite site is photography.tutsplus.com!";
    break;
  default:
    echo "I like everything at tutsplus.com!";
}

// try catch
function inverse($x) {
    if (!$x) {
        throw new Exception('División por cero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
} catch (Exception $e) {
    echo 'Excepción capturada: ',  $e->getMessage(), "\n";
} finally {
    echo "Primer finally.\n";
}
?>

operadores logicos

EjemploNombreResultado
$a and $bAnd (y)true si tanto $a como $b son true.
$a or $bOr (o inclusivo)true si cualquiera de $a o $b es true.
$a xor $bXor (o exclusivo)true si $a o $b es true, pero no ambos.
!$aNot (no)true si $a no es true.
$a && $bAnd (y)true si tanto $a como $b son true.
$a || $bOr (o inclusivo)true si cualquiera de $a o $b es true.

lanzado bajo la licencia CC-BY-NC-SA