Difference between revisions of "PHP 2"
From Ilianko
Line 6: | Line 6: | ||
$myString = 'ilian'; | $myString = 'ilian'; | ||
$myZeroString = ''; | $myZeroString = ''; | ||
+ | |||
+ | |||
+ | === type casting === | ||
+ | |||
+ | Да се внимава със загубата на информация | ||
+ | |||
+ | |||
+ | |||
+ | $myVar = 1.1; | ||
+ | $myVar = (int) $myVar; | ||
+ | $myVar = (string) $myVar; | ||
+ | $myVar = (bool) $myVar; | ||
+ | |||
+ | var_dump($myVar); | ||
//var_dump( $myZero = $myVar ); | //var_dump( $myZero = $myVar ); | ||
Line 26: | Line 40: | ||
//var_dump( $myZeroString === $myZero ); | //var_dump( $myZeroString === $myZero ); | ||
//var_dump( $myString === $myZero ); | //var_dump( $myString === $myZero ); | ||
+ | |||
+ | |||
+ | <table class="doctable table"> | ||
+ | <caption><strong>Comparisons of <var class="varname"><var class="varname">$x</var></var> with PHP functions</strong></caption> | ||
+ | |||
+ | <thead> | ||
+ | <tr> | ||
+ | <th>Expression</th> | ||
+ | <th> <span class="function"><a href="function.gettype.php" class="function">gettype()</a></span></th> | ||
+ | <th> <span class="function"><a href="function.empty.php" class="function">empty()</a></span></th> | ||
+ | <th> <span class="function"><a href="function.is-null.php" class="function">is_null()</a></span></th> | ||
+ | <th> <span class="function"><a href="function.isset.php" class="function">isset()</a></span></th> | ||
+ | <th><span class="type"><a href="language.types.boolean.php" class="type boolean">boolean</a></span> : <em>if($x)</em></th> | ||
+ | </tr> | ||
+ | |||
+ | </thead> | ||
+ | |||
+ | <tbody class="tbody"> | ||
+ | <tr> | ||
+ | <td><em>$x = "";</em></td> | ||
+ | <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = null;</em></td> | ||
+ | <td><span class="type"><a href="language.types.null.php" class="type NULL">NULL</a></span></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>var $x;</em></td> | ||
+ | <td><span class="type"><a href="language.types.null.php" class="type NULL">NULL</a></span></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><var class="varname"><var class="varname">$x</var></var> is undefined</td> | ||
+ | <td><span class="type"><a href="language.types.null.php" class="type NULL">NULL</a></span></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = array();</em></td> | ||
+ | <td><span class="type"><a href="language.types.array.php" class="type array">array</a></span></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = false;</em></td> | ||
+ | <td><span class="type"><a href="language.types.boolean.php" class="type boolean">boolean</a></span></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = true;</em></td> | ||
+ | <td><span class="type"><a href="language.types.boolean.php" class="type boolean">boolean</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = 1;</em></td> | ||
+ | <td><span class="type"><a href="language.types.integer.php" class="type integer">integer</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = 42;</em></td> | ||
+ | <td><span class="type"><a href="language.types.integer.php" class="type integer">integer</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = 0;</em></td> | ||
+ | <td><span class="type"><a href="language.types.integer.php" class="type integer">integer</a></span></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = -1;</em></td> | ||
+ | <td><span class="type"><a href="language.types.integer.php" class="type integer">integer</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = "1";</em></td> | ||
+ | <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = "0";</em></td> | ||
+ | <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = "-1";</em></td> | ||
+ | <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = "php";</em></td> | ||
+ | <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = "true";</em></td> | ||
+ | <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | <tr> | ||
+ | <td><em>$x = "false";</em></td> | ||
+ | <td><span class="type"><a href="language.types.string.php" class="type string">string</a></span></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>FALSE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | <td><strong><code>TRUE</code></strong></td> | ||
+ | </tr> | ||
+ | |||
+ | </tbody> | ||
+ | |||
+ | </table> | ||
+ | |||
== if == | == if == |
Revision as of 10:14, 20 October 2013
Условни оператори
$myVar = 1; $myZero = 0; $myString = 'ilian'; $myZeroString = ;
Contents
type casting
Да се внимава със загубата на информация
$myVar = 1.1; $myVar = (int) $myVar; $myVar = (string) $myVar; $myVar = (bool) $myVar;
var_dump($myVar);
//var_dump( $myZero = $myVar ); //var_dump( $myVar = $myZero ); //var_dump( $myZero = $myString ); //var_dump( $myZero == $myVar ); //var_dump( $myZero == $myString ); //var_dump( $my == $myZeroString ); //var_dump( $myVar === $myString ); //var_dump( $myVar === $myZeroString ); //var_dump( $myVar == $myString ); //var_dump( $myZeroString == $myVar ); //var_dump( $myZeroString == $myZero ); //var_dump( $myZeroString == $myString ); //var_dump( $myZeroString === $myString ); //var_dump( $myZeroString === $myVar ); //var_dump( $myZeroString === $myZero ); //var_dump( $myString === $myZero );
Expression | <a href="function.gettype.php" class="function">gettype()</a> | <a href="function.empty.php" class="function">empty()</a> | <a href="function.is-null.php" class="function">is_null()</a> | <a href="function.isset.php" class="function">isset()</a> | <a href="language.types.boolean.php" class="type boolean">boolean</a> : if($x) |
---|---|---|---|---|---|
$x = ""; | <a href="language.types.string.php" class="type string">string</a> | TRUE |
FALSE |
TRUE |
FALSE |
$x = null; | <a href="language.types.null.php" class="type NULL">NULL</a> | TRUE |
TRUE |
FALSE |
FALSE |
var $x; | <a href="language.types.null.php" class="type NULL">NULL</a> | TRUE |
TRUE |
FALSE |
FALSE |
$x is undefined | <a href="language.types.null.php" class="type NULL">NULL</a> | TRUE |
TRUE |
FALSE |
FALSE |
$x = array(); | <a href="language.types.array.php" class="type array">array</a> | TRUE |
FALSE |
TRUE |
FALSE |
$x = false; | <a href="language.types.boolean.php" class="type boolean">boolean</a> | TRUE |
FALSE |
TRUE |
FALSE |
$x = true; | <a href="language.types.boolean.php" class="type boolean">boolean</a> | FALSE |
FALSE |
TRUE |
TRUE |
$x = 1; | <a href="language.types.integer.php" class="type integer">integer</a> | FALSE |
FALSE |
TRUE |
TRUE |
$x = 42; | <a href="language.types.integer.php" class="type integer">integer</a> | FALSE |
FALSE |
TRUE |
TRUE |
$x = 0; | <a href="language.types.integer.php" class="type integer">integer</a> | TRUE |
FALSE |
TRUE |
FALSE |
$x = -1; | <a href="language.types.integer.php" class="type integer">integer</a> | FALSE |
FALSE |
TRUE |
TRUE |
$x = "1"; | <a href="language.types.string.php" class="type string">string</a> | FALSE |
FALSE |
TRUE |
TRUE |
$x = "0"; | <a href="language.types.string.php" class="type string">string</a> | TRUE |
FALSE |
TRUE |
FALSE |
$x = "-1"; | <a href="language.types.string.php" class="type string">string</a> | FALSE |
FALSE |
TRUE |
TRUE |
$x = "php"; | <a href="language.types.string.php" class="type string">string</a> | FALSE |
FALSE |
TRUE |
TRUE |
$x = "true"; | <a href="language.types.string.php" class="type string">string</a> | FALSE |
FALSE |
TRUE |
TRUE |
$x = "false"; | <a href="language.types.string.php" class="type string">string</a> | FALSE |
FALSE |
TRUE |
TRUE |
if
if
$myVar = 'Ivan';
if($myVar == 'logged'){ echo 'You are logged'; }
echo 'Click Next';
if else
$myVar = 'Ivan';
if($myVar == 'logged'){ echo 'You are logged'; } else { echo 'Please login'; }
echo 'Click Next';
if elseif else
if($myVar == 'logged'){ $text = 'You are logged'; } elseif($myVar == 'guest') { $text = 'Please choose name'; } else $text = 'Please register
echo $text.' Click Next';
() ? :
$text = ($myVar == 'logged') ? 'You are logged' : 'Please login'; echo $text.' Click Next';