note 109504 deleted from language.expressions by danbrown
| From: | danbrown@php.net | Date: | Mon, 30 Jul 2012 15:57:11 +0000 |
| Subject: | note 109504 deleted from language.expressions by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189504@lists.php.net to get a copy of this message | ||
Note Submitter: oetjenj at gmail dot com
----
phvandeberghe,
while it is true that the asterik can return different results the reason you see that many results
in your code example is because your forgot something very important in the switch statement. Switch
starts executing the code beginning with the first match and continues to execute the code until the
whole switch statement ends or it encounters a break. So your code should look like:
<?php
$a = '*';
switch($a)
{
case 1: echo ' (int)one'; break;
case 0: echo ' (int)zero'; break;
case '0': echo ' (string)zero'; break;
case '*': echo ' (string)star'; break;
case true: echo ' (bool)true'; break;
case false: echo ' (bool)false'; break;
}
// ouput
// (int)zero
?>
but still that wouldn't be correct if you want to check all possible "result" you can
have with an asterik. What you probably wanted is something like:
<?php
$a = '*';
if ($a == 1) {
echo " (int)one;";
}
if ($a == 0) {
echo " (int)zero;";
}
if ($a == '0') {
echo " (string)zero;";
}
if ($a == '*') {
echo " (string)asterik;";
}
if ($a == true) {
echo " (bool)true;";
}
if ($a == false) {
echo " (bool)false;";
}
// ouput
// (int)zero; (string)asterik; (bool)true;
?>
which returns:
- (int)zero because every string that is not a number in string format returns 0 as number;
- (string)asterik because the string is equal;
- (bool)true because every defined variable that is not explicitly false or the number 0 (not
through conversion) is true;