php: back to basics
DESCRIPTION
slides from a workshop - 2012 history, syntax, overflow, operators, oop and functions, php5.4, symfony, automated testsTRANSCRIPT
![Page 1: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/1.jpg)
php: back to basicsa fast overview
Fabio Mora @morafabio http://fabio.mora.name
1
![Page 2: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/2.jpg)
La storia
• Rasmus Lerdorf (1968, danimarca) ha bisogno di tracciare le visite sul suo sito internet
• Per farlo decide di scrivere un modulo CGI (Common Gateway Interface)
• Lo chiama Personal HomePage/Form Interpreter
• La sintassi era simile al Perl
2
1994
![Page 3: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/3.jpg)
La storia
3
PHP/FI
<!--getenv HTTP_USER_AGENT-->
<!--ifsubstr $exec_result Mozilla-->Hey, you are using Netscape!<p>
<!--endif-->
<!--sql database select * from table where user='$username'-->
<!--ifless $numentries 1-->Sorry, that record does not exist<p>
<!--endif exit-->
Welcome <!--$user-->!<p>You have <!--$index:0--> credits left in your account.<p>
<!--include /text/footer.html-->
![Page 4: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/4.jpg)
Rasmus LerdorfphpDay 2012, Verona (IT)
![Page 5: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/5.jpg)
La storia
• Rasmus riscrive PHP/FI con una sintassi il più vicina possibile al C
• Integra supporto ai database(Postres95, DBM, mSQL)
• Accesso ai cookie
• Due studenti australiani, Andi Gutmans e Zeev Suraki, contattano Rasmus per offrirgli aiuto
• Nasce il primo devteam di PHP
5
1995
![Page 6: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/6.jpg)
La storia
6
PHP/FI 2<?
$name = "bob"; $db = "db"; $result = msql($db,"select * from table where firstname='$name'"); $num = msql_numrows($result); echo "$num records found!<p>";
$i=0; while($i<$num); echo msql_result($result,$i,"fullname"); echo "<br>"; echo msql_result($result,$i,"address"); echo "<br>"; $i++; endwhile;
?>
![Page 7: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/7.jpg)
Zeev SuraskiphpDay 2012, Verona (IT)
![Page 8: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/8.jpg)
La storia
• Il progetto ora è PHP Hypertext Preprocessor
• La nuova versione è PHP 3: include API, supporto multi database, è slegato dal POSIX e funziona anche su Microsoft Windows.
• In breve raggiunge il 10% dei domini attivi (erano 60.000 all'epoca)
8
1998
• Viene rilasciato PHP4 con il primo Zend Core(dai nomi Zeev + Andi)
• Include full support ad HTTP, output buffering, moduli esterni ed una prima versione di OOP
2000
![Page 9: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/9.jpg)
La storia
• Arriva PHP 5 assieme allo Zend Engine 2.0
• Il paradigma OOP ora è completo
• PEAR (PHP Extension and Application Repository)
• PECL (PHP Extensions Community Library)
9
2004
• 2008 PHP 5.3: closures, namespaces, lambda...
• 2012 PHP 5.4: traits, short array syntax, UTF-8...
...ad oggi
![Page 10: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/10.jpg)
La sintassi
• Semplice e facile da capire
• Gli statements terminano sempre con semi-colon ;
• Deriva da C, Perl e Java.
• File di testo con all'interno dei tag speciali
• Per commentare una singola riga si usa il double slash // , la multipla /**/.
10
![Page 11: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/11.jpg)
Tag PHP
• Standard <?php ?>
• Short <? ?>, <?=$var ?>
• Script <script language=”php”></script>
• ASP <% %>
11
![Page 12: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/12.jpg)
Tag PHP
• Standard <?php ?>
• Short <? ?>, <?=$var ?>
• Script <script language=”php”></script>
• ASP <% %>
12
Short, script ed ASP sono deprecati, ed il loro uso è fortemente scoraggiato!
![Page 13: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/13.jpg)
Caratteri newline
• Tutti i caratteri fuori dai tag PHP sono copiati così come sono verso l'output
• Le sequenze di newline valide sono \r, \n o \r\n.
• Ma in HTTP il carattere \n è usato per separare gli headers
• La prima newline dopo il tag di chiusura ?> viene sempre eliminata dall’interprete
• Alcuni editor aggiungono automaticamente una \n alla fine dei file
13
![Page 14: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/14.jpg)
Caratteri newline
• Tutti i caratteri fuori dai tag PHP sono copiati così come sono verso l'output
• Le sequenze di newline valide sono \r, \n o \r\n.
• Ma in HTTP il carattere \n è usato per separare gli headers
• La prima newline dopo il tag di chiusura ?> viene sempre eliminata dall’interprete
• Alcuni editor aggiungono automaticamente una \n alla fine dei file
14
Se non è strettamente necessario, è meglio non aggiungere mai il tag PHP di chiusura ad un file
![Page 15: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/15.jpg)
Dati scalariTipi di dato:
• boolean: un valore che può essere o true o false
• int: con segno, intero
• string: un valore con tipo di dato binario
• float: con segno, floating-point
Sistemi di numerazione:
• Decimal: notazione standard, es. 20; -30; 1239...
• Octal: identificata dal leading zero, es. 0777, 0200...Attenzione a non confondere ottale e decimale: 0777 è diverso da 777!
• Hexadecimal: identificata dal prefisso 0x (case insensitive),es. 0x101, 0XFF, -0X12
Notazioni:
• Decimale: tradizionale 0.12, 123.10, -554
• Esponenziale: identificata dalla mantissa, es. 1E2, 2E5, 1.2e3, 15
![Page 16: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/16.jpg)
Dati compound e altri tipi
Compound
• Array: sono contenitori di dati ordinati e possino essere usati per fare store di qualsiasi altro tipo di dato.
• Oggetti: sono contenitori di dati e codice
Altri tipi speciali
• Null: indica che non c'è valore, è una special flag.
• Resource: è un tipo di dato che indica una risorsa non nativa di PHP, come ad esempio i context
16
![Page 17: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/17.jpg)
Overflow?!
17
PHP non tiene traccia degli overflow ed operazioni banali possono causare risultati catastrofici.
print ‘And the answer is... ’ . (int) ( (0.1 + 0.7) * 10 );
Cosa stampa la riga sopra?
![Page 18: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/18.jpg)
Overflow?!
18
PHP non tiene traccia degli overflow ed operazioni banali possono causare risultati catastrofici.
print ‘And the answer is... ’ . (int) ( (0.1 + 0.7) * 10 );
Cosa stampa la riga sopra?0,1 più 0,7 fa 0,8, che per 10 fa 8. Ok?
![Page 19: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/19.jpg)
Overflow?!
19
PHP non tiene traccia degli overflow ed operazioni banali possono causare risultati catastrofici.
print ‘And the answer is... ’ . (int) ( (0.1 + 0.7) * 10 );
Cosa stampa la riga sopra?0,1 più 0,7 fa 0,8, che per 10 fa 8. Ok?And the answer is... 7
![Page 20: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/20.jpg)
Overflow?!
20
PHP non tiene traccia degli overflow ed operazioni banali possono causare risultati catastrofici.
print ‘And the answer is... ’ . (int) ( (0.1 + 0.7) * 10 );
Cosa stampa la riga sopra?0,1 più 0,7 fa 0,8, che per 10 fa 8. Ok?And the answer is... 7
I float 0.1 + 0.7 è invece uguale a 7.999999 e durante il cast, la parte decimale viene troncata. Il risultato è un errore del 12,5%!
Per le operazioni matematiche va usata l’estensione BCMath.
![Page 21: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/21.jpg)
Mappa degli operatori
21
![Page 22: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/22.jpg)
Mappa degli operatori
22
![Page 23: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/23.jpg)
Mappa degli operatori
23
![Page 24: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/24.jpg)
Mappa degli operatori
24
![Page 25: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/25.jpg)
Functions & OOP
25
PHP 5.3 ZCE Study GuideZend Technologies Ltd.
Pagg. 82 - 112
Sebastian BergmannUnderstanding the PHP Object Model
http://goo.gl/pA8MB
![Page 26: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/26.jpg)
Task 1: da procedure a Symfony 2
26
Symfony 2.0 BookSensioLabs
Pagg. 14 - 25
![Page 27: php: back to basics](https://reader036.vdocumenti.com/reader036/viewer/2022081720/558e006e1a28ab75518b45a7/html5/thumbnails/27.jpg)
Books
27
PHP Objects, Patterns, and PracticeMatt Zandstra
Apress