<?php
$mydates = array('2005-01-01', '2005-06-30', '2005-12-31');
foreach($mydates as $mydate){
$ts = strtotime($mydate);
echo 'Day ' . date('d M Y: z', $ts) . "
\n";
}
?>
<?php
// takes a 2 or 4 digit year,
// returns 1 or 0
function is_leap_year($year)
{
$ts = strtotime("$year-01-01");
return date('L', $ts);
}
//test the function for a set of 11 consecutive years
for($i=2000; $i<=2010; $i++)
{
$output = "$i is ";
if( !is_leap_year($i))
{
$output .= "not ";
}
$output .= 'a leap year.
';
echo $output;
}
?>
PHP and classes
<?php
class ShopProduct{
//class body
public $title = "default product";
public $producerMainName = "main name";
public $producerFirstName = "first name";
public $price = 0;
function __construct($title, $firstName, $mainName, $price){
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mainName;
$this->price = $price;
}
function getProducer(){
return "{$this->producerFirstName}" . " {$this->producerMainName}";
}
}
class ShopProductWriter{
public function write($shopProduct){
$str = "{$shopProduct->title}: " . $shopProduct->getProducer() . " ({$shopProduct->price})\n";
print $str;
}
}
$product1 = new ShopProduct("My Antonia", "Willa", "Cather", 7.99);
$writer = new ShopProductWriter();
$writer->write($product1);
print "Author: {$product1->getProducer()}\n";
?>
<?php
class shopProduct {
public $numPages;
public $playLength;
public $title;
public $producerMainName;
public $producerFirstName;
public $price;
function __construct($title, $firstName, $mainName, $price, $numPages=0, $playLength=0)
{
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mainName;
$this->price = $price;
$this->numPages = $numPages;
$this->playLength = $playLength;
}
function getProducer(){
return "{$this->producerFirstName}" . "{$this->producerMainName}";
}
function getSummaryLine(){
$base = "{$this->title} ({$this->producerMainName},";
$base .= "{$this->producerFirstName})";
return $base;
}
}
Class CdProduct extends shopProduct {
function getPlayLength(){
return $this->playLength;
}
function getSummaryLine(){
$base = "{$this->title} ({$this->producerMainName}, ";
$base .= "{$this->producerFirstName})";
$base .=": playing time - {$this->playLength}";
return $base;
}
}
Class BookProduct extends ShopProduct {
function getNumberOfPages() {
return $this->numPages;
}
function getSummaryLine() {
$base = "{$this->title} ({$this->productMainName}, ";
$base .= "{$this->producerFirstName})";
$base .= ": page count - {$this->numPages}";
return $base;
}
}
$product2 = new CdProduct("Exile on Coldharbour Lane", "The", "Alabama 3", 10.99, null, 60.33);
print "Artist: {$product2->getProducer()}\n";
?>
No comments:
Post a Comment