2014. szeptember 15., hétfő

A $_SERVER tömb kilistázása ciklussal

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 10.1 Looping through the $_SERVER array</title>
</head>
<body>
<div>
<?php
foreach ( $_SERVER as $key=>$value ) {
   print "\$_SERVER[\"$key\"] == $value<br/>";
}
?>
</div>
</body>
</html>

Eredmény:
$_SERVER["ALLUSERSPROFILE"] == C:\ProgramData
$_SERVER["APPDATA"] == C:\Users\G?bor\AppData\Roaming
$_SERVER["CommonProgramFiles"] == C:\Program Files (x86)\Common Files
$_SERVER["CommonProgramFiles(x86)"] == C:\Program Files (x86)\Common Files
$_SERVER["CommonProgramW6432"] == C:\Program Files\Common Files
$_SERVER["COMPUTERNAME"] == GABOR-PC
$_SERVER["ComSpec"] == C:\Windows\system32\cmd.exe
$_SERVER["FP_NO_HOST_CHECK"] == NO
$_SERVER["HOMEDRIVE"] == C:
$_SERVER["HOMEPATH"] == \Users\G?bor
$_SERVER["LOCALAPPDATA"] == C:\Users\G?bor\AppData\Local
$_SERVER["LOGONSERVER"] == \\GABOR-PC
$_SERVER["NB_EXEC_EXTEXECUTION_PROCESS_UUID"] == 2acf59eb-c72b-49b8-9f76-88308c4fcb17
$_SERVER["NUMBER_OF_PROCESSORS"] == 2
$_SERVER["OS"] == Windows_NT
$_SERVER["Path"] == C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;e:\Program Files (x86)\AOMEI Backupper Professional Edition 2.0;C:\Program Files\jEdit
$_SERVER["PATHEXT"] == .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
$_SERVER["PROCESSOR_ARCHITECTURE"] == x86
$_SERVER["PROCESSOR_ARCHITEW6432"] == AMD64
$_SERVER["PROCESSOR_IDENTIFIER"] == AMD64 Family 15 Model 107 Stepping 2, AuthenticAMD
$_SERVER["PROCESSOR_LEVEL"] == 15
$_SERVER["PROCESSOR_REVISION"] == 6b02
$_SERVER["ProgramData"] == C:\ProgramData
$_SERVER["ProgramFiles"] == C:\Program Files (x86)
$_SERVER["ProgramFiles(x86)"] == C:\Program Files (x86)
$_SERVER["ProgramW6432"] == C:\Program Files
$_SERVER["PSModulePath"] == C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
$_SERVER["PUBLIC"] == C:\Users\Public
$_SERVER["SESSIONNAME"] == Console
$_SERVER["SystemDrive"] == C:
$_SERVER["SystemRoot"] == C:\Windows
$_SERVER["TEMP"] == C:\Users\GBOR~1\AppData\Local\Temp
$_SERVER["TMP"] == C:\Users\GBOR~1\AppData\Local\Temp
$_SERVER["USERDOMAIN"] == Gabor-PC
$_SERVER["USERNAME"] == G?bor
$_SERVER["USERPROFILE"] == C:\Users\G?bor
$_SERVER["windir"] == C:\Windows
$_SERVER["windows_tracing_flags"] == 3
$_SERVER["windows_tracing_logfile"] == C:\BVTBin\Tests\installpackage\csilogfile.log
$_SERVER["PHP_SELF"] == E:\STY PHP in 24 Hours\Hour 10\listing10.01.php
$_SERVER["SCRIPT_NAME"] == E:\STY PHP in 24 Hours\Hour 10\listing10.01.php
$_SERVER["SCRIPT_FILENAME"] == E:\STY PHP in 24 Hours\Hour 10\listing10.01.php
$_SERVER["PATH_TRANSLATED"] == E:\STY PHP in 24 Hours\Hour 10\listing10.01.php
$_SERVER["DOCUMENT_ROOT"] ==
$_SERVER["REQUEST_TIME_FLOAT"] == 1410773489.2432
$_SERVER["REQUEST_TIME"] == 1410773489
Notice: Array to string conversion in E:\STY PHP in 24 Hours\Hour 10\listing10.01.php on line 12 $_SERVER["argv"] == Array
$_SERVER["argc"] == 1

9. fejezet gyakorlat megoldása: származtatott függvények

<!--listing9.gyak1
1. Hozzuk létre a Szamolo osztalyt, amely két egész számot tárol.
Írjunk hozzá egy kiszamol() nevű tagfüggvényt, amely kiírja a két számot a böngészőbe!
2. Hozzuk létre az Osszead osztályt, amely a Szamolo osztálytól örököl.
Írjuk át ennek kiszamol() tagfüggvényét úgy, hogy ennek a két számnak az összegét írja ki.
3. Hozzuk létre az Osszead alapján a Kivon osztályt!
-->
<?php
class Szamolo {

