Difference between revisions of "PHP 2"
From Ilianko
(→while) |
|||
(20 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:PHP]] Условни оператори | + | [[Category:PHP]] |
+ | Условни оператори | ||
$myVar = 1; | $myVar = 1; | ||
Line 5: | Line 6: | ||
$myString = 'ilian'; | $myString = 'ilian'; | ||
$myZeroString = ''; | $myZeroString = ''; | ||
+ | |||
+ | var_dump($myVar); | ||
+ | |||
+ | http://php.net/manual/en/types.comparisons.php | ||
//var_dump( $myZero = $myVar ); | //var_dump( $myZero = $myVar ); | ||
Line 25: | Line 30: | ||
//var_dump( $myZeroString === $myZero ); | //var_dump( $myZeroString === $myZero ); | ||
//var_dump( $myString === $myZero ); | //var_dump( $myString === $myZero ); | ||
+ | |||
+ | Да се внимава със загубата на информация | ||
+ | |||
+ | $myVar = 1.1; | ||
+ | $myVar = (int) $myVar; | ||
+ | $myVar = (string) $myVar; | ||
+ | $myVar = (bool) $myVar; | ||
== if == | == if == | ||
Line 35: | Line 47: | ||
echo 'You are logged'; | echo 'You are logged'; | ||
} | } | ||
− | + | ||
echo 'Click Next'; | echo 'Click Next'; | ||
Line 41: | Line 53: | ||
$myVar = 'Ivan'; | $myVar = 'Ivan'; | ||
− | + | ||
if($myVar == 'logged'){ | if($myVar == 'logged'){ | ||
echo 'You are logged'; | echo 'You are logged'; | ||
Line 48: | Line 60: | ||
echo 'Please login'; | echo 'Please login'; | ||
} | } | ||
− | + | ||
echo 'Click Next'; | echo 'Click Next'; | ||
Line 61: | Line 73: | ||
else | else | ||
$text = 'Please register | $text = 'Please register | ||
− | + | ||
echo $text.' Click Next'; | echo $text.' Click Next'; | ||
Line 68: | Line 80: | ||
$text = ($myVar == 'logged') ? 'You are logged' : 'Please login'; | $text = ($myVar == 'logged') ? 'You are logged' : 'Please login'; | ||
echo $text.' Click Next'; | echo $text.' Click Next'; | ||
− | |||
== for == | == for == | ||
+ | |||
+ | for ($i = 0; $i < 10; $i++){} | ||
=== for === | === for === | ||
+ | |||
+ | for ($i = 0; $i < 10; $i++){ | ||
+ | echo $i.'<br />'; | ||
+ | } | ||
+ | |||
+ | echo $i.'END'; | ||
+ | |||
+ | for ($i = 0; $i < 10; ++$i){ | ||
+ | echo $i.'<br />'; | ||
+ | } | ||
+ | |||
+ | echo $i.'END'; | ||
+ | |||
+ | $i = 0; | ||
+ | for (; $i < 10; $i++){ | ||
+ | echo $i.'<br />'; | ||
+ | } | ||
+ | |||
+ | echo $i.'END'; | ||
+ | |||
+ | $i = 0; | ||
+ | for (;$i < 10;){ | ||
+ | $even[] = $i; | ||
+ | $i = $i+2; | ||
+ | } | ||
+ | |||
+ | echo '<pre>'; | ||
+ | print_r($even); | ||
+ | echo '</pre>'; | ||
+ | |||
+ | $i = 1; $max = 10; | ||
+ | for (;;){ | ||
+ | $odd[] = $i; | ||
+ | if( $i > $max){ | ||
+ | break; | ||
+ | } | ||
+ | $i = $i+2; | ||
+ | } | ||
+ | |||
+ | echo '<pre>'; | ||
+ | print_r($odd); | ||
+ | echo '</pre>'; | ||
=== foreach === | === foreach === | ||
+ | |||
+ | $even = array( 0, 2, 4, 6, 8); | ||
+ | |||
+ | foreach($myVar as $value) | ||
+ | { | ||
+ | echo $value.'<br />'; | ||
+ | } | ||
+ | |||
+ | $even = array( 0, 2, 4, 6, 8); | ||
+ | |||
+ | foreach($myVar as $value) | ||
+ | { | ||
+ | if( $value == 4 ){ | ||
+ | continue; | ||
+ | } | ||
+ | echo $value.'<br />'; | ||
+ | } | ||
+ | $myNames['first'] = 'Ivan'; | ||
+ | $myNames['second'] = 'Ivanov'; | ||
+ | $myNames['last'] = 'Todorov'; | ||
+ | |||
+ | foreach($myNames as $key => $value){ | ||
+ | echo $key.': '.$value.'<br />'; | ||
+ | } | ||
== while == | == while == | ||
+ | |||
+ | Контролираме условието | ||
+ | |||
+ | $i = 0; | ||
+ | while( $i < 10 ) | ||
+ | { | ||
+ | echo $i.'<br />'; | ||
+ | $i++; | ||
+ | } | ||
== switch == | == switch == |
Latest revision as of 11:03, 20 October 2013
Условни оператори
$myVar = 1; $myZero = 0; $myString = 'ilian'; $myZeroString = ;
var_dump($myVar);
http://php.net/manual/en/types.comparisons.php
//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 );
Да се внимава със загубата на информация
$myVar = 1.1; $myVar = (int) $myVar; $myVar = (string) $myVar; $myVar = (bool) $myVar;
Contents
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';
for
for ($i = 0; $i < 10; $i++){}
for
for ($i = 0; $i < 10; $i++){ echo $i.'<br />'; } echo $i.'END';
for ($i = 0; $i < 10; ++$i){ echo $i.'<br />'; } echo $i.'END';
$i = 0; for (; $i < 10; $i++){ echo $i.'<br />'; } echo $i.'END';
$i = 0; for (;$i < 10;){ $even[] = $i; $i = $i+2; } echo '<pre>'; print_r($even); echo '</pre>';
$i = 1; $max = 10; for (;;){ $odd[] = $i; if( $i > $max){ break; } $i = $i+2; } echo '<pre>'; print_r($odd); echo '</pre>';
foreach
$even = array( 0, 2, 4, 6, 8); foreach($myVar as $value) { echo $value.'<br />'; }
$even = array( 0, 2, 4, 6, 8); foreach($myVar as $value) { if( $value == 4 ){ continue; } echo $value.'<br />'; }
$myNames['first'] = 'Ivan'; $myNames['second'] = 'Ivanov'; $myNames['last'] = 'Todorov'; foreach($myNames as $key => $value){ echo $key.': '.$value.'
'; }
while
Контролираме условието $i = 0; while( $i < 10 ) { echo $i.'
'; $i++; }