home *** CD-ROM | disk | FTP | other *** search
Wrap
Text Truncated. Only the first 1MB is shown below. Download the file for the complete contents.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >PHP Handbuch</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="book" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="BOOK" ><A NAME="manual" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" ><A NAME="manual" >PHP Handbuch</A ></H1 ><DIV CLASS="author" >Mehdi Achour</DIV ><DIV CLASS="author" >Friedhelm Betz</DIV ><DIV CLASS="author" >Antony Dovgal</DIV ><DIV CLASS="author" >Nuno Lopes</DIV ><DIV CLASS="author" >Philip Olson</DIV ><DIV CLASS="author" >Georg Richter</DIV ><DIV CLASS="author" >Damien Seguy</DIV ><DIV CLASS="author" >Jakub Vrana</DIV ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >
<A HREF="#contributors" >And several others</A > </SPAN ><BR></SPAN ><H2 CLASS="EDITEDBY" >Herausgegeben von</H2 ><DIV CLASS="editor" >Gabor Hojtsy</DIV ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Bei der ▄bersetzung wirkten mit:</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Sebastian Bergmann</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Friedhelm Betz</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Cornelia Boenigk</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Ali Chahvand</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Wolfgang Drews</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Ralf Ebert</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Thomas Fromm</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Stefan Hanauska</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Johann-Peter Hartmann</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Hartmut Holzgraefe</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Martin Jansen</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Michael Kaiser</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Mark Kronsbein</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Hakan Kⁿcⁿkyilmaz</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Sven Lauer</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Jan Lehnardt</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Sebastian Nohn</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Tobias Orterer</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Catharina Paulsen</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Peter Petermann</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Sebastian-H. Picklum</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Franziskus Domig</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Georg Richter</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Stefan Saasen</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Martin Samesch</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Egon Schmid</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Thomas Sch÷fbeck</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Holger Schranz</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Thomas Schⁿrmann</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Uwe Steinmann</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Christian Ullrich</SPAN ><BR></SPAN ><SPAN CLASS="collab" ><SPAN CLASS="collabname" >Mark Wiesemann</SPAN ><BR></SPAN ><P CLASS="pubdate" >11-08-2004<BR></P ><P CLASS="copyright" >Copyright © 1997-2004 der PHP-Dokumentationsgruppe</P ><DIV CLASS="legalnotice" ><A NAME="copyright" ></A ><P ><B >Copyright</B ></P ><P >
Copyright ⌐ 1997 - 2004 by the PHP Documentation Group. Dieses Material darf nur gemΣ▀ den Regeln und Bedingungen der Open Publication Licence Version v1.0 oder neuer weiter verbreitet werden. Eine Kopie der <A HREF="#opl.license" >Open Publication License</A > ist in diesem Handbuch enthalten, die aktuellste Version ist gegenwΣrtig immer unter <A HREF="http://www.opencontent.org/openpub/" TARGET="_top" >http://www.opencontent.org/openpub/</A > verfⁿgbar. </P ><P >
Die Verbreitung von substantiell modifizierten Versionen dieses Dokuments ist ohne die ausdrⁿckliche Erlaubnis des Copyright-Inhabers untersagt. </P ><P >
Die Verbreitung dieser Arbeit oder abgeleiteter Arbeiten in jeglicher Standard-(Papier-) Buchform ist ohne vorherige Erlaubnis durch den Copyright-Inhaber verboten. </P ><P >
Fⁿr den Fall, dass Sie daran interessiert sind, dieses Dokument weiter zu verbreiten oder in sonstiger Form zu ver÷ffentlichen, in Teilen oder als Ganzes, entweder verΣndert oder unverΣndert und Sie Fragen haben, k÷nnen Sie Kontakt zu den Copyright-Inhabern ⁿber <A HREF="mailto:doc-license@lists.php.net" TARGET="_top" >doc-license@lists.php.net</A >. aufnehmen. Bitte beachten Sie, dass das Archiv dieser Maillingliste ÷ffentlich zugΣnglich ist. </P ><P >
Das Kapitel 'Extending PHP 4.0' ist ⌐ 2000 by Zend Technologies, Ltd. Dieses Material darf nur gemΣ▀ den Regeln und Bedingungen der Open Publication Licence Version v1.0 oder neuer, (die aktuellste Version ist gegenwΣrtig unter <A HREF="http://www.opencontent.org/openpub/" TARGET="_top" >http://www.opencontent.org/openpub/</A > verfⁿgbar), verbreitet werden. </P ></DIV ><HR></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT ><A HREF="#preface" >Vorwort</A ></DT ><DD ><DL ><DT ><A HREF="#contributors" >Autoren und Mitwirkende</A ></DT ></DL ></DD ><DT >I. <A HREF="#getting-started" >Einfⁿhrung</A ></DT ><DD ><DL ><DT >1. <A HREF="#introduction" >Einleitung</A ></DT ><DT >2. <A HREF="#tutorial" >Kleines Tutorial</A ></DT ></DL ></DD ><DT >II. <A HREF="#install" >Installation und Konfiguration</A ></DT ><DD ><DL ><DT >3. <A HREF="#install.general" >General Installation Considerations</A ></DT ><DT >4. <A HREF="#install.macosx" >Installation on Mac OS X</A ></DT ><DT >5. <A HREF="#install.unix" >Installation on Unix systems</A ></DT ><DT >6. <A HREF="#install.windows" >Installation on Windows systems</A ></DT ><DT >7. <A HREF="#install.problems" >Problems?</A ></DT ><DT >8. <A HREF="#configuration" >Runtime Configuration</A ></DT ></DL ></DD ><DT >III. <A HREF="#langref" >Sprachreferenz</A ></DT ><DD ><DL ><DT >9. <A HREF="#language.basic-syntax" >Grundlagen der Syntax</A ></DT ><DT >10. <A HREF="#language.types" >Typen</A ></DT ><DT >11. <A HREF="#language.variables" >Variablen</A ></DT ><DT >12. <A HREF="#language.constants" >Konstanten</A ></DT ><DT >13. <A HREF="#language.expressions" >Ausdrⁿcke</A ></DT ><DT >14. <A HREF="#language.operators" >Operatoren</A ></DT ><DT >15. <A HREF="#language.control-structures" >Kontroll-Strukturen</A ></DT ><DT >16. <A HREF="#functions" >Funktionen</A ></DT ><DT >17. <A HREF="#language.oop" >Klassen und Objekte</A ></DT ><DT >18. <A HREF="#language.oop5" >Classes and Objects (PHP 5)</A ></DT ><DT >19. <A HREF="#language.references" >Referenzen in PHP</A ></DT ></DL ></DD ><DT >IV. <A HREF="#security" >Sicherheit</A ></DT ><DD ><DL ><DT >20. <A HREF="#security.intro" >Einfⁿhrung</A ></DT ><DT >21. <A HREF="#security.general" >Allgemeine ▄berlegungen</A ></DT ><DT >22. <A HREF="#security.cgi-bin" >CGI-Version</A ></DT ><DT >23. <A HREF="#security.apache" >Apache-Modul</A ></DT ><DT >24. <A HREF="#security.filesystem" >Dateisystem - Sicherheit</A ></DT ><DT >25. <A HREF="#security.database" >Datenbank - Sicherheit</A ></DT ><DT >26. <A HREF="#security.errors" >Fehlerbehandlung</A ></DT ><DT >27. <A HREF="#security.globals" >Verwendung von Register Globals</A ></DT ><DT >28. <A HREF="#security.variables" >Vom Nutzer ⁿbermittelte Daten</A ></DT ><DT >29. <A HREF="#security.hiding" >Verstecken von PHP</A ></DT ><DT >30. <A HREF="#security.current" >Aktuell bleiben</A ></DT ></DL ></DD ><DT >V. <A HREF="#features" >Features</A ></DT ><DD ><DL ><DT >31. <A HREF="#features.http-auth" >HTTP-Authentifizierung mit PHP</A ></DT ><DT >32. <A HREF="#features.cookies" >Cookies</A ></DT ><DT >33. <A HREF="#features.xforms" >Dealing with XForms</A ></DT ><DT >34. <A HREF="#features.file-upload" >Steuerung von Dateiuploads</A ></DT ><DT >35. <A HREF="#features.remote-files" >Zugriff auf entfernte Dateien</A ></DT ><DT >36. <A HREF="#features.connection-handling" >Verbindungssteuerung</A ></DT ><DT >37. <A HREF="#features.persistent-connections" >Persistente Datenbankverbindungen</A ></DT ><DT >38. <A HREF="#features.safe-mode" >Safe Mode</A ></DT ><DT >39. <A HREF="#features.commandline" >PHP auf der Kommandozeile</A ></DT ></DL ></DD ><DT >VI. <A HREF="#funcref" >Funktionsreferenz</A ></DT ><DD ><DL ><DT >I. <A HREF="#ref.apache" >Apache-spezifische Funktionen</A ></DT ><DT >II. <A HREF="#ref.array" >Array Funktionen</A ></DT ><DT >III. <A HREF="#ref.aspell" >Aspell Funktionen [veraltet]</A ></DT ><DT >IV. <A HREF="#ref.bc" >Mathematische Funktionen mit beliebiger Genauigkeit</A ></DT ><DT >V. <A HREF="#ref.bzip2" >Bzip2 Komprimierungsfunktionen</A ></DT ><DT >VI. <A HREF="#ref.calendar" >Kalender-Funktionen</A ></DT ><DT >VII. <A HREF="#ref.ccvs" >CCVS API Funktionen</A ></DT ><DT >VIII. <A HREF="#ref.com" >COM Support Funktionen fⁿr Windows</A ></DT ><DT >IX. <A HREF="#ref.classobj" >Klassen- und Objekt-Funktionen</A ></DT ><DT >X. <A HREF="#ref.cpdf" >ClibPDF Funktionen</A ></DT ><DT >XI. <A HREF="#ref.crack" >Crack Funktionen</A ></DT ><DT >XII. <A HREF="#ref.curl" >cURL, Client URL Bibiothek-Funktionen</A ></DT ><DT >XIII. <A HREF="#ref.cybercash" >Cybercash Bezahl Funktionen</A ></DT ><DT >XIV. <A HREF="#ref.cyrus" >Cyrus IMAP administration Functions</A ></DT ><DT >XV. <A HREF="#ref.ctype" >Character type functions</A ></DT ><DT >XVI. <A HREF="#ref.dba" >dba Datenbank (dbm-style) Abstraktions-Funktionen</A ></DT ><DT >XVII. <A HREF="#ref.datetime" >Datums- und Zeit-Funktionen</A ></DT ><DT >XVIII. <A HREF="#ref.dbase" >dBase Funktionen</A ></DT ><DT >XIX. <A HREF="#ref.dbm" >DBM Datenbankfunktionen</A ></DT ><DT >XX. <A HREF="#ref.dbx" >dbx Funktionen</A ></DT ><DT >XXI. <A HREF="#ref.dbplus" >DB++ Functions</A ></DT ><DT >XXII. <A HREF="#ref.dio" >Direkte Ein-/Ausgabe Funktionen</A ></DT ><DT >XXIII. <A HREF="#ref.dir" >Verzeichnis-Funktionen</A ></DT ><DT >XXIV. <A HREF="#ref.dom" >DOM Functions</A ></DT ><DT >XXV. <A HREF="#ref.domxml" >DOM XML Funktionen</A ></DT ><DT >XXVI. <A HREF="#ref.dotnet" >.NET Funktionen</A ></DT ><DT >XXVII. <A HREF="#ref.errorfunc" >Error Handling and Logging Functions</A ></DT ><DT >XXVIII. <A HREF="#ref.exif" >Exif Functions</A ></DT ><DT >XXIX. <A HREF="#ref.fam" >File Alteration Monitor Functions</A ></DT ><DT >XXX. <A HREF="#ref.fbsql" >FrontBase Funktionen</A ></DT ><DT >XXXI. <A HREF="#ref.filepro" >filePro Funktionen</A ></DT ><DT >XXXII. <A HREF="#ref.filesystem" >Funktionen des Dateisystems</A ></DT ><DT >XXXIII. <A HREF="#ref.fdf" >Forms Data Format Funktionen</A ></DT ><DT >XXXIV. <A HREF="#ref.fribidi" >FriBiDi Funktionen</A ></DT ><DT >XXXV. <A HREF="#ref.ftp" >FTP-Funktionen</A ></DT ><DT >XXXVI. <A HREF="#ref.funchand" >Function Handling functions</A ></DT ><DT >XXXVII. <A HREF="#ref.gettext" >GNU Gettext</A ></DT ><DT >XXXVIII. <A HREF="#ref.gmp" >GMP Functions</A ></DT ><DT >XXXIX. <A HREF="#ref.http" >HTTP functions</A ></DT ><DT >XL. <A HREF="#ref.hw" >Hyperwave Functions</A ></DT ><DT >XLI. <A HREF="#ref.hwapi" >Hyperwave API Functions</A ></DT ><DT >XLII. <A HREF="#ref.iconv" >iconv Funktionen</A ></DT ><DT >XLIII. <A HREF="#ref.image" >Grafik-Funktionen</A ></DT ><DT >XLIV. <A HREF="#ref.imap" >IMAP, POP3 und NNTP Funktionen</A ></DT ><DT >XLV. <A HREF="#ref.ifx" >Informix Funktionen</A ></DT ><DT >XLVI. <A HREF="#ref.ibase" >InterBase-Funktionen</A ></DT ><DT >XLVII. <A HREF="#ref.id3" >ID3 Functions</A ></DT ><DT >XLVIII. <A HREF="#ref.ingres" >Ingres II Funktionen</A ></DT ><DT >XLIX. <A HREF="#ref.ircg" >IRC Gateway Funktionen</A ></DT ><DT >L. <A HREF="#ref.java" >Java</A ></DT ><DT >LI. <A HREF="#ref.ldap" >LDAP Funktionen</A ></DT ><DT >LII. <A HREF="#ref.lzf" >LZF Functions</A ></DT ><DT >LIII. <A HREF="#ref.mail" >Mail-Funktionen</A ></DT ><DT >LIV. <A HREF="#ref.mailparse" >mailparse Functions</A ></DT ><DT >LV. <A HREF="#ref.math" >Mathematische Funktionen</A ></DT ><DT >LVI. <A HREF="#ref.mbstring" >Multibyte String Functions</A ></DT ><DT >LVII. <A HREF="#ref.mcal" >MCAL Funktionen</A ></DT ><DT >LVIII. <A HREF="#ref.mcrypt" >Mcrypt Encryption Functions</A ></DT ><DT >LIX. <A HREF="#ref.mcve" >MCVE Payment Functions</A ></DT ><DT >LX. <A HREF="#ref.memcache" >Memcache Functions</A ></DT ><DT >LXI. <A HREF="#ref.mhash" >Hash Funktionen</A ></DT ><DT >LXII. <A HREF="#ref.mime-magic" >Mimetype Functions</A ></DT ><DT >LXIII. <A HREF="#ref.mssql" >Microsoft SQL Server Funktionen</A ></DT ><DT >LXIV. <A HREF="#ref.ming" >Ming functions for Flash</A ></DT ><DT >LXV. <A HREF="#ref.misc" >Sonstige Funktionen</A ></DT ><DT >LXVI. <A HREF="#ref.mnogo" >mnoGoSearch Funktionen</A ></DT ><DT >LXVII. <A HREF="#ref.msql" >mSQL Funktionen</A ></DT ><DT >LXVIII. <A HREF="#ref.mysql" >MySQL Funktionen</A ></DT ><DT >LXIX. <A HREF="#ref.mysqli" >Verbesserte MySQL Erweiterung</A ></DT ><DT >LXX. <A HREF="#ref.msession" >Mohawk Software Session Handler Funktionen</A ></DT ><DT >LXXI. <A HREF="#ref.muscat" >muscat Functions</A ></DT ><DT >LXXII. <A HREF="#ref.network" >Netzwerk Funktionen</A ></DT ><DT >LXXIII. <A HREF="#ref.ncurses" >Ncurses Terminal Screen Control Functions</A ></DT ><DT >LXXIV. <A HREF="#ref.notes" >Lotus Notes Functions</A ></DT ><DT >LXXV. <A HREF="#ref.nsapi" >NSAPI-specific Functions</A ></DT ><DT >LXXVI. <A HREF="#ref.odbc" >ODBC Funktionen</A ></DT ><DT >LXXVII. <A HREF="#ref.objaggregation" >Object Aggregation/Composition Functions</A ></DT ><DT >LXXVIII. <A HREF="#ref.oci8" >Oracle 8 Funktionen</A ></DT ><DT >LXXIX. <A HREF="#ref.openssl" >OpenSSL Funktionen</A ></DT ><DT >LXXX. <A HREF="#ref.oracle" >Oracle Funktionen</A ></DT ><DT >LXXXI. <A HREF="#ref.ovrimos" >Ovrimos SQL Functions</A ></DT ><DT >LXXXII. <A HREF="#ref.outcontrol" >Funktionen zur Ausgabesteuerung</A ></DT ><DT >LXXXIII. <A HREF="#ref.overload" >Object property and method call overloading</A ></DT ><DT >LXXXIV. <A HREF="#ref.pdf" >PDF Funktionen</A ></DT ><DT >LXXXV. <A HREF="#ref.pfpro" >Verisign Payflow Pro Funktionen</A ></DT ><DT >LXXXVI. <A HREF="#ref.info" >PHP Optionen & Informationen</A ></DT ><DT >LXXXVII. <A HREF="#ref.posix" >POSIX Funktionen</A ></DT ><DT >LXXXVIII. <A HREF="#ref.pgsql" >PostgreSQL Funktionen</A ></DT ><DT >LXXXIX. <A HREF="#ref.pcntl" >Process Control Functions</A ></DT ><DT >XC. <A HREF="#ref.exec" >Funktionen zur Programmausfⁿhrung</A ></DT ><DT >XCI. <A HREF="#ref.printer" >Printer Functions</A ></DT ><DT >XCII. <A HREF="#ref.pspell" >Pspell Funktionen</A ></DT ><DT >XCIII. <A HREF="#ref.readline" >GNU Readline</A ></DT ><DT >XCIV. <A HREF="#ref.recode" >GNU Recode Funktionen</A ></DT ><DT >XCV. <A HREF="#ref.pcre" >RegulΣre Ausdrⁿcke Funktionen (Perl-kompatibel)</A ></DT ><DT >XCVI. <A HREF="#ref.qtdom" >qtdom Functions</A ></DT ><DT >XCVII. <A HREF="#ref.regex" >RegulΣre Ausdrⁿcke Funktionen (POSIX erweitert)</A ></DT ><DT >XCVIII. <A HREF="#ref.sem" >Semaphor und Shared Memory Funktionen</A ></DT ><DT >XCIX. <A HREF="#ref.sesam" >SESAM Datenbankfunktionen</A ></DT ><DT >C. <A HREF="#ref.session" >Session Funktionen</A ></DT ><DT >CI. <A HREF="#ref.shmop" >Shared Memory Funktionen (Gemeinsamer Speicher)</A ></DT ><DT >CII. <A HREF="#ref.simplexml" >SimpleXML functions</A ></DT ><DT >CIII. <A HREF="#ref.soap" >SOAP Functions</A ></DT ><DT >CIV. <A HREF="#ref.sqlite" >SQLite</A ></DT ><DT >CV. <A HREF="#ref.swf" >Shockwave Flash Funktionen</A ></DT ><DT >CVI. <A HREF="#ref.snmp" >SNMP Funktionen</A ></DT ><DT >CVII. <A HREF="#ref.sockets" >Socket Funktionen</A ></DT ><DT >CVIII. <A HREF="#ref.spl" >Standard PHP Library (SPL) Functions</A ></DT ><DT >CIX. <A HREF="#ref.stream" >Stream Functions</A ></DT ><DT >CX. <A HREF="#ref.strings" >String-Funktionen</A ></DT ><DT >CXI. <A HREF="#ref.sybase" >Sybase Funktionen</A ></DT ><DT >CXII. <A HREF="#ref.tcpwrap" >TCP Wrappers Functions</A ></DT ><DT >CXIII. <A HREF="#ref.tidy" >Tidy Functions</A ></DT ><DT >CXIV. <A HREF="#ref.tokenizer" >Tokenizer Functions</A ></DT ><DT >CXV. <A HREF="#ref.url" >URL Funktionen</A ></DT ><DT >CXVI. <A HREF="#ref.variables" >Variablen-Funktionen</A ></DT ><DT >CXVII. <A HREF="#ref.vpopmail" >vpopmail Functions</A ></DT ><DT >CXVIII. <A HREF="#ref.w32api" >W32api Functions</A ></DT ><DT >CXIX. <A HREF="#ref.wddx" >WDDX Funktionen</A ></DT ><DT >CXX. <A HREF="#ref.xml" >XML Parser Functions</A ></DT ><DT >CXXI. <A HREF="#ref.xmlrpc" >XML-RPC Functions</A ></DT ><DT >CXXII. <A HREF="#ref.xdiff" >xdiff Functions</A ></DT ><DT >CXXIII. <A HREF="#ref.xsl" >XSL functions</A ></DT ><DT >CXXIV. <A HREF="#ref.xslt" >XSLT Funktionen</A ></DT ><DT >CXXV. <A HREF="#ref.yaz" >YAZ Functions</A ></DT ><DT >CXXVI. <A HREF="#ref.nis" >YP/NIS Funktionen</A ></DT ><DT >CXXVII. <A HREF="#ref.zip" >ZIP Funktionen (Lesezugriff)</A ></DT ><DT >CXXVIII. <A HREF="#ref.zlib" >Zlib Komprimierungsfunktionen</A ></DT ></DL ></DD ><DT >VII. <A HREF="#zend" >Zend API</A ></DT ><DD ><DL ><DT >40. <A HREF="#zend.overview" >Overview</A ></DT ><DT >41. <A HREF="#zend.possibilities" >Extension Possibilities</A ></DT ><DT >42. <A HREF="#zend.layout" >Source Layout</A ></DT ><DT >43. <A HREF="#zend.build" >PHP's Automatic Build System</A ></DT ><DT >44. <A HREF="#zend.creating" >Creating Extensions</A ></DT ><DT >45. <A HREF="#zend.using" >Using Extensions</A ></DT ><DT >46. <A HREF="#zend.troubleshooting" >Troubleshooting</A ></DT ><DT >47. <A HREF="#zend.structure" >Source Discussion</A ></DT ><DT >48. <A HREF="#zend.arguments" >Accepting Arguments</A ></DT ><DT >49. <A HREF="#zend.variables" >Creating Variables</A ></DT ><DT >50. <A HREF="#zend.copy-constructor" >Duplicating Variable Contents: The Copy Constructor</A ></DT ><DT >51. <A HREF="#zend.returning" >Returning Values</A ></DT ><DT >52. <A HREF="#zend.printing" >Printing Information</A ></DT ><DT >53. <A HREF="#zend.startup-and-shutdown" >Startup and Shutdown Functions</A ></DT ><DT >54. <A HREF="#zend.calling-user-functions" >Calling User Functions</A ></DT ><DT >55. <A HREF="#zend.ini-file-support" >Initialization File Support</A ></DT ><DT >56. <A HREF="#zend.where-to-go" >Where to Go from Here</A ></DT ><DT >57. <A HREF="#zend.configuration-macros" >Reference: Some Configuration Macros</A ></DT ><DT >58. <A HREF="#zend.api-macros" >API Macros</A ></DT ></DL ></DD ><DT >VIII. <A HREF="#api" >PHP API: Schnittstellen fⁿr Programmierer</A ></DT ><DD ><DL ><DT >59. <A HREF="#streams" >Streams API for PHP Extension Authors</A ></DT ></DL ></DD ><DT >IX. <A HREF="#faq" >FAQ: Frequently Asked Questions</A ></DT ><DD ><DL ><DT >60. <A HREF="#faq.general" >Allgemeine Informationen</A ></DT ><DT >61. <A HREF="#faq.mailinglist" >Mailing-Listen</A ></DT ><DT >62. <A HREF="#faq.obtaining" >Obtaining PHP</A ></DT ><DT >63. <A HREF="#faq.databases" >Database issues</A ></DT ><DT >64. <A HREF="#faq.installation" >Installation</A ></DT ><DT >65. <A HREF="#faq.build" >Probleme bei der Compilierung</A ></DT ><DT >66. <A HREF="#faq.using" >PHP benutzen</A ></DT ><DT >67. <A HREF="#faq.html" >PHP und HTML</A ></DT ><DT >68. <A HREF="#faq.com" >PHP and COM</A ></DT ><DT >69. <A HREF="#faq.languages" >PHP und andere Sprachen</A ></DT ><DT >70. <A HREF="#faq.migration" >Von PHP 2 auf PHP 3 umsteigen</A ></DT ><DT >71. <A HREF="#faq.migration4" >Von PHP 3 auf PHP 4 umsteigen</A ></DT ><DT >72. <A HREF="#faq.migration5" >Migrating from PHP 4 to PHP 5</A ></DT ><DT >73. <A HREF="#faq.misc" >Verschiedene Fragen</A ></DT ></DL ></DD ><DT >X. <A HREF="#appendixes" >Anhang</A ></DT ><DD ><DL ><DT >A. <A HREF="#history" >Die Geschichte von PHP und verwandten Projekten</A ></DT ><DT >B. <A HREF="#migration5" >Migrating from PHP 4 to PHP 5</A ></DT ><DT >C. <A HREF="#migration4" >Migrating from PHP 3 to PHP 4</A ></DT ><DT >D. <A HREF="#migration" >Migration von PHP/FI 2.0 zu PHP 3.0</A ></DT ><DT >E. <A HREF="#debugger" >Der PHP-Debugger</A ></DT ><DT >F. <A HREF="#phpdevel" >Extending PHP 3</A ></DT ><DT >G. <A HREF="#configure" >Configure options</A ></DT ><DT >H. <A HREF="#ini" >Liste der core <TT CLASS="filename" >php.ini</TT > Einstellungen</A ></DT ><DT >I. <A HREF="#aliases" >Liste der Funktion Aliase</A ></DT ><DT >J. <A HREF="#reserved" >Reservierte W÷rter in PHP</A ></DT ><DT >K. <A HREF="#resource" >Liste von Ressourcentypen</A ></DT ><DT >L. <A HREF="#wrappers" >List of Supported Protocols/Wrappers</A ></DT ><DT >M. <A HREF="#filters" >List of Available Filters</A ></DT ><DT >N. <A HREF="#transports" >List of Supported Socket Transports</A ></DT ><DT >O. <A HREF="#types.comparisons" >PHP type comparison tables</A ></DT ><DT >P. <A HREF="#tokens" >List of Parser Tokens</A ></DT ><DT >Q. <A HREF="#about" >▄ber das Handbuch</A ></DT ><DT >R. <A HREF="#opl.license" >Open Publication License</A ></DT ><DT >S. <A HREF="#indexes" >Funktions-Index</A ></DT ><DT >T. <A HREF="#missing-stuff" >Fehlendes</A ></DT ></DL ></DD ></DL ></DIV ><DIV CLASS="preface" ><HR><H1 ><A NAME="preface" >Vorwort</A ></H1 ><BLOCKQUOTE CLASS="ABSTRACT" ><DIV CLASS="abstract" ><P ></P ><A NAME="AEN120" ></A ><P >
<ACRONYM CLASS="acronym" >PHP</ACRONYM > ist die Abkⁿrzung fⁿr "PHP: Hypertext Preprocessor", eine weitverbreitete Open Source Skriptsprache speziell fⁿr Webentwicklungen. PHP lΣ▀t sich in HTML einbinden. Die Syntax erinnert an C, Java und Perl und ist einfach zu erlernen. Das Hauptziel dieser Sprache ist es, Webentwicklern die M÷glichkeit zu geben, schnell dynamisch generierte Webseiten zu erzeugen. Aber Sie k÷nnen PHP fⁿr weitaus mehr einsetzen. </P ><P ></P ></DIV ></BLOCKQUOTE ><P >
Dieses Handbuch besteht vorranging aus einer <A HREF="#funcref" >
Funktionsreferenz</A >, enthΣlt aber zusΣtzlich auch eine <A HREF="#langref" >Sprachreferenz</A >, ErlΣuterungen zu den wichtigsten <A HREF="#features" >Features</A > und weitere <A HREF="#appendixes" >ergΣnzende Informationen</A >. </P ><P >
Sie k÷nnen dieses Handbuch in verschiedenen Formaten unter <A HREF="http://www.php.net/download-docs.php" TARGET="_top" >http://www.php.net/download-docs.php</A > herunterladen. Informationen dazu, wie dieses Handbuch erstellt wird finden Sie im Anhang unter dem Kapitel <A HREF="#about" >
'▄ber dieses Handbuch'</A >. Wenn Sie sich fⁿr die <A HREF="#history" >Geschichte von PHP</A > interessieren, lesen Sie den entsprechenden Anhang. </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="contributors" >Autoren und Mitwirkende</A ></H2 ><P >Wir heben die zur Zeit aktivsten Personen auf der Titelseite des Handbuchs hervor, aber es gibt viel mehr Mitwirkende, die zurzeit mithelfen oder in der Vergangenheit einen gro▀en Beitrag zu diesem Projekt geleistet haben. Ebenfalls gibt es eine Menge von Personen, die hier nicht namentlich aufgefⁿhrt sind, die durch ihre User Notes auf den Handbuchseiten mithelfen. Die User Notes werden kontinuierlich in unser Handbuch integriert und wir schΣtzen diese Unterstⁿtzung au▀erordentlich. Alle folgenden Listen sind alphabetisch sortiert.</P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="AEN135" >Authoren und Editoren</A ></H3 ><P >
Obwohl im Augenblick nicht aktiv, haben folgende Personen in der Vergangenheit wesentlichen Inhalt zum Handbuch beigetragen: Jouni Ahto, Alexander Aulbach, Daniel Beckham, Stig Bakken, Jesus M. Castagnetto, Ron Chmara, John Coggeshall, Simone Cortesi, Markus Fischer, Wez Furlong, Sara Golemon, Rui Hirokawa, Brad House, Moriyoshi Koizumi, Rasmus Lerdorf, Andrew Lindeman, Stanislav Malyshev, Rafael Martinez, Yasuo Ohgaki, Derick Rethans, Sander Roobol, Egon Schmid, Thomas Schoefbeck, Sascha Schumann, Lars Torben Wilson, Jim Winstead, Jeroen van Wolffelaar und Andrei Zmievski. </P ><P >
Obwohl im Augenblick nicht aktiv, haben folgende Personen in der Vergangenheit durch Ihre Editionsarbeit wesentlich zum Handbuch beigetragen: Stig Bakken, Hartmut Holzgraefe und Egon Schmid. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="AEN139" >User Note Betreuer</A ></H3 ><P >
Zur Zeit aktive Betreuer: Mehdi Achour, Friedhelm Betz, Vincent Gevers, Aidan Lister, Nuno Lopes und Tom Sommer. </P ><P >
Folgende Personen haben in der Vergangenheit einiges an Mⁿhe und Zeit zur Betreuung der User Notes investiert: Daniel Beckham, Victor Boivie, Jesus M. Castagnetto, Nicolas Chaillan, Ron Chmara, James Cox, Sara Golemon, Zak Greant, Szabolcs Heilig, Oliver Hinckel, Hartmut Holzgraefe, Rasmus Lerdorf, Andrew Lindeman, Maxim Maletsky, James Moore, Sebastian Picklum, Derick Rethans, Sander Roobol, Damien Seguy, Jason Sheets, Jani Taskinen, Yasuo Ohgaki, Philip Olson, Lars Torben Wilson, Jim Winstead, Jared Wyles und Jeroen van Wolffelaar. </P ></DIV ></DIV ></DIV ><DIV CLASS="PART" ><A NAME="getting-started" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >I. Einfⁿhrung</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT >1. <A HREF="#introduction" >Einleitung</A ></DT ><DT >2. <A HREF="#tutorial" >Kleines Tutorial</A ></DT ></DL ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="introduction" >Kapitel 1. Einleitung</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="intro-whatis" >Was ist PHP?</A ></H2 ><P >
<ACRONYM CLASS="acronym" >PHP</ACRONYM > <ACRONYM CLASS="acronym" >PHP</ACRONYM > (Akronym fⁿr "PHP: Hypertext Preprocessor") ist eine weit verbreitete und fⁿr den allgemeinen Gebrauch bestimmte Open Source Skriptsprache, welche speziell fⁿr die Webprogrammierung geeignet ist, und in HTML eingebettet werden kann. </P ><P >
H÷rt sich einfach an, aber was hei▀t es genau? Ein Beispiel: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN154" ></A ><P ><B >Beispiel 1-1. Ein einleitendes Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><html> <head> <title>Beispiel</title> </head> <body> <?php echo "Hallo, ich bin ein PHP-Skript!"; ?> </body> </html></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Beachten Sie den Unterschied zu einem Skript, welches in anderen Sprachen wie Perl oder C geschrieben wurde -- anstatt ein Programm mit vielen Anweisungen zur Ausgabe von HTML zu schreiben, schreibt man einen HTML-Code mit einigen eingebetteten Anweisungen, um etwas auszufⁿhren (z.B. um - wie oben - Text auszugeben). Der PHP-Code steht zwischen speziellen <A HREF="#language.basic-syntax.phpmode" >Anfangs- und Schlusstags</A >, mit denen man in den PHP-Modus und zurⁿck wechseln kann. </P ><P >
Was PHP von clientseitigen Sprachen wie Javaskript unterscheidet, ist dass der Code auf dem Server ausgefⁿhrt wird. Sollten Sie ein Skript wie das obige auf ihrem Server ausfⁿhren, wⁿrde der Besucher nur das Ergebnis empfangen, ohne die M÷glichkeit herauszufinden, wie der zugrundeliegende Code aussieht. Sie k÷nnen ihren Webserver auch anweisen, alle ihre HTML-Dateien mit PHP zu parsen, denn dann gibt es wirklich nichts, das dem Benutzer sagt, was sie in petto haben. </P ><P >
Das Beste an der Verwendung von PHP ist, dass es fⁿr Neulinge extrem simpel ist, aber auch einen riesigen Funktionsumfang fⁿr den professionellen Programmierer bietet. Scheuen Sie sich nicht, die lange Liste der PHP-Funktionen zu lesen. Sie k÷nnen einsteigen, und binnen weniger Stunden bereits mit dem Schreiben von einfachen Skripten beginnen. </P ><P >
Auch wenn die Entwicklung von PHP auf serverseitige Skripte fokussiert ist, k÷nnen Sie mit PHP weitaus mehr anstellen. Lesen Sie mehr im Abschnitt <A HREF="#intro-whatcando" >Was kann PHP?</A > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="intro-whatcando" >Was kann PHP?</A ></H2 ><P >
Alles. PHP ist hauptsΣchlich auf serverseitige Skripte fokussiert, weshalb Sie alles tun k÷nnen, was auch ein anderes CGI Programm kann, wie z.B. Formulardaten sammeln, dynamische Inhalte fⁿr Websites generieren oder Cookies senden und empfangen. Aber PHP kann noch viel mehr. </P ><P >
Es gibt drei Hauptgebiete, in denen PHP Skripte genutzt werden. <P ></P ><UL ><LI ><P >
Serverseitige Skripte. Dies ist das traditionelle und auch Hauptfeld von PHP. Sie ben÷tigen dazu drei Dinge. Den PHP Parser (CGI oder Server-Modul), einen Webserver und einen Webbrowser. Sie mⁿssen den Webserver, verbunden mit einer PHP Installation laufen lassen. Sie k÷nnen Sich Ausgabe der PHP Programme ⁿber den Server mit einem Webbrowser ansehen. Fⁿr weitere Informationen lesen Sie bitte im Abschnitt <A HREF="#installation" >Installation</A > weiter. </P ></LI ><LI ><P >
Skripte auf der Kommandozeile. Sie k÷nnen auch PHP Skripte schreiben, um sie ohne einen Server oder einen Browser laufen zu lassen. Hierfⁿr ben÷tigen Sie nur den PHP Parser. Diese Art der Verwendung ist fⁿr die regelmΣ▀ig auszufⁿhrende Skripte mittels cron (unter *nix oder Linux) oder dem Task Scheduler (unter Windows). Diese Skripte k÷nnen auch fⁿr einfache Aufgaben zur Verarbeitung von Text verwendet werden. Weitere Informationen dazu finden Sie im Abschnitt <A HREF="#features.commandline" >Using PHP from the command line</A >. </P ></LI ><LI ><P >
Schreiben clientseitiger GUI Applikationen. PHP ist wahrscheinlich nicht die allerbeste Sprache um GUI-Applikationen zu schreiben, aber wenn Sie PHP sehr gut kennen und einige weiterfⁿhrende Funktionen in Ihren clientseitigen Applikationen nutzen m÷chten, k÷nnen Sie PHP-GTK nutzen, um derartige Programme zu schreiben. Auf diese Art haben Sie auch die M÷glichkeit, Plattformⁿbergreifende Applikationen zu schreiben. PHP-GTK ist eine Erweiterung von PHP, welche in der Hauptdistribution nicht enthalten ist. Sollten Sie daran interessiert sein, besuchen Sie die <A HREF="http://gtk.php.net/" TARGET="_top" >PHP-GTK Website</A >. </P ></LI ></UL > </P ><P >
PHP kann auf allen gΣngigen Betriebssystemen verwendet werden, inkl. Linux, vielen Unix-Varianten (inkl. HP-UX, Solaris und OpenBSD), Microsoft Windows, Mac OS X, RISC OS, und wahrscheinlich anderen. PHP unterstⁿtzt auch die meisten der heute gebrΣuchlichen Webserver. Dies umfasst Apache, Microsoft Internet Information Server, Personal Web Server, Netscape und iPlanet Server, Oreilly Website Pro Server, Caudium, Xitami, OmniHTTPd, und viele andere. Fⁿr den Gro▀teil der Server bietet PHP ein eigenes Modul, fⁿr die anderen, welche den CGI Standard unterstⁿtzen, kann PHP als CGI Prozessor arbeiten. </P ><P >
So haben Sie Freiheit, PHP auf dem Betriebssystem und dem Webserver Ihrer Wahl laufen zu lassen. Weiters k÷nnen Sie je nach Vorliebe prozedural, objektorientiert, oder gemischt programmieren. Auch wenn in der derzeitigen Version von PHP nicht jedes Standard OOP Feature realisiert ist, sind viele Bibliotheken und gro▀e Applikationen (inklusive der PEAR Bibliothek) exklusiv unter Verwendung von OOP Code geschrieben worden. </P ><P >
Mit PHP sind Sie nicht auf die Ausgabe von HTML beschrΣnkt. Seine FΣhigkeiten umfassen auch das dynamische Generieren von Bildern, PDF Dateien und Flash Animationen (mittels libswf und Ming). Sie k÷nnen auch leicht jede Art von Text, wie XHTML oder irgendeine andere XML Datei ausgeben. PHP kann diese Dateien automatisch generieren und im Dateisystem speichern, anstatt diese nur auszugeben bzw. auszudrucken, und formt auch serverseitigen Zwischenspeicher Ihrer dynamischen Inhalte. </P ><P >
Vielleicht die gr÷▀te und bemerkenswerteste StΣrke von PHP ist seine Unterstⁿtzung fⁿr eine breite Masse von Datenbanken. Eine datenbankgestⁿtzte Website zu erstellen ist unglaublich einfach. Die folgenden Datenbanken werden zur Zeit unterstⁿtzt: <A NAME="AEN181" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD >Adabas D</TD ><TD >Ingres</TD ><TD >Oracle (OCI7 und OCI8)</TD ></TR ><TR ><TD >dBase</TD ><TD >InterBase</TD ><TD >Ovrimos</TD ></TR ><TR ><TD >Empress</TD ><TD >FrontBase</TD ><TD >PostgreSQL</TD ></TR ><TR ><TD >FilePro (nur Lesezugriff)</TD ><TD >mSQL</TD ><TD >Solid</TD ></TR ><TR ><TD >Hyperwave</TD ><TD >Direct MS-SQL</TD ><TD >Sybase</TD ></TR ><TR ><TD >IBM DB2</TD ><TD >MySQL</TD ><TD >Velocis</TD ></TR ><TR ><TD >Informix</TD ><TD >ODBC</TD ><TD >Unix dbm</TD ></TR ></TBODY ></TABLE ><P ></P ></BLOCKQUOTE > Wir haben auch die Erweiterung DBX zur Datenbankabstraktion, welche Ihnen die transparente Verwendung irgendeiner von dieser Erweiterung unterstⁿtzten Datenbank erlaubt. Weiters unterstⁿtzt PHP ODBC, den Open Database Connection Standard, mit welchem Sie sich zu jeder anderen diesen Weltstandard unterstⁿtzenden Datenbank verbinden k÷nnen. </P ><P >
PHP unterstⁿtzt auch die Kommunikation mit anderen Services, welche Protokolle wie LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (auf Windows) und unzΣhlige andere unterstⁿtzen. Sie k÷nnen auch einfache Netzwerk Sockets ÷ffnen, und unter Verwendung irgendeines Protokolls kommunizieren. PHP unterstⁿtzt auch WDDX (Web Distributed Data Exchange) zum Austausch komplexer Daten zwischen Programmiersprachen. Da wir gerade vom Zusammenwirken sprechen: PHP bietet auch Unterstⁿtzung zur Instantiierung von Java Objekten, und deren transparente Verwendung als PHP Objekte. Sie k÷nnen auch unsere CORBA Erweiterung verwenden, um auf entfernte Objekte zuzugreifen. </P ><P >
PHP verfⁿgt ⁿber Σu▀erst hilfreiche Textverarbeitungsfunktionen, von den regulΣren Ausdrⁿcken (POSIX erweitert oder Perl) bis zum Parsen von XML Dokumenten. Fⁿr den Zugriff und das Parsen von XML Dokumenten unterstⁿtzen wir die Standards SAX und DOM. Sie k÷nnen unsere XSLT Erweiterung verwenden, um XML Dokumente zu transformieren. </P ><P >
Wenn Sie PHP im Bereich des E-Commerce nutzen, werden Sie Funktionen fⁿr Cybercash Payment, CyberMUT, VeriSign Payflow Pro und CCVS fⁿr Ihre Onlineprogramme zur Zahlungsabwicklung zu schΣtzen lernen. </P ><P >
Wir haben viele andere interessante Erweiterungen, wie mnoGoSearch fⁿr Suchmaschinen, die IRC Gateway Funktionen, viele Komprimierungswerkzeuge (gzip, bz2), Kalenderumrechnung, ▄bersetzung... </P ><P >
Wie Sie sehen k÷nnen, reicht diese Seite nicht aus, um alle M÷glichkeiten und Vorteile von PHP aufzulisten. Lesen Sie im Abschnitt <A HREF="#installation" >Installation</A > weiter, und konsultieren Sie auch die <A HREF="#funcref" >Funktionsreferenz</A > fⁿr weitere ErlΣuterungen der einzelnen hier erwΣhnten Erweiterungen. </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="tutorial" >Kapitel 2. Kleines Tutorial</A ></H1 ><P >
Auf diesen Seiten m÷chten wir Ihnen die Grundlagen von PHP in einem kleinen Tutorial vorstellen. Dieser Text behandelt nur das Erstellen von dynamischen Web-Seiten, obwohl PHP natⁿrlich nicht nur dafⁿr geeignet ist. Weitere Informationen finden Sie im Abschnitt <A HREF="#intro-whatcando" >Was kann PHP?</A >. </P ><P >
Mit PHP erweiterte Web-Seiten werden wie normale HTML-Seiten behandelt. Sie k÷nnen sie genauso wie normale HTML-Seiten erstellen und bearbeiten. </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="tutorial.requirements" >Was brauche ich?</A ></H2 ><P >
In diesem Tutorial gehen wir davon aus, dass auf Ihrem Server die PHP-Unterstⁿtzung aktiviert ist und dass die Dateiendung <TT CLASS="filename" >.php</TT > PHP zugeordnet ist. Auf den meisten Servern ist dies die Standardeinstellung fⁿr PHP-Dateien, aber fragen Sie bitte Ihren Server-Administrator, um sicherzugehen. Wenn Ihr Server PHP unterstⁿtzt, mⁿssen Sie nichts machen. Erstellen Sie einfach Ihre <TT CLASS="filename" >.php</TT >-Dateien und legen Sie diese in Ihr Web-Verzeichnis - der Server wird sie dann fⁿr Sie parsen. Sie mⁿssen nichts kompilieren und auch keine Zusatz-Tools installieren. Stellen Sie sich diese PHP-erweiterten Dateien wie normale HTML-Seiten mit einer ganzen Familie von "magischen" Tags, die Sie verschiedenste Dinge tun lassen, vor. </P ><P >
Angenommen, Sie m÷chten Bandbreite sparen und lokal entwickeln. In diesem Fall mⁿssen Sie einen Webserver wie z.B. <A HREF="#install.apache" >Apache</A > und natⁿrlich <A HREF="http://www.php.net/downloads.php" TARGET="_top" >PHP</A > installieren. Sehr empfehlenswert ist auch die Installation einer Datenbank wie z.B. <A HREF="http://dev.mysql.com/doc/" TARGET="_top" >MySQL</A >. Sie k÷nnen diese Programm entweder eins nach dem anderen selbst installieren oder nach einem <A HREF="http://www.hotscripts.com/PHP/Software_and_Servers/Installation_Kits/" TARGET="_top" >vorkonfigurierten Paket</A > suchen, dass automatisch alle ben÷tigen Programme mit einigen wenigen Mausklicks installieren kann. Es ist sehr einfach, einen Webserver mit PHP-Support unter jedem Betriebssystem, wie Linux oder Windows, zu installieren. Unter Linux ist <A HREF="http://www.rpmfind.net/" TARGET="_top" >rpmfind</A > nⁿtzlich, um RPMs zu finden. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="tutorial.firstpage" >Ihre erste PHP-erweiterte Seite</A ></H2 ><P >
Erstellen Sie eine Datei mit dem Namen <TT CLASS="filename" >hallo.php</TT > und speichern Sie sie im Root-Verzeichnis Ihres Webservers (<VAR CLASS="varname" >DOCUMENT_ROOT</VAR >) mit dem folgenden Inhalt: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN233" ></A ><P ><B >Beispiel 2-1. Unser erstes PHP-Skript: <TT CLASS="filename" >hallo.php</TT ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><html> <head> <title>PHP-Test</title> </head> <body> <?php echo "<p>Hallo Welt</p>"; ?> </body> </html></PRE ></TD ></TR ></TABLE ><P > Benutzen Sie Ihren Browser, um die Datei ⁿber den Webserver-URL aufzurufen. Der URL muss mit "/halle.php" enden. Wenn Sie lokal entwickeln, sieht der URL z.B. so aus: <VAR CLASS="literal" >http://localhost/hallo.php</VAR > oder <VAR CLASS="literal" >http://127.0.0.1/hallo.php</VAR > - andere Adressen sind aber, abhΣngig vom Webserver, auch m÷glich. Sie k÷nnen sich auch die Direktiven <VAR CLASS="varname" >DocumentRoot</VAR > und <VAR CLASS="varname" >ServerName</VAR > in der Konfigurationsdatei Ihres Webservers fⁿr weitere Informationen anschauen (bei Apache lautet der Name dieser Datei <TT CLASS="filename" >httpd.conf</TT >). Wenn Sie alles korrekt installiert haben, wird die Datei von PHP geparst und Sie werden die folgende Ausgabe in Ihrem Browser sehen: </P ><P >
<TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><html> <head> <title>PHP-Test</title> </head> <body> <p>Hallo Welt</p> </body> </html></PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P >
Beachten Sie, dass dies nicht mit einem CGI-Skript vergleichbar ist. Die Datei muss nicht ausfⁿhrbar oder sonst irgendwie speziell sein. Stellen Sie sich die Datei wie eine normale HTML-Datei vor, die eine spezielle Menge von Tags zur Verfⁿgung stellt, mit denen Sie viele interessante Dinge machen k÷nnen. </P ><P >
Das Beispiel ist extrem einfach und natⁿrlich brauchen Sie PHP nicht, um eine Seite wie diese zu erstellen. Denn es macht nicht mehr, als <VAR CLASS="literal" >Hallo Welt</VAR > mit der <A HREF="#function.echo" ><B CLASS="function" >echo()</B ></A >-Anweisung von PHP auszugeben. </P ><P >
Wenn Sie dieses Beispiel ausprobiert haben und Sie aber keine Ausgabe erhalten haben oder zum Download aufgefordert worden sind oder die komplette Datei als Text erhalten haben, dann ist es sehr wahrscheinlich, dass auf Ihrem Server PHP nicht aktiviert ist. Fragen Sie in diesem Fall Ihren Administrator und weisen Sie ihn auf das <A HREF="#installation" >Installations-Kapitel</A > hin. Wenn Sie lokal entwickeln, lesen Sie bitte das Installations-Kapitel, um festzustellen, ob alles richtig konfiguriert wurde. Sollten Ihre Probleme nach Lesen dieses Kapitels immer noch bestehen, z÷gern Sie nicht und nutzen Sie eines der vielen <A HREF="http://www.php.net/support.php" TARGET="_top" >Support</A >-Angebote. </P ><P >
Der wichtigste Punkt im Beispiel ist, Ihnen das spezielle PHP Tag-Format zu zeigen. Im Beispiel wurde <VAR CLASS="literal" ><?php</VAR > verwendet, um den Beginn eines PHP-Tags zu kennzeichnen. Anschlie▀end folgte die PHP-Anweisung. Mit dem schlie▀enden Tag, <VAR CLASS="literal" >?></VAR >, wurde der PHP-Modus wieder verlassen. Sie k÷nnen an jeder Stelle und so oft Sie wollen, in den PHP-Modus wechseln und ihn wieder verlassen. Fⁿr weitere Details lesen Sie bitte den Abschnitt zu den <A HREF="#language.basic-syntax" >Grundlagen der Syntax</A > von PHP. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung zu Text-Editoren: </B > Es gibt eine ganze Reihe von Text-Editoren und Integrated Development Environments (IDEs), mit denen Sie Ihre PHP-Dateien erstellen, bearbeiten und managen k÷nnen. Eine Liste solcher Programme finden Sie hier: <A HREF="http://phpeditors.linuxbackup.co.uk/" TARGET="_top" >PHP Editor's List</A >. Wenn Sie einen Editor vorschlagen m÷chten, besuchen Sie bitte die genannte Seite und bitten Sie den Betreiber der Seite, dass er den Editor der Liste hinzufⁿgt. Wir empfehlen Ihnen einen Editor zu benutzen, der Syntax-Highlighting bietet. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung zu Textverarbeitungen: </B > Textverarbeitungen wie StarOffice Writer, Microsoft Word und Abiword sind keine gute Wahl, um PHP-Dateien zu bearbeiten. Wenn Sie eines dieser Programme fⁿr dieses Test-Skript nutzen m÷chten, dann mⁿssen Sie sicherstellen, dass die Datei als "Nur Text"-Datei gespeichert wird, da PHP sonst das Skript nicht lesen und nicht ausfⁿhren kann. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung zu Notepad, dem Windows-Standard-Editor: </B > Wenn Sie Ihre PHP-Skripte mit Notepad schreiben, mⁿssen Sie sicherstellen, dass Ihre Dateien mit der Endung <TT CLASS="filename" >.php</TT > gespeichert werden. (Notepad fⁿgt die Endung <TT CLASS="filename" >.txt</TT > automatisch an den Dateinamen an, wenn Sie das nicht mit einem der folgenden Schritte verhindern.) Wenn Sie die Datei speichern und einen Namen fⁿr die Datei eingeben sollen, dann setzen Sie den Dateinamen in Anfⁿhrungszeichen (z.B. "<TT CLASS="filename" >hallo.php</TT >"). Alternativ k÷nnen Sie auch im "Datei speichern"-Fenster in der Drop-Down-Liste "Dateityp" die Einstellung auf "Alle Dateien" Σndern. Sie k÷nnen dann den Dateinamen ohne Anfⁿhrungszeichen eingeben. </P ></BLOCKQUOTE ></DIV ><P > Nachdem Sie jetzt erfolgreich ein einfaches, funktionierendes PHP-Skript geschrieben haben, wird es Zeit, das berⁿhmteste PHP-Skript zu schreiben. Rufen Sie die Funktion <A HREF="#function.phpinfo" ><B CLASS="function" >phpinfo()</B ></A > auf und Sie bekommen viele nⁿtzliche Informationen ⁿber Ihr System und Ihre Installation wie z.B. die verfⁿgbaren <A HREF="#language.variables.predefined" >vordefinierten Variablen</A >, die geladenen PHP-Module und die <A HREF="#configuration" >Konfigurations</A >-Einstellungen. Nehmen Sie sich etwas Zeit und schauen Sie sich diese wichtigen Informationen an. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="tutorial.useful" >Nⁿtzliches</A ></H2 ><P >
Kommen wir nun zu einem etwas nⁿtzlicheren Beispiel. Wir wollen prⁿfen, welchen Browser die Person, die die Seite besucht, benutzt. Um das zu tun, prⁿfen wir den "user agent"-String, den der Browser als Teil seiner HTTP-Anforderung sendet. Diese Information ist in einer <A HREF="#language.variables" >Variablen</A > abgelegt. In PHP beginnen Variablen immer mit einem Dollar-Zeichen. Die Variable, die uns jetzt interessiert, ist <VAR CLASS="varname" >$_SERVER["HTTP_USER_AGENT"]</VAR >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >PHP Autoglobals-Anmerkung: </B > <A HREF="#reserved.variables.server" >$_SERVER</A > ist eine speziell reservierte PHP-Variable, die alle Informationen ⁿber den Webserver enthΣlt. Diese Variable wird auch als auto- oder superglobal bezeichnet. Mehr Informationen darⁿber Sie auf der Manual-Seite ⁿber <A HREF="#language.variables.superglobals" >Autoglobals</A >. Diese speziellen Variablen wurden in PHP <A HREF="http://www.php.net/release_4_1_0.php" TARGET="_top" >4.1.0</A > eingefⁿhrt. Vorher wurden stattdessen die Σlteren <VAR CLASS="varname" >$HTTP_*_VARS</VAR >-Arrays benutzt, also z.B. <VAR CLASS="varname" >$HTTP_SERVER_VARS</VAR >. Auch wenn diese Variablen nicht mehr genutzt werden sollen - sie existieren weiterhin. (Beachten Sie auch die Seite <A HREF="#tutorial.oldcode" >Alten Code mit neuen PHP-Versionen benutzen</A >.) </P ></BLOCKQUOTE ></DIV ><P >
Um die Variable auszugeben, schreiben wir einfach: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN289" ></A ><P ><B >Beispiel 2-2. Variable ausgeben (Array-Element)</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo $_SERVER["HTTP_USER_AGENT"]; ?></PRE ></TD ></TR ></TABLE ><P >
Die Ausgabe dieses Beispiel k÷nnte so aussehen: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" >Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P >
Es gibt viele <A HREF="#language.types" >Typen</A > von Variablen. Im obigen Beispiel haben wir ein <A HREF="#language.types.array" >Array</A >-Element ausgegeben. Arrays k÷nnen sehr nⁿtzlich sein. </P ><P >
<VAR CLASS="varname" >$_SERVER</VAR > ist nur eine von vielen Variablen, die Ihnen automatisch von PHP zur Verfⁿgung gestellt werden. Eine Liste finden Sie auf der Seite <A HREF="#reserved.variables" >Reservierte Variablen</A > im Manual. Eine vollstΣndige Liste k÷nnen Sie auch bekommen, wenn Sie eine Datei wie die folgende erstellen: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN301" ></A ><P ><B >Beispiel 2-3. Alle vordefinierten Variablen mit <A HREF="#function.phpinfo" ><B CLASS="function" >phpinfo()</B ></A > ausgeben</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php phpinfo(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Wenn Sie diese Datei in Ihrem Browser ÷ffnen, sehen Sie eine Seite mit vielen Informationen ⁿber PHP zusammen mit einer Liste aller fⁿr Sie verfⁿgbaren Variablen. </P ><P >
Sie k÷nnen mehrere PHP-Anweisungen innerhalb eines PHP-Tags platzieren und so kleine Code-Bl÷cke schreiben, die mehr als nur eine Ausgabe mit <A HREF="#function.echo" ><B CLASS="function" >echo()</B ></A > enthalten. Wenn wir zum Beispiel prⁿfen m÷chten, ob es sich beim Browser des Besuchers um den Internet Explorer handelt, k÷nnen wir folgenden Code benutzen: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN309" ></A ><P ><B >Beispiel 2-4. Beispiel, das <A HREF="#control-structures" >Kontrollstrukturen</A > und <A HREF="#functions" >Funktionen</A > benutzt</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { echo "Sie benutzen Internet Explorer<br />"; } ?></PRE ></TD ></TR ></TABLE ><P >
Die Ausgabe dieses Skripte k÷nnte so aussehen: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" >Sie benutzen Internet Explorer<br /></PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P >
Hier haben wir Ihnen eine ganze Reihe von neuen Konzepten vorgestellt. Wir haben hier zuerst eine <A HREF="#control-structures.if" >if</A >-Anweisung. Wenn Sie mit der Grundlagen-Syntax von der Programmiersprache C vertraut sind, sollte Ihnen dies logisch erscheinen. Wenn Sie C oder eine andere Sprache, die die obige Syntax benutzt, nicht gut genug kennen, dann sollten Sie sich ein Buch mit einer PHP-Einfⁿhrung besorgen und die ersten Kapitel lesen. Sie k÷nnen natⁿrlich auch in die <A HREF="#langref" >Sprachreferenz</A > des Manuals schauen. Eine Liste von PHP-Bⁿchern finden Sie unter <A HREF="http://www.php.net/books.php" TARGET="_top" >http://www.php.net/books.php</A >. </P ><P >
Das zweite hier vorgestellte Konzept ist der Aufruf der Funktion <A HREF="#function.strstr" ><B CLASS="function" >strstr()</B ></A >. <VAR CLASS="literal" >strstr</VAR > ist eine in PHP eingebaute Funktion, die nach einem String in einem anderen String sucht. In diesem Fall suchen wir nach <VAR CLASS="literal" >"MSIE"</VAR > in <VAR CLASS="varname" >$_SERVER["HTTP_USER_AGENT"]</VAR >. Wenn der String gefunden wird, gibt die Funktion <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wird er nicht gefunden, dann wird <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgegeben. Wenn <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿckgeben wird, wird die <A HREF="#control-structures.if" >if</A >-Anweisung zu <TT CLASS="constant" ><B >TRUE</B ></TT > ausgewertet und der Code innerhalb der geschweiften Klammern wird ausgefⁿhrt. Wenn aber <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgegeben wird, wird der Code innerhalb der Klammern nicht ausgefⁿhrt. Probieren Sie weitere Σhnliche Beispiele mit <A HREF="#control-structures.if" >if</A >, <A HREF="#control-structures.else" >else</A > und anderen Funktionen wie <A HREF="#function.strtoupper" ><B CLASS="function" >strtoupper()</B ></A > oder <A HREF="#function.strlen" ><B CLASS="function" >strlen()</B ></A >. Jede dieser Manual-Seiten enthΣlt weitere Beispiele. </P ><P >
Wir k÷nnen jetzt einen Schritt weitergehen und sehen, wie Sie innerhalb eines PHP-Blocks den PHP-Modus verlassen und wieder in ihn hinein gelangen k÷nnen: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN337" ></A ><P ><B >Beispiel 2-5. HTML- und PHP-Modus vermischt</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { ?> <h3>strstr muss true zurⁿckgegeben haben</h3> <center><b>Sie benutzen Internet Explorer</b></center> <?php } else { ?> <h3>strstr muss false zurⁿckgegeben haben</h3> <center><b>Sie benutzen nicht Internet Explorer</b></center> <?php } ?></PRE ></TD ></TR ></TABLE ><P >
Die Ausgabe dieses Skripts k÷nnte so aussehen: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><h3>strstr muss true zurⁿckgegeben haben</h3> <center><b>Sie benutzen Internet Explorer</b></center></PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P >
Anstatt die PHP <VAR CLASS="literal" >echo</VAR >-Anweisung fⁿr die Ausgabe zu benutzen, haben wir den PHP-Modus verlassen und normales HTML verwendet. Der wichtige Punkt hierbei ist, dass der logische Ablauf des Skripts dadurch nicht gest÷rt wird. Nur einer der beiden HTML-Bl÷cke wird ausgegeben - abhΣngig davon, ob <A HREF="#function.strstr" ><B CLASS="function" >strstr()</B ></A > <TT CLASS="constant" ><B >TRUE</B ></TT > oder <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgibt bzw. ob der String <VAR CLASS="literal" >MSIE</VAR > gefunden wird oder nicht. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="tutorial.forms" >Formulare verarbeiten</A ></H2 ><P >
Eine der mΣchtigsten Funktionen von PHP ist die Art, wie HTML-Formulare verarbeitet werden. Sie sollten wissen, dass jedes Element eines Formulars automatisch in Ihren PHP-Skripts verfⁿgbar ist. Bitte lesen Sie die Seite <A HREF="#language.variables.external" >Variablen au▀erhalb von PHP</A > fⁿr weitere Informationen und Beispiele ⁿber das Benutzen von Formularen mit PHP. Hier ist ein Beispiel-HTML-Formular: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN353" ></A ><P ><B >Beispiel 2-6. Ein einfaches HTML-Formular</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><form action="action.php" method="POST"> Ihr Name: <input type="text" name="name" /> Ihr Alter: <input type="text" name="alter" /> <input type="submit"> </form></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
An diesem Formular ist nichts Besonderes. Es ist ein normales HTML-Formular ohne irgendwelche speziellen Tags. Wenn der Benutzer das Formular ausfⁿllt und den Submit-Button anklickt, wird die Seite <TT CLASS="filename" >action.php</TT > aufgerufen. Diese Datei k÷nnte so aussehen: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN359" ></A ><P ><B >Beispiel 2-7. Daten des Formulars ausgeben</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Hallo <?php echo $_POST["name"]; ?>. Sie sind <?php echo $_POST["alter"]; ?> Jahre alt.</PRE ></TD ></TR ></TABLE ><P >
Die Ausgabe des Skripts k÷nnte dann so aussehen: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" >Hallo Joe. Sie sind 22 Jahre alt.</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P >
Es sollte einfach zu verstehen sein, was hier passiert. Die Variablen <VAR CLASS="varname" >$_POST["name"]</VAR > und <VAR CLASS="varname" >$_POST["alter"]</VAR > werden fⁿr Sie automatisch von PHP gesetzt. Weiter oben haben wir das autoglobale Array <VAR CLASS="varname" >$_SERVER</VAR > eingefⁿhrt, jetzt benutzen wir hier das - ebenfalls autoglobale - Array <A HREF="#reserved.variables.post" >$_POST</A >, dass alle POST-Daten enthΣlt. Beachten Sie, dass die im Formular verwendete <SPAN CLASS="emphasis" ><I CLASS="emphasis" >Methode</I ></SPAN > POST ist. HΣtten wir <SPAN CLASS="emphasis" ><I CLASS="emphasis" >GET</I ></SPAN > verwendet, dann wΣren die Daten unseres Formulars stattdessen im autoglobalen Array <A HREF="#reserved.variables.get" >$_GET</A > verfⁿgbar. Sie k÷nnen auch das autoglobale Array <A HREF="#reserved.variables.request" >$_REQUEST</A > benutzen, wenn die Quelle der Daten keine Rolle spielt. Dieses Array enthΣlt die GET-, POST-, COOKIE- und FILE-Daten. Vgl. auch die <A HREF="#function.import-request-variables" ><B CLASS="function" >import_request_variables()</B ></A >-Funktion. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="tutorial.oldcode" >Alten Code mit neuen PHP-Versionen benutzen</A ></H2 ><P >
Dadurch dass PHP eine immer beliebtere Skriptsprache ist, gibt es immer mehr Quellen, die Ihnen Code-Listings zur Verfⁿgung zu stellen. Die PHP-Entwickler haben versucht, den gr÷▀ten Teil der Sprache abwΣrtskompatibel zu halten. Das bedeutet, dass ein Skript, das fⁿr eine Σltere PHP-Version geschrieben wurde, (im Idealfall) ohne ─nderungen auch unter einer neueren PHP-Version lΣuft. In der Praxis sind aber meist einige ─nderungen n÷tig. </P ><P >
Zwei der wichtigsten aktuellen ─nderungen, die alten Code betreffen, sind: <P ></P ><UL ><LI ><P >
Die Missbilligung der alten <VAR CLASS="varname" >$HTTP_*_VARS</VAR >-Arrays (die global gemacht werden mussten, wenn man sie innerhalb einer Funktion nutzen wollte). In PHP <A HREF="http://www.php.net/release_4_1_0.php" TARGET="_top" >4.1.0</A > wurden die folgenden <A HREF="#language.variables.superglobals" >autoglobalen Arrays</A > eingefⁿhrt: <VAR CLASS="varname" >$_GET</VAR >, <VAR CLASS="varname" >$_POST</VAR >, <VAR CLASS="varname" >$_COOKIE</VAR >, <VAR CLASS="varname" >$_SERVER</VAR >, <VAR CLASS="varname" >$_ENV</VAR >, <VAR CLASS="varname" >$_REQUEST</VAR > und <VAR CLASS="varname" >$_SESSION</VAR >. Die Σlteren <VAR CLASS="varname" >$HTTP_*_VARS</VAR >-Arrays, wie <VAR CLASS="varname" >$HTTP_POST_VARS</VAR >, existierten bereits seit PHP 3 und sind auch immer noch verfⁿgbar. </P ></LI ><LI ><P >
Externe Variablen werden standardmΣ▀ig nicht mehr im globalen Namensraum registriert. Mit anderen Worten, seit PHP <A HREF="http://www.php.net/release_4_2_0.php" TARGET="_top" >4.2.0</A > ist <SPAN CLASS="emphasis" ><I CLASS="emphasis" >off</I ></SPAN > die Standard-Einstellung fⁿr die PHP-Direktive <A HREF="#ini.register-globals" >register_globals</A >. Die empfohlene Methode, auf diese Werte zuzugreifen, ist, die oben genannten autoglobalen Arrays zu verwenden. ─ltere Skripte, Bⁿcher und Tutorials gehen eventuell davon aus, dass diese Einstellung auf <SPAN CLASS="emphasis" ><I CLASS="emphasis" >on</I ></SPAN > steht. WΣre die Einstellung on, wΣre die Variable <VAR CLASS="varname" >$id</VAR > aus der URL <VAR CLASS="literal" >http://www.example.com/foo.php?id=42</VAR > verfⁿgbar. UnabhΣngig davon, ob on oder off, ist immer <VAR CLASS="varname" >$_GET['id']</VAR > verfⁿgbar. </P ></LI ></UL > Fⁿr weitere Details ⁿber diese ─nderungen siehe die Seite ⁿber <A HREF="#language.variables.predefined" >vordefinierte Variablen</A > und die Links dort. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="tutorial.whatsnext" >Und weiter?</A ></H2 ><P >
Mit dem Wissen, das Sie jetzt haben, sollte es Ihnen m÷glich sein, das meiste aus diesem Manual und die vielen Beispiel-Skripte in den Beispiel-Archiven zu verstehen. Sie k÷nnen weitere auf den php.net-Seiten verfⁿgbare Beispiele auf der folgenden Seite finden: <A HREF="http://www.php.net/links.php" TARGET="_top" >http://www.php.net/links.php</A >. </P ></DIV ></DIV ></DIV ><DIV CLASS="PART" ><A NAME="install" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >II. Installation und Konfiguration</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT >3. <A HREF="#install.general" >General Installation Considerations</A ></DT ><DT >4. <A HREF="#install.macosx" >Installation on Mac OS X</A ></DT ><DT >5. <A HREF="#install.unix" >Installation on Unix systems</A ></DT ><DT >6. <A HREF="#install.windows" >Installation on Windows systems</A ></DT ><DT >7. <A HREF="#install.problems" >Problems?</A ></DT ><DT >8. <A HREF="#configuration" >Runtime Configuration</A ></DT ></DL ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="install.general" >Kapitel 3. General Installation Considerations</A ></H1 ><P >
Before starting the installation, first you need to know what do you want to use PHP for. There are three main fields you can use PHP, as described in the <A HREF="#intro-whatcando" >What can PHP do?</A > section: <P ></P ><UL ><LI ><P >Server-side scripting</P ></LI ><LI ><P >Command line scripting</P ></LI ><LI ><P >Client-side GUI applications</P ></LI ></UL > </P ><P >
For the first and most common form, you need three things: PHP itself, a web server and a web browser. You probably already have a web browser, and depending on your operating system setup, you may also have a web server (e.g. Apache on Linux and MacOS X; IIS on Windows). You may also rent webspace at a company. This way, you don't need to set up anything on your own, only write your PHP scripts, upload it to the server you rent, and see the results in your browser. </P ><P >
While setting up the server and PHP on your own, you have two choices for the method of connecting PHP to the server. For many servers PHP has a direct module interface (also called SAPI). These servers include Apache, Microsoft Internet Information Server, Netscape and iPlanet servers. Many other servers have support for ISAPI, the Microsoft module interface (OmniHTTPd for example). If PHP has no module support for your web server, you can always use it as a CGI or FastCGI processor. This means you set up your server to use the CGI executable of PHP to process all PHP file requests on the server. </P ><P >
If you are also interested to use PHP for command line scripting (e.g. write scripts autogenerating some images for you offline, or processing text files depending on some arguments you pass to them), you always need the command line executable. For more information, read the section about <A HREF="#features.commandline" >writing command line PHP applications</A >. In this case, you need no server and no browser. </P ><P >
With PHP you can also write desktop GUI applications using the PHP-GTK extension. This is a completely different approach than writing web pages, as you do not output any HTML, but manage windows and objects within them. For more information about PHP-GTK, please <A HREF="http://gtk.php.net/" TARGET="_top" >visit the site dedicated to this extension</A >. PHP-GTK is not included in the official PHP distribution. </P ><P >
From now on, this section deals with setting up PHP for web servers on Unix and Windows with server module interfaces and CGI executables. You will also find information on the command line executable in the following sections. </P ><P >
PHP source code and binary distributions for Windows can be found at <A HREF="http://www.php.net/downloads.php" TARGET="_top" >http://www.php.net/downloads.php</A >. We recommend you to choose a <A HREF="http://www.php.net/mirrors.php" TARGET="_top" >mirror</A > nearest to you for downloading the distributions. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="install.macosx" >Kapitel 4. Installation on Mac OS X</A ></H1 ><P >
This section contains notes and hints specific to installing PHP on Mac OS X. There are two slightly different versions of Mac OS X, Client and Server, our manual deals with installing PHP on both systems. Note that PHP is not available for MacOS 9 and earlier versions. </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.macosx.packages" >Using Packages</A ></H2 ><P >
There are a few pre-packaged and pre-compiled versions of PHP for Mac OS X. This can help in setting up a standard configuration, but if you need to have a different set of features (such as a secure server, or a different database driver), you may need to build PHP and/or your web server yourself. If you are unfamiliar with building and compiling your own software, it's worth checking whether somebody has already built a packaged version of PHP with the features you need. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.macosx.server" >Compiling for OS X Server</A ></H2 ><DIV CLASS="formalpara" ><P ><A NAME="install.macosx.server.compile" ></A ><B >Mac OS X Server install. </B > <P ></P ><OL TYPE="1" ><LI ><P >Get the latest distributions of Apache and PHP.</P ></LI ><LI ><P >
Untar them, and run the <B CLASS="command" >configure</B > program on Apache like so. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >./configure --exec-prefix=/usr \ --localstatedir=/var \ --mandir=/usr/share/man \ --libexecdir=/System/Library/Apache/Modules \ --iconsdir=/System/Library/Apache/Icons \ --includedir=/System/Library/Frameworks/Apache.framework/Versions/1.3/Headers \ --enable-shared=max \ --enable-module=most \ --target=apache</PRE ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
If you want the compiler to do some optimization, you may also want to add this line: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >setenv OPTIM=-O2</PRE ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
Next, go to the PHP 4 source directory and configure it. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >./configure --prefix=/usr \ --sysconfdir=/etc \ --localstatedir=/var \ --mandir=/usr/share/man \ --with-xml \ --with-apache=/src/apache_1.3.12</PRE ></TD ></TR ></TABLE > If you have any other additions (MySQL, GD, etc.), be sure to add them here. For the <VAR CLASS="option" >--with-apache</VAR > string, put in the path to your apache source directory, for example <TT CLASS="filename" >/src/apache_1.3.12</TT >. </P ></LI ><LI ><P >
Type <B CLASS="command" >make</B > and <B CLASS="command" >make install</B >. This will add a directory to your Apache source directory under <TT CLASS="filename" >src/modules/php4</TT >. </P ></LI ><LI ><P >
Now, reconfigure Apache to build in PHP 4. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >./configure --exec-prefix=/usr \ --localstatedir=/var \ --mandir=/usr/share/man \ --libexecdir=/System/Library/Apache/Modules \ --iconsdir=/System/Library/Apache/Icons \ --includedir=/System/Library/Frameworks/Apache.framework/Versions/1.3/Headers \ --enable-shared=max \ --enable-module=most \ --target=apache \ --activate-module=src/modules/php4/libphp4.a</PRE ></TD ></TR ></TABLE > You may get a message telling you that <TT CLASS="filename" >libmodphp4.a</TT > is out of date. If so, go to the <TT CLASS="filename" >src/modules/php4</TT > directory inside your Apache source directory and run this command: <B CLASS="command" >ranlib libmodphp4.a</B >. Then go back to the root of the Apache source directory and run the above <B CLASS="command" >configure</B > command again. That'll bring the link table up to date. Run <B CLASS="command" >make</B > and <B CLASS="command" >make install</B > again. </P ></LI ><LI ><P >
Copy and rename the <TT CLASS="filename" >php.ini-dist</TT > file to your <TT CLASS="filename" >bin</TT > directory from your PHP 4 source directory: <KBD CLASS="userinput" >cp php.ini-dist /usr/local/bin/php.ini</KBD > or (if your don't have a local directory) <KBD CLASS="userinput" >cp php.ini-dist /usr/bin/php.ini</KBD >. </P ></LI ></OL > </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.macosx.client" >Compiling for MacOS X Client</A ></H2 ><P >
The following instructions will help you install a PHP module for the Apache web server included in MacOS X. This version includes support for the MySQL and PostgreSQL databases. These instructions are graciously provided by <A HREF="http://www.entropy.ch/software/macosx/" TARGET="_top" >Marc Liyanage</A >. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Be careful when you do this, you could screw up your Apache web server! </P ></TD ></TR ></TABLE ></DIV ><P >
Do this to install: <P ></P ><OL TYPE="1" ><LI ><P >
Open a terminal window. </P ></LI ><LI ><P >
Type <KBD CLASS="userinput" >wget http://www.diax.ch/users/liyanage/software/macosx/libphp4.so.gz</KBD >, wait for the download to finish. </P ></LI ><LI ><P >
Type <KBD CLASS="userinput" >gunzip libphp4.so.gz</KBD >. </P ></LI ><LI ><P >
Type <KBD CLASS="userinput" >sudo apxs -i -a -n php4 libphp4.so</KBD > </P ></LI ><LI ><P >
Now type <KBD CLASS="userinput" >sudo open -a TextEdit /etc/httpd/httpd.conf</KBD >. TextEdit will open with the web server configuration file. Locate these two lines towards the end of the file: (Use the Find command) <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache" >#AddType application/x-httpd-php .php #AddType application/x-httpd-php-source .phps</PRE ></TD ></TR ></TABLE > Remove the two hash marks (<VAR CLASS="literal" >#</VAR >), then save the file and quit TextEdit. </P ></LI ><LI ><P >
Finally, type <KBD CLASS="userinput" >sudo apachectl graceful</KBD > to restart the web server. </P ></LI ></OL > </P ><P >
PHP should now be up and running. You can test it by dropping a file into your <TT CLASS="filename" >Sites</TT > folder which is called <TT CLASS="filename" >test.php</TT >. Into that file, write this line: <VAR CLASS="literal" ><?php phpinfo() ?></VAR >. </P ><P >
Now open up <VAR CLASS="literal" >127.0.0.1/~your_username/test.php</VAR > in your web browser. You should see a status table with information about the PHP module. </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="install.unix" >Kapitel 5. Installation on Unix systems</A ></H1 ><P >
This section will guide you through the general configuration and installation of PHP on Unix systems. Be sure to investigate any sections specific to your platform or web server before you begin the process. </P ><P >
As our manual outlines in the <A HREF="#install.general" >General Installation Considerations</A > section, we are mainly dealing with web centric setups of PHP in this section, although we will cover setting up PHP for command line usage as well. </P ><P > There are several ways to install PHP for the Unix platform, either with a compile and configure process, or through various pre-packaged methods. This documentation is mainly focused around the process of compiling and configuring PHP. Many Unix like systems have some sort of package installation system. This can assist in setting up a standard configuration, but if you need to have a different set of features (such as a secure server, or a different database driver), you may need to build PHP and/or your webserver. If you are unfamiliar with building and compiling your own software, it is worth checking to see whether somebody has already built a packaged version of PHP with the features you need. </P ><P >
Prerequisite knowledge and software for compiling: <P ></P ><UL ><LI ><P >
Basic Unix skills (being able to operate "make" and a C compiler) </P ></LI ><LI ><P >
An ANSI C compiler </P ></LI ><LI ><P >
flex </P ></LI ><LI ><P >
bison </P ></LI ><LI ><P >
A web server </P ></LI ><LI ><P >
Any module specific components (such as gd, pdf libs, etc.) </P ></LI ></UL > </P ><P >
The initial PHP setup and configuration process is controlled by the use of the commandline options of the <TT CLASS="filename" >configure</TT > script. Our manual documents the different options separately. You will find the <A HREF="#configure" >core options in the appendix</A >, while the different extension specific options are descibed on the reference pages. </P ><P >
When PHP is configured, you are ready to build the module and/or executables. The command <B CLASS="command" >make</B > should take care of this. If it fails and you can't figure out why, see the <A HREF="#install.problems" >Problems section</A >. </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.gentoo" >Gentoo installation notes</A ></H2 ><P >
This section contains notes and hints specific to installing PHP on <A HREF="http://www.gentoo.org/" TARGET="_top" >Gentoo Linux</A >. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.gentoo.portage" >Using Portage (emerge)</A ></H3 ><P >
While you can just download the PHP source and compile it youself, using Gentoo's packaging system is the simplest and cleanest method of installing PHP. If you are not familiar with building software on Linux, this is the way to go. </P ><P >
If you have built your Gentoo system so far, you are probably used to Portage already. Installing Apache and PHP is no different than the other system tools. </P ><P >
The first decision you need to make is whether you want to install Apache 1.3.x or Apache 2.x. While both can be used with PHP, the steps given bellow will use Apache 1.3.x. Another thing to consider is whether your local Portage tree is up to date. If you have not updated it recently, you need to run <B CLASS="command" >emerge sync</B > before anything else. This way, you will be using the most recent stable version of Apache and PHP. </P ><P >
Now that everything is in place, you can use the following example to install Apache and PHP: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="install.unix.gentoo.portage.example" ></A ><P ><B >Beispiel 5-1. Gentoo Install Example with Apache 1.3</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" ># emerge \<apache-2 # USE="-*" emerge php mod_php # ebuild /var/db/pkg/dev-php/mod_php-<your PHP version>/mod_php-<your PHP version>.ebuild config # nano /etc/conf.d/apache Add "-D PHP4" to APACHE_OPTS # rc-update add apache default # /etc/init.d/apache start</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
You can read more about emerge in the excellent <A HREF="http://www.gentoo.org/doc/en/portage-manual.xml" TARGET="_top" >Portage Manual</A > provided on the Gentoo website. </P ><P >
If you need to use Apache 2, you can simply use <B CLASS="command" >emerge apache</B > in the last example. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.gentoo.config" >Better control on configuration</A ></H3 ><P >
In the last section, PHP was emerged without any activated modules. As of this writing, the only module activated by default with Portage is XML which is needed by <A HREF="http://pear.php.net/" TARGET="_top" >PEAR</A >. This may not be what you want and you will soon discover that you need more activated modules, like MySQL, gettext, GD, etc. </P ><P >
When you compile PHP from source yourself, you need to activate modules via the <B CLASS="command" >configure</B > command. With Gentoo, you can simply provide USE flags which will be passed to the configure script automatically. To see which USE flags to use with emerge, you can try: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="install.unix.gentoo.config.example" ></A ><P ><B >Beispiel 5-2. Getting the list of valid USE flags</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" ># USE="-*" emerge -pv php [ebuild N ] dev-php/php-4.3.6-r1 -X -berkdb -crypt -curl -debug -doc -fdftk -firebird -flash -freetds -gd -gd-external -gdbm -gmp -hardenedphp -imap -informix -ipv6 -java -jpeg -kerberos -ldap -mcal -memlimit -mssql -mysql -ncurses -nls -oci8 -odbc -pam -pdflib -png -postgres -qt -readline -snmp -spell -ssl -tiff -truetype -xml2 -yaz 3,876 kB</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
As you can see from the last output, PHP considers a lot of USE flags. Look at them closely and choose what you need. If you choose a flag and you do not have the proper librairies, Portage will compile them for you. It is a good idea to use <B CLASS="command" >emerge -pv</B > again to see what Portage will compile in accordance to your USE flags. As an example, if you do not have X installed and you choose to include X in the USE flags, Portage will compile X prior to PHP, which can take a couple of hours. </P ><P >
If you choose to compile PHP with MySQL, cURL and GD support, the command will look something like this: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="install.unix.gentoo.config.example2" ></A ><P ><B >Beispiel 5-3. Install PHP with USE flags</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" ># USE="-* curl mysql gd" emerge php mod_php</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
As in the last example, do not forget to emerge php as well as mod_php. php is responsible for the command line version of PHP as mod_php is for the Apache module version of PHP. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.gentoo.faq" >Common Problems</A ></H3 ><P ></P ><UL ><LI ><P >
If you see the PHP source instead of the result the script should produce, you have probably forgot to edit <TT CLASS="filename" >/etc/conf.d/apache</TT >. Apache needs to be started with the -D PHP4 flag. To see if the flag is present, you should be able to see it when using <B CLASS="command" >ps ax | grep apache</B > while Apache is running. </P ></LI ><LI ><P >
Due to slotting problems, you might end up with more than one version of PHP installed on your system. If this is the case, you need to unmerge the old versions manually by using <B CLASS="command" >emerge unmerge mod_php-<old version></B >. </P ></LI ><LI ><P >
If you cannot emerge PHP because of Java, try putting <B CLASS="command" >-*</B > in front of your USE flags like in the above examples. </P ></LI ><LI ><P >
If you are having problems configuring Apache and PHP, you can always search the <A HREF="http://forums.gentoo.org/" TARGET="_top" >Gentoo Forums</A >. Try searching with the keywords "Apache PHP". </P ></LI ></UL ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.hpux" >HP-UX specific installation notes</A ></H2 ><P >
This section contains notes and hints specific to installing PHP on HP-UX systems. (Contributed by paul_mckay at clearwater-it dot co dot uk). </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > These tips were written for PHP 4.0.4 and Apache 1.3.9. </P ></BLOCKQUOTE ></DIV ><P >
<P ></P ><OL TYPE="1" ><LI ><P >
You need gzip, download a binary distribution from <TT CLASS="filename" >http://hpux.connect.org.uk/ftp/hpux/Gnu/gzip-1.2.4a/gzip-1.2.4a-sd-10.20.depot.Z</TT > uncompress the file and install using swinstall. </P ></LI ><LI ><P >
You need gcc, download a binary distribution from <TT CLASS="filename" >http://gatekeep.cs.utah.edu/ftp/hpux/Gnu/gcc-2.95.2/gcc-2.95.2-sd-10.20.depot.gz</TT >. uncompress this file and install gcc using swinstall. </P ></LI ><LI ><P >
You need the GNU binutils, you can download a binary distribution from <TT CLASS="filename" >http://hpux.connect.org.uk/ftp/hpux/Gnu/binutils-2.9.1/binutils-2.9.1-sd-10.20.depot.gz</TT >. uncompress this file and install binutils using swinstall. </P ></LI ><LI ><P >
You now need bison, you can download a binary distribution from <TT CLASS="filename" >http://hpux.connect.org.uk/ftp/hpux/Gnu/bison-1.28/bison-1.28-sd-10.20.depot.gz</TT >, install as above. </P ></LI ><LI ><P >
You now need flex, you need to download the source from one of the http://www.gnu.org mirrors. It is in the non-gnu directory of the ftp site. Download the file, <B CLASS="command" >gunzip</B >, then <B CLASS="command" >tar -xvf</B > it. Go into the newly created flex directory and run <B CLASS="command" >./configure</B >, followed by <B CLASS="command" >make</B >, and then <B CLASS="command" >make install</B >. </P ><P >
If you have errors here, it's probably because gcc etc. are not in your PATH so add them to your PATH. </P ></LI ><LI ><P >
Download the PHP and apache sources. </P ></LI ><LI ><P >
<B CLASS="command" >gunzip</B > and <B CLASS="command" >tar -xvf</B > them. We need to hack a couple of files so that they can compile OK. </P ></LI ><LI ><P >
Firstly the configure file needs to be hacked because it seems to lose track of the fact that you are a hpux machine, there will be a better way of doing this but a cheap and cheerful hack is to put <VAR CLASS="literal" >lt_target=hpux10.20</VAR > on line 47286 of the configure script. </P ></LI ><LI ><P >
Next, the Apache GuessOS file needs to be hacked. Under <TT CLASS="filename" >apache_1.3.9/src/helpers</TT > change line 89 from <VAR CLASS="literal" >echo "hp${HPUXMACH}-hpux${HPUXVER}"; exit 0</VAR > to: <VAR CLASS="literal" >echo "hp${HPUXMACH}-hp-hpux${HPUXVER}"; exit 0</VAR > </P ></LI ><LI ><P >
You cannot install PHP as a shared object under HP-UX so you must compile it as a static, just follow the instructions at the Apache page. </P ></LI ><LI ><P >
PHP and Apache should have compiled OK, but Apache won't start. you need to create a new user for Apache, e.g. www, or apache. You then change lines 252 and 253 of the <TT CLASS="filename" >conf/httpd.conf</TT > in Apache so that instead of </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >User nobody Group nogroup</PRE ></TD ></TR ></TABLE ><P >
you have something like </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >User www Group sys</PRE ></TD ></TR ></TABLE ><P >
This is because you can't run Apache as nobody under hp-ux. Apache and PHP should then work. </P ></LI ></OL > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.openbsd" >OpenBSD installation notes</A ></H2 ><P >
This section contains notes and hints specific to installing PHP on <A HREF="http://www.openbsd.org/" TARGET="_top" >OpenBSD 3.4</A >. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.openbsd.packages" >Using Binary Packages</A ></H3 ><P >
Using binary packages to install PHP on OpenBSD is the recommended and simplest method. The core package has been separated from the various modules, and each can be installed and removed independently from the others. The files you need can be found on your OpenBSD CD or on the FTP site. </P ><P >
The main package you need to install is <TT CLASS="filename" >php4-core-4.3.3.tgz</TT >, which contains the basic engine (plus gettext and iconv). Next, take a look at the module packages, such as <TT CLASS="filename" >php4-mysql-4.3.3.tgz</TT > or <TT CLASS="filename" >php4-imap-4.3.3.tgz</TT >. You need to use the <B CLASS="command" >phpxs</B > command to activate and deactivate these modules in your <TT CLASS="filename" >php.ini</TT >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="install.unix.openbsd.ports.example" ></A ><P ><B >Beispiel 5-4. OpenBSD Package Install Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" ># pkg_add php4-core-4.3.3.tgz # /usr/local/sbin/phpxs -s # cp /usr/local/share/doc/php4/php.ini-recommended /var/www/conf/php.ini (add in mysql) # pkg_add php4-mysql-4.3.3.tgz # /usr/local/sbin/phpxs -a mysql (add in imap) # pkg_add php4-imap-4.3.3.tgz # /usr/local/sbin/phpxs -a imap (remove mysql as a test) # pkg_delete php4-mysql-4.3.3 # /usr/local/sbin/phpxs -r mysql (install the PEAR libraries) # pkg_add php4-pear-4.3.3.tgz</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Read the <A HREF="http://www.openbsd.org/cgi-bin/man.cgi?query=packages" TARGET="_top" >packages(7)</A > manual page for more information about binary packages on OpenBSD. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.openbsd.ports" >Using Ports</A ></H3 ><P >
You can also compile up PHP from source using the <A HREF="http://www.openbsd.org/ports.html" TARGET="_top" >ports tree</A >. However, this is only recommended for users familiar with OpenBSD. The PHP 4 port is split into two sub-directories: core and extensions. The extensions directory generates sub-packages for all of the supported PHP modules. If you find you do not want to create some of these modules, use the <B CLASS="command" >no_*</B > FLAVOR. For example, to skip building the imap module, set the FLAVOR to <B CLASS="command" >no_imap</B >. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.openbsd.faq" >Common Problems</A ></H3 ><P ></P ><UL ><LI ><P >The default install of Apache runs inside a <A HREF="http://www.openbsd.org/cgi-bin/man.cgi?query=chroot" TARGET="_top" >chroot(2) jail</A >, which will restrict PHP scripts to accessing files under <TT CLASS="filename" >/var/www</TT >. You will therefore need to create a <TT CLASS="filename" >/var/www/tmp</TT > directory for PHP session files to be stored, or use an alternative session backend. In addition, database sockets need to be placed inside the jail or listen on the <TT CLASS="filename" >localhost</TT > interface. If you use network functions, some files from <TT CLASS="filename" >/etc</TT > such as <TT CLASS="filename" >/etc/resolv.conf</TT > and <TT CLASS="filename" >/etc/services</TT > will need to be moved into <TT CLASS="filename" >/var/www/etc</TT >. The OpenBSD PEAR package automatically installs into the correct chroot directories, so no special modification is needed there. More information on the OpenBSD Apache is available in the <A HREF="http://www.openbsd.org/faq/faq10.html#httpdchroot" TARGET="_top" >OpenBSD FAQ</A >. </P ></LI ><LI ><P >
The OpenBSD 3.4 package for the <A HREF="http://www.boutell.com/gd/" TARGET="_top" >gd</A > extension requires XFree86 to be installed. If you do not wish to use some of the font features that require X11, install the <TT CLASS="filename" >php4-gd-4.3.3-no_x11.tgz</TT > package instead. </P ></LI ></UL ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.openbsd.older" >Older Releases</A ></H3 ><P >
Older releases of OpenBSD used the FLAVORS system to compile up a statically linked PHP. Since it is hard to generate binary packages using this method, it is now deprecated. You can still use the old stable ports trees if you wish, but they are unsupported by the OpenBSD team. If you have any comments about this, the current maintainer for the port is Anil Madhavapeddy (avsm at openbsd dot org). </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.solaris" >Solaris specific installation tips</A ></H2 ><P >
This section contains notes and hints specific to installing PHP on Solaris systems. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.solaris.required" >Required software</A ></H3 ><P >
Solaris installs often lack C compilers and their related tools. Read <A HREF="#faq.installation.needgnu" >this FAQ</A > for information on why using GNU versions for some of these tools is necessary. The required software is as follows: <P ></P ><UL ><LI ><P >
gcc (recommended, other C compilers may work) </P ></LI ><LI ><P >
make </P ></LI ><LI ><P >
flex </P ></LI ><LI ><P >
bison </P ></LI ><LI ><P >
m4 </P ></LI ><LI ><P >
autoconf </P ></LI ><LI ><P >
automake </P ></LI ><LI ><P >
perl </P ></LI ><LI ><P >
gzip </P ></LI ><LI ><P >
tar </P ></LI ><LI ><P >
GNU sed </P ></LI ></UL > In addition, you will need to install (and possibly compile) any additional software specific to your configuration, such as Oracle or MySQL. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.solaris.packages" >Using Packages</A ></H3 ><P >
You can simplify the Solaris install process by using pkgadd to install most of your needed components. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.apache" >Apache 1.3.x on Unix systems</A ></H2 ><P >
This section contains notes and hints specific to Apache installs of PHP on Unix platforms. We also have <A HREF="#install.unix.apache2" >instructions and notes for Apache 2 on a separate page</A >. </P ><P >
You can select arguments to add to the <B CLASS="command" >configure</B > on line 10 below from the <A HREF="#configure" >list of core configure options</A > and from extension specific options described at the respective places in the manual. The version numbers have been omitted here, to ensure the instructions are not incorrect. You will need to replace the 'xxx' here with the correct values from your files. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="install.unix.apache.example" ></A ><P ><B >Beispiel 5-5. Installation Instructions (Apache Shared Module Version) for PHP </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >1. gunzip apache_xxx.tar.gz 2. tar -xvf apache_xxx.tar 3. gunzip php-xxx.tar.gz 4. tar -xvf php-xxx.tar 5. cd apache_xxx 6. ./configure --prefix=/www --enable-module=so 7. make 8. make install 9. cd ../php-xxx 10. Now, configure your PHP. This is where you customize your PHP with various options, like which extensions will be enabled. Do a ./configure --help for a list of available options. In our example we'll do a simple configure with Apache 1 and MySQL support. Your path to apxs may differ from our example. ./configure --with-mysql --with-apxs=/www/bin/apxs 11. make 12. make install If you decide to change your configure options after installation, you only need to repeat the last three steps. You only need to restart apache for the new module to take effect. A recompile of Apache is not needed. Note that unless told otherwise, 'make install' will also install PEAR, various PHP tools such as phpize, install the PHP CLI, and more. 13. Setup your php.ini file: cp php.ini-dist /usr/local/lib/php.ini You may edit your .ini file to set PHP options. If you prefer your php.ini in another location, use --with-config-file-path=/some/path in step 10. If you instead choose php.ini-recommended, be certain to read the list of changes within, as they affect how PHP behaves. 14. Edit your httpd.conf to load the PHP module. The path on the right hand side of the LoadModule statement must point to the path of the PHP module on your system. The make install from above may have already added this for you, but be sure to check. For PHP 4: LoadModule php4_module libexec/libphp4.so For PHP 5: LoadModule php5_module libexec/libphp5.so 15. And in the AddModule section of httpd.conf, somewhere under the ClearModuleList, add this: For PHP 4: AddModule mod_php4.c For PHP 5: AddModule mod_php5.c 16. Tell Apache to parse certain extensions as PHP. For example, let's have Apache parse the .php extension as PHP. You could have any extension(s) parse as PHP by simply adding more, with each separated by a space. We'll add .phtml to demonstrate. AddType application/x-httpd-php .php .phtml It's also common to setup the .phps extension to show highlighted PHP source, this can be done with: AddType application/x-httpd-php-source .phps 17. Use your normal procedure for starting the Apache server. (You must stop and restart the server, not just cause the server to reload by using a HUP or USR1 signal.)</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Depending on your Apache install and Unix variant, there are many possible ways to stop and restart the server. Below are some typical lines used in restarting the server, for different apache/unix installations. You should replace <VAR CLASS="literal" >/path/to/</VAR > with the path to these applications on your systems. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN726" ></A ><P ><B >Beispiel 5-6. Example commands for restarting Apache</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >1. Several Linux and SysV variants: /etc/rc.d/init.d/httpd restart 2. Using apachectl scripts: /path/to/apachectl stop /path/to/apachectl start 3. httpdctl and httpsdctl (Using OpenSSL), similar to apachectl: /path/to/httpsdctl stop /path/to/httpsdctl start 4. Using mod_ssl, or another SSL server, you may want to manually stop and start: /path/to/apachectl stop /path/to/apachectl startssl</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
The locations of the apachectl and http(s)dctl binaries often vary. If your system has <VAR CLASS="literal" >locate</VAR > or <VAR CLASS="literal" >whereis</VAR > or <VAR CLASS="literal" >which</VAR > commands, these can assist you in finding your server control programs. </P ><P >
Different examples of compiling PHP for apache are as follows: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN734" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >./configure --with-apxs --with-pgsql</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
This will create a <TT CLASS="filename" >libphp4.so</TT > shared library that is loaded into Apache using a LoadModule line in Apache's <TT CLASS="filename" >httpd.conf</TT > file. The PostgreSQL support is embedded into this <TT CLASS="filename" >libphp4.so</TT > library. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN741" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >./configure --with-apxs --with-pgsql=shared</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
This will create a <TT CLASS="filename" >libphp4.so</TT > shared library for Apache, but it will also create a <TT CLASS="filename" >pgsql.so</TT > shared library that is loaded into PHP either by using the extension directive in <TT CLASS="filename" >php.ini</TT > file or by loading it explicitly in a script using the <A HREF="#function.dl" ><B CLASS="function" >dl()</B ></A > function. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN749" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >./configure --with-apache=/path/to/apache_source --with-pgsql</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
This will create a <TT CLASS="filename" >libmodphp4.a</TT > library, a <TT CLASS="filename" >mod_php4.c</TT > and some accompanying files and copy this into the <VAR CLASS="literal" >src/modules/php4</VAR > directory in the Apache source tree. Then you compile Apache using <VAR CLASS="literal" >--activate-module=src/modules/php4/libphp4.a</VAR > and the Apache build system will create <TT CLASS="filename" >libphp4.a</TT > and link it statically into the <TT CLASS="filename" >httpd</TT > binary. The PostgreSQL support is included directly into this <TT CLASS="filename" >httpd</TT > binary, so the final result here is a single <TT CLASS="filename" >httpd</TT > binary that includes all of Apache and all of PHP. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN761" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >./configure --with-apache=/path/to/apache_source --with-pgsql=shared</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Same as before, except instead of including PostgreSQL support directly into the final <TT CLASS="filename" >httpd</TT > you will get a <TT CLASS="filename" >pgsql.so</TT > shared library that you can load into PHP from either the <TT CLASS="filename" >php.ini</TT > file or directly using <A HREF="#function.dl" ><B CLASS="function" >dl()</B ></A >. </P ><P >
When choosing to build PHP in different ways, you should consider the advantages and drawbacks of each method. Building as a shared object will mean that you can compile apache separately, and don't have to recompile everything as you add to, or change, PHP. Building PHP into apache (static method) means that PHP will load and run faster. For more information, see the Apache <A HREF="http://httpd.apache.org/docs/dso.html" TARGET="_top" >webpage on DSO support</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Apache's default <TT CLASS="filename" >httpd.conf</TT > currently ships with a section that looks like this: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN773" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" >User nobody Group "#-1"</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Unless you change that to "Group nogroup" or something like that ("Group daemon" is also very common) PHP will not be able to open files. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Make sure you specify the installed version of apxs when using <VAR CLASS="option" >--with-apxs=/path/to/apxs</VAR >. You must NOT use the apxs version that is in the apache sources but the one that is actually installed on your system. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.apache2" >Apache 2.0 on Unix systems</A ></H2 ><P >
This section contains notes and hints specific to Apache 2.0 installs of PHP on Unix systems. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Verwenden Sie Apache 2.0.x und PHP weder unter Unix noch Windows in einer Produktivumgebung. Fⁿr weitere Informationen und Grⁿnde lesen Sie bitte diesen <A HREF="#faq.installation.apache2" >FAQ Eintrag.</A ></P ></TD ></TR ></TABLE ></DIV ><P >
You are highly encouraged to take a look at the <A HREF="http://httpd.apache.org/docs-2.0/" TARGET="_top" >Apache Documentation</A > to get a basic understanding of the Apache 2.0 Server. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >PHP and Apache 2.0.x compatibility notes: </B > The following versions of PHP are known to work with the most recent version of Apache 2.0.x: <P ></P ><UL COMPACT="COMPACT" ><LI ><SPAN >
PHP 4.3.0 or later available at <A HREF="http://www.php.net/downloads.php" TARGET="_top" >http://www.php.net/downloads.php</A >. </SPAN ></LI ><LI ><SPAN >
the latest stable development version. Get the source code <A HREF="http://snaps.php.net/php4-latest.tar.gz" TARGET="_top" >
http://snaps.php.net/php4-latest.tar.gz</A > or download binaries for Windows <A HREF="http://snaps.php.net/win32/php4-win32-latest.zip" TARGET="_top" >
http://snaps.php.net/win32/php4-win32-latest.zip</A >. </SPAN ></LI ><LI ><SPAN >
a prerelease version downloadable from <A HREF="http://qa.php.net/" TARGET="_top" >http://qa.php.net/</A >. </SPAN ></LI ><LI ><SPAN >
you have always the option to obtain PHP through <A HREF="http://www.php.net/anoncvs.php" TARGET="_top" >anonymous CVS</A >. </SPAN ></LI ></UL > These versions of PHP are compatible to Apache 2.0.40 and later. </P ><P >
Apache 2.0 <VAR CLASS="literal" >SAPI</VAR >-support started with PHP 4.2.0. PHP 4.2.3 works with Apache 2.0.39, don't use any other version of Apache with PHP 4.2.3. However, the recommended setup is to use PHP 4.3.0 or later with the most recent version of Apache2. </P ><P >
All mentioned versions of PHP will work still with Apache 1.3.x. </P ></BLOCKQUOTE ></DIV ><P >
Download the most recent version of <A HREF="http://www.apache.org/" TARGET="_top" >
Apache 2.0</A > and a fitting PHP version from the above mentioned places. This quick guide covers only the basics to get started with Apache 2.0 and PHP. For more information read the <A HREF="http://httpd.apache.org/docs-2.0/" TARGET="_top" >Apache Documentation</A >. The version numbers have been omitted here, to ensure the instructions are not incorrect. You will need to replace the 'NN' here with the correct values from your files. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN809" ></A ><P ><B >Beispiel 5-7. Installation Instructions (Apache 2 Shared Module Version) </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >1. gzip -d httpd-2_0_NN.tar.gz 2. tar xvf httpd-2_0_NN.tar 3. gunzip php-NN.tar.gz 4. tar -xvf php-NN.tar 5. cd httpd-2_0_NN 6. ./configure --enable-so 7. make 8. make install Now you have Apache 2.0.NN available under /usr/local/apache2, configured with loadable module support and the standard MPM prefork. To test the installation use your normal procedure for starting the Apache server, e.g.: /usr/local/apache2/bin/apachectl start and stop the server to go on with the configuration for PHP: /usr/local/apache2/bin/apachectl stop. 9. cd ../php-NN 10. Now, configure your PHP. This is where you customize your PHP with various options, like which extensions will be enabled. Do a ./configure --help for a list of available options. In our example we'll do a simple configure with Apache 2 and MySQL support. Your path to apxs may differ, in fact, the binary may even be named apxs2 on your system. ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql 11. make 12. make install If you decide to change your configure options after installation, you only need to repeat the last three steps. You only need to restart apache for the new module to take effect. A recompile of Apache is not needed. Note that unless told otherwise, 'make install' will also install PEAR, various PHP tools such as phpize, install the PHP CLI, and more. 13. Setup your php.ini cp php.ini-dist /usr/local/lib/php.ini You may edit your .ini file to set PHP options. If you prefer having php.ini in another location, use --with-config-file-path=/some/path in step 10. If you instead choose php.ini-recommended, be certain to read the list of changes within, as they affect how PHP behaves. 14. Edit your httpd.conf to load the PHP module. The path on the right hand side of the LoadModule statement must point to the path of the PHP module on your system. The make install from above may have already added this for you, but be sure to check. For PHP 4: LoadModule php4_module libexec/libphp4.so For PHP 5: LoadModule php5_module libexec/libphp5.so 15. Tell Apache to parse certain extensions as PHP. For example, let's have Apache parse the .php extension as PHP. You could have any extension(s) parse as PHP by simply adding more, with each separated by a space. We'll add .phtml to demonstrate. AddType application/x-httpd-php .php .phtml It's also common to setup the .phps extension to show highlighted PHP source, this can be done with: AddType application/x-httpd-php-source .phps 16. Use your normal procedure for starting the Apache server, e.g.: /usr/local/apache2/bin/apachectl start</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Following the steps above you will have a running Apache 2.0 with support for PHP as <VAR CLASS="literal" >SAPI</VAR > module. Of course there are many more configuration options available for both, Apache and PHP. For more information use <B CLASS="command" >./configure --help</B > in the corresponding source tree. In case you wish to build a multithreaded version of Apache 2.0 you must overwrite the standard MPM-Module <TT CLASS="filename" >prefork</TT > either with <TT CLASS="filename" >worker</TT > or <TT CLASS="filename" >perchild</TT >. To do so append to your configure line in step 6 above either the option <VAR CLASS="option" >--with-mpm=worker</VAR > or <VAR CLASS="option" >--with-mpm=perchild</VAR >. Take care about the consequences and understand what you are doing. For more information read the Apache documentation about the <A HREF="http://httpd.apache.org/docs-2.0/mpm.html" TARGET="_top" >
MPM-Modules</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > To build a multithreaded version of Apache your system must support threads. This also implies to build PHP with experimental Zend Thread Safety (ZTS). Therefore not all extensions might be available. The recommended setup is to build Apache with the standard <TT CLASS="filename" >prefork</TT > MPM-Module. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.caudium" >Caudium</A ></H2 ><P >
PHP 4 can be built as a Pike module for the <A HREF="http://caudium.net/" TARGET="_top" >Caudium webserver</A >. Note that this is not supported with PHP 3. Follow the simple instructions below to install PHP 4 for Caudium. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="install.unix.caudium.instructions" ></A ><P ><B >Beispiel 5-8. Caudium Installation Instructions</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >1. Make sure you have Caudium installed prior to attempting to install PHP 4. For PHP 4 to work correctly, you will need Pike 7.0.268 or newer. For the sake of this example we assume that Caudium is installed in /opt/caudium/server/. 2. Change directory to php-x.y.z (where x.y.z is the version number). 3. ./configure --with-caudium=/opt/caudium/server 4. make 5. make install 6. Restart Caudium if it's currently running. 7. Log into the graphical configuration interface and go to the virtual server where you want to add PHP 4 support. 8. Click Add Module and locate and then add the PHP 4 Script Support module. 9. If the documentation says that the 'PHP 4 interpreter isn't available', make sure that you restarted the server. If you did check /opt/caudium/logs/debug/default.1 for any errors related to <filename>PHP4.so</filename>. Also make sure that <filename>caudium/server/lib/[pike-version]/PHP4.so</filename> is present. 10. Configure the PHP Script Support module if needed.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
You can of course compile your Caudium module with support for the various extensions available in PHP 4. See the reference pages for extension specific configure options. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > When compiling PHP 4 with MySQL support you must make sure that the normal MySQL client code is used. Otherwise there might be conflicts if your Pike already has MySQL support. You do this by specifying a MySQL install directory the <VAR CLASS="option" >--with-mysql</VAR > option. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.fhttpd" >fhttpd related notes</A ></H2 ><P >
To build PHP as an fhttpd module, answer "yes" to "Build as an fhttpd module?" (the <A HREF="#configure.with-fhttpd" >
--with-fhttpd</A >=<VAR CLASS="replaceable" >DIR</VAR > option to configure) and specify the fhttpd source base directory. The default directory is <TT CLASS="filename" >/usr/local/src/fhttpd</TT >. If you are running fhttpd, building PHP as a module will give better performance, more control and remote execution capability. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Support for fhttpd is no longer available as of PHP 4.3.0. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.sun" >Sun, iPlanet and Netscape servers on Sun Solaris</A ></H2 ><P >
This section contains notes and hints specific to Sun Java System Web Server, Sun ONE Web Server, iPlanet and Netscape server installs of PHP on Sun Solaris. </P ><P >
From PHP 4.3.3 on you can use PHP scripts with the <A HREF="#ref.nsapi" >NSAPI module</A > to <A HREF="#install.unix.sun.specialpages" >generate custom directory listings and error pages</A >. Additional functions for Apache compatibility are also available. For support in current webservers read the <A HREF="#install.unix.sun.notes" >note about subrequests</A >. </P ><P >
You can find more information about setting up PHP for the Netscape Enterprise Server (NES) here: <A HREF="http://benoit.noss.free.fr/php/install-php4.html" TARGET="_top" >http://benoit.noss.free.fr/php/install-php4.html</A > </P ><P >
To build PHP with Sun JSWS/Sun ONE WS/iPlanet/Netscape webservers, enter the proper install directory for the <A HREF="#configure.with-nsapi" >--with-nsapi=[DIR]</A > option. The default directory is usually <TT CLASS="filename" >/opt/netscape/suitespot/</TT >. Please also read <TT CLASS="filename" >/php-xxx-version/sapi/nsapi/nsapi-readme.txt</TT >. </P ><P >
<P ></P ><OL TYPE="1" ><LI ><P >
Install the following packages from <A HREF="http://www.sunfreeware.com/" TARGET="_top" >
http://www.sunfreeware.com/</A > or another download site: <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><TT CLASS="filename" >autoconf-2.13</TT ></TD ></TR ><TR ><TD ><TT CLASS="filename" >automake-1.4</TT ></TD ></TR ><TR ><TD ><TT CLASS="filename" >bison-1_25-sol26-sparc-local</TT ></TD ></TR ><TR ><TD ><TT CLASS="filename" >flex-2_5_4a-sol26-sparc-local</TT ></TD ></TR ><TR ><TD ><TT CLASS="filename" >gcc-2_95_2-sol26-sparc-local</TT ></TD ></TR ><TR ><TD ><TT CLASS="filename" >gzip-1.2.4-sol26-sparc-local</TT ></TD ></TR ><TR ><TD ><TT CLASS="filename" >m4-1_4-sol26-sparc-local</TT ></TD ></TR ><TR ><TD ><TT CLASS="filename" >make-3_76_1-sol26-sparc-local</TT ></TD ></TR ><TR ><TD >
<TT CLASS="filename" >mysql-3.23.24-beta</TT > (if you want mysql support) </TD ></TR ><TR ><TD ><TT CLASS="filename" >perl-5_005_03-sol26-sparc-local</TT ></TD ></TR ><TR ><TD ><TT CLASS="filename" >tar-1.13</TT > (GNU tar)</TD ></TR ></TBODY ></TABLE ><P ></P > </P ></LI ><LI ><P >
Make sure your path includes the proper directories <VAR CLASS="literal" >PATH=.:/usr/local/bin:/usr/sbin:/usr/bin:/usr/ccs/bin</VAR > and make it available to your system <KBD CLASS="userinput" >export PATH</KBD >. </P ></LI ><LI ><P >
<KBD CLASS="userinput" >gunzip php-x.x.x.tar.gz</KBD > (if you have a .gz dist, otherwise go to 4). </P ></LI ><LI ><P >
<KBD CLASS="userinput" >tar xvf php-x.x.x.tar</KBD > </P ></LI ><LI ><P >
Change to your extracted PHP directory: <KBD CLASS="userinput" >cd ../php-x.x.x </KBD > </P ></LI ><LI ><P >
For the following step, make sure <TT CLASS="filename" >/opt/netscape/suitespot/</TT > is where your netscape server is installed. Otherwise, change to the correct path and run: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >./configure --with-mysql=/usr/local/mysql \ --with-nsapi=/opt/netscape/suitespot/ \ --enable-libgcc</PRE ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
Run <B CLASS="command" >make</B > followed by <B CLASS="command" >make install</B >. </P ></LI ></OL > </P ><P >
After performing the base install and reading the appropriate readme file, you may need to perform some additional configuration steps. </P ><DIV CLASS="formalpara" ><P ><B >Configuration Instructions for Sun/iPlanet/Netscape. </B > Firstly you may need to add some paths to the <VAR CLASS="varname" >LD_LIBRARY_PATH</VAR > environment for the server to find all the shared libs. This can best done in the start script for your webserver. The start script is often located in: <TT CLASS="filename" >/path/to/server/https-servername/start</TT >. You may also need to edit the configuration files that are located in: <TT CLASS="filename" >/path/to/server/https-servername/config/</TT >. <P ></P ><OL TYPE="1" ><LI ><P >
Add the following line to <TT CLASS="filename" >mime.types</TT > (you can do that by the administration server): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >type=magnus-internal/x-httpd-php exts=php</PRE ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
Edit <TT CLASS="filename" >magnus.conf</TT > (for servers >= 6) or <TT CLASS="filename" >obj.conf</TT > (for servers < 6) and add the following, shlib will vary depending on your system, it will be something like <TT CLASS="filename" >/opt/netscape/suitespot/bin/libphp4.so</TT >. You should place the following lines after <VAR CLASS="literal" >mime types init</VAR >. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Init fn="load-modules" funcs="php4_init,php4_execute,php4_auth_trans" shlib="/opt/netscape/suitespot/bin/libphp4.so" Init fn="php4_init" LateInit="yes" errorString="Failed to initialize PHP!" [php_ini="/path/to/php.ini"]</PRE ></TD ></TR ></TABLE > (PHP >= 4.3.3) The <VAR CLASS="literal" >php_ini</VAR > parameter is optional but with it you can place your <TT CLASS="filename" >php.ini</TT > in your webserver config directory. </P ></LI ><LI ><P >
Configure the default object in <TT CLASS="filename" >obj.conf</TT > (for virtual server classes [version 6.0+] in their <TT CLASS="filename" >vserver.obj.conf</TT >): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><Object name="default"> . . . .#NOTE this next line should happen after all 'ObjectType' and before all 'AddLog' lines Service fn="php4_execute" type="magnus-internal/x-httpd-php" [inikey=value inikey=value ...] . . </Object></PRE ></TD ></TR ></TABLE > (PHP >= 4.3.3) As additional parameters you can add some special <TT CLASS="filename" >php.ini</TT >-values, for example you can set a <VAR CLASS="literal" >docroot="/path/to/docroot"</VAR > specific to the context <VAR CLASS="literal" >php4_execute</VAR > is called. For boolean ini-keys please use 0/1 as value, not <VAR CLASS="literal" >"On","Off",...</VAR > (this will not work correctly), e.g. <VAR CLASS="literal" >zlib.output_compression=1</VAR > instead of <VAR CLASS="literal" >zlib.output_compression="On"</VAR > </P ></LI ><LI ><P >
This is only needed if you want to configure a directory that only consists of PHP scripts (same like a <TT CLASS="filename" >cgi-bin</TT > directory): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><Object name="x-httpd-php"> ObjectType fn="force-type" type="magnus-internal/x-httpd-php" Service fn=php4_execute [inikey=value inikey=value ...] </Object></PRE ></TD ></TR ></TABLE > After that you can configure a directory in the Administration server and assign it the style <VAR CLASS="literal" >x-httpd-php</VAR >. All files in it will get executed as PHP. This is nice to hide PHP usage by renaming files to <TT CLASS="filename" >.html</TT >. </P ></LI ><LI ><P >
Setup of authentication: PHP authentication cannot be used with any other authentication. ALL AUTHENTICATION IS PASSED TO YOUR PHP SCRIPT. To configure PHP Authentication for the entire server, add the following line to your default object: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><Object name="default"> AuthTrans fn=php4_auth_trans . . . </Object></PRE ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
To use PHP Authentication on a single directory, add the following: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><Object ppath="d:\path\to\authenticated\dir\*"> AuthTrans fn=php4_auth_trans </Object></PRE ></TD ></TR ></TABLE > </P ></LI ></OL > </P ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > The stacksize that PHP uses depends on the configuration of the webserver. If you get crashes with very large PHP scripts, it is recommended to raise it with the Admin Server (in the section "MAGNUS EDITOR"). </P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.sun.phpini" >CGI environment and recommended modifications in php.ini</A ></H3 ><P >
Important when writing PHP scripts is the fact that Sun JSWS/Sun ONE WS/iPlanet/Netscape is a multithreaded web server. Because of that all requests are running in the same process space (the space of the webserver itself) and this space has only one environment. If you want to get CGI variables like <VAR CLASS="literal" >PATH_INFO</VAR >, <VAR CLASS="literal" >HTTP_HOST</VAR > etc. it is not the correct way to try this in the old PHP 3.x way with <A HREF="#function.getenv" ><B CLASS="function" >getenv()</B ></A > or a similar way (register globals to environment, <VAR CLASS="literal" >$_ENV</VAR >). You would only get the environment of the running webserver without any valid CGI variables! </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Why are there (invalid) CGI variables in the environment? </P ><P >
Answer: This is because you started the webserver process from the admin server which runs the startup script of the webserver, you wanted to start, as a CGI script (a CGI script inside of the admin server!). This is why the environment of the started webserver has some CGI environment variables in it. You can test this by starting the webserver not from the administration server. Use the command line as root user and start it manually - you will see there are no CGI-like environment variables. </P ></BLOCKQUOTE ></DIV ><P >
Simply change your scripts to get CGI variables in the correct way for PHP 4.x by using the superglobal <VAR CLASS="literal" >$_SERVER</VAR >. If you have older scripts which use <VAR CLASS="literal" >$HTTP_HOST</VAR >, etc., you should turn on <VAR CLASS="literal" >register_globals</VAR > in <TT CLASS="filename" >php.ini</TT > and change the variable order too (important: remove <VAR CLASS="literal" >"E"</VAR > from it, because you do not need the environment here): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >variables_order = "GPCS" register_globals = On</PRE ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.sun.specialpages" >Special use for error pages or self-made directory listings (PHP >= 4.3.3)</A ></H3 ><P >
You can use PHP to generate the error pages for <VAR CLASS="literal" >"404 Not Found"</VAR > or similar. Add the following line to the object in <TT CLASS="filename" >obj.conf</TT > for every error page you want to overwrite: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Error fn="php4_execute" code=XXX script="/path/to/script.php" [inikey=value inikey=value...]</PRE ></TD ></TR ></TABLE > where <VAR CLASS="literal" >XXX</VAR > is the HTTP error code. Please delete any other <VAR CLASS="literal" >Error</VAR > directives which could interfere with yours. If you want to place a page for all errors that could exist, leave the <VAR CLASS="literal" >code</VAR > parameter out. Your script can get the HTTP status code with <VAR CLASS="literal" >$_SERVER['ERROR_TYPE']</VAR >. </P ><P >
Another possibility is to generate self-made directory listings. Just create a PHP script which displays a directory listing and replace the corresponding default Service line for <VAR CLASS="literal" >type="magnus-internal/directory"</VAR > in <TT CLASS="filename" >obj.conf</TT > with the following: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Service fn="php4_execute" type="magnus-internal/directory" script="/path/to/script.php" [inikey=value inikey=value...]</PRE ></TD ></TR ></TABLE > For both error and directory listing pages the original URI and translated URI are in the variables <VAR CLASS="literal" >$_SERVER['PATH_INFO']</VAR > and <VAR CLASS="literal" >$_SERVER['PATH_TRANSLATED']</VAR >. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.sun.notes" >Note about <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > and subrequests (PHP >= 4.3.3)</A ></H3 ><P >
The NSAPI module now supports the <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > function (alias: <A HREF="#function.virtual" ><B CLASS="function" >virtual()</B ></A >) to make subrequests on the webserver and insert the result in the webpage. This function uses some undocumented features from the NSAPI library. On Unix the module automatically looks for the needed functions and uses them if available. If not, <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > is disabled. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > But be warned: Support for <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > is EXPERIMENTAL!!! </P ></BLOCKQUOTE ></DIV ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.unix.commandline" >CGI and commandline setups</A ></H2 ><P >
The default is to build PHP as a CGI program. This creates a commandline interpreter, which can be used for CGI processing, or for non-web-related PHP scripting. If you are running a web server PHP has module support for, you should generally go for that solution for performance reasons. However, the CGI version enables users to run different PHP-enabled pages under different user-ids. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >By using the CGI setup, your server is open to several possible attacks. Please read our <A HREF="#security.cgi-bin" >CGI security section</A > to learn how to defend yourself from those attacks.</P ></TD ></TR ></TABLE ></DIV ><P >
As of PHP 4.3.0, some important additions have happened to PHP. A new SAPI named CLI also exists and it has the same name as the CGI binary. What is installed at <VAR CLASS="literal" >{PREFIX}/bin/php</VAR > depends on your configure line and this is described in detail in the manual section named <A HREF="#features.commandline" >Using PHP from the command line</A >. For further details please read that section of the manual. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.commandline.testing" >Testing</A ></H3 ><P >
If you have built PHP as a CGI program, you may test your build by typing <B CLASS="command" >make test</B >. It is always a good idea to test your build. This way you may catch a problem with PHP on your platform early instead of having to struggle with it later. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.commandline.benchmarking" >Benchmarking</A ></H3 ><P >
If you have built PHP 3 as a CGI program, you may benchmark your build by typing <B CLASS="command" >make bench</B >. Note that if <A HREF="#ini.safe-mode" >Safe Mode</A > is on by default, the benchmark may not be able to finish if it takes longer then the 30 seconds allowed. This is because the <A HREF="#function.set-time-limit" ><B CLASS="function" >set_time_limit()</B ></A > can not be used in <A HREF="#ini.safe-mode" >Safe Mode</A >. Use the <A HREF="#ini.max-execution-time" >max_execution_time</A > configuration setting to control this time for your own scripts. <B CLASS="command" >make bench</B > ignores the <A HREF="#configuration.file" >configuration file</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <B CLASS="command" >make bench</B > is only available for PHP 3. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.unix.commandline.using-variables" >Using Variables</A ></H3 ><P >
Some <A HREF="#reserved.variables.server" >server supplied environment variables</A > are not defined in the current <A HREF="http://hoohoo.ncsa.uiuc.edu/cgi/env.html" TARGET="_top" >CGI/1.1 specification</A >. Only the following variables are defined there: <VAR CLASS="varname" >AUTH_TYPE</VAR >, <VAR CLASS="varname" >CONTENT_LENGTH</VAR >, <VAR CLASS="varname" >CONTENT_TYPE</VAR >, <VAR CLASS="varname" >GATEWAY_INTERFACE</VAR >, <VAR CLASS="varname" >PATH_INFO</VAR >, <VAR CLASS="varname" >PATH_TRANSLATED</VAR >, <VAR CLASS="varname" >QUERY_STRING</VAR >, <VAR CLASS="varname" >REMOTE_ADDR</VAR >, <VAR CLASS="varname" >REMOTE_HOST</VAR >, <VAR CLASS="varname" >REMOTE_IDENT</VAR >, <VAR CLASS="varname" >REMOTE_USER</VAR >, <VAR CLASS="varname" >REQUEST_METHOD</VAR >, <VAR CLASS="varname" >SCRIPT_NAME</VAR >, <VAR CLASS="varname" >SERVER_NAME</VAR >, <VAR CLASS="varname" >SERVER_PORT</VAR >, <VAR CLASS="varname" >SERVER_PROTOCOL</VAR >, and <VAR CLASS="varname" >SERVER_SOFTWARE</VAR >. Everything else should be treated as 'vendor extensions'. </P ></DIV ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="install.windows" >Kapitel 6. Installation on Windows systems</A ></H1 ><P >
This section applies to Windows 98/Me and Windows NT/2000/XP/2003. PHP will not work on 16 bit platforms such as Windows 3.1 and sometimes we refer to the supported Windows platforms as Win32. Windows 95 is no longer supported as of PHP 4.3.0. </P ><P >
There are two main ways to install PHP for Windows: either <A HREF="#install.windows.manual" >manually</A > or by using the <A HREF="#install.windows.installer" >installer</A >. </P ><P >
If you have Microsoft Visual Studio, you can also <A HREF="#install.windows.build" >build</A > PHP from the original source code. </P ><P >
Once you have PHP installed on your Windows system, you may also want to <A HREF="#install.windows.extensions" >load various extensions</A > for added functionality. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
There are several all-in-one installers over the Internet, but none of those are endorsed by PHP.net, as we believe that the manual installation is the best choice to have your system secure and optimised. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.installer" >Windows Installer</A ></H2 ><P >
The Windows PHP installer is available from the downloads page at <A HREF="http://www.php.net/downloads.php" TARGET="_top" >http://www.php.net/downloads.php</A >. This installs the <SPAN CLASS="emphasis" ><I CLASS="emphasis" >CGI version</I ></SPAN > of PHP and for IIS, PWS, and Xitami, it configures the web server as well. The installer does not include any extra external PHP extensions (php_*.dll) as you'll only find those in the Windows Zip Package and <ACRONYM CLASS="acronym" >PECL</ACRONYM > downloads. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > While the Windows installer is an easy way to make PHP work, it is restricted in many aspects as, for example, the automatic setup of extensions is not supported. Use of the installer isn't the preferred method for installing PHP. </P ></BLOCKQUOTE ></DIV ><P >
First, install your selected <ACRONYM CLASS="acronym" >HTTP</ACRONYM > (web) server on your system, and make sure that it works. </P ><P >
Run the executable installer and follow the instructions provided by the installation wizard. Two types of installation are supported - standard, which provides sensible defaults for all the settings it can, and advanced, which asks questions as it goes along. </P ><P >
The installation wizard gathers enough information to set up the <TT CLASS="filename" >php.ini</TT > file, and configure certain web servers to use PHP. With IIS or PWS on a NT Workstation, a list of all the nodes on the server with script map settings is displayed, and you can choose those nodes to which you wish to add the PHP script mappings. One of the web servers the PHP installer does not configure for is Apache, so you'll need to configure it manually. </P ><P >
Once the installation has completed, the installer will inform you if you need to restart your system, restart the server, or just start using PHP. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Be aware, that this setup of PHP is not secure. If you would like to have a secure PHP setup, you'd better go on the manual way, and set every option carefully. This automatically working setup gives you an instantly working PHP installation, but it is not meant to be used on online servers. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.manual" >Manual Installation Steps</A ></H2 ><P >
This install guide will help you manually install and configure PHP with a web server on Microsoft Windows. To get started you'll need to download the zip binary distribution from the downloads page at <A HREF="http://www.php.net/downloads.php" TARGET="_top" >http://www.php.net/downloads.php</A >. </P ><P >
Although there are many all-in-one installation kits, and we also distribute a PHP installer for Microsoft Windows, we recommend you take the time to setup PHP yourself as this will provide you with a better understanding of the system, and enables you to install PHP extensions easily when needed. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><A NAME="install.windows.manual.upgrade" ></A ><P ><B >Upgrading from a previous PHP version: </B > Previous editions of the manual suggest moving various ini and <ACRONYM CLASS="acronym" >DLL</ACRONYM > files into your SYSTEM (i.e. <TT CLASS="filename" >C:\WINDOWS</TT >) folder and while this simplifies the installation procedure it makes upgrading difficult. We advise you remove all of these files (like <TT CLASS="filename" >php.ini</TT > and PHP related DLLs from the Windows SYSTEM folder) before moving on with a new PHP installation. Be sure to backup these files as you might break the entire system. The old <TT CLASS="filename" >php.ini</TT > might be useful in setting up the new PHP as well. And as you'll soon learn, the preferred method for installing PHP is to keep all PHP related files in one directory and have this directory available to your systems PATH. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >MDAC requirements: </B > If you use Microsoft <SPAN CLASS="emphasis" ><I CLASS="emphasis" >Windows 98/NT4</I ></SPAN > download the latest version of the Microsoft Data Access Components (MDAC) for your platform. MDAC is available at <A HREF="http://msdn.microsoft.com/data/" TARGET="_top" >http://msdn.microsoft.com/data/</A >. This requirement exists because <A HREF="#ref.uodbc" >ODBC</A > is built into the distributed Windows binaries. </P ></BLOCKQUOTE ></DIV ><P >
The following steps should be completed on all installations before any server specific instructions are performed: </P ><P >
Extract the distribution file into a directory of your choice. If you are installing PHP 4, extract to <TT CLASS="filename" >C:\</TT >, as the zip file expands to a foldername like <TT CLASS="filename" >php-4.3.7-Win32</TT >. If you are installing PHP 5, extract to <TT CLASS="filename" >C:\php</TT > as the zip file doesn't expand as in PHP 4. You may choose a different location but do not have spaces in the path (like <TT CLASS="filename" >C:\Program Files\PHP</TT >) as some web servers will crash if you do. </P ><P >
The directory structure extracted from the zip is different for PHP versions 4 and 5 and look like as follows: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1095" ></A ><P ><B >Beispiel 6-1. PHP 4 package structure</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >c:\php | +--cli | | | |-php.exe -- CLI executable - ONLY for commandline scripting | +--dlls -- support DLLs required by some extensions | | | |-expat.dll | | | |-fdftk.dll | | | |-... | +--extensions -- extension DLLs for PHP | | | |-php_bz2.dll | | | |-php_cpdf.dll | | | |-.. | +--mibs -- support files for SNMP | +--openssl -- support files for Openssl | +--pdf-related -- support files for PDF | +--sapi -- SAPI (server module support) DLLs | | | |-php4activescript.dll | | | |-php4apache.dll | | | |-php4apache2.dll | | | |-.. | +--PEAR -- initial copy of PEAR | | |-go-pear.bat -- PEAR setup script | |-.. | |-php.exe -- CGI executable | |-.. | |-php.ini-dist -- default php.ini settings | |-php.ini-recommended -- recommended php.ini settings | |-php4ts.dll -- core PHP DLL | |-...</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Or: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1099" ></A ><P ><B >Beispiel 6-2. PHP 5 package structure</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >c:\php | +--dev | | | |-php5ts.lib | +--ext -- extension DLLs for PHP | | | |-php_bz2.dll | | | |-php_cpdf.dll | | | |-.. | +--extras | | | +--mibs -- support files for SNMP | | | +--openssl -- support files for Openssl | | | +--pdf-related -- support files for PDF | | | |-mime.magic | +--pear -- initial copy of PEAR | | |-go-pear.bat -- PEAR setup script | |-fdftk.dll | |-.. | |-php-cgi.exe -- CGI executable | |-php-win.exe -- executes scripts without an opened command prompt | |-php.exe -- CLI executable - ONLY for command line scripting | |-.. | |-php.ini-dist -- default php.ini settings | |-php.ini-recommended -- recommended php.ini settings | |-php5activescript.dll | |-php5apache.dll | |-php5apache2.dll | |-.. | |-php5ts.dll -- core PHP DLL | |-...</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Notice the differences and similarities. Both PHP 4 and PHP 5 have a <ACRONYM CLASS="acronym" >CGI</ACRONYM > executable, a <ACRONYM CLASS="acronym" >CLI</ACRONYM > executable, and server modules, but they are located in different folders and/or have different names. While PHP 4 packages have the server modules in the <TT CLASS="filename" >sapi</TT > folder, PHP 5 distributions have no such directory and instead they're in the PHP folder root. The supporting DLLs for the PHP 5 extensions are also not in a seperate directory. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In PHP 4, you should move all files located in the <TT CLASS="filename" >dll</TT > and <TT CLASS="filename" >sapi</TT > folders to the main folder (e.g. <TT CLASS="filename" >C:\php</TT >). </P ></BLOCKQUOTE ></DIV ><P >
Here is a list of server modules shipped with PHP 4 and PHP 5: <P ></P ><UL ><LI ><P >
<TT CLASS="filename" >sapi/php4activescript.dll (php5activescript.dll)</TT > - <A HREF="#install.windows.activescript" >ActiveScript engine</A >, allowing you to embed PHP in your Windows applications. </P ></LI ><LI ><P >
<TT CLASS="filename" >sapi/php4apache.dll (php5apache.dll)</TT > - Apache 1.3.x module. </P ></LI ><LI ><P >
<TT CLASS="filename" >sapi/php4apache2.dll (php5apache2.dll)</TT > - Apache 2.0.x module. </P ></LI ><LI ><P >
<TT CLASS="filename" >sapi/php4isapi.dll (php5isapi.dll)</TT > - ISAPI Module for ISAPI compliant web servers like IIS 4.0/PWS 4.0 or newer. </P ></LI ><LI ><P >
<TT CLASS="filename" >sapi/php4nsapi.dll (php5nsapi.dll)</TT > - Sun/iPlanet/Netscape server module. </P ></LI ><LI ><P >
<TT CLASS="filename" >sapi/php4pi3web.dll (no equivalent in PHP 5)</TT > - Pi3Web server module. </P ></LI ></UL > </P ><P >
Server modules provide significantly better performance and additional functionality compared to the CGI binary. The CLI version is designed to let you use PHP for command line scripting. More information about CLI is available in the chapter about <A HREF="#features.commandline" >using PHP from the command line</A >. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
The SAPI modules have been significantly improved as of the 4.1 release, however, in older systems you may encounter server errors or other server modules failing, such as ASP. </P ></TD ></TR ></TABLE ></DIV ><P >
The CGI and CLI binaries, and the web server modules all require the <TT CLASS="filename" >php4ts.dll</TT > (<TT CLASS="filename" >php5ts.dll</TT >) file to be available to them. You have to make sure that this file can be found by your PHP installation. The search order for this DLL is as follows: <P ></P ><UL ><LI ><P >
The same directory from where <TT CLASS="filename" >php.exe</TT > is called, or in case you use a SAPI module, the web server's directory (e.g. <TT CLASS="filename" >C:\Program Files\Apache Group\Apache2\bin</TT >). </P ></LI ><LI ><P >
Any directory in your Windows <VAR CLASS="varname" >PATH</VAR > environment variable. </P ></LI ></UL > </P ><P >
To make <TT CLASS="filename" >php4ts.dll</TT > / <TT CLASS="filename" >php5ts.dll</TT > available you have three options: copy the file to the Windows system directory, copy the file to the web server's directory, or add your PHP directory, <TT CLASS="filename" >C:\php</TT > to the <VAR CLASS="varname" >PATH</VAR >. For better maintenance, we advise you to follow the last option, add <TT CLASS="filename" >C:\php</TT > to the <VAR CLASS="varname" >PATH</VAR >, because it will be simpler to upgrade PHP in the future. Read more about how to add your PHP directory to <VAR CLASS="varname" >PATH</VAR > in the <A HREF="#faq.installation.addtopath" >corresponding FAQ entry</A >. </P ><P >
The next step is to set up a valid configuration file for PHP, <TT CLASS="filename" >php.ini</TT >. There are two ini files distributed in the zip file, <TT CLASS="filename" >php.ini-dist</TT > and <TT CLASS="filename" >php.ini-recommended</TT >. We advise you to use <TT CLASS="filename" >php.ini-recommended</TT >, because we optimized the default settings in this file for performance, and security. Read this well documented file carefully because it has changes from <TT CLASS="filename" >php.ini-dist</TT > that will drastically affect your setup. Some examples are <A HREF="#ini.display-errors" >
display_errors</A > being <VAR CLASS="literal" >off</VAR > and <A HREF="#ini.magic-quotes-gpc" >magic_quotes_gpc</A > being <VAR CLASS="literal" >off</VAR >. In addition to reading these, study the <A HREF="#configuration.file" >ini settings</A > and set every element manually yourself. If you would like to achieve the best security, then this is the way for you, although PHP works fine with these default ini files. Copy your chosen ini-file to a directory that PHP is able to find and rename it to <TT CLASS="filename" >php.ini</TT >. PHP searches for <TT CLASS="filename" >php.ini</TT > in the following locations (in order): <P ></P ><UL ><LI ><P >
PHPIniDir directive (Apache 2 module only) </P ></LI ><LI ><P >
<VAR CLASS="literal" >HKEY_LOCAL_MACHINE\SOFTWARE\PHP\IniFilePath</VAR > </P ></LI ><LI ><P >
The <VAR CLASS="varname" >PHPRC</VAR > environment variable </P ></LI ><LI ><P >
Directory of PHP (for CLI), or the web server's directory (for SAPI modules) </P ></LI ><LI ><P >
Windows directory (<TT CLASS="filename" >C:\windows</TT > or <TT CLASS="filename" >C:\winnt</TT >) </P ></LI ></UL > </P ><P >
If you are running Apache 2, the simpler option is to use the PHPIniDir directive (read the <A HREF="#install.windows.apache2" >installation on Apache 2</A > page), otherwise your best option is to set the <VAR CLASS="varname" >PHPRC</VAR > environment variable. This process is explained in the following <A HREF="#faq.installation.phprc" >FAQ entry</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > If you're using NTFS on Windows NT, 2000, XP or 2003, make sure that the user running the web server has read permissions to your <TT CLASS="filename" >php.ini</TT > (e.g. make it readable by Everyone). </P ></BLOCKQUOTE ></DIV ><P >
The following steps are optional: <P ></P ><UL ><LI ><P >
Edit your new <TT CLASS="filename" >php.ini</TT > file. If you plan to use <A HREF="#install.windows.omnihttpd" >OmniHTTPd</A >, do not follow the next step. Set the <A HREF="#ini.doc-root" >doc_root</A > to point to your web servers document_root. For example: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN1198" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" >doc_root = c:\inetpub // for IIS/PWS doc_root = c:\apache\htdocs // for Apache</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></LI ><LI ><P >
Choose the extensions you would like to load when PHP starts. See the section about <A HREF="#install.windows.extensions" >Windows extensions</A >, about how to set up one, and what is already built in. Note that on a new installation it is advisable to first get PHP working and tested without any extensions before enabling them in <TT CLASS="filename" >php.ini</TT >. </P ></LI ><LI ><P >
On PWS and IIS, you can set the <A HREF="#ini.browscap" >browscap</A > configuration setting to point to: <TT CLASS="filename" >c:\windows\system\inetsrv\browscap.ini</TT > on Windows 9x/Me, <TT CLASS="filename" >c:\winnt\system32\inetsrv\browscap.ini</TT > on NT/2000, and <TT CLASS="filename" >c:\windows\system32\inetsrv\browscap.ini</TT > on XP. For an up-to-date <TT CLASS="filename" >browscap.ini</TT >, read the following <A HREF="#faq.obtaining.browscap" >FAQ</A >. </P ></LI ></UL > </P ><P >
PHP is now setup on your system. The next step is to choose a web server, and enable it to run PHP. Choose a webserver from the table of contents. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.activescript" >ActiveScript</A ></H2 ><P >
This section contains notes specific to the ActiveScript installation. </P ><P >
ActiveScript is a windows only SAPI that enables you to use PHP script in any ActiveScript compliant host, like Windows Script Host, ASP/ASP.NET, Windows Script Components or Microsoft Scriptlet control. </P ><P >
As of PHP 5.0.1, ActiveScript has been moved to the <A HREF="http://pecl.php.net" TARGET="_top" >PECL</A > repository. You may download this <ACRONYM CLASS="acronym" >PECL</ACRONYM > extensions <ACRONYM CLASS="acronym" >DLL</ACRONYM > from the <A HREF="http://www.php.net/downloads.php" TARGET="_top" >
PHP Downloads</A > page or at <A HREF="http://snaps.php.net/" TARGET="_top" >
http://snaps.php.net/</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > You should read the <A HREF="#install.windows.manual" >manual installation steps</A > first! </P ></BLOCKQUOTE ></DIV ><P >
After installing PHP, you should download the ActiveScript DLL (<TT CLASS="filename" >php5activescript.dll</TT >) and place it in the main PHP folder (e.g. <TT CLASS="filename" >C:\php</TT >). </P ><P >
After having all the files needed, you must register the DLL on your system. To achieve this, open a Command Prompt window (located in the Start Menu). Then go to your PHP directory by typing something like <VAR CLASS="literal" >cd C:\php</VAR >. To register the DLL just type <VAR CLASS="literal" >regsvr32 php5activescript.dll</VAR >. </P ><P >
To test if ActiveScript is working, create a new file, named <VAR CLASS="literal" >test.wsf</VAR > (the extension is very important) and type: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><job id="test"> <script language="PHPScript"> $WScript->Echo("Hello World!"); </script> </job></PRE ></TD ></TR ></TABLE > Save and double-click on the file. If you receive a little window saying "Hello World!" you're done. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > ActiveScript doesn't use the default <TT CLASS="filename" >php.ini</TT > file. Instead, it will look only in the same directory as the .exe that caused it to load. You should create <TT CLASS="filename" >php-activescript.ini</TT > and place it in that folder, if you wish to load extensions, etc. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.iis" >Microsoft IIS / PWS</A ></H2 ><P >
This section contains notes and hints specific to IIS (Microsoft Internet Information Server). We have included installation instructions for <A HREF="#install.windows.iis.iis3" >PWS/IIS 3</A >, <A HREF="#install.windows.iis.pws4" >PWS 4 or newer</A > and <A HREF="#install.windows.iis.iis4" >IIS 4 or newer</A > versions. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Important for CGI users: </B > Read the <A HREF="#faq.installation.forceredirect" >faq on cgi.force_redirect</A > for important details. This directive needs to be set to <VAR CLASS="literal" >0</VAR >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >By using the CGI setup, your server is open to several possible attacks. Please read our <A HREF="#security.cgi-bin" >CGI security section</A > to learn how to defend yourself from those attacks.</P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.iis.iis3" >Windows and PWS/IIS 3</A ></H3 ><P >
The recommended method for configuring these servers is to use the REG file included with the distribution (<TT CLASS="filename" >pws-php4cgi.reg</TT > in the SAPI folder for PHP 4, or <TT CLASS="filename" >pws-php5cgi.reg</TT > in the main folder for PHP 5). You may want to edit this file and make sure the extensions and PHP install directories match your configuration. Or you can follow the steps below to do it manually. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
These steps involve working directly with the Windows registry. One error here can leave your system in an unstable state. We highly recommend that you back up your registry first. The PHP Development team will not be held responsible if you damage your registry. </P ></TD ></TR ></TABLE ></DIV ><P >
<P ></P ><UL ><LI ><P >
Run Regedit. </P ></LI ><LI ><P >
Navigate to: <VAR CLASS="literal" >HKEY_LOCAL_MACHINE /System /CurrentControlSet /Services /W3Svc /Parameters /ScriptMap</VAR >. </P ></LI ><LI ><P >
On the edit menu select: <VAR CLASS="literal" >New->String Value</VAR >. </P ></LI ><LI ><P >
Type in the extension you wish to use for your php scripts. For example <VAR CLASS="literal" >.php</VAR > </P ></LI ><LI ><P >
Double click on the new string value and enter the path to <TT CLASS="filename" >php.exe</TT > in the value data field. ex: <TT CLASS="filename" >C:\php\php.exe</TT > for PHP 4, or <TT CLASS="filename" >C:\php\php-cgi.exe</TT > for PHP 5. </P ></LI ><LI ><P >
Repeat these steps for each extension you wish to associate with PHP scripts. </P ></LI ></UL > </P ><P >
The following steps do not affect the web server installation and only apply if you want your PHP scripts to be executed when they are run from the command line (ex. run <TT CLASS="filename" >C:\myscripts\test.php</TT >) or by double clicking on them in a directory viewer window. You may wish to skip these steps as you might prefer the PHP files to load into a text editor when you double click on them. </P ><P >
<P ></P ><UL ><LI ><P >
Navigate to: <VAR CLASS="literal" >HKEY_CLASSES_ROOT</VAR > </P ></LI ><LI ><P >
On the edit menu select: <VAR CLASS="literal" >New->Key</VAR >. </P ></LI ><LI ><P >
Name the key to the extension you setup in the previous section. ex: <VAR CLASS="literal" >.php</VAR > </P ></LI ><LI ><P >
Highlight the new key and in the right side pane, double click the "default value" and enter <VAR CLASS="literal" >phpfile</VAR >. </P ></LI ><LI ><P >
Repeat the last step for each extension you set up in the previous section. </P ></LI ><LI ><P >
Now create another <VAR CLASS="literal" >New->Key</VAR > under <VAR CLASS="literal" >HKEY_CLASSES_ROOT</VAR > and name it <VAR CLASS="literal" >phpfile</VAR >. </P ></LI ><LI ><P >
Highlight the new key <VAR CLASS="literal" >phpfile</VAR > and in the right side pane, double click the "default value" and enter <VAR CLASS="literal" >PHP Script</VAR >. </P ></LI ><LI ><P >
Right click on the <VAR CLASS="literal" >phpfile</VAR > key and select <VAR CLASS="literal" >New->Key</VAR >, name it <VAR CLASS="literal" >Shell</VAR >. </P ></LI ><LI ><P >
Right click on the <VAR CLASS="literal" >Shell</VAR > key and select <VAR CLASS="literal" >New->Key</VAR >, name it <VAR CLASS="literal" >open</VAR >. </P ></LI ><LI ><P >
Right click on the <VAR CLASS="literal" >open</VAR > key and select <VAR CLASS="literal" >New->Key</VAR >, name it <VAR CLASS="literal" >command</VAR >. </P ></LI ><LI ><P >
Highlight the new key <VAR CLASS="literal" >command</VAR > and in the right side pane, double click the "default value" and enter the path to <TT CLASS="filename" >php.exe</TT >. ex: <VAR CLASS="literal" >c:\php\php.exe -q %1</VAR >. (don't forget the <VAR CLASS="literal" >%1</VAR >). </P ></LI ><LI ><P >
Exit Regedit. </P ></LI ><LI ><P >
If using PWS on Windows, reboot to reload the registry. </P ></LI ></UL > </P ><P >
PWS and IIS 3 users now have a fully operational system. IIS 3 users can use a nifty <A HREF="http://www.genusa.com/iis/iiscfg.html" TARGET="_top" >tool</A > from Steven Genusa to configure their script maps. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.iis.pws4" >Windows and PWS 4 or newer</A ></H3 ><P >
When installing PHP on Windows with PWS 4 or newer version, you have two options. One to set up the PHP CGI binary, the other is to use the ISAPI module DLL. </P ><P >
If you choose the CGI binary, do the following: <P ></P ><UL ><LI ><P >
Edit the enclosed <TT CLASS="filename" >pws-php4cgi.reg</TT > / <TT CLASS="filename" >pws-php5cgi.reg</TT > file (look into the SAPI folder for PHP 4, or in the main folder for PHP 5) to reflect the location of your <TT CLASS="filename" >php.exe</TT > / <TT CLASS="filename" >php-cgi.exe</TT >. Backslashes should be escaped, for example: <VAR CLASS="literal" >[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w3svc\parameters\Script Map] ".php"="C:\\php\\php.exe"</VAR > (change to <VAR CLASS="literal" >C:\\php\\php-cgi.exe</VAR > if you are using PHP 5) Now merge this registery file into your system; you may do this by double-clicking it. </P ></LI ><LI ><P >
In the PWS Manager, right click on a given directory you want to add PHP support to, and select Properties. Check the 'Execute' checkbox, and confirm. </P ></LI ></UL > </P ><P >
If you choose the ISAPI module, do the following: <P ></P ><UL ><LI ><P >
Edit the enclosed <TT CLASS="filename" >pws-php4isapi.reg</TT > / <TT CLASS="filename" >pws-php5isapi.reg</TT > file (look into the SAPI folder for PHP 4, or in the main folder for PHP 5) to reflect the location of your <TT CLASS="filename" >php4isapi.dll</TT > / <TT CLASS="filename" >php5isapi.dll</TT >. Backslashes should be escaped, for example: <VAR CLASS="literal" >[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w3svc\parameters\Script Map] ".php"="C:\\php\\sapi\\php4isapi.dll"</VAR > (or <VAR CLASS="literal" >C:\\php\\php5isapi.dll</VAR > for PHP 5) Now merge this registery file into your system; you may do this by double-clicking it. </P ></LI ><LI ><P >
In the PWS Manager, right click on a given directory you want to add PHP support to, and select Properties. Check the 'Execute' checkbox, and confirm. </P ></LI ></UL > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.iis.iis4" >Windows NT/2000/XP and IIS 4 or newer</A ></H3 ><P >
To install PHP on an NT/2000/XP Server running IIS 4 or newer, follow these instructions. You have two options to set up PHP, using the CGI binary (<TT CLASS="filename" >php.exe</TT > in PHP 4, or <TT CLASS="filename" >php-cgi.exe</TT > in PHP 5) or with the ISAPI module. </P ><P >
In either case, you need to start the Microsoft Management Console (may appear as 'Internet Services Manager', either in your Windows NT 4.0 Option Pack branch or the Control Panel=>Administrative Tools under Windows 2000/XP). Then right click on your Web server node (this will most probably appear as 'Default Web Server'), and select 'Properties'. </P ><P >
If you want to use the CGI binary, do the following: <P ></P ><UL ><LI ><P >
Under 'Home Directory', 'Virtual Directory', or 'Directory', click on the 'Configuration' button, and then enter the App Mappings tab. </P ></LI ><LI ><P >
Click Add, and in the Executable box, type: <VAR CLASS="literal" >C:\php\php.exe</VAR > for PHP 4 or <VAR CLASS="literal" >C:\php\php-cgi.exe</VAR > for PHP 5 (assuming that you have unziped PHP in <TT CLASS="filename" >c:\php\</TT >). </P ></LI ><LI ><P >
In the Extension box, type the file name extension you want associated with PHP scripts. Leave 'Method exclusions' blank, and check the 'Script engine' checkbox. You may also like to check the 'check that file exists' box - for a small performance penalty, IIS (or PWS) will check that the script file exists and sort out authentication before firing up PHP. This means that you will get sensible 404 style error messages instead of CGI errors complaining that PHP did not output any data. </P ><P >
You must start over from the previous step for each extension you want associated with PHP scripts. <VAR CLASS="literal" >.php</VAR > and <VAR CLASS="literal" >.phtml</VAR > are common, although <VAR CLASS="literal" >.php3</VAR > may be required for legacy applications. </P ></LI ><LI ><P >
Set up the appropriate security. (This is done in Internet Service Manager), and if your NT Server uses NTFS file system, add execute rights for I_USR_ to the directory that contains <TT CLASS="filename" >php.exe</TT > / <TT CLASS="filename" >php-cgi.exe</TT >. </P ></LI ></UL > </P ><P >
To use the ISAPI module, do the following: <P ></P ><UL ><LI ><P >
If you don't want to perform HTTP Authentication using PHP, you can (and should) skip this step. Under ISAPI Filters, add a new ISAPI filter. Use PHP as the filter name, and supply a path to the <TT CLASS="filename" >php4isapi.dll</TT > / <TT CLASS="filename" >php5isapi.dll</TT >. </P ></LI ><LI ><P >
Under 'Home Directory', click on the 'Configuration' button. Add a new entry to the Application Mappings. Use the path to the <TT CLASS="filename" >php4isapi.dll</TT > / <TT CLASS="filename" >php5isapi.dll</TT > as the Executable, supply <VAR CLASS="literal" >.php</VAR > as the extension, leave 'Method exclusions' blank, and check the 'Script engine' checkbox. </P ></LI ><LI ><P >
Stop IIS completely (NET STOP iisadmin) </P ></LI ><LI ><P >
Start IIS again (NET START w3svc) </P ></LI ></UL > </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.apache1" >Apache 1.3.x on Microsft Windows</A ></H2 ><P >
This section contains notes and hints specific to Apache 1.3.x installs of PHP on Microsoft Windows systems. We also have <A HREF="#install.windows.apache2" >instructions and notes for Apache 2 on a separate page</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > You should read the <A HREF="#install.windows.manual" >manual installation steps</A > first! </P ></BLOCKQUOTE ></DIV ><P >
There are two ways to set up PHP to work with Apache 1.3.x on Windows. One is to use the CGI binary (<TT CLASS="filename" >php.exe</TT > for PHP 4 and <TT CLASS="filename" >php-cgi.exe</TT > for PHP 5), the other is to use the Apache module DLL. In either case you need to edit your <TT CLASS="filename" >httpd.conf</TT > to configure Apache to work with PHP, and then restart the server. </P ><P >
It is worth noting here that now the SAPI module has been made more stable under Windows, we recommend it's use above the CGI binary, since it is more transparent and secure. </P ><P >
Although there can be a few variations of configuring PHP under Apache, these are simple enough to be used by the newcomer. Please consult the Apache Documentation for further configuration directives. </P ><P >
After changing the configuration file, remember to restart the server, for example, <B CLASS="command" >NET STOP APACHE</B > followed by <B CLASS="command" >NET START APACHE</B >, if you run Apache as a Windows Service, or use your regular shortcuts. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Beachten Sie bitte, dass Sie bei Pfadangaben in der Apachekonfigurationsdatei unter Windows alle Backslashes, wie z.B. <TT CLASS="filename" >c:\directory\file.ext</TT >, in SchrΣgstriche umwandeln mⁿssen: <TT CLASS="filename" >c:/directory/file.ext</TT >.</P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.apache1.cgi" >Installing as a CGI binary</A ></H3 ><P >
If you unziped the PHP package to <TT CLASS="filename" >C:\php\</TT > as described in the <A HREF="#install.windows.manual" >Manual Installation Steps</A > section, you need to insert these lines to your Apache configuration file to set up the CGI binary: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1426" ></A ><P ><B >Beispiel 6-3. PHP and Apache 1.3.x as CGI</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" >ScriptAlias /php/ "c:/php/" AddType application/x-httpd-php .php # For PHP 4 Action application/x-httpd-php "/php/php.exe" # For PHP 5 Action application/x-httpd-php "/php/php-cgi.exe" # specify the directory where php.ini is SetEnv PHPRC C:/php</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Note that the second line in the list above can be found in the actual versions of <TT CLASS="filename" >httpd.conf</TT >, but it is commented out. Remember also to substitute the <TT CLASS="filename" >c:/php/</TT > for your actual path to PHP. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >By using the CGI setup, your server is open to several possible attacks. Please read our <A HREF="#security.cgi-bin" >CGI security section</A > to learn how to defend yourself from those attacks.</P ></TD ></TR ></TABLE ></DIV ><P >
If you would like to present PHP source files syntax highlighted, there is no such convinient option as with the module version of PHP. If you chose to configure Apache to use PHP as a CGI binary, you will need to use the <A HREF="#function.show-source" ><B CLASS="function" >show_source()</B ></A > function. To do this simply create a PHP script file and add this code: <VAR CLASS="literal" ><?php show_source("original_php_script.php"); ?></VAR >. Substitute <TT CLASS="filename" >original_php_script.php</TT > with the name of the file you wish to show the source of. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.apache1.module" >Installing as an Apache module</A ></H3 ><P >
You should add the following lines to your Apache <TT CLASS="filename" >httpd.conf</TT > file: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1443" ></A ><P ><B >Beispiel 6-4. PHP as an Apache 1.3.x module</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" >AddType application/x-httpd-php .php # For PHP 4 LoadModule php4_module "c:/php/sapi/php4apache.dll" # For PHP 5 LoadModule php5_module "c:/php/php5apache.dll" # specify the directory where php.ini is SetEnv PHPRC C:/php</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
You may find after using the Windows installer for Apache that you need to define the <VAR CLASS="literal" >AddModule</VAR > directive for <TT CLASS="filename" >mod_php4.c</TT >. This is especially important if the <VAR CLASS="literal" >ClearModuleList</VAR > directive is defined, which you will find by scrolling down a few lines. You will see a list of <VAR CLASS="literal" >AddModule</VAR > entries, add the following line <SPAN CLASS="emphasis" ><I CLASS="emphasis" >at the end</I ></SPAN > of the list: <VAR CLASS="literal" >AddModule mod_php4.c</VAR >. For PHP 5, instead use <VAR CLASS="literal" >AddModule mod_php5.c</VAR > </P ><P >
If you would like to use the source code highlighting feature, you need to add the following line to your <TT CLASS="filename" >httpd.conf</TT >: <VAR CLASS="literal" >AddType application/x-httpd-php-source .phps</VAR >. This should be inserted at the same place where you inserted <VAR CLASS="literal" >AddType application/x-httpd-php .php</VAR > above). With this setup, all files served with the <VAR CLASS="literal" >.phps</VAR > extension will be syntax highlighted for the browser. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.apache2" >Apache 2.0.x on Microsoft Windows</A ></H2 ><P >
This section contains notes and hints specific to Apache 2.0.x installs of PHP on Microsoft Windows systems. We also have <A HREF="#install.windows.apache1" >instructions and notes for Apache 1.3.x users on a separate page</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > You should read the <A HREF="#install.windows.manual" >manual installation steps</A > first! </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Verwenden Sie Apache 2.0.x und PHP weder unter Unix noch Windows in einer Produktivumgebung. Fⁿr weitere Informationen und Grⁿnde lesen Sie bitte diesen <A HREF="#faq.installation.apache2" >FAQ Eintrag.</A ></P ></TD ></TR ></TABLE ></DIV ><P >
You are highly encouraged to take a look at the <A HREF="http://httpd.apache.org/docs-2.0/" TARGET="_top" >Apache Documentation</A > to get a basic understanding of the Apache 2.0.x Server. Also consider to read the <A HREF="http://httpd.apache.org/docs-2.0/platform/windows.html" TARGET="_top" >Windows specific notes</A > for Apache 2.0.x before reading on here. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >PHP and Apache 2.0.x compatibility notes: </B > The following versions of PHP are known to work with the most recent version of Apache 2.0.x: <P ></P ><UL COMPACT="COMPACT" ><LI ><SPAN >
PHP 4.3.0 or later available at <A HREF="http://www.php.net/downloads.php" TARGET="_top" >http://www.php.net/downloads.php</A >. </SPAN ></LI ><LI ><SPAN >
the latest stable development version. Get the source code <A HREF="http://snaps.php.net/php4-latest.tar.gz" TARGET="_top" >
http://snaps.php.net/php4-latest.tar.gz</A > or download binaries for Windows <A HREF="http://snaps.php.net/win32/php4-win32-latest.zip" TARGET="_top" >
http://snaps.php.net/win32/php4-win32-latest.zip</A >. </SPAN ></LI ><LI ><SPAN >
a prerelease version downloadable from <A HREF="http://qa.php.net/" TARGET="_top" >http://qa.php.net/</A >. </SPAN ></LI ><LI ><SPAN >
you have always the option to obtain PHP through <A HREF="http://www.php.net/anoncvs.php" TARGET="_top" >anonymous CVS</A >. </SPAN ></LI ></UL > These versions of PHP are compatible to Apache 2.0.40 and later. </P ><P >
Apache 2.0 <VAR CLASS="literal" >SAPI</VAR >-support started with PHP 4.2.0. PHP 4.2.3 works with Apache 2.0.39, don't use any other version of Apache with PHP 4.2.3. However, the recommended setup is to use PHP 4.3.0 or later with the most recent version of Apache2. </P ><P >
All mentioned versions of PHP will work still with Apache 1.3.x. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Apache 2.0.x is designed to run on Windows NT 4.0, Windows 2000 or Windows XP. At this time, support for Windows 9x is incomplete. Apache 2.0.x is not expected to work on those platforms at this time. </P ></TD ></TR ></TABLE ></DIV ><P >
Download the most recent version of <A HREF="http://www.apache.org/" TARGET="_top" >
Apache 2.0.x</A > and a fitting PHP version. Follow the <A HREF="#install.windows.manual" >Manual Installation Steps</A > and come back to go on with the integration of PHP and Apache. </P ><P >
There are two ways to set up PHP to work with Apache 2.0.x on Windows. One is to use the CGI binary the other is to use the Apache module DLL. In either case you need to edit your <TT CLASS="filename" >httpd.conf</TT > to configure Apache to work with PHP and then restart the server. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Beachten Sie bitte, dass Sie bei Pfadangaben in der Apachekonfigurationsdatei unter Windows alle Backslashes, wie z.B. <TT CLASS="filename" >c:\directory\file.ext</TT >, in SchrΣgstriche umwandeln mⁿssen: <TT CLASS="filename" >c:/directory/file.ext</TT >.</P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.apache2.cgi" >Installing as a CGI binary</A ></H3 ><P >
You need to insert these three lines to your Apache <TT CLASS="filename" >httpd.conf</TT > configuration file to set up the CGI binary: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1507" ></A ><P ><B >Beispiel 6-5. PHP and Apache 2.0 as CGI</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" >ScriptAlias /php/ "c:/php/" AddType application/x-httpd-php .php # For PHP 4 Action application/x-httpd-php "/php/php.exe" # For PHP 5 Action application/x-httpd-php "/php/php-cgi.exe"</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >By using the CGI setup, your server is open to several possible attacks. Please read our <A HREF="#security.cgi-bin" >CGI security section</A > to learn how to defend yourself from those attacks.</P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.apache2.module" >Installing as an Apache module</A ></H3 ><P >
You need to insert these two lines to your Apache <TT CLASS="filename" >httpd.conf</TT > configuration file to set up the PHP module for Apache 2.0: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1517" ></A ><P ><B >Beispiel 6-6. PHP and Apache 2.0 as Module</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" ># For PHP 4 do something like this: LoadModule php4_module "c:/php/sapi/php4apache2.dll" AddType application/x-httpd-php .php # For PHP 5 do something like this: LoadModule php5_module "c:/php/php5apache2.dll" AddType application/x-httpd-php .php # configure the path to php.ini PHPIniDir "C:/php"</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Remember to substitute the <TT CLASS="filename" >c:/php/</TT > for your actual path to PHP in the above examples. Take care to use either <TT CLASS="filename" >php4apache2.dll</TT > or <TT CLASS="filename" >php5apache2.dll</TT > in your LoadModule directive and <SPAN CLASS="emphasis" ><I CLASS="emphasis" >not</I ></SPAN > <TT CLASS="filename" >php4apache.dll</TT > or <TT CLASS="filename" >php5apache.dll</TT > as the latter ones are designed to run with <A HREF="#install.windows.apache1" >Apache 1.3.x</A >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Don't mix up your installation with DLL files from <SPAN CLASS="emphasis" ><I CLASS="emphasis" >different PHP versions</I ></SPAN >. You have the only choice to use the DLL's and extensions that ship with your downloaded PHP version. </P ></TD ></TR ></TABLE ></DIV ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.sun" >Sun, iPlanet and Netscape servers on Microsoft Windows</A ></H2 ><P >
This section contains notes and hints specific to Sun Java System Web Server, Sun ONE Web Server, iPlanet and Netscape server installs of PHP on Windows. </P ><P >
From PHP 4.3.3 on you can use PHP scripts with the <A HREF="#ref.nsapi" >NSAPI module</A > to <A HREF="#install.windows.sun.specialpages" >generate custom directory listings and error pages</A >. Additional functions for Apache compatibility are also available. For support in current webservers read the <A HREF="#install.windows.sun.notes" >note about subrequests</A >. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.sun.cgi" >CGI setup on Sun, iPlanet and Netscape servers</A ></H3 ><P >
To install PHP as a CGI handler, do the following: </P ><P ></P ><UL ><LI ><P >
Copy <TT CLASS="filename" >php4ts.dll</TT > to your systemroot (the directory where you installed Windows) </P ></LI ><LI ><P >
Make a file association from the command line. Type the following two lines: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >assoc .php=PHPScript ftype PHPScript=c:\php\php.exe %1 %*</PRE ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
In the Netscape Enterprise Administration Server create a dummy shellcgi directory and remove it just after (this step creates 5 important lines in obj.conf and allow the web server to handle shellcgi scripts). </P ></LI ><LI ><P >
In the Netscape Enterprise Administration Server create a new mime type (Category: type, Content-Type: magnus-internal/shellcgi, File Suffix:php). </P ></LI ><LI ><P >
Do it for each web server instance you want PHP to run </P ></LI ></UL ><P >
More details about setting up PHP as a CGI executable can be found here: <A HREF="http://benoit.noss.free.fr/php/install-php.html" TARGET="_top" >http://benoit.noss.free.fr/php/install-php.html</A > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.sun.nsapi" >NSAPI setup on Sun, iPlanet and Netscape servers</A ></H3 ><P >
To install PHP with NSAPI, do the following: </P ><P ></P ><UL ><LI ><P >
Copy <TT CLASS="filename" >php4ts.dll</TT > to your systemroot (the directory where you installed Windows) </P ></LI ><LI ><P >
Make a file association from the command line. Type the following two lines: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >assoc .php=PHPScript ftype PHPScript=c:\php\php.exe %1 %*</PRE ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
In the Netscape Enterprise Administration Server create a new mime type (Category: type, Content-Type: magnus-internal/x-httpd-php, File Suffix: php). </P ></LI ><LI ><P >
Edit <TT CLASS="filename" >magnus.conf</TT > (for servers >= 6) or <TT CLASS="filename" >obj.conf</TT > (for servers < 6) and add the following: You should place the lines after <VAR CLASS="literal" >mime types init</VAR >. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Init fn="load-modules" funcs="php4_init,php4_execute,php4_auth_trans" shlib="c:/php/sapi/php4nsapi.dll" Init fn="php4_init" LateInit="yes" errorString="Failed to initialise PHP!" [php_ini="c:/path/to/php.ini"]</PRE ></TD ></TR ></TABLE > (PHP >= 4.3.3) The <VAR CLASS="literal" >php_ini</VAR > parameter is optional but with it you can place your <TT CLASS="filename" >php.ini</TT > in your webserver config directory. </P ></LI ><LI ><P >
Configure the default object in <TT CLASS="filename" >obj.conf</TT > (for virtual server classes [Sun Web Server 6.0+] in their <TT CLASS="filename" >vserver.obj.conf</TT >): In the <VAR CLASS="literal" ><Object name="default"></VAR > section, place this line necessarily after all 'ObjectType' and before all 'AddLog' lines: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Service fn="php4_execute" type="magnus-internal/x-httpd-php" [inikey=value inikey=value ...]</PRE ></TD ></TR ></TABLE > (PHP >= 4.3.3) As additional parameters you can add some special <TT CLASS="filename" >php.ini</TT >-values, for example you can set a <VAR CLASS="literal" >docroot="/path/to/docroot"</VAR > specific to the context <VAR CLASS="literal" >php4_execute</VAR > is called. For boolean ini-keys please use 0/1 as value, not <VAR CLASS="literal" >"On","Off",...</VAR > (this will not work correctly), e.g. <VAR CLASS="literal" >zlib.output_compression=1</VAR > instead of <VAR CLASS="literal" >zlib.output_compression="On"</VAR > </P ></LI ><LI ><P >
This is only needed if you want to configure a directory that only consists of PHP scripts (same like a cgi-bin directory): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><Object name="x-httpd-php"> ObjectType fn="force-type" type="magnus-internal/x-httpd-php" Service fn=php4_execute [inikey=value inikey=value ...] </Object></PRE ></TD ></TR ></TABLE > After that you can configure a directory in the Administration server and assign it the style <VAR CLASS="literal" >x-httpd-php</VAR >. All files in it will get executed as PHP. This is nice to hide PHP usage by renaming files to <TT CLASS="filename" >.html</TT >. </P ></LI ><LI ><P >
Restart your web service and apply changes </P ></LI ><LI ><P >
Do it for each web server instance you want PHP to run </P ></LI ></UL ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > More details about setting up PHP as an NSAPI filter can be found here: <A HREF="http://benoit.noss.free.fr/php/install-php4.html" TARGET="_top" >http://benoit.noss.free.fr/php/install-php4.html</A > </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > The stacksize that PHP uses depends on the configuration of the webserver. If you get crashes with very large PHP scripts, it is recommended to raise it with the Admin Server (in the section "MAGNUS EDITOR"). </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.sun.phpini" >CGI environment and recommended modifications in php.ini</A ></H3 ><P >
Important when writing PHP scripts is the fact that Sun JSWS/Sun ONE WS/iPlanet/Netscape is a multithreaded web server. Because of that all requests are running in the same process space (the space of the webserver itself) and this space has only one environment. If you want to get CGI variables like <VAR CLASS="literal" >PATH_INFO</VAR >, <VAR CLASS="literal" >HTTP_HOST</VAR > etc. it is not the correct way to try this in the old PHP 3.x way with <A HREF="#function.getenv" ><B CLASS="function" >getenv()</B ></A > or a similar way (register globals to environment, <VAR CLASS="literal" >$_ENV</VAR >). You would only get the environment of the running webserver without any valid CGI variables! </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Why are there (invalid) CGI variables in the environment? </P ><P >
Answer: This is because you started the webserver process from the admin server which runs the startup script of the webserver, you wanted to start, as a CGI script (a CGI script inside of the admin server!). This is why the environment of the started webserver has some CGI environment variables in it. You can test this by starting the webserver not from the administration server. Use the command line as root user and start it manually - you will see there are no CGI-like environment variables. </P ></BLOCKQUOTE ></DIV ><P >
Simply change your scripts to get CGI variables in the correct way for PHP 4.x by using the superglobal <VAR CLASS="literal" >$_SERVER</VAR >. If you have older scripts which use <VAR CLASS="literal" >$HTTP_HOST</VAR >, etc., you should turn on <VAR CLASS="literal" >register_globals</VAR > in <TT CLASS="filename" >php.ini</TT > and change the variable order too (important: remove <VAR CLASS="literal" >"E"</VAR > from it, because you do not need the environment here): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >variables_order = "GPCS" register_globals = On</PRE ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.sun.specialpages" >Special use for error pages or self-made directory listings (PHP >= 4.3.3)</A ></H3 ><P >
You can use PHP to generate the error pages for <VAR CLASS="literal" >"404 Not Found"</VAR > or similar. Add the following line to the object in <TT CLASS="filename" >obj.conf</TT > for every error page you want to overwrite: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Error fn="php4_execute" code=XXX script="/path/to/script.php" [inikey=value inikey=value...]</PRE ></TD ></TR ></TABLE > where <VAR CLASS="literal" >XXX</VAR > is the HTTP error code. Please delete any other <VAR CLASS="literal" >Error</VAR > directives which could interfere with yours. If you want to place a page for all errors that could exist, leave the <VAR CLASS="literal" >code</VAR > parameter out. Your script can get the HTTP status code with <VAR CLASS="literal" >$_SERVER['ERROR_TYPE']</VAR >. </P ><P >
Another possibility is to generate self-made directory listings. Just create a PHP script which displays a directory listing and replace the corresponding default Service line for <VAR CLASS="literal" >type="magnus-internal/directory"</VAR > in <TT CLASS="filename" >obj.conf</TT > with the following: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Service fn="php4_execute" type="magnus-internal/directory" script="/path/to/script.php" [inikey=value inikey=value...]</PRE ></TD ></TR ></TABLE > For both error and directory listing pages the original URI and translated URI are in the variables <VAR CLASS="literal" >$_SERVER['PATH_INFO']</VAR > and <VAR CLASS="literal" >$_SERVER['PATH_TRANSLATED']</VAR >. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.sun.notes" >Note about <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > and subrequests (PHP >= 4.3.3)</A ></H3 ><P >
The NSAPI module now supports the <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > function (alias: <A HREF="#function.virtual" ><B CLASS="function" >virtual()</B ></A >) to make subrequests on the webserver and insert the result in the webpage. The problem is, that this function uses some undocumented features from the NSAPI library. </P ><P >
Under Unix this is not a problem, because the module automatically looks for the needed functions and uses them if available. If not, <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > is disabled. </P ><P >
Under Windows limitations in the DLL handling need the use of a automatic detection of the most recent <TT CLASS="filename" >ns-httpdXX.dll</TT > file. This is tested for servers till version 6.1. If a newer version of the Sun server is used, the detection fails and <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > is disabled. </P ><P >
If this is the case, try the following: Add the following parameter to <VAR CLASS="literal" >php4_init</VAR > in <TT CLASS="filename" >magnus.conf</TT >/<TT CLASS="filename" >obj.conf</TT >: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Init fn=php4_init ... server_lib="ns-httpdXX.dll"</PRE ></TD ></TR ></TABLE > where <VAR CLASS="literal" >XX</VAR > is the correct DLL version number. To get it, look in the server-root for the correct DLL name. The DLL with the biggest filesize is the right one. </P ><P >
You can check the status by using the <A HREF="#function.phpinfo" ><B CLASS="function" >phpinfo()</B ></A > function. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > But be warned: Support for <A HREF="#function.nsapi-virtual" ><B CLASS="function" >nsapi_virtual()</B ></A > is EXPERIMENTAL!!! </P ></BLOCKQUOTE ></DIV ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.omnihttpd" >OmniHTTPd Server</A ></H2 ><P >
This section contains notes and hints specific to <A HREF="http://www.omnicron.ca/" TARGET="_top" >OmniHTTPd</A > on Windows. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > You should read the <A HREF="#install.windows.manual" >manual installation steps</A > first! </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >By using the CGI setup, your server is open to several possible attacks. Please read our <A HREF="#security.cgi-bin" >CGI security section</A > to learn how to defend yourself from those attacks.</P ></TD ></TR ></TABLE ></DIV ><P >
You need to complete the following steps to make PHP work with OmniHTTPd. This is a CGI executable setup. SAPI is supported by OmniHTTPd, but some tests have shown that it is not so stable to use PHP as an ISAPI module. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Important for CGI users: </B > Read the <A HREF="#faq.installation.forceredirect" >faq on cgi.force_redirect</A > for important details. This directive needs to be set to <VAR CLASS="literal" >0</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
<P ></P ><OL TYPE="1" ><LI ><P >
Install OmniHTTPd server. </P ></LI ><LI ><P >
Right click on the blue OmniHTTPd icon in the system tray and select <VAR CLASS="literal" >Properties</VAR > </P ></LI ><LI ><P >
Click on <VAR CLASS="literal" >Web Server Global Settings</VAR > </P ></LI ><LI ><P >
On the 'External' tab, enter: <VAR CLASS="literal" >virtual = .php | actual = c:\php\php.exe</VAR > (use <TT CLASS="filename" >php-cgi.exe</TT > if installing PHP 5), and use the Add button. </P ></LI ><LI ><P >
On the <VAR CLASS="literal" >Mime</VAR > tab, enter: <VAR CLASS="literal" >virtual = wwwserver/stdcgi | actual = .php</VAR >, and use the Add button. </P ></LI ><LI ><P >
Click <VAR CLASS="literal" >OK</VAR > </P ></LI ></OL > </P ><P >
Repeat steps 2 - 6 for each extension you want to associate with PHP. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Some OmniHTTPd packages come with built in PHP support. You can choose at setup time to do a custom setup, and uncheck the PHP component. We recommend you to use the latest PHP binaries. Some OmniHTTPd servers come with PHP 4 beta distributions, so you should choose not to set up the built in support, but install your own. If the server is already on your machine, use the Replace button in Step 4 and 5 to set the new, correct information. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.sambar" >Sambar Server on Microsoft Windows</A ></H2 ><P >
This section contains notes and hints specific to the <A HREF="http://www.sambar.com/" TARGET="_top" >Sambar Server</A > for Windows. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > You should read the <A HREF="#install.windows.manual" >manual installation steps</A > first! </P ></BLOCKQUOTE ></DIV ><P >
This list describes how to set up the ISAPI module to work with the Sambar server on Windows. </P ><P >
<P ></P ><UL ><LI ><P >
Find the file called <TT CLASS="filename" >mappings.ini</TT > (in the config directory) in the Sambar install directory. </P ></LI ><LI ><P >
Open <TT CLASS="filename" >mappings.ini</TT > and add the following line under <VAR CLASS="literal" >[ISAPI]</VAR >: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1715" ></A ><P ><B >Beispiel 6-7. ISAPI configuration of Sambar</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >#for PHP 4 *.php = c:\php\php4isapi.dll #for PHP 5 *.php = c:\php\php5isapi.dll</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > (This line assumes that PHP was installed in <TT CLASS="filename" >c:\php</TT >.) </P ></LI ><LI ><P >
Now restart the Sambar server for the changes to take effect. </P ></LI ></UL > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.xitami" >Xitami on Microsoft Windows</A ></H2 ><P >
This section contains notes and hints specific to <A HREF="http://www.xitami.com/" TARGET="_top" >Xitami</A > on Windows. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > You should read the <A HREF="#install.windows.manual" >manual installation steps</A > first! </P ></BLOCKQUOTE ></DIV ><P >
This list describes how to set up the PHP CGI binary to work with Xitami on Windows. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Important for CGI users: </B > Read the <A HREF="#faq.installation.forceredirect" >faq on cgi.force_redirect</A > for important details. This directive needs to be set to <VAR CLASS="literal" >0</VAR >. If you want to use <VAR CLASS="literal" >$_SERVER['PHP_SELF']</VAR > you have to enable the <A HREF="#ini.cgi.fix-pathinfo" >cgi.fix_pathinfo</A > directive. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >By using the CGI setup, your server is open to several possible attacks. Please read our <A HREF="#security.cgi-bin" >CGI security section</A > to learn how to defend yourself from those attacks.</P ></TD ></TR ></TABLE ></DIV ><P >
<P ></P ><UL ><LI ><P >
Make sure the webserver is running, and point your browser to xitamis admin console (usually <VAR CLASS="literal" >http://127.0.0.1/admin</VAR >), and click on Configuration. </P ></LI ><LI ><P >
Navigate to the Filters, and put the extension which PHP should parse (i.e. .php) into the field File extensions (.xxx). </P ></LI ><LI ><P >
In Filter command or script put the path and name of your PHP CGI executable i.e. <TT CLASS="filename" >C:\php\php.exe</TT > for PHP 4, or <TT CLASS="filename" >C:\php\php-cgi.exe</TT > for PHP 5. </P ></LI ><LI ><P >
Press the 'Save' icon. </P ></LI ><LI ><P >
Restart the server to reflect changes. </P ></LI ></UL > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.build" >Building from source</A ></H2 ><P >
Before getting started, it is worthwhile answering the question: "Why is building on Windows so hard?" Two reasons come to mind: </P ><P ></P ><OL TYPE="1" ><LI ><P >
Windows does not (yet) enjoy a large community of developers who are willing to freely share their source. As a direct result, the necessary investment in infrastructure required to support such development hasn't been made. By and large, what is available has been made possible by the porting of necessary utilities from Unix. Don't be surprised if some of this heritage shows through from time to time. </P ></LI ><LI ><P >
Pretty much all of the instructions that follow are of the "set and forget" variety. So sit back and try follow the instructions below as faithfully as you can. </P ></LI ></OL ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.build.requirement" >Anforderungen</A ></H3 ><P >
To compile and build PHP you need a Microsoft Development Environment. Microsoft Visual C++ 6.0 is recommended. To extract the downloaded files you need a extraction utility (e.g.: Winzip). If you don't already have an unzip utility, you can get a free version from <A HREF="http://www.info-zip.org/pub/infozip/" TARGET="_top" >InfoZip</A >. </P ><P >
Before you get started, you have to download... </P ><P ></P ><UL ><LI ><P >
..the win32 buildtools from the PHP site at <A HREF="http://www.php.net/extra/win32build.zip" TARGET="_top" >http://www.php.net/extra/win32build.zip</A >. </P ></LI ><LI ><P >
..the source code for the DNS name resolver used by PHP from <A HREF="http://www.php.net/extra/bindlib_w32.zip" TARGET="_top" >
http://www.php.net/extra/bindlib_w32.zip</A >. This is a replacement for the <TT CLASS="filename" >resolv.lib</TT > library included in <TT CLASS="filename" >
win32build.zip</TT >. </P ></LI ><LI ><P >
If you plan to compile PHP as a Apache module you will also need the <A HREF="http://www.apache.org/dist/httpd/" TARGET="_top" >Apache sources</A >. </P ></LI ></UL ><P >
Finally, you are going to need the source to PHP itself. You can get the latest development version using <A HREF="http://www.php.net/anoncvs.php" TARGET="_top" >
anonymous CVS</A >, a <A HREF="http://snaps.php.net/" TARGET="_top" >
snapshot</A > or the most recent released <A HREF="http://www.php.net/downloads.php" TARGET="_top" >
source</A > tarball. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.build.install" >Putting it all together</A ></H3 ><P >
After downloading the required packages you have to extract them in a proper place. <P ></P ><UL ><LI ><P >
Create a working directory where all files end up after extracting, e.g: <TT CLASS="filename" >C:\work</TT >. </P ></LI ><LI ><P >
Create the directory <TT CLASS="filename" >win32build</TT > under your working directory (<TT CLASS="filename" >C:\work</TT >) and unzip <TT CLASS="filename" >
win32build.zip</TT > into it. </P ></LI ><LI ><P >
Create the directory <TT CLASS="filename" >bindlib_w32</TT > under your working directory (<TT CLASS="filename" >C:\work</TT >) and unzip <TT CLASS="filename" >
bindlib_w32.zip</TT > into it. </P ></LI ><LI ><P >
Extract the downloaded PHP source code into your working directory (<TT CLASS="filename" >C:\work</TT >). </P ></LI ></UL > Following this steps your directory structure looks like this: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN1803" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >+--c:\work | | | +--bindlib_w32 | | | | | +--arpa | | | | | +--conf | | | | | +--... | | | +--php-4.x.x | | | | | +--build | | | | | +--... | | | | | +--win32 | | | | | +--... | | | +--win32build | | | | | +--bin | | | | | +--include | | | | | +--lib</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Create the directories <TT CLASS="filename" >c:\usr\local\lib</TT >. Copy <TT CLASS="filename" >bison.simple</TT > from <TT CLASS="filename" >c:\work\win32build\bin </TT > to <TT CLASS="filename" >c:\usr\local\lib</TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <A HREF="http://www.cygwin.com/" TARGET="_top" >Cygwin</A > users may omit the last step. A properly installed Cygwin environment provides the mandatory files <TT CLASS="filename" >bison.simple</TT > and <TT CLASS="filename" >bison.exe</TT >. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.build.configure" >Configure MVC ++</A ></H3 ><P >
The next step is to configure MVC ++ to prepare for compiling. Launch Microsoft Visual C++, and from the menu select Tools => Options. In the dialog, select the directories tab. Sequentially change the dropdown to Executables, Includes, and Library files. Your entries should look like this: <P ></P ><UL ><LI ><P >
Executable files: <TT CLASS="filename" >c:\work\win32build\bin</TT >, Cygwin users: <TT CLASS="filename" >cygwin\bin</TT > </P ></LI ><LI ><P >
Include files: <TT CLASS="filename" >c:\work\win32build\include</TT > </P ></LI ><LI ><P >
Library files: <TT CLASS="filename" >c:\work\win32build\lib</TT > </P ></LI ></UL > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.build.resolvlib" >Build resolv.lib</A ></H3 ><P >
You must build the <TT CLASS="filename" >resolv.lib</TT > library. Decide whether you want to have debug symbols available (bindlib - Win32 Debug) or not (bindlib - Win32 Release). Build the appropriate configuration: <P ></P ><UL ><LI ><P >
For GUI users, launch VC++, and then select File => Open Workspace, navigate to <TT CLASS="filename" >c:\work\bindlib_w32</TT > and select <TT CLASS="filename" >bindlib.dsw</TT >. Then select Build=>Set Active Configuration and select the desired configuration. Finally select Build=>Rebuild All. </P ></LI ><LI ><P >
For command line users, make sure that you either have the C++ environment variables registered, or have run <B CLASS="command" >vcvars.bat</B >, and then execute one of the following commands: </P ><P ></P ><UL ><LI ><P >
<KBD CLASS="userinput" >msdev bindlib.dsp /MAKE "bindlib - Win32 Debug"</KBD > </P ></LI ><LI ><P >
<KBD CLASS="userinput" >msdev bindlib.dsp /MAKE "bindlib - Win32 Release"</KBD > </P ></LI ></UL ></LI ></UL > At this point, you should have a usable <TT CLASS="filename" >resolv.lib</TT > in either your <TT CLASS="filename" >c:\work\bindlib_w32\Debug</TT > or <TT CLASS="filename" >Release</TT > subdirectories. Copy this file into your <TT CLASS="filename" >c:\work\win32build\lib</TT > directory over the file by the same name found in there. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="install.windows.build.compile" >Compiling</A ></H3 ><P >
The best way to get started is to build the CGI version. </P ><P ></P ><UL ><LI ><P >
For GUI users, launch VC++, and then select File => Open Workspace and select <TT CLASS="filename" >c:\work\php-4.x.x\win32\php4ts.dsw </TT >. Then select Build=>Set Active Configuration and select the desired configuration, either <VAR CLASS="literal" >php4ts - Win32 Debug_TS</VAR > or <VAR CLASS="literal" >php4ts - Win32 Release_TS</VAR >. Finally select Build=>Rebuild All. </P ></LI ><LI ><P >
For command line users, make sure that you either have the C++ environment variables registered, or have run <B CLASS="command" >vcvars.bat</B >, and then execute one of the following commands from the <TT CLASS="filename" >c:\work\php-4.x.x\win32 </TT > directory: </P ><P ></P ><UL ><LI ><P >
<KBD CLASS="userinput" >msdev php4ts.dsp /MAKE "php4ts - Win32 Debug_TS"</KBD > </P ></LI ><LI ><P >
<KBD CLASS="userinput" >msdev php4ts.dsp /MAKE "php4ts - Win32 Release_TS"</KBD > </P ></LI ><LI ><P >
At this point, you should have a usable <TT CLASS="filename" >php.exe</TT > in either your <TT CLASS="filename" >c:\work\php-4.x.x.\Debug_TS</TT > or <TT CLASS="filename" >Release_TS</TT > subdirectories. </P ></LI ></UL ></LI ></UL ><P >
It is possible to do minor customization to the build process by editing the <TT CLASS="filename" >main/config.win32.h</TT > file. For example you can change the default location of <TT CLASS="filename" >php.ini</TT >, the built-in extensions, and the default location for your extensions. </P ><P >
Next you may want to build the CLI version which is designed to use <A HREF="#features.commandline" >PHP from the command line</A >. The steps are the same as for building the CGI version, except you have to select the <VAR CLASS="literal" >php4ts_cli - Win32 Debug_TS</VAR > or <VAR CLASS="literal" >php4ts_cli - Win32 Release_TS</VAR > project file. After a successful compiling run you will find the <TT CLASS="filename" >php.exe</TT > in either the directory <TT CLASS="filename" >Release_TS\cli\</TT > or <TT CLASS="filename" >Debug_TS\cli\</TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > If you want to use PEAR and the comfortable command line installer, the CLI-SAPI is mandatory. For more information about PEAR and the installer read the documentation at the <A HREF="http://pear.php.net/manual/" TARGET="_top" >
PEAR</A > website. </P ></BLOCKQUOTE ></DIV ><P >
In order to build the SAPI module (<TT CLASS="filename" >php4isapi.dll</TT >) for integrating PHP with Microsoft IIS, set your active configuration to <TT CLASS="filename" >php4isapi-whatever-config</TT > and build the desired dll. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.windows.extensions" >Installation of extensions on Windows</A ></H2 ><P >
After installing PHP and a webserver on Windows, you will probably want to install some extensions for added functionality. You can choose which extensions you would like to load when PHP starts by modifying your <TT CLASS="filename" >php.ini</TT >. You can also load a module dynamically in your script using <A HREF="#function.dl" ><B CLASS="function" >dl()</B ></A >. </P ><P >
The DLLs for PHP extensions are prefixed with <VAR CLASS="literal" >php_</VAR >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In PHP 4.3.1 BCMath, Calendar, COM, Ctype, FTP, MySQL, ODBC, Overload, PCRE, Session, Tokenizer, WDDX, XML and Zlib support is <SPAN CLASS="emphasis" ><I CLASS="emphasis" >built in</I ></SPAN >. You don't need to load any additional extensions in order to use these functions. See your distributions <TT CLASS="filename" >README.txt</TT > or <TT CLASS="filename" >install.txt</TT > or <A HREF="#install.windows.extensions.overview" >this table</A > for a list of built in modules. </P ></BLOCKQUOTE ></DIV ><P >
The default location PHP searches for extensions is <TT CLASS="filename" >c:\php4\extensions</TT > in PHP 4 and <TT CLASS="filename" >c:\php5</TT > in PHP 5. To change this setting to reflect your setup of PHP edit your <TT CLASS="filename" >php.ini</TT > file: <P ></P ><UL ><LI ><P >
You will need to change the <A HREF="#ini.extension-dir" >extension_dir</A > setting to point to the directory where your extensions lives, or where you have placed your <TT CLASS="filename" >php_*.dll</TT > files. Please do not forget the last backslash. For example: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN1914" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" >extension_dir = c:/php/extensions/</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></LI ><LI ><P >
Enable the extension(s) in <TT CLASS="filename" >php.ini</TT > you want to use by uncommenting the <VAR CLASS="literal" >extension=php_*.dll</VAR > lines in <TT CLASS="filename" >php.ini</TT >. This is done by deleting the leading ; form the extension you want to load. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1921" ></A ><P ><B >Beispiel 6-8. Enable <A HREF="#ref.bzip2" >Bzip2</A > extension for PHP-Windows</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" >// change the following line from ... ;extension=php_bz2.dll // ... to extension=php_bz2.dll</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
Some of the extensions need extra DLLs to work. Couple of them can be found in the distribution package, in the <TT CLASS="filename" >C:\php\dlls\</TT > folder in PHP 4 or in the main folder in PHP 5, but some, for example Oracle (<TT CLASS="filename" >php_oci8.dll</TT >) require DLLs which are not bundled with the distribution package. If you are installing PHP 4, copy the bundled DLLs from <TT CLASS="filename" >C:\php\dlls</TT > folder to the main <TT CLASS="filename" >C:\php</TT > folder. Don't forget to include <TT CLASS="filename" >C:\php</TT > in the system <VAR CLASS="varname" >PATH</VAR > (this process is explained in a separate <A HREF="#faq.installation.addtopath" >FAQ entry</A >). </P ></LI ></UL > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > If you are running a server module version of PHP remember to restart your webserver to reflect your changes to <TT CLASS="filename" >php.ini</TT >. </P ></BLOCKQUOTE ></DIV > </P ><P >
The following table describes some of the extensions available and required additional dlls. <DIV CLASS="table" ><A NAME="install.windows.extensions.overview" ></A ><P ><B >Tabelle 6-1. PHP Extensions</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Extension</TH ><TH >Description</TH ><TH >Notes</TH ></TR ></THEAD ><TBODY ><TR ><TD >php_bz2.dll</TD ><TD ><A HREF="#ref.bzip2" >bzip2</A > compression functions</TD ><TD >None</TD ></TR ><TR ><TD >php_calendar.dll</TD ><TD ><A HREF="#ref.calendar" >Calendar</A > conversion functions</TD ><TD >Built in since PHP 4.0.3</TD ></TR ><TR ><TD >php_cpdf.dll</TD ><TD ><A HREF="#ref.cpdf" >ClibPDF</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_crack.dll</TD ><TD ><A HREF="#ref.crack" >Crack</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_ctype.dll</TD ><TD ><A HREF="#ref.ctype" >ctype</A > family functions</TD ><TD >Built in since PHP 4.3.0</TD ></TR ><TR ><TD >php_curl.dll</TD ><TD ><A HREF="#ref.curl" >CURL</A >, Client URL library functions</TD ><TD >Requires: <TT CLASS="filename" >libeay32.dll</TT >, <TT CLASS="filename" >ssleay32.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_cybercash.dll</TD ><TD ><A HREF="#ref.cybercash" >Cybercash</A > payment functions</TD ><TD >PHP <= 4.2.0</TD ></TR ><TR ><TD >php_db.dll</TD ><TD ><A HREF="#ref.dbm" >DBM</A > functions</TD ><TD >Deprecated. Use DBA instead (<TT CLASS="filename" >php_dba.dll</TT >)</TD ></TR ><TR ><TD >php_dba.dll</TD ><TD ><A HREF="#ref.dba" >DBA</A >: DataBase (dbm-style) Abstraction layer functions</TD ><TD >None</TD ></TR ><TR ><TD >php_dbase.dll</TD ><TD ><A HREF="#ref.dbase" >dBase</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_dbx.dll</TD ><TD ><A HREF="#ref.dbx" >dbx</A > functions</TD ><TD > </TD ></TR ><TR ><TD >php_domxml.dll</TD ><TD ><A HREF="#ref.domxml" >DOM XML</A > functions</TD ><TD >
PHP <= 4.2.0 requires: <TT CLASS="filename" >libxml2.dll</TT > (bundled) PHP >= 4.3.0 requires: <TT CLASS="filename" >iconv.dll</TT > (bundled) </TD ></TR ><TR ><TD >php_dotnet.dll</TD ><TD ><A HREF="#ref.dotnet" >.NET</A > functions</TD ><TD >PHP <= 4.1.1</TD ></TR ><TR ><TD >php_exif.dll</TD ><TD ><A HREF="#function.read-exif-data" >Read EXIF</A > headers from JPEG</TD ><TD >None</TD ></TR ><TR ><TD >php_fbsql.dll</TD ><TD ><A HREF="#ref.fbsql" >FrontBase</A > functions</TD ><TD >PHP <= 4.2.0</TD ></TR ><TR ><TD >php_fdf.dll</TD ><TD ><A HREF="#ref.fdf" >FDF</A >: Forms Data Format functions.</TD ><TD >Requires: <TT CLASS="filename" >fdftk.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_filepro.dll</TD ><TD ><A HREF="#ref.filepro" >filePro</A > functions</TD ><TD >Read-only access</TD ></TR ><TR ><TD >php_ftp.dll</TD ><TD ><A HREF="#ref.ftp" >FTP</A > functions</TD ><TD >Built-in since PHP 4.0.3</TD ></TR ><TR ><TD >php_gd.dll</TD ><TD ><A HREF="#ref.image" >GD</A > library image functions</TD ><TD >
Removed in PHP 4.3.2. Also note that truecolor functions are not available in GD1, instead, use <TT CLASS="filename" >php_gd2.dll</TT >. </TD ></TR ><TR ><TD >php_gd2.dll</TD ><TD ><A HREF="#ref.image" >GD</A > library image functions</TD ><TD >GD2</TD ></TR ><TR ><TD >php_gettext.dll</TD ><TD ><A HREF="#ref.gettext" >Gettext</A > functions</TD ><TD >
PHP <= 4.2.0 requires <TT CLASS="filename" >gnu_gettext.dll</TT > (bundled), PHP >= 4.2.3 requires <TT CLASS="filename" >libintl-1.dll</TT >, <TT CLASS="filename" >iconv.dll</TT > (bundled). </TD ></TR ><TR ><TD >php_hyperwave.dll</TD ><TD ><A HREF="#ref.hw" >HyperWave</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_iconv.dll</TD ><TD ><A HREF="#ref.iconv" >ICONV</A > characterset conversion</TD ><TD >Requires: <TT CLASS="filename" >iconv-1.3.dll</TT > (bundled), PHP >=4.2.1 <TT CLASS="filename" >iconv.dll</TT ></TD ></TR ><TR ><TD >php_ifx.dll</TD ><TD ><A HREF="#ref.ifx" >Informix</A > functions</TD ><TD >Requires: Informix libraries</TD ></TR ><TR ><TD >php_iisfunc.dll</TD ><TD >IIS management functions</TD ><TD >None</TD ></TR ><TR ><TD >php_imap.dll</TD ><TD ><A HREF="#ref.imap" >IMAP</A > POP3 and NNTP functions</TD ><TD >None</TD ></TR ><TR ><TD >php_ingres.dll</TD ><TD ><A HREF="#ref.ingres" >Ingres II</A > functions</TD ><TD >Requires: Ingres II libraries</TD ></TR ><TR ><TD >php_interbase.dll</TD ><TD ><A HREF="#ref.ibase" >InterBase</A > functions</TD ><TD >Requires: <TT CLASS="filename" >gds32.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_java.dll</TD ><TD ><A HREF="#ref.java" >Java</A > functions</TD ><TD >PHP <= 4.0.6 requires: <TT CLASS="filename" >jvm.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_ldap.dll</TD ><TD ><A HREF="#ref.ldap" >LDAP</A > functions</TD ><TD >
PHP <= 4.2.0 requires <TT CLASS="filename" >libsasl.dll</TT > (bundled), PHP >= 4.3.0 requires <TT CLASS="filename" >libeay32.dll</TT >, <TT CLASS="filename" >ssleay32.dll</TT > (bundled) </TD ></TR ><TR ><TD >php_mbstring.dll</TD ><TD ><A HREF="#ref.mbstring" >Multi-Byte String</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_mcrypt.dll</TD ><TD ><A HREF="#ref.mcrypt" >Mcrypt Encryption</A > functions</TD ><TD >Requires: <TT CLASS="filename" >libmcrypt.dll</TT ></TD ></TR ><TR ><TD >php_mhash.dll</TD ><TD ><A HREF="#ref.mhash" >Mhash</A > functions</TD ><TD >PHP >= 4.3.0 requires: <TT CLASS="filename" >libmhash.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_mime_magic.dll</TD ><TD ><A HREF="#ref.mime-magic" >Mimetype</A > functions</TD ><TD >Requires: <TT CLASS="filename" >magic.mime</TT > (bundled)</TD ></TR ><TR ><TD >php_ming.dll</TD ><TD ><A HREF="#ref.ming" >Ming</A > functions for Flash</TD ><TD >None</TD ></TR ><TR ><TD >php_msql.dll</TD ><TD ><A HREF="#ref.msql" >mSQL</A > functions</TD ><TD >Requires: <TT CLASS="filename" >msql.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_mssql.dll</TD ><TD ><A HREF="#ref.mssql" >MSSQL</A > functions</TD ><TD >Requires: <TT CLASS="filename" >ntwdblib.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_mysql.dll</TD ><TD ><A HREF="#ref.mysql" >MySQL</A > functions</TD ><TD >PHP >= 5.0.0, requires <TT CLASS="filename" >libmysql.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_mysqli.dll</TD ><TD ><A HREF="#ref.mysqli" >MySQLi</A > functions</TD ><TD >PHP >= 5.0.0, requires <TT CLASS="filename" >libmysqli.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_oci8.dll</TD ><TD ><A HREF="#ref.oci8" >Oracle 8</A > functions</TD ><TD >Requires: Oracle 8.1+ client libraries</TD ></TR ><TR ><TD >php_openssl.dll</TD ><TD ><A HREF="#ref.openssl" >OpenSSL</A > functions</TD ><TD >Requires: <TT CLASS="filename" >libeay32.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_oracle.dll</TD ><TD ><A HREF="#ref.oracle" >Oracle</A > functions</TD ><TD >Requires: Oracle 7 client libraries</TD ></TR ><TR ><TD >php_overload.dll</TD ><TD ><A HREF="#ref.overload" >Object overloading</A > functions</TD ><TD >Built in since PHP 4.3.0</TD ></TR ><TR ><TD >php_pdf.dll</TD ><TD ><A HREF="#ref.pdf" >PDF</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_pgsql.dll</TD ><TD ><A HREF="#ref.pgsql" >PostgreSQL</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_printer.dll</TD ><TD ><A HREF="#ref.printer" >Printer</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_shmop.dll</TD ><TD ><A HREF="#ref.shmop" >Shared Memory</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_snmp.dll</TD ><TD ><A HREF="#ref.snmp" >SNMP</A > get and walk functions</TD ><TD >NT only!</TD ></TR ><TR ><TD >php_soap.dll</TD ><TD ><A HREF="#ref.soap" >SOAP</A > functions</TD ><TD >PHP >= 5.0.0</TD ></TR ><TR ><TD >php_sockets.dll</TD ><TD ><A HREF="#ref.sockets" >Socket</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_sybase_ct.dll</TD ><TD ><A HREF="#ref.sybase" >Sybase</A > functions</TD ><TD >Requires: Sybase client libraries</TD ></TR ><TR ><TD >php_tidy.dll</TD ><TD ><A HREF="#ref.tidy" >Tidy</A > functions</TD ><TD >PHP >= 5.0.0</TD ></TR ><TR ><TD >php_tokenizer.dll</TD ><TD ><A HREF="#ref.tokenizer" >Tokenizer</A > functions</TD ><TD >Built in since PHP 4.3.0</TD ></TR ><TR ><TD >php_w32api.dll</TD ><TD ><A HREF="#ref.w32api" >W32api</A > functions</TD ><TD >None</TD ></TR ><TR ><TD >php_xmlrpc.dll</TD ><TD ><A HREF="#ref.xmlrpc" >XML-RPC</A > functions</TD ><TD >PHP >= 4.2.1 requires: <TT CLASS="filename" >iconv.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_xslt.dll</TD ><TD ><A HREF="#ref.xslt" >XSLT</A > functions</TD ><TD >
PHP <= 4.2.0 requires <TT CLASS="filename" >sablot.dll</TT >, <TT CLASS="filename" >expat.dll</TT > (bundled). PHP >= 4.2.1 requires <TT CLASS="filename" >sablot.dll</TT >, <TT CLASS="filename" >expat.dll</TT >, <TT CLASS="filename" >iconv.dll</TT > (bundled). </TD ></TR ><TR ><TD >php_yaz.dll</TD ><TD ><A HREF="#ref.yaz" >YAZ</A > functions</TD ><TD >Requires: <TT CLASS="filename" >yaz.dll</TT > (bundled)</TD ></TR ><TR ><TD >php_zip.dll</TD ><TD ><A HREF="#ref.zip" >Zip File</A > functions</TD ><TD >Read only access</TD ></TR ><TR ><TD >php_zlib.dll</TD ><TD ><A HREF="#ref.zlib" >ZLib</A > compression functions</TD ><TD >Built in since PHP 4.3.0</TD ></TR ></TBODY ></TABLE ></DIV > </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="install.problems" >Kapitel 7. Problems?</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="install.problems.faq" >Read the FAQ</A ></H2 ><P >
Some problems are more common than others. The most common ones are listed in the <A HREF="#faq" >PHP FAQ</A >, part of this manual. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.problems.support" >Other problems</A ></H2 ><P >
If you are still stuck, someone on the PHP installation mailing list may be able to help you. You should check out the archive first, in case someone already answered someone else who had the same problem as you. The archives are available from the support page on <A HREF="http://www.php.net/support.php" TARGET="_top" >http://www.php.net/support.php</A >. To subscribe to the PHP installation mailing list, send an empty mail to <A HREF="mailto:php-install-subscribe@lists.php.net" TARGET="_top" >php-install-subscribe@lists.php.net</A >. The mailing list address is <A HREF="mailto:php-install@lists.php.net" TARGET="_top" >php-install@lists.php.net</A >. </P ><P >
If you want to get help on the mailing list, please try to be precise and give the necessary details about your environment (which operating system, what PHP version, what web server, if you are running PHP as CGI or a server module, <A HREF="#ini.safe-mode" >Safe Mode</A >, etc...), and preferably enough code to make others able to reproduce and test your problem. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="install.problems.bugs" >Bug reports</A ></H2 ><P >
If you think you have found a bug in PHP, please report it. The PHP developers probably don't know about it, and unless you report it, chances are it won't be fixed. You can report bugs using the bug-tracking system at <A HREF="http://bugs.php.net/" TARGET="_top" >http://bugs.php.net/</A >. Please do not send bug reports in mailing list or personal letters. The bug system is also suitable to submit feature requests. </P ><P >
Read the <A HREF="http://bugs.php.net/how-to-report.php" TARGET="_top" >How to report a bug</A > document before submitting any bug reports! </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="configuration" >Kapitel 8. Runtime Configuration</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="configuration.file" >The configuration file</A ></H2 ><P >
The configuration file (called <TT CLASS="filename" >php3.ini</TT > in PHP 3, and simply <TT CLASS="filename" >php.ini</TT > as of PHP 4) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the <ACRONYM CLASS="acronym" >CGI</ACRONYM > and <ACRONYM CLASS="acronym" >CLI</ACRONYM > version, it happens on every invocation. </P ><P >
The default location of <TT CLASS="filename" >php.ini</TT > is a compile time option (see the <A HREF="#faq.installation.phpini" >FAQ</A > entry), but can be changed for the <ACRONYM CLASS="acronym" >CGI</ACRONYM > and <ACRONYM CLASS="acronym" >CLI</ACRONYM > version with the <VAR CLASS="literal" >-c</VAR > command line switch, see the chapter about using PHP from the <A HREF="#features.commandline" >command line</A >. You can also use the environment variable <VAR CLASS="varname" >PHPRC</VAR > for an additional path to search for a <TT CLASS="filename" >php.ini</TT > file. </P ><P >
If <TT CLASS="filename" >php-SAPI.ini</TT > exists (where SAPI is used SAPI, so the filename is e.g. <TT CLASS="filename" >php-cli.ini</TT > or <TT CLASS="filename" >php-apache.ini</TT >), it's used instead of <TT CLASS="filename" >php.ini</TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > The Apache web server changes the directory to root at startup causing PHP to attempt to read <TT CLASS="filename" >php.ini</TT > from the root filesystem if it exists. </P ></BLOCKQUOTE ></DIV ><P >
The <TT CLASS="filename" >php.ini</TT > directives handled by extensions are documented respectively on the pages of the extensions themselfs. The <A HREF="#ini" >list of the core directives</A > is available in the appendix. Probably not all the PHP directives are documented in the manual though. For a completel list of directives available in your PHP version, please read your well commented <TT CLASS="filename" >php.ini</TT > file. Alternatively, you may find the <A HREF="http://cvs.php.net/co.php/php-src/php.ini-dist" TARGET="_top" >the latest <TT CLASS="filename" >php.ini</TT ></A > from CVS helpful too. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2326" ></A ><P ><B >Beispiel 8-1. <TT CLASS="filename" >php.ini</TT > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" >; any text on a line after an unquoted semicolon (;) is ignored [php] ; section markers (text within square brackets) are also ignored ; Boolean values can be set to either: ; true, on, yes ; or false, off, no, none register_globals = off track_errors = yes ; you can enclose strings in double-quotes include_path = ".:/usr/local/lib/php" ; backslashes are treated the same as any other character include_path = ".;c:\php\lib"</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="configuration.changes" >How to change configuration settings</A ></H2 ><DIV CLASS="sect2" ><H3 CLASS="sect2" ><A NAME="configuration.changes.apache" >Running PHP as an Apache module</A ></H3 ><P >
When using PHP as an Apache module, you can also change the configuration settings using directives in Apache configuration files (e.g. <TT CLASS="filename" >httpd.conf</TT >) and <TT CLASS="filename" >.htaccess</TT > files. You will need "AllowOverride Options" or "AllowOverride All" privileges to do so. </P ><P >
With PHP 4 and PHP 5, there are several Apache directives that allow you to change the PHP configuration from within the Apache configuration files. For a listing of which directives are <TT CLASS="constant" ><B >PHP_INI_ALL</B ></TT >, <TT CLASS="constant" ><B >PHP_INI_PERDIR</B ></TT >, or <TT CLASS="constant" ><B >PHP_INI_SYSTEM</B ></TT >, have a look at the table found within the <A HREF="#function.ini-set" ><B CLASS="function" >ini_set()</B ></A > documentation. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > With PHP 3, there are Apache directives that correspond to each configuration setting in the <TT CLASS="filename" >php3.ini</TT > name, except the name is prefixed by "php3_". </P ></BLOCKQUOTE ></DIV ><P >
<P ></P ><DIV CLASS="variablelist" ><DL ><DT ><SPAN CLASS="systemitem" >php_value</SPAN > <VAR CLASS="parameter" >name</VAR > <VAR CLASS="parameter" >value</VAR ></DT ><DD ><P >
Sets the value of the specified directive. Can be used only with <TT CLASS="constant" ><B >PHP_INI_ALL</B ></TT > and <TT CLASS="constant" ><B >PHP_INI_PERDIR</B ></TT > type directives. To clear a previously set value use <VAR CLASS="literal" >none</VAR > as the value. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Don't use <SPAN CLASS="systemitem" >php_value</SPAN > to set boolean values. <SPAN CLASS="systemitem" >php_flag</SPAN > (see below) should be used instead. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><SPAN CLASS="systemitem" >php_flag</SPAN > <VAR CLASS="parameter" >name</VAR > <VAR CLASS="parameter" >on|off</VAR ></DT ><DD ><P >
Used to set a boolean configuration directive. Can be used only with <TT CLASS="constant" ><B >PHP_INI_ALL</B ></TT > and <TT CLASS="constant" ><B >PHP_INI_PERDIR</B ></TT > type directives. </P ></DD ><DT ><SPAN CLASS="systemitem" >php_admin_value</SPAN > <VAR CLASS="parameter" >name</VAR > <VAR CLASS="parameter" >value</VAR ></DT ><DD ><P >
Sets the value of the specified directive. This <SPAN CLASS="emphasis" ><I CLASS="emphasis" >can not be used</I ></SPAN > in <TT CLASS="filename" >.htaccess</TT > files. Any directive type set with <SPAN CLASS="systemitem" >php_admin_value</SPAN > can not be overridden by <TT CLASS="filename" >.htaccess</TT > or virtualhost directives. To clear a previously set value use <VAR CLASS="literal" >none</VAR > as the value. </P ></DD ><DT ><SPAN CLASS="systemitem" >php_admin_flag</SPAN > <VAR CLASS="parameter" >name</VAR > <VAR CLASS="parameter" >on|off</VAR ></DT ><DD ><P >
Used to set a boolean configuration directive. This <SPAN CLASS="emphasis" ><I CLASS="emphasis" >can not be used</I ></SPAN > in <TT CLASS="filename" >.htaccess</TT > files. Any directive type set with <SPAN CLASS="systemitem" >php_admin_flag</SPAN > can not be overridden by <TT CLASS="filename" >.htaccess</TT > or virtualhost directives. </P ></DD ></DL ></DIV > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2394" ></A ><P ><B >Beispiel 8-2. Apache configuration example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" ><IfModule mod_php5.c> php_value include_path ".:/usr/local/lib/php" php_admin_flag safe_mode on </IfModule> <IfModule mod_php4.c> php_value include_path ".:/usr/local/lib/php" php_admin_flag safe_mode on </IfModule> <IfModule mod_php3.c> php3_include_path ".:/usr/local/lib/php" php3_safe_mode on </IfModule></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
PHP constants do not exist outside of PHP. For example, in <TT CLASS="filename" >httpd.conf</TT > you can not use PHP constants such as <TT CLASS="constant" ><B >E_ALL</B ></TT > or <TT CLASS="constant" ><B >E_NOTICE</B ></TT > to set the <A HREF="#ini.error-reporting" >error_reporting</A > directive as they will have no meaning and will evaluate to <SPAN CLASS="emphasis" ><I CLASS="emphasis" >0</I ></SPAN >. Use the associated bitmask values instead. These constants can be used in <TT CLASS="filename" >php.ini</TT > </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="configuration.changes.windows" >Changing PHP configuration via the Windows registry</A ></H3 ><P >
When running PHP on Windows, the configuration values can be modified on a per-directory basis using the Windows registry. The configuration values are stored in the registry key <VAR CLASS="literal" >HKLM\SOFTWARE\PHP\Per Directory Values</VAR >, in the sub-keys corresponding to the path names. For example, configuration values for the directory <VAR CLASS="literal" >c:\inetpub\wwwroot</VAR > would be stored in the key <VAR CLASS="literal" >HKLM\SOFTWARE\PHP\Per Directory Values\c\inetpub\wwwroot</VAR >. The settings for the directory would be active for any script running from this directory or any subdirectory of it. The values under the key should have the name of the PHP configuration directive and the string value. PHP constants in the values are not parsed. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="configuration.changes.other" >Other interfaces to PHP</A ></H3 ><P >
Regardless of how you run PHP, you can change certain values at runtime of your scripts through <A HREF="#function.ini-set" ><B CLASS="function" >ini_set()</B ></A >. See the documentation on the <A HREF="#function.ini-set" ><B CLASS="function" >ini_set()</B ></A > page for more information. </P ><P >
If you are interested in a complete list of configuration settings on your system with their current values, you can execute the <A HREF="#function.phpinfo" ><B CLASS="function" >phpinfo()</B ></A > function, and review the resulting page. You can also access the values of individual configuration directives at runtime using <A HREF="#function.ini-get" ><B CLASS="function" >ini_get()</B ></A > or <A HREF="#function.get-cfg-var" ><B CLASS="function" >get_cfg_var()</B ></A >. </P ></DIV ></DIV ></DIV ></DIV ><DIV CLASS="PART" ><A NAME="langref" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >III. Sprachreferenz</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT >9. <A HREF="#language.basic-syntax" >Grundlagen der Syntax</A ></DT ><DT >10. <A HREF="#language.types" >Typen</A ></DT ><DT >11. <A HREF="#language.variables" >Variablen</A ></DT ><DT >12. <A HREF="#language.constants" >Konstanten</A ></DT ><DT >13. <A HREF="#language.expressions" >Ausdrⁿcke</A ></DT ><DT >14. <A HREF="#language.operators" >Operatoren</A ></DT ><DT >15. <A HREF="#language.control-structures" >Kontroll-Strukturen</A ></DT ><DT >16. <A HREF="#functions" >Funktionen</A ></DT ><DT >17. <A HREF="#language.oop" >Klassen und Objekte</A ></DT ><DT >18. <A HREF="#language.oop5" >Classes and Objects (PHP 5)</A ></DT ><DT >19. <A HREF="#language.references" >Referenzen in PHP</A ></DT ></DL ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.basic-syntax" >Kapitel 9. Grundlagen der Syntax</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="language.basic-syntax.phpmode" >Den HTML-Bereich der Datei verlassen</A ></H2 ><P >
WΣhrend des Parsens einer Datei geht PHP den Text solange einfach durch, bis einer der speziellen Tags gefunden wird, der PHP mitteilt, dass ab nun mit der Interpretation des Textes als PHP Code zu beginnen ist. Der Parser fⁿhrt nun den Code solange aus, bis er auf einen schlie▀enden PHP Tag st÷▀t, welcher dem Parser mitteilt, den Text ab hier wieder nur einfach durchzugehen. Das ist der Mechanismus der es erlaubt, PHP Code in HTML einzubinden: alles au▀erhalb der PHP Tags wird einfach alleine gelassen, wΣhrend alles innerhalb dieser Tags als Code geparsed wird. </P ><P >
Es gibt vier Arten von Tags, welche die Bl÷cke mit PHP Code kennzeichnen. Davon sind nur zwei immer verfⁿgbar (<?php. . .?> und <script language="php">. . .</script>), die anderen k÷nnen in der Konfigurationsdatei <TT CLASS="filename" >php.ini</TT > aktiviert bzw. deaktiviert werden. Die Tags in der Kurzform bzw. im ASP-Stil m÷gen zwar praktisch erscheinen, sind jedoch nicht so portabel wie die lΣngeren Versionen. Wenn Sie des Weiteren beabsichtigen, PHP Code in XML oder XHTML einzubinden, werden Sie die XML-konformen <?php. . .?> verwenden mⁿssen. </P ><P >
Die von PHP unterstⁿtzten Tags sind: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2431" ></A ><P ><B >Beispiel 9-1. M÷glichkeiten, den HTML-Bereich zu verlassen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >1. <?php echo("In XHTML- oder XML-konformen Dokumenten benutzen Sie diese Syntax\n"); ?> 2. <? echo ("Die einfachste M÷glichkeit: eine SGML-┤processing instruction┤\n"); ?> <?= expression ?> Dies ist eine Abkⁿrzung fⁿr "<? echo expression ?>" 3. <script language="php"> echo ("manche Editoren(wie FrontPage) m÷gen keine ┤processing instructions┤"); </script> 4. <% echo ("Optional k÷nnen Sie auch Tags im ASP-Stil verwenden"); %> <%= $variable; # Das ist ein Abkⁿrzung fuer "<% echo . . ." %></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Die erste Methode (<?php. . .?>) wird bevorzugt, da sie auch die Verwendung von PHP in XML-konformen Code, wie XHTML, erlaubt. </P ><P >
Die zweite Methode ist nicht immer verfⁿgbar. Die Kurzform der Tags ist nur m÷glich, wenn diese zuvor aktiviert wurde. Dies kann mittels der Funktion <B CLASS="function" >short_tags()</B > (nur PHP 3), dem Setzen der Konfigurationsoption <A HREF="#ini.short-open-tag" >short_open_tag</A > in der PHP Konfigurationsdatei, oder durch das Kompilieren von PHP mit der Option --enable-short-tags. Auch wenn diese Methode standardmΣ▀ig in der php.ini.dist aktiviert ist, wird die Verwendung dieser Kurzform nicht empfohlen. </P ><P >
Die vierte Methode ist nur verfⁿgbar, wenn die Konfigurationsoption <A HREF="#ini.asp-tags" >asp_tags</A > aktiviert ist. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Die Unterstⁿtzung der Tags im ASP-Stil wurde in Version 3.0.4. hinzugefⁿgt. </P ></BLOCKQUOTE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die Verwendung der ┤short tags┤ sollten Sie vermeiden, wenn Sie Applikationen oder Bibliotheken entwickeln, die verteilt oder auf PHP-Servern eingesetzt werden soll, die nicht unter Ihrer Kontrolle stehen, da die ┤short tags┤ auf dem einzusetzenden Server nicht unterstⁿtzt werden k÷nnten. Stellen Sie also sicher, dass fⁿr portablen, weiterverteilbaren Code keine ┤short tags┤ verwendet werden. </P ></BLOCKQUOTE ></DIV ><P >
Der schlie▀ende Tag fⁿr den PHP-Block schlie▀t eine sofort folgende Zeilenschaltung mit ein, falls diese vorhanden ist. Au▀erdem beinhaltet der schlie▀ende Tag ein implizites Semikolon; Sie mⁿssen also die letzte Zeile eines PHP-Blocks nicht mit einem Semikolon beenden. </P ><P >
PHP erm÷glicht auch die Verwendung folgender Strukturen: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2446" ></A ><P ><B >Beispiel 9-2. Erweitertes Verlassen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($expression) { ?> <strong>Das ist richtig.</strong> <?php } else { ?> <strong>Das ist falsch.</strong> <?php } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Dies funktioniert wie erwartet, da PHP nach dem Auffinden eines ?> schlie▀enden Tags einfach alles ausgibt, bis es wieder auf einen ÷ffnenden Tag st÷▀t. Das obige Beispiel ist natⁿrlich gekⁿnstelt, aber fⁿr die Ausgabe von gro▀en Textbl÷cken ist der Ausstieg aus dem Parse-Modus generell effizienter, als den gesamten Text durch <A HREF="#function.echo" ><B CLASS="function" >echo()</B ></A >, <A HREF="#function.print" ><B CLASS="function" >print()</B ></A >, etc. zu jagen. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.basic-syntax.instruction-separation" >Abgrenzung von Anweisungen</A ></H2 ><P >
Einzelne Anweisungen werden genauso voneinander abgetrennt wie in C oder Perl - jedes Statement wird mit einem Semikolon beendet. </P ><P >
Auch der schlie▀ende Tag (?>) bedeutet das Ende eines Statements, deshalb sind die folgenden Beispiele gleichwertig: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2455" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo "Dies ist ein Test"; ?> <?php echo "Dies ist ein Test" ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.basic-syntax.comments" >Kommentare</A ></H2 ><P >
PHP unterstⁿtzt 'C', 'C++' und Unix-Shell-artige Kommentare. Beispiele: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2460" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo "Dies ist ein Test"; // Dies ist ein einzeiliger Kommentar im C++-Stil. /* Dies ist ein mehrzeiliger Kommentar und noch eine Kommentar-Zeile */ echo "Dies ist noch ein Test"; echo "... und ein letzter Test"; # Dies ist ein Shell-artiger Kommentar. ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Die einzeiligen Kommentar-Arten kommentieren sΣmtlichen Text bis zum Zeilenende oder bis zum Ende des aktuellen PHP-Blocks aus, je nachdem, was zuerst eintritt. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2463" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><h1>Dies ist ein <?php # echo "einfaches";?> Beispiel.</h1> <p>Obige ▄berschrift wird lauten: 'Dies ist ein Beispiel.'.</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Man sollte darauf achten, nicht mehrere ┤C┤-artigen Kommentare ineinander zu setzen, was vorkommen kann, wenn man gro▀e Bl÷cke auskommentiert. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2466" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* echo "Dies ist ein Test"; /* Dieser Kommentar wird ein Problem verursachen. */ */ ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Die einzeiligen Kommentar-Arten kommentieren den Text tatsΣchlich nur bis zum Zeilenende oder bis zum Ende des aktuellen PHP-Blocks aus, je nachdem, was zuerst eintritt. Das hei▀t, dass HTML Code nach // ?> ausgegeben wird: ?> steigt aus dem PHP-Modus aus und kehrt in den HTML Modus zurⁿck, ohne dass // dies beeinflussen k÷nnte. </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.types" >Kapitel 10. Typen</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="language.types.intro" >Einfⁿhrung</A ></H2 ><P >
PHP unterstⁿtzt acht primitive Typen. </P ><P >
Vier skalare Typen: <P ></P ><UL ><LI ><P >
Boolean </P ></LI ><LI ><P >
Integer </P ></LI ><LI ><P >
Flie▀komma-Zahl (float) </P ></LI ><LI ><P >
String / Zeichenkette </P ></LI ></UL > Zwei zusammengesetzte Typen: <P ></P ><UL ><LI ><P >
Array </P ></LI ><LI ><P >
Object </P ></LI ></UL > Und zuletzt zwei spezielle Typen: <P ></P ><UL ><LI ><P >
Resource </P ></LI ><LI ><P >
<A HREF="#language.types.null" ><B CLASS="type" >NULL</B ></A > </P ></LI ></UL > Fⁿr die bessere Lesbarkeit fⁿhrt dieses Handbuch ein paar <A HREF="#language.pseudo-types" >Pseudo-Typen</A > ein: <P ></P ><UL ><LI ><P >
<A HREF="#language.types.mixed" >Mixed</A > </P ></LI ><LI ><P >
<A HREF="#language.types.number" >Number</A > </P ></LI ><LI ><P >
<A HREF="#language.types.mixed" >Callback</A > </P ></LI ></UL > Sie werden auch ein paar Hinweise auf den Typ "Double" finden. Betrachten Sie Double als dasselbe wie Float. Die beiden Bezeichnungen existieren nur aus historischen Grⁿnden. </P ><P >
Der Typ einer Variablen wird normalerweise nicht vom Programmierer bestimmt. Zur Laufzeit von PHP wird entschieden, welchen Typs eine Variable ist, abhΣngig vom Zusammenhang in dem die Variable benutzt wird. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Um den Typ und den Wert eines bestimmten <A HREF="#language.expressions" >Ausdrucks (Expression)</A > zu ⁿberprⁿfen, k÷nnen Sie <A HREF="#function.var-dump" ><B CLASS="function" >var_dump()</B ></A > benutzen. </P ><P ><B >Anmerkung: </B > Wenn Sie zur Fehlersuche einfach nur eine lesbare Darstellung eines Typs ben÷tigen, benutzen Sie <A HREF="#function.gettype" ><B CLASS="function" >gettype()</B ></A >. Um auf einen bestimmten Typ zu prⁿfen, sollten Sie <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nicht</I ></SPAN > <A HREF="#function.gettype" ><B CLASS="function" >gettype()</B ></A > benutzen. Stattdessen sollten Sie die <VAR CLASS="literal" >is_<VAR CLASS="replaceable" >type</VAR ></VAR > Funktionen verwenden. Ein paar Beispiele: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2524" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $bool = TRUE; // ein Boolean (Wahrheitswert) $str = "foo"; // ein String (Zeichenkette) $int = 12; // ein Integer (Ganzzahl) echo gettype($bool); // gibt "boolean" aus echo gettype($str); // gibt "string" aus // Falls es ein Integer ist, erh÷he ihn um vier if (is_int($int)) { $int += 4; } // Falls $bool ein String ist, gebe ihn aus // (gibt ⁿberhaupt nichts aus) if (is_string($bool)) { echo "String: $bool"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ><P >
Wenn sie die Umwandlung in einen bestimmten Variablen-Typ erzwingen wollen, erreichen Sie dies entweder durch <A HREF="#language.types.typecasting" >Typ-Umwandlung</A > oder durch Gebrauch der Funktion <A HREF="#function.settype" ><B CLASS="function" >settype()</B ></A >. </P ><P >
Beachten Sie, dass eine Variable abhΣngig vom Typ, dem die Variable zu dem Zeitpunkt entspricht, in bestimmten Situationen unterschiedlich ausgewertet werden kann. Weitere Informationen entnehmen Sie dem Abschnitt zur <A HREF="#language.types.type-juggling" >Typ-VerΣnderung</A >. Schauen Sie sich au▀erdem <A HREF="#types.comparisons" >PHP type comparison tables</A > an, wenn Sie an Beispielen verschiedener typenbezogener Vergleiche interessiert sind. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.boolean" >Boolscher Typ</A ></H2 ><P >
Das ist der einfachste Typ. Ein <A HREF="#language.types.boolean" ><B CLASS="type" >boolean</B ></A > drⁿckt einen Wahrheitswert aus. Dieser kann entweder <TT CLASS="constant" ><B >TRUE</B ></TT > oder <TT CLASS="constant" ><B >FALSE</B ></TT > sein. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der boolsche Typ wurde in PHP 4 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.boolean.syntax" >Syntax</A ></H3 ><P >
Um einen boolschen Typ w÷rtlich anzugeben, benutzen Sie entweder das Schlⁿsselwort <TT CLASS="constant" ><B >TRUE</B ></TT > oder <TT CLASS="constant" ><B >FALSE</B ></TT >. Beide unterscheiden sich nicht bezⁿglich Gro▀- und Kleinschreibung. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2545" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = True; // $foo wird der Wert TRUE zugewiesen ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
▄blicherweise verwenden Sie einen der <A HREF="#language.operators" >Operatoren</A >, der Ihnen einen <A HREF="#language.types.boolean" ><B CLASS="type" >boolean</B ></A > Wert liefert, den Sie dann an eine der <A HREF="#language.control-structures" >Kontroll-Strukturen</A > weiterreichen. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2551" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // == ist ein Vergleichs-Operator der auf Gleichheit ⁿberprⁿft und // einen boolschen Wert liefert if ($action == "show_version") { echo "Die Version ist 1.23"; } // Das ist nicht notwendig... if ($show_separators == TRUE) { echo "<hr>\n"; } // ...weil Sie einfach schreiben k÷nnen: if ($show_separators) { echo "<hr>\n"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.boolean.casting" >Umwandlung nach boolean</A ></H3 ><P >
Um einen Wert ausdrⁿcklich nach <A HREF="#language.types.boolean" ><B CLASS="type" >boolean</B ></A > zu konvertieren benutzen Sie entweder die Umwandlung mittels <VAR CLASS="literal" >(bool)</VAR > oder <VAR CLASS="literal" >(boolean)</VAR >. In den allermeisten FΣllen ist es jedoch nicht notwendig die Umwandlung selbst vorzunehmen. Ein Wert wird automatisch konvertiert, falls ein Operator, eine Funktion oder eine Kontrollstruktur ein <A HREF="#language.types.boolean" ><B CLASS="type" >boolean</B ></A > Argument erfordern. </P ><P >
Siehe auch <A HREF="#language.types.type-juggling" >Typ-VerΣnderung</A >. </P ><P >
Bei der Umwandlung nach <A HREF="#language.types.boolean" ><B CLASS="type" >boolean</B ></A > werden folgende Werte als <TT CLASS="constant" ><B >FALSE</B ></TT > angesehen: <P ></P ><UL ><LI ><P >
das <A HREF="#language.types.boolean" >boolean</A > <TT CLASS="constant" ><B >FALSE</B ></TT > selbst </P ></LI ><LI ><P >
die <A HREF="#language.types.integer" >Integer</A > 0 (Null) </P ></LI ><LI ><P >
die <A HREF="#language.types.float" >Flie▀komma-Zahl</A > 0.0 (Null) </P ></LI ><LI ><P >die leere <A HREF="#language.types.string" >Zeichenkette</A > und die <A HREF="#language.types.string" >Zeichenkette</A > "0" </P ></LI ><LI ><P >
ein <A HREF="#language.types.array" >Array</A > ohne Elemente </P ></LI ><LI ><P >
ein <A HREF="#language.types.object" >Objekt</A > ohne Mitgliedsvariablen </P ></LI ><LI ><P >
der spezielle Typ <A HREF="#language.types.null" >NULL</A > (einschlie▀lich nicht definierter Variablen) </P ></LI ></UL > Jeder andere Wert wird als <TT CLASS="constant" ><B >TRUE</B ></TT > angesehen (einschlie▀lich jeder <A HREF="#language.types.resource" >Resource</A >). <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
<VAR CLASS="literal" >-1</VAR > wird als <TT CLASS="constant" ><B >TRUE</B ></TT > angesehen, wie jede andere Zahl ungleich Null (egal ob negativ oder positiv)! </P ></TD ></TR ></TABLE ></DIV > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2595" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo gettype((bool) ""); // bool(falsch) echo gettype((bool) 1); // bool(wahr) echo gettype((bool) -2); // bool(wahr) echo gettype((bool) "foo"); // bool(wahr) echo gettype((bool) 2.3e5); // bool(wahr) echo gettype((bool) array(12)); // bool(wahr) echo gettype((bool) array()); // bool(falsch) ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.integer" >Integer Typen</A ></H2 ><P >
Ein Integer ist eine Zahl aus der Menge Z = {..., -2, -1, 0, 1, 2, ...}. </P ><P >
Siehe auch: <A HREF="#ref.gmp" >Ganzzahlen beliebiger LΣnge / GMP</A >, <A HREF="#language.types.float" >Flie▀komma-Zahlen</A > und <A HREF="#ref.bc" >Mathematische Funktionen mit beliebiger Genauigkeit</A > </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.integer.syntax" >Syntax</A ></H3 ><P >
Ganzzahlen k÷nnen in dezimaler (10-basierter), hexadezimaler (16-basierter) oder oktaler (8-basierter) Schreibweise angegeben werden, wahlweise mit einem vorangestellten Zeichen (- oder +). </P ><P >
Wenn Sie die oktale Schreibweise verwenden, mⁿssen Sie die Zahl mit einer vorangestellten Null <VAR CLASS="literal" >0</VAR > schreiben; in hexadezimaler Schreibweise mⁿssen Sie vor die Zahl ein <VAR CLASS="literal" >0x</VAR > schreiben. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2611" ></A ><P ><B >Beispiel 10-1. Integer Literale</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 1234; // Dezimalzahl $a = -123; // eine negative Zahl $a = 0123; // Oktalzahl (entspricht 83 dezimal) $a = 0x1A; // Hexadezimalzahl (entspricht 26 dezimal) ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Formell ist die m÷gliche Struktur fⁿr Integer Literale: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2614" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >dezimal : [1-9][0-9]* | 0 hexadezimal : 0[xX][0-9a-fA-F]+ oktal : 0[0-7]+ integer : [+-]?dezimal | [+-]?hexadezimal | [+-]?oktal</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Die Gr÷▀e eines Integer-Wertes ist plattformabhΣngig, ein Maximalwert von ungefΣhr zwei Milliarden ist jedoch ⁿblich (vorzeichenbehafteter 32-Bit-Wert). PHP unterstⁿtzt keine vorzeichenlosen Integer-Werte. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.integer.overflow" >Integer ▄berlauf</A ></H3 ><P >
Wenn Sie eine Zahl jenseits der Grenzen des Typs Integer angeben, wird diese stattdessen als Typ <A HREF="#language.types.float" ><B CLASS="type" >float</B ></A > interpretiert. Wenn Sie eine Operation ausfⁿhren, deren Ergebnis eine Zahl jenseits der Grenzen des Typs Integer ist, wird ebenso eine Zahl vom Typ <A HREF="#language.types.float" ><B CLASS="type" >float</B ></A > zurⁿckgegeben. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2623" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $grosse_zahl = 2147483647; var_dump($grosse_zahl); // Ausgabe: int(2147483647) $grosse_zahl = 2147483648; var_dump($grosse_zahl); // Ausgabe: float(2147483648) // das gilt auch fⁿr Integers in hexadezimaler Schreibweise: var_dump( 0x80000000 ); // Ausgabe: float(2147483648) $million = 1000000; $grosse_zahl = 50000 * $million; var_dump($grosse_zahl); // Ausgabe: float(50000000000) ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Bedauerlicherweise gab es einen Bug in PHP, der die korrekte Funktionsweise verhinderte, wenn negative Zahlen verwendet wurden. Ein Beispiel: Bei der Ausfⁿhrung von <VAR CLASS="literal" >-50000 * $million</VAR > war das Ergebnis <VAR CLASS="literal" >-429496728</VAR >. Sind beide Operanden positiv gibt es keine Probleme. </P ><P >
Dieses Problem ist in PHP 4.1.0 behoben worden. </P ></TD ></TR ></TABLE ></DIV > </P ><P >
In PHP gibt es keinen Operator fⁿr Integer-Divisonen. <VAR CLASS="literal" >1/2</VAR > ergibt <A HREF="#language.types.float" ><B CLASS="type" >float</B ></A > <VAR CLASS="literal" >0.5</VAR >. Sie k÷nnen den Wert in einen Integer umwandeln, um ihn immer abzurunden oder die Funktion <A HREF="#function.round" ><B CLASS="function" >round()</B ></A > benutzen. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2635" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php var_dump(25/7); // float(3.5714285714286) var_dump((int) (25/7)); // int(3) var_dump(round(25/7)); // float(4) ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.integer.casting" >Umwandlung nach integer</A ></H3 ><P >
Um einen Wert ausdrⁿcklich nach <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A > zu konvertieren, benutzen Sie entweder die Umwandlung mittels <VAR CLASS="literal" >(int)</VAR > oder <VAR CLASS="literal" >(integer)</VAR >. In den allermeisten FΣllen ist es jedoch nicht notwendig die Umwandlung selbst vorzunehmen. Ein Wert wird automatisch konvertiert, falls ein Operator, eine Funktion oder eine Kontrollstruktur ein <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A > Argument erfordern. Sie k÷nnen einen Wert auch mit der Funktion <A HREF="#function.intval" ><B CLASS="function" >intval()</B ></A > in einen Integer umwandeln. </P ><P >
Siehe auch <A HREF="#language.types.type-juggling" >Typ-VerΣnderung</A >. </P ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.integer.casting.from-boolean" >Von <A HREF="#language.types.boolean" >Booleans</A ></A ></H4 ><P >
<TT CLASS="constant" ><B >FALSE</B ></TT > ergibt <VAR CLASS="literal" >0</VAR > (Null), und <TT CLASS="constant" ><B >TRUE</B ></TT > ergibt <VAR CLASS="literal" >1</VAR > (Eins). </P ></DIV ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.integer.casting.from-float" >Von <A HREF="#language.types.float" >Flie▀komma-Zahlen</A ></A ></H4 ><P >
Bei der Umwandlung von <A HREF="#language.types.float" ><B CLASS="type" >float</B ></A > nach integer wird die Zahl <SPAN CLASS="emphasis" ><I CLASS="emphasis" >in Richtung Null</I ></SPAN > gerundet. </P ><P >
Wenn der float jenseits der Grenzen von integer liegt (ⁿblicherweise <VAR CLASS="literal" >+/- 2.15e+9 = 2^31</VAR >), ist das Ergebnis nicht definiert, weil float nicht genug PrΣzision besitzt um ein genaues integer Ergebnis zu liefern. Keine Warnung, nicht einmal eine Notiz wird in diesem Fall ausgegeben. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Wandeln Sie nie einen Teil eines Ausdrucks nach <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A > um, da dies manchmal zu unerwarteten Ergebnissen fⁿhren kann. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2666" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo (int) ( (0.1+0.7) * 10 ); // Ausgabe: 7! ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Fⁿr mehr Informationen schauen Sie unter <A HREF="#warn.float-precision" >Warnung ⁿber float-PrΣzision</A > nach. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.integer.casting.from-string" >Von Zeichenketten / Strings</A ></H4 ><P >
Siehe <A HREF="#language.types.string.conversion" >Umwandlung von Strings in Zahlen</A > </P ></DIV ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.integer.casting.from-other" >Von anderen Typen</A ></H4 ><P >
<DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Das Verhalten bei der Umwandlung nach integer ist fⁿr andere Typen nicht definiert. Zum gegenwΣrtigen Zeitpunkt ist das Verhalten so, als ob der Wert zuerst nach <A HREF="#language.types.boolean.casting" >boolean</A > konvertiert wird. Auf jeden Fall sollten Sie sich auf dieses Verhalten <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nicht</I ></SPAN > verlassen. Es kann sich ohne Ankⁿndigung Σndern. </P ></TD ></TR ></TABLE ></DIV > </P ></DIV ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.float" >Flie▀komma-Zahlenwerte</A ></H2 ><P >
Flie▀komma-Zahlenwerte ("floats", "doubles" oder "reelle Zahlen") k÷nnen durch eine der folgenden Anweisungen zugewiesen werden: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2683" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 1.234; $a = 1.2e3; $a = 7E-10; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Formell: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2685" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >LNUM [0-9]+ DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*) EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Die Gr÷▀e einer Flie▀komma-Zahl ist plattformabhΣngig, dennoch stellt ein Maximum von ~1.8e308 mit einer Genauigkeit von 14 Nachkomma-Stellen einen ⁿblichen Wert dar (das entspricht 64-Bit im IEEE-Format). </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B ><A NAME="warn.float-precision" ></A >Flie▀komma PrΣzision</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Es ist ziemlich normal, dass einfache Dezimalzahlen wie <VAR CLASS="literal" >0.1</VAR > oder <VAR CLASS="literal" >0.7</VAR > nicht in ihre internen binΣren Entsprechungen konvertiert werden k÷nnen, ohne einen kleinen Teil ihrer Genauigkeit zu verlieren. Das kann zu verwirrenden Ergebnissen fⁿhren. So wird <VAR CLASS="literal" >floor((0.1 + 0.7) * 10)</VAR > normalerweise <VAR CLASS="literal" >7</VAR > statt des erwarteten Wertes <VAR CLASS="literal" >8</VAR > zurⁿck geben (als Ergebnis der internen Entsprechung von <VAR CLASS="literal" >7.9999999999...</VAR >. </P ><P >
Das grⁿndet sich auf die Tatsache, dass es unm÷glich ist, manche Dezimal-Zahlen durch eine endliche Anzahl an Nachkomma-Stellen darzustellen. Dem Wert <VAR CLASS="literal" >1/3</VAR > entspricht z.B. der interne Wert von <VAR CLASS="literal" >0.3333333. . .</VAR >. </P ><P >
Deshalb sollten Sie nie den Ergebnissen von Flie▀komma-Operationen bis auf die letzte Nachkomma-Stelle trauen und nie solche auf Gleichheit prⁿfen. Ben÷tigen Sie wirklich eine gr÷▀ere Genauigkeit, sollten sie die <A HREF="#ref.bc" >mathematischen Funktionen beliebiger Genauigkeit</A > oder die <A HREF="#ref.gmp" >Gmp</A > Funktionen benutzen. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.float.casting" >Umwandlung nach float</A ></H3 ><P >
Fⁿr Informationen darⁿber, wann und wie Strings in Float umgewandelt werden, lesen Sie bitte den Abschnitt <A HREF="#language.types.string.conversion" >Umwandlung von Strings in Zahlen</A >. Fⁿr die Werte anderer Typen ist die Umwandlung diesselbe, wie wenn der Wert in eine Ganzzahl und dann in einen Float umgewandelt worden wΣre. Fⁿr weitere Informationen siehe Abschnitt <A HREF="#language.types.integer.casting" >Umwandlung nach integer</A >. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.string" >Strings / Zeichenketten</A ></H2 ><P >
Ein String ist eine Folge von Zeichen. In PHP entspricht ein Zeichen einem Byte, das hei▀t, dass exakt 256 unterschiedliche Zeichen m÷glich sind. Das impliziert auch, dass PHP keine native Unterstⁿtzung fⁿr Unicode bietet. Siehe <A HREF="#function.utf8-encode" ><B CLASS="function" >utf8_encode()</B ></A > und <A HREF="#function.utf8-decode" ><B CLASS="function" >utf8_decode()</B ></A > fⁿr etwas Unicode-Unterstⁿtzung. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Fⁿr einen String stellt die LΣnge kein Problem dar. Von PHP-Seite aus gibt es keine praktische Grenze fⁿr die Gr÷▀e eines Strings. Daher gibt es keinen Grund sich Sorgen ⁿber lange Strings zu machen. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.string.syntax" >Syntax</A ></H3 ><P >
Ein String kann auf drei verschiedene Weisen geschrieben werden. <P ></P ><UL ><LI ><P >
<A HREF="#language.types.string.syntax.single" >Einfache Anfⁿhrungszeichen (single quoted)</A > </P ></LI ><LI ><P >
<A HREF="#language.types.string.syntax.double" >Doppelte Anfⁿhrungszeichen (double quoted)</A > </P ></LI ><LI ><P >
<A HREF="#language.types.string.syntax.heredoc" >Heredoc Syntax</A > </P ></LI ></UL > </P ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.string.syntax.single" >Einfache Anfⁿhrungszeichen (Single quoted)</A ></H4 ><P >
Der leichteste Weg einen einfachen String zu schreiben, ist der Einschluss in einfache Anfⁿhrungszeichen (das Zeichen <VAR CLASS="literal" >'</VAR >). </P ><P >
Um ein einfaches Anfⁿhrungszeichen w÷rtlich auszugeben, muss dieses, wie in vielen anderen Programmiersprachen auch mit einem Backslash (<VAR CLASS="literal" >\</VAR >) escaped werden. Wenn Sie innerhalb eines Strings einen Backslash vor einem einfachen Anfⁿhrungszeichen oder am Ende eines Strings ausgeben wollen, mⁿssen Sie diesen verdoppeln. Beachten Sie: wenn Sie versuchen irgendwelche anderen Zeichen zu escapen, wird der Backslash ebenfalls ausgegeben! Daher besteht fⁿr gew÷hnlich keine Notwendigkeit den Backslash selbst zu escapen. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In PHP 3 wird eine Warnung der Stufe <VAR CLASS="literal" >E_NOTICE</VAR > ausgegeben, wenn das passiert. </P ></BLOCKQUOTE ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Anders als bei den zwei anderen Schreibweisen werden <A HREF="#language.variables" >Variablen</A > und escape-Sequenzen fⁿr spezielle Zeichen innerhalb von single-quoted Strings <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nicht</I ></SPAN > ausgewertet. </P ></BLOCKQUOTE ></DIV > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2741" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo 'Das ist ein einfacher String'; echo 'Sie k÷nnen auf diese Weise auch Zeilenumbrⁿche innerhalb von Strings verwenden, wenn Ihnen danach ist'; // Ausgabe: Arnold sagte einmal: "I'll be back" echo 'Arnold sagte einmal: "I\'ll be back"'; // Ausgabe: Sie haben C:\*.* gel÷scht? echo 'Sie haben C:\\*.* gel÷scht?'; // Ausgabe: Sie haben C:\*.* gel÷scht? echo 'Sie haben C:\*.* gel÷scht?'; // Ausgabe: Das wird nicht ausgewertet: \n ein Zeilenumbruch echo 'Das wird nicht ausgewertet: \n ein Zeilenumbruch'; // Ausgabe: Variablen werden auch $nicht $ausgewertet echo 'Variablen werden auch $nicht $ausgewertet'; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.string.syntax.double" >Doppelte Anfⁿhrungszeichen (Double quoted)</A ></H4 ><P >
Wenn ein String in doppelten Anfⁿhrungszeichen (") eingeschlossen ist, versteht PHP mehr Escape-Folgen fⁿr spezielle Zeichen: </P ><DIV CLASS="table" ><A NAME="AEN2746" ></A ><P ><B >Tabelle 10-1. Nicht ausgewertete / ⁿbergangene Zeichen:</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Zeichenfolge</TH ><TH >Bedeutung</TH ></TR ></THEAD ><TBODY ><TR ><TD ><VAR CLASS="literal" >\n</VAR ></TD ><TD >Zeilenvorschub (LF oder 0x0A als ASCII-Code)</TD ></TR ><TR ><TD ><VAR CLASS="literal" >\r</VAR ></TD ><TD >Wagenrⁿcklauf (CR oder 0x0D als ASCII-Code)</TD ></TR ><TR ><TD ><VAR CLASS="literal" >\t</VAR ></TD ><TD >
horizontaler Tabulator (HT oder 0x09 als ASCII-Code) </TD ></TR ><TR ><TD ><VAR CLASS="literal" >\\</VAR ></TD ><TD >Backslash / Rⁿckstrich</TD ></TR ><TR ><TD ><VAR CLASS="literal" >\$</VAR ></TD ><TD >Dollar-Symbol</TD ></TR ><TR ><TD ><VAR CLASS="literal" >\"</VAR ></TD ><TD >doppelte Anfⁿhrungszeichen</TD ></TR ><TR ><TD ><VAR CLASS="literal" >\[0-7]{1,3}</VAR ></TD ><TD >
die Zeichenfolge, die dem regulΣren Ausdruck entspricht ist ein Zeichen in Oktal-Schreibweise </TD ></TR ><TR ><TD ><VAR CLASS="literal" >\x[0-9A-Fa-f]{1,2}</VAR ></TD ><TD >
die Zeichenfolge, die dem regulΣren Ausdruck entspricht ist ein Zeichen in Hexadezimal-Schreibweise </TD ></TR ></TBODY ></TABLE ></DIV ><P >
Noch einmal: wenn Sie versuchen, irgend ein anderes Zeichen zu escapen wird der Backslash ebenfalls ausgegeben! </P ><P >
Das wichtigste Merkmal von double-quoted Strings ist die Tatsache, dass Variablennamen ausgewertet werden. Fⁿr Details siehe <A HREF="#language.types.string.parsing" >String Analyse (parsing)</A > </P ></DIV ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.string.syntax.heredoc" >Heredoc</A ></H4 ><P >
Eine andere M÷glichkeit Strings einzufassen, besteht im Gebrauch der heredoc-Syntax ("<<<"). Hierfⁿr ist nach <VAR CLASS="literal" ><<<</VAR > ein Bezeichner zu setzen. Nun folgt der eigentliche String und dann derselbe Bezeichner um den String abzuschlie▀en. </P ><P >
Der schlie▀ende Bezeichner <SPAN CLASS="emphasis" ><I CLASS="emphasis" >muss</I ></SPAN > in der ersten Spalte der Zeile stehen. Die verwendeten Bezeichner mⁿssen den gleichen Regeln entsprechen wie alle anderen PHP-Labels auch: Sie dⁿrfen lediglich alphanumerische Zeichen und den Unterstrich enthalten und mⁿssen mit einem Unterstrich oder einem Buchstaben beginnen. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Es ist sehr wichtig zu beachten, dass die Zeile mit dem schlie▀enden Bezeichner keine anderen Zeichen enthΣlt, ausgenommen <SPAN CLASS="emphasis" ><I CLASS="emphasis" >m÷glicherweise</I ></SPAN > ein Semikolon (<VAR CLASS="literal" >;</VAR >). Das bedeuted im Besonderen, dass der Bezeichner <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nicht eingerⁿckt werden darf</I ></SPAN > und es dⁿrfen keine Leerzeichen oder Tabulatoren vor oder nach dem Semikolon stehen. Au▀erdem ist es wichtig, zu beachten, dass das erste Zeichen vor dem schlie▀enden Bezeichner ein der Definition Ihres Betriebssystems entsprechender Zeilenvorschub, sein muss. Das ist zum Beispiel auf einem Macintosh ein <VAR CLASS="literal" >\r</VAR >. </P ><P >
Falls diese Regel gebrochen wird und der schlie▀ende Bezeichner nicht "sauber" ist, wird er nicht als schlie▀ender Bezeichner betrachtet und PHP wird weiter danach suchen. Wird in diesem Fall kein korrekter schlie▀ender Bezeichner gefunden, gibt es einen Parser-Fehler mit der Zeilennummer vom Ende des Skripts. </P ></TD ></TR ></TABLE ></DIV ><P >
Heredoc-Text funktioniert wie ein String innnerhalb doppelter Anfⁿhrungszeichen, nur ohne doppelte Anfⁿhrungszeichen. Anfⁿhrungszeichen innerhalb von heredoc-Texten mⁿssen also keiner Sonderbehandlung (escapen) unterzogen werden, aber Sie k÷nnen dennoch die oben aufgefⁿhrten Escape-Anweisungen verwenden. Variablen werden ausgewertet, aber besondere Aufmerksamkeit muss komplexen Variablen gewidmet werden, genau wie bei Strings. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2803" ></A ><P ><B >Beispiel 10-2. Beispiel zur String-Festlegung per "heredoc"-Methode: </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $str = <<<EOD Beispiel eines Strings ⁿber mehrere Script-Zeilen durch Gebrauch der heredoc-Syntax. EOD; /* komplexeres Beispiel, mit Variablen */ class foo { var $foo; var $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'Mein Name'; echo <<<EOT Mein Name ist "$name". Ich schreibe einige $foo->foo. Nun schreibe ich gerade einige {$foo->bar[1]}. Dies sollte ein gro▀es 'A' schreiben: \x41 EOT; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die heredoc Unterstⁿtzung wurde in PHP 4 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.string.parsing" >Variablen-Analyse (parsing)</A ></H4 ><P >
Wird ein String in doppelten Anfⁿhrungszeichen oder mit heredoc angegeben, werden enthaltene <A HREF="#language.variables" >Variablen</A > ausgewertet (geparst). </P ><P >
Es gibt zwei Syntax-Typen: eine <A HREF="#language.types.string.parsing.simple" >einfache</A > und eine <A HREF="#language.types.string.parsing.complex" >komplexe</A >. Die einfache Syntax ist die gelΣufigste und bequemste. Sie bietet die M÷glichkeit eine Variable, einen Array-Wert oder eine Objekt-Eigenschaft auszuwerten (parsen). </P ><P >
Die komplexe Syntax wurde in PHP 4 eingefⁿhrt und ist an den geschweiften Klammern <VAR CLASS="literal" >{}</VAR >erkennbar, die den Ausdruck umschlie▀en. </P ><DIV CLASS="sect4" ><HR><H5 CLASS="sect4" ><A NAME="language.types.string.parsing.simple" >Einfache Syntax</A ></H5 ><P >
Sobald ein Dollarzeichen (<VAR CLASS="literal" >$</VAR >) auftaucht, wird der Parser versuchen, gierig so viele Zeichen wie m÷glich zu bekommen, um einen gⁿltigen Variablennamen zu bilden. Schlie▀en Sie Ihren Varaiblennamen in geschweifte Klammern ein, wenn Sie ausdrⁿcklich das Ende des Namens angeben wollen. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2823" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $beer = 'Heineken'; echo "$beer's Geschmack ist gro▀artig"; // funktioniert, "'" ist kein gⁿltiges // Zeichen fⁿr einen Variablennamen echo "Er hat einige $beers getrunken"; // funktioniert nicht, 's' ist ein gⁿltiges // Zeichen fⁿr einen Variablennamen echo "Er hat einige ${beer}s getrunken"; // funktioniert echo "Er hat einige {$beer}s getrunken"; // funktioniert ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Auf Σhnliche Weise k÷nnen Sie erreichen, dass ein Array-Index oder eine Objekt-Eigenschaft ausgewertet wird. Bei Array-Indizes markiert die schlie▀ende eckige Klammer (<VAR CLASS="literal" >]</VAR >) das Ende des Index. Fⁿr Objekt-Eigenschaften gelten die gleichen Regeln wie bei einfachen Variablen, obwohl es bei Objekt-Eigenschaften keinen Trick gibt, wie dies bei Variablen der Fall ist. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2829" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Diese Beispiele sind spezifisch fⁿr die Verwendung von Arrays // innerhalb von Strings. Setzen sie die String-Schlⁿssel Ihrer Arrays // au▀erhalb von Strings immer in Anfⁿhrungszeichen und verwenden Sie // au▀erhalb von Strings auch keine {geschweiften Klammern}. // Wir lassen uns alle Fehlermeldungen anzeigen error_reporting(E_ALL); $frⁿchte = array('Erdbeere' => 'rot' , 'Banane' => 'gelb'); // Funktioniert aber beachten Sie: au▀erhalb von // String-Anfⁿhrungszeichen funktioniert das anders echo "Eine Banane ist $frⁿchte[Banane]."; // Funktioniert echo "Eine Banane ist {$frⁿchte['Banane']}."; // Funktioniert, aber PHP sucht, wie unten beschrieben, zuerst nach // einer Konstanten namens Banane. echo "Eine Banane ist {$frⁿchte[Banane]}."; // Funktioniert nicht; verwenden Sie geschweifte Klammern. Das // erzeugt einen Parser-Fehler. echo "Eine Banane ist $frⁿchte['Banane']."; // Funktioniert echo "Eine Banane ist " . $frⁿchte['Banane'] . "."; // Funktioniert echo "Dieses Quadrat ist $quadrat->breite Meter breit."; // Funktioniert nicht. Fⁿr eine L÷sung siehe die komplexe Syntax. echo "Dieses Quadrat ist $quadrat->breite00 Zentimeter breit."; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Fⁿr irgendetwas Komplexeres sollten Sie die komplexe Syntax nutzen. </P ></DIV ><DIV CLASS="sect4" ><HR><H5 CLASS="sect4" ><A NAME="language.types.string.parsing.complex" >Komplexe (geschweifte) Syntax</A ></H5 ><P >
Diese wird nicht komplex genannt, weil etwa die Syntax komplex ist, sondern weil Sie auf diesem Weg komplexe Ausdrⁿcke einbeziehen k÷nnen. </P ><P >
TatsΣchlich k÷nnen Sie jeden beliebigen Wert einbeziehen, der im Namensbereich in Strings gⁿltig ist. Schreiben Sie den Ausdruck einfach auf die gleiche Art und Weise, wie au▀erhalb des Strings, und umschlie▀en diesen mit { und }. Da Sie '{' nicht escapen k÷nnen, wird diese Syntax nur erkannt, wenn auf { unmittelbar $ folgt. (Benutzen Sie "{\$" oder "\{$" um ein w÷rtliches "{$" zu erhalten.) Einige Beispiele, um dies zu verdeutlichen: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2836" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Wir lassen uns alle Fehlermeldungen anzeigen error_reporting(E_ALL); $great = 'fantastisch'; // Funktioniert nicht, Ausgabe: Das ist { fantastisch} echo "Das ist { $great}"; // Funktioniert, Ausgabe: Das ist fantastisch echo "Das ist {$great}"; echo "Das ist ${great}"; // Funktioniert echo "Dieses Quadrat ist {$square->width}00 Zentimeter breit."; // Funktioniert echo "Das funktioniert: {$arr[4][3]}"; // Das ist aus dem gleichen Grund falsch // wie $foo[bar] au▀erhalb eines Strings falsch ist. Mit // anderen Worten, es funktioniert zwar, aber weil PHP // zunΣchst nach einer Konstanten namens foo sucht, gibt // es einen Fehler der Stufe E_NOTICE (undefined constant) // aus. echo "Das ist falsch: {$arr[foo][3]}"; // Funktioniert. Benutzen Sie bei der Verwendung // mehrdimensionaler Arrays innerhalb von Strings immer // Klammern um die Arrays. echo "Das funktioniert: {$arr['foo'][3]}"; // Funktioniert echo "Das funktioniert: " . $arr['foo'][3]; echo "Sie k÷nnen sogar schreiben {$obj->values[3]->name}"; echo "Das ist der Wert der Variable mit Namen $name: {${$name}}"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></DIV ></DIV ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.string.substr" >Zugriff und Modifikation von Zeichen in Strings</A ></H4 ><P >
Innerhalb von Zeichenketten (strings) kann durch die Angabe des nullbasierten Offsets in geschweiften Klammern nach dem String auf das gewⁿnschte Zeichen zugegriffen oder dieses modifiziert werden. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Fⁿr AbwΣrtskompatibilitΣt k÷nnen Sie fⁿr den selben Zweck immer noch die Array-Klammern verwenden. Diese Syntax wird jedoch seit PHP 4 missbilligt. </P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2844" ></A ><P ><B >Beispiel 10-3. Einige String-Beispiele</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Das erste Zeichen eines Strings. $str = 'Das ist ein Test.' $erstes = $str{0}; // $erstes enthΣlt "D" // Das dritte Zeichen eines Strings. $drittes = $str{2}; // Das letzte Zeichen eines Strings. $str = 'Das ist immer noch ein Test.' $letztes = $str{strlen($str)-1}; // $letztes enthΣlt "." // Das letzte Zeichen eines Strings verΣndern $str = 'Schau auf die Stra▀i'; $str{strlen($str)-1} = 'e'; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.string.useful-funcs" >Nⁿtzliche Funktionen und Operatoren</A ></H3 ><P >
Strings k÷nnen mittels des '.'(dot)-Operators miteinander verbunden werden. Beachten Sie, dass dafⁿr nicht der '+' (Additions)-Operator verwendet werden kann. Fⁿr mehr Informationen schauen Sie bitte unter den <A HREF="#language.operators.string" >Zeichenketten-Operatoren</A > nach. </P ><P >
Es gibt eine Menge nⁿtzlicher Funktionen zur String-Manipulation. </P ><P >
Fⁿr allgemeine Funktionen schauen Sie sich den Abschnitt ⁿber <A HREF="#ref.strings" >String Funktionen</A > an, und fⁿr fortgeschrittenes Suchen & Ersetzen die Funktionen zu RegulΣren Ausdrⁿcken (in zwei Ausfⁿhrungen: <A HREF="#ref.pcre" >Perl Kompatibel</A > und <A HREF="#ref.regex" >POSIX erweitert</A >). </P ><P >
Weiterhin gibt es auch noch <A HREF="#ref.url" >Funktionen fⁿr URL-Strings</A >, und Funktionen zum Verschlⁿsseln/Entschlⁿsseln von Strings (<A HREF="#ref.mcrypt" >mcrypt</A > und <A HREF="#ref.mhash" >mhash</A >). </P ><P >
Schlie▀lich, falls Sie immer noch nicht das gefunden haben wonach Sie suchen, schauen Sie unter den <A HREF="#ref.ctype" >Zeichen-Typen Funktionen</A > nach. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.string.casting" >Umwandlung nach string</A ></H3 ><P >
Sie k÷nnen einen Wert mit der <VAR CLASS="literal" >(string)</VAR >-Umwandlung oder mit der Funktion <A HREF="#function.strval" ><B CLASS="function" >strval()</B ></A > in einen String umwandeln. Die Umwandlung in einen String wird im dem Bereich eines Ausdrucks, in dem ein String ben÷tigt wird, automatisch fⁿr Sie vorgenommen. Dies geschieht, wenn Sie die Funktionen <A HREF="#function.echo" ><B CLASS="function" >echo()</B ></A > oder <A HREF="#function.print" ><B CLASS="function" >print()</B ></A > benutzen oder wenn Sie den Wert einer Variablen mit einem String vergleichen. Die Abschnitte ⁿber <A HREF="#language.types" >Typen</A > und <A HREF="#language.types.type-juggling" >Typen-Tricks</A > verdeutlichen das folgende. Siehe auch <A HREF="#function.settype" ><B CLASS="function" >settype()</B ></A >. </P ><P >
Ein Boolean <TT CLASS="constant" ><B >TRUE</B ></TT >-Wert wird in den String <VAR CLASS="literal" >"1"</VAR > umgewandelt, der <TT CLASS="constant" ><B >FALSE</B ></TT >-Wert wird als <VAR CLASS="literal" >""</VAR > (leerer String) dargestellt. Dadurch k÷nnen Sie Boolean- und String-Werte ineinander umwandeln. </P ><P >
Ein Integer oder eine Flie▀kommazahl (Float) wird in einen String umgewandelt, der die Zahl durch seine Ziffern darstellt (bei Flie▀kommazahlen inklusive Exponententeil). </P ><P >
Arrays werden immer in den String <VAR CLASS="literal" >"Array"</VAR > umgewandelt. Sie k÷nnen also den Inhalt eines Arrays nicht mit <A HREF="#function.echo" ><B CLASS="function" >echo()</B ></A > oder <A HREF="#function.print" ><B CLASS="function" >print()</B ></A > ausgeben, um zu sehen, was es enthΣlt. Um ein Element anzuschauen, ben÷tigen Sie etwas in der Art <VAR CLASS="literal" >echo $arr['foo']</VAR >. Siehe weiter unten fⁿr Hinweise zur Ausgabe/Ansicht des kompletten Inhalts. </P ><P >
Objekte werden immer in den String <VAR CLASS="literal" >"Object"</VAR > umgewandelt. Wenn Sie fⁿr die Fehlersuche die Werte der Mitgliedsvariablen eines Objekts ausgeben m÷chten, lesen Sie bitte die Abschnitte weiter unten. Benutzen Sie <A HREF="#function.get-class" ><B CLASS="function" >get_class()</B ></A >, wenn Sie den Namen der Klasse erfahren m÷chten, von der ein Objekt eine Instanz ist. </P ><P >
Ressourcen werden immer in Strings mit der Struktur <VAR CLASS="literal" >"Resource id #1"</VAR > umgewandelt, wobei <VAR CLASS="literal" >1</VAR > die eindeutige Nummer ist, die der Resource von PHP zur Laufzeit zugeordnet wurde. Benutzen Sie <A HREF="#function.get-resource-type" ><B CLASS="function" >get_resource_type()</B ></A >, wenn Sie wissen m÷chten, welchen Typs die Ressource ist. </P ><P >
<TT CLASS="constant" ><B >NULL</B ></TT > wird immer in einen leeren String umgewandelt. </P ><P >
Wie Sie oben erfahren haben, liefert Ihnen die Ausgabe von Arrays, Objekten oder Ressourcen keine brauchbaren Informationen ⁿber die Werte selbst. Schauen Sie sich fⁿr eine bessere M÷glichkeit, sich Werte fⁿr die Fehlersuche ausgeben zu lassen, die Funktionen <A HREF="#function.print-r" ><B CLASS="function" >print_r()</B ></A > und <A HREF="#function.var-dump" ><B CLASS="function" >var_dump()</B ></A > an. </P ><P >
Sie k÷nnen PHP-Werte auch in Strings umwandeln, um sie dauerhaft zu speichern. Diese Methode wird als Serialisierung bezeichnet und kann mit der Funktion <A HREF="#function.serialize" ><B CLASS="function" >serialize()</B ></A > vorgenommen werden. Falls Ihr PHP ⁿber <A HREF="#ref.wddx" >WDDX</A >-Unterstⁿtzung verfⁿgt, k÷nnen Sie PHP-Werte auch in XML-Strukturen serialisieren. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.string.conversion" >Umwandlung von Strings in Zahlen</A ></H3 ><P >
Sobald ein String als numerischer Wert ausgewertet wird, werden der resultierende Wert und der Typ wie folgt festgelegt. </P ><P >
Der String wird als <A HREF="#language.types.float" ><B CLASS="type" >float</B ></A > ausgewertet, wenn er eines der Zeichen '.', 'e' oder 'E' enthΣlt. Ansonsten wird er als Integer-Wert interpretiert. </P ><P >
Der Wert wird durch den Anfangsteil des Strings bestimmt. Sofern der String mit gⁿltigen numerischen Daten beginnt, werden diese als Wert benutzt. Andernfalls wird der Wert 0 (Null) sein. Gⁿltige numerische Daten sind ein optionales Vorzeichen, gefolgt von einer oder mehreren Zahlen (optional mit einem Dezimalpunkt). Wahlweise kann auch ein Exponent angegeben werden. Der Exponent besteht aus einem 'e' oder 'E', gefolgt von einer oder mehreren Zahlen. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2910" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = 1 + "10.5"; // $foo ist float (11.5) $foo = 1 + "-1.3e3"; // $foo ist float (-1299) $foo = 1 + "bob-1.3e3"; // $foo ist integer (1) $foo = 1 + "bob3"; // $foo ist integer (1) $foo = 1 + "10 Kleine Schweine"; // $foo ist integer (11) $foo = 4 + "10.2 Ferkel"; // $foo ist float (14.2) $foo = "10 Schweine " + 1; // $foo ist integer (11) $foo = "10.0 Schweine " + 1; // $foo ist float (11) $foo = "10.0 Schweine " + 1.0; // $foo ist float (11) ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Weitere Informationen ⁿber solche Umwandlungen finden Sie in den UNIX Manualseiten unter strtod(3). </P ><P >
Wenn Sie irgendein Beispiel dieses Abschnitts testen wollen, k÷nnen Sie dieses per "Copy und Paste" ⁿbernehmen. Fⁿgen Sie die folgende Zeile Ihrem Skript hinzu und Sie sehen selbst was passiert: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2914" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo "\$foo==$foo; Typ ist " . gettype ($foo) . "<br />\n"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Erwarten Sie nicht, den Code eines Zeichens zu bekommen, indem Sie es in einen Integer umwandeln (wie das z.B. in C der Fall wΣre). Benutzen Sie die Funktionen <A HREF="#function.ord" ><B CLASS="function" >ord()</B ></A > und <A HREF="#function.chr" ><B CLASS="function" >chr()</B ></A >, um Zeichencodes und Zeichen ineinander umzuwandeln. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.array" >Arrays</A ></H2 ><P >
Ein Array in PHP ist eigentlich eine geordnete Abbildung. Eine Abbildung ist ein Typ der <SPAN CLASS="emphasis" ><I CLASS="emphasis" >Werte</I ></SPAN > auf <SPAN CLASS="emphasis" ><I CLASS="emphasis" >Schlⁿssel</I ></SPAN > abbildet. Dieser Typ ist auf mehrere Arten optimiert, so dass Sie ihn auf verschiedene Weise benutzen k÷nnen: als reales Array, als Liste (Vektor), als Hash-Tabelle (das ist eine praktische Anwendung einer Abbildung), als Verzeichnis, als Sammlung, als Stapel (Stack), als Warteschlange (Queue) und m÷glicherweise mehr. Da Sie ein weiteres PHP-Array als Wert benutzen k÷nnen, ist es ziemlich einfach m÷glich Baumstrukturen zu simulieren. </P ><P >
Die ErklΣrung dieser Datenstrukturen sprengt den Rahmen dieses Handbuchs, aber Sie finden zumindest ein Beispiel fⁿr jede dieser Strukturen. Fⁿr weitergehende Informationen verweisen wir auf externe Literatur zu diesem breit gefΣcherten Thema. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.array.syntax" >Syntax</A ></H3 ><DIV CLASS="sect3" ><H4 CLASS="sect3" ><A NAME="language.types.array.syntax.array-func" >Angabe mit <A HREF="#function.array" ><B CLASS="function" >array()</B ></A ></A ></H4 ><P >
Ein <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A > kann mit Hilfe des Sparchkonstrukts <A HREF="#function.array" ><B CLASS="function" >array()</B ></A > erzeugt werden. Es ben÷tigt eine bestimmte Anzahl von Komma-getrennten <VAR CLASS="literal" ><VAR CLASS="replaceable" >Schlⁿssel</VAR > => <VAR CLASS="replaceable" >Wert</VAR ></VAR > Paaren. </P ><P >
<TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="synopsis" >array( [<VAR CLASS="replaceable" >Schlⁿssel</VAR > =>] <VAR CLASS="replaceable" >Wert</VAR > , ... ) // <VAR CLASS="replaceable" >Schlⁿssel</VAR > kann ein Integer oder String sein // <VAR CLASS="replaceable" >Wert</VAR > kann irgendetwas sein</PRE ></TD ></TR ></TABLE > </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2946" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $arr = array("foo" => "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Ein <VAR CLASS="varname" >Schlⁿssel</VAR > kann entweder ein <VAR CLASS="literal" >integer</VAR > oder ein <A HREF="#language.types.string" ><B CLASS="type" >string</B ></A > sein. Wenn ein Schlⁿssel die Standard-Darstellung einer <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A > Zahl ist, wird es als solche interpretiert werden, (z.B. <VAR CLASS="literal" >"8"</VAR > wird als <VAR CLASS="literal" >8</VAR > interpretiert, wΣhrend <VAR CLASS="literal" >"08"</VAR > als <VAR CLASS="literal" >"08"</VAR > interpretiert werden wird). Es gibt in PHP keine unterschiedlichen Typen indizierter oder assoziativer Arrays. Es gibt nur einen Array-Typ, der sowohl Integer- als auch String-Indizes enthalten kann. </P ><P >
Ein Wert kann jeder PHP-Typ sein. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2958" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $arr = array("einarray" => array(6 => 5, 13 => 9, "a" => 42)); echo $arr["einarray"][6]; // 5 echo $arr["einarray"][13]; // 9 echo $arr["einarray"]["a"]; // 42 ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Falls Sie fⁿr einen Wert keinen Schlⁿssel angeben, wird der gr÷▀te der Ganzzahl-Indizes genommen und der neue Schlⁿssel wird dieses Maximum + 1 sein. Falls Sie einen Schlⁿssel angeben dem schon ein Wert zugeordnet wurde, wird dieser Wert ⁿberschrieben. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2961" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Dieses Array ist dasselbe wie ... array(5 => 43, 32, 56, "b" => 12); // ... dieses Array array(5 => 43, 6 => 32, 7 => 56, "b" => 12); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Ab PHP 4.3.0 hat sich die oben beschriebene Generierung von Indizes geΣndert. Wenn Sie etwas an ein Array anhΣngen, in dem der gr÷▀te Schlⁿssel negativ ist, wird der nΣchste Schlⁿssel jetzt Null (<VAR CLASS="literal" >0</VAR >) sein. Davor wΣre der neue Index, genauso wie bei positiven Indizes, der gr÷▀te vorhandene Schlⁿssel + 1 gewesen. </P ></TD ></TR ></TABLE ></DIV ><P >
Wenn Sie <TT CLASS="constant" ><B >TRUE</B ></TT > als Schlⁿssel benutzen, wird dies als Schlⁿssel vom Typ <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A > <VAR CLASS="literal" >1</VAR > ausgewertet. Benutzen Sie <TT CLASS="constant" ><B >FALSE</B ></TT > als Schlⁿssel, wird dies als Schlⁿssel vom Typ <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A > <VAR CLASS="literal" >0</VAR > ausgewertet. Die Benutzung von <VAR CLASS="literal" >NULL</VAR > als Schlⁿssel fⁿhrt dazu, dass der Schlⁿssel als leerer String gewertet wird. Verwenden Sie einen leeren String als Schlⁿssel, wird ein Schlⁿssel mit einem leeren String und seinem Wert erzeugt (oder ⁿberschrieben). Das entspricht nicht der Verwendung von leeren Klammern. </P ><P >
Sie k÷nnen keine Arrays oder Objekte als Schlⁿssel benutzen. Der Versuch wird mit einer Warnung enden: <VAR CLASS="literal" >Illegal offset type</VAR >. </P ></DIV ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.types.array.syntax.modifying" >Erzeugen / VerΣndern mit der eckigen Klammer-Syntax</A ></H4 ><P >
Sie k÷nnen ein bestehendes Array durch explizites Zuweisen von Werten verΣndern. </P ><P >
Weisen Sie dem Array Werte zu indem Sie den Schlⁿssel in eckigen Klammern angeben. Sie k÷nnen den Schlⁿssel auch weglassen. In diesem Fall schreiben Sie einfach ein leeres Klammerpaar ("<VAR CLASS="literal" >[]</VAR >") hinter den Variablennamen. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="synopsis" >$arr[<VAR CLASS="replaceable" >Schlⁿssel</VAR >] = <VAR CLASS="replaceable" >Wert</VAR >; $arr[] = <VAR CLASS="replaceable" >Wert</VAR >; // <VAR CLASS="replaceable" >Schlⁿssel</VAR > kann ein <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A > oder ein <A HREF="#language.types.string" ><B CLASS="type" >string</B ></A > sein // <VAR CLASS="replaceable" >Wert</VAR > kann irgendetwas sein.</PRE ></TD ></TR ></TABLE > Falls <VAR CLASS="varname" >$arr</VAR > bis dahin nicht existiert, wird es erzeugt. Das ist also eine alternative Syntax um ein Array zu erzeugen. Um einen bestimmten Wert zu Σndern, weisen Sie einfach einem Element, das Sie mit seinem Schlⁿssel spezifizieren, einen neuen Wert zu. Wenn Sie ein Schlⁿssel/Wert Paar entfernen m÷chten, benutzen Sie <A HREF="#function.unset" ><B CLASS="function" >unset()</B ></A >. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2991" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $arr = array(5 => 1, 12 => 2); $arr[] = 56; // An dieser Stelle des Skrips ist das // dasselbe wie $arr[13] = 56; $arr["x"] = 42; // Fⁿgt dem Array ein neues Element mit // dem Schlⁿssel "x" hinzu unset($arr[5]); // Entfernt das Element aus dem Array unset($arr); // L÷scht das komplette Array ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Wie weiter oben schon erwΣhnt, wird, falls Sie die Klammern setzen ohne einen Schlⁿssel anzugeben, der gr÷▀te der Integer-Indizes genommen und der neue Schlⁿssel wird dieses Maximum + 1 sein. Falls es noch keine Integer-Indizes gibt, wird der Schlⁿssel zu <VAR CLASS="literal" >0</VAR > (Null). Falls Sie einen Schlⁿssel angeben dem schon ein Wert zugeordnet wurde, wird dieser Wert ⁿberschrieben. </P ><P >
<DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Ab PHP 4.3.0 hat sich die oben beschriebene Generierung von Indizes geΣndert. Wenn Sie etwas an ein Array anhΣngen, in dem der gr÷▀te Schlⁿssel negativ ist, wird der nΣchste Schlⁿssel jetzt Null (<VAR CLASS="literal" >0</VAR >) sein. Davor wΣre der neue Index, genauso wie bei positiven Indizes, der gr÷▀te vorhandene Schlⁿssel + 1 gewesen. </P ></TD ></TR ></TABLE ></DIV > </P ><P >
Beachten Sie, dass der hierbei verwendete gr÷▀te Integer-Schlⁿssel <SPAN CLASS="emphasis" ><I CLASS="emphasis" >im Array zu diesem Zeitpunkt nicht vorhanden sein muss</I ></SPAN >. Er muss nur irgendwann im Array existiert haben, seitdem es zum letzten Mal neu indiziert wurde. Folgendes Beispiel zur Veranschaulichung: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3002" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Wir erzeugen ein einfaches Array $array = array(1, 2, 3, 4, 5); print_r($array); // Nun l÷schen wir alle Elemente, lassen das Array selbst // aber intakt: foreach ($array as $i => $wert) { unset($array[$i]); } print_r($array); // Wir hΣngen ein Element an (beachten Sie, dass der neue Schlⁿssel 5 // ist, statt 0, wie sie vielleicht erwarten wⁿrden). $array[] = 6; print_r($array); // Neu indizieren: $array = array_values($array); $array[] = 7; print_r($array); ?></PRE ></TD ></TR ></TABLE ><P >
Obiges Beispiel wⁿrde folgende Ausgabe erzeugen: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( ) Array ( [5] => 6 ) Array ( [0] => 6 [1] => 7 )</PRE ></TD ></TR ></TABLE > </P ><P ></P ></DIV ></BLOCKQUOTE ></DIV ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.array.useful-funcs" >Nⁿtzliche Funktionen</A ></H3 ><P >
Es gibt viele nⁿtzliche Funktionen um mit Arrays zu arbeiten. Schauen Sie unter dem Abschnitt <A HREF="#ref.array" >Array Funktionen</A > nach. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Mit <A HREF="#function.unset" ><B CLASS="function" >unset()</B ></A > k÷nnen Sie Schlⁿssel eines Arrays l÷schen. Halten Sie sich vor Augen, dass das Array NICHT neu indiziert wird. Wenn Sie nur "gew÷hnliche Integer-Indizes" (bei Null beginnend, jeweils um eins steigend) verwenden, erreichen Sie die Neuindizierung durch Verwendung von <A HREF="#function.array-values" ><B CLASS="function" >array_values()</B ></A >. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3014" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array(1 => 'eins', 2 => 'zwei', 3 => 'drei'); unset($a[2]); /* erzeugt ein Array das wie folgt definiert worden wΣre: $a = array(1 => 'eins', 3 => 'drei'); und NICHT $a = array(1 => 'eins', 2 => 'drei'); */ $b = array_values($a); // Jetzt ist $b array(1 => 'eins', 2 =>'drei') ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ><P >
Die <A HREF="#control-structures.foreach" >foreach</A > Kontrollstruktur gibt es speziell fⁿr Arrays. Damit ist es leicht ein Array zu durchlaufen. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.array.donts" >Array do's und don'ts</A ></H3 ><DIV CLASS="sect3" ><H4 CLASS="sect3" ><A NAME="language.types.array.foo-bar" >Warum ist <VAR CLASS="literal" >$foo[bar]</VAR > falsch?</A ></H4 ><P >
Sie sollten immer Anfⁿhrungszeichen fⁿr einen String-Literal-Index eines Arrays benutzen. Zum Beispiel sollten Sie $foo['bar'] und nicht $foo[bar] benutzen. Aber warum ist $foo[bar] falsch? Vieleicht kennen Sie folgende Syntax aus alten Skripts: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3024" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo[bar] = 'Feind'; echo $foo[bar]; // usw ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Es ist falsch, funktioniert aber. Warum ist es dann falsch? Der Grund ist, dass dieser Code statt eines Strings ('bar'- beachten Sie die Anfⁿhrungszeichen) eine undefinierte Konstante (bar) enthΣlt und PHP in Zukunft Konstanten definieren k÷nnte, die, unglⁿcklicherweise fⁿr Ihren Code, den gleichen Namen verwenden. Es funktioniert, weil PHP einen <SPAN CLASS="emphasis" ><I CLASS="emphasis" >blo▀en String</I ></SPAN > (ein String ohne Anfⁿhrungszeichen, der keinem bekannten Symbol entpricht) automatisch in einen String umwandelt, der den blo▀en String enthΣlt. Wenn beispielsweise keine Konstante namens <TT CLASS="constant" ><B >bar</B ></TT > definiert ist, ersetzt es PHP durch den String <VAR CLASS="literal" >'bar'</VAR > und verwendet diesen. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Das bedeutet nicht, dass Schlⁿssel <SPAN CLASS="emphasis" ><I CLASS="emphasis" >immer</I ></SPAN > in Anfⁿhrungszeichen gesetzt werden mⁿssen. Wenn Sie nΣmlich Schlⁿssel, die <A HREF="#language.constants" >Konstanten</A > oder <A HREF="#language.variables" >Variablen</A > sind, in Anfⁿhrungszeichen setzen, werden diese von PHP nicht ausgewertet. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3034" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); // Ein einfaches Array: $array = array(1, 2); $anzahl = count($array); for ($i = 0; $i < $anzahl; $i++) { echo "\n▄berprⁿfe $i: \n"; echo "Schlecht: " . $array['$i'] . "\n"; echo "Gut: " . $array[$i] . "\n"; echo "Schlecht: {$array['$i']}\n"; echo "Gut: {$array[$i]}\n"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P ><B >Anmerkung: </B > Obiges Beispiel wⁿrde folgende Ausgabe erzeugen: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >▄berprⁿfe 0: Notice: Undefined index: $i in /path/to/script.html on line 9 Schlecht: Gut: 1 Notice: Undefined index: $i in /path/to/script.html on line 11 Schlecht: Gut: 1 ▄berprⁿfe 1: Notice: Undefined index: $i in /path/to/script.html on line 9 Schlecht: Gut: 2 Notice: Undefined index: $i in /path/to/script.html on line 11 Schlecht: Gut: 2</PRE ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ><P >
Weitere Beispiele zur Veranschaulichung dieses Sachverhalts: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3039" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Wir lassen uns alle Fehlermeldungen anzeigen error_reporting(E_ALL); $arr = array('obst' => 'Apfel', 'gemuese' => 'Karotte'); // Richtig print $arr['obst']; // Apfel print $arr['gemuese']; // Karotte // Falsch. Es funktioniert, gibt aber wegen der undefinierten // Konstante namens obst auch einen PHP-Fehler der Stufe E_NOTICE // aus: // // Notice: Use of undefined constant obst - assumed 'obst' in... print $arr[obst]; // Apfel // Definieren wir nun eine Konstante um zu veranschaulichen was // passiert. Wir weisen einer Konstanten namens obst den Wert // 'gemuese' zu. define('obst', 'gemuese'); // Beachten Sie jetzt den Unterschied print $arr['obst']; // Apfel print $arr[obst]; // Karotte // Folgendes ist in Ordnung, weil es innerhalb eines Strings ist. // Innerhalb eines Strings wird nicht nach Konstanten gesucht und // deshalb gibt es hier auch keinen E_NOTICE-Fehler. print "Hallo $arr[obst]"; // Hallo Apfel // Eine Ausnahme: innerhalb von Strings erm÷glichen {Klammern} // um Arrays, dass nach Konstanten gesucht wird print "Hallo {$arr[obst]}"; // Hallo Karotte print "Hallo {$arr['obst']}"; // Hallo Apfel // Das funktioniert nicht und ergibt in etwa folgenden Parser-Fehler: // Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING' // Das gilt natⁿrlich auch fⁿr die Verwendung von Autoglobalen in // Strings print "Hallo $arr['obst']"; print "Hallo $_GET['foo']"; // Eine andere M÷glichkeit ist die Verkettung print "Hallo " . $arr['obst']; // Hallo Apfel ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Wenn Sie <A HREF="#function.error-reporting" ><B CLASS="function" >error_reporting()</B ></A > auf Fehler der Stufe <TT CLASS="constant" ><B >E_NOTICE</B ></TT > einstellen (beispielsweise durch setzen auf <TT CLASS="constant" ><B >E_ALL</B ></TT >), werden Sie diese Fehlermeldungen zu sehen bekommen. StandardmΣ▀ig ist <A HREF="#ini.error-reporting" >error_reporting</A > so eingestellt, dass sie nicht angezeigt werden. </P ><P >
Wie im Abschnitt <A HREF="#language.types.array.syntax" >Syntax</A > ausgefⁿhrt, muss zwischen den eckigen Klammern ('<VAR CLASS="literal" >[</VAR >' und '<VAR CLASS="literal" >]</VAR >') ein Ausdruck stehen. Das hei▀t, dass Sie Dinge Σhnlich diesen schreiben k÷nnen: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3050" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo $arr[somefunc($bar)]; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Das ist ein Beispiel dafⁿr, wie Sie den Rⁿckgabewert einer Funktion als Array-Index verwenden k÷nnen. Ebenso kennt PHP Konstanten. Vielleicht haben Sie die <VAR CLASS="literal" >E_*</VAR > Konstanten schon einmal gesehen. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3053" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $error_descriptions[E_ERROR] = "Ein schwerwiegender Fehler ist aufgetreten"; $error_descriptions[E_WARNING] = "PHP hat eine Warnung ausgegeben"; $error_descriptions[E_NOTICE] = "Das ist nur eine informelle Notiz"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Beachten Sie, dass <VAR CLASS="literal" >E_ERROR</VAR > ebenso ein gⁿltiger Bezeichner ist, genau wie <VAR CLASS="literal" >bar</VAR > im ersten Beispiel. Das letzte Beispiel entspricht aber tatsΣchlich dem folgendem Code: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3057" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $error_descriptions[1] = "Ein schwerwiegender Fehler ist aufgetreten"; $error_descriptions[2] = "PHP hat eine Warnung ausgegeben"; $error_descriptions[8] = "Das ist nur eine informelle Notiz"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > weil <VAR CLASS="literal" >E_ERROR</VAR > gleich <VAR CLASS="literal" >1</VAR > ist, usw. </P ><P >
Wie schon in den obigen Beispielen erklΣrt wurde, funktioniert <VAR CLASS="literal" >$foo[bar]</VAR > zwar, ist aber falsch. Es funktioniert, weil <VAR CLASS="literal" >bar</VAR > aufgrund seiner Syntax als Konstante ausgewertet wird. In diesem Fall jedoch existiert keine Konstane mit dem Namen <VAR CLASS="literal" >bar</VAR >. PHP nimmt an, dass Sie <VAR CLASS="literal" >bar</VAR > w÷rtlich gemeint haben, wie den String <VAR CLASS="literal" >'bar'</VAR >, aber vergessen haben, die Anfⁿhrungszeichen zu setzen. </P ><DIV CLASS="sect4" ><HR><H5 CLASS="sect4" ><A NAME="AEN3067" >Aber warum ist es schlecht?</A ></H5 ><P >
Eines Tages k÷nnte sich das PHP-Team entscheiden eine weitere Konstante oder ein Schlⁿsselwort hinzuzufⁿgen oder Sie k÷nnten eine weitere Konstante in Ihre Anwendung einfⁿgen. Und dann geraten Sie in Schwierigkeiten. Zum Beispiel k÷nnen Sie bereits die W÷rter <VAR CLASS="literal" >empty</VAR > und <VAR CLASS="literal" >default</VAR > auf diese Art nicht benutzen, weil es speziele Schlⁿsselw÷rter sind. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Um es nochmals zu wiederholen, innerhalb eines Strings in doppelten Anfⁿhrungszeichen ist es zulΣssig, Array-Indizes ohne Anfⁿhrungszeichen zu schreiben, weshalb <VAR CLASS="literal" >"$foo[bar]"</VAR > gⁿltig ist. Schauen Sie sich fⁿr Einzelheiten darⁿber, warum das so ist, obige Beispiele und den Abschnitt ⁿber <A HREF="#language.types.string.parsing" >Variablen-Analyse in Strings</A > an. </P ></BLOCKQUOTE ></DIV ></DIV ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.array.casting" >Umwandlung nach array</A ></H3 ><P >
Fⁿr die Typen Integer, Float, String, Boolean und Resource gilt: wenn Sie einen Wert in ein Array umwandeln, bekommen Sie ein Array mit einem Element (mit Index 0), das der skalare Wert ist, den Sie anfangs hatten. </P ><P >
Wenn Sie ein Objekt in ein Array umwandeln, bekommen Sie die Eigenschaften (Mitgliedsvariablen) des Objekts als Elemente des Arrays. Die Schlⁿssel sind die Namen der Mitgliedsvariablen. </P ><P >
Wenn Sie einen <TT CLASS="constant" ><B >NULL</B ></TT >-Wert in ein Array umwandeln, bekommen Sie ein leeres Array. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.array.comparing" >Vergleiche</A ></H3 ><P >
Arrays k÷nnen Sie mit der Funktion <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A > und mit den <A HREF="#language.operators.array" >Array Operatoren </A > vergleichen. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.array.examples" >Beispiele</A ></H3 ><P >
Der Array-Typ in PHP ist sehr vielseitig. Hier folgen einige Beispiele um Ihnen die MΣchtigkeit von Arrays zu veranschaulichen. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3097" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Das hier $a = array('Farbe' => 'rot', 'Geschmack' => 'sⁿ▀', 'Form' => 'rund', 'Name' => 'Apfel', 4 // Schlⁿssel ist 0 ); // ist komplett gleichwertig mit $a['Farbe'] = 'rot'; $a['Geschmack'] = 'sⁿ▀'; $a['Form'] = 'rund'; $a['Name'] = 'Apfel'; $a[] = 4; // Schlⁿssel ist 0 $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; /* ergibt das Array: array(0 => 'a', 1 => 'b', 2 => 'c'), oder einfacher: array('a', 'b', 'c') */ ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3099" ></A ><P ><B >Beispiel 10-4. Verwenden von <A HREF="#function.array" ><B CLASS="function" >array()</B ></A ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Array als (Eigenschafts-)Abbildung $map = array('version' => 4, 'OS' => 'Linux', 'Sprache' => 'englisch', 'short_tags' => true ); // strikt numerische Schlⁿssel $array = array(7, 8, 0, 156, -10, ); // Dies entspricht: array(0 => 7, 1 => 8, ...) // wechselnde Schlⁿssel $wechselnd = array( 10, // Schlⁿssel: 0 5 => 6, 3 => 7, 'a' => 4, 11, // Schlⁿssel: 6 (Maximum der Integer-Indices war 5) '8' => 2, // Schlⁿssel: 8 (integer!) '02' => 77, // Schlⁿssel: '02' 0 => 12 // 10 wird mit dem neuen Wert 12 ⁿberschrieben ); // leeres Array $leer = array(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="language.types.array.examples.loop" ></A ><P ><B >Beispiel 10-5. Sammlung</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $farben = array('rot', 'blau', 'grⁿn', 'gelb'); foreach ($farben as $farbe) { echo "GefΣllt Ihnen $farbe?\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Ausgabe: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >GefΣllt Ihnen rot? GefΣllt Ihnen blau? GefΣllt Ihnen grⁿn? GefΣllt Ihnen gelb?</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Beachten Sie, dass es gegenwΣrtig nicht m÷glich ist die Werte eines Arrays direkt in einer solchen Schleife zu Σndern. Eine L÷sung ist folgendes: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="language.types.array.examples.changeloop" ></A ><P ><B >Beispiel 10-6. Sammlung</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php foreach ($farben as $schluessel => $farbe) { // funktioniert nicht: //$farbe = strtoupper($farbe); // funktioniert: $farben[$schluessel] = strtoupper($farbe); } print_r($farben); ?></PRE ></TD ></TR ></TABLE ><P >
Ausgabe: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => ROT [1] => BLAU [2] => GR▄N [3] => GELB )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Dieses Beispiel erzeugt ein Array, dessen Schlⁿssel bei eins beginnt. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3115" ></A ><P ><B >Beispiel 10-7. 1-basierter Index</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $erstes_viertel = array(1 => 'Januar', 'Februar', 'MΣrz'); print_r($erstes_viertel); ?></PRE ></TD ></TR ></TABLE ><P >
Ausgabe: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [1] => 'Januar' [2] => 'Februar' [3] => 'MΣrz' )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3120" ></A ><P ><B >Beispiel 10-8. Fⁿllen eines Arrays</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Fⁿllen eines Arrays mit allen Inhalten eines Verzeichnisses $handle = opendir('.'); while (false !== ($datei = readdir($handle))) { $dateien[] = $datei; } closedir($handle); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Arrays sind geordnet. Diese Ordnung k÷nnen Sie mit Hilfe verschiedener Sortierfunktionen verΣndern. Fⁿr mehr Informationen schauen Sie unter dem Abschnitt zu den <A HREF="#ref.array" >Array Funktionen</A > nach. Die Anzahl der Elemente in einem Array k÷nnen Sie mit der Funktion <A HREF="#function.count" ><B CLASS="function" >count()</B ></A > zΣhlen. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3126" ></A ><P ><B >Beispiel 10-9. Sortieren eines Arrays</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php sort($dateien); print_r($dateien); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Weil der Wert eines Arrays alles sein kann, kann dieser Wert wiederum ein Array sein. Somit k÷nnen Sie rekursive und multidimensionale Arrays erzeugen. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3130" ></A ><P ><B >Beispiel 10-10. Rekursive and multidimensionale Arrays</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("frⁿchte" => array("a" => "Orange", "b" => "Banane", "c" => "Apfel" ), "nummern" => array(1, 2, 3, 4, 5, 6 ), "l÷cher" => array( "erstens", 5 => "zweitens", "drittens" ) ); // Ein paar Beispiele zum Ansprechen der Werte aus obigem Array echo $fruits["l÷cher"][5]; // gibt "zweitens" aus echo $fruits["frⁿchte"]["a"]; // gibt "Orange" aus unset($fruits["l÷cher"][0]); // entfernt "erstens" // Erzeugen eines neuen mehrdimensionalen Arrays $saefte["Apfel"]["gruen"] = "gut"; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Sie sollten wissen, dass beim Zuweisen eines Arrays die Werte immer kopiert werden. Um ein Array als Referenz zu kopieren, mⁿssen Sie den Referenz-Operator verwenden. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3134" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $arr1 = array(2, 3); $arr2 = $arr1; $arr2[] = 4; // $arr2 wird verΣndert, // $arr1 ist immer noch array(2, 3) $arr3 = &$arr1; $arr3[] = 4; // nun sind $arr1 und $arr3 gleich ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.object" >Objekte</A ></H2 ><DIV CLASS="sect2" ><H3 CLASS="sect2" ><A NAME="language.types.object.init" >Objekt-Initialisierung</A ></H3 ><P >
Um ein Objekt zu initialisieren benutzen sie die Angabe <VAR CLASS="literal" >new</VAR >, dadurch wird das Objekt einer Variablen-Instanz zugewiesen. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3142" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class foo { function tue_foo() { echo "Tue gerade foo."; } } $bar = new foo; $bar->tue_foo(); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Fⁿr eine ausfⁿhrliche Darstellung lesen Sie das Kapitel <A HREF="#language.oop" >Klassen und Objekte</A > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.object.casting" >Umwandlung nach object</A ></H3 ><P >
Wenn ein Objekt in ein Objekt umgewandelt wird, wird es nicht verΣndert. Wenn ein Wert eines anderen Typs in ein Objekt umgewandelt wird, wird eine neue Instanz der eingebauten Klasse <VAR CLASS="literal" >stdClass</VAR > erzeugt. War der Wert Null, dann ist die neue Instanz leer. Bei jedem anderen Wert enthΣlt eine Mitgliedsvariable namens <VAR CLASS="literal" >scalar</VAR > den Wert. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3151" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $obj = (object) 'ciao'; echo $obj->scalar; // gibt 'ciao' aus ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.resource" >Resource</A ></H2 ><P >
Eine Resource ist eine spezielle Variable, die eine Referenz auf eine externe Ressource enthΣlt. Ressourcen werden von bestimmten Funktionen erzeugt und benutzt. Eine Liste dieser Funktionen und der entsprechenden Resource-Typen finden Sie im <A HREF="#resource" >Anhang</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der Resource-Typ wurde in PHP 4 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.resource.casting" >Umwandlung nach resource</A ></H3 ><P >
Da Resource-Typen spezielle Routinen zu ge÷ffneten Dateien, Datenbankverbindungen, Bildinformationen und ─hnlichem enthalten, k÷nnen Sie keinen Wert in eine Resource umwandeln. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.resource.self-destruct" >Freigabe von Ressourcen</A ></H3 ><P >
Aufgrund des Reference-Counting Systems, das mit PHP 4's Zend Egine eingefⁿhrt wurde, wird automatisch entdeckt, wenn auf eine Ressource nicht mehr zugegriffen wird (wie in Java). Wenn dies der Fall ist werden alle Ressourcen, die fⁿr diese Ressource in Gebrauch waren, durch den "Mⁿllsammler" (garbage collector) freigegeben. Aus diesem Grund ist es selten jemals notwendig, Speicher manuell durch Aufruf von free_result-Funktionen freizugeben. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Persistente Datenbank-Links stellen einen Sonderfall dar, sie werden durch den garbage collector <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nicht</I ></SPAN > zerst÷rt. Siehe auch den Abschnitt ⁿber <A HREF="#features.persistent-connections" >Persistente Datenbankverbindungen</A >. </P ></BLOCKQUOTE ></DIV > </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.null" >NULL</A ></H2 ><P >
Der spezielle Wert <TT CLASS="constant" ><B >NULL</B ></TT > steht dafⁿr, dass eine Varaiable keinen Wert hat. <TT CLASS="constant" ><B >NULL</B ></TT > ist der einzig m÷gliche Wert des Typs <A HREF="#language.types.null" ><B CLASS="type" >NULL</B ></A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der Null-Typ wurde in PHP 4 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ><P >
Eine Variable wird als <TT CLASS="constant" ><B >NULL</B ></TT > interpretiert, wenn <P ></P ><UL ><LI ><P >
ihr die Konstannte <TT CLASS="constant" ><B >NULL</B ></TT > als Wert zugewiesen wurde, </P ></LI ><LI ><P >
ihr bis jetzt kein Wert zugewiesen wurde, </P ></LI ><LI ><P >
sie mit <A HREF="#function.unset" ><B CLASS="function" >unset()</B ></A > gel÷scht wurde. </P ></LI ></UL > </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.null.syntax" >Syntax</A ></H3 ><P >
Es gibt nur einen Wert des Typs <TT CLASS="constant" ><B >NULL</B ></TT >: das Schlⁿsselwort <TT CLASS="constant" ><B >NULL</B ></TT >. Gro▀-/Kleinschreibung spielt keine Rolle. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3193" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $var = NULL; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Siehe auch <A HREF="#function.is-null" ><B CLASS="function" >is_null()</B ></A > und <A HREF="#function.unset" ><B CLASS="function" >unset()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.pseudo-types" >In dieser Dokumentation verwendete Pseudo-Typen</A ></H2 ><DIV CLASS="sect2" ><H3 CLASS="sect2" ><A NAME="language.types.mixed" >Mixed</A ></H3 ><P >
<VAR CLASS="literal" >Mixed</VAR > deutet darauf hin, dass ein Parameter mehrere (aber nicht notwendigerweise alle) Typen akzeptiert. </P ><P >
<A HREF="#function.gettype" ><B CLASS="function" >gettype()</B ></A > akzeptiert beispielsweise alle PHP-Typen, wΣhrend <A HREF="#function.str-replace" ><B CLASS="function" >str_replace()</B ></A > Strings und Arrays akzeptiert. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.number" >Number</A ></H3 ><P >
<VAR CLASS="literal" >Number</VAR > deutet darauf hin, dass ein Parameter entweder ein Integer oder ein Float sein kann. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.callback" >Callback</A ></H3 ><P >
Einige Funktionen wie <A HREF="#function.call-user-func" ><B CLASS="function" >call_user_func()</B ></A > oder <A HREF="#function.usort" ><B CLASS="function" >usort()</B ></A > akzeptieren benutzerdefinierte Callback-Funktionen als Parameter. Callback-Funktionen k÷nnen nicht nur einfache Funktionen sein, sondern auch Objektmethoden einschlie▀lich statischer Klassenmethoden. </P ><P >
Eine PHP-Funktion wird einfach durch Ihren Namen als String ⁿbergeben. Sie k÷nnen jede eingebaute oder benutzerdefinierte Funktion ⁿbergeben mit Ausnahme von <A HREF="#function.array" ><B CLASS="function" >array()</B ></A >, <A HREF="#function.echo" ><B CLASS="function" >echo()</B ></A >, <A HREF="#function.empty" ><B CLASS="function" >empty()</B ></A >, <A HREF="#function.eval" ><B CLASS="function" >eval()</B ></A >, <A HREF="#function.exit" ><B CLASS="function" >exit()</B ></A >, <A HREF="#function.isset" ><B CLASS="function" >isset()</B ></A >, <A HREF="#function.list" ><B CLASS="function" >list()</B ></A >, <A HREF="#function.print" ><B CLASS="function" >print()</B ></A > und <A HREF="#function.unset" ><B CLASS="function" >unset()</B ></A >. </P ><P >
Eine Methode eines instanzierten Objekts wird als Array ⁿbergeben, das ein Objekt als Element mit Index 0 und einen Methodennamen als Element mit Index 1 enthΣlt. </P ><P >
Statische Klassenmethoden k÷nnen auch ⁿbergeben werden, ohne ein Objekt dieser Klasse zu instanzieren, indem statt des Objekts der Name der Klasse als Element mit Index 0 ⁿbergeben wird. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3231" ></A ><P ><B >Beispiel 10-11. Beispiele zur Callback-Funktion</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // einfaches callback Beispiel function meine_callback_funktion() { echo 'hallo welt!'; } call_user_func('meine_callback_funktion'); // Beispiele fⁿr Callbackmethoden class MeineKlasse { function meineCallbackMethode() { echo 'Hallo Welt!'; } } // Aufruf einer statischen Klassenmethode ohne Instanzierung // eines Objekts call_user_func(array('MeineKlasse', 'meineCallbackMethode')); // Aufruf einer Objektmethode $obj = new MeineKlasse(); call_user_func(array(&$obj, 'meineCallbackMethode')); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.types.type-juggling" >Typen-Tricks</A ></H2 ><P >
PHP erfordert (bzw. unterstⁿtzt) keine expliziten Typ-Definitionen bei der Deklaration von Variablen; der Typ einer Variablen wird durch den Zusammenhang bestimmt in dem die Variable benutzt wird. Das bedeutet, dass bei der Zuweisung eines Strings zu einer Variablen <VAR CLASS="parameter" >$var</VAR > diese Variable <VAR CLASS="parameter" >$var</VAR > zum Typ String wird. Sollten Sie dananch der Variablen <VAR CLASS="parameter" >$var</VAR > einen Integer-Wert zuweisen, wird sie zu einem Integer-Typ. </P ><P >
Ein Beispiel fⁿr die automatische Typ-Konvertierung von PHP ist der Plus-Operator '+'. Ist einer der zu addierenden Werte vom Typ <A HREF="#language.types.float" ><B CLASS="type" >float</B ></A >, werden alle Werte als float-Typ behandelt. Auch das Ergebnis der Addition wird vom Typ float sein. Andernfalls werden die Werte als <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A >-Typen angesehen und das Ergebnis wird ebenfalls vom Typ Integer sein. Beachten Sie, dass hierdurch NICHT der Typ der Operanden selbst beeinflusst wird; der Unterschied liegt einzig und allein in der Auswertung dieser Operanden. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3243" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = "0"; // $foo ist vom Typ String (ASCII 48) $foo += 2; // $foo ist jetzt vom Typ Integer (2) $foo = $foo + 1.3; // $foo ist nun vom Typ float (3.3) $foo = 5 + "10 Sehr kleine Ferkel"; // $foo ist vom Typ Integer (15) $foo = 5 + "10 Kleine Schweine"; // $foo ist vom Typ Integer (15) ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Wenn Ihnen die beiden letzten Beispiele merkwⁿrdig erscheinen, beachten Sie die <A HREF="#language.types.string.conversion" >Umwandlung von Strings in Zahlen</A >. </P ><P >
Wenn Sie die Auswertung einer Variablen als einen speziellen Typ erzwingen wollen, beachten sie den Abschnitt in <A HREF="#language.types.typecasting" >Typ-Umwandlung</A >. Wollen Sie den Typ einer Variable Σndern, schauen Sie bei <A HREF="#function.settype" ><B CLASS="function" >settype()</B ></A > nach. </P ><P >
Um die Beispiele dieses Kapitels auszuprobieren, k÷nnen Sie die Funktion <A HREF="#function.var-dump" ><B CLASS="function" >var_dump()</B ></A > verwenden. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Das Verhalten einer automatischen Umwandlung zum Typ Array ist zur Zeit nicht definiert. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3254" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = "1"; // $a ist ein String $a[0] = "f"; // Was ist mit den String offsets? Was passiert? ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Weil PHP (aus historischen Grⁿnden) die Indizierung innerhalb von Strings mittels der gleichen Syntax wie bei der Array-Indizierung unterstⁿtzt, fⁿhrt das o.a. Beispiel zu einem Problem: Soll $a zu einem Array werden, dessen erstes Element ein "f" ist oder soll "f" das erste Zeichen des Strings $a werden? </P ><P >
Die aktuellen Versionen von PHP interpretieren die zweite Anweisung als Stringversatz-Kennung, weshalb $a zu "f" wird. Das Ergebnis dieser automatischen Umwandlung sollte jedoch als undefiniert betrachtet werden. Mit PHP 4 wurde die neue Syntax mit geschweiften Klammern eingefⁿhrt um auf Zeichen in einem String zuzugreifen. Verwenden Sie statt der oben vorgestellten Syntax folgende: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3258" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = "abc"; // $a ist ein String $a{1} = "f"; // $a ist jetzt "afc" ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Fⁿr weitere Informationen siehe Abschnitt <A HREF="#language.types.string.substr" >Zugriff auf Zeichen in Strings</A >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.types.typecasting" >Typ-Umwandlung</A ></H3 ><P >
Typ-Umwandlung in PHP funktioniert vielfach wie in C: Der Name des gewⁿnschten Typs wird vor der umzuwandelnden Variablen in Klammern gesetzt. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3264" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><?php $foo = 10; // $foo ist ein Integer-Wert $bar = (boolean) $foo; // $bar ist vom Typ boolean ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Folgende Umwandlungen sind m÷glich: <P ></P ><UL ><LI ><P >(int), (integer) - nach integer</P ></LI ><LI ><P >(bool), (boolean) - nach boolean</P ></LI ><LI ><P >(float), (double), (real) - nach float</P ></LI ><LI ><P >(string) - nach string</P ></LI ><LI ><P >(array) - nach array</P ></LI ><LI ><P >(object) - Wandlung zum Objekt</P ></LI ></UL > </P ><P >
Beachten sie, dass Tabulatoren und Leerzeichen innerhalb der Klammern erlaubt sind. Deshalb sind die folgenden Beispiele identisch: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3281" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = (int) $bar; $foo = ( int ) $bar; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Anstatt eine Variable in einen String umzuwandeln, k÷nnen Sie die Variable auch in doppelte Anfⁿhrungszeichen einschlie▀en. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3285" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = 10; // $foo ist ein Integer $str = "$foo"; // $str ist ein String $fst = (string) $foo; // $fst ist auch ein String // Das gibt aus "sie sind gleich" if ($fst === $str) { echo "sie sind gleich"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ><P >
Es ist nicht immer offenkundig, was bei der Typ-Umwandlung geschieht. Fⁿr weitere Informationen schauen Sie unter folgenden Abschnitten nach: <P ></P ><UL ><LI ><P >
<A HREF="#language.types.boolean.casting" >Umwandlung nach boolean</A > </P ></LI ><LI ><P >
<A HREF="#language.types.integer.casting" >Umwandlung nach integer</A > </P ></LI ><LI ><P >
<A HREF="#language.types.float.casting" >Umwandlung nach float</A > </P ></LI ><LI ><P >
<A HREF="#language.types.string.casting" >Umwandlung nach string</A > </P ></LI ><LI ><P >
<A HREF="#language.types.array.casting" >Umwandlung nach array</A > </P ></LI ><LI ><P >
<A HREF="#language.types.object.casting" >Umwandlung nach object</A > </P ></LI ><LI ><P >
<A HREF="#language.types.resource.casting" >Umwandlung nach resource</A > </P ></LI ><LI ><P >
<A HREF="#types.comparisons" >PHP type comparison tables</A > </P ></LI ></UL > </P ></DIV ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.variables" >Kapitel 11. Variablen</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="language.variables.basics" >Grundlegendes</A ></H2 ><P >
Variablen werden in PHP dargestellt durch ein Dollar-Zeichen ($) gefolgt vom Namen der Variablen. Bei Variablen-Namen wird zwischen Gro▀- und Kleinschreibung unterschieden (case-sensitive). </P ><P >
Variablen-Namen werden in PHP nach den gleichen Regeln wie andere Bezeichner erstellt. Ein gⁿltiger Variablen-Name beginnt mit einem Buchstaben oder einem Unterstrich ("_"), gefolgt von einer beliebigen Anzahl von Buchstaben, Zahlen oder Unterstrichen. Als regulΣrer Ausdruck (regular expression) wⁿrde das wie folgt ausgedrⁿckt: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Unserem Zweck entspricht also ein Buchstabe von a bis z bzw. A bis Z oder einem ASCII-Zeichen von 127 bis 255 (0x7f bis 0xff). </P ></BLOCKQUOTE ></DIV ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3322" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $var = "Du"; $vaR = "und"; $Var = "ich"; $vAr = "wir lernen PHP" echo "$var $vaR $Var, $vAr"; // gibt "Du und ich, wir lernen PHP" aus $4site = 'nicht jetzt'; // ungⁿltig, da Anfang eine Zahl $_4site = 'nicht jetzt'; // gⁿltig, da Unterstrich am Anfang $tΣbyte = 'irgendwas'; // gⁿltig, da 'Σ' dem (Erweiterten) ASCII-Wert 228 entspricht ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Variablen werden in PHP 3 durch ihren Wert bestimmt. Das heisst, wenn Sie einer Variablen einen Ausdruck zuweisen, wird der gesamte Inhalt des Originalausdrucks in die Zielvariable kopiert. Die Folge ist, dass eine Variable, die ihren Inhalt von einer anderen Variablen erhalten hat, ihren Inhalt behΣlt, auch wenn Sie danach den Inhalt der anderen (Quell- / Ursprungs-)Variablen Σndern. Die Inhalte der Ziel- und Quellvariablen sind also insoweit unabhΣngig voneinander. Fⁿr weitere Informationen lesen Sie bitte das Kapitel unter <A HREF="#language.expressions" >Expressions / Ausdrⁿcke</A >. </P ><P >
PHP 4 bietet eine andere M÷glichkeit der Wertzuweisung bei Variablen: <A HREF="#language.references" >Zuweisung durch Referenzierung</A >. Das bedeutet, dass der Wert der neuen Variablen eine Referenz zur Ursprungs-Variablen darstellt (mit anderen Worten: Der Wert ist ein Alias bzw. Zeiger auf den Inhalt der Ursprungsvariablen). Beide Variablen zeigen also auf die selbe(n) Speicherstelle(n). ─nderungen der neuen Variablen Σndern auch deren Ursprungs-Variable und umgekehrt. Der Wert / Inhalt wird also nicht kopiert. Die ▄bertragung geschieht dadurch auch schneller als in PHP 3. Dies wird sich aber nur bei umfangreichen Schleifen oder bei der ▄bertragung von grossen <A HREF="#language.types.array" >Arrays</A > oder <A HREF="#language.types.object" >Objekten</A > bemerkbar machen. </P ><P >
Fⁿr die Zuweisung per Referenz mⁿssen Sie lediglich ein & der (Ausgangs-, Quell-) Variablen voranstellen, die sie einer anderen Variablen zuweisen wollen. Der folgende Skript- Ausschnitt wird zweimal 'Mein Name ist Bob' ausgeben: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3331" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = 'Bob'; // 'Bob' der Variablen $foo zuweisen. $bar = &$foo; // Zeiger auf $foo in $bar erzeugen. $bar = "Ich heiße $bar"; // $bar verΣndern... echo $bar; echo $foo; // $foo wurde dadurch ebenfalls verΣndert. ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Zu beachten ist, dass nur Variablenbezeichner referenziert werden k÷nnen. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3334" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = 25; $bar = &$foo; // Gⁿltige Zuweisung. $bar = &(24 * 7); // Ungⁿltig, da kein Variablenbezeichner // zugewiesen wird. function test() { return 25; } $bar = &test(); // Ungⁿltig. ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.variables.predefined" >Vordefinierte Variablen</A ></H2 ><P >
PHP bietet jedem ausgefⁿhrtem Skript eine Vielzahl von vordefinierten Variablen an. Viele dieser Variablen k÷nnen jedoch nicht vollstΣndig erlΣutert werden, da sie abhΣngig sind vom Web-Server, der Version und dem Setup des Web- Servers sowie weiteren Faktoren. Einige dieser Variablen stehen nicht zur Verfⁿgung, wenn PHP-Skripte per <A HREF="#features.commandline" >Kommando-Zeilen-Aufruf</A > ausgefⁿhrt werden. Fⁿr eine Liste dieser Variablen lesen Sie bitte den Abschnitt <A HREF="#reserved.variables" >Vordefinierte Variablen</A >. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Ab PHP 4.2.0 ist der standardmΣ▀ige Wert fⁿr die PHP-Anweisung <A HREF="#ini.register-globals" >register_globals</A > <SPAN CLASS="emphasis" ><I CLASS="emphasis" >off</I ></SPAN >. Dies ist eine wesentliche ─nderung in PHP. Die Anweisung register_globals <SPAN CLASS="emphasis" ><I CLASS="emphasis" >off</I ></SPAN > beeinflusst den Satz von vordefinierten Variablen, die im globalen Bereich verfⁿgbar sind. Um zum Beispiel <VAR CLASS="varname" >DOCUMENT_ROOT</VAR > zu bekommen, mⁿssen Sie <VAR CLASS="varname" >$_SERVER['DOCUMENT_ROOT']</VAR > statt <VAR CLASS="varname" >$DOCUMENT_ROOT</VAR > verwenden oder um <VAR CLASS="varname" >$id</VAR > von der URL <VAR CLASS="literal" >http://www.example.com/test.php?id=3</VAR > zu bekommen <VAR CLASS="varname" >$_GET['id']</VAR > statt <VAR CLASS="varname" >$id</VAR > oder <VAR CLASS="varname" >$_ENV['HOME']</VAR > statt <VAR CLASS="varname" >$HOME</VAR >. </P ><P >
Fⁿr diese ─nderung betreffende Informationen lesen Sie bitte den Konfigurations-Eintrag fⁿr <A HREF="#ini.register-globals" >register_globals</A >, das Sicherheitskapitel ⁿber die <A HREF="#security.registerglobals" >Verwendung von Register Globals</A > und au▀erdem die PHP <A HREF="http://www.php.net/release_4_1_0.php" TARGET="_top" >4.1.0</A > und <A HREF="http://www.php.net/release_4_2_0.php" TARGET="_top" >4.2.0</A > Release Announcements. </P ><P >
Die reservierten vordefinierten Variablen, wie die <A HREF="#language.variables.superglobals" >Superglobalen Arrays</A >, sollten bevorzugt verwendet werden. </P ></TD ></TR ></TABLE ></DIV ><P >
Ab Version 4.1.0 stehen in PHP eine zusΣtzliche Reihe vordefinierter Arrays zur Verfⁿgung, die Variablen vom Webserver (gegebenenfalls), von der Umgebung und von Benutzereingaben enthalten. Diese neuen Arrays sind insofern etwas sehr Spezielles, als sie automatisch global sind -- d.h., sie stehen automatisch in jedem Bereich zur Verfⁿgung. Deshalb sind sie auch bekannt als 'Autoglobale' oder 'Superglobale'. (Es gibt in PHP keinen Mechanismus fⁿr benutzerdefinierte Superglobale.) Die Superglobale werden nachfolgend aufgelistet, aber fⁿr eine Liste ihres Inhalts und die weitere Diskussion vordefinierter Variablen und ihres Wesens lesen Sie bitte den Abschnitt <A HREF="#reserved.variables" >Reservierte vordefinierte Variablen</A >. Au▀erdem werden Sie feststellen, dass die alten vordefinierten Variablen (<VAR CLASS="varname" >$HTTP_*_VARS</VAR >) noch existieren. Seit PHP 5.0.0 k÷nnen Sie die Registrierung der langen von PHP <A HREF="#language.variables.predefined" >
vordefinierten Arrays</A > mit der Konfigurationsoption <A HREF="#ini.register-long-arrays" >register_long_arrays</A > abschalten. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Variable Variablen: </B > Superglobale k÷nnen nicht als <A HREF="#language.variables.variable" >Variable Variablen</A > verwendet werden. </P ></BLOCKQUOTE ></DIV ><P >
Falls bestimmte Variablen nicht unter <A HREF="#ini.variables-order" >variables_order</A > angegeben sind, dann bleiben auch ihre entsprechenden vordefinierten Arrays leer. </P ><P ></P ><DIV CLASS="variablelist" ><P ><B >PHP Superglobale</B ></P ><DL ><DT ><A HREF="#reserved.variables.globals" >$GLOBALS</A ></DT ><DD ><P >
Diese Variable enthΣlt eine Referenz zu jeder Variable, die im globalen Bereich des laufenden Skripts zur Verfⁿgung steht. Die Schlⁿssel dieses Arrays sind die Namen der globalen Variablen. <VAR CLASS="varname" >$GLOBALS</VAR > gibt es seit PHP 3. </P ></DD ><DT ><A HREF="#reserved.variables.server" >$_SERVER</A ></DT ><DD ><P >
Variablen, die vom Webserver gesetzt werden oder anderweitig direkt mit der ausfⁿhrenden Umgebung des aktuellen Skripts zusammenhΣngen. Entspricht dem alten <VAR CLASS="varname" >$HTTP_SERVER_VARS</VAR >-Array, das zwar noch zur Verfⁿgung steht, aber abzulehnen ist. </P ></DD ><DT ><A HREF="#reserved.variables.get" >$_GET</A ></DT ><DD ><P >
Variablen, die dem Skript ⁿber HTTP GET geliefert werden. Entspricht dem alten <VAR CLASS="varname" >$HTTP_GET_VARS</VAR >-Array, das zwar noch zur Verfⁿgung steht, aber abzulehnen ist. </P ></DD ><DT ><A HREF="#reserved.variables.post" >$_POST</A ></DT ><DD ><P >
Variablen, die dem Skript ⁿber HTTP POST geliefert werden. Entspricht dem alten <VAR CLASS="varname" >$HTTP_POST_VARS</VAR >-Array, das zwar noch zur Verfⁿgung steht, aber abzulehnen ist. </P ></DD ><DT ><A HREF="#reserved.variables.cookies" >$_COOKIE</A ></DT ><DD ><P >
Variablen, die dem Skript ⁿber HTTP Cookies geliefert werden. Entspricht dem alten <VAR CLASS="varname" >$HTTP_COOKIE_VARS</VAR >-Array, das zwar noch zur Verfⁿgung steht, aber abzulehnen ist. </P ></DD ><DT ><A HREF="#reserved.variables.files" >$_FILES</A ></DT ><DD ><P >
Variablen, die dem Skript ⁿber HTTP Post Datei-Uploads geliefert werden. Entspricht dem alten <VAR CLASS="varname" >$HTTP_POST_FILES</VAR >-Array, das zwar noch zur Verfⁿgung steht, aber abzulehnen ist. Fⁿr mehr Informationen lesen Sie bitte <A HREF="#features.file-upload.post-method" >Dateiuploads mit POST</A >. </P ></DD ><DT ><A HREF="#reserved.variables.environment" >$_ENV</A ></DT ><DD ><P >
Variablen, die dem Skript ⁿber die Umgebung geliefert werden. Entspricht dem alten <VAR CLASS="varname" >$HTTP_ENV_VARS</VAR >-Array, das zwar noch zur Verfⁿgung steht, aber abzulehnen ist. </P ></DD ><DT ><A HREF="#reserved.variables.request" >$_REQUEST</A ></DT ><DD ><P >
Variablen, die dem Skript ⁿber die GET-, POST- und COOKIE-Eingabemechanismen geliefert werden und von daher nicht vertrauenswⁿrdig sind. Die PrΣsenz und die Reihenfolge der Aufnahme der Variablen in dieses Array ist entsprechend der Konfigurationsanweisung <A HREF="#ini.variables-order" >variables_order</A > definiert. Dieses Array hat in PHP vor Version 4.1.0 keine direkte Entsprechung. Siehe auch <A HREF="#function.import-request-variables" ><B CLASS="function" >import_request_variables()</B ></A >. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Seit PHP 4.3.0 gibt es in <VAR CLASS="varname" >$_REQUEST</VAR > keine FILE-Informationen von <VAR CLASS="varname" >$_FILES</VAR > mehr. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Wenn PHP-Skripte auf der <A HREF="#features.commandline" >Kommandozeile</A > ausgefⁿhrt werden, sind die EintrΣge <VAR CLASS="varname" >argv</VAR > und <VAR CLASS="varname" >argc</VAR > <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nicht</I ></SPAN > in diesem Array enthalten; diese befinden sich im <VAR CLASS="varname" >$_SERVER</VAR >-Array. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A HREF="#reserved.variables.session" >$_SESSION</A ></DT ><DD ><P >
Variablen, die aktuell in der Session eines Skripts registriert sind. Entspricht dem alten <VAR CLASS="varname" >$HTTP_SESSION_VARS</VAR >-Array, das zwar noch zur Verfⁿgung steht, aber abzulehnen ist. Fⁿr mehr Informationen lesen Sie bitte den Abschnitt <A HREF="#ref.session" >Session Funktionen</A >. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.variables.scope" >Geltungsbereich von Variablen</A ></H2 ><P >
Der Geltungsbereich einer Variablen ergibt sich aus dem Zusammenhang, in dem sie definiert wurde. Meistens besteht dieser aus einem einzigen Bereich. Dieser beinhaltet auch den Bereich fⁿr Dateien, die per "include"- oder "require"-Anweisung eingebunden wurden, z.B.: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3446" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 1; include "b.inc"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Die Variable $a ist auch in der eingebundenen Datei <TT CLASS="filename" >b.inc</TT > verfⁿgbar. In benutzerdefinierten Funktionen wird ein auf die Funktion beschrΣnkter Geltungsbereich eingefⁿhrt. Jede in einer Funktion benutzte Variable ist zunΣchst auf den lokalen Bereich der Funktion beschrΣnkt, z.B.: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3450" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 1; // globaler Bereich function test () { echo $a; // Referenz auf einen lokalen Variablen-Bereich } test (); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Dieses Skript erzeugt keine Bildschirm-Ausgabe, da sich die Echo- Anweisung auf eine lokale Variable namens <VAR CLASS="varname" >$a</VAR > bezieht und dieser kein Wert im lokalen Bezug zugewiesen worden ist. Dies ist ein kleiner Unterschied zu C, wo globale Variablen auch in Funktionen vorhanden sind, es sei denn, sie werden durch eine funktionsinterne Definition ⁿberschrieben. Das kann zu Problemen fⁿhren, denn in PHP mⁿssen global geltende Variablen innerhalb von Funktionen als solche definiert werden. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.variables.scope.global" >Das global Schlⁿsselwort</A ></H3 ><P >
ZunΣchst ein Beispiel fⁿr die Verwendung von <VAR CLASS="literal" >global</VAR >: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3459" ></A ><P ><B >Beispiel 11-1. Die Verwendung von global</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 1; $b = 2; function Summe() { global $a, $b; $b = $a + $b; } Summe(); echo $b; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Das obige Skript gibt "3" aus. Durch das Deklararieren der Variablen <VAR CLASS="varname" >$a</VAR > und <VAR CLASS="varname" >$b</VAR >innerhalb der Funktion als global, weisen alle Referenzen zu beiden Variablen auf die nun globalen Werte. Es gibt keine BeschrΣnkungen bei der Anzahl an globalen Variablen, die durch eine Funktion verΣndert werden k÷nnen. </P ><P >
Eine weitere M÷glichkeit besteht in der Verwendung des speziellen <VAR CLASS="varname" >$GLOBALS</VAR > PHP-Array. Das obige Beispiel kann damit auch so geschrieben werden: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3468" ></A ><P ><B >Beispiel 11-2. Die Verwendung von <VAR CLASS="varname" >$GLOBALS</VAR > statt global </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 1; $b = 2; function Summe() { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } Summe(); echo $b; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Das <VAR CLASS="varname" >$GLOBALS</VAR >-Array ist ein assoziatives Array mit dem Bezeichner der globalen Variablen als Schlⁿssel und dem Inhalt dieser Variablen als Wert des Array-Elements. Beachten Sie, dass <VAR CLASS="varname" >$GLOBALS</VAR > in jedem Bereich existiert, weil $GLOBALS eine <A HREF="#language.variables.superglobals" >Superglobale</A > ist. Hier ist ein Beispiel, das die StΣrke von Superglobalen demonstriert: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3477" ></A ><P ><B >Beispiel 11-3. Beispiel zur Demonstration von Superglobalen und Bereich </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function test_global() { // Die meisten vordefinierten Variablen sind nicht "super" und // ben÷tigen 'global', um im lokalen Bereich von Funktionen zur // Verfⁿgung zu stehen. global $HTTP_POST_VARS; echo $HTTP_POST_VARS['name']; // Superglobale stehen in jedem Bereich zur Verfⁿgung und // ben÷tigen kein 'global'. Superglobale stehen seit PHP 4.1.0 // zur Verfⁿgung. echo $_POST['name']; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.variables.scope.static" >Die Verwendung von statischen Variablen</A ></H3 ><P >
Ein weiterer wichtiger Anwendungszweck von Variablen-Bereichen ist die <SPAN CLASS="emphasis" ><I CLASS="emphasis" >static</I ></SPAN >-Variable. Eine statische Variable existiert nur in einem lokalen Funktions-Bereich, der Wert geht beim Verlassen dieses Bereichs aber nicht verloren. Schauen Sie das folgende Beispiel an: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3485" ></A ><P ><B >Beispiel 11-4. Beispiel, das die Notwendigkeit von statischen Variablen demonstriert </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function test () { $a = 0; echo $a; $a++; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Diese Funktion ist sinnlos, da sie bei jedem Aufruf <VAR CLASS="varname" >$a</VAR > auf <VAR CLASS="literal" >0</VAR > setzt und "0" ausgibt. Die Anweisung $a++, welche den Wert erh÷ht, macht keinen Sinn, da der Wert von <VAR CLASS="varname" >$a</VAR > beim Verlassen der Funktion verloren geht. Um eine sinnvolle ZΣhlfunktion zu implementieren, die ihren aktuell gesetzten Wert nicht vergisst, mⁿssen Sie die Variable <VAR CLASS="varname" >$a</VAR >als "static" deklarieren: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3494" ></A ><P ><B >Beispiel 11-5. Beispiel zur Verwendung statischer Variablen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function Test() { static $a = 0; echo $a; $a++; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Jetzt wird bei jedem Aufruf der Test()-Funktion der aktuelle Wert von <VAR CLASS="varname" >$a</VAR > ausgegeben und dann um 1 erh÷ht. </P ><P >
Static-Variablen erm÷glichen auch einen Weg zum Umgang mit rekursiven Funktionen. Das sind Funktionen, die sich selbst aufrufen. Hierbei besteht die Gefahr, so genannte Endlos- Schleifen zu programmieren. Sie mⁿssen also einen Weg vorsehen, diese Rekursion zu beenden. Die folgende einfache Funktion zΣhlt rekursiv bis 10. Die statische Variable <VAR CLASS="varname" >$zaehler</VAR > wird benutzt, um die Rekursion zu beenden: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3502" ></A ><P ><B >Beispiel 11-6. Statische Variablen in rekursiven Funktionen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function Test() { static $zaehler = 0; $zaehler++; echo $zaehler; if ($zaehler < 10) { Test (); } $zaehler--; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Statische Variablen werden wie in oben stehenden Beispielen deklariert. Das Zuweisen eines Wertes, welcher das Ergebnis eines Ausdrucks ist, wird mit einem parse error quittiert. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3508" ></A ><P ><B >Beispiel 11-7. Statische Variablen deklarieren</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function foo(){ static $int = 0; // korrekt static $int = 1+2; // falsch (da ein Ausdruck vorliegt) static $int = sqrt(121); // falsch (ebenfalls ein Ausdruck) $int++; echo $int; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.variables.scope.references" >Referenzen bei globalen und statischen Variablen</A ></H3 ><P >
Die Zend Engine 1, die <VAR CLASS="literal" >PHP 4</VAR > zugrunde liegt, fⁿhrt die <A HREF="#language.variables.scope.static" >static</A >- und <A HREF="#language.variables.scope.global" >global</A >-Wandler fⁿr Variablen in Bezug auf <A HREF="#language.references" >Referenzen</A > aus. Zum Beispiel erzeugt eine echte globale Variable, die mit der Anweisung <VAR CLASS="literal" >global</VAR > in den Funktionsbereich importiert wurde, tatsΣchlich eine Referenz zur globalen Variable. Das kann zu einem unerwarteten Verhalten fⁿhren, auf das im folgenden Beispiel eingegangen wird: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3519" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function test_global_ref() { global $obj; $obj = &new stdclass; } function test_global_noref() { global $obj; $obj = new stdclass; } test_global_ref(); var_dump($obj); test_global_noref(); var_dump($obj); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Die Ausfⁿhrung dieses Beispiels erzeugt die folgende Ausgabe: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >NULL object(stdClass)(0) { }</PRE ></TD ></TR ></TABLE ><P >
Ein Σhnliches Verhalten gilt auch fⁿr die Anweisung <VAR CLASS="literal" >static</VAR >. Referenzen werden nicht statisch gespeichert: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3525" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function &get_instance_ref() { static $obj; echo "Statisches Objekt: "; var_dump($obj); if (!isset($obj)) { // Der statischen Variablen eine Referenz zuweisen $obj = &new stdclass; } $obj->eigenschaft++; return $obj; } function &get_instance_noref() { static $obj; echo "Statisches Objekt: "; var_dump($obj); if (!isset($obj)) { // Der statischen Variablen ein Objekt zuweisen $obj = new stdclass; } $obj->eigenschaft++; return $obj; } $obj1 = get_instance_ref(); $immer_noch_obj1 = get_instance_ref(); echo "\n"; $obj2 = get_instance_noref(); $immer_noch_obj2 = get_instance_noref(); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Die Ausfⁿhrung dieses Beispiels erzeugt die folgende Ausgabe: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Statisches Objekt: NULL Statisches Objekt: NULL Statisches Objekt: NULL Statisches Objekt: object(stdClass)(1) { ["eigenschaft"]=> int(1) }</PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel demonstriert, dass die Referenz, die einer statischen Variablen zugewiesen wird, beim zweiten Aufruf der Funktion <VAR CLASS="literal" >&get_instance_ref()</VAR > vergessen ist. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.variables.variable" >Variable Variablen</A ></H2 ><P >
Manchmal ist es komfortabel, variable Variablen-Bezeichner zu benutzen. Das bedeutet, einen Variablen-Namen zu setzen und dynamisch zu gebrauchen. Eine normale Variable wird wie folgt gebildet: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3534" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = "Hallo"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Eine variable Variable nimmt den Wert einer Variablen und behandelt ihn als Bezeichner der Variablen. Im obigen Beispiel kann <SPAN CLASS="emphasis" ><I CLASS="emphasis" >Hallo</I ></SPAN > als Variablen-Name gebraucht werden, indem man zwei $-Zeichen benutzt, also schreibt: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3538" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $$a = "Welt"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Nun existieren in der PHP-Symbol-Struktur zwei definierte und gespeicherte Variablen: <VAR CLASS="varname" >$a</VAR > mit dem Inhalt "Hallo" und <VAR CLASS="varname" >$Hallo</VAR > mit dem Inhalt "Welt". Deshalb wird die Anweisung </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3543" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo "$a ${$a}"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
zur genau gleichen Ausgabe fⁿhren wie: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3546" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><?php echo "$a $Hallo"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
also zu: <SAMP CLASS="computeroutput" >Hallo Welt</SAMP >. </P ><P >
Wenn Sie variable Variablen mit Arrays verwenden, mⁿssen Sie eine Doppeldeutigkeit beachten. Wenn Sie nΣmlich <VAR CLASS="varname" >$$a[1]</VAR > schreiben, dann muss der Parser wissen, ob Sie <VAR CLASS="varname" >$a[1]</VAR > als Variable oder <VAR CLASS="varname" >$$a</VAR > als Variable und dann [1] als Index dieser Variablen verwenden wollen bzw. gemeint haben. Die Syntax zur L÷sung dieser Doppeldeutigkeit: Verwenden Sie im ersten Fall <VAR CLASS="varname" >${$a[1]}</VAR > und im zweiten Fall <VAR CLASS="varname" >${$a}[1]</VAR >. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Bitte beachten Sie, dass variable Variablen nicht bei <A HREF="#language.variables.superglobals" >Superglobalen Arrays</A > verwendet werden k÷nnen. Das bedeutet, etwas wie <VAR CLASS="varname" >${$_GET}</VAR > ist nicht m÷glich. Wenn Sie nach einer M÷glichkeit suchen, die Verfⁿgbarkeit von Superglobalen und den alten <VAR CLASS="varname" >HTTP_*_VARS</VAR > zu handhaben, k÷nnen Sie das mit <A HREF="#language.references" >referenzieren</A > versuchen. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.variables.external" >Variablen ausserhalb von PHP</A ></H2 ><DIV CLASS="sect2" ><H3 CLASS="sect2" ><A NAME="language.variables.external.form" >HTML-Formulare (GET and POST)</A ></H3 ><P >
Sobald ein Formular an ein PHP-Skript ⁿbergeben wird, werden die Informationen dieses Formulars dem Skript automatisch verfⁿgbar gemacht. Es gibt viele M÷glichkeiten, auf diese Informationen zuzugreifen, zum Beispiel: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3568" ></A ><P ><B >Beispiel 11-8. Ein einfaches HTML-Formular</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><form action="foo.php" method="post"> Name: <input type="text" name="username" /><br /> Email: <input type="text" name="email" /><br /> <input type="submit" name="submit" value="Und ab!" /> </form></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
AbhΣngig von Ihrem speziellen Setup und Ihren pers÷nlichen Vorlieben gibt es viele M÷glichkeiten, auf die Daten von Ihren HTML-Formularen zuzugreifen. Hier einige Beispiele: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3573" ></A ><P ><B >Beispiel 11-9. Zugriff auf die Daten von einem einfachen POST HTML-Formular </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><?php // Seit PHP 4.1.0 verfⁿgbar echo $_POST['benutzername']; echo $_REQUEST['benutzername']; import_request_variables('p', 'p_'); echo $p_benutzername; // Seit PHP 3 verfⁿgbar. Ab PHP 5.0.0 k÷nnen diese langen // vordefinierten Variablen mit der Anweisung register_long_arrays // deaktiviert werden. echo $HTTP_POST_VARS['benutzername']; // Verfⁿgbar, falls die PHP-Anweisung register_globals = on. Ab // PHP 4.2.0 ist der standardmΣ▀ige Wert von register_globals = off. // Es ist nicht empfehlenswert, diese Methode zu verwenden, bzw. sich // darauf zu verlassen. echo $benutzername; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Die Verwendung eines GET Formulars ist, davon abgesehen, dass Sie stattdessen die entsprechende vordefinierte GET-Variable erhalten, Σhnlich. Au▀erdem wird GET auch fⁿr den QUERY_STRING (die Information nach dem '?' in einer URL) verwendet. So enthΣlt zum Beispiel <VAR CLASS="literal" >http://www.example.com/test.php?id=3</VAR > GET-Daten, auf die mit <VAR CLASS="varname" >$_GET['id']</VAR > zugegriffen werden kann. Siehe auch <A HREF="#reserved.variables.request" >$_REQUEST</A > und <A HREF="#function.import-request-variables" ><B CLASS="function" >import_request_variables()</B ></A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <A HREF="#language.variables.superglobals" >Superglobale Arrays</A > wie <VAR CLASS="varname" >$_POST</VAR > und <VAR CLASS="varname" >$_GET</VAR > stehen seit PHP 4.1.0 zur Verfⁿgung. </P ></BLOCKQUOTE ></DIV ><P >
Wie gezeigt, war <A HREF="#ini.register-globals" >register_globals</A > vor PHP 4.2.0 standardmΣ▀ig <SPAN CLASS="emphasis" ><I CLASS="emphasis" >on</I ></SPAN >. In PHP 3 war es immer aktiviert. Die PHP-Gemeinschaft ermuntert alle, sich nicht auf diese Anweisung zu stⁿtzen, weil es vorzuziehen ist, davon auszugehen, dass sie <SPAN CLASS="emphasis" ><I CLASS="emphasis" >off</I ></SPAN > ist und entsprechend zu programmieren. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die Konfigurationseinstellung zu <A HREF="#ini.magic-quotes-gpc" >magic_quotes_gpc</A > betrifft Get-, Post- und Cookie-Werte. Ist diese Einstellung aktiv, wird der Wert (It's "PHP!") automatisch zu (It\'s \"PHP!\"). Escaping ist notwendig, wenn Sie ihre Daten in eine Datenbank einfⁿgen wollen. Siehe auch <A HREF="#function.addslashes" ><B CLASS="function" >addslashes()</B ></A >, <A HREF="#function.stripslashes" ><B CLASS="function" >stripslashes()</B ></A > und <A HREF="#ini.magic-quotes-sybase" >magic_quotes_sybase</A >. </P ></BLOCKQUOTE ></DIV ><P >
Im Zusammenhang mit Formular-Variablen versteht PHP auch Arrays (siehe auch die <A HREF="#faq.html" >verwandte Faq</A >). Sie k÷nnen z.B. die betreffenden Variablen gruppieren oder dieses Leistungsmerkmal nutzen, um Werte aus Mehrfach-Auswahl-Bereichen zu erhalten. Schicken wir zum Beispiel ein Formular an sich selbst und lassen nach dem Abschicken die Daten anzeigen: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3599" ></A ><P ><B >Beispiel 11-10. Komplexere Formular-Variablen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if (isset($_POST['action']) && $_POST['action'] == 'abgeschickt') { echo '<pre>'; print_r($_POST); echo '<a href="'. $_SERVER['PHP_SELF'] .'">Bitte versuchen Sie es nochmal</a>'; echo '</pre>'; } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Name: <input type="text" name="personal[name]" /><br /> Email: <input type="text" name="personal[email]" /><br /> Bier: <br /> <select multiple name="bier[]"> <option value="warthog">Warthog</option> <option value="guinness">Guinness</option> <option value="stuttgarter">Stuttgarter SchwabenbrΣu</option> </select><br /> <input type="hidden" name="action" value="abgeschickt" /> <input type="submit" name="submit" value="Und ab!" /> </form> <?php } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In PHP 3 ist die Verwendung von Arrays in Formularvariablen auf eindimensionale Arrays beschrΣnkt. In PHP 4 besteht diese EinschrΣnkung nicht mehr. </P ><DIV CLASS="sect3" ><HR><H4 CLASS="sect3" ><A NAME="language.variables.external.form.submit" >IMAGE SUBMIT Variablen-Bezeichner</A ></H4 ><P >
Zur ▄bertragung eines Formulars kann auch ein Bild (Image) statt eines ▄bertragungs-Schalters (Submit-Button) benutzt werden, dessen Tag wie folgt aussieht: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3606" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><input type="image" src="image.gif" name="sub" /></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Klickt der Benutzer irgendwo auf das Bild, wird das entsprechende Formular an den Web-Server ⁿbertragen. Hierbei sind zwei zusΣtzliche Variablen vorhanden, sub_x und sub_y. Diese enthalten die Koordinaten des Klick-Punktes innerhalb des Bildes. Die Erfahreneren werden sagen, dass die Variablen, die vom Browser gesendet werden einen Punkt enthalten statt eines Unterstrichs. Dieser Punkt wird von PHP automatisch in einen Unterstrich verwandelt. </P ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.variables.external.cookies" >HTTP-Cookies</A ></H3 ><P >
PHP unterstⁿtzt HTTP-Cookies, wie sie in <A HREF="http://www.netscape.com/newsref/std/cookie_spec.html" TARGET="_top" >Netscape's Spec</A > definiert sind. Cookies erm÷glichen die Daten-Speicherung innerhalb der jeweiligen Browser-Umgebung zur Weiterleitung oder wiederholten Identifikation von Benutzern. Sie k÷nnen Cookies erzeugen, indem Sie die Funktion <A HREF="#function.setcookie" ><B CLASS="function" >setcookie()</B ></A > benutzen. Cookies sind Teil des HTTP-Headers, deshalb muss die setcookie-Funktion aufgerufen werden, bevor irgendeine Ausgabe an den Browser gesendet wird. Dabei handelt es sich um die gleiche EinschrΣnkung, die auch fⁿr die <A HREF="#function.header" ><B CLASS="function" >header()</B ></A >-Funktion gilt. Cookie-Daten stehen dann sowohl in den entsprechenden Cookie-Daten-Arrays, wie zum Beispiel <VAR CLASS="varname" >$_COOKIE</VAR >, <VAR CLASS="varname" >$HTTP_COOKIE_VARS</VAR > als auch in <VAR CLASS="varname" >$_REQUEST</VAR > zur Verfⁿgung. Fⁿr weitere Details und Beispiele lesen Sie bitte die <A HREF="#function.setcookie" ><B CLASS="function" >setcookie()</B ></A >-Seite des Handbuchs. </P ><P >
Wenn Sie einer einzelnen Cookie-Variable mehrere Werte zuweisen wollen, mⁿssen Sie diese als Array ⁿbertragen. Zum Beispiel: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3620" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php setcookie("MeinCookie[foo]", "Ich teste 1", time()+3600); setcookie("MeinCookie[bar]", "Ich teste 2", time()+3600); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Das erzeugt zwei einzelne Cookies obwohl MeinCookie in Ihrem Skript nun ein einziges Array ist. Wenn Sie nur ein Cookie mit mehreren Werten setzen wollen, sollten Sie zuerst <A HREF="#function.serialize" ><B CLASS="function" >serialize()</B ></A > oder <A HREF="#function.explode" ><B CLASS="function" >explode()</B ></A > auf das Array anwenden. </P ><P >
Bedenken Sie, dass ein Cookie ein vorhergehendes Cookie gleichen Namens ⁿberschreibt, es sei denn, der Pfad oder die Domain ist anders. Fⁿr eine Warenkorb-Anwendung k÷nnen Sie deshalb z.B. einen ZΣhler bilden und diesen weiterleiten: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3626" ></A ><P ><B >Beispiel 11-11. Ein <A HREF="#function.setcookie" ><B CLASS="function" >setcookie()</B ></A >-Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if (isset($_COOKIE['zaehler'])) { $zaehler = $_COOKIE['zaehler'] + 1; } else { $zaehler = 1; } setcookie("zaehler", $zaehler, time()+3600); setcookie("Cart[$zaehler]", $item, time()+3600); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.variables.external.dot-in-names" >Punkte in eingelesenen Variablen-Bezeichnern</A ></H3 ><P >
Normalerweise verΣndert PHP die Variablen-Bezeichner nicht, wenn sie einem Skript ⁿbergeben werden. Es sollte aber beachtet werden, dass der Punkt (".") kein gⁿltiger Bestandteil eines Variablen-Bezeichners ist. Deshalb achten Sie auf folgendes: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $varname.ext; /* ungⁿltiger Variablen-Bezeichner */ ?></PRE ></TD ></TR ></TABLE > Der PHP-Parser sieht eine Variable namens <VAR CLASS="varname" >$varname</VAR >, gefolgt von einem Zeichenketten-Verbindungs-Operator, dieser wiederrum gefolgt von der offenen Zeichenkette 'ext' (also nicht eingegrenzt durch '"' und auch keinem Schlⁿssel oder reserviertem Bezeichner entsprechend). Das kann natⁿrlich nicht zum gewⁿnschten Ergebnis fⁿhren. </P ><P >
Deshalb ist es wichtig zu wissen, dass PHP in den ihm ⁿbergebenen Variablen alle Punkte (.) automatisch durch einen Unterstrich (_) ersetzt. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.variables.determining-type-of" >Bestimmung des Variablen-Typs</A ></H3 ><P >
Da PHP den Typ der Variablen bestimmt und (im Allgemeinen) selbst eine entsprechende Umformung vornimmt, ist es nicht immer klar, welchen Typ eine Variable gerade hat. PHP beinhaltet einige Funktionen, die dies herausfinden, wie zum Beispiel: <A HREF="#function.gettype" ><B CLASS="function" >gettype()</B ></A >, <A HREF="#function.is-array" ><B CLASS="function" >is_array()</B ></A >, <A HREF="#function.is-float" ><B CLASS="function" >is_float()</B ></A >, <A HREF="#function.is-int" ><B CLASS="function" >is_int()</B ></A >, <A HREF="#function.is-object" ><B CLASS="function" >is_object()</B ></A > und <A HREF="#function.is-string" ><B CLASS="function" >is_string()</B ></A >. Lesen Sie bitte auch das Kapitel ⁿber <A HREF="#language.types" >Typen</A >. </P ></DIV ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.constants" >Kapitel 12. Konstanten</A ></H1 ><P >
Eine Konstante ist ein Bezeichner (Name) fⁿr eine simple Variable. Wie der Name schon ausdrⁿckt, kann sich der Wert einer Konstanten zur Laufzeit eines Skripts nicht Σndern (eine Ausnahme bilden die <A HREF="#language.constants.predefined" >Magischen Konstanten</A >, die aber tatsΣchlich keine Konstanten sind.) Eine Konstante unterscheidet zwischen Gro▀- und KLeinschreinbung (case-sensitive). Nach gΣngiger Konvention werden Konstanten immer in Gro▀buchstaben geschrieben. </P ><P >
Der Name einer Konstanten folgt den gleichen Regeln wie alle anderen Bezeichner in PHP. Ein gⁿltiger Name beginnt mit einem Buchstaben oder einem Unterstrich, gefolgt von beliebig vielen Buchstaben, Nummern oder Understrichen. Als regulΣrer Ausdruck geschrieben: <VAR CLASS="literal" >[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</VAR > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3653" ></A ><P ><B >Beispiel 12-1. Gⁿltige und ungⁿltige Namen fⁿr Konstanten</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Gⁿltige Namen fⁿr Konstanten define("FOO", "irgendwas"); define("FOO2", "etwas anderes"); define("FOO_BAR", "irgendwas ganz anderes") // Invalid constant names define("2FOO", "irgendwas"); // Folgendes ist korrekt, sollte aber vermieden werden: // Eines Tages k÷nnte es in PHP eine Magische Konstante // __FOO__ geben und Ihr Skript wird dadurch nicht mehr // wie gewⁿnscht funktionieren. define("__FOO__", "irgendwas"); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Fⁿr unsere Zwecke ist ein Buchstabe a-z, A-Z und die ASCII-Zeichen von 127 bis 255 (0x7f-0xff). </P ></BLOCKQUOTE ></DIV ><P >
Wie bei <A HREF="#language.variables.predefined" >superglobals</A > ist der Gⁿltigkeitsbereich einer Konstanten global. UnabhΣngig vom Gⁿltigkeitsbereich k÷nnen Sie in Ihrem Skript ⁿberall auf eine Konstante zugreifen. Fⁿr mehr Information zum Gⁿltigkeitsbereich lesen Sie bitte den Abschnitt ⁿber den <A HREF="#language.variables.scope" >Geltungsbereich von Variablen</A >. For more information on scope, read the manual section on <A HREF="#language.variables.scope" >variable scope</A >. </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.constants.syntax" >Syntax</A ></H2 ><P >
Eine Konstante k÷nnen Sie ⁿber die Funktion <A HREF="#function.define" ><B CLASS="function" >define()</B ></A > definieren. Einmal definiert, kann eine Konstane weder verΣndert noch gel÷scht werden. </P ><P >
Konstanten k÷nnen nur scalare Daten (<A HREF="#language.types.boolean" ><B CLASS="type" >boolean</B ></A >, <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A >, <A HREF="#language.types.float" ><B CLASS="type" >float</B ></A > und <A HREF="#language.types.string" ><B CLASS="type" >string</B ></A >) enthalten. </P ><P >
Den Wert einer Konstanten erhalten Sie ganz einfach durch die Angabe ihres Namens. Einer Konstanten sollten Sie <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nicht</I ></SPAN >, wie bei Variablen, ein <VAR CLASS="literal" >$</VAR > voranstellen. Ebenso k÷nnen Sie die Funktion <A HREF="#function.constant" ><B CLASS="function" >constant()</B ></A > benutzen um den Wert einer Konstanten auszulesen, wenn Sie den Namen der Konstanten dynamisch erhalten wollen. Benutzen Sie <A HREF="#function.get-defined-constants" ><B CLASS="function" >get_defined_constants()</B ></A > um eine Liste aller definierten Konstanten zu erhalten. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Konstanten und (globale) Variablen befinden sich in unterschiedlichen NamensrΣumen. Das hat zum Beispiel zur Folge, dass <TT CLASS="constant" ><B >TRUE</B ></TT > und <VAR CLASS="varname" >$TRUE</VAR > grundsΣtzlich unterschiedlich sind. </P ></BLOCKQUOTE ></DIV ><P >
Falls Sie eine undefinierte Konstante verwenden nimmt PHP an, dass Sie den Namen der Konstanten selber meinen, genauso als ob Sie einen <A HREF="#language.types.string" ><B CLASS="type" >string</B ></A > (CONSTANT vs "CONSTANT") angegeben hΣtten. Falls das passiert, wird Ihnen ein Fehler vom Typ <A HREF="#ref.errorfunc" >E_NOTICE</A > ausgegeben. Lesen Sie hier nach, warum die Angabe <A HREF="#language.types.array.foo-bar" >$foo[bar]</A > falsch ist (zumindest solange Sie nicht zuerst <VAR CLASS="literal" >bar</VAR > mittels <A HREF="#function.define" ><B CLASS="function" >define()</B ></A > als Konstante definiert haben). M÷chten Sie einfach nur nachprⁿfen, ob eine Konstante definiert ist, benutzen Sie die Funktion <A HREF="#function.defined" ><B CLASS="function" >defined()</B ></A > fⁿr diesen Zweck. </P ><P >
Das hier sind die Unterschiede zwischen Konstanten und Variablen: <P ></P ><UL ><LI ><P >
Konstanten haben kein Dollarzeichen (<VAR CLASS="literal" >$</VAR >) vorangestellt; </P ></LI ><LI ><P >
Konstanten k÷nnen nur ⁿber die Funktion <A HREF="#function.define" ><B CLASS="function" >define()</B ></A > definiert werden, nicht durch einfache Zuweisung; </P ></LI ><LI ><P >
Konstanten k÷nnen ⁿberall definiert werden und auf Ihren Wert k÷nnen Sie ohne Rⁿcksicht auf Namensraumregeln von Variablen zugreifen; </P ></LI ><LI ><P >
Sobald Konstanten definiert sind, k÷nnen sie nicht neu definiert oder gel÷scht werden; und </P ></LI ><LI ><P >
Konstanten k÷nnen nur skalare Werte haben. </P ></LI ></UL > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3702" ></A ><P ><B >Beispiel 12-2. Definiton von Konstanten</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php define("CONSTANT", "Hallo Welt."); echo CONSTANT; // Ausgabe: "Hallo Welt." echo Constant; // Ausgabe: "Constant" und eine Notice. ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.constants.predefined" >Magische Konstanten</A ></H2 ><P >
PHP stellt jedem Skript eine Vielzahl von <A HREF="#reserved.constants" >vordefinierten Konstanten</A > zur Verfⁿgung. Viele dieser Konstanten werden jedoch von verschiedenen Erweiterungen definiert, die nur zur Verfⁿgung stehen, wenn diese Erweiterungen selbst zur Verfⁿgung stehen, d.h. entweder ⁿber dynamisches Laden zur Laufzeit oder sie sind einkompiliert. </P ><P >
Es gibt fⁿnf magische Konstanten, die abhΣngig davon, wo sie eingesetzt werden, einen unterschiedlichen Wert haben. Zum Beispiel hΣngt der Wert der Konstanten <TT CLASS="constant" ><B >__LINE__</B ></TT > davon ab, in welcher Zeile ihres Skripts Sie diese Konstante verwenden. Diese besonderen Konstanten sind unabhΣngig von Gro▀-/Kleinschreibung und sind folgende: </P ><P >
<DIV CLASS="table" ><A NAME="AEN3712" ></A ><P ><B >Tabelle 12-1. Einige "magische" PHP "Konstanten"</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Name</TH ><TH >Beschreibung</TH ></TR ></THEAD ><TBODY ><TR ><TD ><TT CLASS="constant" ><B >__LINE__</B ></TT ></TD ><TD >
Die aktuelle Zeilennummer einer Datei. </TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >__FILE__</B ></TT ></TD ><TD >
Der vollstΣndige Pfad- und Dateiname einer Datei. </TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >__FUNCTION__</B ></TT ></TD ><TD >
Der Name einer Funktion. Steht seit PHP 4.3.0 zur Verfⁿgung. </TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >__CLASS__</B ></TT ></TD ><TD >
Der Name einer Klasse. Steht seit PHP 4.3.0 zur Verfⁿgung. </TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >__METHOD__</B ></TT ></TD ><TD >
Der Name einer Klassenmethode. Steht seit PHP 5.0.0 zur Verfⁿgung. </TD ></TR ></TBODY ></TABLE ></DIV > </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.expressions" >Kapitel 13. Ausdrⁿcke</A ></H1 ><P >
Ausdrⁿcke (Expressions) sind die wichtigsten Bausteine von PHP. In PHP ist fast alles, was geschrieben wird, ein Ausdruck. Die einfachste, aber auch zutreffenste Definition fⁿr einen Ausdruck ist "alles, was einen Wert hat".</P ><P >
Die grundlegendste Formen von Ausdrⁿcken sind Konstanten und Variablen. Wenn man "$a = 5" schreibt, weist man $a den Ausdruck ┤5┤ zu. ┤5┤ hat offensichtlich den Wert 5. Anders ausgedrⁿckt: ┤5┤ ist ein Ausdruck mit dem Wert 5 (in diesem Fall ist ┤5┤ eine Integer-Konstante).</P ><P >
Nach dieser Zuweisung wⁿrde man erwarten, dass der Wert von $a nun ebenfalls 5 ist, wenn man also $b = $a schreibt, sollte dasselbe dabei herauskommen, als hΣtte man $b = 5 geschrieben. Anders ausgedrⁿckt: $a wΣre ebenfalls ein Ausdruck mit dem Wert 5. Wenn alles richtig funktioniert, wird genau das passieren.</P ><P >
Etwas kompliziertere Beispiele fⁿr Ausdrⁿcke sind Funktionen: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3746" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >function foo () { return 5; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></P ><P >
Angenommen, Sie sind mit dem Konzept von Funktionen vertraut (wenn Sie es nicht sind, lesen Sie das Kapitel ⁿber Funktionen), dann wⁿrden Sie annehmen, dass die Eingabe von <VAR CLASS="literal" >$c = foo()</VAR > grundsΣtzlich daselbe bedeutet, als wⁿrde man schreiben <VAR CLASS="literal" >$c = 5</VAR >,und genau das trifft zu. Funktionen sind Ausdrⁿcke mit dem Wert ihres Rⁿckgabewertes. Da foo() den Wert 5 zurⁿckgibt, ist der Wert des Ausdruckes ┤foo()┤ 5. Normalerweise geben Funktionen nicht einfach einen statischen Wert zurⁿck, sondern berechnen irgendetwas.</P ><P >
Natⁿrlich mⁿssen Werte in PHP keine Integer-Zahlen sein, und oft sind sie es auch nicht. PHP unterstⁿtzt drei skalare Datentypen: integer values (Integer-Zahlen),floating point values (Flie▀kommazahlen) und string values (Zeichenketten). (Skalare sind Datentypen, die man nicht in kleinere Stⁿcke ┤brechen┤ kann, im Gegensatz zu Arrays). PHP unsterstⁿtzt auch zwei zusammengesetzte (nicht-skalare) Datentypen: Arrays und Objekte. Jeder dieser Datentypen kann Variablen zugewiesen und von Funktionen zurⁿckgegeben werden.</P ><P >
Bis hierhin sollten Benutzer von PHP/FI 2 keine VerΣnderung bemerkt haben. PHP fasst den Begriff ┤Ausdruck┤ aber noch viel weiter, wie es auch andere Programmiersprachen tun. PHP ist in dem Sinne eine ausdrucksoriente Sprache, dass fast alles ein Ausdruck ist. Zurⁿck zu dem Beispiel, mit dem wir uns schon beschΣftigt haben: ┤$a = 5┤. Es ist einfach zu erkennen, dass hier zwei Werte enthalten sind: Der Wert der Integer-Konstanten ┤5┤ und der Wert von $a, der auf 5 geΣndert wird. In Wirklichkeit ist aber noch ein zusΣtzlicher Wert enthalten, nΣmlich der Wert der Zuweisung selbst. Die Zuweisung selbst enthΣlt den zugewiesenen Wert, in diesem Fall 5. In der Praxis bedeutet dies, dass ┤$a = 5┤, egal was es tut, immer einen Ausdruck mit dem Wert 5 darstellt. Folglich ist ┤$b = ($a = 5)┤ gleichbedeutend mit ┤$a = 5; $b = 5;┤ (ein Semikolon markiert das Ende einer Anweisung). Da Wertzuweisungen von rechts nach links geparst werden, kann man auch ┤$b = $a = 5┤ schreiben. </P ><P >
Ein anderes gutes Beispiel fⁿr die Ausdrucksorientierung von PHP sind PrΣ- und Post-Inkrement sowie die entsprechenden Dekremente. Benutzer von PHP/FI 2 und vielen anderen Sprachen sind vermutlich mit den Notationen ┤variable++┤ und ┤variable--┤ vertraut. Dies sind Inkrement- und Dekrement-Operatoren. IN PHP/FI 2 hat das Statement ┤$a++┤ keinen Wert (es ist kein Ausdruck) und daher kann man es nicht als Wert zuweisen oder in irgendeiner Weise benutzen. PHP erweitert die Eigenschaften von Dekrement und Inkrement, indem es die beiden ebenfalls zu Ausdrⁿcken macht. In PHP gibt es, wie in C, zwei Arten von Inkrementen - PrΣ-Inkrement und Post-Inkrement. GrundsΣtzlich erh÷hen sowohl PrΣ- als auch Post-Inkrement den Wert der Variable, und der Effekt auf die Variable ist derselbe. Der Unterschied ist der Wert des Inkrement-Ausdruckes. Das PrΣ-Inkrement, das ┤++$variable┤ geschrieben wird, enthΣlt als Wert den Wert der erh÷hten Variabe (PHP erh÷ht den Wert der Variable, bevor es ihren Wert ausliest, daher der Name ┤PR─-Inkrement┤). Das Post-Inkrement, das ┤$variable++┤ geschrieben wird, enthΣlt dagegen den ursprⁿnglichen Wert der Variablen vor der Erh÷hung (PHP erh÷ht den Wert der Variablen, nachdem es ihren Wert ausgelesen hat, daher der Name ┤POST-Inkrement┤).</P ><P >
Ein sehr gebrΣuchlicher Typ von Ausdrⁿcken sind Vergleichsausdrⁿcke. Diese Ausdrⁿcke geben entweder 0 (=FALSCH) oder 1 (=WAHR) zurⁿck. PHP unterstⁿtzt > (gr÷▀er), >= (gr÷▀er oder gleich), == (gleich), != (ungleich), < (kleiner), und <= (kleiner oder gleich). Diese Ausdrⁿcke werden meistens in bedingten Anweisungen, wie z. B. in <VAR CLASS="literal" >if</VAR >-Anweisungen, verwendet.</P ><P >
Im letzten Beispiel fⁿr Ausdrⁿcke befassen wir uns mit kombinierten Zuweisungs- und Operator-Ausdrⁿcken. Wir haben schon gezeigt, wie man den Wert von $a um 1 erh÷ht, nΣmlich indem man einfach ┤$a++┤ oder ┤++$a┤ schreibt. Aber was tut man, wenn man den Wert um mehr als eins erh÷hen will, z. B. um 3? Man k÷nnte mehrer Male ┤$a++┤ schreiben, aber das ist offensichtlich weder effizient noch sehr komfortabel. Viel ⁿblicher ist es, einfach ┤$a = $a + 3┤ zu schreiben. ┤$a + 3┤ gibt den Wert von $a plus 3 zurⁿck, dieser wird wieder $a zugewiesen, was dazu fⁿhrt, dass $a nun um 3 erh÷ht wurde. In PHP - wie in einigen anderen Programmiersprachen, z. B. in C - kann man dies aber noch kⁿrzer schreiben, was mit der Zeit klarer wird und auch einfacher zu verstehen ist. Um 3 zu dem aktuellen Wert hinzuzufⁿgen, schreibt man ┤$a += 3┤. Das bedeutet exakt: "Nimm┤ den Wert von $a, addiere 3 hinzu und weise $a den entstandenen Wert zu". ZusΣtzlich dazu, dass diese Schreibweise kⁿrzer und klarer ist, resultiert sie auch in einer schnelleren Ausfⁿhrung. Der Wert von ┤$a += 3┤ ist, wie der Wert einer regulΣren Zuweisung, der zugewiesene Wert. Es ist zu beachten, dass dieser Wert NICHT 3, sondern dem kombinierten Wert von $a plus 3 entspricht (Das ist der Wert, der $a zugewiesen wird). Jeder Operator, der zwei Elemente verbindet, kann in dieser Schreibweise verwendet werden, z. B. ┤$a -= 5┤ (vermindert den Wert von $a um 5) oder ┤$a *= 7┤ (multipliziert den Wert von $a mit 7 und weist das Ergebnis $a zu), usw.</P ><P >
Es gibt einen weiteren Ausdruck, der Ihnen vielleicht seltsam vorkommt, wenn Sie ihn bisher noch in keiner Programmiersprache kennengelernt haben, den dreifach konditionalen Operator: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3758" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >$eins ? $zwei : $drei</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Wenn der Wert des ersten Sub-Ausdruckes (hier: $eins) wahr ist (d. h. nicht <TT CLASS="constant" ><B >NULL</B ></TT >), dann wird der Wert des zweiten Subausdrucks (hier: $zwei) zurⁿckgeben und ist das Ergebnis des konditionalen Ausdrucks. Andernfalls (d. h. wenn der erste Ausdruck falsch ist), wird der Wert des dritten Subausdruckes (hier: $drei) zurⁿckgeben. </P ><P >
Das folgende Beispiel sollte das VerstΣndnis von PrΣ- und Post-Inkrement und von Ausdrⁿcken im allgemeinen erleichtern: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3762" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >function verdoppeln($i) { return $i*2; } $b = $a = 5; /* Weise den Variablen $a und $b beiden den Wert 5 zu */ $c = $a++; /* Post-Inkrement, der urspruengliche Wert von $a (also 5) wird $c zugewiesen. */ $e = $d = ++$b; /* Prae-Inkrement, der erh÷hte Wert von $b (= 6) wird $d und $e zugewiesen. */ /* An diesem Punkt sind $d und $e beide gleich 6 */ $f = verdoppeln($d++); /* Weise $f den doppelten Wert von $d <SPAN CLASS="emphasis" ><I CLASS="emphasis" >vor</I ></SPAN > der Erh÷hung um eins zu, 2*6 = 12 */ $g = double(++$e); /* Weise $g den doppelten Wert von $e <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nach</I ></SPAN > der Erhoehung zu, 2*7 = 14 to $g */ $h = $g += 10; /* Zuerst wie $g um 10 erh÷ht und hat schliesslich den Wert 24. Der Wert dieser Zuweisung (24) wird dann $h zugewiesen, womit $h ebenfalls den Wert von 24 hat. */</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></P ><P >
Am Anfang dieses Kapitels hatten wir gesagt, wir wⁿrden die verschiedenen Arten von Anweisungen beschreiben und, wie versprochen, Ausdrⁿcke k÷nnen Anweisungen sein. Trotzdem ist nicht jeder Ausdruck eine Anweisung. In diesem Fall hat eine Anweisung die Form ┤Ausdr┤ ┤;┤, d. h. ein Ausdruck gefolgt von einem Semikolon. In ┤$b=$a=5;┤ ist $a=5 ein gⁿltiger Ausdruck, aber fⁿr sich allein keine Anweisung. ┤$b=$a=5;┤ ist jedoch eine gⁿltige Anweisung.</P ><P >
Ein letzter Punkt, der noch zu erwΣhnen ist, ist der ┤wahr┤-Wert von Ausdrⁿcken. In vielen FΣllen, hauptsΣchlich in bedingten Anweisungen und Schleifen, ist man nicht am spezifischen Wert eines Ausdrucks interessiert, sondern kⁿmmert sich nur darum, ob er WAHR oder FALSCH bedeutet (PHP hat keinen speziellen boolean-Datentyp). Der Wahrheitswert eines Ausdrucks in in PHP wird Σhnlich bestimmt wie in Perl. Jeder numerische Wert, der nicht <TT CLASS="constant" ><B >NULL</B ></TT > ist, bedeutet WAHR, <TT CLASS="constant" ><B >NULL</B ></TT > bedeutet FALSCH. Es ist zu beachten, dass negative Werte nicht <TT CLASS="constant" ><B >NULL</B ></TT > sind und deshalb als WAHR aufgefasst werden! Eine leere Zeichenkette und die Zeichenkette "0" sind FALSCH; alle anderen Zeichenketten sind WAHR. Nicht-skalare Datentypen (Arrays und Objekte) werden als FALSCH betrachtet, wenn sie keine Elemente enthalten, anderfalls geben sie WAHR zurⁿck.</P ><P >
PHP stellt eine vollstΣndige und mΣchtige Implementat von Ausdrⁿcken bereit und, deren vollstΣndige Dokumentation den Rahmen dieses Manuals sprengen wⁿrde. Die obigen Beispiele sollten Ihnen einen guten Eindruck darⁿber verschaffen, was Ausdrⁿcke sind und wie man nⁿtzliche Ausdrⁿcke konstruieren kann. Im Rest dieses Manuals werden wir <VAR CLASS="replaceable" >ausdr</VAR > schreiben, um ausdrⁿcken, dass an dieser Stelle jeder gⁿltige PHP-Ausdruck stehen kann.</P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.operators" >Kapitel 14. Operatoren</A ></H1 ><P >
Ein Operator ist etwas das Sie mit einem oder mehreren Werten fⁿttern (oder Ausdrⁿcken, um im Programmierjargon zu sprechen) und Sie erhalten als Ergebnis einen anderen Wert (damit wird diese Konstruktion selbst zu einem Ausdruck). Als Eselsbrⁿcke k÷nnen Sie sich Operatoren als Funktionen oder Konstrukte vorstellen, die Ihnen einen Wert zurⁿck liefern (Σhnlich print) und alles, was Ihnen keinen Wert zurⁿck liefert (Σhnlich echo) als irgend etwas Anderes. </P ><P >
Es gibt drei Arten von Opratoren. Als erstes gibt es den unΣren Operator, der nur mit einem Wert umgehen kann, zum Beispiel ! (der Verneinungsoperator) oder ++ (der Inkrementoperator). Die zweite Gruppe sind die sogenannten binΣren Operatoren; diese Gruppe enthΣlt die meisten Operatoren, die PHP unterstⁿtzt. Eine Liste dieser Operatoren finden Sie weiter unten im Abschnitt <A HREF="#language.operators.precedence" >Operator-Rangfolge </A >. </P ><P >
Die dritte Gruppe bildet der ternΣre Operator : ?:. Dieser sollte eher benutzt werden um abhΣngig von einem dritten Ausdruck eine Auswahl zwischen zwei Ausdrⁿcken zu treffen, als zwischen zwei SΣtzen oder Pfaden der Programmausfⁿhrung zu wΣhlen. ▄brigens ist es eine sehr gute Idee ternΣre Ausdrⁿcke in Klammern zu setzen. </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.precedence" >Operator-Rangfolge</A ></H2 ><P >
Die Operator-Rangfolge legt fest, wie "eng" ein Operator zwei Ausdrⁿcke miteinander verbindet. Zum Beispiel ist das Ergebnis des Ausdruckes <VAR CLASS="literal" >1 + 5 * 3</VAR > <VAR CLASS="literal" >16</VAR > und nicht <VAR CLASS="literal" >18</VAR >, da der Multiplikations-Operator ("*") in der Rangfolge h÷her steht als der Additions-Operator ("+"). Wenn n÷tig, k÷nnen Sie Klammern setzen, um die Rangfolge der Operatoren zu beeinflussen. Zum Beispiel ergibt: <VAR CLASS="literal" >(1 + 5) * 3</VAR > <VAR CLASS="literal" >18</VAR >. Ist die Rangfolge der Operatoren gleich, wird links nach rechts AssoziativitΣt benutzt. </P ><P >
Die folgende Tabelle zeigt die Rangfolge der Operatoren, oben steht der Operator mit dem h÷chsten Rang. <DIV CLASS="table" ><A NAME="AEN3788" ></A ><P ><B >Tabelle 14-1. Operator-Rangfolge</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >AssoziativitΣt</TH ><TH >Operator</TH ></TR ></THEAD ><TBODY ><TR ><TD >keine Richtung</TD ><TD >new</TD ></TR ><TR ><TD >rechts</TD ><TD >[</TD ></TR ><TR ><TD >rechts</TD ><TD >
! ~ ++ -- (int) (float) (string) (array) (object) @ </TD ></TR ><TR ><TD >links</TD ><TD >* / %</TD ></TR ><TR ><TD >links</TD ><TD >+ - .</TD ></TR ><TR ><TD >links</TD ><TD ><< >></TD ></TR ><TR ><TD >keine Richtung</TD ><TD >< <= > >=</TD ></TR ><TR ><TD >keine Richtung</TD ><TD >== != === !==</TD ></TR ><TR ><TD >links</TD ><TD >&</TD ></TR ><TR ><TD >links</TD ><TD >^</TD ></TR ><TR ><TD >links</TD ><TD >|</TD ></TR ><TR ><TD >links</TD ><TD >&&</TD ></TR ><TR ><TD >links</TD ><TD >||</TD ></TR ><TR ><TD >links</TD ><TD >? :</TD ></TR ><TR ><TD >rechts</TD ><TD >
= += -= *= /= .= %= &= |= ^= <<= >>= </TD ></TR ><TR ><TD >rechts</TD ><TD >print</TD ></TR ><TR ><TD >links</TD ><TD >and</TD ></TR ><TR ><TD >links</TD ><TD >xor</TD ></TR ><TR ><TD >links</TD ><TD >or</TD ></TR ><TR ><TD >links</TD ><TD >,</TD ></TR ></TBODY ></TABLE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Obwohl <VAR CLASS="literal" >!</VAR > einen h÷heren Rang gegenⁿber <VAR CLASS="literal" >=</VAR > hat, erlaubt es Ihnen PHP immer noch Σhnliche Ausdrⁿcke wie den folgenden zu schreiben: <VAR CLASS="literal" >if (!$a =foo())</VAR >.In diesem Ausdruck wird die Ausgabe von <VAR CLASS="literal" >foo()</VAR > der Variablen <VAR CLASS="varname" >$a</VAR > zugewiesen. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.arithmetic" >Arithmetische Operatoren</A ></H2 ><P >
Erinnern Sie sich noch an die Grundrechenarten aus der Schule? Die arithmetischen Operatoren funktionieren genauso: </P ><DIV CLASS="table" ><A NAME="AEN3866" ></A ><P ><B >Tabelle 14-2. Arithmetische Operatoren</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Beispiel</TH ><TH >Name</TH ><TH >Ergebnis</TH ></TR ></THEAD ><TBODY ><TR ><TD >$a + $b</TD ><TD >Addition</TD ><TD >Summe von $a und $b.</TD ></TR ><TR ><TD >$a - $b</TD ><TD >Subtraktion</TD ><TD >Differenz von $a und $b.</TD ></TR ><TR ><TD >$a * $b</TD ><TD >Multiplikation</TD ><TD >Produkt von $a und $b.</TD ></TR ><TR ><TD >$a / $b</TD ><TD >Division</TD ><TD >Quotient von $a und $b.</TD ></TR ><TR ><TD >$a % $b</TD ><TD >Modulus</TD ><TD >Rest von $a geteilt durch $b.</TD ></TR ></TBODY ></TABLE ></DIV ><P >
Der Divisions-Operator ("/") gibt immer eine Flie▀kommazahl zurⁿck, sogar wenn die zwei Operanden Ganzzahlen sind (oder Zeichenketten, die nach Ganzzahlen umgewandelt wurden). </P ><P >
Siehe auch im Handbuch das Kapitel ⁿber <A HREF="#ref.math" >Mathematische Funktionen</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.assignment" >Zuweisungsoperatoren</A ></H2 ><P >
Der einfachste Zuweisungsoperator ist "=". Wahrscheinlich kommt man als erstes auf die Idee, ihn mit "ist gleich" zu bezeichnen. Das ist falsch. In Wirklichkeit bedeutet er, dass dem linken Operanden der Wert des Ausdrucks auf der rechten Seite zugewiesen wird (man mⁿsste ihn also mit "wird gesetzt auf den Wert von" ⁿbersetzen). </P ><P >
Der Wert eines Zuweisungs-Ausdruckes ist der zugewiesene Wert. D.h. der Wert des Ausdruckes "$a = 3" ist 3. Das erlaubt es, einige raffinierte Dinge anzustellen: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3902" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = ($b = 4) + 5; // $a ist nun gleich 9 und $b wurde auf den Wert 4 gesetzt. ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
ZusΣtzlich zu dem oben vorgestellten Zuweisungsoperator "=" gibt es "kombinierte Operatoren" fⁿr alle <A HREF="#language.operators" >binΣren, arithmetischen</A > und String-Operatoren, die es erlauben, den Wert einer Variablen in einem Ausdruck zu benutzen, und dieser anschlie▀end das Ergebnis des Ausdrucks als neuen Wert zuzuweisen. Zum Beispiel: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3906" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 3; $a += 5; // setzt $a auf den Wert 8, als ob wir geschrieben haetten: $a = $a + 5; $b = "Hallo "; $b .= "Du!"; // setzt $b auf den Wert "Hallo Du!", aequivalent zu // $b = $b . "Du!"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Man beachte, dass die Zuweisung nur den Wert der Ursprungsvarialbe der neuen Variable zuweist (Zuweisung als Wert, sie "kopiert"), weshalb sich ─nderungen an der einen Variablen nicht auf die andere auswirken werden. Das kann wichtig sein, wenn man ein gro▀es Array o. Σ. in einer Schleife kopieren muss. PHP 4 unterstⁿtzt 'assignement by reference' (Zuweisung als Verweis), mit Hilfe der Schreibweise <SAMP CLASS="computeroutput" >$var = &$othervar;</SAMP >, das funktioniert jedoch nicht in PHP 3. 'Assignement by reference' bedeutet, dass beide Variablen nach der Zuweisung die selben Daten reprΣsentieren und nichts kopiert wird. Um mehr ⁿber Referenzen zu lernen, lesen Sie bitte den Abschnitt <A HREF="#language.references" >Referenzen erklΣrt</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.bitwise" >Bit-Operatoren</A ></H2 ><P >
Bit-Operatoren erlauben es, in einem Integer bestimmte Bits "ein- oder auszuschalten" (auf 0 oder 1 zu setzen). Wenn beide, der links- und rechtsseitige Parameter, Zeichenketten sind, arbeiten die Bit-Operatoren mit ASCII-Werten der einzelnen Zeichen. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN3915" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo 12 ^ 9; // Ausgabe '5' echo "12" ^ "9"; // Ausgabe: das Backspace-Zeichen (ascii 8) // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8 echo "hallo" ^ "hello"; // Gibt die ASCII-Werte #0 #4 #0 #0 #0 // 'a' ^ 'e' = #4 aus ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="table" ><A NAME="AEN3917" ></A ><P ><B >Tabelle 14-3. Bit-Operatoren</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Beispiel</TH ><TH >Name</TH ><TH >Ergebnis</TH ></TR ></THEAD ><TBODY ><TR ><TD >$a & $b</TD ><TD >Und</TD ><TD >
Bits, die in $a und $b gesetzt sind werden gesetzt. </TD ></TR ><TR ><TD >$a | $b</TD ><TD >Oder</TD ><TD >
Bits, die in $a oder $b gesetzt sind werden gesetzt. </TD ></TR ><TR ><TD >$a ^ $b</TD ><TD >Entweder oder (Xor)</TD ><TD >
Bits, die entweder in $a oder $b gesetzt sind, werden gesetzt aber nicht in beiden. </TD ></TR ><TR ><TD >~ $a</TD ><TD >Nicht</TD ><TD >
Die Bits, die in $a nicht gesetzt sind, werden gesetzt und umgekehrt. </TD ></TR ><TR ><TD >$a << $b</TD ><TD >Nach links verschieben</TD ><TD >
Verschiebung der Bits von $a um $b Stellen nach links (jede Stelle entspricht einer Mulitplikation mit zwei). </TD ></TR ><TR ><TD >$a >> $b</TD ><TD >Nach rechts verschieben</TD ><TD >
Verschiebt die Bits von $a um $b Stellen nach rechts (jede Stelle entspricht einer Division durch zwei). </TD ></TR ></TBODY ></TABLE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.comparison" >Vergleichs-Operatoren</A ></H2 ><P >
Vergleichs-Operatoren erlauben es - wie der Name schon sagt - zwei Werte zu vergleichen. Wenn Sie an Beispielen verschiedener auf Typen bezogener Vergleiche interessiert sind, k÷nnen Sie sich <A HREF="#types.comparisons" >PHP type comparison tables</A > anschauen. </P ><DIV CLASS="table" ><A NAME="AEN3954" ></A ><P ><B >Tabelle 14-4. Vergleichsoperatoren</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Beispiel</TH ><TH >Name</TH ><TH >Ergebnis</TH ></TR ></THEAD ><TBODY ><TR ><TD >$a == $b</TD ><TD >Gleich</TD ><TD >Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn $a gleich $b ist.</TD ></TR ><TR ><TD >$a === $b</TD ><TD >Identisch</TD ><TD >
Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck wenn $a gleich $b ist und beide vom gleichen Typ sind(nur PHP 4). </TD ></TR ><TR ><TD >$a != $b</TD ><TD >Ungleich</TD ><TD >
Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn $a nicht gleich $b ist. </TD ></TR ><TR ><TD >$a <> $b</TD ><TD >Ungleich</TD ><TD >
Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn $a nicht gleich $b ist. </TD ></TR ><TR ><TD >$a !== $b</TD ><TD >Nicht identisch</TD ><TD >
Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn $a nicht gleich $b ist, oder wenn beide nicht vom gleichen Typ sind (nur PHP 4). </TD ></TR ><TR ><TD >$a < $b</TD ><TD >Kleiner Als</TD ><TD >Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn $a kleiner als $b ist.</TD ></TR ><TR ><TD >$a > $b</TD ><TD >Gr÷▀er Als</TD ><TD >Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn $a gr÷▀er als $b ist.</TD ></TR ><TR ><TD >$a <= $b</TD ><TD >Kleiner Gleich </TD ><TD >
Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn $a kleiner oder gleich $b ist. </TD ></TR ><TR ><TD >$a >= $b</TD ><TD >Gr÷▀er Gleich</TD ><TD >
Gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn $a gr÷▀er oder gleich $b ist. </TD ></TR ></TBODY ></TABLE ></DIV ><P >
Ein weiter Vergleichs-Operator ist der "?:"- oder TrinitΣts-Operator. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4009" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Beispielanwendung fⁿr den TrinitΣts-Operator $action = (empty($_POST['action'])) ? 'standard' : $_POST['action']; // Obiges ist mit dieser if/else-Anweisung identisch if (empty($_POST['action'])) { $action = 'standard'; } else { $action = $_POST['action']; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Der Ausdruck <VAR CLASS="literal" >(ausdr1) ? (ausdr2) : (ausdr3)</VAR > gibt <VAR CLASS="replaceable" >ausdr2</VAR > zurⁿck, wenn <VAR CLASS="replaceable" >ausdr1</VAR > <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿckgibt und <VAR CLASS="replaceable" >ausdr3</VAR >, wenn <VAR CLASS="replaceable" >ausdr1</VAR > <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgibt. </P ><P >
Siehe auch <A HREF="#function.strcasecmp" ><B CLASS="function" >strcasecmp()</B ></A >, <A HREF="#function.strcmp" ><B CLASS="function" >strcmp()</B ></A >, <A HREF="#language.operators.array" >Array Operatoren</A > und den Abschnitt ⁿber <A HREF="#language.types" >Typen</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.errorcontrol" >Fehler-Kontroll-Operatoren</A ></H2 ><P >
PHP unterstⁿtzt einen Operator zur Fehlerkontrolle: Das @-Symbol. Stellt man das @ in PHP vor einen Ausdruck werden alle Fehlermeldungen, die von diesem Ausdruck erzeugt werden k÷nnten, ignoriert. </P ><P >
Ist das <A HREF="#ini.track-errors" ><VAR CLASS="option" >track_errors</VAR ></A >-Feature aktiviert, werden alle Fehlermeldungen, die von diesem Ausdruck erzeugt werden, in der Variablen <A HREF="#reserved.variables.phperrormsg" >$php_errormsg</A > gespeichert. Da diese Variable mit jedem neuen Auftreten eines Fehlers ⁿberschrieben wird, sollte man sie m÷glichst bald nach Verwendung des Ausdrucks ⁿberprⁿfen, wenn man mit ihr arbeiten will. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4031" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* Beabsichtigter Dateifehler */ $my_file = @file ('nicht_vorhandene_Datei') or die ("Datei konnte nicht ge÷ffnetwerden: Fehler war:'$php_errormsg'"); // Das funktioniert bei jedem Ausdruck, nicht nur bei Funktionen: $value = @$cache[$key]; // erzeugt keine Notice, falls der Index $key nicht vorhanden ist. ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der @-Operator funktioniert nur bei <A HREF="#language.expressions" >Ausdrⁿcken</A >. Eine einfache Daumenregel: wenn Sie den Wert von etwas bestimmen k÷nnen, dann k÷nnen Sie den @-Operator davor schreiben. Zum Beispiel k÷nnen Sie ihn vor Variablen, Funktionsaufrufe und vor <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > setzen, vor Konstanten und so weiter. Nicht verwenden k÷nnen Sie diesen Operator vor Funktions- oder Klassendefinitionen oder vor Kontrollstrukturen wie zum Beispiel <VAR CLASS="literal" >if</VAR > und <VAR CLASS="literal" >foreach</VAR > und so weiter. </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.error-reporting" ><B CLASS="function" >error_reporting()</B ></A > und den Abschnitt ⁿber <A HREF="#ref.errorfunc" >Error Handling and Logging Functions</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der @ Fehler-Kontroll-Operator verhindert jedoch keine Meldungen, welche aus Fehlern beim Parsen resultieren. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Zum gegenwΣrtigen Zeitpunkt deaktiviert der "@" Fehler-Kontrolloperator die Fehlermeldungen selbst bei kritischen Fehlern, die die Ausfⁿhrung eines Skripts beenden. Unter anderem bedeutet das, wenn Sie "@" einer bestimmten Funktion voranstellen, diese aber nicht zur Verfⁿgung steht oder falsch geschrieben wurde, Ihr PHP-Skript einfach beendet wird, ohne Hinweis auf die Ursache. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.execution" >Operatoren zur Programmausfⁿhrung</A ></H2 ><P >
PHP unterstⁿtzt einen Operator zur Ausfⁿhrung externer Programme: Die sog. Backticks (``). Achtung: Die Backticks sind keine einfachen Anfⁿhrungszeichen! PHP versucht, den Text zwischen den Backticks als Kommandozeilen-Befehl auszufⁿhren. Die Ausgabe des aufgerufenen Programms wird zurⁿckgegeben (d.h. wird nicht einfach ausgegeben, sondern kann einer Variablen zugewiesen werden). Die Verwendung des Backtick-Operators ist mit <A HREF="#function.shell-exec" ><B CLASS="function" >shell_exec()</B ></A > identisch. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4050" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $output = `ls -al`; echo "<pre>$output</pre>"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der Backtick-Operator steht nicht zur Verfⁿgung, wenn <A HREF="#ini.safe-mode" >Safe Mode</A > aktiviert ist oder die Funktion <A HREF="#function.shell-exec" ><B CLASS="function" >shell_exec()</B ></A > deaktiviert wurde. </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch den Abschnitt ⁿber <A HREF="#ref.exec" >Funktionen zur Programmausfⁿhrung</A >, <A HREF="#function.popen" ><B CLASS="function" >popen()</B ></A >, <A HREF="#function.proc-open" ><B CLASS="function" >proc_open()</B ></A > und <A HREF="#features.commandline" >Using PHP from the commandline</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.increment" >Inkrement- bzw. Dekrementoperatoren</A ></H2 ><P >
PHP unterstⁿtzt PrΣ- und Post-Inkrement- und Dekrementoperatoren im Stil der Programmiersprache C. </P ><DIV CLASS="table" ><A NAME="AEN4064" ></A ><P ><B >Tabelle 14-5. Inkrement- und Dekrementoperatoren</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Beispiel</TH ><TH >Name</TH ><TH >Auswirkung</TH ></TR ></THEAD ><TBODY ><TR ><TD >++$a</TD ><TD >PrΣ-Inkrement</TD ><TD >
Erh÷ht den Wert von $a um eins (inkrementiert $a) und gibt anschlie▀end den neuen Wert von $a zurⁿck. </TD ></TR ><TR ><TD >$a++</TD ><TD >Post-Inkrement</TD ><TD >
Gibt zuerst den aktuellen Wert von $a zurⁿck und erh÷ht dann den Wert von $a um eins. </TD ></TR ><TR ><TD >--$a</TD ><TD >PrΣ-Dekrement</TD ><TD >
Vermindert den Wert von $a um eins (dekrementiert $a) und gibt anschlie▀end den neuen Wert von $a zurⁿck. </TD ></TR ><TR ><TD >$a--</TD ><TD >Post-Dekrement</TD ><TD >
Gibt zuerst den aktuellen Wert von $a zurⁿck und erniedrigt dann den Wert von $a um eins. </TD ></TR ></TBODY ></TABLE ></DIV ><P >
Ein einfaches Beispiel-Skript: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4090" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo "<h3>Post-Inkrement</h3>"; $a = 5; echo "Sollte 5 sein: " . $a++ . "<br />\n"; echo "Sollte 6 sein: " . $a . "<br />\n"; echo "<h3>Pre-Inkrement</h3>"; $a = 5; echo "Sollte 6 sein: " . ++$a . "<br />\n"; echo "Sollte 6 sein: " . $a . "<br />\n"; echo "<h3>Post-Dekrement</h3>"; $a = 5; echo "Sollte 5 sein: " . $a-- . "<br />\n"; echo "Sollte 4 sein: " . $a . "<br />\n"; echo "<h3>Pre-Dekrement</h3>"; $a = 5; echo "Sollte 4 sein: " . --$a . "<br />\n"; echo "Sollte 4 sein: " . $a . "<br />\n"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
PHP folgt bei der Behandlung arithmetischer Operationen an Zeichenvariablen der Perl-Konvention und nicht der von C. Zum Beispiel wird in Perl aus 'Z'+1 'AA', wΣhrend aus 'Z'+1 in C '[' wird ( ord('Z') == 90, ord('[') == 91 ). Beachten Sie, dass Zeichenvariablen zwar inkrementiert aber nicht dekrementiert werden k÷nnen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4093" ></A ><P ><B >Beispiel 14-1. Arithmetrische Operationen an Zeichenvariablen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $i = 'W'; for($n=0; $n<6; $n++) echo ++$i . "\n"; /* Erzeugt in etwa folgende Ausgabe: X Y Z AA AB AC */ ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.logical" >Logische Operatoren</A ></H2 ><DIV CLASS="table" ><A NAME="AEN4098" ></A ><P ><B >Tabelle 14-6. Logische Operatoren</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Beispiel</TH ><TH >Name</TH ><TH >Ergebnis</TH ></TR ></THEAD ><TBODY ><TR ><TD >$a and $b</TD ><TD >Und</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn sowohl $a als auch $b <TT CLASS="constant" ><B >TRUE</B ></TT > ist.</TD ></TR ><TR ><TD >$a or $b</TD ><TD >Oder</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn $a oder $b <TT CLASS="constant" ><B >TRUE</B ></TT > ist.</TD ></TR ><TR ><TD >$a xor $b</TD ><TD >Entweder Oder</TD ><TD >
<TT CLASS="constant" ><B >TRUE</B ></TT > wenn entweder $a oder $b <TT CLASS="constant" ><B >TRUE</B ></TT > ist, aber nicht beide. </TD ></TR ><TR ><TD >! $a</TD ><TD >Nicht</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn $a nicht <TT CLASS="constant" ><B >TRUE</B ></TT > ist.</TD ></TR ><TR ><TD >$a && $b</TD ><TD >Und</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn sowohl $a als auch $b <TT CLASS="constant" ><B >TRUE</B ></TT > ist.</TD ></TR ><TR ><TD >$a || $b</TD ><TD >Oder</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn $a oder $b <TT CLASS="constant" ><B >TRUE</B ></TT > ist.</TD ></TR ></TBODY ></TABLE ></DIV ><P >
Der Grund dafⁿr, dass es je zwei unterschiedliche Operatoren fⁿr die "Und"- und die "Oder"-Verknⁿpfung gibt ist der, dass die beiden Operatoren jeweils Rangfolgen haben. (siehe auch <A HREF="#language.operators.precedence" >Operator-Rangfolge</A >.) </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.string" >Zeichenketten-Operatoren</A ></H2 ><P >
Es gibt in PHP zwei Operatoren fⁿr <A HREF="#language.types.string" ><B CLASS="type" >string</B ></A > (Zeichenkette). Der erste ist der Vereinigungs-Operator ('.'), dessen Rⁿckgabewert eine zusammengesetzte Zeichenkette aus dem rechten und dem linken Argument ist. Der zweite ist der Vereinigungs-Zuweisungsoperator ('.='), der das Argument auf der rechten Seite an das Argument der linken Seite anhΣngt. Siehe <A HREF="#language.operators.assignment" >Zuweisungs-Operatoren</A > fⁿr weitere Informationen. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4151" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = "Hallo "; $b = $a . "Welt!"; // $b enthΣlt jetzt den Text "Hallo Welt!" $a = "Hallo "; $a .= "Welt!"; // $a enthΣlt jetzt den Text "Hallo Welt!" ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Siehe auch die Abschnitte ⁿber <A HREF="#language.types.string" >Strings / Zeichenketten</A > und <A HREF="#ref.strings" >String-Funktionen</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.array" >Array Operatoren</A ></H2 ><DIV CLASS="table" ><A NAME="AEN4158" ></A ><P ><B >Tabelle 14-7. Array Operatoren</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Beispiel</TH ><TH >Name</TH ><TH >Ergebnis</TH ></TR ></THEAD ><TBODY ><TR ><TD >$a + $b</TD ><TD >Vereinigung</TD ><TD >Verinigung von $a und $b.</TD ></TR ><TR ><TD >$a == $b</TD ><TD >Gleichwerigkeit</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn $a und $b die gleichen Elemente enthalten.</TD ></TR ><TR ><TD >$a === $b</TD ><TD >IdentitΣt</TD ><TD >
<TT CLASS="constant" ><B >TRUE</B ></TT > wenn $a und $b die gleichen Elemente in der gleichen Reihenfolge enthalten. </TD ></TR ><TR ><TD >$a != $b</TD ><TD >Ungleichheit</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn $a nicht gleich $b ist.</TD ></TR ><TR ><TD >$a <> $b</TD ><TD >Ungleichheit</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn $a nicht gleich $b ist.</TD ></TR ><TR ><TD >$a !== $b</TD ><TD >nicht identisch</TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT > wenn $a nicht identisch zu $b ist.</TD ></TR ></TBODY ></TABLE ></DIV ><P >
Der <VAR CLASS="literal" >+</VAR > Operator hΣngt das rechsstehende Array an das linksstehende Array an, wobei doppelte Schlⁿssel NICHT ⁿberschrieben werden. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4199" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array("a" => "Apfel", "b" => "Banane"); $b = array("a" =>"pear", "b" => "Erdbeere", "c" => "Kirsche"); $c = $a + $b; // Verinigung von $a mit $b; echo "Vereinigung von \$a mit \$b: \n"; var_dump($c); $c = $b + $a; // Vereinigung von $b mit $a; echo "Vereinigung von \$b mit \$a: \n"; var_dump($c); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Dieses Skript gibt folgendes aus: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Vereinigung von $a mit $b: array(3) { ["a"]=> string(5) "Apfel" ["b"]=> string(6) "Banane" ["c"]=> string(7) "Kirsche" } Vereinigung von $b mit $a: array(3) { ["a"]=> string(4) "pear" ["b"]=> string(8) "Erdbeere" ["c"]=> string(7) "Kirsche" }</PRE ></TD ></TR ></TABLE > </P ><P >
Beim Vergleich werden Arrayelemente als gleich angesehen, wenn diese dieselben Schlⁿssel und Werte beinhalten. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4204" ></A ><P ><B >Beispiel 14-2. Array Vergleiche</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array("a" => "Apfel", "b" => "Banane"); $b = array(1 => "Banane", "0" => "Apfel"); var_dump($a == $b); // bool(true) var_dump($a === $b); // bool(false) ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch die Abschnitte ⁿber <A HREF="#language.types.array" >Arrays</A > und <A HREF="#ref.array" >Array Funktionen</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.operators.type" >Typ Operatoren</A ></H2 ><P >
In PHP gibt es einen einzigen Typ Operator: <VAR CLASS="literal" >instanceof</VAR >. <VAR CLASS="literal" >instanceof</VAR > wird dazu verwendet um festzustellen, ob ein gegebenes Objekt ein Objekt ist, das zu einer bestimmten <A HREF="#language.oop" >Klasse</A > geh÷rt. </P ><P >
<VAR CLASS="literal" >instanceof</VAR > wurde in PHP 5 eingefⁿhrt. Vorher wurde <A HREF="#function.is-a" ><B CLASS="function" >is_a()</B ></A > benutzt, aber <A HREF="#function.is-a" ><B CLASS="function" >is_a()</B ></A > ist veraltet und <VAR CLASS="literal" >instanceof</VAR > sollte stattdessen benutzt werden. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4221" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><?php class A { } class B { } $ding = new A; if ($ding instanceof A) { echo 'A'; } if ($ding instanceof B) { echo 'B'; } ?></PRE ></TD ></TR ></TABLE ><P >
Da <VAR CLASS="varname" >$ding</VAR > ein <A HREF="#language.types.object" ><B CLASS="type" >object</B ></A > vom Typ A und nicht von B ist, wird nur der Programmblock ausgefⁿhrt, der abhΣngig von Typ A ist: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >A</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
See auch <A HREF="#function.get-class" ><B CLASS="function" >get_class()</B ></A > und <A HREF="#function.is-a" ><B CLASS="function" >is_a()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.control-structures" >Kapitel 15. Kontroll-Strukturen</A ></H1 ><P >
Jedes PHP-Skript besteht aus einer Reihe von Anweisungen. Eine Anweisung kann eine Zuweisung, ein Funktionsaufruf, eine Schleife, eine bedingte Anweisung oder ein Befehl sein, der nichts macht (eine leere Anweisung). Jede Anweisung endet gew÷hnlich mit einem Semikolon. Darⁿber hinaus k÷nnen Anweisungen zu einer Anweisungsgruppe zusammengefasst werden, welche durch geschweifte Klammern begrenzt wird. Eine Anweisungsgruppe selbst ist auch wieder eine Anweisung. Die unterschiedlichen Arten von Anweisungen werden in diesem Kapitel erlΣutert. </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.if" ><VAR CLASS="literal" >if</VAR ></A ></H2 ><P >
Das <VAR CLASS="literal" >if</VAR >-Konstrukt ist eine der wichtigsten M÷glichkeiten vieler Programmier-Sprachen, PHP eingeschlossen. Es erlaubt die bedingte Ausfⁿhrung von Programmteilen. PHP kennt eine <VAR CLASS="literal" >if</VAR > -Struktur, die Σhnlich wie in der Programmiersprache C implementiert ist: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4239" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><?php if (ausdr) Anweisung ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Wie im Abschnitt ⁿber <A HREF="#language.expressions" >Ausdrⁿcke</A > beschrieben, wird <VAR CLASS="replaceable" >ausdr</VAR > auf seinen boolschen Wertinhalt ausgewertet. Wenn <VAR CLASS="replaceable" >ausdr</VAR > als <TT CLASS="constant" ><B >TRUE</B ></TT > ausgewertet wird, fⁿhrt PHP die <VAR CLASS="replaceable" >Anweisung</VAR > aus. Falls die Auswertung <TT CLASS="constant" ><B >FALSE</B ></TT > ergibt, wird die <VAR CLASS="replaceable" >Anweisung</VAR > ⁿbergangen. Mehr Informationen drⁿber welche Werte als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet werden finden Sie im Abschnitt <A HREF="#language.types.boolean.casting" >'Umwandlung nach boolean'</A >. </P ><P >
Das folgende Beispiel wird <SAMP CLASS="computeroutput" >a ist gr÷▀er als b </SAMP >anzeigen, wenn <VAR CLASS="varname" >$a</VAR > gr÷▀er als <VAR CLASS="varname" >$b</VAR > ist: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4255" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($a > $b) echo "a ist gr÷▀er als b"; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Oft werden Sie die bedingte Ausfⁿhrung von mehr als einer Anweisung wollen. SelbstverstΣndlich ist es nicht erforderlich, jede Anweisung mit einer <VAR CLASS="literal" >if</VAR >-Bedingung zu versehen. Statt dessen k÷nnen Sie mehrere Anweisungen in Gruppen zusammenfassen. Der folgende Programm-Code wird zum Beispiel <SAMP CLASS="computeroutput" >a ist gr÷▀er als b</SAMP > anzeigen, wenn <VAR CLASS="varname" >$a</VAR > gr÷▀er als <VAR CLASS="varname" >$b</VAR > ist und danach wird der Wert von <VAR CLASS="varname" >$a</VAR > in <VAR CLASS="varname" >$b</VAR > gespeichert: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4264" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($a > $b) { print "a ist gr÷▀er als b"; $b = $a; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
<VAR CLASS="literal" >if</VAR >-Anweisungen k÷nnen beliebig oft innerhalb anderer <VAR CLASS="literal" >if</VAR >-Anweisungen definiert werden. Das erm÷glicht ihnen v÷llige FlexibilitΣt bei der bedingten Ausfⁿhrung verschiedenster Programmteile. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.else" ><VAR CLASS="literal" >else</VAR ></A ></H2 ><P >
HΣufig m÷chten Sie eine Anweisung auszufⁿhren, wenn eine bestimmte Bedingung erfⁿllt ist und eine andere Anweisung, falls sie nicht erfⁿllt ist. Dafⁿr gibt es <VAR CLASS="literal" >else</VAR >. <VAR CLASS="literal" >else</VAR > erweitert eine <VAR CLASS="literal" >if</VAR >-Anweisung um die Ausfⁿhrung von Anweisungen, sobald der Ausdruck der <VAR CLASS="literal" >if</VAR >-Anweisung als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet wird. Der folgende Code wird z.B. <SAMP CLASS="computeroutput" >a ist gr÷▀er als b</SAMP > ausgeben, wenn <VAR CLASS="varname" >$a</VAR > gr÷▀er als <VAR CLASS="varname" >$b</VAR > ist, anderenfalls <SAMP CLASS="computeroutput" >a ist NICHT gr÷▀er als b</SAMP >: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4282" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($a > $b) { print "a ist gr÷▀er als b"; } else { print "a ist NICHT gr÷▀er als b"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Die <VAR CLASS="literal" >else</VAR >-Anweisung wird nur ausgefⁿhrt, wenn der <VAR CLASS="literal" >if</VAR >-Ausdruck als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet wurde und wenn bei vorhandenen <VAR CLASS="literal" >elseif</VAR >-Ausdrⁿcken diese ebenfalls <TT CLASS="constant" ><B >FALSE</B ></TT > sind (siehe <A HREF="#control-structures.elseif" >elseif</A >). </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.elseif" ><VAR CLASS="literal" >elseif</VAR ></A ></H2 ><P >
Wie der Name schon sagt ist <VAR CLASS="literal" >elseif</VAR > eine Verbindung von <VAR CLASS="literal" >if</VAR > und <VAR CLASS="literal" >else</VAR >. Wie <VAR CLASS="literal" >else</VAR > erweitert sie eine <VAR CLASS="literal" >if</VAR >-Anweisung um die Ausfⁿhrung anderer Anweisungen, sobald die normale <VAR CLASS="literal" >if</VAR >-Bedingung als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet wird. Anders als bei <VAR CLASS="literal" >else</VAR > wird die Ausfⁿhrung dieser alternativen Anweisungen nur durchgefⁿhrt, wenn die bei <VAR CLASS="literal" >elseif</VAR > angegebene alternative Bedingung als <TT CLASS="constant" ><B >TRUE</B ></TT > ausgewertet wird. Der folgende Code wird z.B. <SAMP CLASS="computeroutput" >a ist gr÷▀er als b</SAMP >, <SAMP CLASS="computeroutput" >a ist gleich b</SAMP > oder <SAMP CLASS="computeroutput" >a ist kleiner als b</SAMP > ausgeben: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4307" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($a > $b) { echo "a ist gr÷▀er als b"; } elseif ($a == $b) { echo "a ist gleich b"; } else { echo "a ist kleiner als b"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Es kann mehrere <VAR CLASS="literal" >elseif</VAR >-Anweisungen innerhalb einer <VAR CLASS="literal" >if</VAR >-Anweisung geben. Die erste <VAR CLASS="literal" >elseif</VAR >-Bedingung (falls vorhanden), die <TT CLASS="constant" ><B >TRUE</B ></TT > ist, wird ausgefⁿhrt. In PHP kann man auch 'else if' schreiben (zwei W÷rter). Das Verhalten ist identisch zu 'elseif' (ein Wort). Die Bedeutung der Syntax ist leicht unterschiedlich (falls Sie mit C vertraut sind, das ist das gleiche Verhalten) aber der Grundtenor ist der, dass beide Schreibweisen, bezogen auf das Ergebnis, sich exakt gleich verhalten. </P ><P >
Die <VAR CLASS="literal" >elseif</VAR >-Anweisung wird nur ausgefⁿhrt, wenn die vorausgehende <VAR CLASS="literal" >if</VAR >-Bedingung sowie jede vorherige <VAR CLASS="literal" >elseif</VAR >-Bedingung als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet wird und die aktuelle <VAR CLASS="literal" >elseif</VAR >-Bedingung <TT CLASS="constant" ><B >TRUE</B ></TT > ist. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.alternative-syntax" >Alternative Syntax fⁿr Kontroll-Strukturen</A ></H2 ><P >
PHP bietet eine alternative Syntax fⁿr einige seiner Kontroll-Strukturen, als da sind <VAR CLASS="literal" >if</VAR >, <VAR CLASS="literal" >while</VAR >, <VAR CLASS="literal" >for</VAR >, <VAR CLASS="literal" >foreach</VAR > und <VAR CLASS="literal" >switch</VAR >. Die ÷ffnende Klammer muss immer durch einen Doppelpunkt ":" und die schlie▀ende Klammer entsprechend durch <VAR CLASS="literal" >endif;</VAR >, <VAR CLASS="literal" >endwhile;</VAR >, <VAR CLASS="literal" >endfor;</VAR >, <VAR CLASS="literal" >endforeach;</VAR > oder <VAR CLASS="literal" >endswitch;</VAR > ersetzt werden. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4334" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($a == 5): ?> A ist gleich 5 <?php endif; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Im obigen Beispiel ist der HTML-Bereich "A ist gleich 5" in eine <VAR CLASS="literal" >if</VAR >-Anweisung mit alternativer Syntax eingebettet. Der HTML-Bereich wird nur ausgegeben, wenn <VAR CLASS="varname" >$a</VAR > gleich 5 ist. </P ><P >
Die alternative Syntax kann auch auf <VAR CLASS="literal" >else</VAR > und <VAR CLASS="literal" >elseif</VAR > angewendet werden. Es folgt eine <VAR CLASS="literal" >if</VAR >-Struktur mit <VAR CLASS="literal" >elseif</VAR > und <VAR CLASS="literal" >else</VAR > im alternativen Format: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4345" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($a == 5): echo "a ist gleich 5"; echo "..."; elseif ($a == 6): echo "a ist gleich 6"; echo "!!!"; else: echo "a ist weder 5 noch 6"; endif; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Siehe auch <A HREF="#control-structures.while" >while</A >, <A HREF="#control-structures.for" >for</A > und <A HREF="#control-structures.if" >if</A > fⁿr weitere Beispiele. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.while" ><VAR CLASS="literal" >while</VAR ></A ></H2 ><P >
Die <VAR CLASS="literal" >while</VAR >-Schleifen sind die einfachste Form von Schleifen in PHP. Sie funktionieren genau wie in C. Die Grundform einer <VAR CLASS="literal" >while</VAR >-Anweisung lautet: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4357" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >while (ausdr) Anweisung</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Die Bedeutung einer <VAR CLASS="literal" >while</VAR >-Anweisung ist einfach. Sie weist PHP an, einen in ihr eingebetteten Befehl so lange zu wiederholen, wie die <VAR CLASS="literal" >while</VAR >-Bedingung als <TT CLASS="constant" ><B >TRUE</B ></TT > ausgewertet wird. Der Wert der Bedingung wird immer am Anfang der Schleife geprⁿft. Wird der Wert wΣhrend der Ausfⁿhrung der Anweisungen innerhalb der <VAR CLASS="literal" >while</VAR >-Schleife verΣndert, endet die Ausfⁿhrung dieses Anweisungsblocks erst mit dem Ende der Iteration (Jeder Schleifendurchlauf ist eine Iteration). Falls die <VAR CLASS="literal" >while</VAR >-Bedingung bereits beim ersten Mal <TT CLASS="constant" ><B >FALSE</B ></TT > ist, werden die Anweisungen der <VAR CLASS="literal" >while</VAR >-Schleife nicht ein einziges Mal durchlaufen. </P ><P >
Wie bei der <VAR CLASS="literal" >if</VAR >-Anweisung k÷nnen Sie mehrere Anweisungen innerhalb der gleichen <VAR CLASS="literal" >while</VAR >-Schleife angeben, indem Sie dieses mit geschweiften Klammern umschlie▀en oder die alternative Syntax verwenden: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4370" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >while (ausdr): Anweisung ... endwhile;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Die folgenden Beispiele sind identisch; beide geben Zahlen von 1 bis 10 aus: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4373" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >/* Beispiel 1 */ $i = 1; while ($i <= 10) { echo $i++; /* es wird erst $i ausgegeben, bevor der Wert erh÷ht wird (Post-Inkrement) */ } /* Beispiel 2 */ $i = 1; while ($i <= 10): echo $i; $i++; endwhile;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.do.while" ><VAR CLASS="literal" >do..while</VAR ></A ></H2 ><P >
<VAR CLASS="literal" >do..while</VAR >-Schleifen sind den <VAR CLASS="literal" >while</VAR > -Schleifen sehr Σhnlich, au▀er dass der Wahrheitsgehalt des Ausdrucks erst am Ende jedes Durchlaufs geprⁿft wird, statt am Anfang. Der Hauptunterschied zu gew÷hnlichen <VAR CLASS="literal" >while</VAR >-Schleifen ist der, dass die Schleife bei <VAR CLASS="literal" >do..while</VAR > in jeden Fall einmal durchlaufen wird (die Bedingung wird erst am Ende eines Durchlaufs geprⁿft).Bei <VAR CLASS="literal" >while</VAR >-Schleifen hingegen kann es durchaus passieren, dass die Schleife nie durchlaufen wird (die Bedingung wird immer am Anfang eines Durchlaufs ⁿberprⁿft. Wird diese Bedingung von Anfang an als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet endet die Ausfⁿhrung der Schleife sofort). </P ><P >
Es gibt nur eine Syntax fⁿr <VAR CLASS="literal" >do..while</VAR >-Schleifen: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4387" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $i = 0; do { echo $i; } while ($i>0); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Die obige Schleife wird genau einmal durchlaufen, da nach der ersten Wiederholung die Erfⁿllung der Bedingung geprⁿft wird. Diese Bedingung ist aber nicht erfⁿllt ($i ist nicht gr÷▀er als 0), wird als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet, und die Schleifenausfⁿhrung beendet. </P ><P >
Erfahrene C-Anwender kennen auch die M÷glichkeit, Programm-Bl÷cke mit <VAR CLASS="literal" >do..while</VAR > (0) einzuschliessen und dann die <A HREF="#control-structures.break" ><VAR CLASS="literal" >break</VAR ></A > Anweisung zu benutzen. Der folgende Programm-Ausschnitt zeigt diese M÷glichkeit: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4395" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >do { if ($i < 5) { echo "i ist nicht gro▀ genug"; break; } $i *= $factor; if ($i < $minimum_limit) { break; } echo "i ist ok"; /* mach was mit i */ } while (0);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Es ist nicht weiter tragisch, wenn Sie dieses Beispiel nicht oder nur zum Teil verstehen. Sie k÷nnen auch ohne dieses 'Feature' effektive PHP-Programme und Skripte schreiben. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.for" ><VAR CLASS="literal" >for</VAR ></A ></H2 ><P >
Die <VAR CLASS="literal" >for</VAR >-Schleifen sind die komplexesten Schleifen in PHP. Sie funktionieren wie ihr Gegenstⁿck in C. Die Syntax einer <VAR CLASS="literal" >for</VAR >-Schleife sieht wie folgt aus: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4404" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >for (ausdr1; ausdr2; ausdr3) Anweisung</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Der erste Ausdruck (<VAR CLASS="varname" >ausdr1</VAR >) wird beim Schleifenbeginn (ohne jegliche Vorbedingung) geprⁿft bzw. ausgefⁿhrt. </P ><P >
Zu Beginn jedes Durchlaufs wird nun <VAR CLASS="varname" >ausdr2</VAR > geprⁿft. Wenn dieser <TT CLASS="constant" ><B >TRUE</B ></TT > ist, fΣhrt die Schleife mit der Ausfⁿhrung der nachfolgenden Anweisung(en) fort. Ist das Ergebnis <TT CLASS="constant" ><B >FALSE</B ></TT >, wird die Schleife beendet. </P ><P >
Am Ende jedes Durchlaufs wird <VAR CLASS="varname" >ausdr3</VAR > geprⁿft (ausgefⁿhrt). </P ><P >
Jeder Ausdruck kann leer sein. Ist <VAR CLASS="varname" >ausdr2</VAR > leer, wird die Schleife endlos oft durchlaufen (PHP wertet diesen, wie in C, implizit als <TT CLASS="constant" ><B >TRUE</B ></TT >). Das ist gar nicht so sinnlos, wie Sie vielleicht zunΣchst glauben, weil man hΣufig eine Schleife erst durch eine bedingte <A HREF="#control-structures.break" ><VAR CLASS="literal" >break</VAR > </A >-Anweisung statt durch eine unwahr werdende <VAR CLASS="literal" >for</VAR >-Bedingung beenden m÷chte. </P ><P >
Beachten Sie die folgenden Beispiele. Alle geben Zahlen von 1 bis 10 aus: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4421" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* Beispiel 1 */ for ($i = 1; $i <= 10; $i++) { echo $i; } /* Beispiel 2 */ for ($i = 1; ; $i++) { if ($i > 10) { break; } echo $i; } /* Beispiel 3 */ $i = 1; for (; ; ) { if ($i > 10) { break; } echo $i; $i++; } /* Beispiel 4 */ for ($i = 1; $i <= 10; echo $i, $i++); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
SelbstverstΣndlich sieht das erste (oder vielleicht das vierte) Beispiel am besten aus, aber Sie werden noch feststellen, dass es oftmals ganz nⁿtzlich sein kann, leere Parameter in <VAR CLASS="literal" >for</VAR >-Schleifen zu verwenden. </P ><P >
PHP unterstⁿtzt auch bei <VAR CLASS="literal" >for</VAR >-Schleifen die alternative "Doppelpunkt-Syntax". <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4427" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >for (ausdr1; ausdr2; ausdr3): Anweisung; ...; endfor;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Andere Sprachen haben fⁿr das Durchlaufen eines Hashs oder Arrays eine <VAR CLASS="literal" >foreach</VAR >-Anweisung. PHP 3 hat dies nicht; im Gegensatz zu PHP 4 (vgl. <A HREF="#control-structures.foreach" >foreach</A >). In PHP 3 k÷nnen Sie fⁿr diesen Zweck <A HREF="#control-structures.while" >while</A > mit der <A HREF="#function.list" ><B CLASS="function" >list()</B ></A >- und <A HREF="#function.each" ><B CLASS="function" >each()</B ></A >-Funktion kombinieren. Beispiele finden Sie in der Dokumentation zu diesen Funktionen. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.foreach" ><VAR CLASS="literal" >foreach</VAR ></A ></H2 ><P >
PHP 4 (nicht PHP 3) enthΣlt ein <VAR CLASS="literal" >foreach</VAR > Konstrukt, genau wie Perl und einige andere Sprachen. Diese erm÷glicht es, auf einfache Weise ein Array zu durchlaufen. <VAR CLASS="literal" >foreach</VAR > funktioniert nur in Verbindung mit Arrays. Wenn Sie versuchen <VAR CLASS="literal" >foreach</VAR > mit Variablen eines anderen Datentyps oder nicht initialisierten Variablen zu benutzen, gibt PHP einen Fehler aus. Es gibt zwei Syntaxformen; die zweite ist eine unbedeutende, aber sinnvolle Erweiterung der ersten Syntax: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4442" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >foreach (array_expression as $value) Anweisung foreach (array_expression as $key => $value) Anweisung</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Die erste Form durchlΣuft das <VAR CLASS="literal" >array_expression</VAR >-Array. Bei jedem Durchgang wird der Wert des aktuellen Elements <VAR CLASS="literal" >$value</VAR > zugewiesen und der interne Array-Zeiger um eins erh÷ht. Dadurch wird beim nΣchsten Durchgang automatisch das nΣchste Element ausgewertet. </P ><P >
Die zweite Form arbeitet genauso, au▀er dass bei jedem Durchlauf auch der aktuelle Schlⁿssel der Variablen <VAR CLASS="literal" >$key</VAR > zugewiesen wird. </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Sobald <VAR CLASS="literal" >foreach</VAR > zum ersten Mal ausgefⁿhrt wird, wird der interne Arrayzeiger automatisch auf das erste Element des Arrays gesetzt. Das bedeutet, dass Sie vor einem Durchlauf von <VAR CLASS="literal" >foreach</VAR > <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A > nicht aufrufen mⁿssen. </P ></BLOCKQUOTE ></DIV > </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Beachten Sie auch, dass <VAR CLASS="literal" >foreach</VAR > mit einer Kopie des angegebenen Arrays arbeitet, nicht mit dem Array selbst. Deshalb wird auch der Arrayzeiger nicht wie bei dem <A HREF="#function.each" ><B CLASS="function" >each()</B ></A >-Konstrukt verΣndert und VerΣnderungen an ausgegebenen Arrayelementen haben keine Auswirkung auf das originale Array. Trotzdem <SPAN CLASS="emphasis" ><I CLASS="emphasis" >wird</I ></SPAN > der interne Arrayzeiger des originalen Arrays bei der Verarbeitung bewegt. Angenommen, die foreach-Schleife ist komplett abgearbeitet, wird der interne Arrayzeiger (des originalen Arrays) auf das letzte Element zeigen. </P ></BLOCKQUOTE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Bei <VAR CLASS="literal" >foreach</VAR > ist es nicht m÷glich Fehlermeldungen durch den Gebrauch von '@' zu unterdrⁿcken ist </P ></BLOCKQUOTE ></DIV ><P >
Beachten Sie, dass die folgenden Beispiele in ihrer FunktionalitΣt identisch sind: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4465" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $arr = array("eins", "zwei", "drei"); reset ($arr); while (list(, $value) = each ($arr)) { echo "Wert: $value<br />\n"; } foreach ($arr as $value) { echo "Wert: $value<br />\n"; ?> }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Auch hier funktioniert alles gleich: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4467" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $arr = array("eins", "zwei", "drei"); reset ($arr); while (list($key, $value) = each ($arr)) { echo "Schlⁿssel: $key; Wert: $value<br />\n"; } foreach ($arr as $key => $value) { echo "Schlⁿssel: $key; Wert: $value<br />\n"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Noch einige Beispiele, die die Anwendung verdeutlichen: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4470" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* foreach Beispiel 1: Nur der Wert */ $a = array(1, 2, 3, 17); foreach ($a as $v) { echo "Aktueller Wert von \$a: $v.\n"; } /* foreach Beispiel 2: Wert (mit Ausgabe des Arrayschlⁿssels zur Veranschaulichung) */ $a = array(1, 2, 3, 17); $i = 0; /* nur zu Veranschaulichung */ foreach($a as $v) { echo "\$a[$i] => $v.\n"; $i++; } /* foreach Beispiel 3: Schlⁿssel und Wert */ $a = array( "eins" => 1, "zwei" => 2, "drei" => 3, "siebzehn" => 17 ); foreach($a as $k => $v) { echo "\$a[$k] => $v.\n"; } /* foreach Beispiel 4: multidimensionale Arrays */ $a[0][0] = "a"; $a[0][1] = "b"; $a[1][0] = "y"; $a[1][1] = "z"; foreach($a as $v1) { foreach ($v1 as $v2) { echo "$v2\n"; } } /* foreach Beispiel 5: dynamische Arrays */ foreach (array(1, 2, 3, 4, 5) as $v) { echo "$v\n"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.break" ><VAR CLASS="literal" >break</VAR ></A ></H2 ><P >
<VAR CLASS="literal" >Break</VAR > bricht die Ausfⁿhrung der aktuellen <VAR CLASS="literal" >for</VAR >, <VAR CLASS="literal" >foreach</VAR > <VAR CLASS="literal" >while</VAR >, <VAR CLASS="literal" >do..while</VAR > Schleife oder einer <VAR CLASS="literal" >switch</VAR > Anweisungssequenz ab. </P ><P >
Einem <VAR CLASS="literal" >break</VAR > kann optional ein nummerisches Argument angehΣngt werden, das die Anzahl der abzubrechenden Befehlssequenzen enthΣlt. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4485" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $arr = array('eins', 'zwei', 'drei', 'vier', 'stop', 'fⁿnf'); while (list ( , $val) = each ($arr)) { if ($val == 'stop') { break; /* Sie k÷nnten hier auch 'break 1;' schreiben. */ } echo "$val<br />\n"; /* Benutzung des optionalen Argumentes. */ $i = 0; while (++$i) { switch ($i) { case 5: echo "Bei 5<br />\n"; break 1; /* Beendet nur switch. */ case 10: echo "Bei 10; aussteigen<br />\n"; break 2; /* Beendet switch und while. */ default: break; } } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.continue" ><VAR CLASS="literal" >continue</VAR ></A ></H2 ><P >
<VAR CLASS="literal" >continue</VAR > wird innerhalb von Schleifen verwendet. Die Schleife wird an der aktuellen Stelle abgebrochen und es wird der nΣchste Durchlauf begonnen. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Beachten Sie, dass in PHP die <A HREF="#control-structures.switch" >switch</A > Anweisung als Schleifenstruktur zum Zweck von <VAR CLASS="literal" >continue</VAR > angesehen wird. </P ></BLOCKQUOTE ></DIV ><P >
Bei <VAR CLASS="literal" >continue</VAR > k÷nnen Sie ein optionales nummerisches Argument mitgeben, das angibt, wie viele Ebenen von enhaltenen Schleifen ⁿbersprungen werden sollen. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4499" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php while (list ($key, $value) = each ($arr)) { if (!($key % 2)) { // ⁿberspringe ungerade Werte continue; } tue_was_mit_ungerade ($value); } $i = 0; while ($i++ < 5) { echo "Au▀en<br />\n"; while (1) { echo " Mitte<br />\n"; while (1) { echo " Innen<br />\n"; continue 3; } echo "Das wird nie ausgegeben.<br />\n"; } echo "Das hier auch nicht.<br />\n"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Lassen Sie das Semikolon nach <VAR CLASS="literal" >continue</VAR > weg, kann dies zu verwirrenden Ergebnissen fⁿhren. Es folgt ein Beispiel wie Sie es nicht machen sollten. </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4504" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php for ($i = 0; $i < 5; ++$i) { if ($i == 2) continue print "$i\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Sie k÷nnten erwarten, dass das Ergebnis wie folgt aussieht, </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >0 1 3 4</PRE ></TD ></TR ></TABLE ><P >
aber tatsΣchlich sieht die Ausgabe so aus, </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >2</PRE ></TD ></TR ></TABLE ><P >
weil der Rⁿckgabewert von <A HREF="#function.print" ><B CLASS="function" >print()</B ></A > <VAR CLASS="literal" >int(1)</VAR > ist und das wird als das oben angesprochene optinale Argument gewertet. </P ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.switch" ><VAR CLASS="literal" >switch</VAR ></A ></H2 ><P >
Die <VAR CLASS="literal" >switch</VAR >-Anweisung ist gleichbedeutend einer Reihe von <VAR CLASS="literal" >if</VAR >-Anweisungen mit dem gleichen Parameter. HΣufig wollen Sie ein und dieselbe Variable (bzw. den selben Ausdruck) mit verschiedensten Werten vergleichen und in AbhΣngigkeit vom Auswertungsergebnis verschiedene Programmteile ausfⁿhren. Genau das erm÷glicht die <VAR CLASS="literal" >switch</VAR >-Anweisung. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Beachten Sie bitte, dass im Unterschied zu anderen Programmiersprachen die <A HREF="#control-structures.continue" >continue</A > Anweisung auch bei switch ihre Gⁿltigkeit hat und Σhnlich wie <VAR CLASS="literal" >break</VAR > funktioniert. Falls Sie switch innerhalb einer Schleife verwenden und mit dem nΣchsten Durchlauf der Σu▀eren Schleife beginnen m÷chten, verwenden Sie <VAR CLASS="literal" >continue 2</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
Das folgende Beispiele zeigt Ihnen zwei verschiedene M÷glichkeiten auf, das Gleiche zu erreichen. Einmal werden <VAR CLASS="literal" >if</VAR > und <VAR CLASS="literal" >elseif</VAR > Anweisungen benutzt, das andere Mal eine <VAR CLASS="literal" >switch</VAR >-Anweisung: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4529" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($i == 0) { echo "i ist gleich 0"; } elseif ($i == 1) { echo "i ist gleich 1"; } elseif ($i == 2) { echo "i ist gleich 2"; } switch ($i) { case 0: echo "i ist gleich 0"; break; case 1: echo "i ist gleich 1"; break; case 2: echo "i ist gleich 2"; break; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Es ist wichtig, die Ausfⁿhrung einer <VAR CLASS="literal" >switch</VAR >-Anweisung zu verstehen, um Fehler zu vermeiden. Die <VAR CLASS="literal" >switch</VAR >-Anweisung wird Zeile fⁿr Zeile (also Anweisung fⁿr Anweisung) abgearbeitet. Zu Beginn wird kein Code ausgefⁿhrt. Erst wenn bei einem <VAR CLASS="literal" >case</VAR >-Teil eine Entsprechung zum <VAR CLASS="literal" >switch</VAR >-Ausdruck vorliegt, werden die darin enthaltenen Anweisungen von PHP ausgefⁿhrt. PHP fΣhrt dann mit der Abarbeitung des restlichen Codes innerhalb des <VAR CLASS="literal" >switch</VAR >-Blocks fort oder bis zum ersten Auftreten einer <VAR CLASS="literal" >break</VAR >-Anweisung. Ohne <VAR CLASS="literal" >break</VAR > am Ende eines case-Teils werden also noch die folgenden case-Bl÷cke ausgefⁿhrt. Zum Beispiel: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4539" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php switch ($i) { case 0: echo "i ist gleich 0"; case 1: echo "i ist gleich 1"; case 2: echo "i ist gleich 2"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Wenn hier <VAR CLASS="varname" >$i</VAR > gleich 0 ist, wⁿrde PHP alle echo Anweisungen ausfⁿhren! Ist <VAR CLASS="varname" >$i</VAR > gleich 1, werden die letzten beiden echo Anweisungen ausgefⁿhrt. Nur wenn <VAR CLASS="varname" >$i</VAR > gleich 2 ist, erhalten Sie das erwartete Ergebnis: die Ausgabe von "i ist gleich 2". Deshalb ist es wichtig <VAR CLASS="literal" >break</VAR >-Anweisungen zu setzen (abgesehen von bestimmten FΣllen, in denen Sie diese mit Absicht weglassen). </P ><P >
Bei einer <VAR CLASS="literal" >switch</VAR >-Anweisung wird die Bedingung also nur einmal ⁿberprⁿft und das Ergebnis mit jeder <VAR CLASS="literal" >case</VAR >-Anweisung verglichen. Bei einer <VAR CLASS="literal" >elseif</VAR >-Anweisung wird die Bedingung neu geprⁿft. Ist ihre Bedingung komplizierter als ein einfacher Vergleich und/oder in einer umfangreichen Schleife eingebettet, kann eine <VAR CLASS="literal" >switch</VAR >-Anweisung schneller sein. </P ><P >
Der Anweisungsteil von case kann auch leer sein. Dann wird die Kontrolle einfach an den nΣchsten case-Teil ⁿbergeben. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4552" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php switch ($i) { case 0: case 1: case 2: echo "i ist kleiner als 3 aber nicht negativ"; break; case 3: echo "i ist gleich 3"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Ein Spezialfall ist <VAR CLASS="literal" >default</VAR >. Dieser Fall trifft auf alles zu, was nicht von den voranstehenden case-Ausdrⁿcken erfasst wurde und sollte als letzte <VAR CLASS="literal" >case</VAR > Anweisung angegeben werden. Zum Beispiel: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4557" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php switch ($i) { case 0: echo "i ist gleich 0"; break; case 1: echo "i ist gleich 1"; break; case 2: echo "i ist gleich 2"; break; default: echo "i ist weder 0, 1 noch 2"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Der <VAR CLASS="literal" >case</VAR >-Ausdruck kann eine Prⁿfung einfacher Typen sein, also von Integer- oder Flie▀komma-Zahlen oder von Strings/Zeichenketten. Arrays oder Objekte k÷nnen nicht benutzt werden, es sei denn, sie wurden in einfache Typen umgewandelt. </P ><P >
Die alternative Syntax der Kontrollstrukturen gilt auch fⁿr switch-Sequenzen. Mehr Informationen dazu erhalten Sie unter <A HREF="#control-structures.alternative-syntax" >Alternative Syntax fⁿr Kontroll-Strukturen</A >. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4563" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php switch ($i): case 0: echo "i ist gleich 0"; break; case 1: echo "i ist gleich 1"; break; case 2: echo "i ist gleich 2"; break; default: echo "i ist weder 0, 1 noch 2"; endswitch; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="control-structures.declare" ><VAR CLASS="literal" >declare</VAR ></A ></H2 ><P >
Das Sprachkonstrukt <VAR CLASS="literal" >declare</VAR > wird dazu verwendet, um Ausfⁿhrungsdirektiven fⁿr einen Codeblock anzugeben. Die Schreibweise von <VAR CLASS="literal" >declare</VAR > ist der anderer Kontrollstrukturen Σhnlich: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4571" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >declare (Direktive) Anweisung</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Die <VAR CLASS="literal" >Direktive</VAR > gibt Ihnen die M÷glichkeit, das Verhalten des <VAR CLASS="literal" >declare</VAR >-Blocks zu bestimmen. Zur Zeit wird nur eine Direktive unterstⁿtzt: die <VAR CLASS="literal" >ticks</VAR > (Weiter unten finden Sie mehr Informationen zu den <A HREF="#control-structures.declare.ticks" >ticks</A >). </P ><P >
Der <VAR CLASS="literal" >Anweisung</VAR >steil des <VAR CLASS="literal" >declare</VAR >-Blocks wird ausgefⁿhrt - wie genau diese Ausfⁿhrung passiert und welche Nebeneffekte wΣhrend der Ausfⁿhrung auftreten, hΣngt von der Direktive ab, die Sie im <VAR CLASS="literal" >directive</VAR >-Block angegeben haben. </P ><P >
Das <VAR CLASS="literal" >declare</VAR > Konstrukt kann auch im globalen Geltungsbereich benutzt werden und gilt dann fⁿr den folgenden Code. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4584" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // bewirkt das Gleiche: // sie k÷nnen declare so benutzen: declare(ticks=1) { // hier folgt Ihr ganzes Skript } // oder wie hier: declare(ticks=1); // hier folgt Ihr ganzes Skript ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="control-structures.declare.ticks" >Ticks</A ></H3 ><P >
Ein tick ist ein Ereigniss, das bei jedem <VAR CLASS="varname" >N</VAR >-ten Autreten der low-level Anweisungen innerhalb des <VAR CLASS="literal" >declare</VAR > Blocks, die vom Parser ausgefⁿhrt werden, auftritt. Der Wert von <VAR CLASS="varname" >N</VAR > wird durch die Angabe von <VAR CLASS="literal" >ticks=<VAR CLASS="varname" >N</VAR ></VAR > innerhalb des <VAR CLASS="literal" >declare</VAR >-Blocks in dem <VAR CLASS="literal" >directive</VAR > Abschnitt bestimmt. </P ><P >
Das Ereignis/die Ereignisse, die bei jedem tick eintreten, legen Sie mit der Funktion <A HREF="#function.register-tick-function" ><B CLASS="function" >register_tick_function()</B ></A > fest. Weitere Einzelheiten k÷nnen Sie dem Beispiel unten entnehmen. Beachten Sie, dass mehr als ein Ereigniss fⁿr jeden tick eintreten kann. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4599" ></A ><P ><B >Beispiel 15-1. Profil eines Bereichs von PHP Code</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Funktion, die bei Aufruf die Zeit aufzeichnet function profile($dump = FALSE) { static $profile; // Rⁿckgabe der gespeicherten Zeit aus profile, danach l÷schen if ($dump) { $temp = $profile; unset ($profile); return ($temp); } $profile[] = microtime (); } // Einen tick handler bestimmen register_tick_function("profile"); // Funktion vor dem declare-Block initialisieren profile(); // Ausfⁿhren eines Code-Blocks, jede 2te Anweisung l÷st einen tick aus declare(ticks=2) { for ($x = 1; $x < 50; ++$x) { echo similar_text(md5($x), md5($x*$x)), "<br />;"; } } // Ausgabe der gespeicherten Daten aus dem Profiler print_r(profile (TRUE)); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Dieses Beispiel 'profiliert' den PHP Code der im 'declare'-Block steht, indem die Zeit festgehalten wird, zu der jede zweite low-level Anweisung im Codeblock ausgefⁿhrt wird. Diese Information k÷nnen Sie dazu benutzen, langsame Bereiche innerhalb bestimmter Codesegmente zu identifizieren. Das gleiche Ziel k÷nnen Sie auch mit anderen Methoden erreichen: die Benutzung von ticks ist bequemer und einfacher zu implementieren. </P ><P >
Ticks sind gut fⁿr Debugging, einfaches Multitasking, Hintergrund I/O und viele andere Aufgaben geeignet. </P ><P >
Siehe auch <A HREF="#function.register-tick-function" ><B CLASS="function" >register_tick_function()</B ></A > und <A HREF="#function.unregister-tick-function" ><B CLASS="function" >unregister_tick_function()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="function.return" >return</A ></H2 ><P >
Wird die <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > Anweisung innerhalb einer Funktion aufgerufen, wird die Ausfⁿhrung der Funktion sofort beendet und das Argument als Wert des Funktionsaufrufs zurⁿckgegeben. <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > beendet auch die Ausfⁿhrung einer <A HREF="#function.eval" ><B CLASS="function" >eval()</B ></A > Anweisung oder einer Skriptdatei. </P ><P >
Erfolgt der Aufruf innerhalb des globalen Bereichs, wird die Ausfⁿhrung des aktuellen Skripts beendet. Wurde das aktuelle Skript <A HREF="#function.include" ><B CLASS="function" >include()</B ></A >ed oder <A HREF="#function.require" ><B CLASS="function" >require()</B ></A >ed, wird die Kontrolle an das aufrufende Skript zurⁿckgegeben. Wurde das aktuelle Skript <A HREF="#function.include" ><B CLASS="function" >include()</B ></A >ed, wird der Wert, der <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > zugewiesen wurde, als Wert des Aufrufs von <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > zurⁿckgegeben. Wird <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > innerhalb des Hauptskripts aufgerufen, wird die Ausfⁿhrung beendet. Handelt es sich bei dem Skript um eine Datei, die ⁿber die EintrΣge <A HREF="#ini.auto-prepend-file" >auto_prepend_file</A > oder <A HREF="#ini.auto-append-file" >auto_append_file</A > in der <TT CLASS="filename" >php.ini</TT > aufgerufen wurde, wird die Ausfⁿhrung dieses Skripts beendet. </P ><P >Weitere Informationen erhalten Sie im Abschnitt <A HREF="#functions.returning-values" >Rⁿckgabewerte</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Beachten Sie, dass <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > ein Sprachkonstrukt und keine Funktion ist. Die Klammern um ein Argument sind deshalb <SPAN CLASS="emphasis" ><I CLASS="emphasis" >nur</I ></SPAN > zwingend notwendig, wenn es sich um einen Ausdruck handelt, dessen Ergebnis zurⁿckgegeben werden soll. Es ist gebrΣuchlich die Klammern wegzulassen, wenn eine Variable zurⁿck gegeben soll. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="function.require" ><A HREF="#function.require" ><B CLASS="function" >require()</B ></A ></A ></H2 ><P >
Die <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > Anweisung bindet die angegebene Datei ein und wertet diese aus. </P ><P >
<A HREF="#function.require" ><B CLASS="function" >require()</B ></A > bindet die angegebene Datei ein und fⁿhrt diese aus. Genaue Informationen wie die Einbindung funktioniert ist in der Dokumentation fⁿr <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > beschrieben. </P ><P >
<A HREF="#function.require" ><B CLASS="function" >require()</B ></A > und <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > sind in jeder Hinsicht gleichwertig mit der einen Ausnahme: der Umgang mit Fehlern. <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > erzeugt ein <A HREF="#internal.e-warning" >Warning</A > wΣhrend <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > in einem <A HREF="#internal.e-error" >
Fatal Error</A > endet. Mit anderen Worten: scheuen Sie sich nicht, <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > zu benutzen, wenn Sie m÷chten, dass eine fehlende Datei die Ausfⁿhrung ihres Skripts beendet. <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > verhΣlt sich anders, ihr Skript wird weiterhin ausgefⁿhrt. Stellen Sie au▀erdem sicher, dass Sie einen gⁿltigen <A HREF="#ini.include-path" >include_path</A > gesetzt haben. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4647" ></A ><P ><B >Beispiel 15-2. Grundlegende <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > Beispiele</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php require 'prepend.php'; require $somefile; require ('somefile.txt'); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Weitere Beispiele finden Sie in der Dokumentation zu <A HREF="#function.include" ><B CLASS="function" >include()</B ></A >. </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Fⁿr frⁿhere Versionen als PHP 4.0.2 gilt folgendes: <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > wird immer versuchen die Zieldatei zu lesen, selbst wenn die Zeile in der die Anweisung steht, nie ausgefⁿhrt wird. Eine bedingte Anweisung hat keine Auswirkungen auf <A HREF="#function.require" ><B CLASS="function" >require()</B ></A >. Wenn jedoch die Zeile in der <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > steht, nie ausgefⁿhrt wird, wird auch der Code der Zieldatei nie ausgefⁿhrt werden. ─hnliches gilt fⁿr Schleifenstrukturen, diese beeinflussen das Verhalten von <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > nicht. Obwohl der Code, der in der Zieldatei enthalten ist, zur Schleife geh÷rt, wird <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > selbst nur einmal ausgefⁿhrt. </P ></BLOCKQUOTE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Da dies ein Sprachkonstrukt und keine Funktion ist, deshalb k÷nnen Sie dieses nicht mit <A HREF="#functions.variable-functions" >Variablenfunktionen</A > verwenden.</P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >PHP Versionen kleiner 4.3.0 fⁿr Windows, erlauben den Zugriff auf Remote-Dateien mit dieser Funktion nicht, selbst wenn <A HREF="#ini.allow-url-fopen" >allow_url_fopen</A > aktiviert ist.</P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch <A HREF="#function.include" ><B CLASS="function" >include()</B ></A >, <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A >, <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A >, <A HREF="#function.eval" ><B CLASS="function" >eval()</B ></A >, <A HREF="#function.file" ><B CLASS="function" >file()</B ></A >, <A HREF="#function.readfile" ><B CLASS="function" >readfile()</B ></A >, <A HREF="#function.virtual" ><B CLASS="function" >virtual()</B ></A > und <A HREF="#ini.include-path" >include_path</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="function.include" ><A HREF="#function.include" ><B CLASS="function" >include()</B ></A ></A ></H2 ><P >
Die <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > Anweisung bindet die angegebene Datei ein und wertet diese aus. </P ><P >
Die untenstehende Dokumentation gilt ebenso fⁿr <A HREF="#function.require" ><B CLASS="function" >require()</B ></A >. Diese beiden Konstrukte sind in jeder Hinsicht gleichwertig mit der einen Ausnahme: der Umgang mit Fehlern. <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > erzeugt ein <A HREF="#internal.e-warning" >Warning</A > wΣhrend <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > in einem <A HREF="#internal.e-error" >
Fatal Error</A > endet. Mit anderen Worten, verwenden Sie <A HREF="#function.require" ><B CLASS="function" >require()</B ></A >, wenn Sie m÷chten, dass eine fehlende Datei die Ausfⁿhrung ihres Skripts beendet. <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > verhΣlt sich anders, ihr Skript wird weiterhin ausgefⁿhrt. Stellen Sie au▀erdem sicher, dass Sie einen gⁿltigen <A HREF="#ini.include-path" >include_path</A > gesetzt haben. Beachten Sie, dass eine Parse Error in einer Datei, die mit require eingebunden wurde, die Ausfⁿhrung ihre Skripts nicht unterbricht. </P ><P >
Dateien die mit include eingebunden werden, werden zuerst im include_path relativ zum gegenwΣrtigen Arbeitsverzeichnis gesucht und danach im include_path relativ zum Verzeichnis des ausgefⁿhrten Skripts. Zur Veranschaulichung: Falls Ihr include_path <VAR CLASS="literal" >.</VAR > entspricht, ist das gegenwΣrtige Arbeitsverzeichnis <TT CLASS="filename" >/www/</TT >. Sie haben ⁿber include <TT CLASS="filename" >include/a.php</TT > eingebunden und in diesem Skript steht die Anweisung <VAR CLASS="literal" >include "b.php"</VAR >. In diesem Fall wird <TT CLASS="filename" >b.php</TT > zuerst im Verzeichnis <TT CLASS="filename" >/www/</TT > gesucht und danach im Verzeichnis <TT CLASS="filename" >/www/include/</TT >. </P ><P >
Wenn eine Datei eingebunden wird, erbt der enthaltene Code den <A HREF="#language.variables.scope" >Geltungsbereich von Variablen</A > der Zeile in der die Anweisung steht. Ab dieser Zeile stehen alle verfⁿgbaren Variablen in der aufgerufenen Datei im aufrufenden Skript zur Verfⁿgung. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4701" ></A ><P ><B >Beispiel 15-3. Grundlegendes <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >vars.php <?php $color = 'grⁿn'; $fruit = 'Apfel'; ?> test.php <?php echo "Ein $color $fruit"; // A include 'vars.php'; echo "Ein $color $fruit"; // Ein grⁿner Apfel ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Steht include im aufrufenden Skript innerhalb einer Funktion, verhΣlt sich der gesamte Code der aufgerufenen Datei genau so, als ob Sie diesen Code innerhalb dieser Funktion definiert hΣtten. Aus diesem Grund hat dieser Code den Geltungsbereich der Variablen dieser Funktion. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4707" ></A ><P ><B >Beispiel 15-4. Include innerhalb einer Funktion</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function foo() { global $color; include 'vars.php'; echo "Ein $color $fruit"; } /* vars.php ist im Geltungsbereich von foo(), * * d.h. $fruit steht au▀erhalb dieses Bereichs * * NICHT zur Verfⁿgung. $color schon, da wir * * diese Variable als global definiert haben */ foo(); // Ein grⁿner Apfel echo "Ein $color $fruit"; // Ein grⁿner ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Beim Einbinden einer Datei wechselt der Parser vom PHP-Modus zu Beginn der Zieldatei in den HTML-Modus und kehrt am Ende der eingebunden Datei wieder in den PHP-Modus zurⁿck. Deshalb muss jeglicher Code innerhalb der eingebundenen Datei, der als PHP-Code ausgefⁿhrt werden soll, von <A HREF="#language.basic-syntax.phpmode" >gⁿltigen PHP-Start- und Ende-Tags</A > eingefa▀t sein. </P ><P >
Wenn "<A HREF="#ini.allow-url-fopen" >URL fopen wrappers</A >" in PHP aktiviert sind (in der Standardkonfiguration ist das der Fall) k÷nnen Sie als Pfad der einzubindenden Datei auch eine URL (via HTTP oder anderen unterstⁿtzen Wrappern - eine Liste der unterstⁿtzen Protokolle finden Sie unter <A HREF="#wrappers" >Anhang L</A >) statt einer lokalen Pfadangabe angeben. Falls der Zielserver die Zieldatei als PHP-Code interpretiert, k÷nnen Sie an die einzubindende Datei Variablen in einem Request-String ⁿbergeben, genauso wie bei HTTP GET. Streng genommen ist das nicht das Gleiche, wie diese Datei einzubinden und diesem den Geltungsbereich des Vater-Skripts zu vererben; das Skript wird auf dem Remote-Server ausgefⁿhrt und danach wird das Ergebnis in das lokale Skript eingebunden. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >PHP Versionen kleiner 4.3.0 fⁿr Windows, erlauben den Zugriff auf Remote-Dateien mit dieser Funktion nicht, selbst wenn <A HREF="#ini.allow-url-fopen" >allow_url_fopen</A > aktiviert ist.</P ></TD ></TR ></TABLE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4719" ></A ><P ><B >Beispiel 15-5. <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > ⁿber HTTP</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* Dieses Beispiel geht davon aus, dass www.example.com so konfiguriert * * ist, dass .php-Dateien geparst werden und keine .txt Dateien. Also meint * * 'Funkt' hier, dass die Variablen $foo und $bar innerhalb der * * angeforderten Datei zur Verfⁿgung stehen */ // Funkt nicht; file.txt wird von www.example.com nicht als PHP geparst include 'http://www.example.com/file.txt?foo=1&bar=2'; // Funkt nicht; schaut nach einer lokalen Datei namens // 'file.php?foo=1&bar=2' im lokalen Dateisystem include 'file.php?foo=1&bar=2'; // Funkt include 'http://www.example.com/file.php?foo=1&bar=2'; $foo = 1; $bar = 2; include 'file.txt'; // Funkt include 'file.php'; // Funkt ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Siehe auch <A HREF="#features.remote-files" >
Zugriff auf entfernte Dateien</A >, <A HREF="#function.fopen" ><B CLASS="function" >fopen()</B ></A > und <A HREF="#function.file" ><B CLASS="function" >file()</B ></A > fⁿr verwandte Informationen. </P ><P >
Da <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > und <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > spezielle Sprachkonstrukte sind, mⁿssen Sie diese innerhalb einer bedingten Anweisung in einen Anweisungsblock setzen. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4730" ></A ><P ><B >Beispiel 15-6. include() und bedingte Bl÷cke</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Das ist FALSCH und fⁿhrt nicht zum gewⁿnschten Ergebnis. if ($bedingung) include $datei; else include $andere_datei; // Das ist KORREKT. if ($bedingung) { include $datei; } else { include $andere_datei; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Der Umgang mit Returns: Es ist m÷glich eine <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > -Anweisung innerhalb einer eingebunden Datei anzugeben, um die Ausfⁿhrung innerhalb dieser Datei abzubrechen und zum aufrufenden Skript zurⁿckzukehren. Ebenso ist die Rⁿckgabe von Werten aus einer eingebunden Datei m÷glich. Sie k÷nnen den Wert eines include-Aufrufs auf die gleiche Art und Weise nutzen, wie Sie es bei einer Funktion machen wⁿrden. Allerdings besteht diese M÷glichkeit nicht, wenn Sie entfernte Dateien mittels include einbinden au▀er, wenn die Ausgabe der entfernten Datei <A HREF="#language.basic-syntax.phpmode" >
gⁿltige PHP Start- und Endetags</A > beeinhaltet (wie jede lokale Datei auch). Innerhalb dieser Tags k÷nnen Sie die ben÷tigten Variablen deklarieren und diese werden dann an dem Punkt Ihres Skripts eingefⁿhrt, von wo aus der Aufruf mit include erfolgt ist. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In PHP 3 darf eine return-Anweisung nicht innerhalb eines Blocks auftreten, es sei denn, es ist ein Funktionsblock. In diesem Fall gilt <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > fⁿr diese Funktion und nicht fⁿr die ganze Datei. </P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4740" ></A ><P ><B >Beispiel 15-7. <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > und die <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > Anweisung</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >return.php <?php $var = 'PHP'; return $var; ?> noreturn.php <?php $var = 'PHP'; ?> testreturns.php <?php $foo = include 'return.php'; echo $foo; // gibt 'PHP' aus $bar = include 'noreturn.php'; echo $bar; // gibt 1 aus ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<VAR CLASS="literal" >$bar</VAR > hat den Wert <VAR CLASS="literal" >1</VAR >, weil include erfolgreich war. Beachten Sie die Unterschiede in den obigen Beispielen. Das erste nutzt <A HREF="#function.return" ><B CLASS="function" >return()</B ></A > innerhalb der eingebundenen Datei im Gegensatz zum zweiten Beispiel. Weitere M÷glichkeiten Dateien in Variablen "einzubinden" bieten ihnen die Funktionen <A HREF="#function.fopen" ><B CLASS="function" >fopen()</B ></A > und <A HREF="#function.file" ><B CLASS="function" >file()</B ></A > oder <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > im Zusammenspiel mit den <A HREF="#ref.outcontrol" >Funktionen zur Ausgabesteuerung</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Da dies ein Sprachkonstrukt und keine Funktion ist, deshalb k÷nnen Sie dieses nicht mit <A HREF="#functions.variable-functions" >Variablenfunktionen</A > verwenden.</P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.require" ><B CLASS="function" >require()</B ></A >, <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A >, <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A >, <A HREF="#function.readfile" ><B CLASS="function" >readfile()</B ></A >, <A HREF="#function.virtual" ><B CLASS="function" >virtual()</B ></A >und <A HREF="#ini.include-path" >include_path</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="function.require-once" ><A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A ></A ></H2 ><P >
Die <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A > Anweisung bindet eine Datei ein und wertet diese zur Laufzeit des Skripts aus. Das Verhalten ist Σhnlich der <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > Anweisung mit dem einzigen Unterschied, dass einmal eingebundener Code aus einer Datei nicht nocht einmal eingebunden wird. Lesen Sie die Dokumentation zu <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > um mehr Informationen ⁿber die Arbeitsweise dieser Anweisung zu erhalten. </P ><P >
<A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A > sollten Sie in den FΣllen benutzen, wenn die gleiche Datei in einem bestimmten Bereich mehrmals eingebunden und interpretiert werden soll und Sie sicher stellen wollen, dass diese Datei nur exakt einmal eingebunden wird, um Probleme mit Wiederholungen bei Funktions-Definitionen und Wertzuweisungen zu Variablen zu vermeiden, usw. </P ><P >
Weitere Beispiele zu <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A > und <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A > k÷nnen Sie dem <A HREF="http://pear.php.net/" TARGET="_top" >PEAR</A >-Code entnehmen, welcher im aktuellsten PHP-Sourcecode enthalten ist. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A > steht ab PHP 4.01pl2 zur Verfⁿgung. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Beachten Sie, dass auf auf einem Betriebssystem, das nicht zwischen Gro▀- und Kleinschreibung unterschiedet (wie z.B. Windows) das Verhalten von <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A > und <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A > nicht unbedingt ihren Erwartungen entspricht. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4783" ></A ><P ><B >Beispiel 15-8. <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A > unterscheidet nicht zwischen Gro▀-/Kleinschreibung unter Windows </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php require_once("a.php"); // bindet a.php ein require_once("A.php"); // bindet a.php auf Windows nochmal ein! ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >PHP Versionen kleiner 4.3.0 fⁿr Windows, erlauben den Zugriff auf Remote-Dateien mit dieser Funktion nicht, selbst wenn <A HREF="#ini.allow-url-fopen" >allow_url_fopen</A > aktiviert ist.</P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch: <A HREF="#function.require" ><B CLASS="function" >require()</B ></A >, <A HREF="#function.include" ><B CLASS="function" >include()</B ></A >, <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A >, <A HREF="#function.get-required-files" ><B CLASS="function" >get_required_files()</B ></A >, <A HREF="#function.get-included-files" ><B CLASS="function" >get_included_files()</B ></A >, <A HREF="#function.readfile" ><B CLASS="function" >readfile()</B ></A > und <A HREF="#function.virtual" ><B CLASS="function" >virtual()</B ></A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="function.include-once" ><A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A ></A ></H2 ><P >
Die Anweisung <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A > schlie▀t zur Ausfⁿhrungszeit die angegebene Datei ein und wertet diese aus. Dies ist Σhnlich der <A HREF="#function.include" ><B CLASS="function" >include()</B ></A >-Anweisung; mit dem Unterschied, dass einmal eingebundener Code nicht nochmals eingebunden wird. Wie der Name vermuten lΣsst, wird die Datei nur ein einziges Mal eingebunden. </P ><P >
<A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A > sollten Sie in den FΣllen benutzen, wenn die gleiche Datei in einem bestimmten Bereich mehrmals eingebunden und interpretiert werden soll und Sie sicher stellen wollen, dass diese Datei nur exakt einmal eingebunden wird, um Probleme mit Wiederholungen von Funktionsdefinitionen und Wertzuweisungen an Variablen zu vermeiden, usw. </P ><P >
Weitere Beispiele zu <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A > und <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A > k÷nnen Sie dem <A HREF="http://pear.php.net/" TARGET="_top" >PEAR</A >-Code entnehmen, welcher im aktuellsten PHP-Source-Code enthalten ist. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A > steht ab PHP 4.01pl2 zur Verfⁿgung. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Beachten Sie, dass auf auf einem Betriebssystem, das nicht zwischen Gro▀- und Kleinschreibung unterschiedet (wie z.B. Windows) das Verhalten von <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A > und <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A > nicht unbedingt ihren Erwartungen entspricht. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4817" ></A ><P ><B >Beispiel 15-9. <A HREF="#function.include-once" ><B CLASS="function" >include_once()</B ></A > unterscheidet nicht zwischen Gro▀-/Kleinschreibung unter Windows </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php include_once("a.php"); // bindet a.php ein include_once("A.php"); // bindet a.php auf Windows nochmal ein! ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >PHP Versionen kleiner 4.3.0 fⁿr Windows, erlauben den Zugriff auf Remote-Dateien mit dieser Funktion nicht, selbst wenn <A HREF="#ini.allow-url-fopen" >allow_url_fopen</A > aktiviert ist.</P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch <A HREF="#function.include" ><B CLASS="function" >include()</B ></A >, <A HREF="#function.require" ><B CLASS="function" >require()</B ></A >, <A HREF="#function.require-once" ><B CLASS="function" >require_once()</B ></A >, <A HREF="#function.get-required-files" ><B CLASS="function" >get_required_files()</B ></A >, <A HREF="#function.get-included-files" ><B CLASS="function" >get_included_files()</B ></A >, <A HREF="#function.readfile" ><B CLASS="function" >readfile()</B ></A > und <A HREF="#function.virtual" ><B CLASS="function" >virtual()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="functions" >Kapitel 16. Funktionen</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="functions.user-defined" >Vom Nutzer definierte Funktionen</A ></H2 ><P >
Eine Funktion kann wie folgt definiert werden: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4837" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function foo ($arg_1, $arg_2, ..., $arg_n) { echo "Beispielfunktion.\n"; return $retval; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Jeder beliebige korrekte PHP-Code kann in einer Funktion vorkommen, sogar andere Funktionen und <A HREF="#keyword.class" >Klassen-</A > Definitionen. </P ><P >
In PHP 3 mⁿssen Funktionen definiert sein, bevor man auf sie verweist. In PHP4 ist das nicht mehr erforderlich, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >au▀er</I ></SPAN > wenn eine Funktion nur bedingt definiert wird, wie in den beiden untenstehenden Beispielen gezeigt. </P ><P >
Wenn eine Funktion nur unter bestimmten Bedingungen definiert wird, mu▀ die Definition dieser Funktion noch <SPAN CLASS="emphasis" ><I CLASS="emphasis" >vor</I ></SPAN > deren Aufruf abgearbeitet werden. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4845" ></A ><P ><B >Beispiel 16-1. Bedingte Funktionen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $makefoo = true; /* Wir k÷nnen foo() von her aus nicht aufrufen, da sie noch nicht existiert, aber wir k÷nnen bar() aufrufen */ bar(); if ($makefoo) { function foo () { echo "Ich existiere nicht, bis mich die Programmausfⁿhrung erreicht hat.\n"; } } /* Nun k÷nnen wir foo() sicher aufrufen, da $makefoo als true ausgewertet wurde */ if ($makefoo) foo(); function bar() { echo "Ich existiere sofort nach Programmstart.\n"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4848" ></A ><P ><B >Beispiel 16-2. Funktionen innerhalb von Funktionen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function foo() { function bar() { echo "Ich existiere nicht, bis foo() aufgerufen wurde.\n"; } } /* Wir k÷nnen bar() noch nicht aufrufen, da es nicht existiert */ foo(); /* Nun k÷nnen wir auch bar() aufrufen, da sie durch die Abarbeitung von foo() verfⁿgbar gemacht wurde */ bar(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
PHP unterstⁿtzt weder das ▄berladen von Funktionen, noch ist es m÷glich, zuvor deklarierte Funktionen neu zu definieren oder die Definition zu l÷schen. </P ><P >
PHP 3 unterstⁿtzt keine variable Anzahl von Parametern, obwohl Vorgabewerte fⁿr Parameter unterstⁿtzt werden (weitere Informationen finden Sie unter <A HREF="#functions.arguments.default" >Vorgabewerte fⁿr Parameter</A >). PHP 4 unterstⁿtzt beides: siehe <A HREF="#functions.variable-arg-list" >Variable Parameteranzahl</A > und die Funktionsreferenzen fⁿr <A HREF="#function.func-num-args" ><B CLASS="function" >func_num_args()</B ></A >, <A HREF="#function.func-get-arg" ><B CLASS="function" >func_get_arg()</B ></A > und <A HREF="#function.func-get-args" ><B CLASS="function" >func_get_args()</B ></A > fⁿr weitere Informationen. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="functions.arguments" >Funktionsparameter</A ></H2 ><P >
Mit einer Parameterliste kann man Informationen an eine Funktion ⁿbergeben. Die Parameterliste ist eine durch Kommas getrennte Liste von Variablen und/oder Konstanten. </P ><P >
PHP unterstⁿtzt die Weitergabe von Parametern als Werte (das ist der Standard), als <A HREF="#functions.arguments.by-reference" >Verweise</A >, und als <A HREF="#functions.arguments.default" >Vorgabewerte</A >. Die ▄bergabe einer variablen Anzahl von Parametern wird nur von PHP 4 und h÷her unterstⁿtzt, siehe <A HREF="#functions.variable-arg-list" >Variable Parameteranzahl</A > und die Funktionsreferenzen fⁿr <A HREF="#function.func-num-args" ><B CLASS="function" >func_num_args()</B ></A >, <A HREF="#function.func-get-arg" ><B CLASS="function" >func_get_arg()</B ></A > und <A HREF="#function.func-get-args" ><B CLASS="function" >func_get_args()</B ></A > fⁿr weitere Informationen. Durch die ▄bergabe eines Arrays mit Parametern kann man auch in PHP 3 einen Σhnlichen Effekt erreichen: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4868" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function rechne_array($eingabe) { echo "$eingabe[0] + $eingabe[1] = ", $eingabe[0]+$eingabe[1]; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="functions.arguments.by-reference" >Verweise als Parameter ⁿbergeben</A ></H3 ><P >
Normalerweise werden den Funktionen Werte als Parameter ⁿbermittelt. Wenn man den Wert dieses Parameters innerhalb der Funktion Σndert, bleibt der Parameter au▀erhalb der Funktion unverΣndert. Wollen Sie aber genau das erreichen, dann mⁿssen Sie die Parameter als Verweise (Referenzen) ⁿbergeben. </P ><P >
Wenn eine Funktion einen Parameter generell als Verweis behandeln soll, setzt man in der Funktionsdefinition ein kaufmΣnnisches Und (&) vor den Parameternamen: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4874" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function fuege_etwas_anderes_an (&$string) { $string .= 'und nun zu etwas anderem.'; } $str = 'Dies ist ein String, '; fuege_etwas_anderes_an ($str); echo $str; // Ausgabe: 'Dies ist ein String, und nun zu etwas anderem.'</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="functions.arguments.default" >Vorgabewerte fⁿr Parameter</A ></H3 ><P >
Eine Funktion kann C++-artige Vorgabewerte fⁿr skalare Parameter wie folgt definieren: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4879" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function machkaffee ($typ = "Cappucino") { return "Ich mache eine Tasse $typ.\n"; } echo machkaffee (); echo machkaffee ("Espresso");</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Die Ausgabe von diesem kleinen Skript ist: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Ich mache eine Tasse Cappucino. Ich mache eine Tasse Espresso.</PRE ></TD ></TR ></TABLE > </P ><P >
Der Vorgabewert muss ein konstanter Ausdruck sein, darf also (zum Beispiel) keine Variable oder Element einer Klasse sein. </P ><P >
Bitte beachten Sie, dass alle Vorgabewerte rechts von den Nicht-Vorgabeparametern stehen sollten; - sonst wird es nicht funktionieren. Betrachten Sie folgendes Beispiel: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4885" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function mach_joghurt ($typ = "rechtsdrehendes", $geschmack) { return "Mache einen Becher $typ $geschmack-joghurt.\n"; } echo mach_joghurt ("Brombeer"); // arbeitet nicht wie erwartet</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Die Ausgabe dieses Beispiels ist:: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Warning: Missing argument 2 in call to makeyogurt() in /usr/local/etc/httpd/htdocs/php3test/functest.html on line 41 Mache einen Becher Brombeer-joghurt.</PRE ></TD ></TR ></TABLE ></P ><P >
Nun vergleichen Sie bitte oberes Beispiel mit folgendem: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4890" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function mach_joghurt ($geschmack, $typ = "rechtsdrehendes") { return "Mache einen Becher $typ $geschmack-joghurt.\n"; } echo mach_joghurt ("Brombeer"); // arbeitet wie erwartet.</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
... und jetzt ist die Ausgabe: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Mache einen Becher rechtsdrehendes Brombeer-Joghurt.</PRE ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="functions.variable-arg-list" >Variable Anzahl von Parametern</A ></H3 ><P >
PHP 4 unterstⁿtzt eine variable Anzahl von Parametern in benutzerdefinierten Funktionen. Das Handling dieser Parameter fΣllt mittels der Funktionen <A HREF="#function.func-num-args" ><B CLASS="function" >func_num_args()</B ></A >, <A HREF="#function.func-get-arg" ><B CLASS="function" >func_get_arg()</B ></A > und <A HREF="#function.func-get-args" ><B CLASS="function" >func_get_args()</B ></A > sehr leicht. </P ><P >
Es ist keine spezielle Syntax erforderlich. Die Parameter k÷nnen wie gehabt explizit in den Funktionsdeklarationen angegeben werden, und werden sich wie gewohnt verhalten. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="functions.returning-values" >Rⁿckgabewerte</A ></H2 ><P >
Sie k÷nnen Werte mit dem optionalen Befehl "return" zurⁿckgeben. Es k÷nnen Variablen jedes Typs zurⁿckgegeben werden, auch Listen oder Objekte. Die beendet sofort die Funktion, und die Kontrolle wird wieder an die aufrufende Zeile zurⁿckgegeben. Weitere Informationen finden Sie unter <A HREF="#function.return" ><B CLASS="function" >return()</B ></A >. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4905" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function quadrat ($zahl) { return $zahl * $zahl; } echo quadrat (4); // gibt '16' aus.</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Es ist nicht m÷glich, mehrere Werte von einer Funktion zurⁿckzugeben. Ein Σhnliches Resultat kann man aber durch die Rⁿckgabe von Listen erreichen. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4908" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function kleine_zahlen() { return array (0, 1, 2); } list ($null, $eins, $zwei) = kleine_zahlen();</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Um von einer Funktion eine Referenz zurⁿckzugeben, mⁿssen Sie den Referenz-Operator & sowohl in der Funktionsdeklaration, als auch bei der Zuweisung des zurⁿckgegebenen Wertes verwenden: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4911" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function &returniere_referenz() { return $einereferenz; } $neuereferenz =& returniere_referenz();</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Weitere Informationen ⁿber Referenzen finden Sie im Kapitel <A HREF="#language.references" >Referenzen in PHP</A >. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="functions.old-syntax" ><VAR CLASS="literal" >old_function</VAR ></A ></H2 ><P >
Der Befehl <VAR CLASS="literal" >old_function</VAR > erlaubt es, eine Funktion genauso wie bei PHP/FI2 zu definieren (nur "function" wird durch "old_function" ersetzt. </P ><P >
Dieser Befehl sollte nur vom PHP/FI2->PHP3-Konverter genutzt werden. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Funktionen, die durch <VAR CLASS="literal" >old_function</VAR > definiert werden, k÷nnen nicht von PHP selbst aufgerufen werden. Das hei▀t unter anderem, dass sie nicht in Funktionen wie <B CLASS="function" >usort ()</B >, <A HREF="#function.array-walk" ><B CLASS="function" >array_walk()</B ></A > und <B CLASS="function" >
register_shutdown_function()</B > benutzt werden k÷nnen. Dieses Manko k÷nnen Sie umgehen, indem Sie eine Adapterfunktion (in normalem PHP3) schreiben, welche die <VAR CLASS="literal" >old_function</VAR > aufruft. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="functions.variable-functions" >Variablenfunktionen</A ></H2 ><P >
PHP unterstⁿtzt das Konzept der Variablenfunktionen. Wenn Sie an das Ende einer Variablen Klammern hΣngen, versucht PHP eine Funktion aufzurufen, deren Name der aktuelle Wert der Variable ist. Dies kann unter anderem fⁿr Callbacks, Funktionstabellen, usw. genutzt werden. </P ><P >
Variablenfunktionen funktionieren nicht mit Sprachkonstrukten wie <A HREF="#function.echo" ><B CLASS="function" >echo()</B ></A >, <A HREF="#function.print" ><B CLASS="function" >print()</B ></A >, <A HREF="#function.unset" ><B CLASS="function" >unset()</B ></A >, <A HREF="#function.isset" ><B CLASS="function" >isset()</B ></A >, <A HREF="#function.empty" ><B CLASS="function" >empty()</B ></A >, <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > und <A HREF="#function.require" ><B CLASS="function" >require()</B ></A >. Sie mⁿssen Ihre eigenen Wrapperfunktionen verwenden, um diese Konstrukte als variable Funktionen benutzen zu k÷nnen. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4940" ></A ><P ><B >Beispiel 16-3. Beispiel fⁿr Variablenfunktionen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function foo() { echo "In foo()<br>\n"; } function bar($arg = '') { echo "In bar(); der Parameter ist '$arg'.<br>\n"; } // Dies ist eine Wrapperfunkiton fⁿr echo function echoit($string) { echo $string; } $func = 'foo'; $func(); // Dies ruft foo() auf $func = 'bar'; $func('test'); // Dies ruft bar() auf $func = 'echoit'; $func('test'); // Dies ruft echoit() auf ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Sie k÷nnen auch die Methode eines Objektes mittels der variablen Funktionen aufrufen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4944" ></A ><P ><B >Beispiel 16-4. Variable Methode</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Foo { function Var() { $name = 'Bar'; $this->$name(); // Dies ruft die Bar() Methode auf } function Bar() { echo "Das ist Bar"; } } $foo = new Foo(); $funcname = "Var"; $foo->$funcname(); // Dies ruft $foo->Var() auf ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.call-user-func" ><B CLASS="function" >call_user_func()</B ></A >, <A HREF="#language.variables.variable" >
Variable Variablen</A > und <A HREF="#function.function-exists" ><B CLASS="function" >function_exists()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.oop" >Kapitel 17. Klassen und Objekte</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="keyword.class" ><VAR CLASS="literal" >Klassen</VAR ></A ></H2 ><P >
Eine Klasse ist eine Sammlung von Variablen und Funktionen, die mit diesen Variablen arbeiten. Eine Klasse wird folgenderma▀en definiert: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4957" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Cart { var $items; // Artikel in unserem Einkaufswagen // Fⁿge dem Einkaufswagen $num Artikel der Sorte $artnr zu function add_item ($artnr, $num) { $this->items[$artnr] += $num; } // Nimm $num Artikel von $artnr aus dem Einkaufswagen function remove_item ($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
In diesem Beispiel wird eine Klasse "Cart" definiert. Sie besteht aus einem assoziativen Array von Produkten im Einkaufswagen und zwei Funktionen zum Hinzufⁿgen und Entfernen von Artikeln. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Die folgenden warnenden Bemerkungen gelten fⁿr PHP 4. </P ><P >
Der Name <VAR CLASS="literal" >stdClass</VAR > ist reserviert, da er intern von Zend benutzt wird. Sie k÷nnen in PHP keine Klasse mit dem Namen <VAR CLASS="literal" >stdClass</VAR > haben. </P ><P >
Die Funktionsnamen <VAR CLASS="literal" >__sleep</VAR > und <VAR CLASS="literal" >__wakeup</VAR > sind in PHP Klassen "magisch". Sie k÷nnen in Ihren Klassen keine Funktionen mit diesen Namen haben, au▀er Sie wollen sie mit dieser "magischen" FunktionalitΣt assoziieren. Mehr Informationen dazu finden Sie weiter unten. </P ><P >
SΣmtliche mit __ beginnende Funktionsnamen sind PHP als "magisch" vorbehalten. Es wird empfohlen, in PHP keine Funktionsnamen mit __ zu verwenden, au▀er Sie m÷chten dokumentierte "magische" FunktionalitΣt. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In PHP 4 sind nur konstante Initialisierungen fⁿr <VAR CLASS="literal" >var</VAR > Variablen erlaubt. Um Variablen mit nicht konstanten Werten zu initialisieren, ben÷tigen Sie eine Funktion zur Initialisierung, welche beim Erstellen eines Objektes automatisch von der Klasse aufgerufen wird. Eine solche Funktion wird Konstruktor genannt (siehe unten). </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4972" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* Keine wird in PHP 4 funktionieren */ class Cart { var $todays_date = date("Y-m-d"); var $name = $firstname; var $owner = 'Fred ' . 'Jones'; var $items = array("VCR", "TV"); } /* So sollte es gemacht werden */ class Cart { var $todays_date; var $name; var $owner; var $items; function Cart() { $this->todays_date = date("Y-m-d"); $this->name = $GLOBALS['firstname']; /* etc. . . */ } } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE ></DIV ><P >
Klassen sind Typen, das hei▀t sie sind die Blaupausen fⁿr reale Variablen. Um sie zu nutzen, muss zunΣchst eine Variable mit dem Operator <VAR CLASS="literal" >new</VAR > angelegt werden. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4976" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $cart = new Cart; $cart->add_item("10", 1); $another_cart = new Cart; $another_cart->add_item("0815", 3);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Dies erstellt die Objekte $cart und $another_cart aus der Klasse Cart. Dann wird die Funktion add_item() des $cart Objektes aufgerufen, um $cart einen Artikel mit der Artikelnummer 10 hinzuzufⁿgen. 3 Artikel mit der Artikelnummer 0815 werden $another_cart hinzugefⁿgt. </P ><P >
Sowohl $cart als auch $another_cart haben die Funktionen add_item(), remove_item() und die Variable items. Dies sind verschiedene Funktionen und Variablen. Sie k÷nnen sich Objekte Σhnlich den Verzeichnissen in einem Dateisystem vorstellen. Sie k÷nnen in einem Dateisystem zwei verschiedene Dateien README.TXT haben, solange sie sich in verschiedenen Verzeichnissen befinden. So wie Sie in Verzeichnissen den vollen Pfadnamen eingeben mⁿssen, um jede Datei von dem obersten Verzeichnis aus zu erreichen, mⁿssen Sie auch den vollen Namen der aufzurufenden Funktion angeben: Das hei▀t fⁿr PHP, dass das Hauptverzeichnis der globale Namensbereich, und der Separator des Pfadnamens -> wΣre. Deshalb benennen die Namen $cart->items und $another_cart->items auch zwei verschiedene Variablen. Beachten Sie, dass die Variable $cart->items, und nicht $cart->$items genannt wird, da ein Variablenname in PHP nur ein einziges Dollarzeichen hat. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4980" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// korrekt, einfaches $ $cart->items = array("10" => 1); // falsch, denn $cart->$items wird zu $cart->"" $cart->$items = array("10" => 1); // richtig aber fraglich, ob dies erwⁿnscht war: // $cart->$myvar wird zu $cart->items $myvar = 'items'; $cart->$myvar = array("10" => 1);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Innerhalb einer Klassendefinition ist nicht bekannt, unter welchem Namen das Objekt in Ihrem Programm erreichbar sein wird: Als die Klasse Cart geschrieben wurde war nicht bekannt, dass das Objekt spΣter $cart oder $another_cart genannt wird. Deshalb k÷nnen Sie innerhalb der Klasse Cart selbst auch nicht $cart->items schreiben. Um nun die eigenen Funktionen und Variablen innerhalb einer Klasse anzusprechen, k÷nnen Sie die Pseudo-Variable $this verwenden, welche Sie auch als 'meine eigene' oder 'aktuelles Objekt' verstehen k÷nnen. Deshalb kann '$this->items[$artnr] += $num' auch als 'addiere $num zu $artnr in meinem eigenen Array items', oder 'addiere $num zu $artnr im Array items innerhalb des aktuellen Objektes' lesen. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Es gibt ein paar angenehme Funktionen, um mit Klassen und Objekten umzugehen. Mehr darⁿber erfahren Sie im Kapitel <A HREF="#ref.classobj" >Klassen- und Objekt-Funktionen</A >. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="keyword.extends" ><VAR CLASS="literal" >extends</VAR ></A ></H2 ><P >
Oft braucht man Klassen mit in einer anderen Klasse Σhnlichen Variablen und Funktionen. So ist es eine gute Vorgehensweise, eine in allen Ihren Projekten verwendbare Oberklasse zu definieren, und diese dann den Bedⁿrfnissen Ihrer einzelnen Projekte anzupassen. Um dies zu erleichtern, k÷nnen Klassen andere Klassen erweitern. Die erweiterte bzw. abgeleitete Klasse verfⁿgt ⁿber alle Variablen und Funktionen der Basisklasse (dies wird 'Vererbung' genannt, obwohl niemand gestorben ist), und was Sie in der erweiterten Definition hinzufⁿgen. Es ist nicht m÷glich, etwas von einer Klasse wegzunehmen, d.h. Sie k÷nnen keine existierenden Variablen oder Funktionen 'wegdefinieren'. Eine Unterklasse ist immer von einer einzigen Oberklasse abhΣngig, d.h. Mehrfachvererbung wird nicht unterstⁿtzt. Klassen werden mittels dem Schlⁿsselwort 'extends' erweitert. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4990" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >class Named_Cart extends Cart { var $owner; function set_owner ($name) { $this->owner = $name; } }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Hier wird die Klasse Named_Cart definiert, die ⁿber alle Variablen und Funktionen von Cart, plus der Variable $owner und der Funktion set_owner() verfⁿgt. Sie k÷nnen einen bestimmten Einkaufswagen (Named_Cart) auf dem ⁿblichen Weg erstellen, und nun auch den Besitzer (owner) bestimmen und erfragen. Sie k÷nnen noch immer die normalen Cart Funktionen an Named_Cart anwenden: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4993" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$ncart = new Named_Cart; // Erstellt einen bestimmten Einkaufwagen $ncart->set_owner("kris"); // den Besitzer festlegen print $ncart->owner; // den Besitzer ausgeben $ncart->add_item("10", 1); // (vererbte FunktionalitΣt von Cart)</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Dies wird auch eine "Eltern-Kind" Beziehung genannt. Sie erstellen eine Klasse ("Eltern"), und erstellen mittels <VAR CLASS="literal" >extends</VAR > eine neue Klasse, die auf der "Eltern"-Klasse <SPAN CLASS="emphasis" ><I CLASS="emphasis" >basiert</I ></SPAN >: die "Kind"-Klasse. Sie k÷nnen auch diese neue Unterklasse verwenden und eine neue, auf dieser Klasse basierenden Klasse erstellen. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Bevor Sie Klassen verwenden k÷nnen, mⁿssen Sie diese definieren. Wenn Sie wollen, dass <VAR CLASS="literal" >Named_Cart</VAR > die Klasse <VAR CLASS="literal" >Cart</VAR > erweitert, mⁿssen Sie <VAR CLASS="literal" >Cart</VAR > erst definieren. Wenn Sie eine andere Klasse, z.B. <VAR CLASS="literal" >Yellow_named_cart</VAR > erstellen wollen, welche auf <VAR CLASS="literal" >Named_Cart</VAR > basiert, mⁿssen Sie zuerst <VAR CLASS="literal" >Named_Cart</VAR > definieren. Kurz gesagt ist die Reihenfolge, in der die Klassen definiert werden, sehr wichtig. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop.constructor" ><VAR CLASS="literal" >Konstruktoren</VAR ></A ></H2 ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
In PHP 3 und PHP 4 verhalten sich die Konstruktoren unterschiedlich. Die PHP 4 Semantik wird dringend empfohlen. </P ></TD ></TR ></TABLE ></DIV ><P >
Konstruktoren sind Funktionen innerhalb einer Klasse, die automatisch aufgerufen wird, sobald Sie mittels <VAR CLASS="literal" >new</VAR > eine neue Instanz erstellen. In PHP 3 wird eine Funktion zum Konstruktor, wenn sie den gleichen Namen wie die Klasse hat. In PHP 4 wird eine Funktion zum Konstruktor, wenn sie den gleichen Namen wie die Klasse hat, in der sie definiert ist. Der Unterschied ist subtil, aber entscheidend (siehe unten). </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5013" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// Funktioniert sowohl in PHP 3, als auch in PHP 4. class Auto_Cart extends Cart { function Auto_Cart() { $this->add_item ("10", 1); } }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Die Klasse Auto_Cart entspricht der Klasse Cart und hat einen Konstruktor, der Cart bereits mit einem Artikel der Nummer "10" initialisiert, sobald ein neuer Auto_Cart mittels "new" erstellt wird. Konstruktoren k÷nnen Argumente ⁿbernehmen, die optional sein k÷nnen, was sie sehr praktisch macht. Um eine Klasse auch ohne Parameter verwenden zu k÷nnen, sollten alle Parameter fⁿr Konstruktoren optional sein, indem sie mit Standardwerten ausgestattet werden. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5016" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// Funktioniert sowohl in PHP 3, als auch in PHP 4. class Constructor_Cart extends Cart { function Constructor_Cart($item = "10", $num = 1) { $this->add_item ($item, $num); } } // Kaufe das gleiche alte Zeug ein. $default_cart = new Constructor_Cart; // Kaufe etwas anderes... $different_cart = new Constructor_Cart("20", 17);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Sie k÷nnen auch den Operator <VAR CLASS="literal" >@</VAR > verwenden, um Fehlermeldungen innerhalb des Konstruktors zu unterdrⁿcken, z.B. <VAR CLASS="literal" >@new</VAR >. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
In PHP 3 haben abgeleitete Klassen und Konstruktoren ein paar EinschrΣnkungen. Sie sollten das folgende Beispiel sorgfΣltig lesen, um diese EinschrΣnkungen zu verstehen. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5023" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >class A { function A() { echo "I am the constructor of A.<br>\n"; } } class B extends A { function C() { echo "I am a regular function.<br>\n"; } } // In PHP 3 wurde kein Konstruktor aufgerufen. $b = new B;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
In PHP 3 wird bei dem obigen Beispiel kein Konstruktor aufgerufen. Die Regel in PHP 3 besagt: 'Ein Konstruktor ist eine Funktion mit dem selben Namen wie die Klasse'. Der Name der Klasse ist B, und nachdem in Klasse B keine Funktion namens B() existiert, passiert nichts. </P ><P >
Dies ist in PHP 4 mit der Einfⁿhrung einer anderen Regel behoben: Wenn eine Klasse keinen Konstruktor hat, wird der Konstruktor der Basisklasse aufgerufen, so dieser existiert. Das obige Beispiel hΣtte in PHP 4 also 'I am the constructor of A.<br>' ausgegeben. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5027" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >class A { function A() { echo "I am the constructor of A.<br>\n"; } function B() { echo "I am a regular function named B in class A.<br>\n"; echo "I am not a constructor in A.<br>\n"; } } class B extends A { function C() { echo "I am a regular function.<br>\n"; } } // Dies ruft B() als Konstruktor auf. $b = new B;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
In PHP 3 wird die Funktion B() in der Klasse A pl÷tzlich zum Konstruktor in Klasse B, auch wenn dies nie beabsichtigt war. Die Regel in PHP 3 lautet: 'Ein Konstruktor ist eine Funktion mit dem gleichen Namen wie die Klasse'. PHP 3 kⁿmmert sich nicht darum, ob die Funktion in Klasse B definiert, oder ob sie nur vererbt wurde. </P ><P >
Dies ist in PHP 4 mit einer modifizierten Regel behoben: 'Ein Konstruktor ist eine Funktion mit dem selben Namen wie die Klasse, in der sie definiert wurde'. So hΣtte die Klasse B in dem obigen Beispiel keinen eigenen Konstruktor, und wⁿrde der Konstruktor der Basisklasse aufgerufen, welcher 'I am the constructor of A.<br>' ausgeben wⁿrde. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Weder PHP 3, noch PHP 4 rufen vom Konstruktor der abgeleiteten Klasse aus automatisch Konstruktoren der Basisklasse auf. Wenn angebracht, ist der Aufruf von Konstruktoren aufwΣrts Ihre Aufgabe. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In PHP 3 und PHP 4 gibt es keine Destruktoren. Stattdessen k÷nnen Sie <A HREF="#function.register-shutdown-function" ><B CLASS="function" >register_shutdown_function()</B ></A > verwenden, um die meisten Effekte von Destruktoren zu simulieren. </P ></BLOCKQUOTE ></DIV ><P >
Destruktoren sind Funktionen die automatisch aufgerufen werden, wenn ein Objekt entweder mittels <A HREF="#function.unset" ><B CLASS="function" >unset()</B ></A >, oder durch Verlassen des Gⁿltigkeitsbereiches zerst÷rt wird. Es gibt in PHP keine Destruktoren. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="keyword.paamayim-nekudotayim" ><VAR CLASS="literal" >::</VAR ></A ></H2 ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Die folgende Beschreibung ist nur fⁿr PHP 4 gⁿltig. </P ></TD ></TR ></TABLE ></DIV ><P >
Manchmal ist es hilfreich, auf Funktionen in Basisklassen bzw. auf Funktionen in Klassen zuzugreifen, die noch keine Instanzen haben. Dafⁿr wird der :: Operator verwendet. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5044" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >class A { function example() { echo "I am the original function A::example().<br>\n"; } } class B extends A { function example() { echo "I am the redefined function B::example().<br>\n"; A::example(); } } // Es gibt kein Objekt der Klasse A. // Dies wird folgendes ausgeben // I am the original function A::example().<br> A::example(); // Erstellt ein Objekt der Klasse B. $b = new B; // Dies wird folgendes ausgeben // I am the redefined function B::example().<br> // I am the original function A::example().<br> $b->example();</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Das obige Beispiel ruft die Funktion example() der Klasse A auf. Nachdem noch kein Objekt der Klasse A existiert, k÷nnen wir nicht $a->example() oder Σhnliches schreiben. Stattdessen rufen wir example() als 'Klassenfunktion' auf, d.h. als Funktion der Klasse selbst, und nicht irgendein Objekt dieser Klasse. </P ><P >
Es gibt Klassenfunktionen, aber keine Klassenvariablen. TatsΣchlich gibt es zur Zeit des Aufrufs kein Objekt. Deshalb darf eine Klassenfunktion keine Objektvariablen benutzen (aber sie kann lokale und globale Variablen verwenden), und sie darf $this ebenfalls nicht benutzen. </P ><P >
In dem obigen Beispiel definiert Klasse B die Funktion example() neu. Die ursprⁿngliche Definition in Klasse A ist ⁿberschattet und nicht lΣnger verfⁿgbar, au▀er Sie verweisen mittels des ::-Operators speziell auf example() in Klasse A. Schreiben Sie A::example(), um dies zu tun (TatsΣchlich sollten Sie parent::example() schreiben, wie im nΣchsten Abschnitt beschrieben). </P ><P >
In diesem Kontext besteht ein Objekt, das Objektvariablen haben kann. Deshalb k÷nnen Sie auch $this und Objektvariablen verwenden, wenn sie von innerhalb einer Objektfunktion verwendet werden. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="keyword.parent" ><VAR CLASS="literal" >parent</VAR ></A ></H2 ><P >
Wahrscheinlich wollen Sie auch Code schreiben, der sich auch auf Variablen und Funktionen in Basisklassen bezieht. Dies gilt speziell dann, wenn Ihre abgeleitete Klasse eine Verfeinerung oder Spezialisierung von Code in Ihrer Basisklasse ist. </P ><P >
Anstatt in Ihrem Code den w÷rtlichen Namen der Basisklasse zu verwenden, sollten Sie den speziellen Namen <VAR CLASS="literal" >parent</VAR > verwenden, welcher sich auf den in der Deklaration Ihrer Klasse mittels <VAR CLASS="literal" >extends</VAR > gegebenen Namen Ihrer Basisklasse bezieht. So vermeiden Sie die mehrfache Verwendung des Namens der Basisklasse. Sollte sich Ihr Vererbungsbaum wΣhrend der Implementation Σndern, brauchen Sie nur mehr die <VAR CLASS="literal" >extends</VAR > Deklaration Ihrer Klasse zu Σndern. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5058" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >class A { function example() { echo "I am A::example() and provide basic functionality.<br>\n"; } } class B extends A { function example() { echo "I am B::example() and provide additional functionality.<br>\n"; parent::example(); } } $b = new B; // Dies ruft B::example() auf, welches wiederum A::example() aufruft. $b->example();</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop.serialization" >Objekte serialisieren - Objekte in Sessions</A ></H2 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In PHP 3 verlieren Objekte wΣhrend des Prozesses der Serialisierung und Deserialisierung ihre Assoziation zur Klasse. Die resultierende Variable ist vom Typ Objekt, hat aber keine Klasse und keine Methoden, weshalb sie ziemlich unbrauchbar ist (Sie wurde wie ein Array, jedoch mit einer ziemlich komischen Syntax). </P ></BLOCKQUOTE ></DIV ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Die folgende Information ist nur fⁿr PHP 4 gⁿltig. </P ></TD ></TR ></TABLE ></DIV ><P >
<A HREF="#function.serialize" ><B CLASS="function" >serialize()</B ></A > gibt eine Zeichenkette zurⁿck, die eine Byte-Strom-ReprΣsentation irgendeines in PHP speicherbaren Wertes enthΣlt. <A HREF="#function.unserialize" ><B CLASS="function" >unserialize()</B ></A > kann diese Zeichenkette verwenden, um die ursprⁿnglichen Variablenwerte wieder herzustellen. Die Verwendung von serialize zum Speichern eines Objektes wird alle Variablen innerhalb eines Objektes speichern. Die Funktionen in einem Objekt werden nicht gespeichert, sondern nur der Name der Klasse. </P ><P >
Um ein Objekt wieder deserialisieren zu k÷nnen, muss die Klasse dieses Objektes definiert werden. Das hei▀t, wenn Sie ein Objekt $a der Klasse A in page1.php haben und dieses serialisieren, erhalten Sie eine Zeichenkette, die sich auf die Klasse A bezieht, und alle Werte der in $a enthaltenen Variablen enthΣlt. Wenn Sie $a der Klasse A in page2.php mittels unserialize wiederherstellen m÷chten, muss die Definition von Klasse A in page2.php vorhanden sein. Dies kann zum Beispiel durch das Speichern der Klassendefiniton von Klasse A in einer Include-Datei, und das Einbinden dieser Datei sowohl in page1.php und page2.php realisiert werden. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5070" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >classa.inc: class A { var $one = 1; function show_one() { echo $this->one; } } page1.php: include("classa.inc"); $a = new A; $s = serialize($a); // speichere $s irgendwo, wo sie page2.php finden kann. $fp = fopen("store", "w"); fputs($fp, $s); fclose($fp); page2.php: // Dies ist fⁿr das korrekte Arbeiten von unserialize n÷tig. include("classa.inc"); $s = implode("", @file("store")); $a = unserialize($s); // Nun verwenden wir die Funktion show_one() des Objektes $a. $a->show_one();</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
Wenn Sie mit Sessions arbeiten und <A HREF="#function.session-register" ><B CLASS="function" >session_register()</B ></A > verwenden, um Objekte zu registrieren, so werden diese Objekte am Ende der PHP Seite serialisiert, und in jeder folgenden Seite automatisch via unserialize wiederhergestellt. Das hei▀t, dass diese Objekte auf jeder Ihrer Seite auftauchen k÷nnen, sobald sie Teil Ihrer Session sind. </P ><P >
Es wird ausdrⁿcklich empfohlen, dass Sie die Klassendefinitionen der so registrierten Objekte in allen Ihren Seiten einbinden, auch wenn Sie diese Klassen eigentlich nicht auf allen Ihren Seiten ben÷tigen. Tun Sie es nicht und wird ein Objekt ohne einer vorhandenen Klassendefinition deserialisiert, verliert es seine Assoziation zur Klasse, und wird zu einem Objekt der Klasse <VAR CLASS="literal" >stdClass</VAR >, ohne irgendwelchen Funktionen. Das bedeutet, dass es ziemlich nutzlos wird. </P ><P >
Wⁿrde also $a in dem obigen Beispiel mittels <VAR CLASS="literal" >session_register("a")</VAR > ein Teil einer Session, sollten Sie die Datei <VAR CLASS="literal" >classa.inc</VAR > nicht nur in page1.php und page2.php, sondern in all Ihre Seiten einbinden. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop.magic-functions" >Die "magischen" Funktionen <VAR CLASS="literal" >__sleep</VAR > und <VAR CLASS="literal" >__wakeup</VAR ></A ></H2 ><P >
<A HREF="#function.serialize" ><B CLASS="function" >serialize()</B ></A > prⁿft, ob Ihre Klasse eine Funktion mit dem magischen Namen <VAR CLASS="literal" >__sleep</VAR > enthΣlt. Wenn dem so ist, wird diese Funktion vor einer Serialisierung ausgefⁿhrt. Sie kann das Objekt aufrΣumen und soll ein Array mit den Namen aller Variablen des zu serialisierenden Objektes zurⁿckgeben. </P ><P >
Die beabsichtigte Verwendung von <VAR CLASS="literal" >__sleep</VAR > ist, eventuelle Datenbankverbindungen des Objektes zu schlie▀en, noch offene Daten zu committen, oder Σhnliche SΣuberungsaktionen durchzufⁿhren. Weiters ist diese Funktion hilfreich, wenn Sie sehr gro▀e Objekte haben, die nicht komplett gespeichert werden mⁿssen. </P ><P >
Umgekehrt prⁿft <A HREF="#function.unserialize" ><B CLASS="function" >unserialize()</B ></A > die Existenz einer Funktion mit dem magischen Namen <VAR CLASS="literal" >__wakeup</VAR >. Ist diese vorhanden, kann sie Ressourcen des Objektes rekonstruieren. </P ><P >
Die vorgesehene Verwendung von <VAR CLASS="literal" >__wakeup</VAR > ist die Wiederherstellung von Datenbankverbindungen, welche wΣhrend der Serialisierung verloren gegangen sein k÷nnten, sowie andere Neuinitialisierungen. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop.newref" >Referenzen innherhalb des Konstruktors</A ></H2 ><P >
Referenzen innerhalb des Konstruktors k÷nnen zu verwirrenden Resultaten fⁿhren. Dieser Abschnitt hilft, Probleme zu vermeiden. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5096" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >class Foo { function Foo($name) { // eine Referenz innerhalb des globalen Arrays $globalref erstellen global $globalref; $globalref[] = &$this; // setze den Namen auf den ⁿbergebenen Wert $this->setName($name); // und gib' ihn aus $this->echoName(); } function echoName() { echo "<br>",$this->name; } function setName($name) { $this->name = $name; } }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Prⁿfen wir, ob zwischen <VAR CLASS="varname" >$bar1</VAR >, die mittels dem Operator zum Kopieren <VAR CLASS="literal" >=</VAR > erstellt wurde, und <VAR CLASS="varname" >$bar2</VAR >, die mittels dem Referenzoperator <VAR CLASS="literal" >=&</VAR > erstellt wurde, besteht... <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5103" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$bar1 = new Foo('set in constructor'); $bar1->echoName(); $globalref[0]->echoName(); /* Ausgabe: set in constructor set in constructor set in constructor */ $bar2 =& new Foo('set in constructor'); $bar2->echoName(); $globalref[1]->echoName(); /* Ausgabe: set in constructor set in constructor set in constructor */</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Scheinbar besteht kein Unterschied, aber tatsΣchlich existiert ein signifikanter: <VAR CLASS="varname" >$bar1</VAR > und <VAR CLASS="varname" >$globalref[0]</VAR > sind NICHT referenziert, d.h. sie sind NICHT die selbe Variable. Das kommt daher, dass "new" nicht automatisch eine Referenz, sondern eine Kopie zurⁿckgibt. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Das zurⁿckgeben von Kopien anstatt von Referenzen stellt keinen Performanceverlust dar (da PHP 4 und h÷her Reference Counting verwendet). Andererseits ist es sehr oft besser, einfach mit Kopien anstatt mit Referenzen zu arbeiten, da die Erstellung von Referenzen etwas Zeit in Anspruch nimmt, wΣhrend das Erstellen von Kopien so gut wie keine Zeit braucht (sofern keine von ihnen ein gro▀es Array oder Objekt ist, und eines davon geΣndert wird und das/die andere/n nachtrΣglich. In diesem Fall wΣre es besser, Referenzen zu verwenden, um sie alle gleichzeitig zu Σndern). </P ></BLOCKQUOTE ></DIV > Um das zuvor geschriebene zu beweisen, sehen wir uns den folgenden Code an. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5110" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// nun werden wir den Namen Σndern. Was erwarten Sie? // ...dass sowohl $bar1, als auch $globalref[0] ihre Namen Σndern... $bar1->setName('set from outside'); // wie bereits zuvor erwΣhnt, ist dies nicht der Fall $bar1->echoName(); $globalref[0]->echoName(); /* Ausgabe: set from outside set in constructor */ // lassen Sie uns den Unterschied zwischen $bar2 and $globalref[1] ansehen $bar2->setName('set from outside'); // glⁿcklicherweise sind sie nicht nur nicht gleich, sondern auch die selbe // Variable; demnach sind $bar2->name und $globalref[1]->name ebenfalls gleich $bar2->echoName(); $globalref[1]->echoName(); /* Ausgabe: set from outside set from outside */</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Ein anderes, letztes Beispiel zum VerstΣndnis: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5113" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >class A { function A($i) { $this->value = $i; // finden Sie heraus, warum wir hier keine Referenz ben÷tigen $this->b = new B($this); } function createRef() { $this->c = new B($this); } function echoValue() { echo "<br>","class ",get_class($this),': ',$this->value; } } class B { function B(&$a) { $this->a = &$a; } function echoValue() { echo "<br>","class ",get_class($this),': ',$this->a->value; } } // ⁿberlegen Sie, warum hier die Verwendung einer einfachen Kopie in der // mit * markierten Zeile zu einem unerwⁿnschten Ergebnis fⁿhren wⁿrde $a =& new A(10); $a->createRef(); $a->echoValue(); $a->b->echoValue(); $a->c->echoValue(); $a->value = 11; $a->echoValue(); $a->b->echoValue(); // * $a->c->echoValue(); /* Ausgabe: class A: 10 class B: 10 class B: 10 class A: 11 class B: 11 class B: 11 */</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.oop5" >Kapitel 18. Classes and Objects (PHP 5)</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="op5.intro" >Introduction</A ></H2 ><P >
Intro to oop5 for php </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.decon" >Constructors and Destructors</A ></H2 ><DIV CLASS="sect2" ><H3 CLASS="sect2" ><A NAME="oop5-decon-constructor" >Constructor</A ></H3 >void <B CLASS="methodname" >__construct</B > ( [mixed args [, ...]])<BR ></BR ><P >
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Parent constructors are not called implicitly. In order to run a parent constructor, a call to <B CLASS="function" >parent::__construct()</B > is required. </P ></BLOCKQUOTE ></DIV ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5136" ></A ><P ><B >Beispiel 18-1. using new unified constructors</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class BaseClass { function __construct() { print "In BaseClass constructor\n"; } } class SubClass extends BaseClass { function __construct() { parent::__construct(); print "In SubClass constructor\n"; } } $obj = new BaseClass(); $obj = new SubClass(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
For backwards compatibility, if PHP 5 cannot find a <B CLASS="function" >__construct()</B > function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named <B CLASS="function" >__construct()</B > which was used for different semantics. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="oop5-decon-destructor" >Destructor</A ></H3 >void <B CLASS="methodname" >__destruct</B > ( void )<BR ></BR ><P >
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as Java. The destructor method will be called as soon as all references to a paticular object are removed or when the object is explicitly destroyed. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5149" ></A ><P ><B >Beispiel 18-2. Destructor Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class MyDestructableClass { function __construct() { print "In constructor\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "Destroying " . $this->name . "\n"; } } $obj = new MyDestructableClass(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call <B CLASS="function" >parent::__destruct()</B > in the destructor body. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.visibility" >Visibility</A ></H2 ><P >
The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be acessed everywhere. Protected limits access to inherited classes (and to the class that defines the item). Private limits visiblity only to the class that defines the item. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.visiblity-members" >Members Visibility</A ></H3 ><P >
Class members must be defined with public, private, or protected. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5160" ></A ><P ><B >Beispiel 18-3. Member declaration</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class MyClass { public $public = "MyClass::public!\n"; protected $protected = "MyClass::Protected!\n"; protected $protected2 = "MyClass::Protected2!\n"; private $private = "MyClass::private!\n"; function printHello() { print "MyClass::printHello() " . $this->private; print "MyClass::printHello() " . $this->protected; print "MyClass::printHello() " . $this->protected2; } } class MyClass2 extends MyClass { protected $protected = "MyClass2::protected!\n"; function printHello() { MyClass::printHello(); print "MyClass2::printHello() " . $this->public; print "MyClass2::printHello() " . $this->protected; print "MyClass2::printHello() " . $this->protected2; /* Will result in a Fatal Error: */ //print "MyClass2::printHello() " . $this->private; /* Fatal Error */ } } $obj = new MyClass(); print "Main:: " . $obj->public; //print $obj->private; /* Fatal Error */ //print $obj->protected; /* Fatal Error */ //print $obj->protected2; /* Fatal Error */ $obj->printHello(); /* Should print */ $obj2 = new MyClass2(); print "Main:: " . $obj2->private; /* Undefined */ //print $obj2->protected; /* Fatal Error */ //print $obj2->protected2; /* Fatal Error */ $obj2->printHello(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > The use PHP 4 use of declaring a variable with the keyword 'var' is no longer valid for PHP 5 objects. For compatiblity a variable declared in php will be assumed with public visiblity, and a <TT CLASS="constant" ><B >E_STRICT</B ></TT > warning will be issued. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.visiblity-methods" >Method Visibility</A ></H3 ><P >
. </P ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.paamayim-nekudotayim" >::</A ></H2 ><P >
The Paamayim Nekudotayim, or in simpler terms, the double colon. This token provides a way to access <A HREF="#language.oop5.static" >static</A >, <A HREF="#language.oop5.constants" >constant</A > or overridden members or methods of a class. </P ><P >
When referencing these items from outside the class definition, you use name of the class. </P ><P >
Paamayim Nekudotayim would, at first, seem a strange choice for a double-colon. However, at the time of writing of Zend Engine 0.5 (which powered PHP3), that is what Andi and Zeev decided to call it. It actually does mean double-colon - in Hebrew! As PHP has progressed with its development it has just never changed. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5176" ></A ><P ><B >Beispiel 18-4. :: from outside class definition</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class MyClass { const CONST_VALUE = 'A constant value'; } echo MyClass::CONST_VALUE; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Two special keywords <VAR CLASS="varname" >self</VAR > and <VAR CLASS="varname" >parent</VAR > are used to access members or methods from inside the class definition. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5182" ></A ><P ><B >Beispiel 18-5. :: from inside the class definition</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class OtherClass extends MyClass { public static $my_static = 'static var'; public static function doubleColon() { echo parent::CONST_VALUE . "\n"; echo self::$my_static . "\n"; } } OtherClass::doubleColon(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
When an extending class overrides the parents definition of a method, php will not call the parent's method. It is up to the extending class to call the parent method or not, this also applies to <A HREF="#language.oop5.decon" >Constructors and Destructors</A >, <A HREF="#language.oop5.overloading" >Overloading</A > and <A HREF="#language.oop5.magic" >Magic</A > method defintions as well. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5189" ></A ><P ><B >Beispiel 18-6. Calling a parent's method</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class MyClass { protected function myFunc() { echo "MyClass::myFunc()\n"; } } class OtherClass extends MyClass { /* Override parent's definition */ public function myFunc() { /* But still call the parent function */ parent::myFunc(); echo "OtherClass::myFunc()\n"; } } $class = new OtherClass(); $class->myFunc(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.static" >Static Keyword</A ></H2 ><P >
Declaring class members or methods as static, makes them callable from outside the object context. A member or method declared with static can not be accessed with a variable that is an instance of the object and cannot be re-defined in an extending class. </P ><P >
The static declaration must be after the visibilty declaration. For compatibility with PHP 4, if no <A HREF="#language.oop5.visibility" >visibility</A > declaration is used, then the member or method will be treated as if it was declared as <VAR CLASS="literal" >public static</VAR >. </P ><P >
Because static methods are callable without an instance of the object created, the pseudo variable <VAR CLASS="varname" >$this</VAR > is not available inside the method declared as static. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5200" ></A ><P ><B >Beispiel 18-7. Static member example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Foo { public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } class Bar extends Foo { public function fooStatic() { return parent::$my_static; } } print Foo::$my_static . "\n"; $foo = new Foo(); print $foo->staticValue() . "\n"; print $foo->my_static . "\n"; // Undefined my_static print Bar::$my_static . "\n"; $bar = new Bar(); print $bar->fooStatic() . "\n"; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5203" ></A ><P ><B >Beispiel 18-8. Static method example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Foo { public static function aStaticMethod() { // ... } } Foo::aStaticMethod(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.constants" >Object Constants</A ></H2 ><P >
It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don't use the <VAR CLASS="varname" >$</VAR > symbol to declare or use them. Like <A HREF="#language.oop5.static" >static</A > members, constant values can not be accessed from an instance of the object. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5211" ></A ><P ><B >Beispiel 18-9. Defining and using a constant</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class MyClass { const constant = 'constant value'; function showConstant() { echo self::constant . "\n"; } } echo MyClass::constant . "\n"; $class = new MyClass(); $class->showConstant(); /* echo $class::constant; is not allowed */ ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.abstract" >Object Abstraction</A ></H2 ><P >
PHP 5 introduces abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation. </P ><P >
The class that implements the abstract method must define with the same <A HREF="#language.oop5.visibility" >visibillity</A > or weaker. If the abstract method is defined as protected, the function implenetation must be defined as either protected or public. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5219" ></A ><P ><B >Beispiel 18-10. Abstract class example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php abstract class AbstractClass { /* Force Extending class to define this method */ abstract protected function getValue(); /* Common method */ public function print() { print $this->getValue(); } } class ConcreteClass1 extends AbstractClass { protected function getValue() { return "ConcreteClass1"; } } class ConcreteClass2 extends AbstractClass { protected function getValue() { return "ConcreteClass2"; } } $class1 = new ConcreteClass1; $class1->print(); $class2 = new ConcreteClass2; $class2->print(); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Old code that has no user-defined classes or functions named 'abstract' should run without modifications. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.interfaces" >Object Interfaces</A ></H2 ><P >
Object interfaces allow you to create code which specifies which methods and variables a class must implement, without having to define how these methods are handled. </P ><P >
Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined. Classes which implement an interface should do so using the implements keyword, and must have definitions for all the methods listed in the interface. Classes may implement more than one interface if desired by listing each interface split by a space. </P ><P >
Stating that a class implements an interface, and then not implementing all the methods in the interface will result in a fatal error telling you which methods have not been implemented. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5228" ></A ><P ><B >Beispiel 18-11. Interface example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php interface ITemplate { public function setVariable($name, $var); public function getHtml($template); } class Template implements ITemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{'.$name.'}', $value, $template); } return $template; } } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.overloading" >Overloading</A ></H2 ><P >
Both method calls and member accesses can be overloaded via the __call, __get and __set methods. These methods will only be triggered when your object or inherited object doesn't contain the member or method you're trying to access. </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.overloading.members" >Member overloading</A ></H3 >void <B CLASS="methodname" >__set</B > ( string name, mixed value)<BR ></BR >void <B CLASS="methodname" >__get</B > ( mixed name)<BR ></BR ><P >
Class members can be overloaded to run custom code defined in your class by defining these specially named methods. The <VAR CLASS="varname" >$name</VAR > parameter used is the name of the variable that should be set or retrieved. The __set() method's <VAR CLASS="varname" >$value</VAR > parameter specifies the value that the object should set set the <VAR CLASS="varname" >$name</VAR >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5255" ></A ><P ><B >Beispiel 18-12. overloading with __get and __set example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Setter { public $n; private $x = array("a" => 1, "b" => 2, "c" => 3); function __get($nm) { print "Getting [$nm]\n"; if (isset($this->x[$nm])) { $r = $this->x[$nm]; print "Returning: $r\n"; return $r; } else { print "Nothing!\n"; } } function __set($nm, $val) { print "Setting [$nm] to $val\n"; if (isset($this->x[$nm])) { $this->x[$nm] = $val; print "OK!\n"; } else { print "Not OK!\n"; } } } $foo = new Setter(); $foo->n = 1; $foo->a = 100; $foo->a++; $foo->z++; var_dump($foo); ?></PRE ></TD ></TR ></TABLE ><P >
Will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Setting [a] to 100 OK! Getting [a] Returning: 100 Setting [a] to 101 OK! Getting [z] Nothing! Setting [z] to 1 Not OK! object(Setter)#1 (2) { ["n"]=> int(1) ["x:private"]=> array(3) { ["a"]=> int(101) ["b"]=> int(2) ["c"]=> int(3) } }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.overloading.methods" >Method overloading</A ></H3 >mixed <B CLASS="methodname" >__call</B > ( string name, array arguments)<BR ></BR ><P >
Class methods can be overloaded to run custom code defined in your class by defining this specially named method. The <VAR CLASS="varname" >$name</VAR > parameter used is the name as the function name that was requested to be used. The arguments that were passed in the function will be defined as an array in the <VAR CLASS="varname" >$arguments</VAR > parameter. The value returned from the __call() method will be returned to the caller of the method. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5274" ></A ><P ><B >Beispiel 18-13. overloading with __call example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Caller { private $x = array(1, 2, 3); function __call($m, $a) { print "Method $m called:\n"; var_dump($a); return $this->x; } } $foo = new Caller(); $a = $foo->test(1, "2", 3.4, true); var_dump($a); ?></PRE ></TD ></TR ></TABLE ><P >
Will Output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Method test called: array(4) { [0]=> int(1) [1]=> string(1) "2" [2]=> float(3.4) [3]=> bool(true) } array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.iterations" >Object Iteration</A ></H2 ><P >
PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a <A HREF="#control-structures.foreach" >Abschnitt namens <I ><VAR CLASS="literal" >foreach</VAR ></I > in Kapitel 15</A > statement. By default, all public properties will be used for the iteration. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5283" ></A ><P ><B >Beispiel 18-14. Simple Object Iteration</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class MyClass { public $var1 = 'value 1'; public $var2 = 'value 2'; public $var3 = 'value 3'; protected $protected = 'protected'; private $private = 'private'; } $class = new MyClass(); foreach($class as $key => $value) { print "$key => $value\n"; }</PRE ></TD ></TR ></TABLE ><P >
Will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >var1 => value 1 var2 => value 2 var3 => value 3</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
As the output shows, the <A HREF="#control-structures.foreach" >Abschnitt namens <I ><VAR CLASS="literal" >foreach</VAR ></I > in Kapitel 15</A > iterated through each public variable that is defined. To take it a step further you can <VAR CLASS="varname" >implement</VAR > one of PHP 5's internal <A HREF="#language.oop5.interfaces" >Abschnitt namens <I >Object Interfaces</I ></A > named <VAR CLASS="varname" >Iterator</VAR >. This allows the object to decide what and how the object will be iterated. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5293" ></A ><P ><B >Beispiel 18-15. Object Iteration implenting Iterator</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class MyIterator implements Iterator { private $var = array(); public function __construct($array) { if (is_array($array) ) { $this->var = $array; } } public function rewind() { echo "rewinding\n"; reset($this->var); } public function current() { $var = current($this->var); echo "current: $var\n"; return $var; } public function key() { $var = key($this->var); echo "key: $var\n"; return $var; } public function next() { $var = next($this->var); echo "next: $var\n"; return $var; } public function valid() { $var = $this->current() !== false; echo "valid: {$var}\n"; return $var; } } $values = array(1,2,3); $it = new MyIterator($values); foreach ($it as $a => $b) { print "$a: $b\n"; }</PRE ></TD ></TR ></TABLE ><P >
Will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >rewinding current: 1 valid: 1 current: 1 key: 0 0: 1 next: 2 current: 2 valid: 1 current: 2 key: 1 1: 2 next: 3 current: 3 valid: 1 current: 3 key: 2 2: 3 next: current: valid:</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
You can also define your class so that it doesn't have to define all the <VAR CLASS="varname" >Iterator</VAR > functions by simply implementing the PHP 5 <VAR CLASS="varname" >IteratorAggregate</VAR > interface. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5301" ></A ><P ><B >Beispiel 18-16. Object Iteration implenting IteratorAggregate</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class MyCollection implements IteratorAggregate { private $items = array(); private $count = 0; /* Required definition of interface IteratorAggregate */ public function getIterator() { return new MyIterator($this->items); } public function add($value) { $this->items[$this->count++] = $value; } } $coll = new MyCollection(); $coll->add('value 1'); $coll->add('value 2'); $coll->add('value 3'); foreach ($coll as $key => $val) { echo "key/value: [$key -> $val]\n\n"; } ?> </pre></PRE ></TD ></TR ></TABLE ><P >
Will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >rewinding current: value 1 valid: 1 current: value 1 key: 0 key/value: [0 -> value 1] next: value 2 current: value 2 valid: 1 current: value 2 key: 1 key/value: [1 -> value 2] next: value 3 current: value 3 valid: 1 current: value 3 key: 2 key/value: [2 -> value 3] next: current: valid:</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.magic" >Magic Methods</A ></H2 ><P >
. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.final" >Final Keyword</A ></H2 ><P >
PHP 5 introduces the final keyword, which prevents child classes from overriding a method or variable by prefixing the definition with final. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5312" ></A ><P ><B >Beispiel 18-17. Final methods example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class BaseClass { public function test() { echo "BaseClass::test() called\n"; } final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; } } class ChildClass extends BaseClass { public function moreTesting() { echo "ChildClass::moreTesting() called\n"; } } // Results in Fatal error: Cannot override final method BaseClass::moreTesting() ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.cloning" >Object cloning</A ></H2 ><P >
Creating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to another object which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy. </P ><P >
An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5319" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >$copy_of_object = clone $object;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references. If a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5322" ></A ><P ><B >Beispiel 18-18. Cloning an object</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class SubObject { static $instances = 0; public $instance; public function __construct() { $this->instance = ++self::$instances; } public function __clone() { $this->instance = ++self::$instances; } } class MyCloneable { public $object1; public $object2; function __clone() { // Force a copy of this->object, otherwise // it will point to same object. $this->object1 = clone($this->object1); } } $obj = new MyCloneable(); $obj->object1 = new SubObject(); $obj->object2 = new SubObject(); $obj2 = clone $obj; print("Original Object:\n"); print_r($obj); print("Cloned Object:\n"); print_r($obj2); ?></PRE ></TD ></TR ></TABLE ><P >The above example will output:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Original Object: MyCloneable Object ( [object1] => SubObject Object ( [instance] => 1 ) [object2] => SubObject Object ( [instance] => 2 ) ) Cloned Object: MyCloneable Object ( [object1] => SubObject Object ( [instance] => 3 ) [object2] => SubObject Object ( [instance] => 2 ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.object-comparison" >Comparing objects</A ></H2 ><P >
In PHP 5, object comparison is more complicated than in PHP 4 and more in accordance to what one will expect from an Object Oriented Language (not that PHP 5 is such a language). </P ><P >
When using the comparison operator (<VAR CLASS="literal" >==</VAR >), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class. </P ><P >
On the other hand, when using the identity operator (<VAR CLASS="literal" >===</VAR >), object variables are identical if and only if they refer to the same instance of the same class. </P ><P >
An example will clarify these rules. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5335" ></A ><P ><B >Beispiel 18-19. Example of object comparison in PHP 5</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function bool2str($bool) { if ($bool === false) { return 'FALSE'; } else { return 'TRUE'; } } function compareObjects(&$o1, &$o2) { echo 'o1 == o2 : '.bool2str($o1 == $o2)."\n"; echo 'o1 != o2 : '.bool2str($o1 != $o2)."\n"; echo 'o1 === o2 : '.bool2str($o1 === $o2)."\n"; echo 'o1 !== o2 : '.bool2str($o1 !== $o2)."\n"; } class Flag { var $flag; function Flag($flag=true) { $this->flag = $flag; } } class OtherFlag { var $flag; function OtherFlag($flag=true) { $this->flag = $flag; } } $o = new Flag(); $p = new Flag(); $q = $o; $r = new OtherFlag(); echo "Two instances of the same class\n"; compareObjects($o, $p); echo "\nTwo references to the same instance\n"; compareObjects($o, $q); echo "\nInstances of two different classes\n"; compareObjects($o, $r); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > This example will output: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Two instances of the same class o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : FALSE o1 !== o2 : TRUE Two references to the same instance o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : TRUE o1 !== o2 : FALSE Instances of two different classes o1 == o2 : FALSE o1 != o2 : TRUE o1 === o2 : FALSE o1 !== o2 : TRUE</PRE ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.oop5.reflection" >Reflection</A ></H2 ><DIV CLASS="sect2" ><H3 CLASS="sect2" ><A NAME="language.oop5.reflection.introduction" >Introduction</A ></H3 ><P >
PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions and methods as well as extensions. Additionally, the reflection API also offers ways of retrieving doc comments for functions, classes and methods. </P ><P >
The reflection API is an object-oriented extension to the Zend Engine, consisting of the following classes: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5345" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Reflection { } interface Reflector { } class ReflectionException extends Exception { } class ReflectionFunction implements Reflector { } class ReflectionParameter implements Reflector { } class ReflectionMethod extends ReflectionFunction { } class ReflectionClass implements Reflector { } class ReflectionObject extends ReflectionClass { } class ReflectionProperty implements Reflector { } class ReflectionExtension implements Reflector { } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > For details on these classes, have a look at the next chapters. </P ></BLOCKQUOTE ></DIV ><P >
If we were to execute the code in the example below: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5350" ></A ><P ><B >Beispiel 18-20. Basic usage of the reflection API</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php Reflection::export(new ReflectionClass('Exception')); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > We will see: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Class [ <internal> class Exception ] { - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [6] { Property [ <default> protected $message ] Property [ <default> private $string ] Property [ <default> protected $code ] Property [ <default> protected $file ] Property [ <default> protected $line ] Property [ <default> private $trace ] } - Methods [9] { Method [ <internal> final private method __clone ] { } Method [ <internal> <ctor> method __construct ] { } Method [ <internal> final public method getMessage ] { } Method [ <internal> final public method getCode ] { } Method [ <internal> final public method getFile ] { } Method [ <internal> final public method getLine ] { } Method [ <internal> final public method getTrace ] { } Method [ <internal> final public method getTraceAsString ] { } Method [ <internal> public method __toString ] { } } }</PRE ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.reflection.reflectionfunction" ><B CLASS="classname" >ReflectionFunction</B ></A ></H3 ><P >
The <B CLASS="classname" >ReflectionFunction</B > class lets you reverse-engineer functions. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5359" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class ReflectionFunction implements Reflector { public object __construct(string name) public string __toString() public static string export() public string getName() public bool isInternal() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public array getStaticVariables() public mixed invoke(mixed* args) public bool returnsReference() public ReflectionParameter[] getParameters() } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
To introspect a function, you will first have to create an instance of the <B CLASS="classname" >ReflectionFunction</B > class. You can then call any of the above methods on this instance. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5363" ></A ><P ><B >Beispiel 18-21. Using the <B CLASS="classname" >ReflectionFunction</B > class</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /** * A simple counter * * @return int */ function counter() { static $c = 0; return $c++; } // Create an instance of the Reflection_Function class $func = new ReflectionFunction('counter'); // Print out basic information printf( "===> The %s function '%s'\n". " declared in %s\n". " lines %d to %d\n", $func->isInternal() ? 'internal' : 'user-defined', $func->getName(), $func->getFileName(), $func->getStartLine(), $func->getEndline() ); // Print documentation comment printf("---> Documentation:\n %s\n", var_export($func->getDocComment(), 1)); // Print static variables if existant if ($statics = $func->getStaticVariables()) { printf("---> Static variables: %s\n", var_export($statics, 1)); } // Invoke the function printf("---> Invokation results in: "); var_dump($func->invoke()); // you may prefer to use the export() method echo "\nReflectionFunction::export() results:\n"; echo ReflectionFunction::export('counter'); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > The method <B CLASS="function" >invoke()</B > accepts a variable number of arguments which are passed to the function just as in <A HREF="#function.call-user-func" ><B CLASS="function" >call_user_func()</B ></A >. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.reflection.reflectionparameter" ><B CLASS="classname" >ReflectionParameter</B ></A ></H3 ><P >
The <B CLASS="classname" >ReflectionParameter</B > class retrieves information about a function's or method's parameters. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5376" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class ReflectionParameter implements Reflector { public object __construct(string name) public string __toString() public static string export() public string getName() public ReflectionClass getClass() public bool allowsNull() public bool isPassedByReference() public bool isOptional() } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <B CLASS="function" >isOptional()</B > was added in PHP 5.1.0. </P ></BLOCKQUOTE ></DIV ><P >
To introspect function parameters, you will first have to create an instance of the <B CLASS="classname" >ReflectionFunction</B > or <B CLASS="classname" >ReflectionMethod</B > classes and then use their <B CLASS="function" >getParameters()</B > method to retrieve an array of parameters. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5385" ></A ><P ><B >Beispiel 18-22. Using the <B CLASS="classname" >ReflectionParameter</B > class</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function foo($a, $b, $c) { } function bar(Exception $a, &$b, $c) { } function baz(ReflectionFunction $a, $b = 1, $c = null) { } function abc() { } // Create an instance of Reflection_Function with the // parameter given from the command line. $reflect = new ReflectionFunction($argv[1]); echo $reflect; foreach ($reflect->getParameters() as $i => $param) { printf( "-- Parameter #%d: %s {\n". " Class: %s\n". " Allows NULL: %s\n". " Passed to by reference: %s\n". " Is optional?: %s\n". "}\n", $i, $param->getName(), var_export($param->getClass(), 1), var_export($param->allowsNull(), 1), var_export($param->isPassedByReference(), 1), $param->isOptional() ? 'yes' : 'no' ); } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.reflection.reflectionclass" ><B CLASS="classname" >ReflectionClass</B ></A ></H3 ><P >
The <B CLASS="classname" >ReflectionClass</B > class lets you reverse-engineer classes. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5394" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class ReflectionClass implements Reflector { public __construct(string name) public string __toString() public static string export() public string getName() public bool isInternal() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public ReflectionMethod getConstructor() public ReflectionMethod getMethod(string name) public ReflectionMethod[] getMethods() public ReflectionProperty getProperty(string name) public ReflectionProperty[] getProperties() public array getConstants() public mixed getConstant(string name) public bool isInstantiable() public bool isInterface() public bool isFinal() public bool isAbstract() public int getModifiers() public bool isInstance(stdclass object) public stdclass newInstance(mixed* args) public ReflectionClass[] getInterfaces() public ReflectionClass getParentClass() public bool isSubclassOf(ReflectionClass class) } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
To introspect a class, you will first have to create an instance of the <B CLASS="classname" >ReflectionClass</B > class. You can then call any of the above methods on this instance. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5398" ></A ><P ><B >Beispiel 18-23. Using the <B CLASS="classname" >ReflectionClass</B > class</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php interface Serializable { // ... } class Object { // ... } /** * A counter class * */ class Counter extends Object implements Serializable { const START = 0; private static $c = Counter::START; /** * Invoke counter * * @access public * @return int */ public function count() { return self::$c++; } } // Create an instance of the ReflectionClass class $class= new ReflectionClass('Counter'); // Print out basic information printf( "===> The %s%s%s %s '%s' [extends %s]\n". " declared in %s\n". " lines %d to %d\n". " having the modifiers %d [%s]\n", $class->isInternal() ? 'internal' : 'user-defined', $class->isAbstract() ? ' abstract' : '', $class->isFinal() ? ' final' : '', $class->isInterface() ? 'interface' : 'class', $class->getName(), var_export($class->getParentClass(), 1), $class->getFileName(), $class->getStartLine(), $class->getEndline(), $class->getModifiers(), implode(' ', Reflection::getModifierNames($class->getModifiers())) ); // Print documentation comment printf("---> Documentation:\n %s\n", var_export($class->getDocComment(), 1)); // Print which interfaces are implemented by this class printf("---> Implements:\n %s\n", var_export($class->getInterfaces(), 1)); // Print class constants printf("---> Constants: %s\n", var_export($class->getConstants(), 1)); // Print class properties printf("---> Properties: %s\n", var_export($class->getProperties(), 1)); // Print class methods printf("---> Methods: %s\n", var_export($class->getMethods(), 1)); // If this class is instantiable, create an instance if ($class->isInstantiable()) { $counter= $class->newInstance(); echo '---> $counter is instance? '; echo $class->isInstance($counter) ? 'yes' : 'no'; echo "\n---> new Object() is instance? "; echo $class->isInstance(new Object()) ? 'yes' : 'no'; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > The method <B CLASS="function" >newInstance()</B > accepts a variable number of arguments which are passed to the function just as in <A HREF="#function.call-user-func" ><B CLASS="function" >call_user_func()</B ></A >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <VAR CLASS="literal" >$class = new ReflectionClass('Foo'); $class->isInstance($arg)</VAR > is equivalent to <VAR CLASS="literal" >$arg instanceof Foo</VAR > or <VAR CLASS="literal" >is_a($arg, 'Foo')</VAR >. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.reflection.reflectionmethod" ><B CLASS="classname" >ReflectionMethod</B ></A ></H3 ><P >
The <B CLASS="classname" >ReflectionMethod</B > class lets you reverse-engineer class methods. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5416" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class ReflectionMethod extends ReflectionFunction { public __construct(mixed class, string name) public static string export() public mixed invoke(stdclass object, mixed* args) public bool isFinal() public bool isAbstract() public bool isPublic() public bool isPrivate() public bool isProtected() public bool isStatic() public bool isConstructor() public int getModifiers() public ReflectionClass getDeclaringClass() /* Inherited from ReflectionFunction */ public string __toString() public string getName() public bool isInternal() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public array getStaticVariables() public bool returnsReference() public ReflectionParameter[] getParameters() } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
To introspect a method, you will first have to create an instance of the <B CLASS="classname" >ReflectionMethod</B > class. You can then call any of the above methods on this instance. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5420" ></A ><P ><B >Beispiel 18-24. Using the <B CLASS="classname" >ReflectionMethod</B > class</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Counter { private static $c = 0; /** * Increment counter * * @final * @static * @access public * @return int */ final public static function increment() { self::$c++; return self::$c; } } // Create an instance of the Reflection_Method class $method= new ReflectionMethod('Counter', 'increment'); // Print out basic information printf( "===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n". " declared in %s\n". " lines %d to %d\n". " having the modifiers %d[%s]\n", $method->isInternal() ? 'internal' : 'user-defined', $method->isAbstract() ? ' abstract' : '', $method->isFinal() ? ' final' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' static' : '', $method->getName(), $method->isConstructor() ? 'the constructor' : 'a regular method', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers())) ); // Print documentation comment printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1)); // Print static variables if existant if ($statics= $method->getStaticVariables()) { printf("---> Static variables: %s\n", var_export($statics, 1)); } // Invoke the method printf("---> Invokation results in: "); var_dump($method->invoke(NULL)); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Trying to invoke private, protected or abstract methods will result in an exception being thrown from the <B CLASS="function" >invoke()</B > method. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > For static methods as seen above, you should pass NULL as the first argument to <B CLASS="function" >invoke()</B >. For non-static methods, pass an instance of the class. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.reflection.reflectionproperty" ><B CLASS="classname" >ReflectionProperty</B ></A ></H3 ><P >
The <B CLASS="classname" >ReflectionProperty</B > class lets you reverse-engineer class properties. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5435" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class ReflectionProperty implements Reflector { public __construct(mixed class, string name) public string __toString() public static string export() public string getName() public bool isPublic() public bool isPrivate() public bool isProtected() public bool isStatic() public bool isDefault() public int getModifiers() public mixed getValue(stdclass object) public void setValue(stdclass object, mixed value) public ReflectionClass getDeclaringClass() } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
To introspect a method, you will first have to create an instance of the <B CLASS="classname" >ReflectionProperty</B > class. You can then call any of the above methods on this instance. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5439" ></A ><P ><B >Beispiel 18-25. Using the <B CLASS="classname" >ReflectionProperty</B > class</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class String { public $length = 5; } // Create an instance of the ReflectionProperty class $prop = new ReflectionProperty('String', 'length'); // Print out basic information printf( "===> The%s%s%s%s property '%s' (which was %s)\n". " having the modifiers %s\n", $prop->isPublic() ? ' public' : '', $prop->isPrivate() ? ' private' : '', $prop->isProtected() ? ' protected' : '', $prop->isStatic() ? ' static' : '', $prop->getName(), $prop->isDefault() ? 'declared at compile-time' : 'created at run-time', var_export(Reflection::getModifierNames($prop->getModifiers()), 1) ); // Create an instance of String $obj= new String(); // Get current value printf("---> Value is: "); var_dump($prop->getValue($obj)); // Change value $prop->setValue($obj, 10); printf("---> Setting value to 10, new value is: "); var_dump($prop->getValue($obj)); // Dump object var_dump($obj); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Trying to get or set private or protected class property's values will result in an exception being thrown. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.reflection.reflectionextension" ><B CLASS="classname" >ReflectionExtension</B ></A ></H3 ><P >
The <B CLASS="classname" >ReflectionExtension</B > class lets you reverse-engineer extensions. You can retrieve all loaded extensions at runtime using the <A HREF="#function.get-loaded-extensions" ><B CLASS="function" >get_loaded_extensions()</B ></A >. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5451" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class ReflectionExtension implements Reflector { public __construct(string name) public string __toString() public static string export() public string getName() public string getVersion() public ReflectionFunction[] getFunctions() public array getConstants() public array getINIEntries() } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P >
To introspect a method, you will first have to create an instance of the <B CLASS="classname" >ReflectionProperty</B > class. You can then call any of the above methods on this instance. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5455" ></A ><P ><B >Beispiel 18-26. Using the <B CLASS="classname" >ReflectionExtension</B > class</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Create an instance of the ReflectionProperty class $ext = new ReflectionExtension('standard'); // Print out basic information printf( "Name : %s\n". "Version : %s\n". "Functions : [%d] %s\n". "Constants : [%d] %s\n". "INI entries : [%d] %s\n", $ext->getName(), $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION', sizeof($ext->getFunctions()), var_export($ext->getFunctions(), 1), sizeof($ext->getConstants()), var_export($ext->getConstants(), 1), sizeof($ext->getINIEntries()), var_export($ext->getINIEntries(), 1) ); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="language.oop5.reflection.extending" >Extending the reflection classes</A ></H3 ><P >
In case you want to create specialized versions of the built-in classes (say, for creating colorized HTML when being exported, having easy-access member variables instead of methods or having utility methods), you may go ahead and extend them. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5462" ></A ><P ><B >Beispiel 18-27. Extending the built-in classes</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /** * My Reflection_Method class * */ class My_Reflection_Method extends ReflectionMethod { public $visibility= ''; public function __construct($o, $m) { parent::__construct($o, $m); $this->visibility= Reflection::getModifierNames($this->getModifiers()); } } /** * Demo class #1 * */ class T { protected function x() {} } /** * Demo class #2 * */ class U extends T { function x() {} } // Print out information var_dump(new My_Reflection_Method('U', 'x')); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Caution: If you're overwriting the constructor, remember to call the parent's constructor _before_ any code you insert. Failing to do so will result in the following: <VAR CLASS="literal" >
Fatal error: Internal error: Failed to retrieve the reflection object </VAR > </P ></BLOCKQUOTE ></DIV ></DIV ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="language.references" >Kapitel 19. Referenzen in PHP</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="language.references.whatare" >Was Referenzen sind</A ></H2 ><P > Referenzen sind in PHP ein Mechanismus um verschiedene Namen fⁿr den gleichen Inhalt von Variablen zu erm÷glichen. Sie sind nicht mit Zeigern in C zu vergleichen, sondern Aliasdefinitionen fⁿr die Symboltabelle. PHP unterscheidet zwischen Variablenname und Variableninhalt, wobei der gleiche Variableninhalt unterschiedliche Namen besitzen kann. Der bestm÷gliche Vergleich ist der mit Dateinamen und Dateien im Dateisystem von Unix - Variablennamen sind VerzeichniseintrΣge, wΣhrend der Variableninhalt die eigentliche Datei darstellt. Referenzen k÷nnen nun als Hardlinks im Dateisystem verstanden werden. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.references.whatdo" >Was Referenzen leisten</A ></H2 ><P >
PHP Referenzen erlauben es, zwei Variablennamen sich auf den gleichen Variableninhalt beziehen zu lassen. Das heisst im folgenden Beispiel, dass sich <VAR CLASS="varname" >$a</VAR > und <VAR CLASS="varname" >$b</VAR > auf dieselbe Variable beziehen: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5478" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$a =& $b</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <VAR CLASS="varname" >$a</VAR > und <VAR CLASS="varname" >$b</VAR > sind hier gleichwertig, und <VAR CLASS="varname" >$a</VAR > ist nicht nur ein Zeiger auf <VAR CLASS="varname" >$b</VAR > oder umgekehrt, sondern <VAR CLASS="varname" >$a</VAR > und <VAR CLASS="varname" >$b</VAR > zeigen auf den selben Inhalt. </P ></BLOCKQUOTE ></DIV > </P ><P >
Seit PHP 4.0.4 kann <VAR CLASS="literal" >&</VAR > auch in Verbindung mit <VAR CLASS="literal" >new</VAR > verwendet werden. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5491" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$bar =& new fooclass(); $foo =& find_var ($bar);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Wenn der <VAR CLASS="literal" >&</VAR > Operator nicht verwendet wird, erzeugt PHP eine Kopie des Objekts. Wenn nun <VAR CLASS="literal" >$this</VAR > innerhalb der Klasse verwendet wird, bezieht es sich auf die aktuelle Instanz der Klasse. Die Zuordnung ohne <VAR CLASS="literal" >&</VAR > erzeugt eine Kopie der Instanz (d.h. des Objekts) und <VAR CLASS="literal" >$this</VAR > wird sich auf die Kopie beziehen. In der Regel will man aus Performance- und Speicherverbrausgrⁿnden nur eine einzige Instanz einer Klasse erzeugen. Dafⁿr stellt PHP den <VAR CLASS="literal" >&</VAR > bereit. </P ></BLOCKQUOTE ></DIV ><P >
Eine weitere Einsatzm÷glichkeit von Referenzen ist die ▄bergabe von Parametern an eine Funktion mit pass-by-reference. Hierbei beziehen sich der lokale Variablenname als auch der Variablenname der aufrufenden Instanz auf denselben Variableninhalt: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5501" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function foo (&$var) { $var++; } $a=5; foo ($a);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Nach der Ausfⁿhrung hat <VAR CLASS="varname" >$a</VAR > den Wert 6, da sich in der Funktion <VAR CLASS="varname" >foo</VAR > der Variablenname <VAR CLASS="varname" >$var</VAR > auf denselben Variableninhalt bezieht wie <VAR CLASS="varname" >$a</VAR > in der aufrufenden Instanz (hier das Hauptprogramm). </P ><P >
Daneben besteht die M÷glichkeit aus Funktionen heraus Werte mit <A HREF="#language.references.return" >return by-reference</A > zurⁿckzugeben. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.references.arent" >Was Referenzen nicht sind</A ></H2 ><P >
Wie bereits gesagt: Referenzen sind keine Zeiger. Das bedeutet, der folgende Code tut nicht, was zum Beispiel ein C Programmierer erwarten wⁿrde: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5512" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function foo (&$var) { $var =& $GLOBALS["baz"]; } foo($bar);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Folgendes wird passieren: <VAR CLASS="varname" >$var</VAR > in foo wird zunΣchst an <VAR CLASS="varname" >$bar</VAR > aus der aufrufenden Instanz, dann aber an <VAR CLASS="varname" >$GLOBALS["baz"]</VAR >, gebunden. Es gibt keine M÷glichkeit, <VAR CLASS="varname" >$bar</VAR > aus der aufrufenden Instanz mittels Referenz-Mechanismen an etwas anderes zu binden, da <VAR CLASS="varname" >$bar</VAR > in der Funktion foo nicht zur Verfⁿgung steht (<VAR CLASS="varname" >$bar</VAR > wird durch <VAR CLASS="varname" >$var</VAR > reprΣsentiert; <VAR CLASS="varname" >$var</VAR > verfⁿgt nur ⁿber Variableninhalt, besitzt aber keinen name-to-value Eintrag in der Symboltabelle der aufrufenden Instanz). </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.references.return" >Referenzen zurⁿckgeben</A ></H2 ><P >
Das Zurⁿckgeben von Ergebnissen per Referenz aus Funktionen heraus kann manchmal recht nⁿzlich sein. Hierbei ist folgende Syntax zu beachten: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5526" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function &find_var ($param) { ...code... return $found_var; } $foo =& find_var ($bar); $foo->x = 2;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > In diesem Beispiel wird also die Eigenschaft des von <VAR CLASS="varname" >find_var</VAR > gelieferten Objektes gesetzt, nicht die der Kopie, wie es der Fall wΣre, wenn die Funktion <VAR CLASS="varname" >find_var</VAR > ihr Ergebnis nicht per Referenz liefern wⁿrde. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Im Gegensatz zur Parameterⁿbergabe per Referenz ist bei der Rⁿckgabe mittels Referenz an beiden Stellen die Angabe des <VAR CLASS="literal" >&</VAR > notwendig. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.references.unset" >Referenzen aufheben</A ></H2 ><P >
Wird eine Referenz aufgehoben, so wird nur die Bindung zwischen einem Variablennamen und dem Variableninhalt entfernt. Der Inhalt der Variablen wird hierbei nicht gel÷scht. Betrachten wir folgendes Beispiel: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5536" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$a = 1; $b =& $a; unset ($a);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Die Variable <VAR CLASS="varname" >$b</VAR > wird nicht gel÷scht, sondern es wird nur die Bindung des Variablennamen <VAR CLASS="varname" >$a</VAR > an den Variableninhalt aufgehoben. Dieser Variableninhalt ist immer noch ⁿber <VAR CLASS="varname" >$b</VAR > verfⁿgbar. </P ><P >
Wiederum sieht man die Analogie zwischen Unix und den Referenzen: Das Aufheben einer Referenz entspricht einem Aufruf von <B CLASS="command" >unlink</B > unter Unix. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="language.references.spot" >Referenzen entdecken</A ></H2 ><P >
Viele Sprachelemente von PHP sind intern mit der Benutzung von Referenzen implementiert, daher gilt alles bisher gesagte auch fⁿr diese Konstrukte: </P ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="references.global" ><VAR CLASS="literal" >global</VAR > References</A ></H3 ><P >
Die Verwendung von <B CLASS="command" >global $var</B > erzeugt im aktuellen Scope eine Referenz auf die globale Variable <VAR CLASS="varname" >$var</VAR >, sie ist also Σquivalent zu folgendem: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5552" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$var =& $GLOBALS["var"];</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Dies hat zur Folge, dass das Anwenden von unset() auf <VAR CLASS="varname" >$var</VAR > keinen Einfluss auf die globale Variable hat. </P ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="references.this" ><VAR CLASS="literal" >$this</VAR ></A ></H3 ><P >
In einer Objektmethode ist <VAR CLASS="varname" >$this</VAR > immer eine Referenz auf die aufrufende Objektinstanz. </P ></DIV ></DIV ></DIV ></DIV ><DIV CLASS="PART" ><A NAME="security" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >IV. Sicherheit</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT >20. <A HREF="#security.intro" >Einfⁿhrung</A ></DT ><DT >21. <A HREF="#security.general" >Allgemeine ▄berlegungen</A ></DT ><DT >22. <A HREF="#security.cgi-bin" >CGI-Version</A ></DT ><DT >23. <A HREF="#security.apache" >Apache-Modul</A ></DT ><DT >24. <A HREF="#security.filesystem" >Dateisystem - Sicherheit</A ></DT ><DT >25. <A HREF="#security.database" >Datenbank - Sicherheit</A ></DT ><DT >26. <A HREF="#security.errors" >Fehlerbehandlung</A ></DT ><DT >27. <A HREF="#security.globals" >Verwendung von Register Globals</A ></DT ><DT >28. <A HREF="#security.variables" >Vom Nutzer ⁿbermittelte Daten</A ></DT ><DT >29. <A HREF="#security.hiding" >Verstecken von PHP</A ></DT ><DT >30. <A HREF="#security.current" >Aktuell bleiben</A ></DT ></DL ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.intro" >Kapitel 20. Einfⁿhrung</A ></H1 ><P >
PHP ist eine mΣchtige Sprache, und der Interpreter, der in einen Webserver als Modul oder als separate <ACRONYM CLASS="acronym" >CGI</ACRONYM >-Version eingebunden werden kann, kann auf Dateien zugreifen, Befehle ausfⁿhren und Netzwerkverbindungen zu einem Server herstellen. Diese Eigenschaften k÷nnen einen Webserver unsicher machen, wenn man es bei den Voreinstellungen belΣsst. PHP wurde besonders dafⁿr entwickelt, um eine sicherere Sprache als Perl oder C fⁿr die Erstellung von CGI-Programmen bereitzustellen. Mit der richtigen Wahl der Einstellungen beim Kompilieren und zur Laufzeit bietet PHP genau die Kombination aus Freiheit und Sicherheit, die gerade ben÷tigt wird. </P ><P >
Da es sehr viele verschiedene M÷glichkeiten gibt, PHP zu nutzen, gibt es viele Konfigurationseinstellungen, die das Verhalten von PHP beeinflussen. Eine gro▀e Auswahl an Einstellungen garantiert, dass man PHP fⁿr viele Zwecke einsetzen kann. Allerdings bedeutet das auch, dass es Kombinationen gibt, die eine Installation mit nur ungenⁿgender Sicherheit zur Folge haben. </P ><P >
Die FlexibilitΣt der Konfiguration konkurriert mit der FlexibilitΣt in der Programmierung. Mit PHP k÷nnen komplette Server Applikationen mit allen M÷glichkeiten eines Shell Benutzers erstellt werden, oder auch nur einfache Server Side Includes mit einem minimalen Risiko in einer streng kontrollierten Umgebung. Wie die Umgebung erstellt wird, und wie sicher diese ist, ist zu einem gro▀en Teil die Sache des PHP Entwicklers. </P ><P >
Dieses Kapitel beginnt mit einigen generellen RatschlΣgen zur Sicherheit, erklΣrt die verschiedenen Kombinationen der Konfigurationseinstellungen und unter welchen Gegebenheiten sie sicher genutzt werden k÷nnen, und beschreibt verschiedene ▄berlegungen zur Programmierung fⁿr verschiedene Sicherheitsstufen. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.general" >Kapitel 21. Allgemeine ▄berlegungen</A ></H1 ><P >
Ein komplett sicheres System ist praktisch ein Ding der Unm÷glichkeit, weshalb ein unter Sicherheitsprofis oft genutzter Ansatz ist, einen Mittelweg zwischen Risiko und Verwendbarkeit zu finden. Wenn jede von einem Benutzer ⁿbermittelte Variable zwei Formen von biometrischer Prⁿfung (wie z.B. ein Scan der Netzhaut und ein Fingerabdruck) verlangen wⁿrde, wΣre eine extrem hohe Ebene der Verantwortlichkeit erreicht. Ein sehr komplexes Formular auszufⁿllen wⁿrde auch eine halbe Stunde in Anspruch nehmen, die Benutzer dazu ermuntern k÷nnte, Wege zur Umgehung der Sicherheitsma▀nahmen zu suchen. </P ><P >
Die beste Sicherheit ist oft unaufdringlich genug um den Anforderungen zu entsprechen, ohne den Benutzer an seiner Arbeit zu hindern oder den Code-Autor mit ⁿbertriebener KomplexitΣt zu ⁿberlasten. TatsΣchlich sind einige Sicherheitsangriffe nur die Folge von allzu strengen Sicherheitsma▀nahmen, was mit der Zeit nur zu deren Unterminierung fⁿhrt. </P ><P >
Eine Phrase die es wert ist, sich an sie zu erinnern: Ein System ist nur so gut wie das schwΣchste Glied in der Kette. Wenn alle Transaktionen mittels Zeit, Ort, Transaktionstyp, etc. streng mitprotokolliert werden, der Benutzer aber nur mittels einem einzigen Cookie verifiziert wird, lΣsst die ZuverlΣssigkeit fⁿr die Bindung des Benutzers an das Transaktions-Log bedrohlich nach. </P ><P >
Denken Sie wΣhrend der Tests daran, dass Sie selbst fⁿr die einfachsten Seiten nicht alle M÷glichkeiten testen k÷nnen. Der von Ihnen vielleicht erwartete Input wird zu dem eines verstimmten Mitarbeiters oder eines Crackers der Monate Zeit hat, oder einer Katze, die ⁿber die Tastatur lΣuft in keinerlei Zusammenhang stehen. Deshalb betrachten Sie Ihren Code am Besten aus der logischen Perspektive um zu erkennen, wo unerwartete Daten eingebracht werden k÷nnen und fragen sich dann, wie diese modifiziert, reduziert, oder weiter ausgefⁿhrt werden. </P ><P >
Das Internet ist voll von Leuten die versuchen, sich durch Entschlⁿsseln/zerst÷ren Ihres Codes, den Zusammenbruch Ihres Systems, Einsetzen von unangebrachten Inhalten, und anderen, Ihren Tag interessant gestaltenden Ma▀nahmen, einen Namen zu machen. Es ist egal, ob Sie eine kleine oder gro▀e Site haben, Sie sind einfach ein Ziel wenn Sie online sind oder wenn Sie einen Server haben, zu dem man eine Verbindung aufbauen kann. Viele Cracker-Programme erkennen nicht die Gr÷▀e, sondern durchsieben einfach gewaltige IP Bl÷cke im Netz, um Opfer zu finden. Versuchen Sie, keines zu werden. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.cgi-bin" >Kapitel 22. CGI-Version</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="security.cgi-bin.attacks" >M÷gliche Angriffe</A ></H2 ><P >
PHP als <ACRONYM CLASS="acronym" >CGI</ACRONYM > zu nutzen, ist eine M÷glichkeit fⁿr Installationen, bei denen aus irgendwelchen Grⁿnden kein Modul in die Serversoftware eingebunden werden soll (wie beim Apache) oder fⁿr Systeme, bei denen verschiedene CGI-Wrapper genutzt werden sollen, um sichere chroot- und setuid-Umgebungen fⁿrr Skripte zu schaffen. Bei dieser Konfiguration wird das ausfⁿhrbare PHP-Binary ⁿblicherweise im cgi-bin Verzeichnis des Webservers installiert. CERT advisory <A HREF="http://www.cert.org/advisories/CA-1996-11.html" TARGET="_top" >CA-96.11</A > spricht sich gegen die Platzierung von Interpretern im cgi-bin Verzeichnis aus. Obwohl das PHP-Binary als eigenstΣndiger Interpreter verwendet werden kann, wurde PHP so entwickelt, um den durch diese Konfiguration m÷glich werdenden Angriffe vorzubeugen: </P ><P ></P ><UL ><LI ><P >Zugriff auf Systemdateien: <TT CLASS="filename" >http://my.host/cgi-bin/php?/etc/passwd</TT > </P ><P >
Die auf ein Fragezeichen (?) folgende Abfrageinformation in einer URL wird durch das CGI-Interface als Kommandozeilenargument an den Interpreter weitergereicht. In der Kommandozeile wird ⁿblicherweise die im ersten Argument angegebene Datei von Interpretern ge÷ffnet und ausgefⁿhrt. </P ><P >
Beim Aufruf als CGI-Binary verweigert PHP die Interpretation der Kommandozeilenargumente. </P ></LI ><LI ><P >
Zugriff auf beliebige Web-Dokumente auf dem Server: <TT CLASS="filename" >http://my.host/cgi-bin/php/secret/doc.html</TT > </P ><P >
Der Teil der URL-Pfadinformation nach dem Namen der PHP BinΣrdatei, <TT CLASS="filename" >/secret/doc.html</TT > wird im allgemeinen benutzt, um den Namen der Datei zu ⁿbergeben, die durch das <ACRONYM CLASS="acronym" >CGI</ACRONYM >-Programm ge÷ffnet und interpretiert werden soll. Normalerweise werden einige EintrΣge in der Konfigurationsdatei des Webservers benutzt (Apache: Action), um Aufrufe von Dokumenten wie <TT CLASS="filename" >http://my.host/secret/script.php</TT > an den PHP-Interpreter umzuleiten. Bei dieser Konfiguration ⁿberprⁿft der Webserver zuerst die Zugriffsrechte im Verzeichnis <TT CLASS="filename" >/secret</TT > und erstellt anschlie▀end den umgeleiteten Aufruf <TT CLASS="filename" >http://my.host/cgi-bin/php/secret/script.php</TT >. Unglⁿcklicherweise wird, wenn der Aufruf bereits in dieser Form geschieht, vom Webserver keine Zugriffsⁿberprⁿfung der Datei <TT CLASS="filename" >/secret/script.php</TT >, sondern lediglich der Datei <TT CLASS="filename" >/cgi-bin/php</TT > vorgenommen. So ist jeder Benutzer, der auf <TT CLASS="filename" >/cgi-bin/php</TT > zugreifen darf in der Lage, sich zu jedem geschⁿtzten Dokument auf dem Webserver Zugriff zu verschaffen. </P ><P >
Bei PHP k÷nnen beim Kompilieren die Konfigurationsoption <A HREF="#configure.enable-force-cgi-redirect" >--enable-force-cgi-redirect</A > und zur Laufzeit die Konfigurationsdirektiven <A HREF="#ini.doc-root" >doc_root</A > und <A HREF="#ini.user-dir" >user_dir</A > benutzt werden, um diesen Angriff zu verhindern, falls der Verzeichnisbaum des Servers Verzeichnisse mit ZugriffsbeschrΣnkungen beinhaltet. Ausfⁿhrliche Informationen ⁿber die verschiedenen Kombinationen siehe weiter unten. </P ></LI ></UL ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="security.cgi-bin.default" >Fall 1: Nur ÷ffentliche Dateien vorhanden</A ></H2 ><P >
Wenn der Server keine Inhalte hat, die durch Passwort oder IP-basierte Zugriffskontrolle geschⁿtzt sind, werden diese Konfigurationsoptionen nicht ben÷tigt. Wenn der Webserver keine Redirects erlaubt oder keine M÷glichkeit hat, dem PHP-Binary mitzuteilen dass es sich um eine sicher umgeleitete Anfrage handelt, kann die Option <A HREF="#configure.enable-force-cgi-redirect" >--enable-force-cgi-redirect</A > im configure-Script angegeben werden. Nichtsdestotrotz mⁿssen Sie sicherstellen, dass Ihre PHP-Skripte nicht auf die eine oder andere Art des Aufrufs angewiesen sind, weder direkt durch <TT CLASS="filename" >http://my.host/cgi-bin/php/dir/script.php</TT > noch durch einen Redirect <TT CLASS="filename" >http://my.host/dir/script.php</TT >. </P ><P >
Beim Apache kann der Redirect durch den Gebrauch von AddHandler und Action konfiguriert werden (siehe unten). </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="security.cgi-bin.force-redirect" >Fall 2: --enable-force-cgi-redirect benutzen</A ></H2 ><P >
Diese beim Kompilieren verwendete Option verhindert grundsΣtzlich den Aufruf von PHP mit einer URL wie <TT CLASS="filename" >http://my.host/cgi-bin/php/secretdir/script.php</TT >. Stattdessen parst PHP in diesem Modus nur dann, wenn der Aufruf durch einen korrekten Redirect des Webservers erfolgte. </P ><P >
Normalerweise wird der Redirect in der Apache-Konfiguration mit den folgenden EintrΣgen festgelegt:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" >Action php-script /cgi-bin/php AddHandler php-script .php</PRE ></TD ></TR ></TABLE ><P >
Diese Option wurde nur mit dem Apache Webserver getestet und ist abhΣngig davon, wie Apache die nicht standardmΣ▀ige CGI-Umgebungsvariable <VAR CLASS="envar" >REDIRECT_STATUS</VAR > bei Redirect-Anfragen setzt. Sollte Ihr Webserver keine M÷glichkeit unterstⁿtzen, zu ⁿbermitteln, ob es sich um einen direkte Aufruf oder einen Redirect handelt, k÷nnen Sie diese Option nicht verwenden und mⁿssen einen der anderen hier beschriebenen Wege gehen, die CGI-Version zu nutzen. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="security.cgi-bin.doc-root" >Fall 3: doc_root oder user_dir festlegen</A ></H2 ><P >
Aktiven Inhalt, wie beispielsweise Skripts und ausfⁿhrbare Dateien, in den Dokumentverzeichnissen des Webservers abzulegen, wird manchmal als unsichere Methode angesehen. Wenn, beispielsweise aufgrund von Konfigurationsfehlern, die Skripte nicht ausgefⁿhrt, sondern als regulΣre HTML-Dokumente angezeigt werden kann dies ein Durchsickern von geistigem Eigentum und sicherheitsrelevanter Informationen (Passw÷rter!) zur Folge haben. Deshalb ziehen es viele Systemadministratoren vor, eine zweite Verzeichnisstruktur fⁿr Skripte einzurichten, auf die nur durch das PHP-CGI zugegriffen werden kann. Diese werden dann stets interpretiert und nicht angezeigt. </P ><P >
Auch wenn die Methode zum sichergestellten Verhindern einer Umleitung von Anfragen (wie im vorangegangenen Kapitel beschrieben) nicht verfⁿgbar ist, ist es notwendig, ein doc_root fⁿr Skripte zusΣtzlich zum Web-Dokumentenverzeichnis einzurichten. </P ><P >
Sie k÷nnen das PHP-Skriptverzeichnis durch die Direktive <A HREF="#ini.doc-root" >doc_root</A > in der <A HREF="#configuration.file" >Konfigurationsdatei</A > festlegen, oder Sie setzen die Umgebungsvariable <VAR CLASS="envar" >PHP_DOCUMENT_ROOT</VAR >. Wenn sie gesetzt ist, wird die CGI-Version von PHP den Namen der zu ÷ffnenden Datei stets aus <VAR CLASS="parameter" >doc_root</VAR > und der Pfadinformation der Anfrage zusammensetzen, sodass man sicher sein kann, dass au▀erhalb dieses Verzeichnisses keine Skripte ausgefⁿhrt werden (au▀er <VAR CLASS="parameter" >user_dir</VAR >, siehe unten). </P ><P >
Eine weitere hier nⁿtzliche Option ist <A HREF="#ini.user-dir" >user_dir</A >. Wenn das <VAR CLASS="parameter" >user_dir</VAR > nicht gesetzt ist, hat nur <VAR CLASS="parameter" >doc_root</VAR > Einfluss auf die zu ÷ffnende Datei. Der Aufruf einer URL wie <TT CLASS="filename" >http://my.host/~user/doc.php</TT > hat nicht zum Ergebnis, dass eine Datei im Home-Verzeichnis des Benutzers ge÷ffnet wird, sondern eine Datei namens <TT CLASS="filename" >~user/doc.php</TT > unterhalb des doc_root (Ja, ein Verzeichnisname, der mit einer Tilde anfΣngt [<VAR CLASS="literal" >~</VAR >]). </P ><P >
Ist das user_dir beispielsweise auf <TT CLASS="filename" >public_php</TT > gesetzt, wird eine Anfrage wie <TT CLASS="filename" >http://my.host/~user/doc.php</TT > eine Datei namens <TT CLASS="filename" >doc.php</TT > im Verzeichnis <TT CLASS="filename" >public_php</TT > im Heimatverzeichnis des Benutzers ÷ffnen. Wenn das Heimatverzeichnis des Benutzers <TT CLASS="filename" >/home/user</TT > ist, so ist die ausgefⁿhrte Datei <TT CLASS="filename" >/home/user/public_php/doc.php</TT >. </P ><P >
Die <VAR CLASS="parameter" >user_dir</VAR >-Expansion erfolgt ohne Berⁿcksichtigung der <VAR CLASS="parameter" >doc_root</VAR > Einstellung. So k÷nnen Zugriffe auf die Dokumenten- und Benutzerverzeichnisse separat gesteuert werden. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="security.cgi-bin.shell" >Fall 4: PHP-Parser au▀erhalb des Webverzeichnisbaums</A ></H2 ><P >
Eine sehr sichere Sache ist es, das PHP-Parser-Binary irgendwo au▀erhalb des Webverzeichnisbaums zu platzieren, beispielsweise in <TT CLASS="filename" >/usr/local/bin</TT >. Der einzige Nachteil dieses Verfahrens ist, dass eine Zeile Σhnlich der folgenden: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5652" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >#!/usr/local/bin/php</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > als erste Zeile in jeder Datei, die PHP-Tags enthΣlt, stehen muss. Au▀erdem muss die Datei ausfⁿhrbar sein. Ansonsten ist sie genauso zu behandeln wie ein beliebiges CGI-Script in Perl oder sh oder anderen gebrΣuchlichen Scriptsprachen, die den <VAR CLASS="literal" >#!</VAR > shell-escape-Mechanismus nutzen, um sich selbst aufzurufen. </P ><P >
Damit PHP bei dieser Konfiguration die <VAR CLASS="envar" >PATH_INFO</VAR >- und <VAR CLASS="envar" >PATH_TRANSLATED</VAR >-Informationen korrekt auswertet, sollte der PHP-Parser mit der Option <A HREF="#configure.enable-discard-path" >--enable-discard-path</A > kompiliert werden. </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.apache" >Kapitel 23. Apache-Modul</A ></H1 ><P >
Wenn PHP als Apache-Modul eingesetzt wird, ⁿbernimmt es die Benutzerrechte des Apache (ⁿblicherweise user "nobody"). Das hat verschiedene Auswirkungen auf Sicherheit und Authentifizierung, z.B.: Wenn Sie PHP zum Zugriff auf eine Datenbank benutzen, es sei denn diese Datenbank hat eine integrierte Zugriffskontrolle, mⁿssen Sie dem Benutzer "nobody" Zugriffsrechte auf die Datenbank erteilen. Das hei▀t, dass ein b÷swilliges Skript auch Benutzerkennung und Passwort auf die Datenbank zugreifen, und sie verΣndern k÷nnte. Es ist durchaus m÷glich, dass ein Web-Spider ⁿber die Webseite eines Datenbankadministrators stolpert, und alle Ihre Datenbanken l÷scht. Sie k÷nnen sich dagegen mit Apache Authentifizierung schⁿtzen, oder ein eigenes Zugangsmodell unter Verwendung von LDAP, .htaccess Dateien, etc. entwerfen, und diesen Code als Teil Ihrer PHP Skripte einbinden. </P ><P >
Es wurde festgestellt, dass wenn einmal die Sicherheitsma▀nahmen so weit eingerichtet sind dass dem PHP User (in diesem Fall ein Apache User) nur mehr ein geringes Risiko bleibt, PHP daran gehindert wird, virenverseuchte Dateien in das Benutzerverzeichnis zu schreiben. Oder vielleicht wurde es auch daran gehindert, auf Datenbanken zuzugreifen oder diese gar zu verΣndern. In gleicher Weise wird auch davor abgehalten, "gute" oder "b÷sartige" Dateien zu schreiben, oder "gute" bzw. "b÷sartige" Datenbanktransaktionen durchzufⁿhren. </P ><P >
Ein hΣufig gemachter Fehler in Punkto Sicherheit ist Apache Root-Rechte zu erteilen, oder die M÷glichkeiten von Apache in einer anderen Weise auszuweiten. </P ><P >
Die Ausweitung der Benutzerrechte fⁿr Apache auf root ist sehr gefΣhrlich, und kann dem gesamten System schaden, denn mit sudo, chroot, oder anderwΣrtig als root zu arbeiten sollte niemand anders als den Sicherheitsprofis ⁿberlassen werden. </P ><P >
Es gibt auch ein paar einfachere L÷sungen. Mit <A HREF="#ini.open-basedir" >open_basedir()</A > k÷nnen Sie kontrollieren, welche Verzeichnisse PHP benutzen darf oder nicht. Sie k÷nnen auch einen Bereich nur fⁿr Apache einrichten, um alle webbasierten AktivitΣten auf nicht-Benutzer- bzw. nicht-System-Dateien einzuschrΣnken. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.filesystem" >Kapitel 24. Dateisystem - Sicherheit</A ></H1 ><P >
PHP ist von den in den meisten Serversystemen implementierten Sicherheitseinstellungen hinsichtlich der Berechtigungen auf Datei- und Verzeichnisebene abhΣngig. Dies verleiht Ihnen Kontrolle darⁿber, welche Dateien in dem Dateisystem gelesen werden dⁿrfen. Vorsicht ist bei weltweit lesbaren Dateien geboten um sicherzustellen, dass diese sicher von allen Usern mit Zugriff auf dieses Dateisystem (nur) gelesen werden k÷nnen. </P ><P >
Da PHP entwickelt wurde um Zugriffe auf das Dateisystem auf Benutzebene zu erlauben, ist es natⁿrlich auch m÷glich ein PHP Skript zu schreiben das Ihnen erlaubt Systemdateien wie /etc/passwd zu lesen, Ethernetverbindungen zu modifizieren, enorme DruckauftrΣge zu senden, etc. Dies hat offensichtliche Implikationen, indem Sie sicherstellen mⁿssen, dass alle von Ihnen zu lesenden bzw. zu schreibenden Dateien auch die richtigen sind. </P ><P >
Stellen Sie sich folgendes Skript vor, in dem ein User zum Ausdruck bringt, dass gerne eine Datei in seinem Heimatverzeichnis l÷schen m÷chte. Dies geht von einer Situation aus, in der ein PHP Web-Interface regelmΣ▀ig zum Dateimanagement verwendet wird, und der Apache User ist auch berechtigt, in seinem Heimatverzeichnis Dateien zu l÷schen. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5673" ></A ><P ><B >Beispiel 24-1. Schlechte Variablenprⁿfung fⁿhrt zu....</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // L÷schen einer Datei aus dem Heimatverzeichnis des Users $username = $_POST['user_submitted_name']; $homedir = "/home/$username"; $file_to_delete = "$userfile"; unlink ("$homedir/$userfile"); echo "$file_to_delete wurde gel÷scht!"; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Da der Benutzername ⁿber ein User-Formular zu posten ist, kann jeder einen Benutzer- und Dateinamen von jemand anderen ⁿbertragen, und Dateien l÷schen. In diesem Fall empfiehlt es sich, eine andere Form der Authentifizierung zu verwenden. Stellen Sie sich vor was passieren wⁿrde, wenn die ⁿbertragenen Variablen "../etc/" und "passwd" beinhalten wⁿrden. Der Code wⁿrde dann effektiv lesen: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5676" ></A ><P ><B >Beispiel 24-2. ... Ein Angriff auf das Dateisystem</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // l÷scht eine Datei irgendwo auf der Festplatte, wo der // Benutzer die n÷tigen Rechte besitzt. Wenn PHP root hat: $username = "../etc/"; $homedir = "/home/../etc/"; $file_to_delete = "passwd"; unlink ("/home/../etc/passwd"); echo "/home/../etc/passwd wurde gel÷scht!"; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Es gibt zwei wichtige Kriterien die Sie beachten sollten, um diese Dinge zu vermeiden: <P ></P ><UL ><LI ><P >
Erteilen Sie dem PHP Web-user (BinΣrdatei) nur eingeschrΣnkte Rechte. </P ></LI ><LI ><P >
Prⁿfen Sie alle ⁿbertragenen Variablen. </P ></LI ></UL > Hier ist ein verbessertes Skript: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5684" ></A ><P ><B >Beispiel 24-3. Etwas sicherere Prⁿfung des Dateinamens</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // l÷scht eine Datei von der Festplatte, auf die // der PHP user Zugriff hat. $username = $_SERVER['REMOTE_USER']; // verwendet eine // Authentifizierungsmethode $homedir = "/home/$username"; $file_to_delete = basename("$userfile"); // den Pfad entfernen unlink ($homedir/$file_to_delete); $fp = fopen("/home/logging/filedelete.log","+a"); //logge die L÷schung $logstring = "$username $homedir $file_to_delete"; fputs ($fp, $logstring); fclose($fp); echo "$file_to_delete wurde gel÷scht!"; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Auch dies nicht v÷llig makellos. Wenn Ihr Authentifizierungssystem Benutzern erlauben sollte, deren eigene Logins zu kreieren, und ein Benutzer wΣhlt den Login "../etc", ist das System wieder aufgedeckt. Aus diesem Grund ziehen Sie es vielleicht vor, einen besseren Check zu schreiben: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5687" ></A ><P ><B >Beispiel 24-4. Sicherere Dateinamensprⁿfung</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $username = $HTTP_SERVER_VARS['REMOTE_USER']; // verwendet eine // Authentifizierungsmethode $homedir = "/home/$username"; if (!ereg('^[^./][^/]*$', $userfile)) die('bad filename'); // "DIE", gehen Sie nicht weiter if (!ereg('^[^./][^/]*$', $username)) die('bad username'); // "DIE", gehen Sie nicht weiter //etc... ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
AbhΣngig vom Betriebssystem gibt es eine gro▀e Anzahl Dateien mit der Sie sich befassen sollten, inklusive EintrΣge fⁿr GerΣte (/dev/ oder com1), Konfigurationsdateien (/etc/ Dateien und die .ini Dateien), gut bekannte Verzeichnisse (/home/, My Documents), etc. Aus diesem Grund ist es gew÷hnlich einfacher eine Vorgangsweise einzufⁿhren, bei der au▀er den von Ihnen explizit erlaubten Dingen alles verboten ist. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.database" >Kapitel 25. Datenbank - Sicherheit</A ></H1 ><P >
Heutzutage sind Datenbanken die Hauptkomponenten jeder Webbasierten Applikation, aufgrund welcher Websites verschiedene dynamische Inhalte anbieten k÷nnen. Nachdem heikle oder geheime Informationen in solch einer Datenbank gespeichert werden k÷nnen, sollten Sie deren Schutz ernsthaft bedenken. </P ><P >
Um Informationen zu bekommen oder zu speichern, mⁿssen Sie eine legitime Abfrage senden, das Ergebnis holen, und die Verbindung schlie▀en. Heutzutage ist die allgemein verwendete Abfragesprache fⁿr solche Interaktionen die Structured Query Language (SQL). Sehen Sie, wie sich ein Angreifer <A HREF="#security.database.sql-injection" >an einer SQL Abfrage zu schaffen machen</A > kann. </P ><P >
Sie werden merken, dass PHP Ihre Datenbank alleine nicht schⁿtzen kann. Die folgenden Abschnitte sind eine Einfⁿhrung in die Grundlagen, wie man innerhalb von PHP Skripten auf Datenbanken zugreift und diese manipuliert. </P ><P >
Denken Sie an diese einfache Regel: tief gestaffelte Verteidigung. Je mehr Platz Sie den Ma▀nahmen zum Schutz Ihrer Datenbank geben, desto geringer ist die Wahrscheinlichkeit, dass ein Angreifer Erfolg hat, und gespeicherte Geheiminformationen aufdeckt oder missbraucht. Gutes Design des Datenbankschemas und die Applikation wird mit Ihren gr÷▀ten Befⁿrchtungen fertig. </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="security.database.design" >Datenbanken designen</A ></H2 ><P >
Der Erste Schritt liegt immer im Erstellen der Datenbank, au▀er Sie wollen eine bereits existierende Dritter verwenden. Ist eine Datenbank erstellt, ist sie einem Eigentⁿmer zugewiesen, welcher das Kommando zum Erstellen ausgefⁿhrt hat. Gew÷hnlich kann nur der Eigentⁿmer (oder ein Superuser) alles mit den Objekten in dieser Datenbank machen, und um anderen Benutzern die Verwendung zu erlauben, mⁿssen Rechte vergeben werden. </P ><P >
Applikationen sollten sich mit der Datenbank nie als deren Eigentⁿmer oder als ein Superuser verbinden, da diese Benutzer jede gewollte Abfrage ausfⁿhren k÷nnen, um z.B. das Schema zu modifizieren (z.B. Tabellen l÷schen) oder den gesamten Inhalt l÷schen. </P ><P >
Sie k÷nnen verschiedene Datenbanknutzer mit sehr limitierten Rechten auf Datenbankobjekte fⁿr jeden Aspekt Ihrer Applikation anlegen. Nur die wirklich ben÷tigten Rechte sollten gewΣhrt werden, und vermeiden Sie, dass der gleiche Benutzer in verschiedenen AnwendungsfΣllen mit der Datenbank interagieren kann. Das hei▀t, dass Eindringlinge, welche unter Verwendung einer dieser Referenzen Zugriff auf Ihre Datenbank erlangt haben, nur so viele ─nderungen durchfⁿhren k÷nnen, wie es Ihre Applikation kann. </P ><P >
Implementieren Sie nicht alle GeschΣftslogik in die Webapplikation (z.B. Ihr Skript), sondern tun Sie das im Datenbankschema unter Verwendung von Sichten, Triggern, oder Regeln. Wenn sich das System entwickelt, werden neu zu ÷ffnende Ports zu der Datenbank vorgesehen, und Sie mⁿssen die Logik in jedem Datenbank-Client neu implementieren. ▄berdies k÷nnen Trigger verwendet werden, um transparent und automatisch mit Feldern umzugehen, welche beim debuggen Ihrer Applikation oder beim Zurⁿckverfolgen von Transaktionen oft einen Einblick gewΣhren. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="security.database.connection" >Zur Datenbank verbinden</A ></H2 ><P >
Vielleicht wollen Sie die Verbindungen ⁿber SSL herstellen, um die Client/Server Kommunikation fⁿr eine erh÷hte Sicherheit zu verschlⁿsseln, oder aber auch ssh verwenden, um die Netzwerkverbindung zwischen den Clients und dem Datenbankserver zu verschlⁿsseln. Ist eines davon realisiert, wird ein Monitoring Ihres Verkehrs und das Erlangen von Informationen zu harter Arbeit. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="security.database.storage" >Verschlⁿsseltes Speichermodell</A ></H2 ><P >
SSL/SSH schⁿtzt zwar die gerade auf dem Weg befindlichen Daten vom Client zum Server, jedoch nicht die dauernd in einer Datenbank gespeicherten Daten. SSL ist ein "auf-der-Leitung" Protokoll. </P ><P >
Hat ein Angreifer direkten Zugriff auf Ihre Datenbank (den Webserver umgehend), k÷nnen die gespeicherten heiklen Daten aufgedeckt oder zweckentfremdet werden, au▀er wenn die Information von der Datenbank selbst geschⁿtzt ist. Die Daten zu verschlⁿsseln ist ein guter Weg, diese Gefahr zu mildern, doch bieten nur wenige Datenbanken diese Art der Verschlⁿsselung von Daten. </P ><P >
Der einfachste Weg, dieses Problem zu umgehen ist, erst einmal Ihr eigenes Verschlⁿsselungspaket zu erstellen, und dieses dann in Ihren PHP Skripten zu nutzen. PHP kann Ihnen in diesem Fall mit seinen verschiedenen Erweiterungen helfen, wie z.B. <A HREF="#ref.mcrypt" >Mcrypt</A > and <A HREF="#ref.mhash" >Mhash</A >, welche eine gro▀e Auswahl an Verschlⁿsselungsalgorhythmen abdecken. Das Skript verschlⁿsselt die Daten vor dem Speichern, und entschlⁿsselt diese wieder beim Erhalt. Siehe die Verweise fⁿr weitere Beispiele, wie Verschlⁿsselung arbeitet. </P ><P >
Im Fall von wirklich versteckten Daten, wenn deren unverfΣlschte ReprΣsentation nicht n÷tig ist (z.B. keine Anzeige), ist hashing ebenfalls ⁿberlegenswert. Das bekannte Beispiel fⁿr das Hashing ist das Speichern des MD5 hash eines Passwortes in einer Datenbank, anstatt des Passwortes selbst. Siehe auch <A HREF="#function.crypt" ><B CLASS="function" >crypt()</B ></A > und <A HREF="#function.md5" ><B CLASS="function" >md5()</B ></A >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5717" ></A ><P ><B >Beispiel 25-1. Verwenden eines hashed Passwortfeldes</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// Speichern des Passwort hash $query = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');", addslashes($username), md5($password)); $result = pg_exec($connection, $query); // Afragen, ob der User das richtige Passwort ⁿbermittelt hat $query = sprintf("SELECT 1 FROM users WHERE name='%s' AND pwd='%s';", addslashes($username), md5($password)); $result = pg_exec($connection, $query); if (pg_numrows($result) > 0) { echo "Welcome, $username!"; } else { echo "Authentication failed for $username."; }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="security.database.sql-injection" >SQL Injection</A ></H2 ><P >
Viele Entwickler sind sich nicht bewusst, wie man sich an SQL Abfragen zu schaffen machen kann und nehmen an, dass eine SQL Abfrage ein vertrauenswⁿrdiges Kommando ist. Das hei▀t, dass SQL Abfragen Zugriffskontrollen hinters Licht fⁿhren, und dadurch Standard Authentifizierungs- und Authorisationschecks umgehen k÷nnen, und manchmal k÷nnen SQL Abfragen sogar Zugriff zu Kommandos auf Betriebssystemebene erlauben. </P ><P >
Direkt SQL Command Injection ist eine Technik, wo ein Angreifer SQL Kommandos erstellt oder existierende verΣndert, um versteckte Daten sichtbar zu machen, wertvolle Daten zu ⁿberschreiben, oder sogar gefΣhrliche Kommandos auf Systemebene des Datenbank-Hosts auszufⁿhren. Dies wird durch die Applikation erreicht, welche den Input des Benutzers mit statischen Parametern kombiniert, um eine SQL Abfrage zu erstellen. Die folgenden Beispiele basieren - leider - auf wahren Begebenheiten. </P ><P >
Dank dem Mangel an Input Validierungen, und dem Verbinden zum Datenbankserver als ein Superuser oder jemand der Benutzer anlegen kann, kann ein Angreifer einen Superuser in Ihrer Datenbank anlegen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5725" ></A ><P ><B >Beispiel 25-2. Die Ergebnisliste in mehrere Seiten aufsplitten ... und Superuser anlegen (PostgreSQL and MySQL) </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$offset = argv[0]; // Vorsicht, keine Validierung des Input ! $query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"; // mit PostgreSQL $result = pg_exec($conn, $query); // mit MySQL $result = mysql_query($query);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Normale Benutzer klicken auf die 'nΣchste' bzw. 'vorige' Links, wo <VAR CLASS="varname" >$offset</VAR > in der URL enthalten ist. Das Skript erwartet, dass die ankommende <VAR CLASS="varname" >$offset</VAR > einen Dezimalwert enthΣlt. Ganz gleich, jemand versucht einzubrechen, indem er das folgende in einer <A HREF="#function.urlencode" ><B CLASS="function" >urlencode()</B ></A >'d Form an die URL anhΣngt <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5731" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >// Im Fall von PostgreSQL 0; insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd) select 'crack', usesysid, 't','t','crack' from pg_shadow where usename='postgres'; -- // Im Fall von MySQL 0; UPDATE user SET Password=PASSWORD('crack') WHERE user='root'; FLUSH PRIVILEGES;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Wenn es passiert ist, wⁿrde ihm das Skript einen Zugriff als Superuser prΣsentieren. Beachten Sie, dass <VAR CLASS="literal" >0;</VAR > ein gⁿltiges Offset zur ursprⁿnglichen Abfrage liefert, und sie beendet. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Es ist eine ⁿbliche Technik, den SQL Parser mittels dem Kommentarzeichen in SQL <VAR CLASS="literal" >--</VAR > zu zwingen, den Rest der vom Entwickler geschriebenen Abfrage zu ignorieren. </P ></BLOCKQUOTE ></DIV ><P >
Ein gangbarer Weg um Passw÷rter zu finden ist, Ihre Seiten mit den Suchergebnissen hinters Licht zu fⁿhren. Der Angreifer braucht nur zu probieren, ob irgendeine ⁿbertragene Variable, die in dem SQL Statement verwendet wird, nicht richtig gehandhabt wird. Diese Filter k÷nnen gew÷hnlich in einer vorausgehenden Form gesetzt werden, indem <VAR CLASS="literal" >WHERE, ORDER BY, LIMIT</VAR > und <VAR CLASS="literal" >OFFSET</VAR > Klauseln in <VAR CLASS="literal" >SELECT</VAR > Statements umgebaut werden. Wenn Ihre Datenbank das <VAR CLASS="literal" >UNION</VAR > Konstrukt unterstⁿtzt, kann der Angreifer versuchen, eine komplette Abfrage an das Original anzuhΣngen, um Pa▀w÷rter aus einer willkⁿrlichen Tabelle aufzulisten. Die Verwendung von verschlⁿsselten Passwortfeldern wird ausdrⁿcklich empfohlen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5742" ></A ><P ><B >Beispiel 25-3. Artikel auflisten ... und ein paar Passw÷rter (irgendein Datenbankserver) </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$query = "SELECT id, name, inserted, size FROM products WHERE size = '$size' ORDER BY $order LIMIT $limit, $offset;"; $result = odbc_exec($conn, $query);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Der statische Teil der Abfrage kann mit einem anderen <VAR CLASS="literal" >SELECT</VAR > Statement kombiniert werden, welches alle Passw÷rter preisgibt <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5746" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >' union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable; --</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Wenn diese Abfrage (mit dem <VAR CLASS="literal" >'</VAR > und <VAR CLASS="literal" >--</VAR >) einer der in <VAR CLASS="varname" >$query</VAR > verwendeten Variablen zugewiesen wⁿrde, wΣre das "Abfragebiest" erwacht. </P ><P >
SQL UPDATEs sind ebenfalls ein Anlass, Ihre Datenbank anzugreifen. Diese Abfragen sind auch durch das ─ndern und AnhΣngen einer komplett neuen Abfrage gefΣhrdet. Aber der Angreifer k÷nnte auch mit der <VAR CLASS="literal" >SET</VAR > Klausel herumspielen. In diesem Fall muss eine Schemainformation vorhanden sein, um die Abfrage erfolgreich manipulieren zu k÷nnen. Diese kann durch Untersuchen der Variablennamen im Formular, oder simpel mittels brute force gesammelt werden. Es gibt nicht so viele Namenskonventionen fⁿr Felder, welche Passw÷rter oder Benutzernamen speichern. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5753" ></A ><P ><B >Beispiel 25-4. Vom Zurⁿcksetzen eines Passwortes ... zum Erlangen von mehr Rechten (irgendein Datenbankserver) </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$query = "UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';";</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Aber ein b÷swilliger Benutzer ⁿbermittelt den Wert <VAR CLASS="literal" >' or uid like'%admin%'; --</VAR > zu <VAR CLASS="varname" >$uid</VAR >, um das Administrator Passwort zu Σndern, oder setzt einfach <VAR CLASS="varname" >$pwd</VAR > auf <VAR CLASS="literal" >"hehehe', admin='yes', trusted=100 "</VAR > (mit dem hinteren Leerzeichen), um mehr Rechte zu erhalten. Dann wird die Abfrage verdreht: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5760" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// $uid == ' or uid like'%admin%'; -- $query = "UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --"; // $pwd == "hehehe', admin='yes', trusted=100 " $query = "UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE ...;"</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Ein furchterregendes Beispiel, wie der Zugriff auf Kommandos auf Betriebssystemebene bei manchen Datenbankservern erfolgen kann. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5763" ></A ><P ><B >Beispiel 25-5. Angriff auf das Betriebssystem des Datenbank Hosts (MSSQL Server)</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$query = "SELECT * FROM products WHERE id LIKE '%$prod%'"; $result = mssql_query($query);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Wenn ein Angreifer den Wert <VAR CLASS="literal" >a%' exec master..xp_cmdshell 'net user test testpass /ADD' --</VAR > zu <VAR CLASS="varname" >$prod</VAR > ⁿbertrΣgt, wird <VAR CLASS="varname" >$query</VAR > zu: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN5769" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$query = "SELECT * FROM products WHERE id LIKE '%a%' exec master..xp_cmdshell 'net user test testpass /ADD'--"; $result = mssql_query($query);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Der MSSQL Server fⁿhrt die SQL Statements in dem Batch aus, inklusive einem Kommando zum Anlegen eines neuen Benutzers in der Datenbank Accounts. Wⁿrde diese Applikation als <VAR CLASS="literal" >sa</VAR > und der MSSQLSERVER Service mit genⁿgend Rechten laufen, hΣtte der Angreifer nun ein Konto, mit welchem er Zugriff auf diese Maschine hΣtte. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Manche der obigen Beispiele sind an einen spezifischen Datenbankserver gebunden. Das hei▀t jedoch nicht, dass nicht ein Σhnlicher Angriff auf andere Produkte m÷glich wΣre. Ihr Datenbankserver k÷nnte auf andere Weise genauso verwundbar sein. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><HR><H3 CLASS="sect2" ><A NAME="security.database.avoiding" >Techniken zur Vermeidung</A ></H3 ><P >
Sie k÷nnten sich nun darauf berufen, dass der Angreifer in den meisten Beispielen ein Stⁿck Information ⁿber das Datenbankschema haben muss. Sie haben recht, aber Sie wissen nie, wann und wie es genommen werden kann, und wenn es passiert, kann Ihre Datenbank entbl÷▀t werden. Wenn Sie ein Open Source, oder ÷ffentlich verfⁿgbares Paket zur Handhabung von Datenbanken verwenden, welches vielleicht zu einem Content Management System oder Forum geh÷rt, k÷nnen Eindringlinge leicht eine Kopie eines Stⁿcks Ihres Codes erstellen. Es kann auch ein Sicherheitsrisiko sein, wenn es sich um ein schlecht designtes Paket handelt. </P ><P >
Diese Angriffe basieren hauptsΣchlich auf dem Ausnutzen des Codes, welcher ohne Bedenken auf die Sicherheit geschrieben wurde. Vertrauen Sie nie auf irgendeine Art von Input, speziell wenn er von der Clientseite kommt, selbst wenn er von einer Auswahlbox, einem versteckten Eingabefeld, oder einem Cookie kommt. Das erste Beispiel zeigt, dass solch eine untadelige Abfrage ein Disaster anrichten kann. </P ><P ></P ><UL ><LI ><P >
Stellen Sie nie als Superuser oder Owner einer Datenbank eine Verbindung zur Datenbank her. Verwenden Sie immer speziell angelegte Benutzer mit sehr limitierten Rechten. </P ></LI ><LI ><P >
Prⁿfen Sie, ob der gegebene Input dem erwarteten Datentyp entspricht. PHP bietet eine gro▀e Auswahl an Funktionen zum Validieren des Input, von den einfachsten unter <A HREF="#ref.variables" >Variablenfunktionen</A > und <A HREF="#ref.ctype" >Character Type Functions</A > (z.B. <A HREF="#function.is-numeric" ><B CLASS="function" >is_numeric()</B ></A > bzw. <A HREF="#function.ctype-digit" ><B CLASS="function" >ctype_digit()</B ></A >), bis hin zu den <A HREF="#ref.pcre" >Perl kompatiblen RegulΣren Ausdrⁿcken</A >. </P ></LI ><LI ><P >
Wenn die Applikation numerischen Input erwartet, erwΣgen Sie die Prⁿfung der Daten mit <A HREF="#function.is-numeric" ><B CLASS="function" >is_numeric()</B ></A >, oder die ─nderung des Typs mit <A HREF="#function.settype" ><B CLASS="function" >settype()</B ></A >, oder verwenden Sie die numerische ReprΣsentation mittels <A HREF="#function.sprintf" ><B CLASS="function" >sprintf()</B ></A >. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5793" ></A ><P ><B >Beispiel 25-6. Ein sicherer Weg, eine Abfrage zu erstellen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >settype($offset, 'integer'); $query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"; // Beachten Sie %d im Formatstring, %s zu verwenden wΣre sinnlos $query = sprintf("SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;", $offset);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></LI ><LI ><P >
Escapen Sie jeden nicht numerischen Input, welcher zur Datenbank weitergereicht werden soll mit <A HREF="#function.addslashes" ><B CLASS="function" >addslashes()</B ></A > oder <A HREF="#function.addcslashes" ><B CLASS="function" >addcslashes()</B ></A >. Siehe auch <A HREF="#security.database.storage" >das erste Beispiel</A >. Wie dieses Beispiel zeigt, sind in den statischen Teil der Abfrage eingebrachten Escapes nicht genug, und k÷nnen leicht gehacked werden. </P ></LI ><LI ><P >
Geben Sie keinerlei datenbankspezifische Informationen aus, speziell ⁿber das Schema, egal wie (auf ehrliche oder unehrliche Weise). Siehe auch <A HREF="#security.errors" >Fehlerbehandlung</A > und <A HREF="#ref.errorfunc" >Error Handling and Logging Functions</A >. </P ></LI ><LI ><P >
Sie k÷nnen stored procedures und vorher definierte Cursor verwenden, um den Datenzugriff zu abstrahieren, sodass Benutzer nicht direkt auf Tabellen oder Views zugreifen, aber diese L÷sung hat andere Auswirkungen. </P ></LI ></UL ><P >
Abgesehen davon profitieren Sie von einer Protokollierung der Abfragen entweder in Ihrem Skript, oder durch die Datenbank selbst, wenn es hilft. Klar, die Protokollierung kann nicht irgendeinen schΣdlichen Versuch verhindern, aber es kann helfen herauszufinden, welche Applikation umgangen wurde. Das Log ist durch die in ihm enthaltene Information nⁿtzlich, und je mehr Details es enthΣlt, desto besser ist es im Allgemeinen. </P ></DIV ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.errors" >Kapitel 26. Fehlerbehandlung</A ></H1 ><P >
PHP Security hat zwei Seiten der Fehlerbehandlung. Eine ist fⁿr die Erh÷hung der Sicherheit vorteilhaft, die andere ist schΣdlich. </P ><P >
Eine Standard-Angriffstaktik beinhaltet die Erstellung eines Profils des anzugreifenden Systems, indem die aufgrund der Einspeisung von unzulΣssigen Daten zurⁿckgegebenen Fehlermeldungen anhand deren Art und des Kontextes ausgewertet werden. Wenn z.B. ein Angreifer Informationen ⁿber eine auf einem eingesendeten Formular basierte Seite zusammengetragen hat, kann er versuchen, Variablen zu ⁿberschreiben bzw. zu modifizieren: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5812" ></A ><P ><B >Beispiel 26-1. Variablen mit einer eigenen HTML Seite angreifen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><form method="post" action="attacktarget?username=badfoo&password=badfoo"> <input type="hidden" name="username" value="badfoo"> <input type="hidden" name="password" value="badfoo"> </form></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Die normalerweise zurⁿckgegebenen PHP Fehler k÷nnen fⁿr den Entwickler hilfreich sein, wenn dieser ein Skript debuggen m÷chte, Hinweise auf eine nicht korrekt arbeitende Funktion oder Datei, oder die PHP Datei und die Zeilennummer des aufgetretenen Fehlers anzeigen lassen m÷chte. Das sind alles Informationen, die ausgenutzt werden k÷nnen. Es ist fⁿr einen PHP Entwickler nicht unⁿblich, <A HREF="#function.show-source" ><B CLASS="function" >show_source()</B ></A >, <A HREF="#function.highlight-string" ><B CLASS="function" >highlight_string()</B ></A >, oder <A HREF="#function.highlight-file" ><B CLASS="function" >highlight_file()</B ></A > zur Fehlersuche zu verwenden, jedoch kann dies in einem lebenden System auch versteckte Variablen, ungeprⁿfte Syntax und andere gefΣhrliche Informationen aufdecken. Speziell gefΣhrlich ist es, Code von bekannten Quellen mit integrierten Debugging Handlern auszufⁿhren, oder weit verbreitete Debuggingtechniken zu verwenden. Wenn ein Angreifer die von Ihnen benutzte generelle Technik herausfindet, kann er versuchen, mit Brute-Force Ihre Seite zu knacken, indem er verschiedene allgemein gebrΣuchliche Debug Strings sendet: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5819" ></A ><P ><B >Beispiel 26-2. Ausnutzen von gebrΣuchlichen Debugging Variablen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><form method="post" action="attacktarget?errors=Y&showerrors=1"&debug=1"> <input type="hidden" name="errors" value="Y"> <input type="hidden" name="showerrors" value="1"> <input type="hidden" name="debug" value="1"> </form></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Ungeachtet der Fehlerbehandlungsmethode fⁿhrt die M÷glichkeit ein System nach Fehlermeldungen zu sondieren dazu, dass einem Angreifer mehr Informationen geboten werden. </P ><P >
Zum Beispiel weist schon alleine der Stil einer Fehlermeldung darauf hin, dass auf einem System PHP lΣuft. Wenn der Angreifer auf eine .html Seite kommt und untersuchen m÷chte welches System im Hintergrund lΣuft (um nach bekannten SystemschwΣchen zu suchen), k÷nnte dieser mittels der Einspeisung von falschen Daten herausfinden, dass ein System mit PHP aufgebaut ist. </P ><P >
Ein Fehler einer Funktion gibt Aufschluss darⁿber, ob ein System eine bestimmte Datenbankapplikation benutzt, oder gibt Hinweise darauf, wie eine Webseite programmiert bzw. entworfen wurde. Dies erlaubt eine tiefere ▄berprⁿfung von offenen Datenbank-Ports, oder die Suche nach spezifischen Bugs bzw. SchwΣchen einer Webseite. Mit der Einspeisung von falschen Daten kann ein Angreifer z.B. die Reihenfolge der Authentifizierung in einem Skript bestimmen (anhand der Zeilennummern in den Fehlermeldungen), wie auch durch "Herumstochern" Missbrauchsm÷glichkeiten an verschiedenen Stellen im Script herausfinden. </P ><P >
Eine Fehlermeldung des Dateisystems oder eines generellen PHP-Errors welche Rechte der Server hat, wie auch die Struktur und Organisation der Dateien auf dem Webserver. Vom Entwickler geschriebene Fehlermeldungen k÷nnen das Problem verschlimmern, bis hin zum Preisgeben von zuvor "versteckten" Informationen. </P ><P >
Es gibt drei bedeutende L÷sungen zu diesem Thema. Die erste ist, alle Funktionen zu ⁿberprⁿfen und zu versuchen, die Menge an Fehlermeldungen zu ersetzen. Die zweite ist, die Ausgabe von Fehlermeldungen am laufenden Code generell zu deaktivieren. Die dritte ist, sich unter Verwendung der PHP Funktionen zur Fehlerbehandlung seinen eigenen Error-handler zu schreiben. AbhΣngig von Ihrer Sicherheitspolitik k÷nnte jede der drei L÷sungen fⁿr Sie geeignet sein. </P ><P >
Ein Weg, diesen Punkt vorzeitig zu behandeln ist, das PHP eigene <A HREF="#function.error-reporting" ><B CLASS="function" >error_reporting()</B ></A > zu benutzen, um Ihren Code sicherer zu gestalten und m÷glicherweise gefΣhrliche Nutzungen von Variablen zu entdecken. Wenn Sie Ihren Code noch vor dem Einsatz mit E_ALL testen, k÷nnen Sie Bereiche entdecken, in denen Ihre Variablen eventuell fⁿr Verseuchung oder andere Modifikationen offen sind. Sind Sie bereit zum Einsatz, k÷nnen Sie Ihren Code mit E_NONE vor Sondierungen schⁿtzen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5829" ></A ><P ><B >Beispiel 26-3. GefΣhrliche Variablen mit E_ALL finden</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($username) { // Vor Verwendung nicht initialisiert oder geprⁿft $good_login = 1; } if ($good_login == 1) { // Wenn der obige Test fehlschlΣgt, ist vor der // Verwendung nicht initialisiert oder geprⁿft fpassthru ("/highly/sensitive/data/index.html"); } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.globals" >Kapitel 27. Verwendung von Register Globals</A ></H1 ><P >
Ein Feature von PHP zur Erh÷hung der Sicherheit ist die Konfiguration von PHP mit <A HREF="#ini.register-globals" >register_globals</A > = off. Mit Deaktivierung der M÷glichkeit, irgendeine vom Benutzer ⁿbertragenen Variable in den PHP Code zu injizieren, k÷nnen Sie die Anzahl "vergifteter" Variablen reduzieren, welche ein potentieller Angreifer zufⁿgen k÷nnte. Dieser ben÷tigt mehr Zeit, um sich ▄bermittlungen auszudenken, und Ihre internen Variablen sind effektiv von den ⁿbergebenen Benutzervariablen isoliert. </P ><P >
WΣhrend dies den ben÷tigten Aufwand mit PHP zu arbeiten leicht erh÷ht ist dargelegt, dass die Vorteile gegenⁿber dem Aufwand klar ⁿberwiegen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5837" ></A ><P ><B >Beispiel 27-1. Mit register_globals=on arbeiten</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($username) { // kann vom User mit get/post/cookies ⁿbermittelt werden $good_login = 1; } if ($good_login == 1) { // kann vom User mit get/post/cookies ⁿbermittelt werden fpassthru ("/highly/sensitive/data/index.html"); } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5840" ></A ><P ><B >Beispiel 27-2. Mit register_globals = off arbeiten</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if($_COOKIE['username']){ // kann nur von einem Cookie kommen $good_login = 1; fpassthru ("/highly/sensitive/data/index.html"); } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Dies weise genutzt ist es auch m÷glich, prΣventive Messungen durchzufⁿhren, um bei versuchten Vorst÷▀en zu warnen. Wenn Sie im Voraus wissen, woher eine Variable kommen soll, k÷nnen Sie prⁿfen, ob die ⁿbermittelten Daten nicht einen unpassenden Weg genommen haben. Obwohl dies nicht garantiert, dass Daten nicht nur ausgedacht sind, erfordert es von einem Angreifer, auch den richtigen Weg zu finden. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5843" ></A ><P ><B >Beispiel 27-3. Entdecken einfacher Manipulationen von Variablen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($_COOKIE['username'] && !$_POST['username'] && !$_GET['username'] ) { // Durchfⁿhren anderer Checks, ob der Benutzername gⁿltig ist... $good_login = 1; fpassthru ("/highly/sensitive/data/index.html"); } else { mail("admin@example.com", "Possible breakin attempt", $_SERVER['REMOTE_ADDR']); echo "Security violation, admin has been alerted."; exit; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Natⁿrlich bedeutet ein einfaches Deaktivieren von register_globals nicht, dass Ihr Code nun automatisch sicher ist. Jeder Teil mit Daten sollte auch auf andere Arten geprⁿft werden. ▄berprⁿfen Sie immer die Eingaben von Benutzern und initialisieren Sie Ihre Variablen! Um Ihre Skripte auf nicht initialisierte Variablen zu prⁿfen, k÷nnen Sie <A HREF="#function.error-reporting" ><B CLASS="function" >error_reporting()</B ></A > so einstellen, dass Fehler des Levels <TT CLASS="constant" ><B >E_NOTICE</B ></TT > angezeigt werden. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Superglobals: Hinweis zur Verfⁿgbarkeit: </B >Seit PHP 4.1.0 stehen superglobale Arrays wie <VAR CLASS="varname" >$_GET</VAR >, <VAR CLASS="varname" >$_POST</VAR >,<VAR CLASS="varname" >$_SERVER</VAR >, etc. zur Verfⁿgung. Weitere Informationen k÷nnen Sie dem Abschnitt zu <A HREF="#language.variables.predefined" >superglobals</A > entnehmen.</P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.variables" >Kapitel 28. Vom Nutzer ⁿbermittelte Daten</A ></H1 ><P >
In vielen PHP Programmen liegt die gr÷▀te SchwΣche nicht an der Sprache selbst, sondern blo▀ an Code, der nicht mit dem n÷tigen Augenmerk auf die Sicherheit geschrieben wurde. Deshalb sollten Sie sich immer Zeit nehmen, die Implikationen eines gegebenen Codestⁿcks zu bedenken, um einen m÷glichen Schaden durch eine eventuell unerwartete ⁿbergebene Variable festzustellen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5858" ></A ><P ><B >Beispiel 28-1. GefΣhrliche Verwendung von Variablen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // l÷sche eine Datei aus dem Benutzer-Verzeichnis... // oder vielleicht dem eines anderen Benutzers? unlink ($evil_var); // Schreibe die Log-Information von deren Zugriff... // oder vielleicht einen /etc/passwd Eintrag? fputs ($fp, $evil_var); // Fⁿhre etwas triviales aus... oder rm -rf *? system ($evil_var); exec ($evil_var); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Sie sollten Ihren Code immer sorgfΣltig kontrollieren, um eine sorgfΣltige Prⁿfung irgendwelcher von einem Web-Browser ⁿbertragenen Variablen sicherzustellen, und sich selbst folgende Fragen stellen: <P ></P ><UL ><LI ><P >
Wird sich dieses Skript nur auf die vorgesehenen Dateien auswirken? </P ></LI ><LI ><P >
Kann auf ungew÷hnliche oder unerwⁿnschte Daten reagiert werden? </P ></LI ><LI ><P >
Kann dieses Skript auf nicht vorgesehene Art genutzt werden? </P ></LI ><LI ><P >
Kann dies in Verbindung mit anderen Skripten in einer negativen Art benutzt werden? </P ></LI ><LI ><P >
Werden alle Transaktionen ausreichend geloggt? </P ></LI ></UL > Wenn Sie sich diese Fragen anstatt danach schon wΣhrend dem Schreiben des Skriptes stellen, ersparen Sie sich das unangenehme Umschreiben, wenn eine Erh÷hung der Sicherheit erforderlich wird. Mit dieser Art zu denken werden Sie die Sicherheit des Systems zwar nicht garantieren, aber sie k÷nnen helfen, sie zu erh÷hen. </P ><P >
Sie k÷nnten auch ▄berlegungen anstellen, ob Sie nicht register_globals, magic_quotes, oder andere bequeme Einstellungen abschalten, welche Sie in Punkto Gⁿltigkeit, Herkunft, oder Inhalt einer gegebenen Variable durcheinanderbringen k÷nnten. Mit PHP im error_reporting(E_ALL) Modus zu arbeiten kann auch helfen, Sie vor Variablen zu warnen, welche benutzt werden, bevor sie geprⁿft oder initialisiert wurden (so k÷nnen Sie verhindern, dass mit ungew÷hnlichen Daten gearbeitet wird). </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.hiding" >Kapitel 29. Verstecken von PHP</A ></H1 ><P >
Generell ist Sicherheit durch Unklarheit eine der schwΣchsten Formen von Sicherheit. Aber in manchen FΣllen ist ein klein Wenig mehr an extra Sicherheit wⁿnschenswert. </P ><P >
Ein paar einfache Techniken helfen PHP zu Verstecken, um einen nach SchwΣchen in Ihrem System suchenden Angreifer m÷glicherweise langsamer zu machen. Wenn Sie in Ihrer <TT CLASS="filename" >php.ini</TT > expose_php = off setzen, reduzieren Sie damit die ihm zur Verfⁿgung stehenden Informationen. </P ><P >
Eine andere Taktik ist, den Webserver wie z.B. Apache entweder mittels einer <TT CLASS="filename" >.htaccess</TT > Direktive oder in der Apache Konfigurationsdatei selbst so konfigurieren, dass dieser verschiedene Dateitypen durch PHP parst. So k÷nnen Sie irrefⁿhrende Dateierweiterungen verwenden: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5880" ></A ><P ><B >Beispiel 29-1. PHP als andere Sprache ausgeben</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" ># Lasse PHP Code wie andere Arten von Code aussehen AddType application/x-httpd-php .asp .py .pl</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Oder komplett unklar machen: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5883" ></A ><P ><B >Beispiel 29-2. Verwenden von unbekannten Typen fⁿr PHP Dateierweiterungen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" ># Lasse PHP Code wie unbekannte Typen aussehen AddType application/x-httpd-php .bop .foo .133t</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Oder verstecken Sie ihn als html Code, was einen leichten Performanceverlust bedeutet, da alle html Dateien durch die PHP Engine geparst werden: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5886" ></A ><P ><B >Beispiel 29-3. Verwenden von html Typen fⁿr PHP Dateierweiterungen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="apache-conf" ># Lasse PHP code wie html aussehen AddType application/x-httpd-php .htm .html</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Um dies effektiv arbeiten zu lassen, mⁿssen Sie Ihre PHP Dateien nach den obigen Dateierweiterungen umbenennen. WΣhrend dies eine Form der Sicherheit durch Verhⁿllung ist, ist es ein kleines prΣventives Ma▀ mit ein paar Nachteilen. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="security.current" >Kapitel 30. Aktuell bleiben</A ></H1 ><P >
PHP wird, wie jedes andere gro▀e System auch, stΣndig geprⁿft und weiterentwickelt. Jede neue Version wird oft sowohl gr÷▀e, als auch kleine ─nderungen enthalten, welche die Gesamtsicherheit und StabilitΣt des Systems betreffenden Punkte (z.B. Sicherheit, Konfiguration) verbessern bzw. vorhandene MΣngel beheben. </P ><P >
Wie auch bei anderen Scriptsprachen und Programmen auf Systemebene ist der beste Ansatz ein oftmaliges Update, und sich laufend ⁿber die letzten Versionen und deren ─nderungen zu informieren. </P ></DIV ></DIV ><DIV CLASS="PART" ><A NAME="features" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >V. Features</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT >31. <A HREF="#features.http-auth" >HTTP-Authentifizierung mit PHP</A ></DT ><DT >32. <A HREF="#features.cookies" >Cookies</A ></DT ><DT >33. <A HREF="#features.xforms" >Dealing with XForms</A ></DT ><DT >34. <A HREF="#features.file-upload" >Steuerung von Dateiuploads</A ></DT ><DT >35. <A HREF="#features.remote-files" >Zugriff auf entfernte Dateien</A ></DT ><DT >36. <A HREF="#features.connection-handling" >Verbindungssteuerung</A ></DT ><DT >37. <A HREF="#features.persistent-connections" >Persistente Datenbankverbindungen</A ></DT ><DT >38. <A HREF="#features.safe-mode" >Safe Mode</A ></DT ><DT >39. <A HREF="#features.commandline" >PHP auf der Kommandozeile</A ></DT ></DL ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.http-auth" >Kapitel 31. HTTP-Authentifizierung mit PHP</A ></H1 ><P >
Die HTTP-Authentifizierung durch PHP ist nur verfⁿgbar, wenn PHP als Apache-Modul lΣuft und funktioniert daher nicht mit der CGI-Version. In einem PHP-Skript fⁿr ein Apache-Modul kann man die Funktion <A HREF="#function.header" ><B CLASS="function" >header()</B ></A > benutzen, um die Nachricht "Authentifizierung notwendig" an den Client-Browser zu senden, damit dieser ein Fenster zur Eingabe von Benutzername/Passwort ÷ffnet. Hat der Benutzer diese eingegeben, wird die URL des PHP-Scripts mit den Variablen $PHP_AUTH_USER, $PHP_AUTH_PW und $PHP_AUTH_TYPE, die den jeweiligen Benutzernamen, das Passwort und den Typ der Identifizierung enthalten, erneut aufgerufen. Momentan wird nur das Authentifizierungsmodell "basic" unterstⁿtzt. NΣheres hierzu bei der <A HREF="#function.header" ><B CLASS="function" >header()</B ></A > Funktion. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung zur PHP Version: </B > <A HREF="#language.variables.superglobals" >Autoglobals</A >, wie <A HREF="#reserved.variables.server" >$_SERVER</A >, sind seit der PHP Version <A HREF="http://www.php.net/release_4_1_0.php" TARGET="_top" >4.1.0</A > verfⁿgbar. <VAR CLASS="varname" >$HTTP_SERVER_VARS</VAR > seit PHP 3. </P ></BLOCKQUOTE ></DIV ><P >
Ein Auszug aus einem Skript, das die Clientauthentifizierung auf einer Seite erzwingt, wⁿrde so aussehen: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5908" ></A ><P ><B >Beispiel 31-1. HTTP-Authentifizierung</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if (!isset($_SERVER['PHP_AUTH_USER'])) { Header("WWW-Authenticate: Basic realm=\"My Realm\""); Header("HTTP/1.0 401 Unauthorized"); echo "Text to send if user hits Cancel button\n"; exit; } else { echo "Hello {$_SERVER['PHP_AUTH_USER']}"; echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Hinweis: </B > Achten Sie bei den Header-Zeilen fⁿr maximale KompatibilitΣt auf die richtige Schreibweise! Das Schlⁿsselwort "Basic" sollte genau so geschrieben werden, der Realm-String muss in doppelte (nicht einfache) Anfⁿhrungszeichen eingeschlossen sein, und in der "HTTP/1.0 401"-Zeile darf nur genau ein Leerzeichen vor dem 401-Code stehen. </P ></BLOCKQUOTE ></DIV ><P >
Anstatt $_SERVER['PHP_AUTH_USER'] und $_SERVER['PHP_AUTH_PW'] einfach nur auszugeben, werden Sie den Benutzernamen und das Passwort auf Gⁿltigkeit ⁿberprⁿfen wollen. Dies kann durch die Abfrage einer Datenbank oder das Einlesen einer Textdatei geschehen. </P ><P >
Vorsicht bei einigen Internet Explorer-Versionen - sie scheinen sehr wΣhlerisch zu sein, was die Reihenfolge der Header angeht. Abhilfe schafft hier das Senden des <SPAN CLASS="emphasis" ><I CLASS="emphasis" >WWW-Authenticate</I ></SPAN > Headers vor dem <SPAN CLASS="errorcode" >HTTP/1.0 401</SPAN >. </P ><P >
Um zu unterbinden, dass ein Skript das Passwort einer durch einen traditionellen externen Mechanismus geschⁿtzten Seite ausliest, werden die PHP_AUTH Variablen nicht gesetzt, wenn eine externe Authentifizierung fⁿr diese bestimmte Seite aktiviert ist. In diesem Fall kann die $_SERVER['REMOTE_USER'] Variable benutzt werden, um den Benutzer durch die externe Zugriffskontrolle zu identifizieren. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Konfigurationshinweis: </B > PHP prⁿft das Vorhandensein einer <VAR CLASS="literal" >AuthType</VAR > Apache-Direktive, um zu entscheiden, ob eine externe Authentifizierung aktiv ist. Vermeiden Sie deshalb diese Konfigurationsdirektive im Kontext der PHP-Authentifizierung (anderenfalls wird jeder Authentifizierungsversuch misslingen). </P ></BLOCKQUOTE ></DIV ><P >
Zu beachten ist, dass obenstehendes keinesfalls jemanden, der die Kontrolle ⁿber eine nichtgeschⁿtzte URL hat, davon abhalten kann, Passw÷rter von geschⁿtzten URLs auf dem gleichen Rechner auszulesen. </P ><P >
Sowohl Netscape als auch der Internet Explorer l÷schen den lokalen Authentifizierungscache des Browserfensters, wenn der Server eine 401-Meldung zurⁿckgibt. Dies kann benutzt werden, um einen Benutzer "auszuloggen" und eine erneute Eingabe des Benutzernamens/Passworts zu erzwingen. Manchmal wird dieses Verhalten fⁿr das automatische Ausloggen nach Ablauf einer bestimmten Zeitspanne oder fⁿr einen Logout-Button genutzt. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5925" ></A ><P ><B >Beispiel 31-2. HTTP-Authentifizierung, mit erneuter Anforderung von Name/Passwort</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function authenticate() { Header("WWW-Authenticate: Basic realm=\"Test Authentication System\""); Header("HTTP/1.0 401 Unauthorized"); echo "You must enter a valid login ID and password to access this resource\n"; exit; } if (!isset($_SERVER['PHP_AUTH_USER']) || ($SeenBefore == 1 && ! strcmp( $OldAuth, $_SERVER['PHP_AUTH_USER']))) { authenticate(); } else { echo "Welcome: {$_SERVER['PHP_AUTH_USER']}<br>"; echo "Old: $OldAuth"; echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n"; echo "<input type=\"hidden\" name=\"SeenBefore\" value=\"1\">\n"; echo "<input type=\"hidden\" name=\"OldAuth\" value=\"{$_SERVER['PHP_AUTH_USER']}\">\n"; echo "<input type=\"submit\" value=\"Re Authenticate\">\n"; echo "</form>\n"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Dieses Verhalten wird vom Authentifizierungsstandard HTTP Basic nicht gefordert, daher sollte man sich nie darauf verlassen. Tests mit Lynx haben gezeigt, dass Lynx die Authentifizierungsinformationen bei Erhalt einer 401-Meldung nicht l÷scht. Ein Klick auf den Zurⁿck- Button und danach auf VorwΣrts wird die angeforderte Adresse ÷ffnen (und zwar so lange, bis die Identifizierung der Benutzer geΣndert wird). </P ><P >
Weiterhin muss beachtet werden, dass dies unter dem Microsoft IIS mit der CGI-Version von PHP aufgrund einer EinschrΣnkung des IIS nicht funktioniert. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.cookies" >Kapitel 32. Cookies</A ></H1 ><P >
PHP unterstⁿtzt HTTP-Cookies. Hierbei handelt es sich um einen Mechanismus, um Informationen beim Client zu speichern und somit wiederkehrende Besucher zu identifizieren oder ihren Weg innerhalb des Angebotes nachzuvollziehen. Cookies k÷nnen durch die Funktion <A HREF="#function.setcookie" ><B CLASS="function" >setcookie()</B ></A > gesetzt werden. Sie sind Bestandteil des HTTP-Headers, was bedeutet, dass die Funktion <A HREF="#function.setcookie" ><B CLASS="function" >setcookie()</B ></A > aufgerufen werden muss, bevor irgendeine Ausgabe an den Browser erfolgt. Dies ist die gleiche EinschrΣnkung, der auch die Funktion <A HREF="#function.header" ><B CLASS="function" >header()</B ></A > unterliegt. Sie k÷nnen die <A HREF="#ref.outcontrol" >Funktionen zur Ausgabesteuerung</A > benutzen, um die Ausgaben des Skriptes zu verz÷gern, bis entschieden ist, ob Cookies gesetzt bzw. Header gesendet werden sollen oder nicht. </P ><P >
Vom Client gesendete Cookies werden - abhΣngig von den Konfigurationsvariablen <VAR CLASS="literal" >register_globals</VAR > und <VAR CLASS="literal" >variables_order</VAR > - automatisch in eine Variable geschrieben, wie es auch bei mittels GET oder POST gesendeten Daten geschieht. Sollen einem Cookie mehrere Werte zugewiesen werden, so muss dem Cookienamen lediglich <SPAN CLASS="emphasis" ><I CLASS="emphasis" >[]</I ></SPAN > angefⁿgt werden. </P ><P >
Seit PHP 4.1.0 werden alle vom Client gesendeten Cookies in dem auto-globalen Array <VAR CLASS="varname" >$_COOKIE</VAR > gespeichert. In Σlteren PHP-Versionen stehen diese Daten in <VAR CLASS="varname" >$HTTP_COOKIE_VARS</VAR > gespeichert, sofern die Konfigurationsvariable <VAR CLASS="literal" >track_vars</VAR > gesetzt ist. </P ><P >
Weitere Informationen sowie Anmerkungen zu Browser-Bugs finden Sie im Abschnitt <A HREF="#function.setcookie" ><B CLASS="function" >setcookie()</B ></A >. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.xforms" >Kapitel 33. Dealing with XForms</A ></H1 ><P >
<A HREF="http://www.w3.org/MarkUp/Forms/" TARGET="_top" >XForms</A > defines a variation on traditional webforms which allows them to be used on a wider variety of platforms and browsers or even non-traditional media such as PDF documents. </P ><P >
The first key difference in XForms is how the form is sent to the client. <A HREF="http://www.w3.org/MarkUp/Forms/2003/xforms-for-html-authors.html" TARGET="_top" >XForms for HTML Authors</A > contains a detailed description of how to create XForms, for the purpose of this tutorial we'll only be looking at a simple example. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5953" ></A ><P ><B >Beispiel 33-1. A simple XForms search form</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/2002/xforms"> <h:head> <h:title>Search</h:title> <model> <submission action="http://example.com/search" method="post" id="s"/> </model> </h:head> <h:body> <h:p> <input ref="q"><label>Find</label></input> <submit submission="s"><label>Go</label></submit> </h:p> </h:body> </h:html></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
The above form displays a text input box (named <VAR CLASS="parameter" >q</VAR >), and a submit button. When the submit button is clicked, the form will be sent to the page referred to by <VAR CLASS="literal" >action</VAR >. </P ><P >
Here's where it starts to look different from your web application's point of view. In a normal HTML form, the data would be sent as <VAR CLASS="literal" >application/x-www-form-urlencoded</VAR >, in the XForms world however, this information is sent as XML formatted data. </P ><P >
If you're choosing to work with XForms then you probably want that data as XML, in that case, look in <VAR CLASS="varname" >$HTTP_RAW_POST_DATA</VAR > where you'll find the XML document generated by the browser which you can pass into your favorite XSLT engine or document parser. </P ><P >
If you're not interrested in formatting and just want your data to be loaded into the traditional <VAR CLASS="varname" >$_POST</VAR > variable, you can instruct the client browser to send it as <VAR CLASS="literal" >application/x-www-form-urlencoded</VAR > by changing the <VAR CLASS="parameter" >method</VAR > attribute to <SPAN CLASS="emphasis" ><I CLASS="emphasis" >urlencoded-post</I ></SPAN >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5968" ></A ><P ><B >Beispiel 33-2. Using an XForm to populate <VAR CLASS="varname" >$_POST</VAR ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/2002/xforms"> <h:head> <h:title>Search</h:title> <model> <submission action="http://example.com/search" method="urlencoded-post" id="s"/> </model> </h:head> <h:body> <h:p> <input ref="q"><label>Find</label></input> <submit submission="s"><label>Go</label></submit> </h:p> </h:body> </h:html></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > As of this writing, many browsers do not support XForms. Check your browser version if the above examples fails. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.file-upload" >Kapitel 34. Steuerung von Dateiuploads</A ></H1 ><DIV CLASS="sect1" ><H2 CLASS="sect1" ><A NAME="features.file-upload.post-method" >Dateiuploads mit POST</A ></H2 ><P >
PHP kann Dateiuploads mit jedem RFC-1867 konformen Browser (dazu geh÷ren der Netscape Navigator 3 oder h÷her, Microsoft Internet Explorer 3 mit einem Patch von Microsoft oder h÷her ohne Patch) durchfⁿhren. Es k÷nnen sowohl Text- als auch BinΣrdaten hochgeladen werden. Mit PHP┤s Authentifizierungs- und Dateifunktionen besteht volle Kontrolle darⁿber, wer Dateien hochladen darf und was mit den Dateien geschehen soll, wenn der Upload beendet ist. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Diesbezⁿgliche Konfigurationshinweise: </B > Siehe auch die Anweisungen <A HREF="#ini.file-uploads" >file_uploads</A >, <A HREF="#ini.upload-max-filesize" >upload_max_filesize</A >, <A HREF="#ini.upload-tmp-dir" >upload_tmp_dir</A > und <A HREF="#ini.post-max-size" >post_max_size</A > in der <TT CLASS="filename" >php.ini</TT > </P ></BLOCKQUOTE ></DIV ><P >
PHP unterstⁿtzt auch Dateiuploads nach der PUT-Methode, die beispielsweise vom Netscape Composer und den W3C Amaya Clients benutzt wird. Siehe dazu <A HREF="#features.file-upload.put-method" >PUT-Unterstⁿtzung</A > fⁿr nΣhere Informationen. </P ><P >
Eine Maske fⁿr den Dateiupload kann erstellt werden, indem man ein Formular entwirft, das ungefΣhr so aussieht: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5990" ></A ><P ><B >Beispiel 34-1. Formular fⁿr den Dateiupload</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><form enctype="multipart/form-data" action="_URL_" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="30000"> Send this file: <input name="userfile" type="file"> <input type="submit" value="Send File"> </form></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Die _URL_ sollte auf eine PHP-Datei verweisen. Das versteckte Feld MAX_FILE_SIZE muss dem Dateieingabefeld vorangestellt werden und den Wert der maximal akzeptierten Dateigr÷▀e in Bytes enthalten. <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Die maximale Dateigr÷▀e MAX_FILE_SIZE ist fⁿr den Browser nur ein Hinweis und es ist leicht, diese Grenze zu umgehen. Also verlassen Sie sich nicht darauf, dass der Browser Ihrem Wunsch auch nachkommt! Wie auch immer, die PHP-Einstellungen fⁿr die maximale Dateigr÷▀e k÷nnen nicht getΣuscht werden. </P ></TD ></TR ></TABLE ></DIV > </P ><P >
Die fⁿr hochgeladene Dateien definierten Variablen sind je nach PHP Version und Konfiguration verschieden. Die Autoglobale <A HREF="#reserved.variables.files" >$_FILES</A > existiert seit PHP 4.1.0 und das Array <VAR CLASS="varname" >$HTTP_POST_FILES</VAR > seit PHP 4.0.0. Diese Arrays enthalten alle Informationen ⁿber Ihre hochgeladenen Dateien. Die Verwendung von <VAR CLASS="varname" >$_FILES</VAR > wird bevorzugt. Ist die PHP Anweisung <A HREF="#ini.register-globals" >register_globals</A > auf <SPAN CLASS="emphasis" ><I CLASS="emphasis" >on</I ></SPAN >, stehen auch entsprechende Variablennamen zur Verfⁿgung. Seit PHP <A HREF="http://www.php.net/release_4_2_0.php" TARGET="_top" >4.2.0</A > steht <A HREF="#ini.register-globals" >register_globals</A > standardmΣ▀ig auf <SPAN CLASS="emphasis" ><I CLASS="emphasis" >off</I ></SPAN >. </P ><P >
Im Folgenden sind die Inhalte von <A HREF="#reserved.variables.files" >$_FILES</A > aus unserem Beispielskript aufgelistet. Beachten Sie, dass dies auf der Annahme basiert, dass der Name des Dateiuploads wie in dem obigen Beispielskript <SPAN CLASS="emphasis" ><I CLASS="emphasis" >userfile</I ></SPAN > ist. <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><VAR CLASS="varname" >$_FILES['userfile']['name']</VAR ></DT ><DD ><P >
Der ursprⁿngliche Dateiname auf der Client Maschine. </P ></DD ><DT ><VAR CLASS="varname" >$_FILES['userfile']['type']</VAR ></DT ><DD ><P >
Der Mime-Type der Datei, falls der Browser diese Information zur Verfⁿgung gestellt hat. Ein Beispiel wΣre <VAR CLASS="literal" >"image/gif"</VAR >. </P ></DD ><DT ><VAR CLASS="varname" >$_FILES['userfile']['size']</VAR ></DT ><DD ><P >
Die Gr÷▀e der hochgeladenen Datei in Bytes. </P ></DD ><DT ><VAR CLASS="varname" >$_FILES['userfile']['tmp_name']</VAR ></DT ><DD ><P >
Der temporΣre Dateiname, unter dem die hochgeladene Datei auf dem Server gespeichert wurde. </P ></DD ><DT ><VAR CLASS="varname" >$_FILES['userfile']['error']</VAR ></DT ><DD ><P >
Der <A HREF="#features.file-upload.errors" >Fehlercode</A > im Zusammenhang mit dem hochladen der Datei. <SPAN CLASS="emphasis" ><I CLASS="emphasis" >['error']</I ></SPAN > wurde in PHP 4.2.0 eingefⁿhrt. </P ></DD ></DL ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In den Versionen vor PHP 4.1.0 war dies <VAR CLASS="varname" >$HTTP_POST_FILES</VAR >, und ist keine <A HREF="#language.variables.superglobals" >'Autoglobale'</A > Variable wie <VAR CLASS="varname" >$_FILES</VAR >. PHP 3 unterstⁿtzt <VAR CLASS="varname" >$HTTP_POST_FILES</VAR > nicht. </P ></BLOCKQUOTE ></DIV ><P >
Ist <A HREF="#ini.register-globals" >register_globals</A > in der <TT CLASS="filename" >php.ini</TT > aktiviert, stehen zusΣtzliche Variablen zur Verfⁿgung. Zum Beispiel entspricht <VAR CLASS="varname" >$userfile_name</VAR > <VAR CLASS="varname" >$_FILES['userfile']['name']</VAR >, <VAR CLASS="varname" >$userfile_type</VAR > entspricht <VAR CLASS="varname" >$_FILES['userfile']['type']</VAR >, etc. Beachten Sie, dass register_globals standardmΣ▀ig deaktiviert ist, jedoch wird empfohlen, sich nicht darauf zu verlassen. </P ><P >
StandardmΣ▀ig werden Dateien in dem vorgegebenen temporΣren Verzeichnis des Servers gespeichert, au▀er es wurde mittels <A HREF="#ini.upload-tmp-dir" >upload_tmp_dir</A > in der <TT CLASS="filename" >php.ini</TT > ein anderer Ort konfiguriert. Das Standardverzeichnis des Servers kann durch das Setzen der Umgebungsvariablen <VAR CLASS="envar" >TMPDIR</VAR > in der Umgebung, in der PHP ausgefⁿhrt wird, geΣndert werden. Das Setzen mittels der Funktion <A HREF="#function.putenv" ><B CLASS="function" >putenv()</B ></A > innerhalb eines Skriptes ist nicht m÷glich. Mittels dieser Umgebungsvariable kann auch sichergestellt werden, dass auch andere Operationen an hochgeladenen Dateien arbeiten k÷nnen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6054" ></A ><P ><B >Beispiel 34-2. Dateiuploads prⁿfen</B ></P ><P >
Weitere Informationen finden Sie auch in den Beschreibungen fⁿr <A HREF="#function.is-uploaded-file" ><B CLASS="function" >is_uploaded_file()</B ></A > und <A HREF="#function.move-uploaded-file" ><B CLASS="function" >move_uploaded_file()</B ></A >. Das folgende Beispiel verarbeitet einen von einem HTML-Formular kommenden Dateiupload. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // In PHP kleiner als 4.1.0 sollten Sie $HTTP_POST_FILES anstatt $_FILES verwenden. // In PHP kleiner als 4.0.3 verwenden Sie copy() und is_uploaded_file() anstatt von // move_uploaded_file() $uploaddir = '/var/www/uploads/'; print "<pre>"; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) { print "File is valid, and was successfully uploaded. Here's some more debugging info:\n"; print_r($_FILES); } else { print "Possible file upload attack! Here's some debugging info:\n"; print_r($_FILES); } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Das die hochgeladene Datei empfangende Skript sollte die notwendige Logik zur Entscheidung enthalten, was mit der hochgeladenen Datei geschehen soll. Sie k÷nnen zum Beispiel <VAR CLASS="varname" >$_FILES['userfile']['size']</VAR > benutzen, um zu kleine bzw. zu gro▀e Dateien wegzuwerfen. Sie k÷nnen <VAR CLASS="varname" >$_FILES['userfile']['type']</VAR > nutzen, um Dateien eines unerwⁿnschten Typs wegzuwerfen. Seit PHP 4.2.0 k÷nnen Sie Ihre Logik auch mittels <VAR CLASS="varname" >$_FILES['userfile']['error']</VAR > anhand der <A HREF="#features.file-upload.errors" >Fehlercodes</A > planen. Egal welche Logik Sie verwenden, Sie sollten die Datei in dem temporΣren Verzeichnis entweder l÷schen, oder an einen anderen Ort verschieben. </P ><P >
Wurde die Datei in dem temporΣren Verzeichnis nicht verschoben oder umbenannt, wird sie am Ende des Requests gel÷scht. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="features.file-upload.errors" >Fehlermeldungen erklΣrt</A ></H2 ><P >
Seit PHP 4.2.0 gibt PHP zusammen mit dem Datei-Array entsprechende Fehlermeldungen. Die Fehlermeldung befindet sich im Segment <SPAN CLASS="emphasis" ><I CLASS="emphasis" >['error']</I ></SPAN > des Datei-Arrays, welches wΣhrend des Hochladens der Datei erstellt wird. In anderen Worten kann der Fehler in <VAR CLASS="varname" >$_FILES['userfile']['error']</VAR > gefunden werden. </P ><P >
<P ></P ><DIV CLASS="variablelist" ><DL ><DT ><VAR CLASS="varname" >UPLOAD_ERR_OK</VAR ></DT ><DD ><P >
Wert: 0; Es liegt kein Fehler vor, die Datei wurde erfolgreich hochgeladen. </P ></DD ><DT ><VAR CLASS="varname" >UPLOAD_ERR_INI_SIZE</VAR ></DT ><DD ><P >
Wert: 1; Die hochgeladene Datei ⁿberschreitet die in der Anweisung <A HREF="#ini.upload-max-filesize" >upload_max_filesize</A > in <TT CLASS="filename" >php.ini</TT > festgelegte Gr÷▀e. </P ></DD ><DT ><VAR CLASS="varname" >UPLOAD_ERR_FORM_SIZE</VAR ></DT ><DD ><P >
Wert: 2; Die hochgeladene Datei ⁿberschreitet die in dem HTML Formular mittels der Anweisung <SPAN CLASS="emphasis" ><I CLASS="emphasis" >MAX_FILE_SIZE</I ></SPAN > angegebene maximale Dateigr÷▀e. </P ></DD ><DT ><VAR CLASS="varname" >UPLOAD_ERR_PARTIAL</VAR ></DT ><DD ><P >
Wert: 3; Die Datei wurde nur teilweise hochgeladen. </P ></DD ><DT ><VAR CLASS="varname" >UPLOAD_ERR_NO_FILE</VAR ></DT ><DD ><P >
Wert: 4; Es wurde keine Datei hochgeladen. </P ></DD ></DL ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Dies wurden Konstanten in PHP 4.3.0. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="features.file-upload.common-pitfalls" >HΣufige Probleme</A ></H2 ><P >
Der mit <VAR CLASS="literal" >MAX_FILE_SIZE</VAR > eingestellte Wert kann nicht gr÷▀er sein als der des ini-Parameters <A HREF="#ini.upload-max-filesize" >upload_max_filesize</A >. Der Standardwert ist 2 Megabyte. </P ><P >
Ist <VAR CLASS="literal" >memory limit</VAR > aktiviert, k÷nnte eine Erh÷hung von <A HREF="#ini.memory-limit" >memory_limit</A > n÷tig sein. Stellen Sie sicher, dass der Wert von <A HREF="#ini.memory-limit" >memory_limit</A > gro▀ genug ist. </P ><P >
Wenn <A HREF="#ini.max-execution-time" >max_execution_time</A > zu kurz konfiguriert ist, k÷nnte das Skript den Wert ⁿberschritten haben. Stellen Sie sicher, dass der Wert von <VAR CLASS="literal" >max_execution_time</VAR > gro▀ genug ist. </P ><P >
Ist <A HREF="#ini.post-max-size" >post_max_size</A > zu klein konfiguriert, k÷nnen gro▀e Dateien nicht hochgeladen werden. Stellen Sie sicher, dass der Wert von <VAR CLASS="literal" >post_max_size</VAR > gro▀ genug ist. </P ><P >
Nicht zu prⁿfen an welcher Datei Sie arbeiten kann bedeuten, dass Benutzer auf sensible Informationen in anderen Verzeichnissen Zugriff erhalten. </P ><P >
Beachten Sie, dass CERN httpd in dem vom Client erhaltenen Content-Type Mime Header alles nach dem ersten Whitespace wegzuschneiden scheint. Solange dies der Fall ist, unterstⁿtzt CERN httpd keine Dateiuploads. </P ><P >
Aufgrund der vielen m÷glichen Arten der Darstellung von Verzeichnissen k÷nnen wir nicht garantieren, dass Dateien mit exotischen Namen (wie z.B. mit enthaltenen Leerzeichen) auch wirklich richtig verarbeitet werden. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="features.file-upload.multiple" >Upload mehrerer Dateien</A ></H2 ><P >
Mehrere Dateien k÷nnen hochgeladen werden, indem Sie verschiedene Namen <VAR CLASS="literal" >name</VAR > fⁿr <VAR CLASS="literal" >input</VAR > verwenden. </P ><P >
Es ist ebenfalls m÷glich, mehrere Dateien simultan hochzuladen, und die Informationen automatisch in Arrays zu erhalten. Um dies zu tun, verwenden Sie in dem HTML Formular die gleiche Array-Sende-Syntax wie bei Auswahllisten mit Mehrfachauswahl und Checkboxen: </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die Unterstⁿtzung fⁿr den Upload von mehreren Dateien wurde in PHP 3.0.10 hinzugefⁿgt. </P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6130" ></A ><P ><B >Beispiel 34-3. Upload mehrerer Dateien</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><form action="file-upload.php" method="post" enctype="multipart/form-data"> Send these files:<br> <input name="userfile[]" type="file"><br> <input name="userfile[]" type="file"><br> <input type="submit" value="Send files"> </form></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Wenn das obige Formular ⁿbermittelt ist, werden die Arrays <VAR CLASS="varname" >$_FILES['userfile']</VAR >, <VAR CLASS="varname" >$_FILES['userfile']['name']</VAR > und <VAR CLASS="varname" >$_FILES['userfile']['size']</VAR > initialisiert (wie auch in $HTTP_POST_FILES vor PHP Version 4.1.0.). Ist <VAR CLASS="literal" >register_globals</VAR > aktiviert, werden auch globale Variablen fⁿr die hochgeladenen Dateien initialisiert. Jedes von ihnen ist ein numerisch indiziertes Array mit den entsprechenden Werten fⁿr die hochgeladenen Dateien. </P ><P >
Nehmen wir zum Beispiel an, dass die Dateinamen <TT CLASS="filename" >/home/test/review.html</TT > und <TT CLASS="filename" >/home/test/xwp.out</TT > ⁿbermittelt wurden. In diesem Fall wⁿrde <VAR CLASS="varname" >$_FILES['userfile']['name'][0]</VAR > <TT CLASS="filename" >review.html</TT > enthalten, und <VAR CLASS="varname" >$_FILES['userfile']['name'][1]</VAR > hΣtte den Wert <TT CLASS="filename" >xwp.out</TT >. Genauso wⁿrde <VAR CLASS="varname" >$_FILES['userfile']['size'][0]</VAR > die Dateigr÷▀e von <TT CLASS="filename" >review.html</TT > enthalten, usw. </P ><P >
<VAR CLASS="varname" >$_FILES['userfile']['name'][0]</VAR >, <VAR CLASS="varname" >$_FILES['userfile']['tmp_name'][0]</VAR >, <VAR CLASS="varname" >$_FILES['userfile']['size'][0]</VAR > und <VAR CLASS="varname" >$_FILES['userfile']['type'][0]</VAR > sind ebenfalls gesetzt. </P ></DIV ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="features.file-upload.put-method" >PUT-Unterstⁿtzung</A ></H2 ><P >
Die Unterstⁿtzung der PUT-Methode hat sich zwischen PHP 3 und PHP 4 geΣndert. In PHP 4 sollten Sie den standard Input-Datenstrom benutzen, um die Inhalte eines HTTP PUT zu lesen. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6156" ></A ><P ><B >Beispiel 34-4. Speicher von HTTP PUT Dateien mit PHP 4</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* PUT Daten kommen in den stdin Stream */ $putdata = fopen("php://stdin","r"); /* Eine Datei zum Schreiben ÷ffnen */ $fp = fopen("myputfile.ext","w"); /* Jeweils 1kB Daten lesen und in die Datei schreiben */ while ($data = fread($putdata,1024)) fwrite($fp,$data); /* Die Streams schlie▀en */ fclose($fp); fclose($putdata); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > SΣmtliche untenstehende Dokumentation betrifft nur PHP 3. </P ></BLOCKQUOTE ></DIV ><P >
PHP unterstⁿtzt die HTTP PUT-Methode, wie sie von Clients wie Netscape Composer oder W3C Amaya benutzt wird. PUT-Anfragen sind weitaus unkomplizierter als ein Dateiupload und sehen etwa so aus: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN6162" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >PUT /path/filename.html HTTP/1.1</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Das wⁿrde normalerweise bedeuten, dass der entfernte Client den folgenden Inhalt als /path/filename.html im Webverzeichnisbaum speichert. Natⁿrlich ist es keine gute Idee, dass PHP oder Apache jeden Benutzer beliebige Dateien ⁿberschreiben lassen. Um eine solche Anfrage bearbeiten zu k÷nnen, muss der Webserver erst angewiesen werden, ein bestimmtes PHP-Skript fⁿr die Abarbeitung aufzurufen. Im Apache wird dies durch die <SPAN CLASS="emphasis" ><I CLASS="emphasis" >Script</I ></SPAN > - Direktive festgelegt. Sie kann fast ⁿberall in der Apache-Konfigurationsdatei platziert werden, gebrΣuchlich ist die Platzierung innerhalb einer <Directory>- oder <Virtualhost>- Sektion. Eine Zeile wie die folgende erledigt dies: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN6166" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Script PUT /put.php</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Diese Zeile legt fest, dass Apache alle PUT-Anfragen fⁿr URIs, die dem Kontext entsprechen, in dem diese Zeile steht, an das put.php Skript weiterleitet. Dies setzt natⁿrlich voraus, dass PHP aktiv und fⁿr die .php-Dateierweiterung registriert ist. </P ><P >
Innerhalb der put.php-Datei k÷nnte folgendes stehen: </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN6171" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php copy($PHP_UPLOADED_FILE_NAME,$DOCUMENT_ROOT.$REQUEST_URI); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
Dies kopiert die Datei an den vom Client angegebenen Ort. M÷glicherweise sollen vor dem Kopieren der Datei noch ▄berprⁿfungen und/oder Benutzerauthentifizierung stattfinden. Wenn PHP eine PUT-Anfrage erhΣlt, wird die Datei genau wie bei der <A HREF="#features.file-upload.post-method" >POST-Methode</A > in einem temporΣren Verzeichnis gespeichert. Nach dem Abarbeiten der Anfrage wird die temporΣre Datei gel÷scht. Also muss das PHP-Skript, das die PUT-Anfrage bearbeitet, die Datei irgendwohin kopieren. Der Name der temporΣren Datei findet sich in der Variablen $PHP_PUT_FILENAME; der Name der zu speichernden Datei steht in $REQUEST_URI (kann bei Nicht-Apache Webservern variieren). Dieser Zieldateiname wird vom Client festgelegt. Man kann den Client auch umgehen; beispielsweise k÷nnten alle hochgeladenen Dateien in ein spezielles Upload-Directory kopiert werden. </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.remote-files" >Kapitel 35. Zugriff auf entfernte Dateien</A ></H1 ><P >
Wenn die Unterstⁿtzung fⁿr den "URL fopen wrapper" bei der Konfiguration von PHP vorhanden ist (welche standardmΣ▀ig eingestellt ist, es sei denn, die Option <VAR CLASS="option" >--disable-url-fopen-wrapper</VAR > wurde bei der Installation verwendet (Versionen bis 4.0.3) oder der Parameter <VAR CLASS="parameter" >allow_url_fopen</VAR > in der php.ini deaktiviert wurde (neuere Versionen), k÷nnen HTTP und FTP URLs bei den meisten Funktionen verwendet werden, die einen Dateinamen als Parameter ben÷tigen, inklusive <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > und <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > Anweisungen. </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > remote files funktionieren nicht mit <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > und <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > Anweisungen unter Windows. </P ></BLOCKQUOTE ></DIV > </P ><P >
Beispielsweise kann eine Datei auf einem anderen Webserver ge÷ffnet und verarbeitet werden. Diese Daten k÷nnen zur Abfrage einer Datenbank benutzt werden oder passend zum Rest der eigenen Website ausgegeben werden. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6189" ></A ><P ><B >Beispiel 35-1. Den Titel einer entfernten Seite auslesen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $file = fopen ("http://www.example.com/", "r"); if (!$file) { echo "<p>Datei konnte nicht ge÷ffnet werden.\n"; exit; } while (!feof ($file)) { $line = fgets ($file, 1024); /* Funktioniert nur, wenn Titel und title-Tags in einer Zeile stehen */ if (eregi ("<title>(.*)</title>", $line, $out)) { $title = $out[1]; break; } } fclose($file); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Auch eine Datei auf einem FTP-Server kann geschrieben werden, solange man sich ⁿber einen Benutzer mit entsprechenden Zugriffsrechten verbindet und die Datei noch nicht existiert. Um sich mit einem anderen Benutzer als 'anonymous' zu verbinden mu▀ ein username (und m÷glichst ein Passwort) innerhalb der URL angegeben werden, wie z.B. 'ftp://user:password@ftp.example.com/pfad/zur/datei'. (Die selbe Syntax kann verwendet werden, um auf Daten via HTTP zuzugreifen, wenn diese eine Basic Authentication ben÷tigen.) </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6194" ></A ><P ><B >Beispiel 35-2. Daten auf einen entfernten Server speichern</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w"); if (!$file) { echo "<p>Datei konnte zum schreiben nicht ge÷ffnet werden.\n"; exit; } /* Schreibe die Daten hier hin. */ fputs ($file, "$HTTP_USER_AGENT\n"); fclose ($file); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Obiges Beispiel k÷nnte dazu verleiten, dieses Verfahren zu benutzen, um in ein 'remote log-file' zu schreiben. Wie oben erwΣhnt kann man jedoch ausschlie▀lich neue Dateien anlegen, wenn man URL fopen() wrapper benutzt. Fⁿr 'distributed logging' sollte man sich die Funktion <A HREF="#function.syslog" ><B CLASS="function" >syslog()</B ></A > anschauen. </P ></BLOCKQUOTE ></DIV > </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.connection-handling" >Kapitel 36. Verbindungssteuerung</A ></H1 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Folgendes trifft auf die Versionen 3.0.7 und spΣter zu.</P ></BLOCKQUOTE ></DIV ><P >
PHP erhΣlt intern einen Verbindungsstatus. Dieser kann drei ZustΣnde annehmen: <P ></P ><UL ><LI ><P >0 - NORMAL</P ></LI ><LI ><P >1 - ABORTED</P ></LI ><LI ><P >2 - TIMEOUT</P ></LI ></UL > </P ><P >
Wenn ein PHP-Skript aktiv ist, ist der Status ⁿblicherweise NORMAL. Sollte der Client-Rechner die Verbindung beenden, wird der Status auf ABORTED gesetzt. Ein clientseitiges Beenden der Verbindung wird fⁿr gew÷hnlich veranla▀t, wenn der Benutzer den STOP-Button seines Browsers drⁿckt. Wenn das eingestellte Zeitlimit (siehe <A HREF="#function.set-time-limit" ><B CLASS="function" >set_time_limit()</B ></A >) ⁿberschritten wird, wird der Status TIMEOUT gesetzt.</P ><P >
Sie k÷nnen entscheiden, ob der Verbindungsabbruch seitens des Clients den Abbruch des Skriptes zur Folge haben soll. Manchmal ist es sinnvoll, Skripte sauber zu beenden, auch wenn kein Browser mehr zur Verfⁿgung steht, der die Ausgabe empfΣngt. Die Abarbeitung eines Skriptes wird standardmΣ▀ig abgebrochen, wenn der Client die Verbindung beendet. Dieses Verhalten kann sowohl durch die Option ignore_user_abort in der Konfigurationsdatei php3.ini, durch die entsprechende Option php3_ignore_user_abort in der Apache-Konfigurationsdatei als auch durch <A HREF="#function.ignore-user-abort" ><B CLASS="function" >ignore_user_abort()</B ></A > beeinflu▀t werden. Wenn PHP nicht angewiesen wird, einen Verbindungsabbruch durch den Benutzer zu ignorieren und die Verbindung dann durch den Benutzer beendet wird, wird die Abarbeitung des Scriptes abgebrochen. Die einzige Ausnahme ist, wenn durch die Funktion <A HREF="#function.register-shutdown-function" ><B CLASS="function" >register_shutdown_function()</B ></A > eine Shutdown-Funktion angegeben wird, die bei clientseitigem Abbruch ausgefⁿhrt wird. Wenn dann der Benutzer den STOP-Button seines Browsers drⁿckt, wird PHP bei der nΣchsten Ausgabe des Skriptes feststellen, dass die Verbindung abgebrochen wurde und die Shutdown-Funktion aufrufen. Diese Shutdown-Funktion wird auch aufgerufen, wenn das Skript auf normalem Wege beendet wird, daher sollte man, wenn man fⁿr den Fall eines Benutzerabbruchs etwas anderes vorgesehen hat, die Funktion <A HREF="#function.connection-aborted" ><B CLASS="function" >connection_aborted()</B ></A > verwenden. Sie gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn die Verbindung abgebrochen wurde.</P ><P >
Ein Skript kann ebenfalls durch den eingebauten Script-Timer beendet werden. Der Standard-Timeout betrΣgt 30 Sekunden. Er kann durch die Option max_execution_time in der php3.ini, durch den entsprechenden Eintrag php3_max_execution_time in der Apache-Konfigurationsdatei oder durch die Funktion <A HREF="#function.set-time-limit" ><B CLASS="function" >set_time_limit()</B ></A > beeinflu▀t werden. Bei Zeitⁿberschreitung wird das Skript beendet und, genau wie im obigen Fall des Verbindungsabbruchs, eine registrierte Shutdown-Funktion ausgefⁿhrt. Um zu ⁿberprⁿfen, ob es sich um einen Abbruch aufgrund von Zeitⁿberschreitung handelt, kann die Funktion <A HREF="#function.connection-timeout" ><B CLASS="function" >connection_timeout()</B ></A > benutzt werden. Sie gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn es sich um eine Zeitⁿberschreitung handelt.</P ><P >
Zu bemerken ist, dass der ABORTED und der TIMEOUT-Status gleichzeitig auftreten k÷nnen. Dies ist m÷glich, wenn PHP angewiesen wird, Benutzerabbrⁿche zu ignorieren. PHP wird feststellen, dass der Benutzer die Verbindung abgebrochen hat, das Skript allerdings lΣuft weiter. Sollte es dann das Zeitlimit erreichen, wird es abgebrochen und eine Shutdown-Funktion, wenn definiert, wird aufgerufen. Zu diesem Zeitpunkt kann man feststellen, dass <A HREF="#function.connection-timeout" ><B CLASS="function" >connection_timeout()</B ></A > und <A HREF="#function.connection-aborted" ><B CLASS="function" >connection_aborted()</B ></A > <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿckgeben. Diese beiden Statusm÷glichkeiten k÷nnen auch durch einen Aufruf der Funktion <A HREF="#function.connection-status" ><B CLASS="function" >connection_status()</B ></A > abgefragt werden. Sie liefert ein Bitfeld des aktiven Status. Wenn beispielsweise TIMEOUT und ABORTED aktiv sind, wird 3 zurⁿckgegeben. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.persistent-connections" >Kapitel 37. Persistente Datenbankverbindungen</A ></H1 ><P >
Persistente Verbindungen sind SQL-Verbindungen, die nach Abarbeitung des Skriptes nicht geschlossen werden. Wenn eine persistente Verbindung angefordert wird, prⁿft PHP zuerst, ob bereits eine identische persistente Verbindung (die vielleicht vorher offen geblieben ist) existiert und benutzt sie in diesem Fall. Sollte keine Verbindung existieren, wird eine hergestellt. Eine 'identische' Verbindung ist eine Verbindung, die zu dem gleichen Host mit dem gleichen Usernamen und Passwort hergestellt wurde.</P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Auch andere Erweiterungen bieten persistente Verbindungen, wie z.B. <A HREF="#ref.imap" >IMAP extension</A >. </P ></BLOCKQUOTE ></DIV ><P >
Wer nicht durchgΣngig mit der Art und Weise vertraut ist, wie Webserver arbeiten und die Last verteilen, k÷nnte missverstehen, wofⁿr persistente Verbindungen gedacht sind. Im Besonderen bieten sie <SPAN CLASS="emphasis" ><I CLASS="emphasis" >keine</I ></SPAN > M÷glichkeit, 'Benutzersitzungen' ⁿber die gleiche SQL-Verbindung zu ÷ffnen und <SPAN CLASS="emphasis" ><I CLASS="emphasis" >keine</I ></SPAN > M÷glichkeit, eine Transaktion effizient aufzubauen, und sie k÷nnen auch viele andere Dinge nicht. Um absolute Klarheit zu schaffen: Persistente Verbindungen bieten <SPAN CLASS="emphasis" ><I CLASS="emphasis" >keine</I ></SPAN > FunktionalitΣt, die nicht auch von nicht-persistenten Verbindungen bereitgestellt wird. </P ><P >
Warum? </P ><P >
Das hat mit der Arbeitsweise von Webservern zu tun. Es gibt drei M÷glichkeiten, wie ein Webserver PHP zur Generierung von Webseiten einsetzen kann. </P ><P >
Die erste Methode ist, PHP als CGI-'Wrapper' zu benutzen. Wenn diese Methode eingesetzt wird, wird fⁿr jede Anfrage nach einer PHP-Seite vom Webserver eine Instanz des PHP- Interpreters gestartet und anschlie▀end wieder beendet. Durch die Beendigung des Interpreters nach abgeschlossener Anfrage werden alle Ressourcen, auf die zugegriffen wurde (wie beispielsweise eine Verbindung zu einem SQL- Datenbankserver) wieder geschlossen. In diesem Fall erreicht man nichts, wenn man persistente Verbindungen benutzt - sie sind eben nicht bestΣndig. </P ><P >
Die zweite und populΣrste Methode ist der Einsatz von PHP als Modul in einem Multiprozess-Webserver, was momentan nur auf den Apache zutrifft. Typischerweise hat ein Multiprozess-Webserver einen Prozess (den 'Eltern' Prozess), der einen Satz weiterer Prozesse (die 'Kinder') koordiniert, welche die eigentliche Arbeit des Bereitstellens der Seiten ⁿbernehmen. Jede Anfrage, die von einem Client erfolgt, wird an einen untergeordneten Prozess, der noch keine andere Anfrage bearbeitet, weitergereicht. Das bedeutet, dass eine zweite Anfrage des gleichen Clients an den Server unter UmstΣnden von einem anderen untergeordneten Prozess als die erste Anfrage bearbeitet wird. In diesem Fall sorgt eine persistente Verbindung dafⁿr, dass jeder untergeordnete Prozess sich nur einmal mit dem SQL-Server verbinden muss, wenn eine solche ben÷tigt wird. Ben÷tigt dann eine weitere Seite die Verbindung mit dem SQL-Server, kann auf die zurⁿckgegriffen werden, die der untergeordnete Prozess vorher hergestellt hat. </P ><P >
Die letzte Methode ist, PHP als Plug-in fⁿr einen Multithread- Webserver zu benutzen. Derzeit bietet PHP 4 Unterstⁿtzung fⁿr ISAPI, WSAPI und NSAPI (unter Windows), wodurch die Nutzung von PHP mit Multithread-Serven wie Netscape Fast Track (iPlanet), Microsoft Internet Information Server (IIS) und O'Reilly's WebSite Pro erm÷glicht wird. Das Verhalten entspricht im wesentlichen dem oben beschriebenen Multiprozess-Modell. Beachten Sie, dass PHP 3 keine Unterstⁿtzung fⁿr SAPI bietet. </P ><P >
Wozu dienen persistente Verbindungen, wenn sie keine zusΣtzliche FunktionalitΣt bieten? </P ><P >
Die Antwort ist au▀erordentlich einfach: Effizienz. Persistente Verbindungen sind nⁿtzlich, wenn der Aufwand fⁿr das Herstellen einer Verbindung zu einem SQL-Server hoch ist. Ob dies der Fall ist oder nicht, hΣngt von vielen Faktoren ab - zum Beispiel, um welche Datenbank es sich handelt, ob sie auf dem gleichen Rechner wie der Webserver lΣuft oder welche Last die SQL-Maschine zu bewΣltigen hat usw. GrundsΣtzlich gilt, dass, wenn viele Verbindungen hergestellt werden mⁿssen, persistente Verbindungen au▀erordentlich hilfreich sind. Sie veranlassen den untergeordneten Prozess, sich wΣhrend seiner gesamten Lebensdauer lediglich einmal mit dem SQL-Server zu verbinden, anstatt bei jedem Aufruf einer Seite, die eine Verbindung ben÷tigt. Das hei▀t, dass jeder untergeordnete Prozess, der eine persistente Verbindung ÷ffnet, seine eigene dauerhafte Verbindung zum Server hat. Bei 20 untergeordneten Prozessen, die ein Skript ausfⁿhren, das eine persistente Verbindung zum SQL-Server herstellt, hat man beispielsweise 20 verschiedene Verbindungen zum SQL-Server - eine fⁿr jeden untergeordneten Prozess. </P ><P >
Beachten Sie jedoch, dass dies auch ein paar Nachteile haben kann, wenn Sie eine Datenbank mit limitierten Verbindungen benutzen, welche durch persistente Verbindungen ⁿberschritten werden. Wenn Ihre Datenbank ein Limit von 16 gleichzeitigen Verbindungen hat, und aufgrund einer stark ausgelasteten Server-Session 17 Kind-Prozesse versuchen, eine Verbindung herzustellen, wird es einem nicht gelingen. Sollten in Ihren Skripten Fehler bestehen, welche das Schlie▀en der Verbindungen nicht erlauben (wie z.B. Endlosschleifen), kann das eine Datenbank mit mit nur 16 Verbindungen sehr schnell ⁿberschwemmen. Konsultieren Sie die Dokumentation Ihrer Datenbank bezⁿglich der Behandlung von aufgegebenen Verbindungen oder Verbindungen im Leerlauf. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Sie sollten sich zur Vorsicht noch ein paar Gedanken machen, wenn Sie persistente Verbindungen benutzen. Einer ist, wenn Sie ⁿber eine persistente Verbindung Tabellen sperren und das Skript diese Sperre aus welchem Grund auch immer nicht mehr aufheben kann, nachfolgende Skripte, welche die selbe Verbindung benutzen, blockieren und den Neustart von entweder dem Webserver oder dem Datenbankserver verlangen. Ein weiterer ist, dass wenn Sie Transaktionen benutzen, ein Transaktionsblock zu dem nΣchsten die Verbindung nutzenden Skript ⁿbertragen wird, wenn die Ausfⁿhrung des Skriptes vor dem Transaktionsblock gestoppt wird. In jedem Fall k÷nnen Sie <A HREF="#function.register-shutdown-function" ><B CLASS="function" >register_shutdown_function()</B ></A > benutzen, um eine einfache Funktion zu registrieren, welche Ihre Tabellen wieder entsperrt, oder Ihre Transaktionen zurⁿckstellt. Besser ist es, wenn Sie dieses Problem gΣnzlich vermeiden, indem keine persistenten Verbindungen in Skripten benutzen, welche Tabellen sperren oder Transaktionen verwenden (Sie k÷nnen sie immer noch anderswo benutzen). </P ></TD ></TR ></TABLE ></DIV ><P >
Eine wichtige Zusammenfassung. Persistente Verbindungen wurden entwickelt, um eins-zu-eins Abbildungen auf regulΣre Verbindungen zu haben. Das hei▀t, dass man <SPAN CLASS="emphasis" ><I CLASS="emphasis" >immer</I ></SPAN > in der Lage sein sollte, die persistenten Verbindungen durch nicht-persistente zu ersetzten, ohne dass dies den Skriptablauf verΣndert. Es <SPAN CLASS="emphasis" ><I CLASS="emphasis" >
kann</I ></SPAN > (und wird vermutlich auch) die Effizienz des Skriptes beeinflussen, aber nicht dessen Verhalten. </P ><P >
Siehe auch <A HREF="#function.fbsql-pconnect" ><B CLASS="function" >fbsql_pconnect()</B ></A >, <A HREF="#function.ibase-pconnect" ><B CLASS="function" >ibase_pconnect()</B ></A >, <A HREF="#function.ifx-pconnect" ><B CLASS="function" >ifx_pconnect()</B ></A >, <B CLASS="function" >imap_popen()</B >, <A HREF="#function.ingres-pconnect" ><B CLASS="function" >ingres_pconnect()</B ></A >, <A HREF="#function.msql-pconnect" ><B CLASS="function" >msql_pconnect()</B ></A >, <A HREF="#function.mssql-pconnect" ><B CLASS="function" >mssql_pconnect()</B ></A >, <A HREF="#function.mysql-pconnect" ><B CLASS="function" >mysql_pconnect()</B ></A >, <A HREF="#function.ociplogon" ><B CLASS="function" >ociplogon()</B ></A >, <A HREF="#function.odbc-pconnect" ><B CLASS="function" >odbc_pconnect()</B ></A >, <A HREF="#function.ora-plogon" ><B CLASS="function" >ora_plogon()</B ></A >, <A HREF="#function.pfsockopen" ><B CLASS="function" >pfsockopen()</B ></A >, <A HREF="#function.pg-pconnect" ><B CLASS="function" >pg_pconnect()</B ></A > und <A HREF="#function.sybase-pconnect" ><B CLASS="function" >sybase_pconnect()</B ></A >. </P ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.safe-mode" >Kapitel 38. Safe Mode</A ></H1 ><P >
Safe Mode ist der Versuch, Sicherheitsprobleme bei gemeinsam genutzten Servern zu l÷sen. Bezogen auf die Systemarchitektur, ist es der falsche Ansatz, diese Probleme innerhalb der PHP Schicht l÷sen zu wollen. Da es auf Ebene des Webservers bzw. des Betriebssystems keine praktischen Alternativen gibt, wird Safe Mode nunmehr von vielen Leuten, vor allem von Providern, eingesetzt. </P ><P >
Es existieren folgende Direktiven um Safe Mode zu konfigurieren: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" >safe_mode = Off safe_mode_gid = 0 safe_mode_include_dir = safe_mode_exec_dir = open_basedir = safe_mode_allowed_env_vars = PHP_ safe_mode_protected_env_vars = LD_LIBRARY_PATH disable_functions =</PRE ></TD ></TR ></TABLE > </P ><P >
Wenn <A HREF="#ini.safe-mode" >safe_mode</A > aktiviert ist, ⁿberprⁿft PHP, ob der Eigentⁿmer des laufenden Skriptes dem Eigentⁿmer der Datei entspricht, an der mit Hilfe der Dateifunktionen gearbeitet werden soll. Beispiel: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ls" >-rw-rw-r-- 1 rasmus rasmus 33 Jul 1 19:20 script.php -rw-r--r-- 1 root root 1116 May 26 18:01 /etc/passwd</PRE ></TD ></TR ></TABLE > Bei der Ausfⁿhrung dieses Skriptes <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php readfile('/etc/passwd'); ?></PRE ></TD ></TR ></TABLE > erscheint bei aktiviertem Safe Mode folgende Warnung: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2</PRE ></TD ></TR ></TABLE > </P ><P >
Wenn anstelle von <A HREF="#ini.safe-mode" >safe_mode</A > ein <A HREF="#ini.open-basedir" >open_basedir</A > Verzeichnis angegeben wurde, k÷nnen Dateioperationen nur noch unterhalb dieses Verzeichnisses vorgenommen werden. Beispiel (Apache httpd.conf): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" ><Directory /docroot> php_admin_value open_basedir /docroot </Directory></PRE ></TD ></TR ></TABLE > Wenn das Skript script.php mit der <A HREF="#ini.open-basedir" >open_basedir</A > Einstellung ausgefⁿhrt wird, erscheint folgende Warnung: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Warning: open_basedir restriction in effect. File is in wrong directory in /docroot/script.php on line 2</PRE ></TD ></TR ></TABLE > </P ><P >
Darⁿberhinaus k÷nnen einzelne Funktionen deaktiviert werden. Wⁿrde man folgenden Eintrag in der php.ini vornehmen: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="ini" >disable_functions readfile,system</PRE ></TD ></TR ></TABLE > Wⁿrde diese Warnung erscheinen: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Warning: readfile() has been disabled for security reasons in /docroot/script.php on line 2</PRE ></TD ></TR ></TABLE > </P ><DIV CLASS="sect1" ><HR><H2 CLASS="sect1" ><A NAME="features.safe-mode.functions" >Funktionen, die durch Safe Mode deaktiviert oder eingeschrΣnkt werden</A ></H2 ><P >
Dies ist eine Auflistung der Funktionen, die durch <A HREF="#features.safe-mode" >Safe Mode</A > eingeschrΣnkt werden. Diese Liste ist m÷glicherweise unvollstΣndig und fehlerhaft! <DIV CLASS="table" ><A NAME="AEN6291" ></A ><P ><B >Tabelle 38-1. Durch Safe Mode eingeschrΣnkte Funktionen</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Funktion</TH ><TH >EinschrΣnkungen</TH ></TR ></THEAD ><TBODY ><TR ><TD ><A HREF="#function.dbmopen" ><B CLASS="function" >dbmopen()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.dbase-open" ><B CLASS="function" >dbase_open()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.filepro" ><B CLASS="function" >filepro()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.filepro-rowcount" ><B CLASS="function" >filepro_rowcount()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.filepro-retrieve" ><B CLASS="function" >filepro_retrieve()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><B CLASS="function" >ifx_*()</B ></TD ><TD >sql_safe_mode restrictions, (!= Safe Mode)</TD ></TR ><TR ><TD ><B CLASS="function" >ingres_*()</B ></TD ><TD >sql_safe_mode restrictions, (!= Safe Mode)</TD ></TR ><TR ><TD ><B CLASS="function" >mysql_*()</B ></TD ><TD >sql_safe_mode restrictions, (!= Safe Mode)</TD ></TR ><TR ><TD ><B CLASS="function" >pg_loimport()</B ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.posix-mkfifo" ><B CLASS="function" >posix_mkfifo()</B ></A ></TD ><TD >▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.putenv" ><B CLASS="function" >putenv()</B ></A ></TD ><TD >
Fⁿhrt die safe_mode_protected_env_vars und safe_mode_allowed_env_vars ini-directiven aus. Siehe auch Dokumentation zu <A HREF="#function.putenv" ><B CLASS="function" >putenv()</B ></A > </TD ></TR ><TR ><TD ><A HREF="#function.move-uploaded-file" ><B CLASS="function" >move_uploaded_file()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. </TD ></TR ><TR ><TD ><A HREF="#function.chdir" ><B CLASS="function" >chdir()</B ></A ></TD ><TD >▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.dl" ><B CLASS="function" >dl()</B ></A ></TD ><TD >Diese Funktion steht im <A HREF="#features.safe-mode" >Safe Mode</A > nicht zur Verfⁿgung.</TD ></TR ><TR ><TD ><A HREF="#language.operators.execution" >
backtick operator</A > </TD ><TD >Diese Funktion steht im <A HREF="#features.safe-mode" >Safe Mode</A > nicht zur Verfⁿgung.</TD ></TR ><TR ><TD ><A HREF="#function.shell-exec" ><B CLASS="function" >shell_exec()</B ></A > (funktional gleichwertig zu den Backtick-Operatoren)</TD ><TD >Diese Funktion steht im <A HREF="#features.safe-mode" >Safe Mode</A > nicht zur Verfⁿgung.</TD ></TR ><TR ><TD ><A HREF="#function.exec" ><B CLASS="function" >exec()</B ></A ></TD ><TD >Ausfⁿhrbare Dateien k÷nnen nur innerhalb des durch <A HREF="#ini.safe-mode-exec-dir" >safe_mode_exec_dir</A > angegebenen Verzeichnisses ausgefⁿhrt werden. Aus praktischen Grⁿnden ist es momentan nicht erlaubt, <VAR CLASS="literal" >..</VAR > im Pfad zu den ausfⁿhrbaren Datein anzugeben. </TD ></TR ><TR ><TD ><A HREF="#function.system" ><B CLASS="function" >system()</B ></A ></TD ><TD >Ausfⁿhrbare Dateien k÷nnen nur innerhalb des durch <A HREF="#ini.safe-mode-exec-dir" >safe_mode_exec_dir</A > angegebenen Verzeichnisses ausgefⁿhrt werden. Aus praktischen Grⁿnden ist es momentan nicht erlaubt, <VAR CLASS="literal" >..</VAR > im Pfad zu den ausfⁿhrbaren Datein anzugeben. </TD ></TR ><TR ><TD ><A HREF="#function.passthru" ><B CLASS="function" >passthru()</B ></A ></TD ><TD >Ausfⁿhrbare Dateien k÷nnen nur innerhalb des durch <A HREF="#ini.safe-mode-exec-dir" >safe_mode_exec_dir</A > angegebenen Verzeichnisses ausgefⁿhrt werden. Aus praktischen Grⁿnden ist es momentan nicht erlaubt, <VAR CLASS="literal" >..</VAR > im Pfad zu den ausfⁿhrbaren Datein anzugeben. </TD ></TR ><TR ><TD ><A HREF="#function.popen" ><B CLASS="function" >popen()</B ></A ></TD ><TD >Ausfⁿhrbare Dateien k÷nnen nur innerhalb des durch <A HREF="#ini.safe-mode-exec-dir" >safe_mode_exec_dir</A > angegebenen Verzeichnisses ausgefⁿhrt werden. Aus praktischen Grⁿnden ist es momentan nicht erlaubt, <VAR CLASS="literal" >..</VAR > im Pfad zu den ausfⁿhrbaren Datein anzugeben. </TD ></TR ><TR ><TD ><A HREF="#function.mkdir" ><B CLASS="function" >mkdir()</B ></A ></TD ><TD >▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.rmdir" ><B CLASS="function" >rmdir()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.rename" ><B CLASS="function" >rename()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. ▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst. </TD ></TR ><TR ><TD ><A HREF="#function.unlink" ><B CLASS="function" >unlink()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. ▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.copy" ><B CLASS="function" >copy()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. ▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst. (on <VAR CLASS="parameter" >source</VAR > and <VAR CLASS="parameter" >target</VAR >) </TD ></TR ><TR ><TD ><A HREF="#function.chgrp" ><B CLASS="function" >chgrp()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.chown" ><B CLASS="function" >chown()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.chmod" ><B CLASS="function" >chmod()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. In addition, you cannot set the SUID, SGID and sticky bits</TD ></TR ><TR ><TD ><A HREF="#function.touch" ><B CLASS="function" >touch()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. ▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst.</TD ></TR ><TR ><TD ><A HREF="#function.symlink" ><B CLASS="function" >symlink()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. ▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst. (note: only the target is checked)</TD ></TR ><TR ><TD ><A HREF="#function.link" ><B CLASS="function" >link()</B ></A ></TD ><TD >▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. ▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst. (note: only the target is checked)</TD ></TR ><TR ><TD ><A HREF="#function.getallheaders" ><B CLASS="function" >getallheaders()</B ></A ></TD ><TD >
Header die mit 'authorization' (unabhΣngig von Gro▀- oder Kleinschreibung) beginnen, werden bei Verwendung von Safe Mode nicht zurⁿckgegeben. Achtung: Dies ist fehlerhaft bei der AOL-Server Implementation der Funktion <A HREF="#function.getallheaders" ><B CLASS="function" >getallheaders()</B ></A >! </TD ></TR ><TR ><TD >
<A HREF="#function.highlight-file" ><B CLASS="function" >highlight_file()</B ></A >, <A HREF="#function.show-source" ><B CLASS="function" >show_source()</B ></A > </TD ><TD >
▄berprⁿft ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID Eigentⁿmer haben wie das Skript selbst. ▄berprⁿft ob das Verzeichnis in dem das Skript ausgefⁿhrt werden soll, die gleiche UID (Eigentⁿmer) hat wie das Skript selbst. (Hinweis: gilt nur ab Version PHP 4.2.1) </TD ></TR ><TR ><TD >Jede Funktion, die <TT CLASS="filename" >php4/main/fopen_wrappers.c</TT > benutzt... </TD ><TD >??</TD ></TR ></TBODY ></TABLE ></DIV > </P ></DIV ></DIV ><DIV CLASS="chapter" ><HR><H1 ><A NAME="features.commandline" >Kapitel 39. PHP auf der Kommandozeile</A ></H1 ><P >
Seit Version 4.3.0 unterstⁿtzt <VAR CLASS="literal" >PHP</VAR > einen neuen Typ <VAR CLASS="literal" >SAPI</VAR > (Server Application Programming Interface) namens <VAR CLASS="literal" >CLI</VAR > (<SPAN CLASS="emphasis" ><I CLASS="emphasis" >Command Line Interface</I ></SPAN >). Wie der Name schon verrΣt, liegt das Hauptaugemerk dieses <VAR CLASS="literal" >SAPI</VAR >-Typs auf der Entwicklung von Anwendungen fⁿr die Kommandozeile (und auch den Desktop) mit <VAR CLASS="literal" >PHP</VAR >. Es gibt etliche Unterschiede zwischen dem <VAR CLASS="literal" >CLI SAPI</VAR > und anderen <VAR CLASS="literal" >SAPI</VAR >s, die in diesem Kapitel beschrieben werden. Es ist erwΣhnenswert, dass <VAR CLASS="literal" >CLI</VAR > und <VAR CLASS="literal" >CGI</VAR > verschiedene SAPIs sind, obwohl sie viele Verhaltensweisen teilen. </P ><P >
Das <VAR CLASS="literal" >CLI SAPI</VAR > wurde zum ersten Mal mit <VAR CLASS="literal" >PHP 4.2.0</VAR > eingefⁿhrt, war aber noch experimentell und musste explizit mit <VAR CLASS="literal" >--enable-cli</VAR > beim <VAR CLASS="literal" >./configure</VAR >-Aufruf eingeschaltet werden. Seit <VAR CLASS="literal" >PHP 4.3.0</VAR > ist das <VAR CLASS="literal" >CLI SAPI</VAR > nicht lΣnger experimentell und die Option <VAR CLASS="literal" >--enable-cli</VAR > standardmΣ▀ig auf <VAR CLASS="literal" >on</VAR > gestellt. Sie k÷nnen <VAR CLASS="literal" >--disable-cli</VAR > benutzen, um es auszuschalten. </P ><P >
Von <VAR CLASS="literal" >PHP 4.3.0</VAR > an werden Namen, Pfade und Vorhandensein der CLI/CGI-Binaries voneinander abweichen, abhΣngig davon, wie <VAR CLASS="literal" >PHP</VAR > auf Ihrem System installiert ist. StandarmΣ▀ig werden nach dem Aufruf von <VAR CLASS="literal" >make</VAR > sowohl CGI als auch CLI kompiliert und als <VAR CLASS="literal" >sapi/cgi/php</VAR > beziehungsweise <VAR CLASS="literal" >sapi/cli/php</VAR > in Ihrem PHP-Quellcodeverzeichnis hinterlegt. Sie werden bemerken, dass beide <VAR CLASS="literal" >php</VAR > benannt sind. Was wΣhrend <VAR CLASS="literal" >make install</VAR > passiert, hΣngt von Ihrem configure-Kommando ab. Wenn ein Modul-SAPI wΣhrend configure gewΣhlt wird, wie etwa <VAR CLASS="literal" >apxs</VAR > oder wenn die <VAR CLASS="literal" >--disable-cgi</VAR >-Option benutzt wird, wird das CLI wΣhrend <VAR CLASS="literal" >make install</VAR > nach <VAR CLASS="literal" >{PREFIX}/bin/php</VAR > kopiert, ansonsten wird das CGI dort hinterlegt. Also wird, wenn beispielsweise <VAR CLASS="literal" >--with--apxs</VAR > in Ihrem configure-Kommando auftaucht, das CLI wΣhrend <VAR CLASS="literal" >make install</VAR > nach <VAR CLASS="literal" >{PREFIX}/bin/php</VAR > kopiert. Wenn Sie die Installation des CGI-Binaries au▀er Kraft setzen wollen, benutzen Sie <VAR CLASS="literal" >make install-cli</VAR > nach <VAR CLASS="literal" >make install</VAR >. Alternativ k÷nnen Sie <VAR CLASS="literal" >--disable-cgi</VAR > in Ihrem configure-Kommando angeben. </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Weil sowohl <VAR CLASS="literal" >--enable-cli</VAR > als auch <VAR CLASS="literal" >
--enable-cgi</VAR > standardmΣ▀ig eingeschaltet sind, muss <VAR CLASS="literal" >--enable-cli</VAR > in Ihrem configure-Kommando nicht zwingend bedeuten, dass das CLI wΣhrend <VAR CLASS="literal" >make install</VAR > nach <VAR CLASS="literal" >{PREFIX}/bin/php</VAR > kopiert wird. </P ></BLOCKQUOTE ></DIV > </P ><P >
Die Windows-Pakete zwischen PHP 4.2.0 und PHP 4.2.3 enthielten das CLI als <TT CLASS="filename" >php-cli.exe</TT >, beheimatet im selben Verzeichnis wie das CGI <TT CLASS="filename" >php.exe</TT >. Beginnend mit PHP 4.3.0 enthΣlt das Windows-Paket das CLI als <TT CLASS="filename" >php.exe</TT > in einem eigenen Verzeichnis namens <VAR CLASS="literal" >cli</VAR >, also <VAR CLASS="literal" >cli/php.exe</VAR >. Beginnend mit PHP 5 wird das CLI im Hauptordner al <TT CLASS="filename" >php.exe</TT > ausgeliefert. Die CGI-Version heisst <TT CLASS="filename" >php-cgi.exe</TT >. </P ><P >
Seit PHP gibt es eine neue Datei, <TT CLASS="filename" >php-win.exe</TT >. Diese ist identisch mit der CLI-Version, die einzige Ausnahme besteht darin, dass php-win nichts ausgibt und somit keine Konsole bietet. (Es erscheint keine "MS-DOS-Eingabeaufforderung".) Dieses Verhalten ist Σhnlich php-gtk. Sie sollten die folgende Option fⁿr ./configure benutzen: <VAR CLASS="literal" >--enable-cli-win32</VAR >. </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Welche SAPI habe ich?: </B > Auf der Kommandozeile wird Ihnen die Eingabe von <VAR CLASS="literal" >php -v</VAR > verraten, ob Sie <VAR CLASS="literal" >php</VAR > als CGI oder CLI einsetzen. Sehen Sie sich auch die Funktion <A HREF="#function.php-sapi-name" ><B CLASS="function" >php_sapi_name()</B ></A > und die Konstante <TT CLASS="constant" ><B >PHP_SAPI</B ></TT > an. </P ></BLOCKQUOTE ></DIV > </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Eine UNIX <VAR CLASS="literal" >man</VAR >ual page wurde in PHP 4.3.2 hinzugefⁿgt. Sie k÷nnen sie ansehen, indem sie <VAR CLASS="literal" >man php</VAR > auf der Kommandozeile eintippen. </P ></BLOCKQUOTE ></DIV > </P ><P >
Beachtenswerte Unterschiede des <VAR CLASS="literal" >CLI SAPI</VAR > verglichen mit anderen <VAR CLASS="literal" >SAPI</VAR >s: <P ></P ><UL ><LI ><P >
Anders als beim <VAR CLASS="literal" >CGI SAPI</VAR > werden keine Header ausgegeben. </P ><P >
Obwohl das <VAR CLASS="literal" >CGI SAPI</VAR > einen Weg bietet, HTTP-Header zu unterdrⁿcken, gibt es keine gleichwertige Option, sie im <VAR CLASS="literal" >CLI SAPI</VAR > anzuzeigen. </P ><P >
CLI wird standardmΣ▀ig im stillen Modus gestartet, dennoch gibt es die Optionen <VAR CLASS="literal" >-q</VAR > und <VAR CLASS="literal" >--no-header</VAR > aus KompatibilitΣtsgrⁿnden noch, so dass Sie auch Σltere CGI-Skripte noch benutzen k÷nnen. </P ><P >
Es verΣndert das Arbeitsverzeichnis nicht zu dem des Skriptes. (Die Optionen <VAR CLASS="literal" >-C</VAR > und <VAR CLASS="literal" >--no-chdir</VAR > wurden aus KompatibilitΣtsgrⁿnden beibehalten.) </P ><P >
Fehlermeldungen in Plaintext (keine HTML-Formatierung). </P ></LI ><LI ><P >
Es gibt bestimmte <TT CLASS="filename" >php.ini</TT >-Direktiven, die vom <VAR CLASS="literal" >CLI SAPI</VAR > au▀er Kraft gesetzt werden, weil sie in einer Kommandozeilenumgebung keinen Sinn ergeben. </P ><P >
<DIV CLASS="table" ><A NAME="AEN6546" ></A ><P ><B >Tabelle 39-1. ▄berschriebene <TT CLASS="filename" >php.ini</TT >-Direktiven</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Direktive</TH ><TH ><VAR CLASS="literal" >CLI SAPI</VAR >-Standardwert</TH ><TH >Kommentar</TH ></TR ></THEAD ><TBODY ><TR ><TD ><A HREF="#ini.html-errors" >html_errors</A ></TD ><TD ><TT CLASS="constant" ><B >FALSE</B ></TT ></TD ><TD >
Es kann sehr schwierig sein, eine Fehlermeldung auf der Kommandozeile zu lesen, wenn sie mit all diesen bedeutungslosen <VAR CLASS="literal" >HTML</VAR >-Tags vollgestopft ist, deswegen wird diese Direktive auf <TT CLASS="constant" ><B >FALSE</B ></TT > gesetzt. </TD ></TR ><TR ><TD ><A HREF="#ini.implicit-flush" >implicit_flush</A ></TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT ></TD ><TD >
Es ist erwⁿnscht, dass alle Ausgaben, die von <A HREF="#function.print" ><B CLASS="function" >print()</B ></A >, <A HREF="#function.echo" ><B CLASS="function" >echo()</B ></A > und Konsorten kommen, sofort auf die Ausgabe geschrieben werden und nicht in irgendeinem Puffer gecacht werden. Sie k÷nnen immer noch <A HREF="#ref.outcontrol" >output buffering</A > benutzen, wenn Sie die Standardausgabe aufschieben oder manipulieren wollen. </TD ></TR ><TR ><TD ><A HREF="#ini.max-execution-time" >max_execution_time</A ></TD ><TD >0 (unlimited)</TD ><TD >
Wegen der unendlichen M÷glichkeiten, <VAR CLASS="literal" >PHP</VAR > in Kommandozeilenumgebungen zu benutzen wurde die maximale Ausfⁿhrungszeit auf unbegrenzte Zeit eingestellt. Wohingegen Anwendungen, die fⁿr das Web geschrieben wurden, oft in sehr kurzer Zeit ausgefⁿhrt werden, neigen Anwendungen fⁿr die Kommandozeile dazu, eine viel lΣngere Ausfⁿhrungszeit zu beanspruchen. </TD ></TR ><TR ><TD ><A HREF="#ini.register-argc-argv" >register_argc_argv</A ></TD ><TD ><TT CLASS="constant" ><B >TRUE</B ></TT ></TD ><TD >
<P >
Weil diese Einstellung auf <TT CLASS="constant" ><B >TRUE</B ></TT > gesetzt ist, werden Sie immer Zugriff auf <SPAN CLASS="emphasis" ><I CLASS="emphasis" >argc</I ></SPAN > (Anzahl der Argumente, die der Anwendung ⁿbergeben wurden) und <SPAN CLASS="emphasis" ><I CLASS="emphasis" >argv</I ></SPAN > (Array der eigentlichen Argumente) im <VAR CLASS="literal" >CLI SAPI</VAR > haben. </P > <P >
Seit PHP 4.3.0 werden die <VAR CLASS="literal" >PHP</VAR >-Variablen <VAR CLASS="varname" >$argc</VAR > und <VAR CLASS="varname" >$argv</VAR > registriert und mit den entsprechenden Werten gefⁿllt, wenn das <VAR CLASS="literal" >CLI SAPI</VAR > benutzt wird. Vor dieser Version verhielt sich die Erzeugung dieser Variablen so wie in den <VAR CLASS="literal" >CGI</VAR > und <VAR CLASS="literal" >MODULE</VAR >-Versionen, die die PHP-Direktive <A HREF="#ini.register-globals" >register_globals</A > auf <SPAN CLASS="emphasis" ><I CLASS="emphasis" >on</I ></SPAN > verlangen. Ungeachtet der Version oder der Einstellung von <A HREF="#ini.register-globals" >register_globals</A > k÷nnen Sie immer <A HREF="#reserved.variables.server" >$_SERVER</A > oder <VAR CLASS="varname" >$HTTP_SERVER_VARS</VAR > benutzen. Beispiel: <VAR CLASS="varname" >$_SERVER['argv']</VAR > </P > </TD ></TR ></TBODY ></TABLE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Diese Direktiven k÷nnen nicht mit einem anderen Wert aus der Konfigurationsdatei <TT CLASS="filename" >php.ini</TT > oder einer eigenen (falls angegeben) initialisiert werden. Das ist eine EinschrΣnkung, weil diese Standardwerte angewandt werden, nachdem alle Konfigurationsdateien durchlaufen wurden. Dennoch k÷nnen die Werte zur Laufzeit verΣndert werden (was aber fⁿr alle diese Direktiven keinen Sinn ergibt, beispielsweise <A HREF="#ini.register-argc-argv" >register_argc_argv</A >). </P ></BLOCKQUOTE ></DIV ></LI ><LI ><P >
Um die Arbeit in der Kommandozeilenumgebung zu vereinfachen, wurden folgende Konstanten definiert: <DIV CLASS="table" ><A NAME="AEN6610" ></A ><P ><B >Tabelle 39-2. CLI-spezifische Konstanten</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Konstante</TH ><TH >Beschreibung</TH ></TR ></THEAD ><TBODY ><TR ><TD ><TT CLASS="constant" ><B >STDIN</B ></TT ></TD ><TD >
Ein schon ge÷ffneter Stream auf <VAR CLASS="literal" >stdin</VAR >. Das erspart das ÷ffnen mit <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $stdin = fopen('php://stdin', 'r'); ?></PRE ></TD ></TR ></TABLE > </TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >STDOUT</B ></TT ></TD ><TD >
Ein schon ge÷ffneter Stream auf <VAR CLASS="literal" >stdout</VAR >. Das erspart das ÷ffnen mit <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $stdout = fopen('php://stdout', 'w'); ?></PRE ></TD ></TR ></TABLE > </TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >STDERR</B ></TT ></TD ><TD >
Ein schon ge÷ffneter Stream auf <VAR CLASS="literal" >stderr</VAR >. Das erspart das ÷ffnen mit <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $stderr = fopen('php://stderr', 'w'); ?></PRE ></TD ></TR ></TABLE > </TD ></TR ></TBODY ></TABLE ></DIV > </P ><P >
Angesichts des oben genannten brauchen Sie beispielsweise nicht selbst einen Stream fⁿr <VAR CLASS="literal" >stderr</VAR > ÷ffnen, sondern einfach die Konstante statt der Ressource: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >php -r 'fwrite(STDERR, "stderr\n");'</PRE ></TD ></TR ></TABLE > Sie brauchen diese Ressourcen nicht explizit schliessen, weil sie automatisch von <VAR CLASS="literal" >PHP</VAR > geschlossen werden, wenn ihr Skript endet. </P ></LI ><LI ><P >
Das <VAR CLASS="literal" >CLI SAPI</VAR > Σndert das aktuelle Verzeichnis <SPAN CLASS="strong" ><B CLASS="emphasis" >nicht</B ></SPAN > auf das Verzeichnis des ausgefⁿhrten Skriptes! </P ><P >
Ein Beispiel, das den Unterschied zum <VAR CLASS="literal" >CGI SAPI</VAR > zeigt: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Our simple test application named test.php echo getcwd(), "\n"; ?></PRE ></TD ></TR ></TABLE > </P ><P >
Bei der <VAR CLASS="literal" >CGI</VAR >-Version ist die Ausgabe folgende: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ pwd /tmp $ php -q another_directory/test.php /tmp/another_directory</PRE ></TD ></TR ></TABLE > Dies zeigt deutlich, dass <VAR CLASS="literal" >PHP</VAR > das aktuelle Verzeichnis auf das des asugefⁿhrten Skriptes Σndert. </P ><P >
Benutzen der <VAR CLASS="literal" >CLI SAPI</VAR >-Version ergibt: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ pwd /tmp $ php -f another_directory/test.php /tmp</PRE ></TD ></TR ></TABLE > Dies erlaubt eine gr÷▀ere AnpassungsfΣhigkeit beim Schreiben von Kommandozeilenanwendungen in <VAR CLASS="literal" >PHP</VAR >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Das <VAR CLASS="literal" >CGI SAPI</VAR > unterstⁿtzt das Verhalten des <VAR CLASS="literal" >CLI SAPI</VAR > anhand der Option <VAR CLASS="literal" >-C</VAR >, wenn es auf der Kommandozeile gestartet wird. </P ></BLOCKQUOTE ></DIV ></LI ></UL > </P ><P >
Die Liste der Kommandozeilenoptionen, die das <VAR CLASS="literal" >PHP</VAR >-Binary bereitstellt, k÷nnen jederzeit abgerufen werden, wenn man <VAR CLASS="literal" >PHP</VAR > mit der Option <VAR CLASS="literal" >-h</VAR > startet: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Usage: php [options] [-f] <file> [args...] php [options] -r <code> [args...] php [options] [-- args...] -s Display colour syntax highlighted source. -w Display source with stripped comments and whitespace. -f <file> Parse <file>. -v Version number -c <path>|<file> Look for php.ini file in this directory -a Run interactively -d foo[=bar] Define INI entry foo with value 'bar' -e Generate extended information for debugger/profiler -z <file> Load Zend extension <file>. -l Syntax check only (lint) -m Show compiled in modules -i PHP information -r <code> Run PHP <code> without using script tags <?..?> -h This help args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin</PRE ></TD ></TR ></TABLE > </P ><P >
Beim <VAR CLASS="literal" >CLI SAPI</VAR > gibt es drei verschiedene M÷glichkeiten, den zur Ausfⁿhrung bestimmten <VAR CLASS="literal" >PHP</VAR >-Code zu ⁿbergeben: <P ></P ><OL TYPE="1" ><LI ><P >
<VAR CLASS="literal" >PHP</VAR > mitteilen, eine bestimmte Datei auszufⁿhren. </P ><P >
<TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >php my_script.php php -f my_script.php</PRE ></TD ></TR ></TABLE > Beide Arten (ob sie die Option <VAR CLASS="literal" >-f</VAR > benutzen oder nicht) fⁿhren die Datei <TT CLASS="filename" >my_script.php</TT > aus. Sie k÷nnen eine beliebige Datei ausfⁿhren - Ihr <VAR CLASS="literal" >PHP</VAR >-Skript muss nicht die Dateiendung <TT CLASS="filename" >.php</TT > haben, sie kann einen beliebigen Namen und eine beliebige Endung haben. </P ></LI ><LI ><P >
Den <VAR CLASS="literal" >PHP</VAR >-Code, der ausgefⁿhrt werden soll, direkt auf der Kommandozeile ⁿbergeben. </P ><P >
<TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >php -r 'print_r(get_defined_constants());'</PRE ></TD ></TR ></TABLE > Bezⁿglich Ersetzung von Kommandozeilenvariablen und Benutzung von Quotes muss besondere Vorsicht getroffen werden. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Betrachten Sie das Beispiel sorgfΣltig, es sind keine Start- oder Endtags gesetzt. Die Option <VAR CLASS="literal" >-r</VAR > ben÷tigt sie nicht. Werden sie dennoch verwendet, kommt es zu einem Parser-Fehler. </P ></BLOCKQUOTE ></DIV ></LI ><LI ><P >
Stellen Sie den <VAR CLASS="literal" >PHP</VAR >-Code, der ausgefⁿhrt werden soll ⁿber den Standard-Input (<VAR CLASS="literal" >stdin</VAR >) bereit. </P ><P >
Dies er÷ffnet die leistungsfΣhige M÷glichkeit, <VAR CLASS="literal" >PHP</VAR >-Code dynamisch zu generieren und ihn dem <VAR CLASS="literal" >PHP</VAR >-Binary zu ⁿbergeben, wie in diesem m÷glichem Beispiel: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ some_application | some_filter | php | sort -u >final_output.txt</PRE ></TD ></TR ></TABLE > </P ></LI ></OL > Sie k÷nnen diese drei M÷glichkeiten zur Ausfⁿhrung nicht kombinieren. </P ><P >
Wie jede Kommandozeilenanwendung nimmt das <VAR CLASS="literal" >PHP</VAR >-Binary etliche Argumente entegegen, aber auch ein <VAR CLASS="literal" >PHP</VAR >-Skript kann Argumente entgegennehmen. Die Anzahl der Argumente, die an ein Skript ⁿbergeben werden k÷nnen, ist nicht von <VAR CLASS="literal" >PHP</VAR > beschrΣnkt (aber die Kommandozeilenumgebung hat ein bestimmtes Gr÷ssenlimit fⁿr die Anzahl der Argumente, die ⁿbergeben werden k÷nnen; normalerweise werden Sie dieses Limit nicht erreichen). Die Argumente, die dem Skript ⁿbergeben werden, sind in dem globalen Array <VAR CLASS="literal" >$argv</VAR > gespeichert. Der Null-Index enthΣlt immer den Skriptnamen (das bedeutet <VAR CLASS="literal" >-</VAR >, wenn der <VAR CLASS="literal" >PHP</VAR >-Code entweder von der Standardeingabe oder von der Kommandozeilenoption <VAR CLASS="literal" >-r</VAR > kommt). Die zweite registrierte globale Variable ist <VAR CLASS="literal" >$argc</VAR >, die die Anzahl der Elemente im Array <VAR CLASS="literal" >$argv</VAR > enthΣlt (<SPAN CLASS="strong" ><B CLASS="emphasis" >nicht</B ></SPAN > die Anzahl Argumente, die dem Skript ⁿbergeben wurden). </P ><P >
Solange die Argumente, die Sie dem Skript ⁿbergeben m÷chten, nicht mit dem Zeichen <VAR CLASS="literal" >-</VAR > beginnen, gibt es nichts besonderes zu beachten. Ein Argument, das mit <VAR CLASS="literal" >-</VAR > beginnt zu ⁿbergeben, wird Probleme bereiten, weil <VAR CLASS="literal" >PHP</VAR > selbst denkt, dass sie verarbeitet werden muss. Benutzen Sie das Argument-Trennzeichen <VAR CLASS="literal" >--</VAR > um das zu vermeiden. Nachdem dieses Trennzeichen von <VAR CLASS="literal" >PHP</VAR > geparst wurde, wird jedes folgende Argument unangetastet an das Skript weitergereicht. </P ><P >
<TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" ># This will not execute the given code but will show the PHP usage $ php -r 'var_dump($argv);' -h Usage: php [options] [-f] <file> [args...] [...] # This will pass the '-h' argument to your script and prevent PHP from showing it's usage $ php -r 'var_dump($argv);' -- -h array(2) { [0]=> string(1) "-" [1]=> string(2) "-h" }</PRE ></TD ></TR ></TABLE > </P ><P >
Dennoch gibt es einen anderen Weg, um <VAR CLASS="literal" >PHP</VAR > fⁿr Kommandozeilenskripte zu benutzen. Sie k÷nnen ein Skript erstellen, in dem die erste Zeile mit <VAR CLASS="literal" >#!/usr/bin/php</VAR > beginnt. Nachfolgend k÷nnen Sie normalen <VAR CLASS="literal" >PHP</VAR >-Code innerhalb der <VAR CLASS="literal" >PHP</VAR >-Start- und -End-Tags setzen. Sobald Sie die Datei ausfⁿhrbar gemacht haben (beispielsweise <VAR CLASS="literal" >chmod +x test</VAR >), kann Ihr Skript wie ein normales Kommandozeilen- oder Perlskript gestartet werden. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >#!/usr/bin/php <?php var_dump($argv); ?></PRE ></TD ></TR ></TABLE > Angenommen, die Datei heisst <TT CLASS="filename" >test</TT > und befindet sich im aktuellen Verzeichnis, dann k÷nnen wir nun folgendes tun: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ chmod 755 test $ ./test -h -- foo array(4) { [0]=> string(6) "./test" [1]=> string(2) "-h" [2]=> string(2) "--" [3]=> string(3) "foo" }</PRE ></TD ></TR ></TABLE > Wie Sie sehen k÷nnen, mⁿssen Sie sich in diesem Fall um nichts weiter kⁿmmern, wenn Sie Ihrem Skript Paramater, die mit <VAR CLASS="literal" >-</VAR > beginnen, ⁿbergeben. </P ><P >
Die Langformen der Optionen sind seit PHP 4.3.3 verfⁿgbar. <DIV CLASS="table" ><A NAME="AEN6724" ></A ><P ><B >Tabelle 39-3. Kommandozeilenoption</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Option</TH ><TH >Langform</TH ><TH >Beschreibung</TH ></TR ></THEAD ><TBODY ><TR ><TD >-s</TD ><TD >--syntax-highlight</TD ><TD >
<P >
Den Quelltext mit farbiger Syntaxhervorhebung anzeigen. </P > <P >
Diese Option benutzt den internen Mechanismus zum Analysieren der Datei, generiert eine <VAR CLASS="literal" >HTML</VAR >-Version mit Syntax-Highlighting davon, und schreibt sie auf die Standardausgabe. Beachten Sie, dass sie nur einen Block von <VAR CLASS="literal" ><code> [...] </code></VAR >-HTML-Tags erzeugt, keine HTML-Header. </P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Diese Option funktioniert nicht zusammen mit <VAR CLASS="literal" >-r</VAR >. </P ></BLOCKQUOTE ></DIV > </TD ></TR ><TR ><TD >-s</TD ><TD >--syntax-highlighting</TD ><TD >
<P >
Ein Alias fⁿr <VAR CLASS="literal" >--syntax-highlight</VAR >. </P > </TD ></TR ><TR ><TD >-w</TD ><TD >--strip</TD ><TD >
<P >
Den Quelltext mit entfernten Kommentaren und Leerzeichen anzeigen. </P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Diese Option funktioniert nicht zusammen mit <VAR CLASS="literal" >-r</VAR >. </P ></BLOCKQUOTE ></DIV > </TD ></TR ><TR ><TD >-f</TD ><TD >--file</TD ><TD >
<P >
Parst und fⁿhrt die angegebene Datei aus. Diese Option ist nicht verbindlich und kann weggelassen werden. Alleinige Angabe des Dateinamens ist ausreichend. </P > </TD ></TR ><TR ><TD >-v</TD ><TD >--version</TD ><TD >
<P >
Schreibt die <VAR CLASS="literal" >PHP</VAR >, <VAR CLASS="literal" >PHP SAPI</VAR > und <VAR CLASS="literal" >Zend</VAR >-Version auf die Standardausgabe. Zum Beispiel: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ php -v PHP 4.3.0 (cli), Copyright (c) 1997-2002 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies</PRE ></TD ></TR ></TABLE > </P > </TD ></TR ><TR ><TD >-c</TD ><TD >--php-ini</TD ><TD >
<P >
Mit dieser Option kann man entweder ein Verzichnis angeben, wo nach <TT CLASS="filename" >php.ini</TT > gesucht werden soll, oder man kann eine eigene <VAR CLASS="literal" >INI</VAR >-Datei direkt angeben (diese muss nicht <TT CLASS="filename" >php.ini</TT > heissen). Zum Beispiel: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ php -c /custom/directory/ my_script.php $ php -c /custom/directory/custom-file.ini my_script.php</PRE ></TD ></TR ></TABLE > </P > </TD ></TR ><TR ><TD >-n</TD ><TD >--no-php-ini</TD ><TD >
<P >
php.ini komplett ignorieren. Diese Option gibt es seit PHP 4.3.0. </P > </TD ></TR ><TR ><TD >-d</TD ><TD >--define</TD ><TD >
<P >
Diese Option erlaubt es Ihnen, einen eigenen Wert fⁿr eine beliebige in <TT CLASS="filename" >php.ini</TT > erlaubte Direktive zu setzen. Die Syntax ist: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >-d configuration_directive[=value]</PRE ></TD ></TR ></TABLE > </P > <P >
Beispiel (Die Zeilen sind aus layouttechnischen Grⁿnden umgebrochen): <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" ># Omitting the value part will set the given configuration directive to "1" $ php -d max_execution_time -r '$foo = ini_get("max_execution_time"); var_dump($foo);' string(1) "1" # Passing an empty value part will set the configuration directive to "" php -d max_execution_time= -r '$foo = ini_get("max_execution_time"); var_dump($foo);' string(0) "" # The configuration directive will be set to anything passed after the '=' character $ php -d max_execution_time=20 -r '$foo = ini_get("max_execution_time"); var_dump($foo);' string(2) "20" $ php -d max_execution_time=doesntmakesense -r '$foo = ini_get("max_execution_time"); var_dump($foo);' string(15) "doesntmakesense"</PRE ></TD ></TR ></TABLE > </P > </TD ></TR ><TR ><TD >-a</TD ><TD >--interactive</TD ><TD >
<P >
<VAR CLASS="literal" >PHP</VAR > interaktiv starten. </P > </TD ></TR ><TR ><TD >-e</TD ><TD >--profile-info</TD ><TD >
<P >
Erweiterte Informationen fⁿr Debugger/Profiler erstellen. </P > </TD ></TR ><TR ><TD >-z</TD ><TD >--zend-extension</TD ><TD >
<P >
Eine <VAR CLASS="literal" >Zend</VAR >-Erweiterung laden. Wenn nur ein Dateiname angegeben wird, versucht <VAR CLASS="literal" >PHP</VAR >, die Erweiterung aus dem momentanen Standard-Bibliotheken-Pfad auf Ihrem System zu laden (auf Linux-Systemen normalerweise in <TT CLASS="filename" >/etc/ld.so.conf</TT > angegeben). Wenn ein Dateiname mit absoluter Pfadinformation ⁿbergeben wird, wird der Bibliotheken-Pfad des Systems nicht benutzt. Ein relativer Dateiname mit Pfadinformationen wird <VAR CLASS="literal" >PHP</VAR > nur veranlassen, zu versuchen, die Erweiterung in einem Pfad relativ zum aktuellen Verzeichnis zu laden. </P > </TD ></TR ><TR ><TD >-l</TD ><TD >--syntax-check</TD ><TD >
<P >
Diese Option stellt eine angenehme Methode bereit, nur eine Syntaxⁿberprⁿfung auf den angegebenen <VAR CLASS="literal" >PHP</VAR >-Code durchzufⁿhren. Bei Erfolg wird der Text <VAR CLASS="literal" >No syntax errors detected in <filename></VAR > auf die Standardausgabe geschrieben und der Rⁿckgabewert ist <VAR CLASS="literal" >0</VAR >. Bei Misserfolg wird der Text <VAR CLASS="literal" >Errors parsing <filename></VAR > zusΣtzlich zur internen Parser-Fehlermeldung auf der Standardausgabe ausgegeben und der Rⁿckgabewert auf <VAR CLASS="literal" >255</VAR > gesetzt. </P > <P >
Diese Option wird keine Fatalen Fehler (wie undefinierte Funktionen) finden. Benutzen Sie die Option <VAR CLASS="literal" >-f</VAR >, wenn sie auch diese ⁿberprⁿfen wollen. </P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Diese Option funktioniert nicht zusammen mit <VAR CLASS="literal" >-r</VAR >. </P ></BLOCKQUOTE ></DIV > </TD ></TR ><TR ><TD >-m</TD ><TD >--modules</TD ><TD >
<P >
Wird diese Option benutzt, gibt <VAR CLASS="literal" >PHP</VAR > die eingebauten (und geladenen) <VAR CLASS="literal" >PHP</VAR >- und <VAR CLASS="literal" >Zend</VAR >-Module aus: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ php -m [PHP Modules] xml tokenizer standard session posix pcre overload mysql mbstring ctype [Zend Modules]</PRE ></TD ></TR ></TABLE > </P > </TD ></TR ><TR ><TD >-i</TD ><TD >--info</TD ><TD >
Diese Kommandozeilenoption ruft <A HREF="#function.phpinfo" ><B CLASS="function" >phpinfo()</B ></A > auf und gibt das Ergebnis aus. Wenn <VAR CLASS="literal" >PHP</VAR > nicht korrekt funktioniert, ist es ratsam, <VAR CLASS="literal" >php -i</VAR > zu benutzen und nachzusehen, ob etwa Fehlermeldungen vor oder statt der Informationstabellen ausgegeben werden. Beachten Sie, dass die Ausgabe in <VAR CLASS="literal" >HTML</VAR > erfolgt und deswegen ziemlich umfangreich ist. </TD ></TR ><TR ><TD >-r</TD ><TD >--run</TD ><TD >
<P >
Diese Option erlaubt die Ausfⁿhrung von <VAR CLASS="literal" >PHP</VAR > direkt aus der Kommandozeile. Die <VAR CLASS="literal" >PHP</VAR >-Start- und -End-Tags (<VAR CLASS="literal" ><?php</VAR > and <VAR CLASS="literal" >?></VAR >) werden <SPAN CLASS="strong" ><B CLASS="emphasis" >nicht ben÷tigt</B ></SPAN > und verursachen Parserfehler, wenn sie dennoch vorhanden sind. </P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Man muss bei Benutzung dieser Form von <VAR CLASS="literal" >PHP</VAR > aufpassen, dass es zu keinen Kollisionen mit Kommandozeilenvariablen kommt. </P ><P >
Dieses Beispiel zeigt einen Parserfehler: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ php -r "$foo = get_defined_constants();" Command line code(1) : Parse error - parse error, unexpected '='</PRE ></TD ></TR ></TABLE > Das Problem hier ist, dass die sh/bash Variablen auch ersetzt, wenn doppelte Anfⁿhrungszeichen benutzt werden (<VAR CLASS="literal" >"</VAR >). Weil die Variable <VAR CLASS="literal" >$foo</VAR > wahrscheinlich nicht definiert ist, passiert nichts. Der tatsΣchliche Code, der an <VAR CLASS="literal" >PHP</VAR > ⁿbergeben wird, sieht so aus: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ php -r " = get_defined_constants();"</PRE ></TD ></TR ></TABLE > Der korrekte Weg wΣre, einfache Anfⁿhrungszeichen zu benutzen (<VAR CLASS="literal" >'</VAR >). Variablen in einfachen Anfⁿhrungszeichen werden von sh/bash nicht ersetzt. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$ php -r '$foo = get_defined_constants(); var_dump($foo);' array(370) { ["E_ERROR"]=> int(1) ["E_WARNING"]=> int(2) ["E_PARSE"]=> int(4) ["E_NOTICE"]=> int(8) ["E_CORE_ERROR"]=> [...]</PRE ></TD ></TR ></TABLE > Wenn Sie eine andere Kommandozeilenumgebung als sh/bash benutzen, k÷nnten Sie auf weitere Probleme stossen. Sie k÷nnen gerne einen Bugreport anmelden oder eine Email an phpdoc@lists.php.net schreiben. Man kann dennoch leicht in Schwierigkeiten kommen, wenn man versucht, Kommandozeilenvariablen zu verwenden oder mit Backslashes zu escapen. Sie wurden gewarnt. </P ></BLOCKQUOTE ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <VAR CLASS="literal" >-r</VAR > ist nur im <SPAN CLASS="emphasis" ><I CLASS="emphasis" >CLI</I ></SPAN >-SAPI verfⁿgbar und nicht im <SPAN CLASS="emphasis" ><I CLASS="emphasis" >CGI</I ></SPAN >-SAPI </P ></BLOCKQUOTE ></DIV > </TD ></TR ><TR ><TD >-h</TD ><TD >--help</TD ><TD >
Mit dieser Option bekommen Sie Informationen ⁿber die tatsΣchlichen Kommandozeilenoptionen und kurze Beschreibungen dazu. </TD ></TR ><TR ><TD >-?</TD ><TD >--usage</TD ><TD >
Ein Alias fⁿr <VAR CLASS="literal" >--help</VAR >. </TD ></TR ></TBODY ></TABLE ></DIV > </P ><P >
Die ausfⁿhrbare PHP-Version kann benutzt werden, um PHP-Skripte vollkommen unabhΣngig von einem Webserver auszufⁿhren. Wenn Sie auf einem UNIX-System arbeiten, sollten Sie Ihrem Skript eine spezielle erste Zeile hinzufⁿgen und es ausfⁿhrbar machen, so dass das System erkennt, welches Programm das Skript ausfⁿhren soll. Auf einem Windows-System k÷nnen Sie <VAR CLASS="literal" >php.exe</VAR > mit der Doppelklick-Option von <VAR CLASS="literal" >.php</VAR >-Dateien verknⁿpfen. Sie k÷nnen auch eine Batchdatei erzeugen, um das Skript von PHP ausfⁿhren zu lassen. Die erste Zeile, die dem Skript hinzugefⁿgt wird, damit es unter UNIX lΣuft, st÷rt unter Windows nicht, so dass Sie auf diese Weise problemlos plattformⁿbergreifende Programme schreiben k÷nnen. Sie finden anschlie▀end ein einfaches Beispiel, wie man ein Kommandozeilenprogramm in PHP schreibt. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6886" ></A ><P ><B >Beispiel 39-1. Ein Skript, das von der Kommandozeile aus ausgefⁿhrt werden soll (script.php)</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >#!/usr/bin/php <?php if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) { ?> This is a command line PHP script with one option. Usage: <?php echo $argv[0]; ?> <option> <option> can be some word you would like to print out. With the --help, -help, -h, or -? options, you can get this help. <?php } else { echo $argv[1]; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In dem oben beschriebenen Skript haben wir die erste Zeile dazu benutzt, anzugeben, dass die Datei von PHP ausgefⁿhrt werden soll. Wir arbeiten hier mit einer CLI-version, deswegen werden keine HTTP-Header ausgegeben. Es gibt zwei Variablen, die Sie in Kommandozeilen-Applikationen mit PHP benutzen k÷nnen: <VAR CLASS="varname" >$argc</VAR > und <VAR CLASS="varname" >$argv</VAR >. Die erste ist die Anzahl der Argumente plus Eins (dem Namen des laufenden Skripts). Die zweite ist ein Array, das die Argumente enthΣlt, beginnend mit dem Skriptnamen als Nummer Null (<VAR CLASS="varname" >$argv[0]</VAR >). </P ><P >
In dem oben genannten Programm haben wir ⁿberprⁿft, ob es weniger oder mehr als zwei Argumente gibt. Falls das Argument <VAR CLASS="literal" >--help</VAR >, <VAR CLASS="literal" >-help</VAR >, <VAR CLASS="literal" >-h</VAR > oder <VAR CLASS="literal" >-?</VAR > war, haben wir die Hilfe-Nachricht mit dem dynamisch ⁿbergebenen Skriptnamen ausgegeben. Wenn wir ein anderes Argument erhalten haben, haben wir es ausgegeben. </P ><P >
Wenn Sie das oben genannte Skript unter UNIX laufen lassen wollen, mⁿssen Sie es zuerst ausfⁿhrbar machen, und es danach einfach als <VAR CLASS="literal" >script.php echothis</VAR > oder <VAR CLASS="literal" >script.php -h</VAR > aufrufen. Unter Windows k÷nnen Sie zu diesem Zweck eine Batchdatei erstellen: </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6902" ></A ><P ><B >Beispiel 39-2. Batchdatei zur Ausfⁿhrung eines Kommandozeilen-Skripts in PHP (script.bat)</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >@c:\php\cli\php.exe script.php %1 %2 %3 %4</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Angenommen, Sie haben das Programm <TT CLASS="filename" >script.php</TT > genannt und die CLI-<TT CLASS="filename" >php.exe</TT > hat den Pfad <TT CLASS="filename" >c:\php\cli\php.exe</TT >, wird diese Batchdatei es mit den von Ihnen ⁿbergebnen Optionen <VAR CLASS="literal" >script.bat echothis</VAR > oder <VAR CLASS="literal" >script.bat -h</VAR > ausfⁿhren. </P ><P >
Sehen Sie sich auch die zusΣtzlichen Funktionen in der Dokumentation der <A HREF="#ref.readline" >Readline</A >-Extension an, um Ihre in PHP geschriebenen Kommandozeilenanwendungen weiterzuentwickeln. </P ></DIV ></DIV ><DIV CLASS="PART" ><A NAME="funcref" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >VI. Funktionsreferenz</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT >I. <A HREF="#ref.apache" >Apache-spezifische Funktionen</A ></DT ><DT >II. <A HREF="#ref.array" >Array Funktionen</A ></DT ><DT >III. <A HREF="#ref.aspell" >Aspell Funktionen [veraltet]</A ></DT ><DT >IV. <A HREF="#ref.bc" >Mathematische Funktionen mit beliebiger Genauigkeit</A ></DT ><DT >V. <A HREF="#ref.bzip2" >Bzip2 Komprimierungsfunktionen</A ></DT ><DT >VI. <A HREF="#ref.calendar" >Kalender-Funktionen</A ></DT ><DT >VII. <A HREF="#ref.ccvs" >CCVS API Funktionen</A ></DT ><DT >VIII. <A HREF="#ref.com" >COM Support Funktionen fⁿr Windows</A ></DT ><DT >IX. <A HREF="#ref.classobj" >Klassen- und Objekt-Funktionen</A ></DT ><DT >X. <A HREF="#ref.cpdf" >ClibPDF Funktionen</A ></DT ><DT >XI. <A HREF="#ref.crack" >Crack Funktionen</A ></DT ><DT >XII. <A HREF="#ref.curl" >cURL, Client URL Bibiothek-Funktionen</A ></DT ><DT >XIII. <A HREF="#ref.cybercash" >Cybercash Bezahl Funktionen</A ></DT ><DT >XIV. <A HREF="#ref.cyrus" >Cyrus IMAP administration Functions</A ></DT ><DT >XV. <A HREF="#ref.ctype" >Character type functions</A ></DT ><DT >XVI. <A HREF="#ref.dba" >dba Datenbank (dbm-style) Abstraktions-Funktionen</A ></DT ><DT >XVII. <A HREF="#ref.datetime" >Datums- und Zeit-Funktionen</A ></DT ><DT >XVIII. <A HREF="#ref.dbase" >dBase Funktionen</A ></DT ><DT >XIX. <A HREF="#ref.dbm" >DBM Datenbankfunktionen</A ></DT ><DT >XX. <A HREF="#ref.dbx" >dbx Funktionen</A ></DT ><DT >XXI. <A HREF="#ref.dbplus" >DB++ Functions</A ></DT ><DT >XXII. <A HREF="#ref.dio" >Direkte Ein-/Ausgabe Funktionen</A ></DT ><DT >XXIII. <A HREF="#ref.dir" >Verzeichnis-Funktionen</A ></DT ><DT >XXIV. <A HREF="#ref.dom" >DOM Functions</A ></DT ><DT >XXV. <A HREF="#ref.domxml" >DOM XML Funktionen</A ></DT ><DT >XXVI. <A HREF="#ref.dotnet" >.NET Funktionen</A ></DT ><DT >XXVII. <A HREF="#ref.errorfunc" >Error Handling and Logging Functions</A ></DT ><DT >XXVIII. <A HREF="#ref.exif" >Exif Functions</A ></DT ><DT >XXIX. <A HREF="#ref.fam" >File Alteration Monitor Functions</A ></DT ><DT >XXX. <A HREF="#ref.fbsql" >FrontBase Funktionen</A ></DT ><DT >XXXI. <A HREF="#ref.filepro" >filePro Funktionen</A ></DT ><DT >XXXII. <A HREF="#ref.filesystem" >Funktionen des Dateisystems</A ></DT ><DT >XXXIII. <A HREF="#ref.fdf" >Forms Data Format Funktionen</A ></DT ><DT >XXXIV. <A HREF="#ref.fribidi" >FriBiDi Funktionen</A ></DT ><DT >XXXV. <A HREF="#ref.ftp" >FTP-Funktionen</A ></DT ><DT >XXXVI. <A HREF="#ref.funchand" >Function Handling functions</A ></DT ><DT >XXXVII. <A HREF="#ref.gettext" >GNU Gettext</A ></DT ><DT >XXXVIII. <A HREF="#ref.gmp" >GMP Functions</A ></DT ><DT >XXXIX. <A HREF="#ref.http" >HTTP functions</A ></DT ><DT >XL. <A HREF="#ref.hw" >Hyperwave Functions</A ></DT ><DT >XLI. <A HREF="#ref.hwapi" >Hyperwave API Functions</A ></DT ><DT >XLII. <A HREF="#ref.iconv" >iconv Funktionen</A ></DT ><DT >XLIII. <A HREF="#ref.image" >Grafik-Funktionen</A ></DT ><DT >XLIV. <A HREF="#ref.imap" >IMAP, POP3 und NNTP Funktionen</A ></DT ><DT >XLV. <A HREF="#ref.ifx" >Informix Funktionen</A ></DT ><DT >XLVI. <A HREF="#ref.ibase" >InterBase-Funktionen</A ></DT ><DT >XLVII. <A HREF="#ref.id3" >ID3 Functions</A ></DT ><DT >XLVIII. <A HREF="#ref.ingres" >Ingres II Funktionen</A ></DT ><DT >XLIX. <A HREF="#ref.ircg" >IRC Gateway Funktionen</A ></DT ><DT >L. <A HREF="#ref.java" >Java</A ></DT ><DT >LI. <A HREF="#ref.ldap" >LDAP Funktionen</A ></DT ><DT >LII. <A HREF="#ref.lzf" >LZF Functions</A ></DT ><DT >LIII. <A HREF="#ref.mail" >Mail-Funktionen</A ></DT ><DT >LIV. <A HREF="#ref.mailparse" >mailparse Functions</A ></DT ><DT >LV. <A HREF="#ref.math" >Mathematische Funktionen</A ></DT ><DT >LVI. <A HREF="#ref.mbstring" >Multibyte String Functions</A ></DT ><DT >LVII. <A HREF="#ref.mcal" >MCAL Funktionen</A ></DT ><DT >LVIII. <A HREF="#ref.mcrypt" >Mcrypt Encryption Functions</A ></DT ><DT >LIX. <A HREF="#ref.mcve" >MCVE Payment Functions</A ></DT ><DT >LX. <A HREF="#ref.memcache" >Memcache Functions</A ></DT ><DT >LXI. <A HREF="#ref.mhash" >Hash Funktionen</A ></DT ><DT >LXII. <A HREF="#ref.mime-magic" >Mimetype Functions</A ></DT ><DT >LXIII. <A HREF="#ref.mssql" >Microsoft SQL Server Funktionen</A ></DT ><DT >LXIV. <A HREF="#ref.ming" >Ming functions for Flash</A ></DT ><DT >LXV. <A HREF="#ref.misc" >Sonstige Funktionen</A ></DT ><DT >LXVI. <A HREF="#ref.mnogo" >mnoGoSearch Funktionen</A ></DT ><DT >LXVII. <A HREF="#ref.msql" >mSQL Funktionen</A ></DT ><DT >LXVIII. <A HREF="#ref.mysql" >MySQL Funktionen</A ></DT ><DT >LXIX. <A HREF="#ref.mysqli" >Verbesserte MySQL Erweiterung</A ></DT ><DT >LXX. <A HREF="#ref.msession" >Mohawk Software Session Handler Funktionen</A ></DT ><DT >LXXI. <A HREF="#ref.muscat" >muscat Functions</A ></DT ><DT >LXXII. <A HREF="#ref.network" >Netzwerk Funktionen</A ></DT ><DT >LXXIII. <A HREF="#ref.ncurses" >Ncurses Terminal Screen Control Functions</A ></DT ><DT >LXXIV. <A HREF="#ref.notes" >Lotus Notes Functions</A ></DT ><DT >LXXV. <A HREF="#ref.nsapi" >NSAPI-specific Functions</A ></DT ><DT >LXXVI. <A HREF="#ref.odbc" >ODBC Funktionen</A ></DT ><DT >LXXVII. <A HREF="#ref.objaggregation" >Object Aggregation/Composition Functions</A ></DT ><DT >LXXVIII. <A HREF="#ref.oci8" >Oracle 8 Funktionen</A ></DT ><DT >LXXIX. <A HREF="#ref.openssl" >OpenSSL Funktionen</A ></DT ><DT >LXXX. <A HREF="#ref.oracle" >Oracle Funktionen</A ></DT ><DT >LXXXI. <A HREF="#ref.ovrimos" >Ovrimos SQL Functions</A ></DT ><DT >LXXXII. <A HREF="#ref.outcontrol" >Funktionen zur Ausgabesteuerung</A ></DT ><DT >LXXXIII. <A HREF="#ref.overload" >Object property and method call overloading</A ></DT ><DT >LXXXIV. <A HREF="#ref.pdf" >PDF Funktionen</A ></DT ><DT >LXXXV. <A HREF="#ref.pfpro" >Verisign Payflow Pro Funktionen</A ></DT ><DT >LXXXVI. <A HREF="#ref.info" >PHP Optionen & Informationen</A ></DT ><DT >LXXXVII. <A HREF="#ref.posix" >POSIX Funktionen</A ></DT ><DT >LXXXVIII. <A HREF="#ref.pgsql" >PostgreSQL Funktionen</A ></DT ><DT >LXXXIX. <A HREF="#ref.pcntl" >Process Control Functions</A ></DT ><DT >XC. <A HREF="#ref.exec" >Funktionen zur Programmausfⁿhrung</A ></DT ><DT >XCI. <A HREF="#ref.printer" >Printer Functions</A ></DT ><DT >XCII. <A HREF="#ref.pspell" >Pspell Funktionen</A ></DT ><DT >XCIII. <A HREF="#ref.readline" >GNU Readline</A ></DT ><DT >XCIV. <A HREF="#ref.recode" >GNU Recode Funktionen</A ></DT ><DT >XCV. <A HREF="#ref.pcre" >RegulΣre Ausdrⁿcke Funktionen (Perl-kompatibel)</A ></DT ><DT >XCVI. <A HREF="#ref.qtdom" >qtdom Functions</A ></DT ><DT >XCVII. <A HREF="#ref.regex" >RegulΣre Ausdrⁿcke Funktionen (POSIX erweitert)</A ></DT ><DT >XCVIII. <A HREF="#ref.sem" >Semaphor und Shared Memory Funktionen</A ></DT ><DT >XCIX. <A HREF="#ref.sesam" >SESAM Datenbankfunktionen</A ></DT ><DT >C. <A HREF="#ref.session" >Session Funktionen</A ></DT ><DT >CI. <A HREF="#ref.shmop" >Shared Memory Funktionen (Gemeinsamer Speicher)</A ></DT ><DT >CII. <A HREF="#ref.simplexml" >SimpleXML functions</A ></DT ><DT >CIII. <A HREF="#ref.soap" >SOAP Functions</A ></DT ><DT >CIV. <A HREF="#ref.sqlite" >SQLite</A ></DT ><DT >CV. <A HREF="#ref.swf" >Shockwave Flash Funktionen</A ></DT ><DT >CVI. <A HREF="#ref.snmp" >SNMP Funktionen</A ></DT ><DT >CVII. <A HREF="#ref.sockets" >Socket Funktionen</A ></DT ><DT >CVIII. <A HREF="#ref.spl" >Standard PHP Library (SPL) Functions</A ></DT ><DT >CIX. <A HREF="#ref.stream" >Stream Functions</A ></DT ><DT >CX. <A HREF="#ref.strings" >String-Funktionen</A ></DT ><DT >CXI. <A HREF="#ref.sybase" >Sybase Funktionen</A ></DT ><DT >CXII. <A HREF="#ref.tcpwrap" >TCP Wrappers Functions</A ></DT ><DT >CXIII. <A HREF="#ref.tidy" >Tidy Functions</A ></DT ><DT >CXIV. <A HREF="#ref.tokenizer" >Tokenizer Functions</A ></DT ><DT >CXV. <A HREF="#ref.url" >URL Funktionen</A ></DT ><DT >CXVI. <A HREF="#ref.variables" >Variablen-Funktionen</A ></DT ><DT >CXVII. <A HREF="#ref.vpopmail" >vpopmail Functions</A ></DT ><DT >CXVIII. <A HREF="#ref.w32api" >W32api Functions</A ></DT ><DT >CXIX. <A HREF="#ref.wddx" >WDDX Funktionen</A ></DT ><DT >CXX. <A HREF="#ref.xml" >XML Parser Functions</A ></DT ><DT >CXXI. <A HREF="#ref.xmlrpc" >XML-RPC Functions</A ></DT ><DT >CXXII. <A HREF="#ref.xdiff" >xdiff Functions</A ></DT ><DT >CXXIII. <A HREF="#ref.xsl" >XSL functions</A ></DT ><DT >CXXIV. <A HREF="#ref.xslt" >XSLT Funktionen</A ></DT ><DT >CXXV. <A HREF="#ref.yaz" >YAZ Functions</A ></DT ><DT >CXXVI. <A HREF="#ref.nis" >YP/NIS Funktionen</A ></DT ><DT >CXXVII. <A HREF="#ref.zip" >ZIP Funktionen (Lesezugriff)</A ></DT ><DT >CXXVIII. <A HREF="#ref.zlib" >Zlib Komprimierungsfunktionen</A ></DT ></DL ></DIV ></DIV ><DIV CLASS="reference" ><A NAME="ref.apache" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >I. Apache-spezifische Funktionen</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN6918" ></A ><DIV CLASS="section" ><H2 CLASS="section" ><A NAME="apache.intro" >Einfⁿhrung</A ></H2 ><P >
Diese Funktionen stehen nur zur Verfⁿgung, wenn PHP als Apache Modul lΣuft. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die Servervariable PATH_TRANSLATED wird bei Verwendung des Apache 2 SAPI nicht mehr lΣnger implizit gesetzt. Im Gegensatz hierzu wird bei Verwendung des Apache 1 SAPI PATH_TRANSLATED, falls von Apache nicht zur Verfⁿgung gestellt, auf den gleichen Wert wie die Servervariable SCRIPT_FILENAME gesetzt. Die VerΣnderung unter Apache 2 wurde vorgenommen um der <A HREF="http://hoohoo.ncsa.uiuc.edu/cgi/" TARGET="_top" >CGI Spezification</A > zu entsprechen. Fⁿr weitere Informationen shauen Sie bitte unter <A HREF="http://bugs.php.net/23610" TARGET="_top" >bug #23610</A > nach. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="apache.installation" >Installation</A ></H2 ><P >
Informationen zur Installation von PHP unter Apache finden Sie im Kapitel Installation unter dem <A HREF="#install.apache" >Abschnitt zu Apache</A > </P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="apache.configuration" >Laufzeit Konfiguration</A ></H2 ><P >
Das Verhalten des Apache PHP-Moduls wird durch Einstellungen in der <TT CLASS="filename" >php.ini</TT > beeinflusst. Konfigurationseinstellungen aus der <TT CLASS="filename" >php.ini</TT > k÷nnen Sie durch <A HREF="#configuration.changes.apache" >php_flag</A > Einstellungen in der Server Konfigurationsdatei oder lokal in <TT CLASS="filename" >.htaccess</TT > Dateien ⁿberschreiben. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6937" ></A ><P ><B >Beispiel 1. Parsen von PHP fⁿr ein Verzeichnis ausschalten mittels <TT CLASS="filename" >.htaccess</TT ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >php_flag engine off</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
<DIV CLASS="table" ><A NAME="AEN6942" ></A ><P ><B >Tabelle 1. Apache Konfigurations Optionen</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><COL><THEAD ><TR ><TH >Bezeichnung</TH ><TH >Grundeinstellung</TH ><TH >─nderbar</TH ><TH >Bedeutung</TH ></TR ></THEAD ><TBODY ><TR ><TD >engine</TD ><TD >On</TD ><TD >PHP_INI_ALL</TD ><TD >Parsen von PHP ein/-auschalten</TD ></TR ><TR ><TD >child_terminate</TD ><TD >Off</TD ><TD >PHP_INI_ALL</TD ><TD >
Gibt an, ob PHP Skripte veranlassen, dass Kindprozesse nach dem Request beendet werden, siehe auch <A HREF="#function.apache-child-terminate" ><B CLASS="function" >apache_child_terminate()</B ></A >. </TD ></TR ><TR ><TD >last_modified</TD ><TD >Off</TD ><TD >PHP_INI_ALL</TD ><TD >
Das letzte Datum der ─nderung des PHP Skripts wird als Last-Modified: header fⁿr diesen Request gesendet. </TD ></TR ><TR ><TD >xbithack</TD ><TD >Off</TD ><TD >PHP_INI_ALL</TD ><TD >
Dateien mit gesetztem ausfⁿhrbaren Bit werden unabhΣngig von ihrer Dateiendung durch PHP geparst. </TD ></TR ></TBODY ></TABLE ></DIV > </P ><P >Hier eine kurze ErklΣrung der Konfigurationsoptionen:</P ><P >
<P ></P ><DIV CLASS="variablelist" ><DL ><DT ><A NAME="ini.engine" ></A ><VAR CLASS="parameter" >engine</VAR > <A HREF="#language.types.boolean" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P >
Diese Option ist in erster Linie nur sinnvoll, wenn PHP als Modul in den Apache einkompiliert wurde. Sie wird von Seiten benutzt, die den PHP-Parser fⁿr bestimmte Verzeichnisse oder virtuelle Server aus- bzw. einschalten wollen. Wenn Sie <KBD CLASS="userinput" >engine off</KBD > in den dafⁿr vorgesehenen Bl÷cken in der <TT CLASS="filename" >httpd.conf</TT > Datei benutzen, kann PHP aktiviert bzw. deaktiviert werden. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="apache.resources" >Resource Typen</A ></H2 ><P >Diese Erweiterung definiert keine Resource-Typen.</P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="apache.constants" >Vordefinierte Konstanten</A ></H2 ><P >Diese Erweiterung definiert keine Konstanten.</P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT ><A HREF="#function.apache-child-terminate" >apache_child_terminate</A > -- Beendet einen Apache Prozess nach der Anfrage</DT ><DT ><A HREF="#function.apache-get-modules" >apache_get_modules</A > -- Liste der geladenen Apachemodule </DT ><DT ><A HREF="#function.apache-get-version" >apache_get_version</A > -- Liefert die Version von Apache </DT ><DT ><A HREF="#function.apache-getenv" >apache_getenv</A > -- Get an Apache subprocess_env variable </DT ><DT ><A HREF="#function.apache-lookup-uri" >apache_lookup_uri</A > -- Fⁿhrt eine Teilanfrage fⁿr eine URI durch und liefert alle Informationen darⁿber zurⁿck. </DT ><DT ><A HREF="#function.apache-note" >apache_note</A > -- Setzt und liest Apache Notes</DT ><DT ><A HREF="#function.apache-request-headers" >apache_request_headers</A > -- Liefert alle HTTP-Header der aktuellen Anfrage</DT ><DT ><A HREF="#function.apache-response-headers" >apache_response_headers</A > -- Liefert alle HTTP-Antwort Header </DT ><DT ><A HREF="#function.apache-setenv" >apache_setenv</A > -- Setzt eine Apache subprocess_env Variable</DT ><DT ><A HREF="#function.ascii2ebcdic" >ascii2ebcdic</A > -- Zeichenkette von ASCII in EBCDIC ⁿbersetzen</DT ><DT ><A HREF="#function.ebcdic2ascii" >ebcdic2ascii</A > -- Zeichenkette von EBCDIC in ASCII ⁿbersetzen</DT ><DT ><A HREF="#function.getallheaders" >getallheaders</A > -- Liefert alle HTTP-Header der aktuellen Anfrage aus </DT ><DT ><A HREF="#function.virtual" >virtual</A > -- Fⁿhrt eine Apache-Unteranfrage durch</DT ></DL ></DIV ></DIV ><H1 ><A NAME="function.apache-child-terminate" ></A >apache_child_terminate</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN6991" ></A ><P > (PHP 4 >= 4.0.5, PHP 5)</P >apache_child_terminate -- Beendet einen Apache Prozess nach der Anfrage</DIV ><DIV CLASS="refsect1" ><A NAME="AEN6994" ></A ><H2 >Beschreibung</H2 >bool <B CLASS="methodname" >apache_child_terminate</B > ( void )<BR ></BR ><P >
<B CLASS="function" >apache_child_terminate()</B > beendet den Apache Prozess, der die aktuelle PHP-Anfrage ausfⁿhrt, nachdem diese vollstΣndig ausgefⁿhrt wurde. Sie k÷nnen diese Funktion benutzen, nachdem Sie ein Skript mit hohem Speicherverbrauch ausgefⁿhrt haben. Der verbrauchte Speicher wird gew÷hnlich nur intern, aber nicht fⁿr das Betriebsystem, freigegeben. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die Verfⁿgbarkeit dieses Features wird durch die Anweisung <VAR CLASS="option" >child_terminate</VAR > in der <TT CLASS="filename" >php.ini</TT > beeinflusst. Die Standardeinstellung ist <VAR CLASS="literal" >off</VAR >. </P ><P >
Dieses Feature steht bei multithreaded Versionen von Apache, wie der win32 Version, nicht zur Verfⁿgung. </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.exit" ><B CLASS="function" >exit()</B ></A >. </P ></DIV ><H1 ><A NAME="function.apache-get-modules" ></A >apache_get_modules</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7011" ></A ><P > (PHP 4 >= 4.3.2, PHP 5)</P >apache_get_modules -- Liste der geladenen Apachemodule </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7014" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >apache_get_modules</B > ( void )<BR ></BR ><P >
Diese Funktion liefert Ihnen ein Array mit allen geladenen Apachemodulen. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7022" ></A ><P ><B >Beispiel 1. <B CLASS="function" >apache_get_modules()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php print_r(apache_get_modules()); ?></PRE ></TD ></TR ></TABLE ><P >
Die Ausgabe des obigen Programms sieht ähnlich wie folgt aus: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => core [1] => http_core [2] => mod_so [3] => sapi_apache2 [4] => mod_mime [5] => mod_rewrite )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.apache-get-version" ></A >apache_get_version</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7029" ></A ><P > (PHP 4 >= 4.3.2, PHP 5)</P >apache_get_version -- Liefert die Version von Apache </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7032" ></A ><H2 >Beschreibung</H2 >string <B CLASS="methodname" >apache_get_version</B > ( void )<BR ></BR ><P >
<B CLASS="function" >apache_get_version()</B > gibt die Apache-Version als Zeichenkette zurⁿck, oder <TT CLASS="constant" ><B >FALSE</B ></TT > bei einem Fehler. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7041" ></A ><P ><B >Beispiel 1. <B CLASS="function" >apache_get_version()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $version = apache_get_version(); echo "$version\n"; ?></PRE ></TD ></TR ></TABLE ><P >
Die Ausgabe des obigen Programms sieht Σhnlich wie folgt aus: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Apache/1.3.29 (Unix) PHP/4.3.4</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Siehe auch <A HREF="#function.phpinfo" ><B CLASS="function" >phpinfo()</B ></A >. </P ></DIV ><H1 ><A NAME="function.apache-getenv" ></A >apache_getenv</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7050" ></A ><P > (PHP 4 >= 4.3.0, PHP 5)</P >apache_getenv -- Get an Apache subprocess_env variable </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7053" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >apache_getenv</B > ( string variable [, bool walk_to_top])<BR ></BR ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfⁿgung. </P ></TD ></TR ></TABLE ></DIV ></DIV ><H1 ><A NAME="function.apache-lookup-uri" ></A >apache_lookup_uri</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7067" ></A ><P > (PHP 3>= 3.0.4, PHP 4 , PHP 5)</P >apache_lookup_uri -- Fⁿhrt eine Teilanfrage fⁿr eine URI durch und liefert alle Informationen darⁿber zurⁿck. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7070" ></A ><H2 >Beschreibung</H2 >object <B CLASS="methodname" >apache_lookup_uri</B > ( string Dateiname)<BR ></BR ><P >
<B CLASS="function" >apache_lookup_uri()</B > fⁿhrt eine Teilanfrage ⁿber eine bestimmte URI durch. Die Abfrage geht gerade soweit, dass alle wichtigen Informationen ⁿber die gegebene Quelle gesammelt werden k÷nnen. Als Resultat wird eine Klasse zurⁿckgeliefert, die folgende Elemente enthΣlt: <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD >status</TD ></TR ><TR ><TD >the_request</TD ></TR ><TR ><TD >status_line</TD ></TR ><TR ><TD >method</TD ></TR ><TR ><TD >content_type</TD ></TR ><TR ><TD >handler</TD ></TR ><TR ><TD >uri</TD ></TR ><TR ><TD >filename</TD ></TR ><TR ><TD >path_info</TD ></TR ><TR ><TD >args</TD ></TR ><TR ><TD >boundary</TD ></TR ><TR ><TD >no_cache</TD ></TR ><TR ><TD >no_local_copy</TD ></TR ><TR ><TD >allowed</TD ></TR ><TR ><TD >send_bodyct</TD ></TR ><TR ><TD >bytes_sent</TD ></TR ><TR ><TD >byterange</TD ></TR ><TR ><TD >clength</TD ></TR ><TR ><TD >unparsed_uri</TD ></TR ><TR ><TD >mtime</TD ></TR ><TR ><TD >request_time</TD ></TR ></TBODY ></TABLE ><P ></P > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7103" ></A ><P ><B >Beispiel 1. <B CLASS="function" >apache_lookup_uri()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $info = apache_lookup_uri('index.php?var=value'); print_r($info); if (file_exists($info->filename)) { echo 'file exists!'; } ?></PRE ></TD ></TR ></TABLE ><P >
Die Ausgabe des obigen Beispiel sieht so Σhnlich aus wie folgt: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >stdClass Object ( [status] => 200 [the_request] => GET /dir/file.php HTTP/1.1 [method] => GET [mtime] => 0 [clength] => 0 [chunked] => 0 [content_type] => application/x-httpd-php [no_cache] => 0 [no_local_copy] => 1 [unparsed_uri] => /dir/index.php?var=value [uri] => /dir/index.php [filename] => /home/htdocs/dir/index.php [args] => var=value [allowed] => 0 [sent_bodyct] => 0 [bytes_sent] => 0 [request_time] => 1074282764 ) file exists!</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <B CLASS="function" >apache_lookup_uri()</B > funktioniert nur, wenn PHP als Apache-Modul eingerichtet wurde. </P ></BLOCKQUOTE ></DIV ></DIV ><H1 ><A NAME="function.apache-note" ></A >apache_note</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7113" ></A ><P > (PHP 3>= 3.0.2, PHP 4 , PHP 5)</P >apache_note -- Setzt und liest Apache Notes</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7116" ></A ><H2 >Beschreibung</H2 >string <B CLASS="methodname" >apache_note</B > ( string note_name [, string note_value])<BR ></BR ><P >
<B CLASS="function" >apache_note()</B > ist eine Apache-spezifische Funktion, die EintrΣge in Apaches <VAR CLASS="literal" >notes</VAR >-Tabelle setzt und ausliest. Wenn sie mit einem einzelnen Parameter aufgerufen wird, gibt sie den aktuellen Wert der Note <VAR CLASS="parameter" >note_name</VAR > zurⁿck. Bei zwei Parametern wird der Eintrag <VAR CLASS="parameter" >note_name</VAR > auf den zweiten Parameter <VAR CLASS="parameter" >note_value</VAR > gesetzt und liefert den bisherigen Wert zurⁿck. </P ></DIV ><H1 ><A NAME="function.apache-request-headers" ></A >apache_request_headers</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7134" ></A ><P > (PHP 4 >= 4.3.0, PHP 5)</P >apache_request_headers -- Liefert alle HTTP-Header der aktuellen Anfrage</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7137" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >apache_request_headers</B > ( void )<BR ></BR ><P >
Die Funktion <B CLASS="function" >apache_request_headers()</B > liefert ein assoziatives Array zurⁿck, das alle HTTP-Header-Informationen der aktuellen HTTP-Anfrage enthΣlt. Diese Funktion wird nur untersstⁿtzt, wenn PHP als Modul fⁿr <SPAN CLASS="productname" >Apache</SPAN > lΣuft. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7147" ></A ><P ><B >Beispiel 1. <B CLASS="function" >apache_request_headers()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $headers = apache_request_headers(); foreach ($headers as $header => $value) { echo "$header: $value <br />\n"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In frⁿheren Versionen als PHP 4.3.0 hie▀ die Funktion <B CLASS="function" >apache_request_headers()</B > <A HREF="#function.getallheaders" ><B CLASS="function" >getallheaders()</B ></A >. Mit PHP 4.3.0 ist <A HREF="#function.getallheaders" ><B CLASS="function" >getallheaders()</B ></A > ein Alias fⁿr <B CLASS="function" >apache_request_headers()</B >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die Werte der gebrΣuchlichsten CGI-Variablen k÷nnen Sie auch durch das Auslesen der Umgebungsvariablen erhalten. Dieser Weg funktioniert unabhΣngig davon, ob Sie PHP als Modul fⁿr <SPAN CLASS="productname" >Apache</SPAN > benutzen. Benutzen Sie <A HREF="#function.phpinfo" ><B CLASS="function" >phpinfo()</B ></A >, um eine Liste aller zur Verfⁿgung stehenden <A HREF="#language.variables.predefined" >Umgebungsvariablen</A > zu erhalten. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Ab PHP 4.3.3 k÷nnen Sie diese Funktion auch mit dem <A HREF="#ref.nsapi" >NSAPI Server Modul</A > fⁿr Netscape/iPlanet/SunONE Webserver nutzen.</P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.apache-response-headers" ><B CLASS="function" >apache_response_headers()</B ></A >. </P ></DIV ><H1 ><A NAME="function.apache-response-headers" ></A >apache_response_headers</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7168" ></A ><P > (PHP 4 >= 4.3.0, PHP 5)</P >apache_response_headers -- Liefert alle HTTP-Antwort Header </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7171" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >apache_response_headers</B > ( void )<BR ></BR ><P >
Rⁿckgabewert: ein assoziatives Array mit allen Apache-Antwort Headern. </P ><P >
Siehe auch <A HREF="#function.apache-request-headers" ><B CLASS="function" >apache_request_headers()</B ></A > und <A HREF="#function.headers-sent" ><B CLASS="function" >headers_sent()</B ></A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Ab PHP 4.3.3 k÷nnen Sie diese Funktion auch mit dem <A HREF="#ref.nsapi" >NSAPI Server Modul</A > fⁿr Netscape/iPlanet/SunONE Webserver nutzen.</P ></BLOCKQUOTE ></DIV ></DIV ><H1 ><A NAME="function.apache-setenv" ></A >apache_setenv</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7185" ></A ><P > (PHP 4 >= 4.2.0, PHP 5)</P >apache_setenv -- Setzt eine Apache subprocess_env Variable</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7188" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >apache_setenv</B > ( string Variable, string Wert [, bool walk_to_top])<BR ></BR ><P >
<DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfⁿgung. </P ></TD ></TR ></TABLE ></DIV > </P ></DIV ><H1 ><A NAME="function.ascii2ebcdic" ></A >ascii2ebcdic</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7206" ></A ><P > (PHP 3>= 3.0.17)</P >ascii2ebcdic -- Zeichenkette von ASCII in EBCDIC ⁿbersetzen</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7209" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >ascii2ebcdic</B > ( string ascii_str)<BR ></BR ><P >
<B CLASS="function" >ascii2ebcdic()</B > ist eine Apache-spezifische Funktion, die nur auf Systemen mit EBCDIC-Zeichensatz existiert (Betriebssysteme: OS/390, BS2000). Die Funktion setzt die ASCII-kodierte Zeichenkette <VAR CLASS="parameter" >ascii_str</VAR > in ihre Σquivalente EBCDIC-Darstellung um (binΣrfest), und liefert das konvertierte Ergebnis zurⁿck. </P ><P >
Siehe auch die Umkehrfunktion <A HREF="#function.ebcdic2ascii" ><B CLASS="function" >ebcdic2ascii()</B ></A >. </P ></DIV ><H1 ><A NAME="function.ebcdic2ascii" ></A >ebcdic2ascii</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7223" ></A ><P > (PHP 3>= 3.0.17)</P >ebcdic2ascii -- Zeichenkette von EBCDIC in ASCII ⁿbersetzen</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7226" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >ebcdic2ascii</B > ( string ebcdic_str)<BR ></BR ><P >
<B CLASS="function" >ebcdic2ascii()</B > ist eine Apache-spezifische Funktion, die nur auf Systemen mit EBCDIC-Zeichensatz existiert (Betriebssysteme: OS/390, BS2000). Die Funktion setzt die EBCDIC-kodierte Zeichenkette <VAR CLASS="parameter" >ebcdic_str</VAR > in ihre Σquivalente ASCII-Darstellung um (binΣrfest), und liefert das konvertierte Ergebnis zurⁿck. </P ><P >
Siehe auch die Umkehrfunktion <A HREF="#function.ascii2ebcdic" ><B CLASS="function" >ascii2ebcdic()</B ></A >. </P ></DIV ><H1 ><A NAME="function.getallheaders" ></A >getallheaders</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7240" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >getallheaders -- Liefert alle HTTP-Header der aktuellen Anfrage aus </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7243" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >getallheaders</B > ( void )<BR ></BR ><P >
<B CLASS="function" >getallheaders()</B > ist ein Alias fⁿr die Funktion <A HREF="#function.apache-request-headers" ><B CLASS="function" >apache_request_headers()</B ></A >. Die Funktion liefert ein assoziatives Array zurⁿck, das alle HTTP-Header der aktuellen HTTP-Anfrage enthΣlt. Lesen Sie bitte die Dokumentation zu <A HREF="#function.apache-request-headers" ><B CLASS="function" >apache_request_headers()</B ></A > um mehr Informationen ⁿber die Funktionsweise zu erhalten. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In PHP 4.3.0 wurde aus <B CLASS="function" >getallheaders()</B > ein Alias fⁿr <A HREF="#function.apache-request-headers" ><B CLASS="function" >apache_request_headers()</B ></A >. TatsΣchlich wurde die Funktion umbenannt. Der Grund: diese Funktion funktioniert nur, wenn PHP als Modul fⁿr <SPAN CLASS="productname" >Apache</SPAN > kompiliert wurde. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Ab PHP 4.3.3 k÷nnen Sie diese Funktion auch mit dem <A HREF="#ref.nsapi" >NSAPI Server Modul</A > fⁿr Netscape/iPlanet/SunONE Webserver nutzen.</P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.apache-request-headers" ><B CLASS="function" >apache_request_headers()</B ></A >. </P ></DIV ><H1 ><A NAME="function.virtual" ></A >virtual</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7264" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >virtual -- Fⁿhrt eine Apache-Unteranfrage durch</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7267" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >virtual</B > ( string filename)<BR ></BR ><P >
<B CLASS="function" >virtual()</B > ist eine Apache-spezifische Funktion, die <!--#include virtual... --> aus mod_include entspricht. Diese Funktion fⁿhrt eine Unteranfrage durch. Das ist nⁿtzlich fⁿr das Einbinden von CGI-Skripten, .shtml-Dateien oder allen anderen Inhalten, die von Apache verarbeitet werden sollen. Beachten Sie bitte, dass ein CGI-Skript gⁿltige CGI-Header generieren muss, mindestens einen Content-type-Header. </P ><P >
Um die Unteranfrage auszufⁿhren, werden alle Buffer beendet und der Inhalt im Browser ausgegeben. Anstehende Header werden ebenfalls abgeschickt. </P ><P >
Seit PHP 4.0.6 k÷nnen Sie <B CLASS="function" >virtual()</B > fⁿr PHP Dateien benutzen. Trotzdem ist es typischerweise besser, <A HREF="#function.include" ><B CLASS="function" >include()</B ></A > oder <A HREF="#function.require" ><B CLASS="function" >require()</B ></A > zu benutzen um PHP Dateien einzubinden. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Ab PHP 4.3.3 k÷nnen Sie diese Funktion auch mit dem <A HREF="#ref.nsapi" >NSAPI Server Modul</A > fⁿr Netscape/iPlanet/SunONE Webserver nutzen.</P ></BLOCKQUOTE ></DIV ></DIV ></DIV ><DIV CLASS="reference" ><A NAME="ref.array" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >II. Array Funktionen</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN7288" ></A ><DIV CLASS="section" ><H2 CLASS="section" ><A NAME="array.intro" >Einfⁿhrung</A ></H2 ><P >
Diese Funktionen erlauben es, Arrays auf verschiedene Arten zu manipulieren bzw. mit ihnen zu interagieren. Arrays sind wichtig, um Gruppen von Variablen zu verwalten, zu bearbeiten, oder zu speichern. </P ><P >
Es werden einfache und mehrdimensionale Arrays unterstⁿtzt, welche entweder vom Benutzer oder von einer anderen Funktion erstellt werden k÷nnen. Es gibt bestimmte Datenbankfunktionen, welche die Ergebnisse aus Datenbankabfragen in Arrays speichern, und verschiedene andere Funktionen, die Arrays als Rⁿckgabewerte haben. </P ><P >
Im Abschnitt <A HREF="#language.types.array" >Arrays</A > finden Sie eine detaillierte ErklΣrung, wie Arrays in PHP implementiert sind und wie sie benutzt werden. </P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="array.requirements" >Anforderungen</A ></H2 ><P >Diese Erweiterung ben÷tigt keine externen Bibliotheken.</P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="array.installation" >Installation</A ></H2 ><P >Fⁿr diese Funktionen ist keine Installation erforderlich, diese geh÷ren zum Grundbestand von PHP.</P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="array.configuration" >Laufzeit Konfiguration</A ></H2 ><P >Diese Erweiterung definiert keine Konfigurationseinstellungen in der <TT CLASS="filename" >php.ini</TT >.</P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="array.resources" >Resource Typen</A ></H2 ><P >Diese Erweiterung definiert keine Resource-Typen.</P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="array.constants" >Vordefinierte Konstanten</A ></H2 ><P >Die aufgelisteten Konstanten stehen immer zur Verfⁿgung, da sie zum Grundbestand von PHP geh÷ren.</P ><P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="constant" ><B >CASE_LOWER</B ></TT > (<A HREF="#language.types.integer" >integer</A >)</DT ><DD ><P >
<TT CLASS="constant" ><B >CASE_LOWER</B ></TT > wird benⁿtzt mit <A HREF="#function.array-change-key-case" ><B CLASS="function" >array_change_key_case()</B ></A > um die Schlⁿssel von Arrays in Kleinbuchstaben umzuwandeln. Das ist auch der Standard fⁿr <A HREF="#function.array-change-key-case" ><B CLASS="function" >array_change_key_case()</B ></A >. </P ></DD ><DT ><TT CLASS="constant" ><B >CASE_UPPER</B ></TT > (<A HREF="#language.types.integer" >integer</A >)</DT ><DD ><P >
<TT CLASS="constant" ><B >CASE_UPPER</B ></TT > wird benⁿtzt mit <A HREF="#function.array-change-key-case" ><B CLASS="function" >array_change_key_case()</B ></A > um die Schlⁿssel von Arrays in Gro▀buchstaben umzuwandeln. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="array.seealso" >Siehe auch</A ></H2 ><P >
Siehe auch <A HREF="#function.is-array" ><B CLASS="function" >is_array()</B ></A >, <A HREF="#function.explode" ><B CLASS="function" >explode()</B ></A >, <A HREF="#function.implode" ><B CLASS="function" >implode()</B ></A >, <A HREF="#function.split" ><B CLASS="function" >split()</B ></A >, <A HREF="#function.preg-split" ><B CLASS="function" >preg_split()</B ></A > und <A HREF="#function.unset" ><B CLASS="function" >unset()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT ><A HREF="#function.array-change-key-case" >array_change_key_case</A > -- Liefert ein Array mit allen String-Schlⁿsseln in Klein- oder Gro▀buchstaben </DT ><DT ><A HREF="#function.array-chunk" >array_chunk</A > -- Splittet ein Array in Teile auf</DT ><DT ><A HREF="#function.array-combine" >array_combine</A > -- Creates an array by using one array for keys and another for its values </DT ><DT ><A HREF="#function.array-count-values" >array_count_values</A > -- ZΣhlt die Werte eines Arrays</DT ><DT ><A HREF="#function.array-diff-assoc" >array_diff_assoc</A > -- Computes the difference of arrays with additional index check</DT ><DT ><A HREF="#function.array-diff-key" >array_diff_key</A > -- Computes the difference of arrays using keys for comparison</DT ><DT ><A HREF="#function.array-diff-uassoc" >array_diff_uassoc</A > -- Computes the difference of arrays with additional index check which is performed by a user supplied callback function </DT ><DT ><A HREF="#function.array-diff-ukey" >array_diff_ukey</A > -- Computes the difference of arrays using a callback function on the keys for comparison</DT ><DT ><A HREF="#function.array-diff" >array_diff</A > -- Ermittelt die Unterschiede von Arrays</DT ><DT ><A HREF="#function.array-fill" >array_fill</A > -- Fⁿllt ein Array mit Werten</DT ><DT ><A HREF="#function.array-filter" >array_filter</A > -- Filtert Elemente eines Arrays mittels einer Callback-Funktion </DT ><DT ><A HREF="#function.array-flip" >array_flip</A > -- Vertauscht Werte und Schlⁿssel in einem Array</DT ><DT ><A HREF="#function.array-intersect-assoc" >array_intersect_assoc</A > -- Computes the intersection of arrays with additional index check</DT ><DT ><A HREF="#function.array-intersect-key" >array_intersect_key</A > -- Computes the intersection of arrays using keys for comparison</DT ><DT ><A HREF="#function.array-intersect-uassoc" >array_intersect_uassoc</A > -- Computes the intersection of arrays with additional index check. The indexes are compared by using a callback function</DT ><DT ><A HREF="#function.array-intersect-ukey" >array_intersect_ukey</A > -- Computes the intersection of arrays using a callback function on the keys for comparison</DT ><DT ><A HREF="#function.array-intersect" >array_intersect</A > -- Ermittelt die Schnittmenge von Arrays</DT ><DT ><A HREF="#function.array-key-exists" >array_key_exists</A > -- Prⁿft, ob ein Schlⁿssel in einem Array existiert</DT ><DT ><A HREF="#function.array-keys" >array_keys</A > -- Liefert alle Schlⁿssel eines Arrays</DT ><DT ><A HREF="#function.array-map" >array_map</A > -- Wendet eine Callback-Funktion auf die Elemente von Arrays an </DT ><DT ><A HREF="#function.array-merge-recursive" >array_merge_recursive</A > -- Fⁿhrt zwei oder mehr Arrays rekursiv zusammen </DT ><DT ><A HREF="#function.array-merge" >array_merge</A > -- Fⁿhrt zwei oder mehr Arrays zusammen</DT ><DT ><A HREF="#function.array-multisort" >array_multisort</A > -- Sortiert mehrere oder multidimensionale Arrays</DT ><DT ><A HREF="#function.array-pad" >array_pad</A > -- Vergr÷▀ert ein Array auf die spezifizierte LΣnge mit einem Wert </DT ><DT ><A HREF="#function.array-pop" >array_pop</A > -- Liefert das letzte Element eines Arrays</DT ><DT ><A HREF="#function.array-push" >array_push</A > -- Fⁿgt ein oder mehr Elemente an das Ende eines Arrays </DT ><DT ><A HREF="#function.array-rand" >array_rand</A > -- Liefert einen oder mehrere zufΣllige EintrΣge eines Arrays </DT ><DT ><A HREF="#function.array-reduce" >array_reduce</A > -- Iterative Reduktion eines Arrays zu einem Wert mittels einer Callback Funktion </DT ><DT ><A HREF="#function.array-reverse" >array_reverse</A > -- Liefert ein Array mit umgekehrter Reihenfolge der Elemente </DT ><DT ><A HREF="#function.array-search" >array_search</A > -- Durchsucht ein Array nach einem Wert liefert bei Erfolg den Schlⁿssel </DT ><DT ><A HREF="#function.array-shift" >array_shift</A > -- Liefert ein Element vom Beginn eines Arrays </DT ><DT ><A HREF="#function.array-slice" >array_slice</A > -- Extrahiert einen Ausschnitt eines Arrays</DT ><DT ><A HREF="#function.array-splice" >array_splice</A > -- Entfernt einen Teil eines Arrays und ersetzt ihn durch etwas anderes </DT ><DT ><A HREF="#function.array-sum" >array_sum</A > -- Liefert die Summe der Werte in einem Array </DT ><DT ><A HREF="#function.array-udiff-assoc" >array_udiff_assoc</A > -- Computes the difference of arrays with additional index check. The data is compared by using a callback function</DT ><DT ><A HREF="#function.array-udiff-uassoc" >array_udiff_uassoc</A > -- Computes the difference of arrays with additional index check. The data is compared by using a callback function. The index check is done by a callback function also</DT ><DT ><A HREF="#function.array-udiff" >array_udiff</A > -- Computes the difference of arrays by using a callback function for data comparison</DT ><DT ><A HREF="#function.array-uintersect-assoc" >array_uintersect_assoc</A > -- Computes the intersection of arrays with additional index check. The data is compared by using a callback function</DT ><DT ><A HREF="#function.array-uintersect-uassoc" >array_uintersect_uassoc</A > -- Computes the intersection of arrays with additional index check. Both the data and the indexes are compared by using a callback functions</DT ><DT ><A HREF="#function.array-uintersect" >array_uintersect</A > -- Computes the intersection of arrays. The data is compared by using a callback function</DT ><DT ><A HREF="#function.array-unique" >array_unique</A > -- Entfernt doppelte Werte aus einem Array</DT ><DT ><A HREF="#function.array-unshift" >array_unshift</A > -- Fⁿgt ein oder mehr Elemente am Anfang eines Arrays ein </DT ><DT ><A HREF="#function.array-values" >array_values</A > -- Liefert alle Werte eines Arrays</DT ><DT ><A HREF="#function.array-walk-recursive" >array_walk_recursive</A > -- Apply a user function recursively to every member of an array </DT ><DT ><A HREF="#function.array-walk" >array_walk</A > -- Wendet eine Benutzerfunktion an jedem Element eines Arrays an </DT ><DT ><A HREF="#function.array" >array</A > -- Erstellt ein Array </DT ><DT ><A HREF="#function.arsort" >arsort</A > -- Sortiert ein Array in umgekehrter Reihenfolge und behΣlt die Verbindung zum Index </DT ><DT ><A HREF="#function.asort" >asort</A > -- Sortiert ein Array und behΣlt die Indexverbindungen </DT ><DT ><A HREF="#function.compact" >compact</A > -- Erstellt ein Array mit Variablen und deren Werten </DT ><DT ><A HREF="#function.count" >count</A > -- ZΣhlt die Elemente einer Variable</DT ><DT ><A HREF="#function.current" >current</A > -- Liefert das aktuelle Element eines Arrays</DT ><DT ><A HREF="#function.each" >each</A > -- Liefert das aktuelle Paar (Schlⁿssel und Wert) eines Arrays und rⁿckt den Array Cursor vor. </DT ><DT ><A HREF="#function.end" >end</A > -- Positioniert den internen Zeiger eines Arrays auf dessen letztes Element </DT ><DT ><A HREF="#function.extract" >extract</A > -- Importiert Variablen eines Arrays in die aktuelle Symboltabelle </DT ><DT ><A HREF="#function.in-array" >in_array</A > -- Prⁿft, ob ein Wert in einem Array existiert</DT ><DT ><A HREF="#function.key" >key</A > -- Liefert einen Schlⁿssel eines assoziativen Arrays </DT ><DT ><A HREF="#function.krsort" >krsort</A > -- Sortiert ein Array nach Schlⁿsseln in umgekehrter Reihenfolge </DT ><DT ><A HREF="#function.ksort" >ksort</A > -- Sortiert ein Array nach Schlⁿsseln</DT ><DT ><A HREF="#function.list" >list</A > -- Weist Variablen zu, als wΣren sie ein Array </DT ><DT ><A HREF="#function.natcasesort" >natcasesort</A > -- Sortiert ein Array in "natⁿrlicher Reihenfolge", Gro▀/Kleinschreibung wird ignoriert </DT ><DT ><A HREF="#function.natsort" >natsort</A > -- Sortiert ein Array in "natⁿrlicher Reihenfolge" </DT ><DT ><A HREF="#function.next" >next</A > -- Rⁿckt den internen Zeiger eines Arrays vor </DT ><DT ><A HREF="#function.pos" >pos</A > -- Liefert das aktuelle Element eines Arrays</DT ><DT ><A HREF="#function.prev" >prev</A > -- Verkleinert den internen Zeiger eines Arrays </DT ><DT ><A HREF="#function.range" >range</A > -- Erstellt ein Array mit einem Bereich von Elementen </DT ><DT ><A HREF="#function.reset" >reset</A > -- Setzt den internen Zeiger eines Arrays auf sein erstes Element </DT ><DT ><A HREF="#function.rsort" >rsort</A > -- Sortiert ein Array in umgekehrter Reihenfolge </DT ><DT ><A HREF="#function.shuffle" >shuffle</A > -- Mischt die Elemente eines Arrays</DT ><DT ><A HREF="#function.sizeof" >sizeof</A > -- Alias fⁿr <A HREF="#function.count" ><B CLASS="function" >count()</B ></A ></DT ><DT ><A HREF="#function.sort" >sort</A > -- Sortiert ein Array</DT ><DT ><A HREF="#function.uasort" >uasort</A > -- Sortiert ein Array mittels einer benutzerdefinierten Vergleichsfunktion und behΣlt Indexassoziationen bei. </DT ><DT ><A HREF="#function.uksort" >uksort</A > -- Sortiert ein Array nach Schlⁿsseln mittels einer benutzerdefinierten Vergleichsfunktion. </DT ><DT ><A HREF="#function.usort" >usort</A > -- Sortiert ein Array nach Werten mittels einer benutzerdefinierten Vergleichsfunktion. </DT ></DL ></DIV ></DIV ><H1 ><A NAME="function.array-change-key-case" ></A >array_change_key_case</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7339" ></A ><P > (PHP 4 >= 4.2.0, PHP 5)</P >array_change_key_case -- Liefert ein Array mit allen String-Schlⁿsseln in Klein- oder Gro▀buchstaben </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7342" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_change_key_case</B > ( array input [, int case])<BR ></BR ><P >
<B CLASS="function" >array_change_key_case()</B > wandelt die Schlⁿssel des Arrays <VAR CLASS="parameter" >input</VAR > in Klein- bzw. Gro▀buchstaben um. Die Richtung der Umwandlung wird mit dem optionalen Parameter <VAR CLASS="parameter" >case</VAR > festgelegt, mit dem die zwei Konstanten <TT CLASS="constant" ><B >CASE_UPPER</B ></TT > und <TT CLASS="constant" ><B >CASE_LOWER</B ></TT > ⁿbergeben werden k÷nnen. Default ist <TT CLASS="constant" ><B >CASE_LOWER</B ></TT >. Diese Funktion lΣsst die numerischen Indizes unverΣndert. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7360" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_change_key_case()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $input_array = array("FirSt" => 1, "SecOnd" => 4); print_r(array_change_key_case($input_array, CASE_UPPER)); ?></PRE ></TD ></TR ></TABLE ><P >
wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [FIRST] => 1 [SECOND] => 4 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Hat ein Array Indices, die bei einem Durchlauf durch diese Funktion gleich sind (z.B. "keY" and "kEY"), hebt der letztere Wert im Array andere Indices auf. </P ></DIV ><H1 ><A NAME="function.array-chunk" ></A >array_chunk</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7368" ></A ><P > (PHP 4 >= 4.2.0, PHP 5)</P >array_chunk -- Splittet ein Array in Teile auf</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7371" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_chunk</B > ( array input, int size [, bool preserve_keys])<BR ></BR ><P >
<B CLASS="function" >array_chunk()</B > teilt das Array in verschiedene Arrays mit <VAR CLASS="parameter" >size</VAR > Werten auf. Am Ende kann auch ein Array mit weniger Werten erzeugt werden. Die Arrays werden als Teile eines mehrdimensionalen Arrays erzeugt, welches bei Null und beginnend numerisch indiziert ist. </P ><P >
Sie k÷nnen PHP dazu zwingen, die originalen Schlⁿssel des Arrays input beizubehalten, indem Sie den optionalen Parameter <VAR CLASS="parameter" >preserve_keys</VAR > auf <TT CLASS="constant" ><B >TRUE</B ></TT > setzen. Geben Sie <TT CLASS="constant" ><B >FALSE</B ></TT > an, werden in jedem erzeugten Array neue numerische Indizes erzeugt, welche bei Null beginnen. Default ist <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7393" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_chunk()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $input_array = array('a', 'b', 'c', 'd', 'e'); print_r(array_chunk($input_array, 2)); print_r(array_chunk($input_array, 2, TRUE)); ?></PRE ></TD ></TR ></TABLE ><P >
wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [3] => d ) [2] => Array ( [4] => e ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><H1 ><A NAME="function.array-combine" ></A >array_combine</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7400" ></A ><P > (PHP 5)</P >array_combine -- Creates an array by using one array for keys and another for its values </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7403" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_combine</B > ( array keys, array values)<BR ></BR ><P >
Returns an <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A > by using the values from the <VAR CLASS="parameter" >keys</VAR > array as keys and the values from the <VAR CLASS="parameter" >values</VAR > array as the corresponding values. </P ><P >
Returns <TT CLASS="constant" ><B >FALSE</B ></TT > if the number of elements for each array isn't equal or if the arrays are empty. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7421" ></A ><P ><B >Beispiel 1. A simple <B CLASS="function" >array_combine()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array('green', 'red', 'yellow'); $b = array('avocado', 'apple', 'banana'); $c = array_combine($a, $b); print_r($c); ?></PRE ></TD ></TR ></TABLE ><P >
Outputs: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [green] => avocado [red] => apple [yellow] => banana )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
See also <A HREF="#function.array-merge" ><B CLASS="function" >array_merge()</B ></A >, <A HREF="#function.array-walk" ><B CLASS="function" >array_walk()</B ></A >, and <A HREF="#function.array-values" ><B CLASS="function" >array_values()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-count-values" ></A >array_count_values</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7432" ></A ><P > (PHP 4 , PHP 5)</P >array_count_values -- ZΣhlt die Werte eines Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7435" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_count_values</B > ( array input)<BR ></BR ><P >
<B CLASS="function" >array_count_values()</B > gibt ein Array zurⁿck, in dem die Werte des Arrays <VAR CLASS="parameter" >input</VAR > als Schlⁿssel, und deren Anzahl als Werte angegeben sind. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7447" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_count_values()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array = array (1, "hello", 1, "world", "hello"); print_r(array_count_values ($array)); ?></PRE ></TD ></TR ></TABLE ><P >
Wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [1] => 2 [hello] => 2 [world] => 1 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.array-diff-assoc" ></A >array_diff_assoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7454" ></A ><P > (PHP 4 >= 4.3.0, PHP 5)</P >array_diff_assoc -- Computes the difference of arrays with additional index check</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7457" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_diff_assoc</B > ( array array1, array array2 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_diff_assoc()</B > returns an <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A > containing all the values from <VAR CLASS="parameter" >array1</VAR > that are not present in any of the other arguments. Note that the keys are used in the comparison unlike <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7477" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_diff_assoc()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_diff_assoc($array1, $array2); print_r($result); ?></PRE ></TD ></TR ></TABLE ><P >
The result is: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [b] => brown [c] => blue [0] => red )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In our example above you see the <VAR CLASS="literal" >"a" => "green"</VAR > pair is present in both arrays and thus it is not in the ouput from the function. Unlike this, the pair <VAR CLASS="literal" >0 => "red"</VAR > is in the ouput because in the second argument <VAR CLASS="literal" >"red"</VAR > has key which is <VAR CLASS="literal" >1</VAR >. </P ><P >
Two values from <SPAN CLASS="emphasis" ><I CLASS="emphasis" >key => value</I ></SPAN > pairs are considered equal only if <VAR CLASS="literal" >(string) $elem1 === (string) $elem2 </VAR >. In other words a strict check takes place so the string representations must be the same. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, <VAR CLASS="literal" >array_diff_assoc($array1[0], $array2[0]);</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, and <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-diff-key" ></A >array_diff_key</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7499" ></A ><P > (no version information, might be only in CVS)</P >array_diff_key -- Computes the difference of arrays using keys for comparison</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7502" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_diff_key</B > ( array array1, array array2 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_diff_key()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > that have keys that are not present in any of the other arguments. Note that the associativity is preserved. This function is like <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A > except the comparison is done on the keys instead of the values. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7521" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_diff_key()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4); $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8); var_dump(array_diff_key($array1, $array2)); ?></PRE ></TD ></TR ></TABLE ><P >
The printout of the above program will be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >array(2) { ["red"]=> int(2) ["purple"]=> int(4) }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
The two keys from the <VAR CLASS="literal" >key => value</VAR > pairs are considered equal only if <VAR CLASS="literal" >(string) $key1 === (string) $key2 </VAR >. In other words a strict type check is executed so the string representation must be the same. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using <VAR CLASS="literal" >array_diff_key($array1[0], $array2[0]);</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A > <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-diff-uassoc" ><B CLASS="function" >array_diff_uassoc()</B ></A >, <A HREF="#function.array-udiff-assoc" ><B CLASS="function" >array_udiff_assoc()</B ></A >, <A HREF="#function.array-udiff-uassoc" ><B CLASS="function" >array_udiff_uassoc()</B ></A >, <A HREF="#function.array-diff-ukey" ><B CLASS="function" >array_diff_ukey()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-intersect-uassoc" ><B CLASS="function" >array_intersect_uassoc()</B ></A >, <A HREF="#function.array-intersect-key" ><B CLASS="function" >array_intersect_key()</B ></A > and <A HREF="#function.array-intersect-ukey" ><B CLASS="function" >array_intersect_ukey()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-diff-uassoc" ></A >array_diff_uassoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7547" ></A ><P > (PHP 5)</P >array_diff_uassoc -- Computes the difference of arrays with additional index check which is performed by a user supplied callback function </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7550" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_diff_uassoc</B > ( array array1, array array2 [, array ..., callback key_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_diff_uassoc()</B > returns an <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A > containing all the values from <VAR CLASS="parameter" >array1</VAR > that are not present in any of the other arguments. Note that the keys are used in the comparison unlike <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >. This comparison is done by a user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. This is unlike <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A > where an internal function for comparing the indices is used. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7574" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_diff_uassoc()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function key_compare_func($a, $b) { if ($a === $b) { return 0; } return ($a > $b)? 1:-1; } $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_diff_uassoc($array1, $array2, "key_compare_func"); ?></PRE ></TD ></TR ></TABLE ><P >
The result is: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [b] => brown [c] => blue [0] => red )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In our example above you see the <VAR CLASS="literal" >"a" => "green"</VAR > pair is present in both arrays and thus it is not in the ouput from the function. Unlike this, the pair <VAR CLASS="literal" >0 => "red"</VAR > is in the ouput because in the second argument <VAR CLASS="literal" >"red"</VAR > has key which is <VAR CLASS="literal" >1</VAR >. </P ><P >
The equality of 2 indices is checked by the user supplied callback function. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, <VAR CLASS="literal" >array_diff_uassoc($array1[0], $array2[0], "key_compare_func");</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A >, <A HREF="#function.array-udiff-assoc" ><B CLASS="function" >array_udiff_assoc()</B ></A >, <A HREF="#function.array-udiff-uassoc" ><B CLASS="function" >array_udiff_uassoc()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-uintersect" ><B CLASS="function" >array_uintersect()</B ></A >, <A HREF="#function.array-uintersect-assoc" ><B CLASS="function" >array_uintersect_assoc()</B ></A > and <A HREF="#function.array-uintersect-uassoc" ><B CLASS="function" >array_uintersect_uassoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-diff-ukey" ></A >array_diff_ukey</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7601" ></A ><P > (no version information, might be only in CVS)</P >array_diff_ukey -- Computes the difference of arrays using a callback function on the keys for comparison</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7604" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_diff_ukey</B > ( array array1, array array2 [, array ..., callback key_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_diff_ukey()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > that have keys that are not present in any of the other arguments. Note that the associativity is preserved. This function is like <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A > except the comparison is done on the keys instead of the values. </P ><P >
This comparison is done by a user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first key is considered to be respectively less than, equal to, or greater than the second. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7627" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_diff_ukey()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function key_compare_func($key1, $key2) { if ($key1 == $key2) return 0; else if ($key1 > $key2) return 1; else return -1; } $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4); $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8); var_dump(array_diff_ukey($array1, $array2, 'key_compare_func')); ?></PRE ></TD ></TR ></TABLE ><P >
The printout of the above program will be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >array(2) { ["red"]=> int(2) ["purple"]=> int(4) }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
The two keys from the <VAR CLASS="literal" >key => value</VAR > pairs are considered equal only if <VAR CLASS="literal" >(string) $key1 === (string) $key2 </VAR >. In other words a strict type check is executed so the string representation must be the same. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using <VAR CLASS="literal" >array_diff_ukey($array1[0], $array2[0], 'callback_func');</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A > <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-diff-uassoc" ><B CLASS="function" >array_diff_uassoc()</B ></A >, <A HREF="#function.array-udiff-assoc" ><B CLASS="function" >array_udiff_assoc()</B ></A >, <A HREF="#function.array-udiff-uassoc" ><B CLASS="function" >array_udiff_uassoc()</B ></A >, <A HREF="#function.array-diff-key" ><B CLASS="function" >array_diff_key()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-intersect-uassoc" ><B CLASS="function" >array_intersect_uassoc()</B ></A >, <A HREF="#function.array-intersect-key" ><B CLASS="function" >array_intersect_key()</B ></A > and <A HREF="#function.array-intersect-ukey" ><B CLASS="function" >array_intersect_ukey()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-diff" ></A >array_diff</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7653" ></A ><P > (PHP 4 >= 4.0.1, PHP 5)</P >array_diff -- Ermittelt die Unterschiede von Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7656" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_diff</B > ( array array1, array array2 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_diff()</B > gibt ein Array mit allen Werten von <VAR CLASS="parameter" >array1</VAR >, welche in keinem der anderen Argumente enthalten sind, zurⁿck. Beachten Sie, dass Schlⁿssel erhalten bleiben. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7674" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_diff()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$array1 = array ("a" => "grⁿn", "rot", "blau", "rot"); $array2 = array ("b" => "grⁿn", "gelb", "rot"); $result = array_diff ($array1, $array2);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Dies speichert <VAR CLASS="literal" >array("blau");</VAR > in <VAR CLASS="varname" >$result</VAR >. Mehrfache Vorkommen in $array1 werden auf die gleiche Weise behandelt. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Zwei Elemente werden nur dann als gleich angesehen, wenn <VAR CLASS="literal" >(string) $elem1 === (string) $elem2</VAR >. In Worten: Wenn die String-ReprΣsentation die gleiche ist. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Beachten Sie, dass diese Funktion nur eine Dimension eines n-dimensionalen Arrays prⁿft. Natⁿrlich k÷nnen Sie tiefere Dimensionen prⁿfen, indem Sie z.B. <VAR CLASS="literal" >array_diff($array1[0], $array2[0]);</VAR > benutzen. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Dies war in PHP 4.0.4 fehlerhaft! </P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A > und <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-fill" ></A >array_fill</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7694" ></A ><P > (PHP 4 >= 4.2.0, PHP 5)</P >array_fill -- Fⁿllt ein Array mit Werten</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7697" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_fill</B > ( int start_index, int num, mixed value)<BR ></BR ><P >
<B CLASS="function" >array_fill()</B > fⁿllt ein Array mit <VAR CLASS="parameter" >num</VAR > EintrΣgen des Wertes des <VAR CLASS="parameter" >value</VAR > Parameters. Die Indizes des Arrays beginnen mit dem <VAR CLASS="parameter" >start_index</VAR > Parameter. Beachten Sie, dass <VAR CLASS="parameter" >num</VAR > eine Zahl gr÷▀er als Null sein muss, ansonsten gibt PHP eine Warnung aus. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7718" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_fill()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array_fill(5, 6, 'Banane'); print_r($a); ?></PRE ></TD ></TR ></TABLE ><P >
Nun ist <VAR CLASS="varname" >$a</VAR >: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [5] => Banane [6] => Banane [7] => Banane [8] => Banane [9] => Banane [10] => Banane )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.array-filter" ></A >array_filter</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7726" ></A ><P > (PHP 4 >= 4.0.6, PHP 5)</P >array_filter -- Filtert Elemente eines Arrays mittels einer Callback-Funktion </DIV ><DIV CLASS="refsect1" ><A NAME="AEN7729" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_filter</B > ( array input [, callback function])<BR ></BR ><P >
<B CLASS="function" >array_filter()</B > ⁿbergibt nacheinander alle Werte in dem Array <VAR CLASS="parameter" >input</VAR > and die <VAR CLASS="parameter" >callback</VAR > Funktion. Gibt die <VAR CLASS="parameter" >callback</VAR > Funktion true zurⁿck, wird der aktuelle Wert von <VAR CLASS="parameter" >input</VAR > in das Ergebnis-Array geschrieben. Die Schlⁿssel des Arrays bleiben erhalten. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7747" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_filter()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function ungerade($var) { return ($var % 2 == 1); } function gerade($var) { return ($var % 2 == 0); } $array1 = array ("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array (6, 7, 8, 9, 10, 11, 12); echo "Ungerade :\n"; print_r(array_filter($array1, "ungerade")); echo "Gerade:\n"; print_r(array_filter($array2, "gerade")); ?></PRE ></TD ></TR ></TABLE ><P >
Wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Ungerade : Array ( [a] => 1 [c] => 3 [e] => 5 ) Gerade: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Von der Benutzerfunktion aus darf das Array selbst nicht geΣndert werden, wie zum Beispiel durch Hinzufⁿgen oder L÷schen eines Elementes, oder auch L÷schen des Arrays, auf das <B CLASS="function" >array_filter()</B > angewendet wird. Ist das Array geΣndert, so ist das Verhalten dieser Funktion undefiniert. </P ><P >
Siehe auch <A HREF="#function.array-map" ><B CLASS="function" >array_map()</B ></A >, <A HREF="#function.array-reduce" ><B CLASS="function" >array_reduce()</B ></A > und <A HREF="#function.array-walk" ><B CLASS="function" >array_walk()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-flip" ></A >array_flip</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7760" ></A ><P > (PHP 4 , PHP 5)</P >array_flip -- Vertauscht Werte und Schlⁿssel in einem Array</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7763" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_flip</B > ( array trans)<BR ></BR ><P >
<B CLASS="function" >array_flip()</B > gibt ein Array in umgekehrter Reihenfolge zurⁿck, d.h. Schlⁿssel von <VAR CLASS="parameter" >trans</VAR > werden Werte, und die Werte von <VAR CLASS="parameter" >trans</VAR > werden zu Schlⁿsseln. </P ><P >
Beachten Sie, dass die Werte von <VAR CLASS="parameter" >trans</VAR > gⁿltige Schlⁿssel sein mⁿssen, z.B. haben sie entweder <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A > oder <A HREF="#language.types.string" ><B CLASS="type" >string</B ></A > zu sein. Ist ein Wert vom falschen Typ wird eine Warnung ausgegeben, und das Schlⁿssel/Werte-Paar <SPAN CLASS="emphasis" ><I CLASS="emphasis" >wird nicht vertauscht</I ></SPAN >. </P ><P >
Kommt ein Wert mehrmals vor, wird der letzte Schlⁿssel mit seinem Wert vertauscht, und alle anderen Schlⁿssel- Wertepaare gehen verloren. </P ><P >
<B CLASS="function" >array_flip()</B > gibt im Fehlerfall <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿck. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7786" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_flip()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $trans = array_flip ($trans); $original = strtr ($str, $trans); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7791" ></A ><P ><B >Beispiel 2. <B CLASS="function" >array_flip()</B >: Kollision</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $trans = array ("a" => 1, "b" => 1, "c" => 2); $trans = array_flip ($trans); print_r($trans); ?></PRE ></TD ></TR ></TABLE ><P >
Nun ist <VAR CLASS="varname" >$trans</VAR >: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [1] => b [2] => c )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.array-intersect-assoc" ></A >array_intersect_assoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7799" ></A ><P > (PHP 4 >= 4.3.0, PHP 5)</P >array_intersect_assoc -- Computes the intersection of arrays with additional index check</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7802" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_intersect_assoc</B > ( array array1, array array2 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_intersect_assoc()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > that are present in all the arguments. Note that the keys are used in the comparison unlike in <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7821" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_intersect_assoc()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result_array = array_intersect_assoc($array1, $array2); ?></PRE ></TD ></TR ></TABLE ><P >
$result_array will look like: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [a] => green )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In our example you see that only the pair <VAR CLASS="literal" >"a" => "green"</VAR > is present in both arrays and thus is returned. The value <VAR CLASS="literal" >"red"</VAR > is not returned because in <VAR CLASS="varname" >$array1</VAR > its key is <VAR CLASS="literal" >0</VAR > while the key of "red" in <VAR CLASS="varname" >$array2</VAR > is <VAR CLASS="literal" >1</VAR >. </P ><P >
The two values from the <VAR CLASS="literal" >key => value</VAR > pairs are considered equal only if <VAR CLASS="literal" >(string) $elem1 === (string) $elem2 </VAR >. In otherwords a strict type check is executed so the string representation must be the same. </P ><P >
See also <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-uintersect-assoc" ><B CLASS="function" >array_uintersect_assoc()</B ></A >, <A HREF="#function.array-intersect-uassoc" ><B CLASS="function" >array_intersect_uassoc()</B ></A >, <A HREF="#function.array-uintersect-uassoc" ><B CLASS="function" >array_uintersect_uassoc()</B ></A >, <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A > and <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-intersect-key" ></A >array_intersect_key</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7845" ></A ><P > (no version information, might be only in CVS)</P >array_intersect_key -- Computes the intersection of arrays using keys for comparison</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7848" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_intersect_key</B > ( array array1, array array2 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_intersect_key()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > which have matching keys that are present in all the arguments. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7866" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_intersect_key()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4); $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8); var_dump(array_intersect_key($array1, $array2)); ?></PRE ></TD ></TR ></TABLE ><P >
The printout of the above program will be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >array(2) { ["blue"]=> int(1) ["green"]=> int(3) })</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In our example you see that only the keys <VAR CLASS="literal" >'blue'</VAR > and <VAR CLASS="literal" >'green'</VAR > are present in both arrays and thus returned. Also notice that the values for the keys <VAR CLASS="literal" >'blue'</VAR > and <VAR CLASS="literal" >'green'</VAR > differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of <VAR CLASS="parameter" >array1</VAR >. </P ><P >
The two keys from the <VAR CLASS="literal" >key => value</VAR > pairs are considered equal only if <VAR CLASS="literal" >(string) $key1 === (string) $key2 </VAR >. In other words a strict type check is executed so the string representation must be the same. </P ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A > <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-diff-uassoc" ><B CLASS="function" >array_diff_uassoc()</B ></A >, <A HREF="#function.array-udiff-assoc" ><B CLASS="function" >array_udiff_assoc()</B ></A >, <A HREF="#function.array-udiff-uassoc" ><B CLASS="function" >array_udiff_uassoc()</B ></A >, <A HREF="#function.array-diff-key" ><B CLASS="function" >array_diff_key()</B ></A >, <A HREF="#function.array-diff-ukey" ><B CLASS="function" >array_diff_ukey()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-intersect-uassoc" ><B CLASS="function" >array_intersect_uassoc()</B ></A > and <A HREF="#function.array-intersect-ukey" ><B CLASS="function" >array_intersect_ukey()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-intersect-uassoc" ></A >array_intersect_uassoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7895" ></A ><P > (PHP 5)</P >array_intersect_uassoc -- Computes the intersection of arrays with additional index check. The indexes are compared by using a callback function</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7898" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_intersect_uassoc</B > ( array array1, array array2 [, array ..., callback key_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_intersect_uassoc()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > that are present in all the arguments. Note that the keys are used in the comparison unlike in <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >. </P ><P >
The index comparison is done by a user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7921" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_intersect_uassoc()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red"); print_r(array_intersect_uassoc($array1, $array2, "strcasecmp")); ?></PRE ></TD ></TR ></TABLE ><P >
Result will look like: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [b] => brown )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
See also <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-uintersect-assoc" ><B CLASS="function" >array_uintersect_assoc()</B ></A >, <A HREF="#function.array-uintersect-uassoc" ><B CLASS="function" >array_uintersect_uassoc()</B ></A >, <A HREF="#function.array-intersect-key" ><B CLASS="function" >array_intersect_key()</B ></A > and <A HREF="#function.array-intersect-ukey" ><B CLASS="function" >array_intersect_ukey()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-intersect-ukey" ></A >array_intersect_ukey</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7935" ></A ><P > (no version information, might be only in CVS)</P >array_intersect_ukey -- Computes the intersection of arrays using a callback function on the keys for comparison</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7938" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_intersect_ukey</B > ( array array1, array array2 [, array ..., callback key_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_intersect_ukey()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > which have matching keys that are present in all the arguments. </P ><P >
This comparison is done by a user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first key is considered to be respectively less than, equal to, or greater than the second. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7960" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_intersect_ukey()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function key_compare_func($key1, $key2) { if ($key1 == $key2) return 0; else if ($key1 > $key2) return 1; else return -1; } $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4); $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8); var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func')); ?></PRE ></TD ></TR ></TABLE ><P >
The printout of the above program will be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >array(2) { ["blue"]=> int(1) ["green"]=> int(3) })</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In our example you see that only the keys <VAR CLASS="literal" >'blue'</VAR > and <VAR CLASS="literal" >'green'</VAR > are present in both arrays and thus returned. Also notice that the values for the keys <VAR CLASS="literal" >'blue'</VAR > and <VAR CLASS="literal" >'green'</VAR > differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of <VAR CLASS="parameter" >array1</VAR >. </P ><P >
The two keys from the <VAR CLASS="literal" >key => value</VAR > pairs are considered equal only if <VAR CLASS="literal" >(string) $key1 === (string) $key2 </VAR >. In other words a strict type check is executed so the string representation must be the same. </P ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A > <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-diff-uassoc" ><B CLASS="function" >array_diff_uassoc()</B ></A >, <A HREF="#function.array-udiff-assoc" ><B CLASS="function" >array_udiff_assoc()</B ></A >, <A HREF="#function.array-udiff-uassoc" ><B CLASS="function" >array_udiff_uassoc()</B ></A >, <A HREF="#function.array-diff-key" ><B CLASS="function" >array_diff_key()</B ></A >, <A HREF="#function.array-diff-ukey" ><B CLASS="function" >array_diff_ukey()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-intersect-uassoc" ><B CLASS="function" >array_intersect_uassoc()</B ></A > and <A HREF="#function.array-intersect-key" ><B CLASS="function" >array_intersect_key()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-intersect" ></A >array_intersect</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7989" ></A ><P > (PHP 4 >= 4.0.1, PHP 5)</P >array_intersect -- Ermittelt die Schnittmenge von Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7992" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_intersect</B > ( array array1, array array2 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_intersect()</B > gibt ein Array mit allen Werten von <VAR CLASS="parameter" >array1</VAR >, die auch in allen anderen Argumenten enthalten sind, zurⁿck. Beachten Sie, dass die Schlⁿssel erhalten bleiben. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8010" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_intersect()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array ("a" => "grⁿn", "rot", "blau"); $array2 = array ("b" => "grⁿn", "gelb", "rot"); $result = array_intersect ($array1, $array2); ?></PRE ></TD ></TR ></TABLE ><P >
Danach enthΣlt <VAR CLASS="varname" >$result</VAR > </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [a] => grⁿn [0] => rot )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Zwei Elemente werden nur dann als gleich angesehen, wenn <VAR CLASS="literal" >(string) $elem1 === (string) $elem2</VAR >. In Worten: Wenn die String-ReprΣsentation die gleiche ist. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Dies war in PHP 4.0.4 fehlerhaft! </P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A > und <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-key-exists" ></A >array_key_exists</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8027" ></A ><P > (PHP 4 >= 4.1.0, PHP 5)</P >array_key_exists -- Prⁿft, ob ein Schlⁿssel in einem Array existiert</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8030" ></A ><H2 >Beschreibung</H2 >bool <B CLASS="methodname" >array_key_exists</B > ( mixed key, array search)<BR ></BR ><P >
<B CLASS="function" >array_key_exists()</B > gibt <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, wenn <VAR CLASS="parameter" >key</VAR > in dem Array vorhanden ist. <VAR CLASS="parameter" >key</VAR > kann jeder fⁿr einen Array-Index m÷gliche Wert sein. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8047" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_key_exists()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $search_array = array("erstes" => 1, "zweites" => 4); if (array_key_exists("erstes", $search_array)) { echo "Das Element 'erstes' ist in dem Array vorhanden"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der Name dieser Funktion ist in PHP 4.0.6 <B CLASS="function" >key_exists()</B >. </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.isset" ><B CLASS="function" >isset()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-keys" ></A >array_keys</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8057" ></A ><P > (PHP 4 , PHP 5)</P >array_keys -- Liefert alle Schlⁿssel eines Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8060" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_keys</B > ( array input [, mixed search_value])<BR ></BR ><P >
<B CLASS="function" >array_keys()</B > gibt die Schlⁿssel (numerisch und String) des Arrays <VAR CLASS="parameter" >input</VAR > zurⁿck. </P ><P >
Ist der optionale Parameter <VAR CLASS="parameter" >search_value</VAR > angegeben, werden nur die Schlⁿssel fⁿr diesen Wert zurⁿckgegeben. Andernfalls werden all Schlⁿssel von <VAR CLASS="parameter" >input</VAR > zurⁿckgegeben. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8078" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_keys()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array = array (0 => 100, "Farbe" => "rot"); print_r(array_keys ($array)); $array = array ("blau", "rot", "grⁿn", "blau", "blau"); print_r(array_keys ($array, "blau")); $array = array ("Farbe" => array("blau", "rot", "grⁿn"), "Gr÷▀e" => array("klein", "mittel", "gro▀")); print_r(array_keys ($array)); ?></PRE ></TD ></TR ></TABLE ><P >
wⁿrde folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => 0 [1] => Farbe ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => Farbe [1] => Gr÷▀e )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.array-values" ><B CLASS="function" >array_values()</B ></A > und. <A HREF="#function.array-key-exists" ><B CLASS="function" >array_key_exists()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-map" ></A >array_map</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8088" ></A ><P > (PHP 4 >= 4.0.6, PHP 5)</P >array_map -- Wendet eine Callback-Funktion auf die Elemente von Arrays an </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8091" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_map</B > ( mixed callback, array arr1 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_map()</B > gibt ein Array mit allen Elementen von <VAR CLASS="parameter" >arr1</VAR > zurⁿck, nachdem die Callback-Funktion <VAR CLASS="parameter" >callback</VAR > auf jedes einzelne Element angewandt wurde. Die Anzahl Parameter, welche die Callback-Funktion <VAR CLASS="parameter" >callback</VAR > akzeptiert, sollte der Anzahl der an <B CLASS="function" >array_map()</B > ⁿbergebenen Arrays entsprechen. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8112" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_map()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function cube($n) { return $n*$n*$n; } $a = array(1, 2, 3, 4, 5); $b = array_map("cube", $a); print_r($b); ?></PRE ></TD ></TR ></TABLE ><P >
Dies speichert in <VAR CLASS="varname" >$b</VAR >: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8120" ></A ><P ><B >Beispiel 2. <B CLASS="function" >array_map()</B > - Verwendung mehrerer Arrays </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function show_Spanish($n, $m) { return "Die Zahl $n hei▀t auf Spanisch $m"; } function map_Spanish($n, $m) { return array ($n => $m); } $a = array(1, 2, 3, 4, 5); $b = array("uno", "dos", "tres", "cuatro", "cinco"); $c = array_map("show_Spanish", $a, $b); print_r($c); $d = array_map("map_Spanish", $a , $b); print_r($d); ?></PRE ></TD ></TR ></TABLE ><P >
Dies resultiert in: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >// Ausgabe von $c Array ( [0] => Die Zahl 1 hei▀t auf Spanisch uno [1] => Die Zahl 2 hei▀t auf Spanisch dos [2] => Die Zahl 3 hei▀t auf Spanisch tres [3] => Die Zahl 4 hei▀t auf Spanisch cuatro [4] => Die Zahl 5 hei▀t auf Spanisch cinco ) // Ausgabe von $d Array ( [0] => Array ( [1] => uno ) [1] => Array ( [2] => dos ) [2] => Array ( [3] => tres ) [3] => Array ( [4] => cuatro ) [4] => Array ( [5] => cinco ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Bei Verwendung von zwei oder mehr Arrays sollten diese gew÷hnlich die gleiche LΣnge besitzen, da die Callback-Funktion parallel auf die entsprechenden Elemente angewandt wird. Haben die Arrays unterschiedliche LΣngen, wird das kⁿrzeste um leere Elemente erweitert. </P ><P >
Eine interessante Anwendung dieser Funktion ist die Konstruktion eines Arrays bestehend aus Arrays, was mit <TT CLASS="constant" ><B >NULL</B ></TT > als Name der Callback-Funktion leicht realisiert werden kann. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8130" ></A ><P ><B >Beispiel 3. Erstellen eines Arrays mit Arrays</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array(1, 2, 3, 4, 5); $b = array("eins", "zwei", "drei", "vier", "fⁿnf"); $c = array("uno", "dos", "tres", "cuatro", "cinco"); $d = array_map(null, $a, $b, $c); print_r($d); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Wⁿrde folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => Array ( [0] => 1 [1] => eins [2] => uno ) [1] => Array ( [0] => 2 [1] => zwei [2] => dos ) [2] => Array ( [0] => 3 [1] => drei [2] => tres ) [3] => Array ( [0] => 4 [1] => vier [2] => cuatro ) [4] => Array ( [0] => 5 [1] => fⁿnf [2] => cinco ) )</PRE ></TD ></TR ></TABLE ><P >
Siehe auch <A HREF="#function.array-filter" ><B CLASS="function" >array_filter()</B ></A >, <A HREF="#function.array-reduce" ><B CLASS="function" >array_reduce()</B ></A > und <A HREF="#function.array-walk" ><B CLASS="function" >array_walk()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-merge-recursive" ></A >array_merge_recursive</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8140" ></A ><P > (PHP 4 >= 4.0.1, PHP 5)</P >array_merge_recursive -- Fⁿhrt zwei oder mehr Arrays rekursiv zusammen </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8143" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_merge_recursive</B > ( array array1, array array2 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_merge_recursive()</B > fⁿgt die Elemente von zwei oder mehr Arrays zusammen, so dass die Werte des einen Arrays an die des vorigen angehΣngt werden. Das daraus resultierende Array wird zurⁿckgegeben. </P ><P >
Haben die angegebenen Arrays die selben String-Schlⁿssel, dann wird der hintere Wert dieses Schlⁿssels den vorhergehenden ⁿberschreiben. Haben die Arrays den gleichen numerischen Schlⁿssel, dann wird der hintere Wert den vorhergehenden nicht ⁿberschreiben, sondern an den vorderen angehΣngt. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8161" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_merge_recursive()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $ar1 = array ("farbe" => array ("favorit" => "rot"), 5); $ar2 = array (10, "farbe" => array ("favorit" => "grⁿn", "blau")); $result = array_merge_recursive ($ar1, $ar2); ?></PRE ></TD ></TR ></TABLE ><P >
The <VAR CLASS="literal" >$result</VAR > will be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [farbe] => Array ( [favorit] => Array ( [0] => rot [1] => grⁿn ) [0] => blau ) [0] => 5 [1] => 10 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
See also <A HREF="#function.array-merge" ><B CLASS="function" >array_merge()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-merge" ></A >array_merge</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8171" ></A ><P > (PHP 4 , PHP 5)</P >array_merge -- Fⁿhrt zwei oder mehr Arrays zusammen</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8174" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_merge</B > ( array array1, array array2 [, array ...])<BR ></BR ><P >
<B CLASS="function" >array_merge()</B > fⁿgt die Elemente von zwei oder mehr Arrays zusammen, indem die Werte des einen an das Ende des anderen angehΣngt werden. Das daraus resultierende Array wird zurⁿckgegeben. </P ><P >
Haben die angegebenen Arrays die selben String-Schlⁿssel, dann wird der hintere Wert dieses Schlⁿssels den vorhergehenden ⁿberschreiben. Haben die Arrays den gleichen numerischen Schlⁿssel, dann wird der hintere Wert den vorhergehenden <SPAN CLASS="strong" ><B CLASS="emphasis" >nicht</B ></SPAN > ⁿberschreiben, sondern angehΣngt. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8193" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_merge()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array ("farbe" => "rot", 2, 4); $array2 = array ("a", "b", "farbe" => "grⁿn", "form" => "trapezoid", 4); $result = array_merge ($array1, $array2); print_r($result); ?></PRE ></TD ></TR ></TABLE ><P >
Nun ist <VAR CLASS="literal" >$result</VAR >: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [farbe] => grⁿn [0] => 2 [1] => 4 [2] => a [3] => b [form] => trapezoid [4] => 4 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8201" ></A ><P ><B >Beispiel 2. Einfaches <B CLASS="function" >array_merge()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array(); $array2 = array(1 => "data"); $result = array_merge($array1, $array2); ?></PRE ></TD ></TR ></TABLE ><P >
Vergessen Sie nicht, dass numerische Schlⁿssel neu numeriert werden! </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => data )</PRE ></TD ></TR ></TABLE ><P >
Wenn Sie die Arrays komplett erhalten und nur eines an das andere anhΣngen wollen, verwenden Sie den Operator <VAR CLASS="literal" >+</VAR >: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array(); $array2 = array(1 => "data"); $result = $array1 + $array2; ?></PRE ></TD ></TR ></TABLE ><P >
Der numerische Schlⁿssel sowie die Assoziation bleiben erhalten. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [1] => data )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Gemeinsame Schlⁿssel werden nach dem Prinzip "Wer zuerst kommt mahlt zuerst" behandelt. </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.array-merge-recursive" ><B CLASS="function" >array_merge_recursive()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-multisort" ></A >array_multisort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8217" ></A ><P > (PHP 4 , PHP 5)</P >array_multisort -- Sortiert mehrere oder multidimensionale Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8220" ></A ><H2 >Beschreibung</H2 >bool <B CLASS="methodname" >array_multisort</B > ( array ar1 [, mixed arg [, mixed ... [, array ...]]])<BR ></BR ><P >
<B CLASS="function" >array_multisort()</B > wird zum Sortieren von entweder mehreren Arrays auf einmal, oder eines multidimensionalen Arrays (entsprechend einer von mehreren Dimensionen) benutzt. Bei der Sortierung werden die Schlⁿsselassoziationen beibehalten. </P ><P >
Die angegebenen Arrays werden als Spalten einer Tabelle behandelt, welche nach Reihen sortiert werden - Σhnlich der SQL Klausel ORDER BY. Das erste Array ist auch das erste in der Sortierreihenfolge. Die in diesem Array gleichen Zeilen (Werte) werden anhand des nΣchsten angegebenen Arrays sortiert, usw. </P ><P >
Die Struktur der Argumente ist etwas ungew÷hnlich, aber flexibel. Das aller erste Argument muss ein Array sein. Die nachfolgenden Argumente k÷nnen entweder ein Array oder eines der folgenden Sortierflags sein. </P ><P >
Flags fⁿr Sortierreihenfolge: <P ></P ><UL ><LI ><P >SORT_ASC - sortiere in aufsteigender Reihenfolge</P ></LI ><LI ><P >SORT_DESC - sortiere in absteigender Reihenfolge</P ></LI ></UL > </P ><P >
Flags fⁿr Sortiertypen: <P ></P ><UL ><LI ><P >SORT_REGULAR - vergleiche Felder normal</P ></LI ><LI ><P >SORT_NUMERIC - vergleiche Felder numerisch</P ></LI ><LI ><P >SORT_STRING - vergleiche Felder als Strings</P ></LI ></UL > </P ><P >
Es kann nur ein Sortierflag des selben Typs nach jedem Array spezifiziert werden. Sortierflags nach einem Array Argument gelten nur fⁿr dieses Array, und werden vor jedem neuen Array Argument zu den Defaultflags SORT_ASC und SORT_REGULAR zurⁿckgesetzt. </P ><P >
Gibt bei Erfolg <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, im Fehlerfall <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8260" ></A ><P ><B >Beispiel 1. Sortieren mehrerer Arrays</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $ar1 = array ("10", 100, 100, "a"); $ar2 = array (1, 3, "2", 1); array_multisort ($ar1, $ar2); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In diesem Beispiel enthΣlt das erste Array nach dem Sortieren 10, "a", 100, 100. Das zweite Array wird 1, 1, "2", 3 enthalten. Die EintrΣge des zweiten Arrays, welche den identischen EintrΣgen des ersten Arrays entsprechen (100 und 100) wurden ebenfalls sortiert. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8265" ></A ><P ><B >Beispiel 2. Sortieren eines mehrdimensionalen Arrays</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $ar = array (array ("10", 100, 100, "a"), array (1, 3, "2", 1)); array_multisort ($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In diesem Beispiel enthΣlt das erste Array nach dem Sortieren 10, 100, 100, "a" (wurde als Strings in aufsteigender Reihenfolge sortiert), und das zweite enthΣlt 1, 3, "2", 1 (numerisch und absteigender Reihenfolge sortiert). </P ></DIV ><H1 ><A NAME="function.array-pad" ></A >array_pad</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8270" ></A ><P > (PHP 4 , PHP 5)</P >array_pad -- Vergr÷▀ert ein Array auf die spezifizierte LΣnge mit einem Wert </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8273" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_pad</B > ( array input, int pad_size, mixed pad_value)<BR ></BR ><P >
<B CLASS="function" >array_pad()</B > liefert eine Kopie von <VAR CLASS="parameter" >input</VAR >, welche auf die von <VAR CLASS="parameter" >pad_size</VAR > spezifizierte Gr÷▀e mit dem Wert <VAR CLASS="parameter" >pad_value</VAR > erweitert wurde. Ist der Parameter <VAR CLASS="parameter" >pad_size</VAR > positiv, wird das Array rechts erweitert, ist er negativ, dann erfolgt die Erweiterung links. Ist der absolute Wert von <VAR CLASS="parameter" >pad_size</VAR > kleiner oder gleich der LΣnge von <VAR CLASS="parameter" >input</VAR >, erfolgt keine Erweiterung. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8296" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_pad()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $input = array (12, 10, 9); $result = array_pad ($input, 5, 0); // Ergebnis ist array (12, 10, 9, 0, 0) $result = array_pad ($input, -7, -1); // Ergebnis ist array (-1, -1, -1, -1, 12, 10, 9) $result = array_pad ($input, 2, "noop"); // nicht erweitert ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.array-pop" ></A >array_pop</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8301" ></A ><P > (PHP 4 , PHP 5)</P >array_pop -- Liefert das letzte Element eines Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8304" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >array_pop</B > ( array array)<BR ></BR ><P >
<B CLASS="function" >array_pop()</B > liefert den letzten Wert von <VAR CLASS="parameter" >array</VAR >, und verkⁿrzt <VAR CLASS="parameter" >array</VAR > um ein Element. Ist <VAR CLASS="parameter" >array</VAR > leer (oder kein Array), wird <TT CLASS="constant" ><B >NULL</B ></TT > zurⁿckgegeben. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Diese Funktion setzt (<A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >) nach Verwendung den <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A >-Zeiger zurⁿck.</P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8323" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_pop()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $stack = array ("Orange", "Banane", "Apfel", "Himbeere"); $fruit = array_pop ($stack); print_r($stack); ?></PRE ></TD ></TR ></TABLE ><P >
Danach hat <VAR CLASS="varname" >$stack</VAR > nur 3 Elemente: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => Orange [1] => Banane [2] => Apfel )</PRE ></TD ></TR ></TABLE ><P >
und <VAR CLASS="literal" >Himbeere</VAR > wird <VAR CLASS="varname" >$fruit</VAR > zugewiesen. </P ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Diese Funktion kann sowohl das Boolsche <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckliefern, als auch einen nicht-Boolschen Wert, wie zum Beispiel<VAR CLASS="literal" >0</VAR > oder "". der von einem einfachen if-Statement als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet wird. Weitere Informationen entnehmen Sie bitte dem Abschnitt ⁿber die <A HREF="#language.types.boolean" >
Boolsche Typen</A >. Benutzen Sie deshalb <A HREF="#language.operators.comparison" >den === Operator</A > um den Rⁿckgabewert dieser Funktion zu ⁿberprⁿfen.</P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch <A HREF="#function.array-push" ><B CLASS="function" >array_push()</B ></A >, <A HREF="#function.array-shift" ><B CLASS="function" >array_shift()</B ></A > und <A HREF="#function.array-unshift" ><B CLASS="function" >array_unshift()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-push" ></A >array_push</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8345" ></A ><P > (PHP 4 , PHP 5)</P >array_push -- Fⁿgt ein oder mehr Elemente an das Ende eines Arrays </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8348" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >array_push</B > ( array array, mixed var [, mixed ...])<BR ></BR ><P >
<B CLASS="function" >array_push()</B > behandelt <VAR CLASS="parameter" >array</VAR > als Stapel (Stack), und fⁿgt die ⁿbergebenen Variablen an das Ende von <VAR CLASS="parameter" >array</VAR >. Die LΣnge von <VAR CLASS="parameter" >array</VAR > wird dabei um die Anzahl der angefⁿgten Variablen erh÷ht. Dies hat den selben Effekt wie: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array[] = $var; ?></PRE ></TD ></TR ></TABLE > fⁿr jede Variable <VAR CLASS="parameter" >var</VAR > wiederholt. </P ><P >
Liefert die neue Anzahl Elemente des Arrays. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8371" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_push()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $stack = array ("Orange", "Banane"); array_push ($stack, "Apfel", "Himbeere"); print_r($stack); ?></PRE ></TD ></TR ></TABLE ><P >
Danach enthΣlt <VAR CLASS="varname" >$stack</VAR > die folgenden Elemente: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => Orange [1] => Banane [2] => Apfel [3] => Himbeere )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.array-pop" ><B CLASS="function" >array_pop()</B ></A >, <A HREF="#function.array-shift" ><B CLASS="function" >array_shift()</B ></A > und <A HREF="#function.array-unshift" ><B CLASS="function" >array_unshift()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-rand" ></A >array_rand</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8383" ></A ><P > (PHP 4 , PHP 5)</P >array_rand -- Liefert einen oder mehrere zufΣllige EintrΣge eines Arrays </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8386" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >array_rand</B > ( array input [, int num_req])<BR ></BR ><P >
<B CLASS="function" >array_rand()</B > ist ziemlich nⁿtzlich, wenn Sie einen oder mehrere zufΣllige EintrΣge eines Arrays auswΣhlen m÷chten. Die Funktion ⁿbernimmt das Array <VAR CLASS="parameter" >input</VAR >, und ein optionales Argument <VAR CLASS="parameter" >num_req</VAR >, welches die gewⁿnschte Anzahl EintrΣge spezifiziert. Ist <VAR CLASS="parameter" >num_req</VAR > nicht angegeben, wird ein Defaultwert von 1 angenommen. </P ><P >
Wenn Sie nur einen Eintrag auswΣhlen, liefert <B CLASS="function" >array_rand()</B > den Schlⁿssel eines zufΣlligen Eintrages. Andernfalls wird ein Array mit den Schlⁿsseln der zufΣlligen EintrΣge zurⁿckgegeben. Dies hat den Zweck, dass Sie zufΣllige Schlⁿssel und auch Werte aus dem Array auswΣhlen k÷nnen. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Seit PHP 4.2.0 besteht keine Notwendigkeit mehr, den Zufallsgenerator fⁿr Zahlen mit <A HREF="#function.srand" ><B CLASS="function" >srand()</B ></A > oder <A HREF="#function.mt-srand" ><B CLASS="function" >mt_srand()</B ></A > zu fⁿttern, das geschieht nun automatisch.</P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8409" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_rand()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php srand ((float) microtime() * 10000000); $input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand ($input, 2); print $input[$rand_keys[0]]."\n"; print $input[$rand_keys[1]]."\n"; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.array-reduce" ></A >array_reduce</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8414" ></A ><P > (PHP 4 >= 4.0.5, PHP 5)</P >array_reduce -- Iterative Reduktion eines Arrays zu einem Wert mittels einer Callback Funktion </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8417" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >array_reduce</B > ( array input, callback function [, int initial])<BR ></BR ><P >
<B CLASS="function" >array_reduce()</B > wendet die Funktion <VAR CLASS="parameter" >function</VAR > iterativ bei den Elementen des Arrays <VAR CLASS="parameter" >input</VAR > so an, dass das Array auf einen einzigen Wert reduziert wird. Ist der optionale Parameter <VAR CLASS="parameter" >intial</VAR > angegeben, wird er am Beginn des Prozesses benutzt oder als Resultat verwendet, sollte das Array leer sein. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8437" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_reduce()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function rsum($v, $w) { $v += $w; return $v; } function rmul($v, $w) { $v *= $w; return $v; } $a = array(1, 2, 3, 4, 5); $x = array(); $b = array_reduce($a, "rsum"); $c = array_reduce($a, "rmul", 10); $d = array_reduce($x, "rsum", 1); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Hier enthΣlt <VAR CLASS="varname" >$b</VAR > <VAR CLASS="literal" >15</VAR >, <VAR CLASS="varname" >$c</VAR > <VAR CLASS="literal" >1200</VAR > (= 1*2*3*4*5*10), und <VAR CLASS="varname" >$d</VAR > enthΣlt <VAR CLASS="literal" >1</VAR >. </P ><P >
Siehe auch <A HREF="#function.array-filter" ><B CLASS="function" >array_filter()</B ></A > und <A HREF="#function.array-map" ><B CLASS="function" >array_map()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-reverse" ></A >array_reverse</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8452" ></A ><P > (PHP 4 , PHP 5)</P >array_reverse -- Liefert ein Array mit umgekehrter Reihenfolge der Elemente </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8455" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_reverse</B > ( array array [, bool preserve_keys])<BR ></BR ><P >
<B CLASS="function" >array_reverse()</B > liefert ein Array mit der umgekehrten Reihenfolge der Elemente von <VAR CLASS="parameter" >array</VAR >. Ist der optionale Parameter <VAR CLASS="parameter" >preserve_keys</VAR > <TT CLASS="constant" ><B >TRUE</B ></TT >, bleibt die Reihenfolge der Schlⁿssel aufrecht. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8472" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_reverse()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $input = array ("php", 4.0, array ("grⁿn", "rot")); $result = array_reverse ($input); $result_keyed = array_reverse ($input, TRUE); ?></PRE ></TD ></TR ></TABLE ><P >
Danach haben <VAR CLASS="varname" >$result</VAR > und <VAR CLASS="varname" >$result_keyed</VAR > die gleichen Elemente, aber Beachten Sie den Unterschied zwischen den Schlⁿsseln. Die Ausgabe von <VAR CLASS="varname" >$result</VAR > und <VAR CLASS="varname" >$result_keyed</VAR > wird wie folgt aussehen: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => Array ( [0] => grⁿn [1] => rot ) [1] => 4 [2] => php ) Array ( [2] => Array ( [0] => grⁿn [1] => rot ) [1] => 4 [0] => php )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der zweite Parameter wurde in PHP 4.0.3 hinzugefⁿgt. </P ></BLOCKQUOTE ></DIV ></DIV ><H1 ><A NAME="function.array-search" ></A >array_search</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8485" ></A ><P > (PHP 4 >= 4.0.5, PHP 5)</P >array_search -- Durchsucht ein Array nach einem Wert liefert bei Erfolg den Schlⁿssel </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8488" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >array_search</B > ( mixed needle, array haystack [, bool strict])<BR ></BR ><P >
Diese Funktion durchsucht <VAR CLASS="parameter" >haystack</VAR > nach <VAR CLASS="parameter" >needle</VAR > und gibt bei Erfolg den Schlⁿssel zurⁿck, andernfalls <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Vor PHP 4.2.0 gibt <B CLASS="function" >array_search()</B > im Fehlerfall <TT CLASS="constant" ><B >NULL</B ></TT > statt <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿck. </P ></BLOCKQUOTE ></DIV ><P >
Ist der optionale dritte Parameter <VAR CLASS="parameter" >strict</VAR > auf <TT CLASS="constant" ><B >TRUE</B ></TT > gesetzt, prⁿft <B CLASS="function" >array_search()</B > auch die Typen von <VAR CLASS="parameter" >needle</VAR > in <VAR CLASS="parameter" >haystack</VAR >. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Diese Funktion kann sowohl das Boolsche <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckliefern, als auch einen nicht-Boolschen Wert, wie zum Beispiel<VAR CLASS="literal" >0</VAR > oder "". der von einem einfachen if-Statement als <TT CLASS="constant" ><B >FALSE</B ></TT > ausgewertet wird. Weitere Informationen entnehmen Sie bitte dem Abschnitt ⁿber die <A HREF="#language.types.boolean" >
Boolsche Typen</A >. Benutzen Sie deshalb <A HREF="#language.operators.comparison" >den === Operator</A > um den Rⁿckgabewert dieser Funktion zu ⁿberprⁿfen.</P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch <A HREF="#function.array-keys" ><B CLASS="function" >array_keys()</B ></A > und <A HREF="#function.in-array" ><B CLASS="function" >in_array()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-shift" ></A >array_shift</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8528" ></A ><P > (PHP 4 , PHP 5)</P >array_shift -- Liefert ein Element vom Beginn eines Arrays </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8531" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >array_shift</B > ( array array)<BR ></BR ><P >
<B CLASS="function" >array_shift()</B > liefert den ersten Wert von <VAR CLASS="parameter" >array</VAR >, verschiebt die anderen Werte hinunter, und verkⁿrzt <VAR CLASS="parameter" >array</VAR > um ein Element. Alle numerischen Schlⁿssel werden so modifiziert, dass bei null zu zΣhlen begonnen wird. Strings als Schlⁿssel bleiben unverΣndert. Ist <VAR CLASS="parameter" >array</VAR > leer (oder kein Array), wird <TT CLASS="constant" ><B >NULL</B ></TT > zurⁿckgegeben. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Diese Funktion setzt (<A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >) nach Verwendung den <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A >-Zeiger zurⁿck.</P ></BLOCKQUOTE ></DIV ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8549" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_shift()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $stack = array ("Orange", "Banane", "Apfel", "Himbeere"); $fruit = array_shift ($stack); print_r($stack); ?></PRE ></TD ></TR ></TABLE ><P >
Danach blieben in <VAR CLASS="varname" >$stack</VAR > 3 Elemente ⁿbrig: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => Banane [1] => Apfel [2] => Himbeere )</PRE ></TD ></TR ></TABLE ><P >
und <VAR CLASS="literal" >orange</VAR > wird <VAR CLASS="varname" >$fruit</VAR > zugeordnet. </P ></DIV ></TD ></TR ></TABLE ><P >
Siehe auch <A HREF="#function.array-unshift" ><B CLASS="function" >array_unshift()</B ></A >, <A HREF="#function.array-push" ><B CLASS="function" >array_push()</B ></A > und <A HREF="#function.array-pop" ><B CLASS="function" >array_pop()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-slice" ></A >array_slice</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8564" ></A ><P > (PHP 4 , PHP 5)</P >array_slice -- Extrahiert einen Ausschnitt eines Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8567" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_slice</B > ( array array, int offset [, int length])<BR ></BR ><P >
<B CLASS="function" >array_slice()</B > liefert die mittels <VAR CLASS="parameter" >offset</VAR > und <VAR CLASS="parameter" >length</VAR > spezifizierte Sequenz von Elementen des Arrays <VAR CLASS="parameter" >array</VAR > </P ><P >
Ist <VAR CLASS="parameter" >offset</VAR > positiv, beginnt die Sequenz bei diesem Offset in dem <VAR CLASS="parameter" >array</VAR >. Ist <VAR CLASS="parameter" >offset</VAR > negativ, beginnt die Sequenz <VAR CLASS="parameter" >offset</VAR > Elemente vor dem Ende von <VAR CLASS="parameter" >array</VAR >. </P ><P >
Ist <VAR CLASS="parameter" >length</VAR > positiv, enthΣlt die Sequenz genauso viele Elemente. Ist <VAR CLASS="parameter" >length</VAR > negativ, enthΣlt die Sequenz alle Elemente des Arrays von <VAR CLASS="parameter" >offset</VAR > bis <VAR CLASS="parameter" >length</VAR > Elemente vor dem Ende des Arrays. Ist <VAR CLASS="parameter" >length</VAR > nicht angegeben, enthΣlt die Sequenz alle Elemente von <VAR CLASS="parameter" >offset</VAR > bis zum Ende von <VAR CLASS="parameter" >array</VAR >. </P ><P >
Beachten Sie, dass <B CLASS="function" >array_slice()</B > Schlⁿssel ignoriert, und Offsets sowie LΣngen anhand der aktuellen Positionen berechnet. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8603" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_slice()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $input = array ("a", "b", "c", "d", "e"); $output = array_slice ($input, 2); // liefert "c", "d", und "e" $output = array_slice ($input, 2, -1); // liefert "c", "d" $output = array_slice ($input, -2, 1); // liefert "d" $output = array_slice ($input, 0, 3); // liefert "a", "b", und "c" ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.array-splice" ><B CLASS="function" >array_splice()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-splice" ></A >array_splice</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8610" ></A ><P > (PHP 4 , PHP 5)</P >array_splice -- Entfernt einen Teil eines Arrays und ersetzt ihn durch etwas anderes </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8613" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_splice</B > ( array input, int offset [, int length [, array replacement]])<BR ></BR ><P >
<B CLASS="function" >array_splice()</B > entfernt die durch <VAR CLASS="parameter" >offset</VAR > und <VAR CLASS="parameter" >length</VAR > angegebenen Elemente des Arrays <VAR CLASS="parameter" >input</VAR >, und ersetzt diese durch die Elemente des Arrays <VAR CLASS="parameter" >replacement</VAR >, wenn angegeben und gibt ein Array mit den entfernten Elemente zurⁿck. </P ><P >
Ist <VAR CLASS="parameter" >offset</VAR > positiv, beginnt der zu entfernende Bereich bei diesem Offset vom Anfang des Arrays <VAR CLASS="parameter" >input</VAR >. Ist <VAR CLASS="parameter" >offset</VAR > negativ, beginnt der zu entfernende Bereich <VAR CLASS="parameter" >offset</VAR > Elemente vor dem Ende des Arrays <VAR CLASS="parameter" >input</VAR >. </P ><P >
Ist <VAR CLASS="parameter" >length</VAR > nicht angegeben, wird alles von <VAR CLASS="parameter" >offset</VAR > bis zum Ende des Arrays entfernt. Ist <VAR CLASS="parameter" >length</VAR > positiv, wird die angegebene Anzahl Elemente entfernt. Ist <VAR CLASS="parameter" >length</VAR > negativ, dann wird der Bereich von <VAR CLASS="parameter" >length</VAR > Elementen vor dem Ende, bis zum Ende des Arrays entfernt. Tipp: Um alles von <VAR CLASS="parameter" >offset</VAR > bis zum Ende des Arrays zu entfernen wenn <VAR CLASS="parameter" >replacement</VAR > ebenfalls angegeben ist, verwenden Sie <VAR CLASS="literal" >count($input)</VAR > als <VAR CLASS="parameter" >length</VAR >. </P ><P >
Ist das Array <VAR CLASS="parameter" >replacement</VAR > angegeben, werden die entfernten Elemente durch die Elemente dieses Arrays ersetzt. Sind <VAR CLASS="parameter" >offset</VAR > und <VAR CLASS="parameter" >length</VAR > so angegeben dass nichts entfernt wird, werden die Elemente von <VAR CLASS="parameter" >replacement</VAR > an der von <VAR CLASS="parameter" >offset</VAR > spezifizierten Stelle eingefⁿgt. Tipp: Soll die Ersetzung durch nur ein Element erfolgen ist es nicht n÷tig ein Array zu anzugeben es sei denn, dieses Element ist selbst ein Array. </P ><P >
Hier einige Entsprechungen: <DIV CLASS="table" ><A NAME="AEN8659" ></A ><P ><B >Tabelle 1. <B CLASS="function" >array_splice()</B > equivalents</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><TBODY ><TR ><TD >
array_push($input, $x, $y) </TD ><TD >
array_splice($input, count($input), 0, array($x, $y)) </TD ></TR ><TR ><TD >
array_pop($input) </TD ><TD >
array_splice($input, -1) </TD ></TR ><TR ><TD >
array_shift($input) </TD ><TD >
array_splice($input, -1) </TD ></TR ><TR ><TD >
array_unshift($input, $x, $y) </TD ><TD >
array_splice($input, 0, 0, array($x, $y)) </TD ></TR ><TR ><TD >
$a[$x] = $y </TD ><TD >
array_splice($input, $x, 1, $y) </TD ></TR ></TBODY ></TABLE ></DIV > </P ><P >
Gibt das Array mit den entfernten Elementen zurⁿck. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8681" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_splice()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $input = array ("rot", "grⁿn", "blau", "gelb"); array_splice ($input, 2); // $input ist nun array ("rot", "grⁿn") $input = array ("rot", "grⁿn", "blau", "gelb"); array_splice ($input, 1, -1); // $input ist nun array ("rot", "gelb") $input = array ("rot", "grⁿn", "blau", "gelb"); array_splice ($input, 1, count($input), "orange"); // $input ist nun array ("rot", "orange") $input = array ("rot", "grⁿn", "blau", "gelb"); array_splice ($input, -1, 1, array("schwarz", "braun")); // $input ist nun array ("rot", "grⁿn", "blau", "schwarz", "braun") ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.array-slice" ><B CLASS="function" >array_slice()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-sum" ></A >array_sum</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8688" ></A ><P > (PHP 4 >= 4.0.4, PHP 5)</P >array_sum -- Liefert die Summe der Werte in einem Array </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8691" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >array_sum</B > ( array array)<BR ></BR ><P >
<B CLASS="function" >array_sum()</B > liefert die Summe der Werte eines Arrays als Integer oder Float. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8702" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_sum()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array(2, 4, 6, 8); echo "sum(a) = ".array_sum($a)."\n"; $b = array("a"=>1.2,"b"=>2.3,"c"=>3.4); echo "sum(b) = ".array_sum($b)."\n"; ?></PRE ></TD ></TR ></TABLE ><P >
Obiges Programm wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >sum(a) = 20 sum(b) = 6.9</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > PHP Versionen vor 4.2.1 haben das ⁿbergebene Array selbst modifiziert, und Strings in Zahlen konvertiert (welche abhΣngig von deren Wert meist zu null konvertiert wurden). </P ></BLOCKQUOTE ></DIV ></DIV ><H1 ><A NAME="function.array-udiff-assoc" ></A >array_udiff_assoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8711" ></A ><P > (PHP 5)</P >array_udiff_assoc -- Computes the difference of arrays with additional index check. The data is compared by using a callback function</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8714" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_udiff_assoc</B > ( array array1, array array2 [, array ..., callback data_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_udiff_assoc()</B > returns an <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A > containing all the values from <VAR CLASS="parameter" >array1</VAR > that are not present in any of the other arguments. Note that the keys are used in the comparison unlike <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A > and <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A >. The comparison of arrays' data is performed by using an user-supplied callback. In this aspect the behaviour is opposite to the behaviour of <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A > which uses internal function for comparison. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8739" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_udiff_assoc()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class cr { private $priv_member; function cr($val) { $this->priv_member = $val; } function comp_func_cr($a, $b) { if ($a->priv_member === $b->priv_member) return 0; return ($a->priv_member > $b->priv_member)? 1:-1; } } $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),); $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),); $result = array_udiff_assoc($a, $b, array("cr", "comp_func_cr")); print_r($result); ?></PRE ></TD ></TR ></TABLE ><P >
The result is: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0.1] => cr Object ( [priv_member:private] => 9 ) [0.5] => cr Object ( [priv_member:private] => 12 ) [0] => cr Object ( [priv_member:private] => 23 ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In our example above you see the <VAR CLASS="literal" >"1" => new cr(4)</VAR > pair is present in both arrays and thus it is not in the ouput from the function. </P ><P >
For comparison is used the user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, <VAR CLASS="literal" >array_udiff_assoc($array1[0], $array2[0], "some_comparison_func");</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-diff-uassoc" ><B CLASS="function" >array_diff_uassoc()</B ></A >, <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A >, <A HREF="#function.array-udiff-uassoc" ><B CLASS="function" >array_udiff_uassoc()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-uintersect" ><B CLASS="function" >array_uintersect()</B ></A >, <A HREF="#function.array-uintersect-assoc" ><B CLASS="function" >array_uintersect_assoc()</B ></A > and <A HREF="#function.array-uintersect-uassoc" ><B CLASS="function" >array_uintersect_uassoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-udiff-uassoc" ></A >array_udiff_uassoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8763" ></A ><P > (PHP 5)</P >array_udiff_uassoc -- Computes the difference of arrays with additional index check. The data is compared by using a callback function. The index check is done by a callback function also</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8766" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_udiff_uassoc</B > ( array array1, array array2 [, array ..., callback data_compare_func, callback key_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_udiff_uassoc()</B > returns an <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A > containing all the values from <VAR CLASS="parameter" >array1</VAR > that are not present in any of the other arguments. Note that the keys are used in the comparison unlike <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A > and <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A >. The comparison of arrays' data is performed by using an user-supplied callback : <VAR CLASS="parameter" >data_compare_func</VAR >. In this aspect the behaviour is opposite to the behaviour of <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A > which uses internal function for comparison. The comparison of keys (indices) is done also by the callback function <VAR CLASS="parameter" >key_compare_func</VAR >. This behaviour is unlike what <A HREF="#function.array-udiff-assoc" ><B CLASS="function" >array_udiff_assoc()</B ></A > does, since the latter compares the indices by using an internal function. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8797" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_udiff_uassoc()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class cr { private $priv_member; function cr($val) { $this->priv_member = $val; } function comp_func_cr($a, $b) { if ($a->priv_member === $b->priv_member) return 0; return ($a->priv_member > $b->priv_member)? 1:-1; } function comp_func_key($a, $b) { if ($a === $b) return 0; return ($a > $b)? 1:-1; } } $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),); $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),); $result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key")); print_r($result); ?></PRE ></TD ></TR ></TABLE ><P >
The result is: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0.1] => cr Object ( [priv_member:private] => 9 ) [0.5] => cr Object ( [priv_member:private] => 12 ) [0] => cr Object ( [priv_member:private] => 23 ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
In our example above you see the <VAR CLASS="literal" >"1" => new cr(4)</VAR > pair is present in both arrays and thus it is not in the ouput from the function. Keep in mind that you have to supply 2 callback functions. </P ><P >
For comparison is used the user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, <VAR CLASS="literal" >array_udiff_uassoc($array1[0], $array2[0], "data_compare_func", "key_compare_func");</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-diff-uassoc" ><B CLASS="function" >array_diff_uassoc()</B ></A >, <A HREF="#function.array-udiff" ><B CLASS="function" >array_udiff()</B ></A >, <A HREF="#function.array-udiff-assoc" ><B CLASS="function" >array_udiff_assoc()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-uintersect" ><B CLASS="function" >array_uintersect()</B ></A >, <A HREF="#function.array-uintersect-assoc" ><B CLASS="function" >array_uintersect_assoc()</B ></A > and <A HREF="#function.array-uintersect-uassoc" ><B CLASS="function" >array_uintersect_uassoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-udiff" ></A >array_udiff</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8821" ></A ><P > (PHP 5)</P >array_udiff -- Computes the difference of arrays by using a callback function for data comparison</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8824" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_udiff</B > ( array array1, array array2 [, array ..., callback data_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_udiff()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > that are not present in any of the other arguments. Note that keys are preserved. For the comparison of the data <VAR CLASS="parameter" >data_compare_func</VAR > is used. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. This is unlike <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A > which uses an internal function for comparing the data. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8847" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_udiff()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class cr { private $priv_member; function cr($val) { $this->priv_member = $val; } function comp_func_cr($a, $b) { if ($a->priv_member === $b->priv_member) return 0; return ($a->priv_member > $b->priv_member)? 1:-1; } } $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),); $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),); $result = array_udiff($a, $b, array("cr", "comp_func_cr")); print_r($result); ?></PRE ></TD ></TR ></TABLE ><P >
The result is: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0.5] => cr Object ( [priv_member:private] => 12 ) [0] => cr Object ( [priv_member:private] => 23 ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Two elements are considered equal if and only if <VAR CLASS="literal" >(string) $elem1 === (string) $elem2</VAR >. In words: when the string representation is the same. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using <VAR CLASS="literal" >array_udiff($array1[0], $array2[0], "data_compare_func");</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
See also <A HREF="#function.array-diff" ><B CLASS="function" >array_diff()</B ></A >, <A HREF="#function.array-diff-assoc" ><B CLASS="function" >array_diff_assoc()</B ></A >, <A HREF="#function.array-diff-uassoc" ><B CLASS="function" >array_diff_uassoc()</B ></A >, <A HREF="#function.array-udiff-assoc" ><B CLASS="function" >array_udiff_assoc()</B ></A >, <A HREF="#function.array-udiff-uassoc" ><B CLASS="function" >array_udiff_uassoc()</B ></A >, <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-uintersect" ><B CLASS="function" >array_uintersect()</B ></A >, <A HREF="#function.array-uintersect-assoc" ><B CLASS="function" >array_uintersect_assoc()</B ></A > and <A HREF="#function.array-uintersect-uassoc" ><B CLASS="function" >array_uintersect_uassoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-uintersect-assoc" ></A >array_uintersect_assoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8871" ></A ><P > (PHP 5)</P >array_uintersect_assoc -- Computes the intersection of arrays with additional index check. The data is compared by using a callback function</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8874" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_uintersect_assoc</B > ( array array1, array array2 [, array ..., callback data_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_uintersect_assoc()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > that are present in all the arguments. Note that the keys are used in the comparison unlike in <A HREF="#function.array-uintersect" ><B CLASS="function" >array_uintersect()</B ></A >. The data is compared by using a callback function. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8896" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_uintersect_assoc()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red"); print_r(array_uintersect_assoc($array1, $array2, "strcasecmp")); ?></PRE ></TD ></TR ></TABLE ><P >
Result will look like: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [a] => green )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
For comparison is used the user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. </P ><P >
See also <A HREF="#function.array-uintersect" ><B CLASS="function" >array_uintersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-intersect-uassoc" ><B CLASS="function" >array_intersect_uassoc()</B ></A > and <A HREF="#function.array-uintersect-uassoc" ><B CLASS="function" >array_uintersect_uassoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-uintersect-uassoc" ></A >array_uintersect_uassoc</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8909" ></A ><P > (PHP 5)</P >array_uintersect_uassoc -- Computes the intersection of arrays with additional index check. Both the data and the indexes are compared by using a callback functions</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8912" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_uintersect_uassoc</B > ( array array1, array array2 [, array ..., callback data_compare_func, callback key_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_uintersect_uassoc()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > that are present in all the arguments. Note that the keys are used in the comparison unlike in <A HREF="#function.array-uintersect" ><B CLASS="function" >array_uintersect()</B ></A >. Both the data and the indexes are compared by using a callback functions. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8937" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_uintersect_uassoc()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red"); print_r(array_uintersect_uassoc($array1, $array2, "strcasecmp", "strcasecmp")); ?></PRE ></TD ></TR ></TABLE ><P >
Result will look like: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [a] => green [b] => brown )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
For comparison is used the user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. </P ><P >
See also <A HREF="#function.array-uintersect" ><B CLASS="function" >array_uintersect()</B ></A >, <A HREF="#function.array-intersect-assoc" ><B CLASS="function" >array_intersect_assoc()</B ></A >, <A HREF="#function.array-intersect-uassoc" ><B CLASS="function" >array_intersect_uassoc()</B ></A > and <A HREF="#function.array-uintersect-assoc" ><B CLASS="function" >array_uintersect_assoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-uintersect" ></A >array_uintersect</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8950" ></A ><P > (PHP 5)</P >array_uintersect -- Computes the intersection of arrays. The data is compared by using a callback function</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8953" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_uintersect</B > ( array array1, array array2 [, array ..., callback data_compare_func])<BR ></BR ><P >
<B CLASS="function" >array_uintersect()</B > returns an array containing all the values of <VAR CLASS="parameter" >array1</VAR > that are present in all the arguments. The data is compared by using a callback function. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8974" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_uintersect()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red"); print_r(array_uintersect($array1, $array2, "strcasecmp")); ?></PRE ></TD ></TR ></TABLE ><P >
Result will look like: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [a] => green [b] => brown [0] => red )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
For comparison is used the user supplied callback function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. </P ><P >
See also <A HREF="#function.array-intersect" ><B CLASS="function" >array_intersect()</B ></A >, <A HREF="#function.array-uintersect-assoc" ><B CLASS="function" >array_uintersect_assoc()</B ></A >, <A HREF="#function.array-intersect-uassoc" ><B CLASS="function" >array_intersect_uassoc()</B ></A > and <A HREF="#function.array-uintersect-uassoc" ><B CLASS="function" >array_uintersect_uassoc()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-unique" ></A >array_unique</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8987" ></A ><P > (PHP 4 >= 4.0.1, PHP 5)</P >array_unique -- Entfernt doppelte Werte aus einem Array</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8990" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_unique</B > ( array array)<BR ></BR ><P >
<B CLASS="function" >array_unique()</B > nimmt <VAR CLASS="parameter" >array</VAR > und gibt ein neues Array zurⁿck, aus dem alle doppelten EintrΣge entfernt wurden. </P ><P >
Beachten Sie, dass Schlⁿssel bewahrt bleiben. Erst behandelt <B CLASS="function" >array_unique()</B > die Werte als Strings und sortiert sie, danach wird der erste gefundene Schlⁿssel behalten, und alle folgenden Schlⁿssel ignoriert. Das hei▀t nicht, dass der Schlⁿssel des ersten zugeh÷rigen Wertes aus dem unsortierten <VAR CLASS="parameter" >array</VAR > behalten wird. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Zwei Elemente werden nur dann als gleich angesehen, wenn <VAR CLASS="literal" >(string) $elem1 === (string) $elem2</VAR >. In Worten: Wenn die String-ReprΣsentation die gleiche ist. </P ><P >
Das erste Element wird verwendet. </P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9009" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_unique()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $input = array ("a" => "grⁿn", "rot", "b" => "grⁿn", "blau", "rot"); $result = array_unique ($input); print_r($result); ?></PRE ></TD ></TR ></TABLE ><P >
Dies wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [a] => grⁿn [0] => rot [1] => blau )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9016" ></A ><P ><B >Beispiel 2. <B CLASS="function" >array_unique()</B > und Typen</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $input = array (4,"4","3",4,3,"3"); $result = array_unique ($input); var_dump($result); ?></PRE ></TD ></TR ></TABLE ><P >
Dies wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >array(2) { [0] => int(4) [2] => string(1) "3" }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.array-unshift" ></A >array_unshift</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9023" ></A ><P > (PHP 4 , PHP 5)</P >array_unshift -- Fⁿgt ein oder mehr Elemente am Anfang eines Arrays ein </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9026" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >array_unshift</B > ( array array, mixed var [, mixed ...])<BR ></BR ><P >
<B CLASS="function" >array_unshift()</B > fⁿgt die ⁿbergebenen Elemente am Anfang von <VAR CLASS="parameter" >array</VAR > ein. Beachten Sie, dass die Liste von Elementen als Ganzes eingefⁿgt wird, sodass die eingefⁿgten Elemente die selbe Reihenfolge haben. Die numerischen Schlⁿssel werden so modifiziert, dass bei null zu zΣhlen begonnen wird, Strings als Schlⁿssel bleiben unverΣndert. </P ><P >
Diese Funktion gibt die neue Anzahl der in <VAR CLASS="parameter" >array</VAR > enthaltenen Elemente zurⁿck. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9046" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_unshift()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $queue = array ("Orange", "Banane"); array_unshift ($queue, "Apfel", "Himbeere"); ?></PRE ></TD ></TR ></TABLE ><P >
Als Ergebnis enthΣlt <VAR CLASS="varname" >$queue</VAR > die folgenden Elemente: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => Apfel [1] => Himbeere [2] => Orange [3] => Banane )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.array-shift" ><B CLASS="function" >array_shift()</B ></A >, <A HREF="#function.array-push" ><B CLASS="function" >array_push()</B ></A > und <A HREF="#function.array-pop" ><B CLASS="function" >array_pop()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-values" ></A >array_values</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9058" ></A ><P > (PHP 4 , PHP 5)</P >array_values -- Liefert alle Werte eines Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN9061" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array_values</B > ( array input)<BR ></BR ><P >
<B CLASS="function" >array_values()</B > liefert alle Werte des Arrays <VAR CLASS="parameter" >input</VAR > mit einem numerischen Index. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9073" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_values()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array = array ("gr÷▀e" => "XL", "farbe" => "gold"); print_r(array_values ($array)); ?></PRE ></TD ></TR ></TABLE ><P >
Dies wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => XL [1] => gold )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.array-keys" ><B CLASS="function" >array_keys()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-walk-recursive" ></A >array_walk_recursive</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9082" ></A ><P > (PHP 5)</P >array_walk_recursive -- Apply a user function recursively to every member of an array </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9085" ></A ><H2 >Description</H2 >bool <B CLASS="methodname" >array_walk_recursive</B > ( array &input, string funcname [, mixed userdata])<BR ></BR ><P >
Applies the user-defined function <VAR CLASS="parameter" >funcname</VAR > to each element of the <VAR CLASS="parameter" >input</VAR > array. This function will recur into deeper arrays. Typically, <VAR CLASS="parameter" >funcname</VAR > takes on two parameters. The <VAR CLASS="parameter" >input</VAR > parameter's value being the first, and the key/index second. If the optional <VAR CLASS="parameter" >userdata</VAR > parameter is supplied, it will be passed as the third parameter to the callback <VAR CLASS="parameter" >funcname</VAR >. </P ><P >
Gibt bei Erfolg <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, im Fehlerfall <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > If <VAR CLASS="parameter" >funcname</VAR > needs to be working with the actual values of the array, specify the first parameter of <VAR CLASS="parameter" >funcname</VAR > as a <A HREF="#language.references" >reference</A >. Then, any changes made to those elements will be made in the original array itself. </P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9115" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_walk_recursive()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $sweet = array('a' => 'apple', 'b' => 'banana'); $fruits = array('sweet' => $sweet, 'sour' => 'lemon'); function test_print($item, $key) { echo "$key holds $item\n"; } array_walk_recursive($fruits, 'test_print'); ?></PRE ></TD ></TR ></TABLE ><P >
The printout of the program above will be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >a holds apple b holds banana sour holds lemon</PRE ></TD ></TR ></TABLE ><P >
You may notice that the key 'sweet' is never displayed. Any key that holds an <A HREF="#language.types.array" ><B CLASS="type" >array</B ></A > will not be passed to the function. </P ></DIV ></TD ></TR ></TABLE > </P ><P >
See also <A HREF="#function.array-walk" ><B CLASS="function" >array_walk()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array-walk" ></A >array_walk</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9126" ></A ><P > (PHP 3>= 3.0.3, PHP 4 , PHP 5)</P >array_walk -- Wendet eine Benutzerfunktion an jedem Element eines Arrays an </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9129" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >array_walk</B > ( array array, callback function [, mixed userdata])<BR ></BR ><P >
Gibt bei Erfolg <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, im Fehlerfall <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P >
Wendet die mittels <VAR CLASS="parameter" >function</VAR > angegebene Benutzerfunktion fⁿr jedes Element von <VAR CLASS="parameter" >array</VAR > an. <VAR CLASS="parameter" >function</VAR > ben÷tigt zwei Parameter. Der Wert von <VAR CLASS="parameter" >array</VAR > wird als erster ⁿbergeben, und der Schlⁿssel/Index als zweites. Ist der optionale Parameter <VAR CLASS="parameter" >userdata</VAR > angegeben, so wird er als dritter Parameter an die Funktion <VAR CLASS="parameter" >function</VAR > ⁿbergeben. </P ><P >
Sollte <VAR CLASS="parameter" >function</VAR > mehr als die ihr ⁿbergebenen Parameter ben÷tigen, wird bei jedem Aufruf von <VAR CLASS="parameter" >function</VAR > seitens <B CLASS="function" >array_walk()</B > eine Fehlermeldung der Stufe <A HREF="#errorfunc.constants" >
E_WARNING</A > generiert. Diese Warnmeldungen k÷nnen unterdrⁿckt werden, indem man dem Funktionsaufruf von <B CLASS="function" >array_walk()</B > den Operator <A HREF="#language.operators.errorcontrol" >@</A > voranstellt, oder <A HREF="#function.error-reporting" ><B CLASS="function" >error_reporting()</B ></A > verwendet. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Wenn <VAR CLASS="parameter" >function</VAR > direkt mit den Werten des Arrays arbeiten soll, spezifizieren Sie den ersten Parameter von <VAR CLASS="parameter" >function</VAR > als <A HREF="#language.references" >Referenz</A >. Dann werden alle ─nderungen an diesen Elementen direkt in dem originalen Array durchgefⁿhrt. </P ><P >
Die Modifizierung des Arrays aus <VAR CLASS="parameter" >function</VAR > heraus k÷nnte ein unvorhersehbares Verhalten hervorrufen. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die ▄bergabe des Schlⁿssels und von <VAR CLASS="parameter" >userdata</VAR > an <VAR CLASS="parameter" >function</VAR > wurde mit PHP 4.0.0 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ><P >
<B CLASS="function" >array_walk()</B > ist nicht von dem internen Arrayzeiger von <VAR CLASS="parameter" >array</VAR > betroffen. <B CLASS="function" >
array_walk()</B > geht ungeachtet der Zeigerposition das gesamte Array durch. Um den Zeiger zurⁿckzusetzen, verwenden Sie <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >. In PHP 3 setzt <B CLASS="function" >array_walk()</B > den Zeiger zurⁿck. </P ><P >
Von der Benutzerfunktion aus darf das Array selbst nicht geΣndert werden, wie zum Beispiel durch Hinzufⁿgen oder L÷schen eines Elementes, etc. Wird das auf <B CLASS="function" >array_walk()</B > angewendete Array geΣndert, so ist das Verhalten dieser Funktion undefiniert und unvorhersagbar. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9181" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array_walk()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("d"=>"Zitrone", "a"=>"Orange", "b"=>"Banane", "c"=>"Apfel"); function test_alter (&$item1, $key, $prefix) { $item1 = "$prefix: $item1"; } function test_print ($item2, $key) { echo "$key. $item2<br>\n"; } echo "Vorher ...:\n"; array_walk ($fruits, 'test_print'); array_walk ($fruits, 'test_alter', 'Frucht'); echo "... und nachher:\n"; array_walk ($fruits, 'test_print'); ?></PRE ></TD ></TR ></TABLE ><P >
Das obige Programm wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Vorher ...: d. Zitrone a. Orange b. Banane c. Apfel ... und nachher: d. Frucht: Zitrone a. Frucht: Orange b. Frucht: Banane c. Frucht: Apfel</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.list" ><B CLASS="function" >list()</B ></A >, <A HREF="#control-structures.foreach" >foreach</A >, <A HREF="#function.each" ><B CLASS="function" >each()</B ></A > und <A HREF="#function.call-user-func-array" ><B CLASS="function" >call_user_func_array()</B ></A >. </P ></DIV ><H1 ><A NAME="function.array" ></A >array</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9193" ></A ><P > (PHP 3, PHP 4, PHP 5 )</P >array -- Erstellt ein Array </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9196" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >array</B > ( [mixed ...])<BR ></BR ><P >
Gibt ein den Parametern entsprechendes Array zurⁿck. Mit dem <VAR CLASS="literal" >=></VAR > Operator k÷nnen die Parameter indiziert werden. Weitere Informationen zu Arrays finden Sie im Abschnitt <A HREF="#language.types.array" >array type</A >. </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <B CLASS="function" >array()</B > ist keine richtige Funktion sondern ein Sprachkonstrukt, das zur Erzeugung eines Wertefeldes genutzt wird. </P ></BLOCKQUOTE ></DIV > </P ><P >
Die Syntax "Index => Werte", durch Kommas getrennt, definiert Index und Werte. Index kann vom Typ String oder numerisch sein. Wird der Index weggelassen, erstellt die Funktion automatisch einen numerischen Index, der bei 0 beginnt. Ist der Index als Integer-Wert angegeben, wird der nΣchste generierte Index der gr÷▀te Integer Index + 1. Beachten Sie, dass wenn zwei identische Indexe definiert sind, der letzte den ersten ⁿberschreibt. </P ><P >
Das folgende Beispiel zeigt wie man ein zweidimensionales Array erstellt, wie man Schlⁿssel fⁿr assoziative Arrays festlegt, und wie man numerische Indizes in normalen Arrays ⁿberspringt und fortsetzt. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9213" ></A ><P ><B >Beispiel 1. <B CLASS="function" >array()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ( "Frⁿchte" => array ("a"=>"Orange", "b"=>"Banane", "c"=>"Apfel"), "Zahlen" => array (1, 2, 3, 4, 5, 6), "L÷cher" => array ("erstes", 5 => "zweites", "drittes") ); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9218" ></A ><P ><B >Beispiel 2. Automatischer Index mit <B CLASS="function" >array()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array = array( 1, 1, 1, 1, 1, 8=>1, 4=>1, 19, 3=>13); print_r($array); ?></PRE ></TD ></TR ></TABLE ><P >
wird folgendes anzeigen: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 13 [4] => 1 [8] => 1 [9] => 19 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Beachten Sie, dass Index '3' doppelt definiert ist, und den letzten definierten Wert 13 behΣlt. Index 4 wurde nach dem Index 8 definiert, und der nΣchste generierte Index (Wert 19) ist 9, da der gr÷▀te Index 8 war. </P ><P >
Dieses Beispiel erstellt ein auf dem Index 1 basierendes Array. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9226" ></A ><P ><B >Beispiel 3. 1-basierter Index mit <B CLASS="function" >array()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $erstesquartal = array(1 => 'Januar', 'Februar', 'MΣrz'); print_r($erstesquartal); ?></PRE ></TD ></TR ></TABLE ><P >
wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [1] => Januar [2] => Februar [3] => MΣrz )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.array-pad" ><B CLASS="function" >array_pad()</B ></A >, <A HREF="#function.list" ><B CLASS="function" >list()</B ></A >, <A HREF="#control-structures.foreach" >foreach</A > und <A HREF="#function.range" ><B CLASS="function" >range()</B ></A >. </P ></DIV ><H1 ><A NAME="function.arsort" ></A >arsort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9238" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >arsort -- Sortiert ein Array in umgekehrter Reihenfolge und behΣlt die Verbindung zum Index </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9241" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >arsort</B > ( array array [, int sort_flags])<BR ></BR ><P >
Diese Funktion sortiert ein Array so, dass der Zusammenhang zwischen den Indizes und den entsprechenden Arrayelementen erhalten bleibt. Dies wird hauptsΣchlich bei assoziativen Arrays verwendet, bei denen die aktuelle Reihenfolge der Elemente bedeutend ist. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9253" ></A ><P ><B >Beispiel 1. <B CLASS="function" >arsort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("d"=>"Zitrone", "a"=>"Orange", "b"=>"Banane", "c"=>"Apfel"); arsort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key = $val\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >d = Zitrone a = Orange b = Banane c = Apfel</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Die Frⁿchte wurden in umgekehrter alphabetischer Reihenfolge sortiert, und die Zuordnung zwischen Index und Element blieb beibehalten. </P ><P >
Sie k÷nnen das Verhalten der Sortierung mittels dem optionalen Parameter <VAR CLASS="parameter" >sort_flags</VAR > beeinflussen, fⁿr Details siehe <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >. </P ><P >
Siehe auch <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >, <A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A > und <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >. </P ></DIV ><H1 ><A NAME="function.asort" ></A >asort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9269" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >asort -- Sortiert ein Array und behΣlt die Indexverbindungen </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9272" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >asort</B > ( array array [, int sort_flags])<BR ></BR ><P >
Diese Funktion sortiert ein Array so, dass der Zusammenhang zwischen den Indizes und den entsprechenden Arrayelementen erhalten bleibt. Dies wird hauptsΣchlich bei assoziativen Arrays verwendet, bei denen die aktuelle Reihenfolge der Elemente bedeutend ist. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9284" ></A ><P ><B >Beispiel 1. <B CLASS="function" >asort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("d"=>"Zitrone", "a"=>"Orange", "b"=>"Banane", "c"=>"Apfel"); asort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key = $val\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >c = Apfel b = Banane a = Orange d = Zitrone</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P >
Die Frⁿchte wurden alphabetisch sortiert, und die Zuordnung zwischen Index und Element blieb beibehalten </P ><P >
Sie k÷nnen das Verhalten der Sortierung mittels dem optionalen Parameter <VAR CLASS="parameter" >sort_flags</VAR > beeinflussen, fⁿr Details siehe <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >. </P ><P >
Siehe auch <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >, <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >, <A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A > und <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >. </P ></DIV ><H1 ><A NAME="function.compact" ></A >compact</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9300" ></A ><P > (PHP 4 , PHP 5)</P >compact -- Erstellt ein Array mit Variablen und deren Werten </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9303" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >compact</B > ( mixed varname [, mixed ...])<BR ></BR ><P >
<B CLASS="function" >compact()</B > ⁿbernimmt eine variable Anzahl von Parametern. Jeder Parameter kann entweder ein String mit einem Variablennamen, oder ein Array mit Variablennamen sein. Dieses Array kann auch andere Arrays mit Variablennamen enthalten; <B CLASS="function" >compact()</B > behandelt sie rekursiv. </P ><P >
Fⁿr alle diese sucht <B CLASS="function" >compact()</B > nach einer Variablen in der aktuellen Symboltabelle, und fⁿgt diese dem zurⁿckzugebenden Array hinzu, wobei der Variablenname als Schlⁿssel, und der Inhalt der Variablen als Wert gespeichert wird. Kurz, diese Funktion tut das Gegenteil von <A HREF="#function.extract" ><B CLASS="function" >extract()</B ></A >. Sie gibt ein Array mit allen Variablen zurⁿck. </P ><P >
Strings, welche nicht gesetzt sind, werden einfach ignoriert. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9322" ></A ><P ><B >Beispiel 1. <B CLASS="function" >compact()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $city = "San Francisco"; $state = "CA"; $event = "SIGGRAPH"; $location_vars = array ("city", "state"); $result = compact ("event", "nothing_here", $location_vars); ?></PRE ></TD ></TR ></TABLE ><P >
Danach wird <VAR CLASS="varname" >$result</VAR >: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [event] => SIGGRAPH [city] => San Francisco [state] => CA )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.extract" ><B CLASS="function" >extract()</B ></A >. </P ></DIV ><H1 ><A NAME="function.count" ></A >count</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9332" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >count -- ZΣhlt die Elemente einer Variable</DIV ><DIV CLASS="refsect1" ><A NAME="AEN9335" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >count</B > ( mixed var)<BR ></BR ><P >
Liefert die Anzahl von Elementen in <VAR CLASS="parameter" >var</VAR >, welche typischerweise ein Array ist (da alles andere ein Element enthΣlt). </P ><P >
Ist <VAR CLASS="parameter" >var</VAR > kein Array, wird <VAR CLASS="literal" >1</VAR > zurⁿckgegeben (Ausnahme: <VAR CLASS="literal" >count(<TT CLASS="constant" ><B >NULL</B ></TT >)</VAR > ist gleich <VAR CLASS="literal" >0</VAR >). </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
<B CLASS="function" >count()</B > kann 0 zurⁿckgeben wenn die Variable nicht gesetzt ist, aber sie k÷nnte ebenfalls 0 zurⁿckgeben, wenn eine Variable als leeres Array initialisiert wurde. Verwenden Sie <A HREF="#function.isset" ><B CLASS="function" >isset()</B ></A > um zu testen, ob eine Variable gesetzt ist. </P ></TD ></TR ></TABLE ></DIV ><P >
In der Sektion <A HREF="#language.types.array" >Arrays</A > finden Sie eine detaillierte ErklΣrung, wie Arrays in PHP implementiert sind und wie sie benutzt werden. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9359" ></A ><P ><B >Beispiel 1. <B CLASS="function" >count()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count ($a); //$result == 3 $b[0] = 7; $b[5] = 9; $b[10] = 11; $result = count ($b); // $result == 3; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die Funktion <A HREF="#function.sizeof" ><B CLASS="function" >sizeof()</B ></A > ist ein <A HREF="#aliases" >Alias</A > fⁿr <B CLASS="function" >count()</B >. </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.is-array" ><B CLASS="function" >is_array()</B ></A >, <A HREF="#function.isset" ><B CLASS="function" >isset()</B ></A > und <A HREF="#function.strlen" ><B CLASS="function" >strlen()</B ></A >. </P ></DIV ><H1 ><A NAME="function.current" ></A >current</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9373" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >current -- Liefert das aktuelle Element eines Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN9376" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >current</B > ( array array)<BR ></BR ><P >
Jedes Array hat einen internen Zeiger auf sein "aktuelles" Element, welcher auf das erste in das Array eingefⁿgte Element initialisiert wird. </P ><P >
Die Funktion <B CLASS="function" >current()</B > liefert das Array Element, auf das gerade vom internen Zeiger gezeigt wird. Sie bewegt den Zeiger in keinster Weise. Zeigt der interne Zeiger hinter das Ende der Elementenliste, gibt <B CLASS="function" >current()</B > <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿck. <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
EnthΣlt das Array leere Elemente (0 oder "", den leeren String), gibt diese Funktion auch fⁿr diese Elemente <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿck. Das macht es unm÷glich, unter Verwendung von <B CLASS="function" >current()</B > herauszufinden, ob das wirklich am Ende der Liste sind. Um ein Array, das leere Elemente enthalten k÷nnte richtig durchzugehen, verwenden Sie die Funktion <A HREF="#function.each" ><B CLASS="function" >each()</B ></A >. </P ></TD ></TR ></TABLE ></DIV > </P ><P >
Siehe auch <A HREF="#function.end" ><B CLASS="function" >end()</B ></A >, <A HREF="#function.key" ><B CLASS="function" >key()</B ></A >, <A HREF="#function.next" ><B CLASS="function" >next()</B ></A >, <A HREF="#function.prev" ><B CLASS="function" >prev()</B ></A > und <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >. </P ></DIV ><H1 ><A NAME="function.each" ></A >each</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9401" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >each -- Liefert das aktuelle Paar (Schlⁿssel und Wert) eines Arrays und rⁿckt den Array Cursor vor. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9404" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >each</B > ( array array)<BR ></BR ><P >
Liefert das aktuelle Schlⁿssel- Wertepaar des Arrays <VAR CLASS="parameter" >array</VAR >, und erh÷ht den Array Cursor. Dieses Paar wird als Array mit 4 Elementen zurⁿckgegeben, und zwar mit den Schlⁿsseln <SPAN CLASS="emphasis" ><I CLASS="emphasis" >0</I ></SPAN >, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >1</I ></SPAN >, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >key</I ></SPAN >, und <SPAN CLASS="emphasis" ><I CLASS="emphasis" >value</I ></SPAN >. Die Elemente <SPAN CLASS="emphasis" ><I CLASS="emphasis" >0</I ></SPAN > und <SPAN CLASS="emphasis" ><I CLASS="emphasis" >key</I ></SPAN > enthalten den Namen des Schlⁿssels, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >1</I ></SPAN > und <SPAN CLASS="emphasis" ><I CLASS="emphasis" >value</I ></SPAN > den Wert. </P ><P >
Zeigt der interne Zeiger hinter das Ende der Array-Inhalte, gibt <B CLASS="function" >each()</B > <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿck. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9426" ></A ><P ><B >Beispiel 1. <B CLASS="function" >each()</B > Beispiele</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese"); $bar = each ($foo); print_r($bar); ?></PRE ></TD ></TR ></TABLE ><P >
<VAR CLASS="varname" >$bar</VAR > enthΣlt nun die folgenden Schlⁿssel/Wert Paare: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [1] => bob [value] => bob [0] => 0 [key] => 0 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<DIV CLASS="informalexample" ><P ></P ><A NAME="AEN9434" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = array ("Robert" => "Bob", "Seppo" => "Sepi"); $bar = each ($foo); print_r($bar); ?></PRE ></TD ></TR ></TABLE ><P >
<VAR CLASS="varname" >$bar</VAR > enthΣlt nun die folgenden Schlⁿssel/Wert Paare: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [1] => Bob [value] => Bob [0] => Robert [key] => Robert )</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
<B CLASS="function" >each()</B > wird typischerweise in Verbindung mit <A HREF="#function.list" ><B CLASS="function" >list()</B ></A > verwendet, um ein Array durchzugehen: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9442" ></A ><P ><B >Beispiel 2. Durchgehen eines Arrays mit <B CLASS="function" >each()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruit = array('a' => 'Apfel', 'b' => 'Banane', 'c' => 'Preiselbeere'); reset ($fruit); while (list ($key, $val) = each ($fruit)) { echo "$key => $val\n"; } /* Ausgabe: a => Apfel b => Banane c => Preiselbeere */ ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Nachdem <B CLASS="function" >each()</B > ausgefⁿhrt wurde, bleibt der Array Cursor beim nΣchsten Element des Arrays, oder wenn am Ende des Arrays angelangt, nach dem letzten Element. Sie mⁿssen <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A > verwenden, wenn Sie das Array erneut mittels each durchgehen wollen. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
Da die Zuweisung eines Arrays zu einer anderen Variable den originalen Arrayzeiger zurⁿcksetzt, wⁿrde unser obiges Beispiel in eine Endlosschleife geraten, hΣtten wir <VAR CLASS="varname" >$fruit</VAR > innerhalb der Schleife einer anderen Variable zugeordnet. </P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch <A HREF="#function.key" ><B CLASS="function" >key()</B ></A >, <A HREF="#function.list" ><B CLASS="function" >list()</B ></A >, <A HREF="#function.current" ><B CLASS="function" >current()</B ></A >, <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >, <A HREF="#function.next" ><B CLASS="function" >next()</B ></A >, <A HREF="#function.prev" ><B CLASS="function" >prev()</B ></A > und <A HREF="#control-structures.foreach" >foreach</A >. </P ></DIV ><H1 ><A NAME="function.end" ></A >end</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9461" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >end -- Positioniert den internen Zeiger eines Arrays auf dessen letztes Element </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9464" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >end</B > ( array array)<BR ></BR ><P >
<B CLASS="function" >end()</B > rⁿckt den internen Zeiger von <VAR CLASS="parameter" >array</VAR > bis zum letzten Element vor, und gibt dieses Element zurⁿck. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9476" ></A ><P ><B >Beispiel 1. Ein einfaches <B CLASS="function" >end()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array('apple','banana','cranberry'); print end($fruits); // cranberry ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.current" ><B CLASS="function" >current()</B ></A >, <A HREF="#function.each" ><B CLASS="function" >each()</B ></A >, <A HREF="#function.next" ><B CLASS="function" >next()</B ></A > und <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >. </P ></DIV ><H1 ><A NAME="function.extract" ></A >extract</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9486" ></A ><P > (PHP 3>= 3.0.7, PHP 4 , PHP 5)</P >extract -- Importiert Variablen eines Arrays in die aktuelle Symboltabelle </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9489" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >extract</B > ( array var_array [, int extract_type [, string prefix]])<BR ></BR ><P >
Diese Funktion wird verwendet, um Variablen eines Arrays in die aktuelle Symboltabelle zu importieren. Es behandelt die Schlⁿssel des assoziativen Arrays <VAR CLASS="parameter" >var_array</VAR > als Variablennamen, und die Werte als Variablenwerte. Fⁿr jedes Paar Schlⁿssel/Wert wird - abhΣngig von den Parametern <VAR CLASS="parameter" >extract_type</VAR > und <VAR CLASS="parameter" >prefix</VAR > - eine Variable in der aktuellen Symboltabelle eingetragen. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Seit Version 4.0.5 gibt diese Funktion die Anzahl der extrahierten Variablen zurⁿck. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <TT CLASS="constant" ><B >EXTR_IF_EXISTS</B ></TT > und <TT CLASS="constant" ><B >EXTR_PREFIX_IF_EXISTS</B ></TT > wurden in Version 4.2.0. eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <TT CLASS="constant" ><B >EXTR_REFS</B ></TT > wurde in Version 4.3.0 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ><P >
<B CLASS="function" >extract()</B > prⁿft jeden Schlⁿssel, ob dieser einen gⁿltigen Variablennamen ergibt, und auch auf eventuelle Kollisionen mit existierenden Variablen in der Symboltabelle. Die Art, wie ungⁿltige/numerische Schlⁿssel und Kollisionen behandelt werden, wird mit <VAR CLASS="parameter" >extract_type</VAR > spezifiziert. Es kann einen der folgenden Werte annehmen: <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="constant" ><B >EXTR_OVERWRITE</B ></TT ></DT ><DD ><P >
Im Falle einer Kollision wird die existierende Variable ⁿberschrieben. </P ></DD ><DT ><TT CLASS="constant" ><B >EXTR_SKIP</B ></TT ></DT ><DD ><P >
Im Falle einer Kollision wird die existierende Variable nicht ⁿberschrieben. </P ></DD ><DT ><TT CLASS="constant" ><B >EXTR_PREFIX_SAME</B ></TT ></DT ><DD ><P >Im Falle einer Kollision wird dem Variablennamen <VAR CLASS="parameter" >prefix</VAR > vorangestellt. </P ></DD ><DT ><TT CLASS="constant" ><B >EXTR_PREFIX_ALL</B ></TT ></DT ><DD ><P >
Allen Variablennamen wird <VAR CLASS="parameter" >prefix</VAR > vorangestellt. Seit PHP 4.0.5 gilt dies auch fⁿr numerische Variablen. </P ></DD ><DT ><TT CLASS="constant" ><B >EXTR_PREFIX_INVALID</B ></TT ></DT ><DD ><P >
Nur ungⁿltigen/numerischen Variablennamen wird <VAR CLASS="parameter" >prefix</VAR > vorangestellt. Dieser Flag wurde mit PHP 4.0.5 eingefⁿhrt. </P ></DD ><DT ><TT CLASS="constant" ><B >EXTR_IF_EXISTS</B ></TT ></DT ><DD ><P >
▄berschreibt die Variable nur, wenn sie bereits in der aktuellen Symboltabelle existiert, sonst geschieht nichts. Dies ist dann hilfreich, wenn Sie eine Liste mit gⁿltigen Variablen definieren, und dann z.B. nur jene Variablen extrahieren, welche Sie aus $_REQUEST definiert haben. Dieser Flag wurde in PHP 4.2.0 eingefⁿhrt. </P ></DD ><DT ><TT CLASS="constant" ><B >EXTR_PREFIX_IF_EXISTS</B ></TT ></DT ><DD ><P >
Erstellt nur Variablennamen mit PrΣfix, wenn die Version ohne PrΣfix der selben Variable in der aktuellen Symboltabelle existiert. Dieser Flag wurde in PHP 4.2.0 eingefⁿhrt. </P ></DD ><DT ><TT CLASS="constant" ><B >EXTR_REFS</B ></TT ></DT ><DD ><P >
Extrahiert Variablen als Referenzen. Das hei▀t, dass die Werte der importierten Variablen noch immer auf die Werte des Parameters <VAR CLASS="parameter" >var_array</VAR > referenzieren. Sie k÷nnen diesen Flag alleine oder auch in Kombination mit einem anderen Flag verwenden, indem Sie Oder-Verknⁿpfungen im <VAR CLASS="parameter" >extract_type</VAR > erstellen. Dieser Flag wurde in PHP 4.3.0 eingefⁿhrt. </P ></DD ></DL ></DIV > </P ><P >
Ist <VAR CLASS="parameter" >extract_type</VAR > nicht spezifiziert, so wird <TT CLASS="constant" ><B >EXTR_OVERWRITE</B ></TT > angenommen. </P ><P >
Beachten Sie, dass <VAR CLASS="parameter" >prefix</VAR > nur dann n÷tig ist, wenn <VAR CLASS="parameter" >extract_type</VAR > <TT CLASS="constant" ><B >EXTR_PREFIX_SAME</B ></TT >, <TT CLASS="constant" ><B >EXTR_PREFIX_ALL</B ></TT >, <TT CLASS="constant" ><B >EXTR_PREFIX_INVALID</B ></TT > or <TT CLASS="constant" ><B >EXTR_PREFIX_IF_EXISTS</B ></TT > ist. Ist das Resultat trotz PrΣfix kein gⁿltiger Variablenname, wird es nicht in die Symboltabelle importiert. </P ><P >
<B CLASS="function" >extract()</B > gibt die Anzahl erfolgreich in die Symboltabelle importierter Variablen zurⁿck. </P ><P >
Eine m÷gliche Verwendung fⁿr <B CLASS="function" >extract()</B > ist der Import der Variablen in einem von <A HREF="#function.wddx-deserialize" ><B CLASS="function" >wddx_deserialize()</B ></A > gelieferten assoziativen Array in die Symboltabelle. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9581" ></A ><P ><B >Beispiel 1. <B CLASS="function" >extract()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* Annahme: $var_array ist ein von wddx_deserialize zurⁿckgegebenes Array */ $groesse = "gro▀"; $var_array = array ("farbe" => "blau", "groesse" => "mittel", "form" => "Kugel"); extract ($var_array, EXTR_PREFIX_SAME, "wddx"); print "$farbe, $groesse, $form, $wddx_groesse\n"; ?></PRE ></TD ></TR ></TABLE ><P >
Das obige Beispiel wird folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >blau, gro▀, Kugel, mittel</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<VAR CLASS="varname" >$size</VAR > wurde nicht ⁿberschrieben, da wir <TT CLASS="constant" ><B >EXTR_PREFIX_SAME</B ></TT > spezifiziert haben, was im Anlegen von <VAR CLASS="varname" >$wddx_size</VAR > endete. WΣre <TT CLASS="constant" ><B >EXTR_SKIP</B ></TT > spezifiziert worden, dann wΣre $wddx_size nicht angelegt worden. <TT CLASS="constant" ><B >EXTR_OVERWRITE</B ></TT > hΣtte <VAR CLASS="varname" >$size</VAR > mit dem Wert "medium" ⁿberschrieben, und <TT CLASS="constant" ><B >EXTR_PREFIX_ALL</B ></TT > hΣtte die neuen Variablen <VAR CLASS="varname" >$wddx_color</VAR >, <VAR CLASS="varname" >$wddx_size</VAR >, und <VAR CLASS="varname" >$wddx_shape</VAR > angelegt. </P ><P >
Sie mⁿssen assoziative Arrays benutzen, numerisch indizierte Arrays liefern keine Resultate, au▀er Sie verwenden <TT CLASS="constant" ><B >EXTR_PREFIX_ALL</B ></TT > oder <TT CLASS="constant" ><B >EXTR_PREFIX_INVALID</B ></TT >. </P ><P >
Siehe auch <A HREF="#function.compact" ><B CLASS="function" >compact()</B ></A >. </P ></DIV ><H1 ><A NAME="function.in-array" ></A >in_array</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9604" ></A ><P > (PHP 4 , PHP 5)</P >in_array -- Prⁿft, ob ein Wert in einem Array existiert</DIV ><DIV CLASS="refsect1" ><A NAME="AEN9607" ></A ><H2 >Beschreibung</H2 >bool <B CLASS="methodname" >in_array</B > ( mixed needle, array haystack [, bool strict])<BR ></BR ><P >
Diese Funktion sucht in <VAR CLASS="parameter" >haystack</VAR > nach <VAR CLASS="parameter" >needle</VAR > und gibt bei Erfolg <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, andernfalls <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P >
Ist der dritte Parameter <VAR CLASS="parameter" >strict</VAR > auf <TT CLASS="constant" ><B >TRUE</B ></TT > gesetzt, prⁿft <B CLASS="function" >in_array()</B > auch die <A HREF="#language.types" >Typen</A > von <VAR CLASS="parameter" >needle</VAR > in <VAR CLASS="parameter" >haystack</VAR >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Ist <VAR CLASS="parameter" >needle</VAR > ein String, erfolgt der Vergleich unter Berⁿcksichtigung der Gro▀- und Kleinschreibung. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > In den PHP Versionen vor 4.2.0 durfte <VAR CLASS="parameter" >needle</VAR > kein Array sein </P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9640" ></A ><P ><B >Beispiel 1. <B CLASS="function" >in_array()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $os = array ("Mac", "NT", "Irix", "Linux"); if (in_array ("Irix", $os)) { print "Got Irix"; } if (in_array ("mac", $os)) { print "Got mac"; } ?></PRE ></TD ></TR ></TABLE ><P >
Die zweite Bedingung schlΣgt fehl, da <B CLASS="function" >in_array()</B > die Gro▀-/Kleinschreibung berⁿcksichtigt, daher wird das obige Programm folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Got Irix</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9648" ></A ><P ><B >Beispiel 2. <B CLASS="function" >in_array()</B > mit strict</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array('1.10', 12.4, 1.13); if (in_array('12.4', $a, TRUE)) { echo "'12.4' gefunden mit strict check\n"; } if (in_array(1.13, $a, TRUE)) { echo "1.13 gefunden mit strict check\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dies wird folgendes anzeigen: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >1.13 gefunden mit strict check</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9655" ></A ><P ><B >Beispiel 3. <B CLASS="function" >in_array()</B > mit einem Array als needle</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = array(array('p', 'h'), array('p', 'r'), 'o'); if (in_array(array ('p', 'h'), $a)) { echo "'ph' wurde gefunden\n"; } if (in_array(array ('f', 'i'), $a)) { echo "'fi' wurde nicht gefunden\n"; } if (in_array('o', $a)) { echo "'o' wurde gefunden\n"; } ?> /* Ausgabe: 'ph' wurde gefunden 'o' wurde gefunden */</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.array-search" ><B CLASS="function" >array_search()</B ></A >. </P ></DIV ><H1 ><A NAME="function.key" ></A >key</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9662" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >key -- Liefert einen Schlⁿssel eines assoziativen Arrays </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9665" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >key</B > ( array array)<BR ></BR ><P >
<B CLASS="function" >key()</B > liefert das Index Element der aktuellen Position im Array. </P ><P >
Siehe auch <A HREF="#function.current" ><B CLASS="function" >current()</B ></A > und <A HREF="#function.next" ><B CLASS="function" >next()</B ></A >. </P ></DIV ><H1 ><A NAME="function.krsort" ></A >krsort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9679" ></A ><P > (PHP 3>= 3.0.13, PHP 4 , PHP 5)</P >krsort -- Sortiert ein Array nach Schlⁿsseln in umgekehrter Reihenfolge </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9682" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >krsort</B > ( array array [, int sort_flags])<BR ></BR ><P >
Sortiert ein Array nach Schlⁿsseln in umgekehrter Reihenfolge, wobei die Zuordnungen zwischen Schlⁿssel und Wert erhalten bleiben. Dies ist hauptsΣchlich bei assoziativen Arrays hilfreich. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9695" ></A ><P ><B >Beispiel 1. <B CLASS="function" >krsort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("d"=>"Zitrone", "a"=>"Orange", "b"=>"Banane", "c"=>"Apfel"); krsort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key = $val\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >d = Zitrone c = Apfel b = Banane a = Orange</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Sie k÷nnen das Verhalten der Sortierung mittels dem optionalen Parameter <VAR CLASS="parameter" >sort_flags</VAR > beeinflussen, fⁿr Details siehe <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >. </P ><P >
Siehe auch <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >, <A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A >, <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >, <A HREF="#function.natsort" ><B CLASS="function" >natsort()</B ></A > und <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >. </P ></DIV ><H1 ><A NAME="function.ksort" ></A >ksort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9712" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >ksort -- Sortiert ein Array nach Schlⁿsseln</DIV ><DIV CLASS="refsect1" ><A NAME="AEN9715" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >ksort</B > ( array array [, int sort_flags])<BR ></BR ><P >
Sortiert ein Array nach Schlⁿsseln, wobei die Zuordnungen zwischen Schlⁿssel und Wert erhalten bleiben. Dies ist hauptsΣchlich bei assoziativen Arrays hilfreich. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9728" ></A ><P ><B >Beispiel 1. <B CLASS="function" >ksort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("d"=>"Zitrone", "a"=>"Orange", "b"=>"Banane", "c"=>"Apfel"); ksort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key = $val\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >a = Orange b = Banane c = Apfel d = Zitrone</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Sie k÷nnen das Verhalten der Sortierung mittels dem optionalen Parameter <VAR CLASS="parameter" >sort_flags</VAR > beeinflussen, fⁿr Details siehe <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >. </P ><P >
Siehe auch <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >, <A HREF="#function.krsort" ><B CLASS="function" >krsort()</B ></A >, <A HREF="#function.uksort" ><B CLASS="function" >uksort()</B ></A >, <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >, <A HREF="#function.natsort" ><B CLASS="function" >natsort()</B ></A > und <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der zweite Parameter wurde mit PHP 4 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ></DIV ><H1 ><A NAME="function.list" ></A >list</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9748" ></A ><P > (PHP 3, PHP 4, PHP 5 )</P >list -- Weist Variablen zu, als wΣren sie ein Array </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9751" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >list</B > ( mixed ...)<BR ></BR ><P >
Wie <A HREF="#function.array" ><B CLASS="function" >array()</B ></A > ist auch dies keine wirkliche Funktion, sondern ein Sprachkonstrukt. <B CLASS="function" >list()</B > wird verwendet, um eine Liste von Variablen innerhalb einer Operation zuzuweisen. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > <B CLASS="function" >list()</B > funktioniert nur bei numerischen Arrays und basiert auf der Annahme, dass die numerischen Indizes bei 0 beginnen. </P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9766" ></A ><P ><B >Beispiel 1. <B CLASS="function" >list()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $info = array('coffee', 'brown', 'caffeine'); // Auflisten aller Variablen list($drink, $color, $power) = $info; print "$drink is $color and $power makes it special.\n"; // Ein paar davon auflisten list($drink, , $power) = $info; print "$drink has $power.\n"; // Oder nur die dritte ausgeben list( , , $power) = $info; print "I need $power!\n"; ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9771" ></A ><P ><B >Beispiel 2. <B CLASS="function" >list()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><table> <tr> <th>Mitarbeitername</th> <th>Gehalt</th> </tr> <?php $result = mysql_query ("SELECT id, name, salary FROM employees",$conn); while (list ($id, $name, $salary) = mysql_fetch_row ($result)) { print (" <tr>\n". " <td><a href=\"info.php?id=$id\">$name</a></td>\n". " <td>$salary</td>\n". " </tr>\n"); } ?> </table></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
<B CLASS="function" >list()</B > weist die Werte von rechts beginnend zu. Wenn Sie einfache Variablen benutzen, brauchen Sie sich nicht darum zu kⁿmmern. Wenn Sie jedoch Arrays mit Indizes verwenden, erwarten Sie gew÷hnlich die Reihenfolge der Indizes in dem Array genau so, wie Sie sie in <B CLASS="function" >list()</B > geschrieben haben (von links nach rechts), was jedoch nicht der Fall ist. Es wird in der umgekehrten Reihenfolge zugewiesen. </P ></TD ></TR ></TABLE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9780" ></A ><P ><B >Beispiel 3. <B CLASS="function" >list()</B > mit Array Indizes verwenden</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $info = array('coffee', 'brown', 'caffeine'); list($a[0], $a[1], $a[2]) = $info; var_dump($a); ?></PRE ></TD ></TR ></TABLE ><P >
Die Ausgabe (Beachten Sie die Reihenfolge der Elemente, verglichen mit der in <B CLASS="function" >list()</B > eingetragenen Reihenfolge): </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >array(3) { [2]=> string(8) "caffeine" [1]=> string(5) "brown" [0]=> string(6) "coffee" }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.each" ><B CLASS="function" >each()</B ></A >, <A HREF="#function.array" ><B CLASS="function" >array()</B ></A > und <A HREF="#function.extract" ><B CLASS="function" >extract()</B ></A >. </P ></DIV ><H1 ><A NAME="function.natcasesort" ></A >natcasesort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9792" ></A ><P > (PHP 4 , PHP 5)</P >natcasesort -- Sortiert ein Array in "natⁿrlicher Reihenfolge", Gro▀/Kleinschreibung wird ignoriert </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9795" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >natcasesort</B > ( array array)<BR ></BR ><P >
Diese Funktion implementiert einen Sortieralgorithmus, welcher alphanumerische Zeichenketten reiht, wie es auch ein Mensch tun wⁿrde. Das wird als "natⁿrliche Reihenfolge" bezeichnet. </P ><P >
<B CLASS="function" >natcasesort()</B > arbeitet wie <A HREF="#function.natsort" ><B CLASS="function" >natsort()</B ></A >, nur dass es die Gro▀/Kleinschreibung ignoriert. Ein Beispiel fⁿr den Unterschied zwischen diesem und dem normalen Sortieralgorithmus fⁿr Strings eines Computers finden Sie in <A HREF="#function.natsort" ><B CLASS="function" >natsort()</B ></A >. </P ><P >
Detailliertere Informationen finden Sie auf Martin Pool's <A HREF="http://sourcefrog.net/projects/natsort/" TARGET="_top" >Natural Order String Comparison</A > Seite. </P ><P >
Siehe auch <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >, <A HREF="#function.natsort" ><B CLASS="function" >natsort()</B ></A >, <A HREF="#function.strnatcmp" ><B CLASS="function" >strnatcmp()</B ></A > und <A HREF="#function.strnatcasecmp" ><B CLASS="function" >strnatcasecmp()</B ></A >. </P ></DIV ><H1 ><A NAME="function.natsort" ></A >natsort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9816" ></A ><P > (PHP 4 , PHP 5)</P >natsort -- Sortiert ein Array in "natⁿrlicher Reihenfolge" </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9819" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >natsort</B > ( array array)<BR ></BR ><P >
Diese Funktion implementiert einen Sortieralgorithmus, welcher alphanumerische Zeichenketten reiht, wie es auch ein Mensch tun wⁿrde. Hier ein Beispiel fⁿr den Unterschied zwischen diesem und dem normalen Sortieralgorithmus fⁿr Strings eines Computers (verwendet in <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >): </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9830" ></A ><P ><B >Beispiel 1. <B CLASS="function" >natsort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $array1 = $array2 = array ("img12.png", "img10.png", "img2.png", "img1.png"); sort ($array1); echo "Standardsortierung\n"; print_r ($array1); natsort ($array2); echo "\nSortieren mit natⁿrlicher Reihenfolge\n"; print_r ($array2); ?></PRE ></TD ></TR ></TABLE ><P >
Der obige Code wird folgendes anzeigen: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Standardsortierung Array ( [0] => img1.png [1] => img10.png [2] => img12.png [3] => img2.png ) Sortieren mit natⁿrlicher Reihenfolge Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )</PRE ></TD ></TR ></TABLE ><P >
Detailliertere Informationen finden Sie auf Martin Pool's <A HREF="http://sourcefrog.net/projects/natsort/" TARGET="_top" >Natural Order String Comparison</A > Seite. </P ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Wenn Sie die Index/Wert Assoziationen beibehalten wollen, denken Sie an die Verwendung von <VAR CLASS="literal" >uasort($arr, 'strnatcmp')</VAR >. </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.natcasesort" ><B CLASS="function" >natcasesort()</B ></A >, <A HREF="#function.strnatcmp" ><B CLASS="function" >strnatcmp()</B ></A > und <A HREF="#function.strnatcasecmp" ><B CLASS="function" >strnatcasecmp()</B ></A >. </P ></DIV ><H1 ><A NAME="function.next" ></A >next</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9846" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >next -- Rⁿckt den internen Zeiger eines Arrays vor </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9849" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >next</B > ( array array)<BR ></BR ><P >
Diese Funktion gibt das nΣchste Element des Arrays zurⁿck, auf das der interne Zeiger des Arrays gerade zeigt. Sind keine Elemente mehr vorhanden, wird <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgegeben. </P ><P >
<B CLASS="function" >next()</B > verhΣlt sich Σhnlich wie <A HREF="#function.current" ><B CLASS="function" >current()</B ></A >, mit einem Unterschied. Der interne Zeiger wird um ein Element vorgerⁿckt, bevor das Element zurⁿckgegeben wird, d.h. es wird das nΣchste Element zurⁿckgegeben, und der interne Zeiger um eins erh÷ht. Zeigt der interne Zeiger hinter das Ende der Elementliste, wird <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgegeben. <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
EnthΣlt das Array leere Elemente oder Elemente mit dem Schlⁿssel 0, wird fⁿr diese Elemente ebenfalls <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgegeben. Um ein Array, das leere Elemente oder Elemente mit dem Schlⁿssel 0 enthalten k÷nnte richtig durchzugehen, verwenden Sie die Funktion <A HREF="#function.each" ><B CLASS="function" >each()</B ></A >. </P ></TD ></TR ></TABLE ></DIV > </P ><P >
Siehe auch <A HREF="#function.current" ><B CLASS="function" >current()</B ></A >, <A HREF="#function.end" ><B CLASS="function" >end()</B ></A >, <A HREF="#function.prev" ><B CLASS="function" >prev()</B ></A > und <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >. </P ></DIV ><H1 ><A NAME="function.pos" ></A >pos</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9873" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >pos -- Liefert das aktuelle Element eines Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN9876" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >pos</B > ( array array)<BR ></BR ><P >
Dies ist nur ein <A HREF="#aliases" >Alias</A > fⁿr die Funktion <A HREF="#function.current" ><B CLASS="function" >current()</B ></A >. </P ><P >
Siehe auch <A HREF="#function.end" ><B CLASS="function" >end()</B ></A >, <A HREF="#function.next" ><B CLASS="function" >next()</B ></A >, <A HREF="#function.prev" ><B CLASS="function" >prev()</B ></A > und <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >. </P ></DIV ><H1 ><A NAME="function.prev" ></A >prev</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9893" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >prev -- Verkleinert den internen Zeiger eines Arrays </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9896" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >prev</B > ( array array)<BR ></BR ><P >
Liefert das Element vor der Position des Arrays, auf die der interne Zeiger gerade zeigt. Sind keine Elemente mehr vorhanden, wird <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgegeben. <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warnung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
EnthΣlt das Array leere Elemente, wird fⁿr diese ebenfalls <TT CLASS="constant" ><B >FALSE</B ></TT > zurⁿckgegeben. Um ein Array mit leeren Elementen richtig durchzugehen, verwenden Sie die Funktion <A HREF="#function.each" ><B CLASS="function" >each()</B ></A >. </P ></TD ></TR ></TABLE ></DIV > </P ><P >
<B CLASS="function" >prev()</B > verhΣlt sich wie <A HREF="#function.next" ><B CLASS="function" >next()</B ></A >, nur dass hier der interne Zeiger um eine Position zurⁿck, und nicht nach vorne gerⁿckt wird. </P ><P >
Siehe auch <A HREF="#function.current" ><B CLASS="function" >current()</B ></A >, <A HREF="#function.end" ><B CLASS="function" >end()</B ></A >, <A HREF="#function.next" ><B CLASS="function" >next()</B ></A > und <A HREF="#function.reset" ><B CLASS="function" >reset()</B ></A >. </P ></DIV ><H1 ><A NAME="function.range" ></A >range</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9919" ></A ><P > (PHP 3>= 3.0.8, PHP 4 , PHP 5)</P >range -- Erstellt ein Array mit einem Bereich von Elementen </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9922" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >range</B > ( int low, int high [, int step])<BR ></BR ><P >
<B CLASS="function" >range()</B > gibt ein Array mit Elementen im Bereich von <VAR CLASS="parameter" >low</VAR > bis <VAR CLASS="parameter" >high</VAR > zurⁿck. Wenn low > high, wird die Sequenz von high nach low sein. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Neuer Parameter: </B > Der optionale Parameter <VAR CLASS="parameter" >step</VAR > wurde in 5.0.0 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ><P >
Ist ein <VAR CLASS="parameter" >step</VAR > Wert angegeben, wird es Schrittweite zwischen den Elementen in der Sequenz verwendet. <VAR CLASS="parameter" >step</VAR > sollte als positive Zahl angegeben werden. Ist <VAR CLASS="parameter" >step</VAR > nicht angegeben, wird automatisch der Wert 1 angenommen. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN9949" ></A ><P ><B >Beispiel 1. <B CLASS="function" >range()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // array(0,1,2,3,4,5,6,7,8,9,10,11,12) foreach(range(0, 12) as $number) { echo $number; } // Der Parameter step wurde in PHP 5.0.0 eingefⁿhrt // array(0,10,20,30,40,50,60,70,80,90,100) foreach(range(0, 100, 10) as $number) { echo $number; } // Die Verwendung von Zeichenfolgen wurde in 4.1.0 eingefⁿhrt // array('a','b','c','d','e','f','g','h','i'); foreach(range('a', 'i') as $letter) { echo $letter; } // array('c','b','a'); foreach(range('c', 'a') as $letter) { echo $letter; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Vor der Version 4.1.0 hat die Funktion <B CLASS="function" >range()</B > nur aufsteigende Integer Arrays erstellt. Die Unterstⁿtzung fⁿr Zeichenfolgen und Arrays in absteigender Reihenfolge wurde in 4.1.0 hinzugefⁿgt. Werte mit Zeichenfolgen sind auf eine LΣnge von 1 limitiert. Wird eine LΣnge gr÷▀er als 1 eingegeben, wird nur das erste Zeichen verwendet. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Achtung</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >
In den PHP Versionen 4.1.0 bis 4.3.2 werden numerische Zeichenfolgen von <B CLASS="function" >range()</B > als Strings, und nicht als integer gesehen. Stattdessen werden diese fⁿr Zeichenfolgen genutzt, z.B. wird <VAR CLASS="literal" >"4242"</VAR > als <VAR CLASS="literal" >"4"</VAR > betrachtet. </P ></TD ></TR ></TABLE ></DIV ><P >
Siehe auch <A HREF="#function.shuffle" ><B CLASS="function" >shuffle()</B ></A > und <A HREF="#control-structures.foreach" >foreach</A >. </P ></DIV ><H1 ><A NAME="function.reset" ></A >reset</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9965" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >reset -- Setzt den internen Zeiger eines Arrays auf sein erstes Element </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9968" ></A ><H2 >Beschreibung</H2 >mixed <B CLASS="methodname" >reset</B > ( array array)<BR ></BR ><P >
<B CLASS="function" >reset()</B > setzt den internen Zeiger von <VAR CLASS="parameter" >array</VAR > auf das erste Element, und gibt den Wert des ersten Arrayelements zurⁿck. </P ><P >
Siehe auch <A HREF="#function.current" ><B CLASS="function" >current()</B ></A >, <A HREF="#function.each" ><B CLASS="function" >each()</B ></A >, <A HREF="#function.next" ><B CLASS="function" >next()</B ></A > und <A HREF="#function.prev" ><B CLASS="function" >prev()</B ></A >. </P ></DIV ><H1 ><A NAME="function.rsort" ></A >rsort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN9985" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >rsort -- Sortiert ein Array in umgekehrter Reihenfolge </DIV ><DIV CLASS="refsect1" ><A NAME="AEN9988" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >rsort</B > ( array array [, int sort_flags])<BR ></BR ><P >
Diese Funktion sortiert ein Array in umgekehrter Reihenfolge (vom h÷chsten zum niedrigsten Wert). </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10001" ></A ><P ><B >Beispiel 1. <B CLASS="function" >rsort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("Zitrone", "Orange", "Banane", "Apfel"); rsort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "$key = $val\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >0 = Zitrone 1 = Orange 2 = Banane 3 = Apfel</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Die Frⁿchte wurden in umgekehrter alphabetischer Reihenfolge sortiert. </P ><P >
Sie k÷nnen das Verhalten der Sortierung mittels dem optionalen Parameter <VAR CLASS="parameter" >sort_flags</VAR > beeinflussen, fⁿr Details siehe <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >. </P ><P >
Siehe auch <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >, <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A >, <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A > und <A HREF="#function.usort" ><B CLASS="function" >usort()</B ></A >. </P ></DIV ><H1 ><A NAME="function.shuffle" ></A >shuffle</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10018" ></A ><P > (PHP 3>= 3.0.8, PHP 4 , PHP 5)</P >shuffle -- Mischt die Elemente eines Arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN10021" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >shuffle</B > ( array array)<BR ></BR ><P >
Diese Funktion mischt die Reihenfolge der Elemente eines Arrays zufΣllig (shuffle). Benutzen Sie <A HREF="#function.srand" ><B CLASS="function" >srand()</B ></A >, um den Startwert fⁿr den Zufallsgenerator festzulegen. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10031" ></A ><P ><B >Beispiel 1. <B CLASS="function" >shuffle()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $numbers = range (1,20); srand ((float)microtime()*1000000); shuffle ($numbers); while (list (, $number) = each ($numbers)) { echo "$number "; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B >Seit PHP 4.2.0 besteht keine Notwendigkeit mehr, den Zufallsgenerator fⁿr Zahlen mit <A HREF="#function.srand" ><B CLASS="function" >srand()</B ></A > oder <A HREF="#function.mt-srand" ><B CLASS="function" >mt_srand()</B ></A > zu fⁿttern, das geschieht nun automatisch.</P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >, <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A >, <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >, <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A > und <A HREF="#function.usort" ><B CLASS="function" >usort()</B ></A >. </P ></DIV ><H1 ><A NAME="function.sizeof" ></A >sizeof</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10047" ></A >sizeof -- Alias fⁿr <A HREF="#function.count" ><B CLASS="function" >count()</B ></A ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN10051" ></A ><H2 >Beschreibung</H2 ><P >
Diese Funktion ist ein Alias fⁿr <A HREF="#function.count" ><B CLASS="function" >count()</B ></A >. </P ></DIV ><H1 ><A NAME="function.sort" ></A >sort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10056" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >sort -- Sortiert ein Array</DIV ><DIV CLASS="refsect1" ><A NAME="AEN10059" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >sort</B > ( array array [, int sort_flags])<BR ></BR ><P >
Diese Funktion sortiert ein Array. Die Elemente werden aufsteigend vom niedrigsten zum h÷chsten Wert sortiert. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10072" ></A ><P ><B >Beispiel 1. <B CLASS="function" >sort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("Zitrone", "Orange", "Banane", "Apfel"); sort ($fruits); reset ($fruits); while (list ($key, $val) = each ($fruits)) { echo "fruits[".$key."] = ".$val."\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes anzeigen: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >fruits[0] = Apfel fruits[1] = Banane fruits[2] = Orange fruits[3] = Zitrone</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Die Frⁿchte wurden in alphabetischer Reihenfolge sortiert. </P ><P >
Der optionale zweite Parameter <VAR CLASS="parameter" >sort_flags</VAR > kann benutzt werden, um das Sortierverhalten mit den folgenden Flags zu beeinflussen. </P ><P >
Flags fⁿr Sortiertypen: <P ></P ><UL ><LI ><P >SORT_REGULAR - Vergleiche EintrΣge normal</P ></LI ><LI ><P >SORT_NUMERIC - Vergleiche EintrΣge numerisch</P ></LI ><LI ><P >SORT_STRING - Vergleiche EintrΣge als Strings</P ></LI ></UL > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Der zweite Parameter wurde in PHP 4 eingefⁿhrt. </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >, <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A >, <A HREF="#function.natsort" ><B CLASS="function" >natsort()</B ></A >, <A HREF="#function.natcasesort" ><B CLASS="function" >natcasesort()</B ></A >, <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >, <A HREF="#function.usort" ><B CLASS="function" >usort()</B ></A >, <A HREF="#function.array-multisort" ><B CLASS="function" >array_multisort()</B ></A > und <A HREF="#function.uksort" ><B CLASS="function" >uksort()</B ></A >. </P ></DIV ><H1 ><A NAME="function.uasort" ></A >uasort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10102" ></A ><P > (PHP 3>= 3.0.4, PHP 4 , PHP 5)</P >uasort -- Sortiert ein Array mittels einer benutzerdefinierten Vergleichsfunktion und behΣlt Indexassoziationen bei. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10105" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >uasort</B > ( array array, callback cmp_function)<BR ></BR ><P >
Diese Funktion sortiert ein Array so, dass die Beziehung der Indices zu den Arrayelementen beibehalten bleibt. Dies wird hauptsΣchlich bei assoziativen Arrays angewandt, wenn die aktuelle Reihenfolge der Arrayelemente signifikant ist. Die Vergleichsfunktion wird vom Benutzer definiert. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Siehe <A HREF="#function.usort" ><B CLASS="function" >usort()</B ></A > und <A HREF="#function.uksort" ><B CLASS="function" >uksort()</B ></A > fⁿr Beispiele von benutzerdefinierten Vergleichsfunktionen </P ></BLOCKQUOTE ></DIV ><P >
Siehe auch <A HREF="#function.usort" ><B CLASS="function" >usort()</B ></A >, <A HREF="#function.uksort" ><B CLASS="function" >uksort()</B ></A >, <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >, <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >, <A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A >, und <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >. </P ></DIV ><H1 ><A NAME="function.uksort" ></A >uksort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10130" ></A ><P > (PHP 3>= 3.0.4, PHP 4 , PHP 5)</P >uksort -- Sortiert ein Array nach Schlⁿsseln mittels einer benutzerdefinierten Vergleichsfunktion. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10133" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >uksort</B > ( array array, callback cmp_function)<BR ></BR ><P >
Diese Funktion sortiert die Schlⁿssel eines Arrays mittels einer benutzerdefinierten Vergleichsfunktion. Soll das Array nach nicht trivialen Kriterien sortiert werden, sollten Sie diese Funktion verwenden. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10146" ></A ><P ><B >Beispiel 1. <B CLASS="function" >uksort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function cmp ($a, $b) { if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } $a = array (4 => "vier", 3 => "drei", 20 => "zwanzig", 10 => "zehn"); uksort ($a, "cmp"); while (list ($key, $value) = each ($a)) { echo "$key: $value\n"; }</PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes ausgeben: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >20: zwanzig 10: zehn 4: vier 3: drei</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.usort" ><B CLASS="function" >usort()</B ></A >, <A HREF="#function.uasort" ><B CLASS="function" >uasort()</B ></A >, <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >, <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >, <A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A >, <A HREF="#function.natsort" ><B CLASS="function" >natsort()</B ></A > und <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >. </P ></DIV ><H1 ><A NAME="function.usort" ></A >usort</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10162" ></A ><P > (PHP 3>= 3.0.3, PHP 4 , PHP 5)</P >usort -- Sortiert ein Array nach Werten mittels einer benutzerdefinierten Vergleichsfunktion. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10165" ></A ><H2 >Beschreibung</H2 >void <B CLASS="methodname" >usort</B > ( array array, callback cmp_function)<BR ></BR ><P >
Diese Funktion sortiert ein Array nach seinen Werten mittels einer benutzerdefinierten Vergleichsfunktion. Soll das Array nach nicht trivialen Kriterien sortiert werden, sollten Sie diese Funktion verwenden. </P ><P >
Die Vergleichsfunktion muss einen Integer zurⁿckgeben, der kleiner, gleich, oder gr÷▀er ist als <TT CLASS="constant" ><B >NULL</B ></TT >, wenn fⁿr das erste Argument auch angenommen wird, dass es kleiner, gleich, oder gr÷▀er ist als das zweite. </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Sind zwei Elemente gleich, ist deren Reihenfolge nicht definiert. Bis zu PHP 4.0.6 wⁿrde die benutzerdefinierte Funktion die originΣre Reihenfolge beibehalten, doch seit der Einfⁿhrung des neuen Sortieralgorhythmus mit 4.1.0 ist dies nicht mehr der Fall, da es keine effiziente L÷sung dafⁿr gibt. </P ></BLOCKQUOTE ></DIV > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10183" ></A ><P ><B >Beispiel 1. <B CLASS="function" >usort()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function cmp ($a, $b) { if ($a == $b) return 0; return ($a < $b) ? -1 : 1; } $a = array (3, 2, 5, 6, 1); usort ($a, "cmp"); while (list ($key, $value) = each ($a)) { echo "$key: $value\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes anzeigen: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >0: 1 1: 2 2: 3 3: 5 4: 6</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Offensichtlich wⁿrde <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A > fⁿr diesen einfachen Fall besser passen. </P ></BLOCKQUOTE ></DIV ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10193" ></A ><P ><B >Beispiel 2. <B CLASS="function" >usort()</B > mit einem mehrdimensionalen Array </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function cmp ($a, $b) { return strcmp($a["fruit"], $b["fruit"]); } $fruits[0]["fruit"] = "Zitronen"; $fruits[1]["fruit"] = "─pfel"; $fruits[2]["fruit"] = "Trauben"; usort($fruits, "cmp"); while (list ($key, $value) = each ($fruits)) { echo "\$fruits[$key]: " . $value["fruit"] . "\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Wird ein mehrdimensionales Array sortiert, enthalten $a und $b Referenzen zu dem ersten Index des Arrays. </P ><P >
Dieses Beispiel wⁿrde folgendes anzeigen: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$fruits[0]: ─pfel $fruits[1]: Trauben $fruits[2]: Zitronen</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10201" ></A ><P ><B >Beispiel 3. <B CLASS="function" >usort()</B > mit Verwendung einer Member Funktion eines Objektes </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class TestObj { var $name; function TestObj($name) { $this->name = $name; } /* Dies ist die statische Vergleichsfunktion: */ function cmp_obj($a, $b) { $al = strtolower($a->name); $bl = strtolower($b->name); if ($al == $bl) return 0; return ($al > $bl) ? +1 : -1; } } $a[] = new TestObj("c"); $a[] = new TestObj("b"); $a[] = new TestObj("d"); uasort($a, array ("TestObj", "cmp_obj")); foreach ($a as $item) { print $item->name."\n"; } ?></PRE ></TD ></TR ></TABLE ><P >
Dieses Beispiel wⁿrde folgendes anzeigen: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >b c d</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.uasort" ><B CLASS="function" >uasort()</B ></A >, <A HREF="#function.uksort" ><B CLASS="function" >uksort()</B ></A >, <A HREF="#function.sort" ><B CLASS="function" >sort()</B ></A >, <A HREF="#function.asort" ><B CLASS="function" >asort()</B ></A >, <A HREF="#function.arsort" ><B CLASS="function" >arsort()</B ></A >,<A HREF="#function.ksort" ><B CLASS="function" >ksort()</B ></A >, <A HREF="#function.natsort" ><B CLASS="function" >natsort()</B ></A > und <A HREF="#function.rsort" ><B CLASS="function" >rsort()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="reference" ><A NAME="ref.aspell" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >III. Aspell Funktionen [veraltet]</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN10219" ></A ><DIV CLASS="section" ><H2 CLASS="section" ><A NAME="aspell.intro" >Einfⁿhrung</A ></H2 ><P >
Die <B CLASS="function" >aspell()</B > Funktionen erlauben es ein Wort auf korrekte Rechtschreibung zu prⁿfen und Alternativen anzubieten. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Diese Erweiterung wurde aus <VAR CLASS="literal" >PHP</VAR > entfernt und steht seit der Version 4.3.0 nicht mehr zur Verfⁿgung. Falls Sie eine Rechtschreibprⁿfung mit PHP benutzen wollen, verwenden Sie statt dessen die <A HREF="#ref.pspell" >Pspell Funktionen</A >. Diese nutzen die Pspell Bibliothek und arbeiten mit neueren Versionen von Aspell zusammen. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="aspell.requirements" >Anforderungen</A ></H2 ><P >
Aspell arbeitet nur mit ziemlich alten (bis .27.* oder so) Versionen der Aspell Bibliothek zusammen. Weder dieses Modul noch jene Versionen der Aspell Bibliothek werden nicht mehr lΣnger unterstⁿtzt.Sie ben÷tigen die aspell-Bibliothek von: <A HREF="http://aspell.sourceforge.net/" TARGET="_top" >http://aspell.sourceforge.net/</A >. </P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="aspell.installation" >Installation</A ></H2 ><P >
Diese Funktionen stehen nur zur Verfⁿgung, wenn Sie <VAR CLASS="literal" >PHP</VAR > mit der Option <VAR CLASS="option" >--with-aspell=[DIR]</VAR > konfiguriert haben. </P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="aspell.seealso" >Siehe auch</A ></H2 ><P >
Siehe auch <A HREF="#ref.pspell" >pspell</A >. </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT ><A HREF="#function.aspell-check-raw" >aspell_check-raw</A > -- ▄berprⁿft ein Wort genauso wie es ⁿbergeben wird [veraltet] </DT ><DT ><A HREF="#function.aspell-check" >aspell_check</A > -- ▄berprⁿft ein Wort [veraltet]</DT ><DT ><A HREF="#function.aspell-new" >aspell_new</A > -- LΣdt ein neues W÷rterbuch [veraltet]</DT ><DT ><A HREF="#function.aspell-suggest" >aspell_suggest</A > -- SchlΣgt m÷gliche Schreibweisen vor [veraltet]</DT ></DL ></DIV ></DIV ><H1 ><A NAME="function.aspell-check-raw" ></A >aspell_check-raw</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10242" ></A ><P > (PHP 3>= 3.0.7, PHP 4 <= 4.2.3)</P >aspell_check-raw -- ▄berprⁿft ein Wort genauso wie es ⁿbergeben wird [veraltet] </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10245" ></A ><H2 >Beschreibung</H2 >boolean <B CLASS="methodname" >aspell_check_raw</B > ( int W÷rterbuchkennung, string Wort)<BR ></BR ><P >
<A HREF="#function.aspell-check-raw" ><B CLASS="function" >aspell_check_raw()</B ></A > ⁿberprⁿft die Rechtschreibung eines Wortes ohne die Gro▀/Kleinschreibung zu variieren oder das Wort in irgendeiner Weise zu kⁿrzen. Liefert <TT CLASS="constant" ><B >TRUE</B ></TT >, wenn das Wort korrekt ist und sonst <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10261" ></A ><P ><B >Beispiel 1. <A HREF="#function.aspell-check-raw" ><B CLASS="function" >aspell_check_raw()</B ></A ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $aspell_link = aspell_new("english"); if (aspell_check_raw($aspell_link, "test")) { echo "Gⁿltige Schreibweise"; } else { echo "Bedaure, falsche Schreibweise"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.aspell-check" ></A >aspell_check</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10266" ></A ><P > (PHP 3>= 3.0.7, PHP 4 <= 4.2.3)</P >aspell_check -- ▄berprⁿft ein Wort [veraltet]</DIV ><DIV CLASS="refsect1" ><A NAME="AEN10269" ></A ><H2 >Beschreibung</H2 >boolean <B CLASS="methodname" >aspell_check</B > ( int W÷rterbuchkennung, string Wort)<BR ></BR ><P >
<B CLASS="function" >aspell_check()</B > ⁿberprⁿft die Rechtschreibung eines Wortes und liefert <TT CLASS="constant" ><B >TRUE</B ></TT >, wenn das Wort korrekt ist, ansonsten <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10285" ></A ><P ><B >Beispiel 1. aspell_check</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $aspell_link = aspell_new("english"); if (aspell_check($aspell_link, "testt")) { echo "Gⁿltige Schreibweise"; } else { echo "Bedaure, falsche Schreibweise"; } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.aspell-new" ></A >aspell_new</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10289" ></A ><P > (PHP 3>= 3.0.7, PHP 4 <= 4.2.3)</P >aspell_new -- LΣdt ein neues W÷rterbuch [veraltet]</DIV ><DIV CLASS="refsect1" ><A NAME="AEN10292" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >aspell_new</B > ( string master, string personal)<BR ></BR ><P >
<B CLASS="function" >aspell_new()</B > ÷ffnet ein neues W÷rterbuch und liefert eine W÷rterbuchkennung zurⁿck, die von anderen aspell-Funktionen gebraucht wird. Liefert <TT CLASS="constant" ><B >FALSE</B ></TT > bei einem Fehler. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10307" ></A ><P ><B >Beispiel 1. aspell_new</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $aspell_link = aspell_new("english"); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.aspell-suggest" ></A >aspell_suggest</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10311" ></A ><P > (PHP 3>= 3.0.7, PHP 4 <= 4.2.3)</P >aspell_suggest -- SchlΣgt m÷gliche Schreibweisen vor [veraltet]</DIV ><DIV CLASS="refsect1" ><A NAME="AEN10314" ></A ><H2 >Beschreibung</H2 >array <B CLASS="methodname" >aspell_suggest</B > ( int W÷rterbuchkennung, string Wort)<BR ></BR ><P >
<B CLASS="function" >aspell_suggest()</B > liefert ein Array mit m÷glichen Schreibweisen eines ⁿbergebenen Wortes. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10328" ></A ><P ><B >Beispiel 1. <B CLASS="function" >aspell_suggest()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $aspell_link = aspell_new("english"); if (!aspell_check($aspell_link, "test")) { $suggestions = aspell_suggest($aspell_link, "test"); foreach ($suggestions as $suggestion) { echo "M÷gliche Schreibweisen: $suggestion<br />\n"; } } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ></DIV ><DIV CLASS="reference" ><A NAME="ref.bc" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >IV. Mathematische Funktionen mit beliebiger Genauigkeit</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN10335" ></A ><DIV CLASS="section" ><H2 CLASS="section" ><A NAME="bc.intro" >Einfⁿhrung</A ></H2 ><P >
Fⁿr beliebig genaue mathematische Operationen bietet PHP den Binary Calculator. Unterstⁿtzt werden Zahlen beliebiger LΣnge und Genauigkeit, die als String vorliegen. </P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="bc.requirements" >Anforderungen</A ></H2 ><P >
Seit PHP 4.0.4 ist libbcmath in PHP enthalten. Sie brauchen keine externen Bibliotheken fⁿr diese Erweiterung. </P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="bc.installation" >Installation</A ></H2 ><P >
In PHP 4 sind diese Funktionen nur verfⁿgbar, wenn <VAR CLASS="literal" >PHP</VAR > mit <VAR CLASS="option" >--enable-bcmath</VAR > konfiguriert wurde. In PHP 3 sind diese Funktionen nur verfⁿgbar, wenn <VAR CLASS="literal" >PHP</VAR > nicht mit <VAR CLASS="option" >--disable-bcmath</VAR > konfiguriert wurde. </P ><P >Die Windowsversion von PHP enthΣlt diese Erweiterung. Um diese Funktionen zu verwenden, mⁿssen Sie keine zusΣtzlichen Erweiterungen aktivieren.</P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="bc.configuration" >Laufzeit Konfiguration</A ></H2 ><P > Das Verhalten dieser Funktionen wird durch Einstellungen in der <TT CLASS="filename" >php.ini</TT > beeinflusst.</P ><P >
<DIV CLASS="table" ><A NAME="AEN10355" ></A ><P ><B >Tabelle 1. BC math Konfigurations-Optionen</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Name</TH ><TH >Standard</TH ><TH >VerΣnderbar</TH ></TR ></THEAD ><TBODY ><TR ><TD >bcmath.scale</TD ><TD >0</TD ><TD >PHP_INI_ALL</TD ></TR ></TBODY ></TABLE ></DIV > Weitere Details und die Definition der PHP_INI_* Konstanten sind unter <A HREF="#function.ini-set" ><B CLASS="function" >ini_set()</B ></A > zu finden. </P ><P >Hier eine kurze ErklΣrung der Konfigurationsoptionen:</P ><P >
<P ></P ><DIV CLASS="variablelist" ><DL ><DT ><A NAME="ini.bcmath.scale" ></A ><VAR CLASS="parameter" >bcmath.scale</VAR > <A HREF="#language.types.integer" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P >
Anzahl der Dezimalstellen fⁿr alle bcmath Funktionen. Siehe auch <A HREF="#function.bcscale" ><B CLASS="function" >bcscale()</B ></A >. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="bc.resources" >Resource Typen</A ></H2 ><P >Diese Erweiterung definiert keine Resource-Typen.</P ></DIV ><DIV CLASS="section" ><HR><H2 CLASS="section" ><A NAME="bc.constants" >Vordefinierte Konstanten</A ></H2 ><P >Diese Erweiterung definiert keine Konstanten.</P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Inhaltsverzeichnis</B ></DT ><DT ><A HREF="#function.bcadd" >bcadd</A > -- Addition zweier Zahlen beliebiger Genauigkeit.</DT ><DT ><A HREF="#function.bccomp" >bccomp</A > -- Vergleich zweier Zahlen beliebiger Genauigkeit. </DT ><DT ><A HREF="#function.bcdiv" >bcdiv</A > -- Division zweier Zahlen beliebiger Genauigkeit.</DT ><DT ><A HREF="#function.bcmod" >bcmod</A > -- Modulo zweier Zahlen mit beliebiger Genauigkeit. </DT ><DT ><A HREF="#function.bcmul" >bcmul</A > -- Multiplikation zweier Zahlen beliebiger Genauigkeit. </DT ><DT ><A HREF="#function.bcpow" >bcpow</A > -- Potenz mit beliebiger Genauigkeit. </DT ><DT ><A HREF="#function.bcpowmod" >bcpowmod</A > -- Raise an arbitrary precision number to another, reduced by a specified modulus. </DT ><DT ><A HREF="#function.bcscale" >bcscale</A > -- Setzt die Genauigkeit aller BC math-Funktionen. </DT ><DT ><A HREF="#function.bcsqrt" >bcsqrt</A > -- Quadratwurzel mit beliebiger Genauigkeit. </DT ><DT ><A HREF="#function.bcsub" >bcsub</A > -- Subtrahiert zwei Zahlen mit beliebiger Genauigkeit. </DT ></DL ></DIV ></DIV ><H1 ><A NAME="function.bcadd" ></A >bcadd</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10386" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >bcadd -- Addition zweier Zahlen beliebiger Genauigkeit.</DIV ><DIV CLASS="refsect1" ><A NAME="AEN10389" ></A ><H2 >Beschreibung</H2 >string <B CLASS="methodname" >bcadd</B > ( string linker Operand, string rechter Operand [, int Genauigkeit])<BR ></BR ><P >
Addiert den <VAR CLASS="parameter" >linken Operanden</VAR > zu dem <VAR CLASS="parameter" >rechten Operanden</VAR > und liefert die Summe als Zeichenkette. Der optionale Parameter <VAR CLASS="parameter" >Genauigkeit</VAR > kann angegeben werden, um die Anzahl der Nachkommastellen im Ergebnis zu bestimmen. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10408" ></A ><P ><B >Beispiel 1. <B CLASS="function" >bcadd()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = 1.234; $b = 5; echo bcadd($a, $b); // 6 echo bcadd($a, $b, 4); // 6.2340 ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.bcsub" ><B CLASS="function" >bcsub()</B ></A >. </P ></DIV ><H1 ><A NAME="function.bccomp" ></A >bccomp</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10415" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >bccomp -- Vergleich zweier Zahlen beliebiger Genauigkeit. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10418" ></A ><H2 >Beschreibung</H2 >int <B CLASS="methodname" >bccomp</B > ( string linker Operand, string rechter Operand [, int Genauigkeit])<BR ></BR ><P >
Vergleicht den <VAR CLASS="parameter" >linken Operanden</VAR > mit dem <VAR CLASS="parameter" >rechten Operanden</VAR > und liefert das Ergebnis als Integer-Wert. Der optionale Parameter <VAR CLASS="parameter" >Genauigkeit</VAR > bestimmt die Anzahl der Nachkommastellen, die fⁿr den Vergleich ausgewertet werden. Der Rⁿckgabewert ist 0, wenn die beiden Operanden gleich sind. Wenn der <VAR CLASS="parameter" >linke Operand</VAR > gr÷▀er ist als der <VAR CLASS="parameter" >rechte Operand</VAR >, wird +1 zurⁿckgeliefert; wenn der <VAR CLASS="parameter" >linke Operand</VAR > kleiner ist als der <VAR CLASS="parameter" >rechte Operand</VAR > wird -1 zurⁿckgeliefert. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10441" ></A ><P ><B >Beispiel 1. <B CLASS="function" >bccomp()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo bccomp('1', '2') . "\n"; echo bccomp('1.00001', '1', 3) . "\n"; echo bccomp('1.00001', '1', 5); ?></PRE ></TD ></TR ></TABLE ><P >
Das obige Beispiel gibt folgendes aus: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >-1 0 1</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.bcdiv" ></A >bcdiv</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10448" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >bcdiv -- Division zweier Zahlen beliebiger Genauigkeit.</DIV ><DIV CLASS="refsect1" ><A NAME="AEN10451" ></A ><H2 >Beschreibung</H2 >string <B CLASS="methodname" >bcdiv</B > ( string linker Operand, string rechter Operand [, int Genauigkeit])<BR ></BR ><P >
Dividiert den <VAR CLASS="parameter" >linken Operanden</VAR > durch den <VAR CLASS="parameter" >rechten Operanden</VAR >. Der optionale Parameter <VAR CLASS="parameter" >Genauigkeit</VAR > legt die Anzahl der Nachkommastellen im Ergebnis fest. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10470" ></A ><P ><B >Beispiel 1. <B CLASS="function" >bcdiv()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo bcdiv(105, 6.55957, 3); // 16.007 ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.bcmul" ><B CLASS="function" >bcmul()</B ></A >. </P ></DIV ><H1 ><A NAME="function.bcmod" ></A >bcmod</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10477" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >bcmod -- Modulo zweier Zahlen mit beliebiger Genauigkeit. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10480" ></A ><H2 >Beschreibung</H2 >string <B CLASS="methodname" >bcmod</B > ( string linker Operand, string Modulus)<BR ></BR ><P >
Liefert den <VAR CLASS="parameter" >linken Operanden</VAR > modulo <VAR CLASS="parameter" >Modulus</VAR >. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10495" ></A ><P ><B >Beispiel 1. <B CLASS="function" >bcmod()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo bcmod(4, 2) . "\n"; echo bcmod(2, 4); ?></PRE ></TD ></TR ></TABLE ><P >
Das obige Beispiel gibt folgendes aus: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >0 2</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.bcdiv" ><B CLASS="function" >bcdiv()</B ></A >. </P ></DIV ><H1 ><A NAME="function.bcmul" ></A >bcmul</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10504" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >bcmul -- Multiplikation zweier Zahlen beliebiger Genauigkeit. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10507" ></A ><H2 >Beschreibung</H2 >string <B CLASS="methodname" >bcmul</B > ( string linker Operand, string rechter Operand [, int Genauigkeit])<BR ></BR ><P >
Multipliziert den <VAR CLASS="parameter" >linken Operanden</VAR > mit dem <VAR CLASS="parameter" >rechten Operanden</VAR >. Der optionale Parameter <VAR CLASS="parameter" >Genauigkeit</VAR > legt die Anzahl der Nachkommastellen im Ergebnis fest. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10526" ></A ><P ><B >Beispiel 1. <B CLASS="function" >bcmul()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo bcmul(1.34747474747, 35, 3) . "\n"; echo bcmul(2, 4); ?></PRE ></TD ></TR ></TABLE ><P >
Das obige Beispiel gibt folgendes aus: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >47.162 8</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.bcdiv" ><B CLASS="function" >bcdiv()</B ></A >. </P ></DIV ><H1 ><A NAME="function.bcpow" ></A >bcpow</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10535" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >bcpow -- Potenz mit beliebiger Genauigkeit. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10538" ></A ><H2 >Beschreibung</H2 >string <B CLASS="methodname" >bcpow</B > ( string x, string y [, int Genauigkeit])<BR ></BR ><P >
Potenziert <VAR CLASS="parameter" >x</VAR > mit <VAR CLASS="parameter" >y</VAR >. Der optionale Parameter <VAR CLASS="parameter" >Genauigkeit</VAR > legt die Anzahl der Nachkommastellen im Ergebnis fest. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10557" ></A ><P ><B >Beispiel 1. <B CLASS="function" >bcpow()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo bcpow(4.2, 3, 2); // 74.08 ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >
Siehe auch <A HREF="#function.bcpowmod" ><B CLASS="function" >bcpowmod()</B ></A > und <A HREF="#function.bcsqrt" ><B CLASS="function" >bcsqrt()</B ></A >. </P ></DIV ><H1 ><A NAME="function.bcpowmod" ></A >bcpowmod</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10565" ></A ><P > (PHP 5)</P >bcpowmod -- Raise an arbitrary precision number to another, reduced by a specified modulus. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10568" ></A ><H2 >Beschreibung</H2 >string <B CLASS="methodname" >bcpowmod</B > ( string x, string y, string modulus [, int scale])<BR ></BR ><P >
Benutzen Sie diese schnelle Exponentialmethode um <VAR CLASS="parameter" >x</VAR > mit <VAR CLASS="parameter" >y</VAR > unter Berⁿcksichtigung des Modulo <VAR CLASS="parameter" >modulus</VAR > zu potenzieren. Der optionale Parameter <VAR CLASS="parameter" >scale</VAR > legt die Anzahl der Nachkommastellen im Ergebnis fest. </P ><P >
Die folgenden zwei Anweisungen sind funktional identisch. Die Variante mit <B CLASS="function" >bcpowmod()</B > ist schneller und akzeptiert gr÷▀ere Parameter. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN10592" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $a = bcpowmod($x, $y, $mod); $b = bcmod(bcpow($x, $y), $mod); // $a und $b sind genau gleich. ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P >
<DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Anmerkung: </B > Die Verwendung von nicht-natⁿrlichen Zahlen kann zu unerwarteten Ergebnissen fⁿhren, weil diese Funktion die Modulodivision verwendet. Eine natⁿrliche Zahl ist jede ganzzahlige positive Zahl die ungleich Null ist. </P ></BLOCKQUOTE ></DIV > </P ><P >
Siehe auch <A HREF="#function.bcpow" ><B CLASS="function" >bcpow()</B ></A > und <A HREF="#function.bcmod" ><B CLASS="function" >bcmod()</B ></A >. </P ></DIV ><H1 ><A NAME="function.bcscale" ></A >bcscale</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10601" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >bcscale -- Setzt die Genauigkeit aller BC math-Funktionen. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN10604" ></A ><H2 >Beschreibung</H2 >bool <B CLASS="methodname" >bcscale</B > ( int Genauigkeit)<BR ></BR ><P >
Diese Funktionen legt die Standard-Genauigkeit aller in der Folge aufgerufenden BC math-Funktionen fest, die nicht explizit die Genauigkeit durch den optionalen Parameter setzen. Gibt bei Erfolg <TT CLASS="constant" ><B >TRUE</B ></TT > zurⁿck, im Fehlerfall <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P >
<TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN10616" ></A ><P ><B >Beispiel 1. <B CLASS="function" >bcscale()</B > Beispiel</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // Genauigkeit festlegen : 3 bcscale(3); echo bcdiv(105, 6.55957); // 16.007 // Das gleiche Ergebnis ohne bcscale() echo bcdiv(105, 6.55957, 3); // 16.007 ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><H1 ><A NAME="function.bcsqrt" ></A >bcsqrt</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN10621" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >bcsqrt --