    function Szamolo( $egyik=2, $masik=3 ) {
        $this->egyik = $egyik;
        $this->masik = $masik;
    }
   
    function kiszamol() {
       print $this->egyik.", ".$this->masik;
    }
}

class Osszead extends Szamolo {
    function kiszamol() {
       print $this->egyik+$this->masik;
    }
}

class Kivon extends Szamolo {
    function kiszamol() {
       print $this->egyik-$this->masik;
    }
}

//ellenőrzés
$teszt = new Szamolo( 5,6 );
$teszt->kiszamol();
print "<BR \>";

$teszt1 = new Osszead( 5,6 );
$teszt1->kiszamol();
print "<BR \>";

$teszt2 = new Kivon( 5,6 );
$teszt2->kiszamol();
print "<BR \>";

?>

Eredmény:
5, 6
11
-1

Konstruktorok használata

<?php
class Item {
    private $name;

    function __construct( $name="item", $code=0 ) {
        $this->name = $name;
        $this->code = $code;
    }
   
    function getName() {
        return $this->name;
    }
}

class PriceItem extends Item {
    private $price;

    function __construct( $name, $code, $price ) {
        parent::__construct( $name, $code );
        $this->price = $price;
    }

    function getName() {
        return "(price) ".parent::getName();
    }
}

$item = new PriceItem( "widget", 5442, 5.20 );
print $item->getName();
// outputs "(price) widget"

?>

Eredmény:
(price) widget

Szülő osztály meghívása: parent

<?php
class Item {
    private $name;

    function __construct( $name="item", $code=0 ) {
        $this->name = $name;
        $this->code = $code;
    }
   
    function getName() {
        return $this->name;
    }
}

class PriceItem extends Item {
    private $price;

    function __construct( $name, $code, $price ) {
        parent::__construct( $name, $code );
        $this->price = $price;
    }

    function getName() {
        return "(price) ".parent::getName();
    }
}

$item = new PriceItem( "widget", 5442, 5.20 );
print $item->getName();
// outputs "(price) widget"

?>

Gyermekosztály által felülírt tagfüggvény

<?php
class Item {
    var $name;

    function Item( $name="item", $code=0 ) {
        $this->name = $name;
        $this->code = $code;
    }
   
    function getName() {
        return $this->name;
    }
}

class PriceItem extends Item {
    function getName() {
        return "(price) ".$this->name;
    }
}

$item = new PriceItem( "widget", 5442 );
print $item->getName();
// outputs "(price) widget"

?>

Eredmény:
(price) widget

Osztály származtatása

A származtatott osztály örökli az ős tulajdonságait és metódusait.

<?php
class Item {
    var $name;

    function Item( $name="item", $code=0 ) {
        $this->name = $name;
        $this->code = $code;
    }
   
    function getName() {
        return $this->name;
    }
}

class PriceItem extends Item {

}

$item = new PriceItem( "widget", 5442 );
print $item->getName();
// outputs "widget"

?>
Eredmény:
widget

Osztály nyilvános tulajdonságokkal

<?php
class Item {
    var $name;
    var $code;
    var $productString;

    function Item( $name="item", $code=0 ) {
        $this->code = $code;
        $this->setName( $name );
    }

    function getProductString() {
        return $this->productString;
    }
   
    function setName( $n ) {
        $this->name = $n;
        $this->productString = $this->name." ".$this->code;
    }

    function getName() {
        return $this->name;
    }
}

$item = new Item("widget", 5442);
print $item->getProductString();
// outputs "widget 5442"

print "<br />";

$item->name = "widget-upgrade";
print $item->getProductString();
// outputs "widget 5442", not "widget-upgrade 5442"
// Mivel nem a setName függvénnyel állítottuk be a nevét, nem annak megfelelő lesz.
?>
Eredmény:
widget 5442
widget 5442

Konstruktorral rendelkező osztály

A konstruktor függvény neve ugyanaz, mint az osztály neve.

<?php
class Item {
    var $name;

    function Item( $name="item" ) {
        $this->name = $name;
    }

    function setName( $n ) {
        $this->name = $n;
    }

    function getName() {
        return $this->name;
    }
}

$item = new Item("widget 5442");
print $item->getName();
// outputs "widget 5442"
?>
Eredmény:
widget 5442

Tulajdonság értékének módosítása függvénnyel.

