PHP/Utility Function/list — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 07:08, 26 мая 2010

Each line is converted into an array

 
<?
    $filename="data.txt";
    $filearray = file($filename);
    if ($filearray) {
            while (list($var, $val) = each($filearray)) {
                    ++$var;
                    $val = trim($val);
                    print "Line $var: $val<br />";
            }
    } else {
            print "Could not open $filename.\n";
    }
?>



Extracting Values from an Array Using list()

 
<?php
  $scores = array(88, 75, 91, 84);
  list($maths, $english, $history, $biology) = $scores;
  printf("<p>Maths: %d; English: %d; History: %d; Biology: %d.</p>\n",
          $maths, $english, $history, $biology);
?>



list($maths, $english, , $biology) = $scores;

 
<?php
  
  $scores = array(88, 75, 91, 84);
  list($maths, $english, , $biology) = $scores;
  @printf("<p>Maths: %d; English: %d; History: %d; Biology: %d.</p>\n",
          $maths, $english, $history, $biology);
?>



Using the list() function.

 
<?php 
$net_address = array("192.168.1.101", "255.255.255.0", "192.168.1.1"); 
list($ip_addr, $net_mask, $gateway) = $net_address; 
echo "ip addr = $ip_addr\n"; 
echo "net mask = $net_mask\n"; 
echo "gateway = $gateway\n"; 
?>