Задачки на знание PHP для начинающих

Предскажите результаты работы фрагментов кода

1. Переменные в index.php

<?
 $GLOBALS['x']=2;
 $x=3;

     
 echo $GLOBALS['x']; 
  1. <?
  2.  $GLOBALS['x']=2;
  3.  $x=3;
  4.  
  5.  echo $GLOBALS['x']; 

2. Тип ключа массива

<?
 $A1=array('1' => '2');

     
 list($key,$val)=each($A1);
 echo 'key: '.$key."<br/>\n";
 echo 'val: '.$val."<br/>\n";
 echo gettype($key)."<br/>\n";
 echo gettype($val);
  1. <?
  2.  $A1=array('1' => '2');
  3.  
  4.  list($key,$val)=each($A1);
  5.  echo 'key: '.$key."<br/>\n";
  6.  echo 'val: '.$val."<br/>\n";
  7.  echo gettype($key)."<br/>\n";
  8.  echo gettype($val);

3. Порядок сравнения

<?
 echo intval('string'==0)."<br/>\n";
 echo intval('string'==1)."<br/>\n";
 echo intval('string'==true)."<br/>\n";
 echo intval('string'==false)."<br/>\n";
  1. <?
  2.  echo intval('string'==0)."<br/>\n";
  3.  echo intval('string'==1)."<br/>\n";
  4.  echo intval('string'==true)."<br/>\n";
  5.  echo intval('string'==false)."<br/>\n";

4. Ловушки

<?
 $Data=array('Data' => null,'x' => 'test');

    
 foreach ($Data as $key => $value)
 {
  $$key=$value;
 }

    
 echo '<pre>';
 print_r($Data);
 echo '</pre>';

    
 echo '<pre>';
 print_r($x);
 echo '</pre>';
  1. <?
  2.  $Data=array('Data' => null,'x' => 'test');
  3.  
  4.  foreach ($Data as $key => $value)
  5.  {
  6.   $$key=$value;
  7.  }
  8.  
  9.  echo '<pre>';
  10.  print_r($Data);
  11.  echo '</pre>';
  12.  
  13.  echo '<pre>';
  14.  print_r($x);
  15.  echo '</pre>';

5. method_exists() vs is_callable()

<?
 class Test
 {
  function x()         {echo __FUNCTION__;}
  private function y() {echo __FUNCTION__;}

    
  function __call ($name,array $Args)
  {
   // ...
  }
 }

    
 $Test=new Test;

    
 echo intval(method_exists($Test,'x'))."<br/>\n";
 echo intval(is_callable(array($Test,'x')))."<br/>\n";
 echo "<br/>\n";

    
 echo intval(method_exists($Test,'y'))."<br/>\n";
 echo intval(is_callable(array($Test,'y')))."<br/>\n";
 echo "<br/>\n";

    
 echo intval(method_exists($Test,'z'))."<br/>\n";
 echo intval(is_callable(array($Test,'z')))."<br/>\n";
 echo "<br/>\n";
  1. <?
  2.  class Test
  3.  {
  4.   function x()         {echo __FUNCTION__;}
  5.   private function y() {echo __FUNCTION__;}
  6.  
  7.   function __call ($name,array $Args)
  8.   {
  9.    // ...
  10.   }
  11.  }
  12.  
  13.  $Test=new Test;
  14.  
  15.  echo intval(method_exists($Test,'x'))."<br/>\n";
  16.  echo intval(is_callable(array($Test,'x')))."<br/>\n";
  17.  echo "<br/>\n";
  18.  
  19.  echo intval(method_exists($Test,'y'))."<br/>\n";
  20.  echo intval(is_callable(array($Test,'y')))."<br/>\n";
  21.  echo "<br/>\n";
  22.  
  23.  echo intval(method_exists($Test,'z'))."<br/>\n";
  24.  echo intval(is_callable(array($Test,'z')))."<br/>\n";
  25.  echo "<br/>\n";

6. Исключение из конструктора

<?
 class Test
 {
  function __construct()
  {
   throw new Exception;
  }
 }

    
 try                 
 {
  $T=new Test;
 }
 catch (Exception $E)
 {
  // ...
 }

    
 echo '<pre>';
 print_r($T);
 echo '</pre>';
  1. <?
  2.  class Test
  3.  {
  4.   function __construct()
  5.   {
  6.    throw new Exception;
  7.   }
  8.  }
  9.  
  10.  try                 
  11.  {
  12.   $T=new Test;
  13.  }
  14.  catch (Exception $E)
  15.  {
  16.   // ...
  17.  }
  18.  
  19.  echo '<pre>';
  20.  print_r($T);
  21.  echo '</pre>';

7. Наложение типа array на объект ^_^

<?
 class Test
 {
  public  $x;
  private $y;
 }

   
 $T1=new Test;

   
 $T2=(array)$T1;
 echo '<pre>';
 print_r($T2);
 echo '</pre>';
  1. <?
  2.  class Test
  3.  {
  4.   public  $x;
  5.   private $y;
  6.  }
  7.  
  8.  $T1=new Test;
  9.  
  10.  $T2=(array)$T1;
  11.  echo '<pre>';
  12.  print_r($T2);
  13.  echo '</pre>';

Продолжение следует...

Автор: Клешнин Иван