A változónkat védetté thetjük, ha csak függvénnyel változtathatjuk az értékét és privátként definiáljuk. (Itt nem privát, de a módszert szemlélteti.)

<?php
class Item {
     var $name = "item";

     function setName( $n ) {
         $this->name = $n;
     }

     function getName() {
         return $this->name;
     }
}

$item = new Item();
$item->setName("widget 5442");
print $item->getName();
// outputs "widget 5442"
?>
Eredmény:
widget 5442

Objektumpéldány változójának és függvényének használata: $this

<?php
class Item {
     var $name = "item";

     function getName() {
         return $this->name;
     }
}

$item = new Item();
$item->name = "widget 5442";
print $item->getName();
// outputs "widget 5442"
?>
Eredmény:
widget 5442

Osztály változóval és tagfüggvénnyel

<?php
//Osztály változóval és tagfüggvénnyel
class Item {
     var $name = "item";

     function getName() {
         return "item";
     }
}

$item = new Item();
print $item->getName();
// outputs "item"
?>
Eredmény:
item

8. fejezet 2. gyakorlat: Számok oszlopba rendezése és igazítása printf() függvénnyel

PHP 8. fejezet 2. gyakorlat megoldása:

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Lebegopontos szamok 2 tizedesre 20 szeles mezoben jobbra igazitva</title>
</head>
<body>
<?php
$lebegopontosok = array(  
    222.4,
    4,
    80.6,
    6548.9775
            );
print "<pre>";
foreach ( $lebegopontosok as $szam ) {
    printf( "%20.2f\n", $szam );
}
print "</pre>";
?>
</body>
</html>
Eredmény:
              222.40
                4.00
               80.60
             6548.98

Egy szöveg darabolása tokenekre: strtok()

Néha szükség van 1-1 hosszabb szöveg felosztására értelmes szakaszokká. Ilyenkor az eredeti szövegbe elválasztó karaktereket tesznek, amik alapján feldarabolhatjuk a kapott szöeget.

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 8.3 Dividing a string into
        tokens with strtok()</title>
</head>
<body>
<div>
<?php
$test  = "http://p24.corrosive.co.uk/tk.php";
$test .= "?id=353&sec=44&user=harry&context=php";
//sajat sor:
print "$test<br/><br/>";
//
$delims = "?&";
$word = strtok( $test, $delims );
while ( is_string( $word ) ) {
   if ( $word ) {
       print "$word<br/>";
    }
    $word = strtok( $delims );
}
?>
</div>
</body>
</html>
Eredmény:
http://p24.corrosive.co.uk/tk.php?id=353&sec=44&user=harry&context=php

http://p24.corrosive.co.uk/tk.php
id=353
sec=44
user=harry
context=php

2014. szeptember 12., péntek

Formázott kiírás: printf

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 8.2 Using printf() to Format a List of Product Prices</title>
</head>
<body>
<?php
$products = array(   
            "Green armchair"=>222.4,
            "Candlestick"=>4,
            "Coffee table"=>80.6
            );
print "<pre>";
printf("%-20s%23s\n", "Name", "Price");
printf("%'-43s\n", "");
foreach ( $products as $key=>$val ) {
    printf( "%-20s%20.2f\n", $key, $val );
}
print "</pre>";
?>
</body>
</html>

Eredmény:

Name                                  Price
-------------------------------------------
Green armchair                    222.40
Candlestick                         4.00
Coffee table                       80.60

Típusok megadása

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 8.1 Demonstrating Some Type Specifiers</title>
</head>
<body>
<div>
<?php
$number = 543;
printf("Decimal: %d<br/>", $number );
printf("Binary: %b<br/>", $number );
printf("Double: %f<br/>", $number );
printf("Octal: %o<br/>", $number );
printf("String: %s<br/>", $number );
printf("Hex (lower): %x<br/>", $number );
printf("Hex (upper): %X<br/>", $number );
?>
</div>
</body>
</html>

Eredmény:

Decimal: 543
Binary: 1000011111
Double: 543.000000
Octal: 1037
String: 543
Hex (lower): 21f
Hex (upper): 21F

7. fejezet 2. gyakorlat megoldása

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//HU"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-->
<title>Listing 7.Gyak Filmek listázása tömbből filmtípusonként</title>
</!-->
<title>Listing 7.Gyak Filmek listazasa tombbol filmtipusonkent</title>
</head>
<body>
<div>
<?php
$karakterek = array (
            array (
                "Sci-Fi"=> "2001 Urodisszeia",
            ),
            array (
                "Sci-Fi"=> "A nyolcadik utas a halal",
            ),
            array (
                "Sci-Fi"=> "Csillagok haboruja",
            ),
            array (
                "Akcio" => "Az utolso cserkesz",
            ),
            array (
                "Romantikus" => "50 elso randi",
            )
        );

foreach ( $karakterek as $film ) {
    print "<p>";
    foreach ( $film as $kulcs=>$ertek ) {
        print "$kulcs: $ertek<br />";
        //print "$kulcs => $ertek<br />";
    }
    print "</p>";
}

?>
</div>
</body>
</html>

Eredmény:

Sci-Fi: 2001 Urodisszeia
Sci-Fi: A nyolcadik utas a halal
Sci-Fi: Csillagok haboruja
Akcio: Az utolso cserkesz
Romantikus: 50 elso randi

7 fejezet 1. gyakorlat megoldása

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//HU"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-->
<title>Listing 7.Gyak Filmek listázása tömbből filmtípusonként</title>
</!-->
<title>Listing 7.Gyak Filmek listazasa tombbol filmtipusonkent</title>
</head>
<body>
<div>
<?php
$karakterek = array (
            array (
                "filmcim"=> "2001 Urodisszeia",
                "filmtipus" => "Sci-Fi",
            ),
            array (
                "filmcim"=> "A nyolcadik utas a halal",
                "filmtipus" => "Sci-Fi",
            ),
            array (
                "filmcim"=> "Csillagok haboruja",
                "filmtipus" => "Sci-Fi",
            ),
            array (
                "filmcim" => "Az utolso cserkesz",
                "filmtipus" => "Akcio",
            ),
            array (
                "filmcim" => "50 elso randi",
                "filmtipus" => "Romantikus",
            )
        );

foreach ( $karakterek as $film ) {
    print "<p>";
    foreach ( $film as $kulcs=>$ertek ) {
        print "$kulcs: $ertek<br />";
    }
    print "</p>";
}

?>
</div>
</body>
</html>

Eredmény:

filmcim: 2001 Urodisszeia
filmtipus: Sci-Fi
filmcim: A nyolcadik utas a halal
filmtipus: Sci-Fi
filmcim: Csillagok haboruja
filmtipus: Sci-Fi
filmcim: Az utolso cserkesz
filmtipus: Akcio
filmcim: 50 elso randi
filmtipus: Romantikus

Tömb vagy más összetett típus értékeinek kiírása: print_r

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 7.4 Testing the print_r() Function</title>
</head>
<body>
<div>
<?php
$characters = array (
            array (
                "name"=> "bob",
                "occupation" => "superhero",
            ),
            array (
                "name" => "sally",
                "occupation" => "superhero",
            )
        );

print_r( $characters );

/*
prints:
Array
(
    [0] => Array
        (
            [name] => bob
            [occupation] => superhero
        )

    [1] => Array
        (
            [name] => sally
            [occupation] => superhero
        )

)
*/

?>
</div>
</body>
</html>

Eredmény:

Array ( [0] => Array ( [name] => bob [occupation] => superhero ) [1] => Array ( [name] => sally [occupation] => superhero ) )

A foreach többdimenziós tömbökre is működik

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 7.3 Looping Through a Multidimensional Array</title>
</head>
<body>
<div>
<?php
$characters = array (
            array (
                "name"=> "bob",
                "occupation" => "superhero",
                "age" => 30,
                "specialty" =>"x-ray vision"
            ),
            array (
                "name" => "sally",
                "occupation" => "superhero",
                "age" => 24,
                "specialty" => "superhuman strength"
            ),
            array (
                "name" => "mary",
                "occupation" => "arch villain",
                "age" => 63,
                "specialty" =>"nanotechnology"
            )
        );

foreach ( $characters as $val ) {
    print "<p>";
    foreach ( $val as $key=>$final_val ) {
        print "$key: $final_val<br />";
    }
    print "</p>";
}

?>
</div>
</body>
</html>

Eredmény:

name: bob
occupation: superhero
age: 30
specialty: x-ray vision
name: sally
occupation: superhero
age: 24
specialty: superhuman strength
name: mary
occupation: arch villain
age: 63
specialty: nanotechnology

Foreach: a tömblistázó szuperfegyver


A foreeach anélkül megy végig a tömb elemein, hogy tudnunk kéne az elemek számát vagy nevét!

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 7.2 Looping Through an Associative Array with foreach</title>
</head>
<body>
<div>
<?php
$character = array (
            "name" => "bob",
            "occupation" => "superhero",
            "age" => 30,
            "special power" => "x-ray vision"
            );
foreach ( $character as $key=>$val ) {
    print "$key = $val<br />";
}

?>
</div>
</body>
</html>
Eredmény:
name = bob
occupation = superhero
age = 30
special power = x-ray vision