Difference between revisions of "PHP 2"

From Ilianko
Line 108: Line 108:
 
  $i = 0;
 
  $i = 0;
 
  for (;$i < 10;){
 
  for (;$i < 10;){
   $even[] = $
+
   $even[] = $i
 
   $i = $i+2;
 
   $i = $i+2;
 
  }
 
  }
 
   
 
   
  echo '&lt;pre'>;  
+
  echo '&lt;pre>';  
 
  print_r($even);
 
  print_r($even);
  echo '&lt;/pre'>;
+
  echo '&lt;/pre>';
  
 
  $i = 1; $max = 10;
 
  $i = 1; $max = 10;
 
  for (;;){
 
  for (;;){
   $odd[] = $i
+
   $odd[] = $i;
 
   if( $i > $max){
 
   if( $i > $max){
 
     break;
 
     break;
Line 125: Line 125:
 
  }
 
  }
 
   
 
   
  echo '&lt;pre'>;  
+
  echo '&lt;pre>';  
 
  print_r($odd);
 
  print_r($odd);
  echo '&lt;/pre'>;
+
  echo '&lt;/pre>';
  
 
=== foreach ===
 
=== foreach ===

Revision as of 10:41, 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;

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

for ($i = 0; $i < 10; $i++
{

while

switch