Home > desarrollo, Spanish, técnico > Evaluación de variables falsas en PHP

Evaluación de variables falsas en PHP

Un simple método para evaluar los valors en PHP:

<?php
$var[0] = ”;
$var[1] = 0; //cero
$var[2] = -1;
$var[3] = false; // solo para  asegurarme ;o)
$var[4] = ‘0’; //cero como string
$var[5] = ‘0a’;
$var[6] = ‘-1’;

$d=0;
foreach($var as $i=>$v){
if ($v) { echo ‘$var[‘.$i.”] is true<br/>\n”;} else { echo ‘$var[‘.$i.”] is false<br/>\n”;}
$d=$i;
}
if ($var[$d+1]) { echo ‘$var[‘.($d+1).”] is true<br/>\n”;} else { echo ‘$var[‘.($d+1).”] is false<br/>\n”;}

Y el resultado de esto, por supuesto, es:

$var[0] is false
$var[1] is false
$var[2] is true
$var[3] is false
$var[4] is false
$var[5] is true
$var[6] is true
$var[7] is false

La duda, para mi, era sobre el valor de “-1”.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: