note 109504 added to language.expressions
| From: | oetjenj at gmail dot com | Date: | Mon, 23 Jul 2012 11:42:17 +0000 |
| Subject: | note 109504 added to language.expressions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189371@lists.php.net to get a copy of this message | ||
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;
----
Server IP: 69.147.83.197
Probable Submitter: 217.70.192.223
----
Manual Page -- http://www.php.net/manual/en/language.expressions.php
Edit -- https://master.php.net/note/edit/109504
Del: integrated -- https://master.php.net/note/delete/109504/integrated
Del: useless -- https://master.php.net/note/delete/109504/useless
Del: bad code -- https://master.php.net/note/delete/109504/bad+code
Del: spam -- https://master.php.net/note/delete/109504/spam
Del: non-english -- https://master.php.net/note/delete/109504/non-english
Del: in docs -- https://master.php.net/note/delete/109504/in+docs
Del: other reasons-- https://master.php.net/note/delete/109504
Reject -- https://master.php.net/note/reject/109504
Search -- https://master.php.net/manage/user-notes.php