- . . : , , , .
PHP :
class _{
var $_;
/* */
function _(){
/* */
}
/* */
}
var, , , . this .
, , . , , . ? , , . :
<?
class Articles { //
var $title;
var $author;
var $description;
// ,
//
function make_article($t, $a, $d){
$this->title = $t;
$this->author = $a;
$this->description = $d;
}
//
function show_article(){
$art = $this->title. "<br>".
$this->description.
"<br>: ". $this->author;
echo $art;
}
}
?>
, Articles, , , .
, PHP, HTML. () . php- . :
<?php
class Articles { //
var $title;
?>
<?php
//
function show_article(){
//
}
} //
?>
.
. PHP, , . , _. . , stdClass, PHP.
. , () make_article(). , , . . PHP4 var . var . , , . - , , .
|
|
. , : , $_POST, . PHP4:
<?class Articles { // var $title= $_POST["title"]; var $author = ""; var $description = $_POST["description"]; var $published = date("Y-m-d");// , // }?>PHP4 , :
<?class Articles { // var $title; var $author = ""; var $description; var $published;// , // function Articles(){ $this->title = $_POST["title"]; $this->description = $_POST["description"]; $this ->published = date("Y-m-d"); }}?>, PHP3 PHP4 -. PHP3 , , , PHP4 , , . , . . PHP5 __construct. , PHP5 , . PHP5 - __destruct.
PHP , . , . . new. , , . : $_->_ $_->_( ). , $ .
<?php$art = new Articles; // $artecho ($art ->title); // $art$another_art = new Articles; // $another_art$another_art->show_article(); // // ?>
. , $art $another_art title, description,author Articles(), show_article(). .
|
|
, $art->title $another_art->title . PHP , $art->$title. title $art, , $title (, $art->"").
<?php$art->title = " Internet"; // // $art->$title = " Internet"; // // ?>
Extends
- . . , . , -, , . : - , , - . , , .. , , .. - , , , . , , , .. . , , , . . . , (, ), (, ). ? . . ( ) , , , ( ). , . , . PHP4 , PHP . PHP extends.
<?phpclass Person { // var $first_name; // var $last_name; // function make_person($t,$a){ // // $this->first_name = $t; $this->last_name = $a; } function show_person(){ // echo ("<h2>". $this->first_name. " ". $this->last_name. "</h2>"); }}class Programmer extends Person{ // // Programmer, Person var $langs = array ("Lisp",Ada); // // var function add_lang($new_lang){ // // $this->langs[] = $new_lang; }}?>Programmer , Person, $langs, , set_lang . new. , , , Person, .. :
|
|
, Person Programmer, . Person , , Programmer, , , . .
. Programmer, Person, Person. , ().
, PHP, PHP4 PHP3 . , PHP3 , . PHP4 , , .
<?php
class Programmer extends Person{
//
// Programmer, Person
var $langs = array ("Lisp");
function Programmer(){
//
// PHP3, PHP4
$this->make_person("","");
}
}
?>
Programmer() , .. Programmer, . , , . , , . . , .
<?php
class Programmer extends Person{
//
// Programmer, Person
var $langs = array ("Lisp");
function Programmer($n = "",
$f = ""){
//
$this->make_person($n,$f);
}
}
$default_progr = new Programmer();
//
$new_progr = new Programmer("",
"");
//
$new_progr->show_person();
?>
PHP3, PHP4, Person. , : Person:
<?php
class Person { //
var $first_name;
var $last_name;
function Person($t,$a){ //
$this->first_name = $t;
$this->last_name = $a;
}
/*... */
}
class Programmer extends Person{
//
// Programmer, Person
var $langs = array ("Lisp");
function set_lang($new_lang){
$this->langs[] = $new_lang;
}
}
$new_progr = new Programmer("",
"");
?>
Programmer, - ? PHP3 , Programmer() ( , ). PHP4 , , .. Person() Person ( , , ).
|
|
, , .
<?php
class Person { //
var $first_name;
var $last_name;
function Person($t,$a){ //
$this->first_name = $t;
$this->last_name = $a;
}
function Programmer($new_lang){
echo " ";
}
}
class Programmer extends Person{
//
// Programmer, Person
var $langs = array ("Lisp");
function set_lang($new_lang){
$this->langs[] = $new_lang;
}
}
$new_progr = new Programmer("",
"");
?>
PHP3 Programmer() Person. , , . , . PHP4 Programmer , .
PHP 3, PHP 4 .
::
. , , . ? PHP4 ::
, Programmer show_name() Person say_hello(), Programmer, :
<?phpclass Person { // var $first_name; var $last_name; function Person($t,$a){ // $this->first_name = $t; $this->last_name = $a; }function show_name(){ // echo (" , ". $this->first_name. " ". $this->last_name. "!<br>"); }}class Programmer extends Person{ // // Programmer, Person function set_lang($new_lang){ // // $this->langs[] = $new_lang; Person::show_name(); // echo " ". $new_lang; } function show_name(){ echo (" , ". $this->first_name. " ". $this->last_name. "!<br>"); } }$new_progr = new Programmer("","");$new_progr->set_lang("PHP");?>:
! PHPProgrammer show_name(), show_name() Person :: , , $this, , .
parent
, , ( Person::show_name()). , , , . , parent (, parent::show_name()). Parent , extends . , , extends .
PHP5
PHP5 . , . , , PHP4. PHP5 , ( ). . PHP4 , .. . PHP5 ( ), ( ) ( ). , . PHP5 - .
|
|
, . , :
array
1. $array_name = array("key1"=>"value1", "key2"=>"value2");2.
$array_name["key1"] = value1;, , . , ( ), . :
<?$del_items = array("10"=>" ", "12"=>"");$del_items["13"] = " Php"; // ?>
, . ? .
+. , . , $a $b, () $c, $a, $b. , , , .. $a. , PHP, .
<?$a = array(""=>"", ""=>"");$b = array(""=>"",""=>"", ""=>"");$c = $a + $b;$d = $b + $a;print_r($c); /* : Array([]=> []=> []=>) */print_r($d); /* : Array([]=> []=> []=>) */?>, . / . , . PHP ==, ===.
<?$a = array(""=>"", ""=>"");$b = array(""=>"", ""=>"");if ($a == $b) echo " "; else echo " ";if ($a === $b) echo " "; else echo " ";// echo " "?>. PHP .
count
count(), . . , 1. NULL count(NULL) 0. , , , COUNT_RECURSIVE.
<?$del_items = array("langs" => array("10"=>"Python", "12"=>"Lisp"), "other"=>"");echo count($del_items). "<br>"; // 2echo count($del_items,COUNT_RECURSIVE); // 4?>, . . .
in_array
in_array(" ","", [" "]);, . true, , , . , .
, . , PHP. :
<?php$langs = array("Lisp","Python","Java", "PHP","Perl");if (in_array("PHP",$langs)) echo " PHP<br>";// " PHP"if (in_array("php",$langs)) echo " php<br>";// , // "PHP", "php"?>. , PHP 4.2.0.
:
<?php$langs = array("Lisp","Python",array("PHP","Java"),"Perl");if (in_array(array("PHP","Java"),$langs)) echo " PHP Java<br>";?>array_search
. in_array array_search , , . :
array_search(" ","", [" "]);, , . PHP 4.2.0, , NULL.
. , , , . , , .
<?php$langs = array("","Lisp","Python","Java", "PHP","Perl");if (!array_search("PHP",$langs)) echo " PHP<br>";else { $k = array_search("PHP",$langs); echo "PHP $k-";}?>:
PHP 4-, , in_array, , , , . , ? array_search() . , array_keys().
array_keys
array_keys() . , . :
array_keys ("", [" "])array_keys() , , .
, . , . , Lisp. :
<?php$langs =array("Lisp","Python","Java","PHP", "Perl","Lisp");$lisp_keys = array_keys($langs,"Lisp");echo "Lisp ". count($lisp_keys)." :<br>";foreach ($lisp_keys as $val){ echo " $val <br>";}?>:
Lisp 2 : 0 5array_keys(), , , .. LISP . array_keys() PHP4. PHP3 .
, , . , . array_values(). , , .. , . .
, , Lisp . (, ), - . array_unique().
array_unique
array_unique() . , . ? , ? . . , .
.
<?php$langs =array("Lisp","Java","Python","Java", "PHP","Perl","Lisp");print_r(array_unique($langs));?>:
Array ([0] => Lisp [1] => Java [2] => Python [3] => PHP [4] => Perl).
, , , . , , , PHP .
sort
sort
sort ( [, ]), .. . , , . true, false.
: , , . :
<? $items = array(10 => "", 20 => "", 30 => "");sort($items); // // , print_r($items); $rev_items = array("" => 10, "" => 30, "" => 20);sort($rev_items); // , // print_r($rev_items);?>:
Array ([0] => [1] => [2] => )Array ([0] => 10 [1] => 20 [2] => 30):
- SORT_REGULAR ;
- SORT_NUMERIC ;
- SORT_STRING .
asort, rsort, arsort
, asort ( [, ]). , .. , rsort ( [, ]). , arsort( [, ]). , , , sort. , sort: SORT_REGULAR, SORT_NUMERIC, SORT_STRING. , SORT_NUMERIC PHP4.
<?php$books = array(""=>" ", ""=>" ", ""=>" ");asort($books); // , // print_r($books);echo "<br>";rsort($books); // , // print_r($books);?>:
Array ([] => [] => [] => )Array ([0] => [1] => [2] => ), . , , . , . . . :
<form action=task.php><table border=1><tr><td> </td><td><input type=text name=title size=5> </td></tr><tr><td> </td><td><input type=text name=description size=5> </td></tr><tr><td> </td><td><input type=text name=author size=5> </td></tr><tr><td> </td><td><input type=text name=published size=5></td></tr></table><input type=submit value=""></form>, , , . arsort(). , . , , , . , :
<?phpprint_r($_GET); echo "<br>";arsort ($_GET); // , // print_r($_GET); echo "<br>";$ordered_names = array_keys($_GET); // foreach($ordered_names as $key => $val)echo "$key:$val <br>"; // ?>