home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phpmanual / phpmanual.exe / php_manual_en.html.htm < prev    next >
Encoding:
Extensible Markup Language  |  2005-01-13  |  9.8 MB  |  670,325 lines

Text Truncated. Only the first 1MB is shown below. Download the file for the complete contents.
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML
  3. ><HEAD
  4. ><TITLE
  5. >PHP Manual</TITLE
  6. ><META
  7. NAME="GENERATOR"
  8. CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><META
  9. HTTP-EQUIV="Content-type"
  10. CONTENT="text/html; charset=ISO-8859-1"></HEAD
  11. ><BODY
  12. CLASS="book"
  13. BGCOLOR="#FFFFFF"
  14. TEXT="#000000"
  15. LINK="#0000FF"
  16. VLINK="#840084"
  17. ALINK="#0000FF"
  18. ><DIV
  19. CLASS="BOOK"
  20. ><A
  21. NAME="manual"
  22. ></A
  23. ><DIV
  24. CLASS="TITLEPAGE"
  25. ><H1
  26. CLASS="title"
  27. ><A
  28. NAME="bookinfo"
  29. >PHP Manual</A
  30. ></H1
  31. ><DIV
  32. CLASS="author"
  33. >Mehdi Achour</DIV
  34. ><DIV
  35. CLASS="author"
  36. >Friedhelm Betz</DIV
  37. ><DIV
  38. CLASS="author"
  39. >Antony Dovgal</DIV
  40. ><DIV
  41. CLASS="author"
  42. >Nuno Lopes</DIV
  43. ><DIV
  44. CLASS="author"
  45. >Philip Olson</DIV
  46. ><DIV
  47. CLASS="author"
  48. >Georg Richter</DIV
  49. ><DIV
  50. CLASS="author"
  51. >Damien Seguy</DIV
  52. ><DIV
  53. CLASS="author"
  54. >Jakub Vrana</DIV
  55. ><SPAN
  56. CLASS="collab"
  57. ><SPAN
  58. CLASS="collabname"
  59. >      <A
  60. HREF="#contributors"
  61. >And several others</A
  62. >
  63.     </SPAN
  64. ><BR></SPAN
  65. ><H2
  66. CLASS="EDITEDBY"
  67. >Edited by</H2
  68. ><DIV
  69. CLASS="editor"
  70. >Gabor Hojtsy</DIV
  71. ><P
  72. CLASS="pubdate"
  73. >03-01-2005<BR></P
  74. ><P
  75. CLASS="copyright"
  76. >Copyright © 1997-2005 the PHP Documentation Group</P
  77. ><DIV
  78. CLASS="legalnotice"
  79. ><A
  80. NAME="copyright"
  81. ></A
  82. ><P
  83. ><B
  84. >Copyright</B
  85. ></P
  86. ><P
  87. >     Copyright ┬⌐ 1997 - 2005 by the PHP Documentation Group.
  88.     This material may be distributed only subject to the terms and
  89.     conditions set forth in the Open Publication License, v1.0 or
  90.     later. A copy of the <A
  91. HREF="#opl.license"
  92. >Open Publication
  93.     License</A
  94. > is distributed with this manual, the latest version
  95.     is presently available at <A
  96. HREF="http://www.opencontent.org/openpub/"
  97. TARGET="_top"
  98. >http://www.opencontent.org/openpub/</A
  99. >.
  100.    </P
  101. ><P
  102. >     Distribution of substantively modified versions of this document
  103.     is prohibited without the explicit permission of the copyright
  104.     holder.
  105.    </P
  106. ><P
  107. >     Distribution of the work or derivative of the work in any
  108.     standard (paper) book form is prohibited unless prior permission
  109.     is obtained from the copyright holder.
  110.    </P
  111. ><P
  112. >     In case you are interested in redistribution or republishing of this document
  113.     in whole or in part, either modified or unmodified, and you have questions,
  114.     please contact the copyright holders at
  115.     <A
  116. HREF="mailto:doc-license@lists.php.net"
  117. TARGET="_top"
  118. >doc-license@lists.php.net</A
  119. >.
  120.     Note that this address is mapped to a publicly archived mailing list.
  121.    </P
  122. ><P
  123. >     The 'Extending PHP 4.0' section of this manual is copyright ┬⌐ 2000 by
  124.     Zend Technologies, Ltd. This material may be distributed only subject to
  125.     the terms and conditions set forth in the Open Publication License, v1.0
  126.     or later (the latest version is presently available at
  127.     <A
  128. HREF="http://www.opencontent.org/openpub/"
  129. TARGET="_top"
  130. >http://www.opencontent.org/openpub/</A
  131. >).
  132.    </P
  133. ></DIV
  134. ><HR></DIV
  135. ><DIV
  136. CLASS="TOC"
  137. ><DL
  138. ><DT
  139. ><B
  140. >Table of Contents</B
  141. ></DT
  142. ><DT
  143. ><A
  144. HREF="#preface"
  145. >Preface</A
  146. ></DT
  147. ><DD
  148. ><DL
  149. ><DT
  150. ><A
  151. HREF="#contributors"
  152. >Authors and Contributors</A
  153. ></DT
  154. ></DL
  155. ></DD
  156. ><DT
  157. >I. <A
  158. HREF="#getting-started"
  159. >Getting Started</A
  160. ></DT
  161. ><DD
  162. ><DL
  163. ><DT
  164. >1. <A
  165. HREF="#introduction"
  166. >Introduction</A
  167. ></DT
  168. ><DT
  169. >2. <A
  170. HREF="#tutorial"
  171. >A simple tutorial</A
  172. ></DT
  173. ></DL
  174. ></DD
  175. ><DT
  176. >II. <A
  177. HREF="#install"
  178. >Installation and Configuration</A
  179. ></DT
  180. ><DD
  181. ><DL
  182. ><DT
  183. >3. <A
  184. HREF="#install.general"
  185. >General Installation Considerations</A
  186. ></DT
  187. ><DT
  188. >4. <A
  189. HREF="#install.unix"
  190. >Installation on Unix systems</A
  191. ></DT
  192. ><DT
  193. >5. <A
  194. HREF="#install.macosx"
  195. >Installation on Mac OS X</A
  196. ></DT
  197. ><DT
  198. >6. <A
  199. HREF="#install.windows"
  200. >Installation on Windows systems</A
  201. ></DT
  202. ><DT
  203. >7. <A
  204. HREF="#install.pecl"
  205. >Installation of PECL extensions</A
  206. ></DT
  207. ><DT
  208. >8. <A
  209. HREF="#install.problems"
  210. >Problems?</A
  211. ></DT
  212. ><DT
  213. >9. <A
  214. HREF="#configuration"
  215. >Runtime Configuration</A
  216. ></DT
  217. ></DL
  218. ></DD
  219. ><DT
  220. >III. <A
  221. HREF="#langref"
  222. >Language Reference</A
  223. ></DT
  224. ><DD
  225. ><DL
  226. ><DT
  227. >10. <A
  228. HREF="#language.basic-syntax"
  229. >Basic syntax</A
  230. ></DT
  231. ><DT
  232. >11. <A
  233. HREF="#language.types"
  234. >Types</A
  235. ></DT
  236. ><DT
  237. >12. <A
  238. HREF="#language.variables"
  239. >Variables</A
  240. ></DT
  241. ><DT
  242. >13. <A
  243. HREF="#language.constants"
  244. >Constants</A
  245. ></DT
  246. ><DT
  247. >14. <A
  248. HREF="#language.expressions"
  249. >Expressions</A
  250. ></DT
  251. ><DT
  252. >15. <A
  253. HREF="#language.operators"
  254. >Operators</A
  255. ></DT
  256. ><DT
  257. >16. <A
  258. HREF="#language.control-structures"
  259. >Control Structures</A
  260. ></DT
  261. ><DT
  262. >17. <A
  263. HREF="#language.functions"
  264. >Functions</A
  265. ></DT
  266. ><DT
  267. >18. <A
  268. HREF="#language.oop"
  269. >Classes and Objects (PHP 4)</A
  270. ></DT
  271. ><DT
  272. >19. <A
  273. HREF="#language.oop5"
  274. >Classes and Objects (PHP 5)</A
  275. ></DT
  276. ><DT
  277. >20. <A
  278. HREF="#language.exceptions"
  279. >Exceptions</A
  280. ></DT
  281. ><DT
  282. >21. <A
  283. HREF="#language.references"
  284. >References Explained</A
  285. ></DT
  286. ></DL
  287. ></DD
  288. ><DT
  289. >IV. <A
  290. HREF="#security"
  291. >Security</A
  292. ></DT
  293. ><DD
  294. ><DL
  295. ><DT
  296. >22. <A
  297. HREF="#security.intro"
  298. >Introduction</A
  299. ></DT
  300. ><DT
  301. >23. <A
  302. HREF="#security.general"
  303. >General considerations</A
  304. ></DT
  305. ><DT
  306. >24. <A
  307. HREF="#security.cgi-bin"
  308. >Installed as CGI binary</A
  309. ></DT
  310. ><DT
  311. >25. <A
  312. HREF="#security.apache"
  313. >Installed as an Apache module</A
  314. ></DT
  315. ><DT
  316. >26. <A
  317. HREF="#security.filesystem"
  318. >Filesystem Security</A
  319. ></DT
  320. ><DT
  321. >27. <A
  322. HREF="#security.database"
  323. >Database Security</A
  324. ></DT
  325. ><DT
  326. >28. <A
  327. HREF="#security.errors"
  328. >Error Reporting</A
  329. ></DT
  330. ><DT
  331. >29. <A
  332. HREF="#security.globals"
  333. >Using Register Globals</A
  334. ></DT
  335. ><DT
  336. >30. <A
  337. HREF="#security.variables"
  338. >User Submitted Data</A
  339. ></DT
  340. ><DT
  341. >31. <A
  342. HREF="#security.magicquotes"
  343. >Magic Quotes</A
  344. ></DT
  345. ><DT
  346. >32. <A
  347. HREF="#security.hiding"
  348. >Hiding PHP</A
  349. ></DT
  350. ><DT
  351. >33. <A
  352. HREF="#security.current"
  353. >Keeping Current</A
  354. ></DT
  355. ></DL
  356. ></DD
  357. ><DT
  358. >V. <A
  359. HREF="#features"
  360. >Features</A
  361. ></DT
  362. ><DD
  363. ><DL
  364. ><DT
  365. >34. <A
  366. HREF="#features.http-auth"
  367. >HTTP authentication with PHP</A
  368. ></DT
  369. ><DT
  370. >35. <A
  371. HREF="#features.cookies"
  372. >Cookies</A
  373. ></DT
  374. ><DT
  375. >36. <A
  376. HREF="#features.sessions"
  377. >Sessions</A
  378. ></DT
  379. ><DT
  380. >37. <A
  381. HREF="#features.xforms"
  382. >Dealing with XForms</A
  383. ></DT
  384. ><DT
  385. >38. <A
  386. HREF="#features.file-upload"
  387. >Handling file uploads</A
  388. ></DT
  389. ><DT
  390. >39. <A
  391. HREF="#features.remote-files"
  392. >Using remote files</A
  393. ></DT
  394. ><DT
  395. >40. <A
  396. HREF="#features.connection-handling"
  397. >Connection handling</A
  398. ></DT
  399. ><DT
  400. >41. <A
  401. HREF="#features.persistent-connections"
  402. >Persistent Database Connections</A
  403. ></DT
  404. ><DT
  405. >42. <A
  406. HREF="#features.safe-mode"
  407. >Safe Mode</A
  408. ></DT
  409. ><DT
  410. >43. <A
  411. HREF="#features.commandline"
  412. >Using PHP from the command line</A
  413. ></DT
  414. ></DL
  415. ></DD
  416. ><DT
  417. >VI. <A
  418. HREF="#funcref"
  419. >Function Reference</A
  420. ></DT
  421. ><DD
  422. ><DL
  423. ><DT
  424. >I. <A
  425. HREF="#ref.apache"
  426. >Apache-specific Functions</A
  427. ></DT
  428. ><DT
  429. >II. <A
  430. HREF="#ref.apd"
  431. >Advanced PHP debugger</A
  432. ></DT
  433. ><DT
  434. >III. <A
  435. HREF="#ref.array"
  436. >Array Functions</A
  437. ></DT
  438. ><DT
  439. >IV. <A
  440. HREF="#ref.aspell"
  441. >Aspell functions [deprecated]</A
  442. ></DT
  443. ><DT
  444. >V. <A
  445. HREF="#ref.bc"
  446. >BCMath Arbitrary Precision Mathematics Functions</A
  447. ></DT
  448. ><DT
  449. >VI. <A
  450. HREF="#ref.bcompiler"
  451. >PHP bytecode Compiler</A
  452. ></DT
  453. ><DT
  454. >VII. <A
  455. HREF="#ref.bzip2"
  456. >Bzip2 Compression Functions</A
  457. ></DT
  458. ><DT
  459. >VIII. <A
  460. HREF="#ref.calendar"
  461. >Calendar Functions</A
  462. ></DT
  463. ><DT
  464. >IX. <A
  465. HREF="#ref.ccvs"
  466. >CCVS API Functions [deprecated]</A
  467. ></DT
  468. ><DT
  469. >X. <A
  470. HREF="#ref.com"
  471. >COM and .Net (Windows)</A
  472. ></DT
  473. ><DT
  474. >XI. <A
  475. HREF="#ref.classkit"
  476. >Classkit Functions</A
  477. ></DT
  478. ><DT
  479. >XII. <A
  480. HREF="#ref.classobj"
  481. >Class/Object Functions</A
  482. ></DT
  483. ><DT
  484. >XIII. <A
  485. HREF="#ref.cpdf"
  486. >ClibPDF Functions</A
  487. ></DT
  488. ><DT
  489. >XIV. <A
  490. HREF="#ref.crack"
  491. >Crack Functions</A
  492. ></DT
  493. ><DT
  494. >XV. <A
  495. HREF="#ref.curl"
  496. >CURL, Client URL Library Functions</A
  497. ></DT
  498. ><DT
  499. >XVI. <A
  500. HREF="#ref.cybercash"
  501. >Cybercash Payment Functions</A
  502. ></DT
  503. ><DT
  504. >XVII. <A
  505. HREF="#ref.cyrus"
  506. >Cyrus IMAP administration Functions</A
  507. ></DT
  508. ><DT
  509. >XVIII. <A
  510. HREF="#ref.ctype"
  511. >Character Type Functions</A
  512. ></DT
  513. ><DT
  514. >XIX. <A
  515. HREF="#ref.dba"
  516. >Database (dbm-style) Abstraction Layer Functions</A
  517. ></DT
  518. ><DT
  519. >XX. <A
  520. HREF="#ref.datetime"
  521. >Date and Time Functions</A
  522. ></DT
  523. ><DT
  524. >XXI. <A
  525. HREF="#ref.dbase"
  526. >dBase Functions</A
  527. ></DT
  528. ><DT
  529. >XXII. <A
  530. HREF="#ref.dbm"
  531. >DBM Functions [deprecated]</A
  532. ></DT
  533. ><DT
  534. >XXIII. <A
  535. HREF="#ref.dbx"
  536. >dbx Functions</A
  537. ></DT
  538. ><DT
  539. >XXIV. <A
  540. HREF="#ref.dbplus"
  541. >DB++ Functions</A
  542. ></DT
  543. ><DT
  544. >XXV. <A
  545. HREF="#ref.dio"
  546. >Direct IO Functions</A
  547. ></DT
  548. ><DT
  549. >XXVI. <A
  550. HREF="#ref.dir"
  551. >Directory Functions</A
  552. ></DT
  553. ><DT
  554. >XXVII. <A
  555. HREF="#ref.dom"
  556. >DOM Functions</A
  557. ></DT
  558. ><DT
  559. >XXVIII. <A
  560. HREF="#ref.domxml"
  561. >DOM XML Functions</A
  562. ></DT
  563. ><DT
  564. >XXIX. <A
  565. HREF="#ref.dotnet"
  566. >.NET Functions</A
  567. ></DT
  568. ><DT
  569. >XXX. <A
  570. HREF="#ref.errorfunc"
  571. >Error Handling and Logging Functions</A
  572. ></DT
  573. ><DT
  574. >XXXI. <A
  575. HREF="#ref.exif"
  576. >Exif Functions</A
  577. ></DT
  578. ><DT
  579. >XXXII. <A
  580. HREF="#ref.fam"
  581. >File Alteration Monitor Functions</A
  582. ></DT
  583. ><DT
  584. >XXXIII. <A
  585. HREF="#ref.fbsql"
  586. >FrontBase Functions</A
  587. ></DT
  588. ><DT
  589. >XXXIV. <A
  590. HREF="#ref.filepro"
  591. >filePro Functions</A
  592. ></DT
  593. ><DT
  594. >XXXV. <A
  595. HREF="#ref.filesystem"
  596. >Filesystem Functions</A
  597. ></DT
  598. ><DT
  599. >XXXVI. <A
  600. HREF="#ref.fdf"
  601. >Forms Data Format Functions</A
  602. ></DT
  603. ><DT
  604. >XXXVII. <A
  605. HREF="#ref.fribidi"
  606. >FriBiDi Functions</A
  607. ></DT
  608. ><DT
  609. >XXXVIII. <A
  610. HREF="#ref.ftp"
  611. >FTP Functions</A
  612. ></DT
  613. ><DT
  614. >XXXIX. <A
  615. HREF="#ref.funchand"
  616. >Function Handling Functions</A
  617. ></DT
  618. ><DT
  619. >XL. <A
  620. HREF="#ref.gettext"
  621. >Gettext</A
  622. ></DT
  623. ><DT
  624. >XLI. <A
  625. HREF="#ref.gmp"
  626. >GMP Functions</A
  627. ></DT
  628. ><DT
  629. >XLII. <A
  630. HREF="#ref.http"
  631. >HTTP Functions</A
  632. ></DT
  633. ><DT
  634. >XLIII. <A
  635. HREF="#ref.hw"
  636. >Hyperwave Functions</A
  637. ></DT
  638. ><DT
  639. >XLIV. <A
  640. HREF="#ref.hwapi"
  641. >Hyperwave API Functions</A
  642. ></DT
  643. ><DT
  644. >XLV. <A
  645. HREF="#ref.iconv"
  646. >iconv Functions</A
  647. ></DT
  648. ><DT
  649. >XLVI. <A
  650. HREF="#ref.image"
  651. >Image Functions</A
  652. ></DT
  653. ><DT
  654. >XLVII. <A
  655. HREF="#ref.imap"
  656. >IMAP, POP3 and NNTP Functions</A
  657. ></DT
  658. ><DT
  659. >XLVIII. <A
  660. HREF="#ref.ifx"
  661. >Informix Functions</A
  662. ></DT
  663. ><DT
  664. >XLIX. <A
  665. HREF="#ref.ibase"
  666. >Firebird/InterBase Functions</A
  667. ></DT
  668. ><DT
  669. >L. <A
  670. HREF="#ref.id3"
  671. >ID3 Functions</A
  672. ></DT
  673. ><DT
  674. >LI. <A
  675. HREF="#ref.ingres"
  676. >Ingres II Functions</A
  677. ></DT
  678. ><DT
  679. >LII. <A
  680. HREF="#ref.ircg"
  681. >IRC Gateway Functions</A
  682. ></DT
  683. ><DT
  684. >LIII. <A
  685. HREF="#ref.java"
  686. >PHP / Java Integration</A
  687. ></DT
  688. ><DT
  689. >LIV. <A
  690. HREF="#ref.ldap"
  691. >LDAP Functions</A
  692. ></DT
  693. ><DT
  694. >LV. <A
  695. HREF="#ref.lzf"
  696. >LZF Functions</A
  697. ></DT
  698. ><DT
  699. >LVI. <A
  700. HREF="#ref.mail"
  701. >Mail Functions</A
  702. ></DT
  703. ><DT
  704. >LVII. <A
  705. HREF="#ref.mailparse"
  706. >mailparse Functions</A
  707. ></DT
  708. ><DT
  709. >LVIII. <A
  710. HREF="#ref.math"
  711. >Mathematical Functions</A
  712. ></DT
  713. ><DT
  714. >LIX. <A
  715. HREF="#ref.mbstring"
  716. >Multibyte String Functions</A
  717. ></DT
  718. ><DT
  719. >LX. <A
  720. HREF="#ref.mcal"
  721. >MCAL Functions</A
  722. ></DT
  723. ><DT
  724. >LXI. <A
  725. HREF="#ref.mcrypt"
  726. >Mcrypt Encryption Functions</A
  727. ></DT
  728. ><DT
  729. >LXII. <A
  730. HREF="#ref.mcve"
  731. >MCVE Payment Functions</A
  732. ></DT
  733. ><DT
  734. >LXIII. <A
  735. HREF="#ref.memcache"
  736. >Memcache Functions</A
  737. ></DT
  738. ><DT
  739. >LXIV. <A
  740. HREF="#ref.mhash"
  741. >Mhash Functions</A
  742. ></DT
  743. ><DT
  744. >LXV. <A
  745. HREF="#ref.mime-magic"
  746. >Mimetype Functions</A
  747. ></DT
  748. ><DT
  749. >LXVI. <A
  750. HREF="#ref.mssql"
  751. >Microsoft SQL Server Functions</A
  752. ></DT
  753. ><DT
  754. >LXVII. <A
  755. HREF="#ref.ming"
  756. >Ming functions for Flash</A
  757. ></DT
  758. ><DT
  759. >LXVIII. <A
  760. HREF="#ref.misc"
  761. >Miscellaneous Functions</A
  762. ></DT
  763. ><DT
  764. >LXIX. <A
  765. HREF="#ref.mnogosearch"
  766. >mnoGoSearch Functions</A
  767. ></DT
  768. ><DT
  769. >LXX. <A
  770. HREF="#ref.msql"
  771. >mSQL Functions</A
  772. ></DT
  773. ><DT
  774. >LXXI. <A
  775. HREF="#ref.mysql"
  776. >MySQL Functions</A
  777. ></DT
  778. ><DT
  779. >LXXII. <A
  780. HREF="#ref.mysqli"
  781. >Improved MySQL Extension</A
  782. ></DT
  783. ><DT
  784. >LXXIII. <A
  785. HREF="#ref.msession"
  786. >Mohawk Software Session Handler Functions</A
  787. ></DT
  788. ><DT
  789. >LXXIV. <A
  790. HREF="#ref.muscat"
  791. >muscat Functions</A
  792. ></DT
  793. ><DT
  794. >LXXV. <A
  795. HREF="#ref.network"
  796. >Network Functions</A
  797. ></DT
  798. ><DT
  799. >LXXVI. <A
  800. HREF="#ref.ncurses"
  801. >Ncurses Terminal Screen Control Functions</A
  802. ></DT
  803. ><DT
  804. >LXXVII. <A
  805. HREF="#ref.notes"
  806. >Lotus Notes Functions</A
  807. ></DT
  808. ><DT
  809. >LXXVIII. <A
  810. HREF="#ref.nsapi"
  811. >NSAPI-specific Functions</A
  812. ></DT
  813. ><DT
  814. >LXXIX. <A
  815. HREF="#ref.uodbc"
  816. >ODBC Functions (Unified)</A
  817. ></DT
  818. ><DT
  819. >LXXX. <A
  820. HREF="#ref.objaggregation"
  821. >Object Aggregation/Composition Functions</A
  822. ></DT
  823. ><DT
  824. >LXXXI. <A
  825. HREF="#ref.oci8"
  826. >Oracle 8 functions</A
  827. ></DT
  828. ><DT
  829. >LXXXII. <A
  830. HREF="#ref.openal"
  831. >OpenAL Audio Bindings</A
  832. ></DT
  833. ><DT
  834. >LXXXIII. <A
  835. HREF="#ref.openssl"
  836. >OpenSSL Functions</A
  837. ></DT
  838. ><DT
  839. >LXXXIV. <A
  840. HREF="#ref.oracle"
  841. >Oracle Functions</A
  842. ></DT
  843. ><DT
  844. >LXXXV. <A
  845. HREF="#ref.ovrimos"
  846. >Ovrimos SQL Functions</A
  847. ></DT
  848. ><DT
  849. >LXXXVI. <A
  850. HREF="#ref.outcontrol"
  851. >Output Control Functions</A
  852. ></DT
  853. ><DT
  854. >LXXXVII. <A
  855. HREF="#ref.overload"
  856. >Object property and method call overloading</A
  857. ></DT
  858. ><DT
  859. >LXXXVIII. <A
  860. HREF="#ref.parsekit"
  861. >Parsekit Functions</A
  862. ></DT
  863. ><DT
  864. >LXXXIX. <A
  865. HREF="#ref.pdf"
  866. >PDF functions</A
  867. ></DT
  868. ><DT
  869. >XC. <A
  870. HREF="#ref.pdo"
  871. >PDO Functions</A
  872. ></DT
  873. ><DT
  874. >XCI. <A
  875. HREF="#ref.pfpro"
  876. >Verisign Payflow Pro Functions</A
  877. ></DT
  878. ><DT
  879. >XCII. <A
  880. HREF="#ref.info"
  881. >PHP Options&Information</A
  882. ></DT
  883. ><DT
  884. >XCIII. <A
  885. HREF="#ref.posix"
  886. >POSIX Functions</A
  887. ></DT
  888. ><DT
  889. >XCIV. <A
  890. HREF="#ref.pgsql"
  891. >PostgreSQL Functions</A
  892. ></DT
  893. ><DT
  894. >XCV. <A
  895. HREF="#ref.pcntl"
  896. >Process Control Functions</A
  897. ></DT
  898. ><DT
  899. >XCVI. <A
  900. HREF="#ref.exec"
  901. >Program Execution Functions</A
  902. ></DT
  903. ><DT
  904. >XCVII. <A
  905. HREF="#ref.printer"
  906. >Printer Functions</A
  907. ></DT
  908. ><DT
  909. >XCVIII. <A
  910. HREF="#ref.pspell"
  911. >Pspell Functions</A
  912. ></DT
  913. ><DT
  914. >XCIX. <A
  915. HREF="#ref.readline"
  916. >GNU Readline</A
  917. ></DT
  918. ><DT
  919. >C. <A
  920. HREF="#ref.recode"
  921. >GNU Recode Functions</A
  922. ></DT
  923. ><DT
  924. >CI. <A
  925. HREF="#ref.pcre"
  926. >Regular Expression Functions (Perl-Compatible)</A
  927. ></DT
  928. ><DT
  929. >CII. <A
  930. HREF="#ref.qtdom"
  931. >qtdom Functions</A
  932. ></DT
  933. ><DT
  934. >CIII. <A
  935. HREF="#ref.rar"
  936. >Rar Functions</A
  937. ></DT
  938. ><DT
  939. >CIV. <A
  940. HREF="#ref.regex"
  941. >Regular Expression Functions (POSIX Extended)</A
  942. ></DT
  943. ><DT
  944. >CV. <A
  945. HREF="#ref.ssh2"
  946. >Secure Shell2 Functions</A
  947. ></DT
  948. ><DT
  949. >CVI. <A
  950. HREF="#ref.sem"
  951. >Semaphore, Shared Memory and IPC Functions</A
  952. ></DT
  953. ><DT
  954. >CVII. <A
  955. HREF="#ref.sesam"
  956. >SESAM Database Functions</A
  957. ></DT
  958. ><DT
  959. >CVIII. <A
  960. HREF="#ref.session"
  961. >Session Handling Functions</A
  962. ></DT
  963. ><DT
  964. >CIX. <A
  965. HREF="#ref.shmop"
  966. >Shared Memory Functions</A
  967. ></DT
  968. ><DT
  969. >CX. <A
  970. HREF="#ref.simplexml"
  971. >SimpleXML functions</A
  972. ></DT
  973. ><DT
  974. >CXI. <A
  975. HREF="#ref.soap"
  976. >SOAP Functions</A
  977. ></DT
  978. ><DT
  979. >CXII. <A
  980. HREF="#ref.sqlite"
  981. >SQLite</A
  982. ></DT
  983. ><DT
  984. >CXIII. <A
  985. HREF="#ref.swf"
  986. >Shockwave Flash Functions</A
  987. ></DT
  988. ><DT
  989. >CXIV. <A
  990. HREF="#ref.snmp"
  991. >SNMP Functions</A
  992. ></DT
  993. ><DT
  994. >CXV. <A
  995. HREF="#ref.sockets"
  996. >Socket Functions</A
  997. ></DT
  998. ><DT
  999. >CXVI. <A
  1000. HREF="#ref.spl"
  1001. >Standard PHP Library (SPL) Functions</A
  1002. ></DT
  1003. ><DT
  1004. >CXVII. <A
  1005. HREF="#ref.stream"
  1006. >Stream Functions</A
  1007. ></DT
  1008. ><DT
  1009. >CXVIII. <A
  1010. HREF="#ref.strings"
  1011. >String Functions</A
  1012. ></DT
  1013. ><DT
  1014. >CXIX. <A
  1015. HREF="#ref.sybase"
  1016. >Sybase Functions</A
  1017. ></DT
  1018. ><DT
  1019. >CXX. <A
  1020. HREF="#ref.tcpwrap"
  1021. >TCP Wrappers Functions</A
  1022. ></DT
  1023. ><DT
  1024. >CXXI. <A
  1025. HREF="#ref.tidy"
  1026. >Tidy Functions</A
  1027. ></DT
  1028. ><DT
  1029. >CXXII. <A
  1030. HREF="#ref.tokenizer"
  1031. >Tokenizer Functions</A
  1032. ></DT
  1033. ><DT
  1034. >CXXIII. <A
  1035. HREF="#ref.url"
  1036. >URL Functions</A
  1037. ></DT
  1038. ><DT
  1039. >CXXIV. <A
  1040. HREF="#ref.var"
  1041. >Variable Functions</A
  1042. ></DT
  1043. ><DT
  1044. >CXXV. <A
  1045. HREF="#ref.vpopmail"
  1046. >vpopmail Functions</A
  1047. ></DT
  1048. ><DT
  1049. >CXXVI. <A
  1050. HREF="#ref.w32api"
  1051. >W32api Functions</A
  1052. ></DT
  1053. ><DT
  1054. >CXXVII. <A
  1055. HREF="#ref.wddx"
  1056. >WDDX Functions</A
  1057. ></DT
  1058. ><DT
  1059. >CXXVIII. <A
  1060. HREF="#ref.xattr"
  1061. >xattr Functions</A
  1062. ></DT
  1063. ><DT
  1064. >CXXIX. <A
  1065. HREF="#ref.xml"
  1066. >XML Parser Functions</A
  1067. ></DT
  1068. ><DT
  1069. >CXXX. <A
  1070. HREF="#ref.xmlrpc"
  1071. >XML-RPC Functions</A
  1072. ></DT
  1073. ><DT
  1074. >CXXXI. <A
  1075. HREF="#ref.xdiff"
  1076. >xdiff Functions</A
  1077. ></DT
  1078. ><DT
  1079. >CXXXII. <A
  1080. HREF="#ref.xsl"
  1081. >XSL functions</A
  1082. ></DT
  1083. ><DT
  1084. >CXXXIII. <A
  1085. HREF="#ref.xslt"
  1086. >XSLT Functions</A
  1087. ></DT
  1088. ><DT
  1089. >CXXXIV. <A
  1090. HREF="#ref.yaz"
  1091. >YAZ Functions</A
  1092. ></DT
  1093. ><DT
  1094. >CXXXV. <A
  1095. HREF="#ref.nis"
  1096. >YP/NIS Functions</A
  1097. ></DT
  1098. ><DT
  1099. >CXXXVI. <A
  1100. HREF="#ref.zip"
  1101. >Zip File Functions (Read Only Access)</A
  1102. ></DT
  1103. ><DT
  1104. >CXXXVII. <A
  1105. HREF="#ref.zlib"
  1106. >Zlib Compression Functions</A
  1107. ></DT
  1108. ></DL
  1109. ></DD
  1110. ><DT
  1111. >VII. <A
  1112. HREF="#zend"
  1113. >Zend API</A
  1114. ></DT
  1115. ><DD
  1116. ><DL
  1117. ><DT
  1118. >44. <A
  1119. HREF="#zend.overview"
  1120. >Overview</A
  1121. ></DT
  1122. ><DT
  1123. >45. <A
  1124. HREF="#zend.possibilities"
  1125. >Extension Possibilities</A
  1126. ></DT
  1127. ><DT
  1128. >46. <A
  1129. HREF="#zend.layout"
  1130. >Source Layout</A
  1131. ></DT
  1132. ><DT
  1133. >47. <A
  1134. HREF="#zend.build"
  1135. >PHP's Automatic Build System</A
  1136. ></DT
  1137. ><DT
  1138. >48. <A
  1139. HREF="#zend.creating"
  1140. >Creating Extensions</A
  1141. ></DT
  1142. ><DT
  1143. >49. <A
  1144. HREF="#zend.using"
  1145. >Using Extensions</A
  1146. ></DT
  1147. ><DT
  1148. >50. <A
  1149. HREF="#zend.troubleshooting"
  1150. >Troubleshooting</A
  1151. ></DT
  1152. ><DT
  1153. >51. <A
  1154. HREF="#zend.structure"
  1155. >Source Discussion</A
  1156. ></DT
  1157. ><DT
  1158. >52. <A
  1159. HREF="#zend.arguments"
  1160. >Accepting Arguments</A
  1161. ></DT
  1162. ><DT
  1163. >53. <A
  1164. HREF="#zend.variables"
  1165. >Creating Variables</A
  1166. ></DT
  1167. ><DT
  1168. >54. <A
  1169. HREF="#zend.copy-constructor"
  1170. >Duplicating Variable Contents: The Copy Constructor</A
  1171. ></DT
  1172. ><DT
  1173. >55. <A
  1174. HREF="#zend.returning"
  1175. >Returning Values</A
  1176. ></DT
  1177. ><DT
  1178. >56. <A
  1179. HREF="#zend.printing"
  1180. >Printing Information</A
  1181. ></DT
  1182. ><DT
  1183. >57. <A
  1184. HREF="#zend.startup-and-shutdown"
  1185. >Startup and Shutdown Functions</A
  1186. ></DT
  1187. ><DT
  1188. >58. <A
  1189. HREF="#zend.calling-user-functions"
  1190. >Calling User Functions</A
  1191. ></DT
  1192. ><DT
  1193. >59. <A
  1194. HREF="#zend.ini-file-support"
  1195. >Initialization File Support</A
  1196. ></DT
  1197. ><DT
  1198. >60. <A
  1199. HREF="#zend.where-to-go"
  1200. >Where to Go from Here</A
  1201. ></DT
  1202. ><DT
  1203. >61. <A
  1204. HREF="#zend.configuration-macros"
  1205. >Reference: Some Configuration Macros</A
  1206. ></DT
  1207. ><DT
  1208. >62. <A
  1209. HREF="#zend.api-macros"
  1210. >API Macros</A
  1211. ></DT
  1212. ></DL
  1213. ></DD
  1214. ><DT
  1215. >VIII. <A
  1216. HREF="#api"
  1217. >PHP API: Interfaces for extension writers</A
  1218. ></DT
  1219. ><DD
  1220. ><DL
  1221. ><DT
  1222. >63. <A
  1223. HREF="#streams"
  1224. >Streams API for PHP Extension Authors</A
  1225. ></DT
  1226. ></DL
  1227. ></DD
  1228. ><DT
  1229. >IX. <A
  1230. HREF="#faq"
  1231. >FAQ: Frequently Asked Questions</A
  1232. ></DT
  1233. ><DD
  1234. ><DL
  1235. ><DT
  1236. >64. <A
  1237. HREF="#faq.general"
  1238. >General Information</A
  1239. ></DT
  1240. ><DT
  1241. >65. <A
  1242. HREF="#faq.mailinglist"
  1243. >Mailing lists</A
  1244. ></DT
  1245. ><DT
  1246. >66. <A
  1247. HREF="#faq.obtaining"
  1248. >Obtaining PHP</A
  1249. ></DT
  1250. ><DT
  1251. >67. <A
  1252. HREF="#faq.databases"
  1253. >Database issues</A
  1254. ></DT
  1255. ><DT
  1256. >68. <A
  1257. HREF="#faq.installation"
  1258. >Installation</A
  1259. ></DT
  1260. ><DT
  1261. >69. <A
  1262. HREF="#faq.build"
  1263. >Build Problems</A
  1264. ></DT
  1265. ><DT
  1266. >70. <A
  1267. HREF="#faq.using"
  1268. >Using PHP</A
  1269. ></DT
  1270. ><DT
  1271. >71. <A
  1272. HREF="#faq.html"
  1273. >PHP and HTML</A
  1274. ></DT
  1275. ><DT
  1276. >72. <A
  1277. HREF="#faq.com"
  1278. >PHP and COM</A
  1279. ></DT
  1280. ><DT
  1281. >73. <A
  1282. HREF="#faq.languages"
  1283. >PHP and other languages</A
  1284. ></DT
  1285. ><DT
  1286. >74. <A
  1287. HREF="#faq.migration"
  1288. >Migrating from PHP 2 to PHP 3</A
  1289. ></DT
  1290. ><DT
  1291. >75. <A
  1292. HREF="#faq.migration4"
  1293. >Migrating from PHP 3 to PHP 4</A
  1294. ></DT
  1295. ><DT
  1296. >76. <A
  1297. HREF="#faq.migration5"
  1298. >Migrating from PHP 4 to PHP 5</A
  1299. ></DT
  1300. ><DT
  1301. >77. <A
  1302. HREF="#faq.misc"
  1303. >Miscellaneous Questions</A
  1304. ></DT
  1305. ></DL
  1306. ></DD
  1307. ><DT
  1308. >X. <A
  1309. HREF="#appendixes"
  1310. >Appendixes</A
  1311. ></DT
  1312. ><DD
  1313. ><DL
  1314. ><DT
  1315. >A. <A
  1316. HREF="#history"
  1317. >History of PHP and related projects</A
  1318. ></DT
  1319. ><DT
  1320. >B. <A
  1321. HREF="#migration5"
  1322. >Migrating from PHP 4 to PHP 5</A
  1323. ></DT
  1324. ><DT
  1325. >C. <A
  1326. HREF="#migration4"
  1327. >Migrating from PHP 3 to PHP 4</A
  1328. ></DT
  1329. ><DT
  1330. >D. <A
  1331. HREF="#migration"
  1332. >Migrating from PHP/FI 2 to PHP 3</A
  1333. ></DT
  1334. ><DT
  1335. >E. <A
  1336. HREF="#debugger"
  1337. >Debugging PHP</A
  1338. ></DT
  1339. ><DT
  1340. >F. <A
  1341. HREF="#phpdevel"
  1342. >Extending PHP 3</A
  1343. ></DT
  1344. ><DT
  1345. >G. <A
  1346. HREF="#configure"
  1347. >Configure options</A
  1348. ></DT
  1349. ><DT
  1350. >H. <A
  1351. HREF="#ini"
  1352. >List of core <TT
  1353. CLASS="filename"
  1354. >php.ini</TT
  1355. > directives</A
  1356. ></DT
  1357. ><DT
  1358. >I. <A
  1359. HREF="#aliases"
  1360. >List of Function Aliases</A
  1361. ></DT
  1362. ><DT
  1363. >J. <A
  1364. HREF="#reserved"
  1365. >List of Reserved Words</A
  1366. ></DT
  1367. ><DT
  1368. >K. <A
  1369. HREF="#resource"
  1370. >List of Resource Types</A
  1371. ></DT
  1372. ><DT
  1373. >L. <A
  1374. HREF="#wrappers"
  1375. >List of Supported Protocols/Wrappers</A
  1376. ></DT
  1377. ><DT
  1378. >M. <A
  1379. HREF="#filters"
  1380. >List of Available Filters</A
  1381. ></DT
  1382. ><DT
  1383. >N. <A
  1384. HREF="#transports"
  1385. >List of Supported Socket Transports</A
  1386. ></DT
  1387. ><DT
  1388. >O. <A
  1389. HREF="#types.comparisons"
  1390. >PHP type comparison tables</A
  1391. ></DT
  1392. ><DT
  1393. >P. <A
  1394. HREF="#tokens"
  1395. >List of Parser Tokens</A
  1396. ></DT
  1397. ><DT
  1398. >Q. <A
  1399. HREF="#about"
  1400. >About the manual</A
  1401. ></DT
  1402. ><DT
  1403. >R. <A
  1404. HREF="#opl.license"
  1405. >Open Publication License</A
  1406. ></DT
  1407. ><DT
  1408. >S. <A
  1409. HREF="#indexes"
  1410. >Function Index</A
  1411. ></DT
  1412. ><DT
  1413. >T. <A
  1414. HREF="#missing-stuff"
  1415. >Missing Stuff</A
  1416. ></DT
  1417. ></DL
  1418. ></DD
  1419. ></DL
  1420. ></DIV
  1421. ><DIV
  1422. CLASS="preface"
  1423. ><HR><H1
  1424. ><A
  1425. NAME="preface"
  1426. >Preface</A
  1427. ></H1
  1428. ><BLOCKQUOTE
  1429. CLASS="ABSTRACT"
  1430. ><DIV
  1431. CLASS="abstract"
  1432. ><P
  1433. ></P
  1434. ><A
  1435. NAME="AEN54"
  1436. ></A
  1437. ><P
  1438. >     <ACRONYM
  1439. CLASS="acronym"
  1440. >PHP</ACRONYM
  1441. >, which stands for "PHP: Hypertext
  1442.     Preprocessor" is a widely-used Open Source general-purpose
  1443.     scripting language that is especially suited for Web
  1444.     development and can be embedded into HTML. Its syntax draws
  1445.     upon C, Java, and Perl, and is easy to learn. The main goal of
  1446.     the language is to allow web developers to write dynamically
  1447.     generated webpages quickly, but you can do much more with PHP.
  1448.    </P
  1449. ><P
  1450. ></P
  1451. ></DIV
  1452. ></BLOCKQUOTE
  1453. ><P
  1454. >    This manual consists primarily of a <A
  1455. HREF="#funcref"
  1456. >    function reference</A
  1457. >, but also contains a 
  1458.    <A
  1459. HREF="#langref"
  1460. >language reference</A
  1461. >, explanations
  1462.    of some of PHP's major <A
  1463. HREF="#features"
  1464. >features</A
  1465. >, 
  1466.    and other <A
  1467. HREF="#appendixes"
  1468. >supplemental</A
  1469.    information.
  1470.   </P
  1471. ><P
  1472. >    You can download this manual in several formats at <A
  1473. HREF="http://www.php.net/download-docs.php"
  1474. TARGET="_top"
  1475. >http://www.php.net/download-docs.php</A
  1476. >.
  1477.    More information about how this manual is developed can be found in the
  1478.    <A
  1479. HREF="#about"
  1480. >'About the manual'</A
  1481. > appendix. If you are
  1482.    interested in the <A
  1483. HREF="#history"
  1484. >history of PHP</A
  1485. >,
  1486.    visit the relevant appendix.
  1487.   </P
  1488. ><DIV
  1489. CLASS="sect1"
  1490. ><HR><H2
  1491. CLASS="sect1"
  1492. ><A
  1493. NAME="contributors"
  1494. >Authors and Contributors</A
  1495. ></H2
  1496. ><P
  1497. > We highlight the currently most active
  1498. people on the manual frontpage, but there are many more contributors who
  1499. currently help in our work or provided a great amount of help to the project
  1500. in the past. There are a lot of unnamed people who help out with their user
  1501. notes on manual pages, which continually get included in the references, the
  1502. work of whom we are also very thankful for. All the lists provided below are in
  1503. alphabetical order.
  1504. </P
  1505. ><DIV
  1506. CLASS="sect2"
  1507. ><HR><H3
  1508. CLASS="sect2"
  1509. ><A
  1510. NAME="AEN69"
  1511. >Authors and Editors</A
  1512. ></H3
  1513. ><P
  1514. >    The following contributors should be
  1515. recognized for the impact they have made and/or continue to make by adding
  1516. content to the manual:
  1517.    Jouni Ahto,              
  1518.    Alexander Aulbach,       
  1519.    Daniel Beckham,          
  1520.    Stig Bakken,             
  1521.    Jesus M. Castagnetto,    
  1522.    Ron Chmara,              
  1523.    John Coggeshall,         
  1524.    Simone Cortesi,          
  1525.    Markus Fischer,          
  1526.    Wez Furlong,             
  1527.    Sara Golemon,            
  1528.    Rui Hirokawa,            
  1529.    Brad House,              
  1530.    Moriyoshi Koizumi,       
  1531.    Rasmus Lerdorf,          
  1532.    Andrew Lindeman,         
  1533.    Stanislav Malyshev,      
  1534.    Rafael Martinez,         
  1535.    Yasuo Ohgaki,            
  1536.    Derick Rethans,          
  1537.    Sander Roobol,           
  1538.    Egon Schmid,             
  1539.    Thomas Schoefbeck,       
  1540.    Sascha Schumann,         
  1541.    Lars Torben Wilson,      
  1542.    Jim Winstead,            
  1543.    Jeroen van Wolffelaar and  
  1544.    Andrei Zmievski.         
  1545.   </P
  1546. ><P
  1547. >    The following contributors have done
  1548. significant work editing the manual:
  1549.    Stig Bakken,                    
  1550.    Hartmut Holzgraefe and  
  1551.    Egon Schmid.                    
  1552.   </P
  1553. ></DIV
  1554. ><DIV
  1555. CLASS="sect2"
  1556. ><HR><H3
  1557. CLASS="sect2"
  1558. ><A
  1559. NAME="AEN73"
  1560. >User Note Maintainers</A
  1561. ></H3
  1562. ><P
  1563. >    The currently most active maintainers are:
  1564.    Mehdi Achour,                
  1565.    Friedhelm Betz,              
  1566.    Vincent Gevers,              
  1567.    Aidan Lister,                
  1568.    Nuno Lopes and       
  1569.    Tom Sommer.                  
  1570.   </P
  1571. ><P
  1572. >    These people have also put a lot of effort
  1573. into managing user notes:
  1574.    Daniel Beckham,         
  1575.    Victor Boivie,          
  1576.    Jesus M. Castagnetto,   
  1577.    Nicolas Chaillan,       
  1578.    Ron Chmara,             
  1579.    James Cox,              
  1580.    Sara Golemon,           
  1581.    Zak Greant,             
  1582.    Szabolcs Heilig,        
  1583.    Oliver Hinckel,         
  1584.    Hartmut Holzgraefe,     
  1585.    Rasmus Lerdorf,         
  1586.    Andrew Lindeman,        
  1587.    Maxim Maletsky,         
  1588.    James Moore,            
  1589.    Sebastian Picklum,      
  1590.    Derick Rethans,         
  1591.    Sander Roobol,          
  1592.    Damien Seguy,           
  1593.    Jason Sheets,           
  1594.    Jani Taskinen,          
  1595.    Yasuo Ohgaki,           
  1596.    Philip Olson,           
  1597.    Lars Torben Wilson,     
  1598.    Jim Winstead,           
  1599.    Jared Wyles and  
  1600.    Jeroen van Wolffelaar.  
  1601.   </P
  1602. ></DIV
  1603. ></DIV
  1604. ></DIV
  1605. ><DIV
  1606. CLASS="PART"
  1607. ><A
  1608. NAME="getting-started"
  1609. ></A
  1610. ><DIV
  1611. CLASS="TITLEPAGE"
  1612. ><H1
  1613. CLASS="title"
  1614. >I. Getting Started</H1
  1615. ><DIV
  1616. CLASS="TOC"
  1617. ><DL
  1618. ><DT
  1619. ><B
  1620. >Table of Contents</B
  1621. ></DT
  1622. ><DT
  1623. >1. <A
  1624. HREF="#introduction"
  1625. >Introduction</A
  1626. ></DT
  1627. ><DT
  1628. >2. <A
  1629. HREF="#tutorial"
  1630. >A simple tutorial</A
  1631. ></DT
  1632. ></DL
  1633. ></DIV
  1634. ></DIV
  1635. ><DIV
  1636. CLASS="chapter"
  1637. ><HR><H1
  1638. ><A
  1639. NAME="introduction"
  1640. >Chapter 1. Introduction</A
  1641. ></H1
  1642. ><DIV
  1643. CLASS="sect1"
  1644. ><H2
  1645. CLASS="sect1"
  1646. ><A
  1647. NAME="intro-whatis"
  1648. >What is PHP?</A
  1649. ></H2
  1650. ><P
  1651. >     <ACRONYM
  1652. CLASS="acronym"
  1653. >PHP</ACRONYM
  1654. > (recursive acronym for "PHP: Hypertext
  1655.     Preprocessor") is a widely-used Open Source general-purpose
  1656.     scripting language that is especially suited for Web
  1657.     development and can be embedded into HTML.
  1658.    </P
  1659. ><P
  1660. >     Simple answer, but what does that mean? An example:
  1661.    </P
  1662. ><P
  1663. >     <TABLE
  1664. WIDTH="100%"
  1665. BORDER="0"
  1666. CELLPADDING="0"
  1667. CELLSPACING="0"
  1668. CLASS="EXAMPLE"
  1669. ><TR
  1670. ><TD
  1671. ><DIV
  1672. CLASS="example"
  1673. ><A
  1674. NAME="AEN87"
  1675. ></A
  1676. ><P
  1677. ><B
  1678. >Example 1-1. An introductory example</B
  1679. ></P
  1680. ><TABLE
  1681. BORDER="0"
  1682. BGCOLOR="#E0E0E0"
  1683. CELLPADDING="5"
  1684. ><TR
  1685. ><TD
  1686. ><PRE
  1687. CLASS="php"
  1688. ><html>
  1689.     <head>
  1690.         <title>Example</title>
  1691.     </head>
  1692.     <body>
  1693.  
  1694.         <?php 
  1695.         echo "Hi, I'm a PHP script!"; 
  1696.         ?>
  1697.  
  1698.     </body>
  1699. </html></PRE
  1700. ></TD
  1701. ></TR
  1702. ></TABLE
  1703. ></DIV
  1704. ></TD
  1705. ></TR
  1706. ></TABLE
  1707. >
  1708.    </P
  1709. ><P
  1710. >     Notice how this is different from a script written in other
  1711.     languages like Perl or C -- instead of writing a program with lots
  1712.     of commands to output HTML, you write an HTML script with some
  1713.     embedded code to do something (in this case, output some
  1714.     text). The PHP code is enclosed in special <A
  1715. HREF="#language.basic-syntax.phpmode"
  1716. >start and end tags</A
  1717. >
  1718.     that allow you to jump into and out of "PHP mode".
  1719.    </P
  1720. ><P
  1721. >     What distinguishes PHP from something like client-side JavaScript
  1722.     is that the code is executed on the server. If you were to have a
  1723.     script similar to the above on your server, the client would receive
  1724.     the results of running that script, with no way of determining what
  1725.     the underlying code may be. You can even configure your web server
  1726.     to process all your HTML files with PHP, and then there's really no
  1727.     way that users can tell what you have up your sleeve.
  1728.    </P
  1729. ><P
  1730. >     The best things in using PHP are that it is extremely simple
  1731.     for a newcomer, but offers many advanced features for
  1732.     a professional programmer. Don't be afraid reading the long
  1733.     list of PHP's features. You can jump in, in a short time, and
  1734.     start writing simple scripts in a few hours.
  1735.    </P
  1736. ><P
  1737. >     Although PHP's development is focused on server-side scripting,
  1738.     you can do much more with it. Read on, and see more in the
  1739.     <A
  1740. HREF="#intro-whatcando"
  1741. >What can PHP do?</A
  1742. > section,
  1743.     or go right to the <A
  1744. HREF="#tutorial"
  1745. >introductory
  1746.     tutorial</A
  1747. > if you are only interested in web programming.
  1748.    </P
  1749. ></DIV
  1750. ><DIV
  1751. CLASS="sect1"
  1752. ><HR><H2
  1753. CLASS="sect1"
  1754. ><A
  1755. NAME="intro-whatcando"
  1756. >What can PHP do?</A
  1757. ></H2
  1758. ><P
  1759. >     Anything. PHP is mainly focused on server-side scripting,
  1760.     so you can do anything any other CGI program can do, such
  1761.     as collect form data, generate dynamic page content, or
  1762.     send and receive cookies. But PHP can do much more.
  1763.    </P
  1764. ><P
  1765. >     There are three main areas where PHP scripts are used.
  1766.     <P
  1767. ></P
  1768. ><UL
  1769. ><LI
  1770. ><P
  1771. >        Server-side scripting. This is the most traditional
  1772.        and main target field for PHP. You need three things
  1773.        to make this work. The PHP parser (CGI or server
  1774.        module), a webserver and a web browser. You need to
  1775.        run the webserver, with a connected PHP installation.
  1776.        You can access the PHP program output with a web browser,
  1777.        viewing the PHP page through the server. All these can
  1778.        run on your home machine if you are just experimenting
  1779.        with PHP programming. See the
  1780.        <A
  1781. HREF="#install"
  1782. >installation instructions</A
  1783. >
  1784.        section for more information.
  1785.       </P
  1786. ></LI
  1787. ><LI
  1788. ><P
  1789. >        Command line scripting. You can make a PHP script
  1790.        to run it without any server or browser.
  1791.        You only need the PHP parser to use it this way.
  1792.        This type of usage is ideal for scripts regularly
  1793.        executed using cron (on *nix or Linux) or Task Scheduler (on
  1794.        Windows). These scripts can also be used for simple text
  1795.        processing tasks. See the section about 
  1796.        <A
  1797. HREF="#features.commandline"
  1798. >Command line usage of PHP</A
  1799. >
  1800.        for more information.
  1801.       </P
  1802. ></LI
  1803. ><LI
  1804. ><P
  1805. >        Writing desktop applications. PHP is probably
  1806.        not the very best language to create a desktop
  1807.        application with a graphical user interface, but if
  1808.        you know PHP very well, and would like to use some
  1809.        advanced PHP features in your client-side applications
  1810.        you can also use PHP-GTK to write such programs. You also
  1811.        have the ability to write cross-platform applications this
  1812.        way. PHP-GTK is an extension to PHP, not available in
  1813.        the main distribution. If you are interested
  1814.        in PHP-GTK, visit <A
  1815. HREF="http://gtk.php.net/"
  1816. TARGET="_top"
  1817. >its
  1818.        own website</A
  1819. >.
  1820.       </P
  1821. ></LI
  1822. ></UL
  1823. >
  1824.    </P
  1825. ><P
  1826. >     PHP can be used on all major operating systems, including
  1827.     Linux, many Unix variants (including HP-UX, Solaris and OpenBSD),
  1828.     Microsoft Windows, Mac OS X, RISC OS, and probably others.
  1829.     PHP has also support for most of the web servers today. This
  1830.     includes Apache, Microsoft Internet Information Server,
  1831.     Personal Web Server, Netscape and iPlanet servers, Oreilly
  1832.     Website Pro server, Caudium, Xitami, OmniHTTPd, and many
  1833.     others. For the majority of the servers PHP has a module,
  1834.     for the others supporting the CGI standard, PHP can work
  1835.     as a CGI processor.
  1836.    </P
  1837. ><P
  1838. >     So with PHP, you have the freedom of choosing an operating
  1839.     system and a web server. Furthermore, you also have the choice
  1840.     of using procedural programming or object oriented
  1841.     programming, or a mixture of them. Although not every
  1842.     standard OOP feature is implemented in PHP 4,
  1843.     many code libraries and large applications (including
  1844.     the PEAR library) are written only using OOP code. PHP 5 fixes the
  1845.     OOP related weaknesses of PHP 4, and introduces a complete object
  1846.     model.
  1847.    </P
  1848. ><P
  1849. >     With PHP you are not limited to output HTML. PHP's abilities
  1850.     includes outputting images, PDF files and even Flash movies
  1851.     (using libswf and Ming) generated on the fly. You can also
  1852.     output easily any text, such as XHTML and any other XML file.
  1853.     PHP can autogenerate these files, and save them in the file
  1854.     system, instead of printing it out, forming a server-side
  1855.     cache for your dynamic content.
  1856.    </P
  1857. ><P
  1858. >     One of the strongest and most significant features in PHP is its
  1859.     support for a wide range of databases. Writing a database-enabled
  1860.     web page is incredibly simple. The following databases are currently
  1861.     supported:
  1862.     <A
  1863. NAME="AEN115"
  1864. ></A
  1865. ><BLOCKQUOTE
  1866. CLASS="BLOCKQUOTE"
  1867. ><P
  1868. ></P
  1869. ><TABLE
  1870. BORDER="0"
  1871. ><TBODY
  1872. ><TR
  1873. ><TD
  1874. >Adabas D</TD
  1875. ><TD
  1876. >InterBase</TD
  1877. ><TD
  1878. >PostgreSQL</TD
  1879. ></TR
  1880. ><TR
  1881. ><TD
  1882. >dBase</TD
  1883. ><TD
  1884. >FrontBase</TD
  1885. ><TD
  1886. >SQLite</TD
  1887. ></TR
  1888. ><TR
  1889. ><TD
  1890. >Empress</TD
  1891. ><TD
  1892. >mSQL</TD
  1893. ><TD
  1894. >Solid</TD
  1895. ></TR
  1896. ><TR
  1897. ><TD
  1898. >FilePro (read-only)</TD
  1899. ><TD
  1900. >Direct MS-SQL</TD
  1901. ><TD
  1902. >Sybase</TD
  1903. ></TR
  1904. ><TR
  1905. ><TD
  1906. >Hyperwave</TD
  1907. ><TD
  1908. >MySQL</TD
  1909. ><TD
  1910. >Velocis</TD
  1911. ></TR
  1912. ><TR
  1913. ><TD
  1914. >IBM DB2</TD
  1915. ><TD
  1916. >ODBC</TD
  1917. ><TD
  1918. >Unix dbm</TD
  1919. ></TR
  1920. ><TR
  1921. ><TD
  1922. >Informix</TD
  1923. ><TD
  1924. >Oracle (OCI7 and OCI8)</TD
  1925. ><TD
  1926. > </TD
  1927. ></TR
  1928. ><TR
  1929. ><TD
  1930. >Ingres</TD
  1931. ><TD
  1932. >Ovrimos</TD
  1933. ><TD
  1934. > </TD
  1935. ></TR
  1936. ></TBODY
  1937. ></TABLE
  1938. ><P
  1939. ></P
  1940. ></BLOCKQUOTE
  1941. >
  1942.     We also have a DBX database abstraction extension allowing you
  1943.     to transparently use any database supported by that extension.
  1944.     Additionally PHP supports ODBC, the Open Database Connection
  1945.     standard, so you can connect to any other database supporting
  1946.     this world standard.
  1947.    </P
  1948. ><P
  1949. >     PHP also has support for talking to other services using protocols
  1950.     such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows) and
  1951.     countless others. You can also open raw network sockets and
  1952.     interact using any other protocol. PHP has support for the WDDX
  1953.     complex data exchange between virtually all Web programming
  1954.     languages. Talking about interconnection, PHP has support for
  1955.     instantiation of Java objects and using them transparently
  1956.     as PHP objects. You can also use our CORBA extension to
  1957.     access remote objects.
  1958.    </P
  1959. ><P
  1960. >     PHP has extremely useful text processing features, from the
  1961.     POSIX Extended or Perl regular expressions to parsing XML
  1962.     documents. For parsing and accessing XML documents, PHP 4
  1963.     supports the SAX and DOM standards, and you can also use the
  1964.     XSLT extension to transform XML documents. PHP 5 standardizes
  1965.     all the XML extensions on the solid base of libxml2 and extends
  1966.     the feature set adding SimpleXML and XMLReader support.
  1967.    </P
  1968. ><P
  1969. >     While using PHP in the e-commerce field, you'll find
  1970.     the Cybercash payment, CyberMUT, VeriSign Payflow
  1971.     Pro and MCVE functions useful for your online payment
  1972.     programs.
  1973.    </P
  1974. ><P
  1975. >     At last but not least, we have many other interesting
  1976.     extensions, the mnoGoSearch search engine functions,
  1977.     the IRC Gateway functions, many compression utilities
  1978.     (gzip, bz2), calendar conversion, translation...
  1979.    </P
  1980. ><P
  1981. >     As you can see this page is not enough to list all
  1982.     the features and benefits PHP can offer. Read on in
  1983.     the sections about <A
  1984. HREF="#install"
  1985. >installing
  1986.     PHP</A
  1987. >, and see the <A
  1988. HREF="#funcref"
  1989. >function
  1990.     reference</A
  1991. > part for explanation of the extensions
  1992.     mentioned here.
  1993.    </P
  1994. ></DIV
  1995. ></DIV
  1996. ><DIV
  1997. CLASS="chapter"
  1998. ><HR><H1
  1999. ><A
  2000. NAME="tutorial"
  2001. >Chapter 2. A simple tutorial</A
  2002. ></H1
  2003. ><P
  2004. >    Here we would like to show the very basics of PHP in a short, simple
  2005.    tutorial. This text only deals with dynamic webpage creation with
  2006.    PHP, though PHP is not only capable of creating webpages. See
  2007.    the section titled <A
  2008. HREF="#intro-whatcando"
  2009. >What can PHP
  2010.    do</A
  2011. > for more information.
  2012.   </P
  2013. ><P
  2014. >    PHP-enabled web pages are treated just like regular HTML pages and
  2015.    you can create and edit them the same way you normally create
  2016.    regular HTML pages.
  2017.   </P
  2018. ><DIV
  2019. CLASS="sect1"
  2020. ><HR><H2
  2021. CLASS="sect1"
  2022. ><A
  2023. NAME="tutorial.requirements"
  2024. >What do I need?</A
  2025. ></H2
  2026. ><P
  2027. >     In this tutorial we assume that your server has activated support 
  2028.     for PHP and that all files ending in <TT
  2029. CLASS="filename"
  2030. >.php</TT
  2031. >
  2032.     are handled by PHP. On most servers, this is the default extension
  2033.     for PHP files, but ask your server administrator to be sure. If
  2034.     your server supports PHP, then you do not need to do anything. Just
  2035.     create your <TT
  2036. CLASS="filename"
  2037. >.php</TT
  2038. > files, put them in your
  2039.     web directory and the server will automatically parse them for you.
  2040.     There is no need to compile anything nor do you need to install
  2041.     any extra tools. Think of these PHP-enabled files as simple HTML
  2042.     files with a whole new family of magical tags that let you do all
  2043.     sorts of things.  Most web hosts offer PHP support, but if your
  2044.     host does not, consider reading the <A
  2045. HREF="http://www.php.net/links.php"
  2046. TARGET="_top"
  2047. >     PHP Links</A
  2048. > section for resources on finding PHP enabled
  2049.     web hosts.
  2050.    </P
  2051. ><P
  2052. >     Let us say you want to save precious bandwidth and develop locally.  
  2053.     In this case, you will want to install a web server, such as 
  2054.     <A
  2055. HREF="http://www.apache.org/"
  2056. TARGET="_top"
  2057. >Apache</A
  2058. >, and of course 
  2059.     <A
  2060. HREF="http://www.php.net/downloads.php"
  2061. TARGET="_top"
  2062. >PHP</A
  2063. >. You will most likely 
  2064.     want to install a database as well, such as 
  2065.     <A
  2066. HREF="http://dev.mysql.com/doc/"
  2067. TARGET="_top"
  2068. >MySQL</A
  2069. >.
  2070.    </P
  2071. ><P
  2072. >     You can either install these individually or choose a simpler way. Our
  2073.     manual has <A
  2074. HREF="#install"
  2075. >installation instructions for
  2076.     PHP</A
  2077. > (assuming you already have some webserver set up). In case
  2078.     you have problems with installing PHP yourself, we would suggest you ask
  2079.     your questions on our <A
  2080. HREF="http://www.php.net/mailing-lists.php"
  2081. TARGET="_top"
  2082. >installation
  2083.     mailing list</A
  2084. >. If you choose to go on the simpler route, then
  2085.     <A
  2086. HREF="http://www.hotscripts.com/PHP/Software_and_Servers/Installation_Kits/"
  2087. TARGET="_top"
  2088. >locate a pre-configured package</A
  2089. >
  2090.     for your operating system,  which automatically installs all of these
  2091.     with just a few mouse clicks. It is easy to setup a web server with PHP
  2092.     support on any operating system, including MacOSX, Linux and Windows.
  2093.     On Linux, you may find <A
  2094. HREF="http://www.rpmfind.net/"
  2095. TARGET="_top"
  2096. >rpmfind</A
  2097. > and
  2098.     <A
  2099. HREF="http://rpm.pbone.net/"
  2100. TARGET="_top"
  2101. >PBone</A
  2102. > helpful for
  2103.     locating RPMs. You may also want to visit <A
  2104. HREF="http://www.apt-get.org/"
  2105. TARGET="_top"
  2106. >apt-get</A
  2107. > to find packages for Debian.
  2108.    </P
  2109. ></DIV
  2110. ><DIV
  2111. CLASS="sect1"
  2112. ><HR><H2
  2113. CLASS="sect1"
  2114. ><A
  2115. NAME="tutorial.firstpage"
  2116. >Your first PHP-enabled page</A
  2117. ></H2
  2118. ><P
  2119. >     Create a file named <TT
  2120. CLASS="filename"
  2121. >hello.php</TT
  2122. > and put it
  2123.     in your web server's root directory (<VAR
  2124. CLASS="varname"
  2125. >DOCUMENT_ROOT</VAR
  2126. >) 
  2127.     with the following content:
  2128.    </P
  2129. ><P
  2130. >     <TABLE
  2131. WIDTH="100%"
  2132. BORDER="0"
  2133. CELLPADDING="0"
  2134. CELLSPACING="0"
  2135. CLASS="EXAMPLE"
  2136. ><TR
  2137. ><TD
  2138. ><DIV
  2139. CLASS="example"
  2140. ><A
  2141. NAME="AEN174"
  2142. ></A
  2143. ><P
  2144. ><B
  2145. >Example 2-1. Our first PHP script: <TT
  2146. CLASS="filename"
  2147. >hello.php</TT
  2148. ></B
  2149. ></P
  2150. ><TABLE
  2151. BORDER="0"
  2152. BGCOLOR="#E0E0E0"
  2153. CELLPADDING="5"
  2154. ><TR
  2155. ><TD
  2156. ><PRE
  2157. CLASS="php"
  2158. ><html>
  2159.  <head>
  2160.   <title>PHP Test</title>
  2161.  </head>
  2162.  <body>
  2163.  <?php echo '<p>Hello World</p>'; ?>
  2164.  </body>
  2165. </html></PRE
  2166. ></TD
  2167. ></TR
  2168. ></TABLE
  2169. ><P
  2170. >       Use your browser to access the file with your web server's URL, ending
  2171.       with the "/hello.php" file reference. When developing locally this
  2172.       URL will be something like <VAR
  2173. CLASS="literal"
  2174. >http://localhost/hello.php</VAR
  2175.       or <VAR
  2176. CLASS="literal"
  2177. >http://127.0.0.1/hello.php</VAR
  2178. > but this depends on the
  2179.       web server's configuration. If everything is configured correctly, this
  2180.       file will be parsed by PHP and the following output will be sent to
  2181.       your browser:
  2182.      </P
  2183. ><TABLE
  2184. BORDER="0"
  2185. BGCOLOR="#E0E0E0"
  2186. CELLPADDING="5"
  2187. ><TR
  2188. ><TD
  2189. ><PRE
  2190. CLASS="html"
  2191. ><html>
  2192.  <head>
  2193.   <title>PHP Test</title>
  2194.  </head>
  2195.  <body>
  2196.  <p>Hello World</p>
  2197.  </body>
  2198. </html></PRE
  2199. ></TD
  2200. ></TR
  2201. ></TABLE
  2202. ></DIV
  2203. ></TD
  2204. ></TR
  2205. ></TABLE
  2206. >
  2207.    </P
  2208. ><P
  2209. >     This program is extremely simple and you really did not need to use
  2210.     PHP to create a page like this. All it does is display:
  2211.     <VAR
  2212. CLASS="literal"
  2213. >Hello World</VAR
  2214. > using the PHP <A
  2215. HREF="#function.echo"
  2216. ><B
  2217. CLASS="function"
  2218. >echo()</B
  2219. ></A
  2220. >
  2221.     statement. Note that the file <SPAN
  2222. CLASS="emphasis"
  2223. ><I
  2224. CLASS="emphasis"
  2225. >does not need to be executable</I
  2226. ></SPAN
  2227. >
  2228.     or special in any way. The server finds out that this file needs to be interpreted
  2229.     by PHP because you used the ".php" extension, which the server is configured
  2230.     to pass on to PHP. Think of this as a normal HTML file which happens to have
  2231.     a set of special tags available to you that do a lot of interesting things.
  2232.    </P
  2233. ><P
  2234. >     If you tried this example and it did not output anything, it prompted 
  2235.     for download, or you see the whole file as text, chances are that the 
  2236.     server you are on does not have PHP enabled, or is not configured properly.
  2237.     Ask your administrator to enable it for you using the
  2238.     <A
  2239. HREF="#install"
  2240. >Installation</A
  2241. > chapter 
  2242.     of the manual. If you are developing locally, also read the
  2243.     installation chapter to make sure everything is configured
  2244.     properly. Make sure that you access the file via http with the server
  2245.     providing you the output. If you just call up the file from your file
  2246.     system, then it will not be parsed by PHP. If the problems persist anyway,
  2247.     do not hesitate to use one of the many
  2248.     <A
  2249. HREF="http://www.php.net/support.php"
  2250. TARGET="_top"
  2251. >PHP support</A
  2252. > options.
  2253.    </P
  2254. ><P
  2255. >     The point of the example is to show the special PHP tag format.
  2256.     In this example we used <VAR
  2257. CLASS="literal"
  2258. ><?php</VAR
  2259. > to indicate the
  2260.     start of a PHP tag. Then we put the PHP statement and left PHP mode by
  2261.     adding the closing tag, <VAR
  2262. CLASS="literal"
  2263. >?></VAR
  2264. >. You may jump in
  2265.     and out of PHP mode in an HTML file like this anywhere you want.  For more
  2266.     details, read the manual section on the <A
  2267. HREF="#language.basic-syntax"
  2268. >     basic PHP syntax</A
  2269. >.
  2270.    </P
  2271. ><DIV
  2272. CLASS="note"
  2273. ><BLOCKQUOTE
  2274. CLASS="note"
  2275. ><P
  2276. ><B
  2277. >A Note on Text Editors: </B
  2278. >
  2279.      There are many text editors and Integrated Development Environments (IDEs)
  2280.      that you can use to create, edit and manage PHP files. A partial list of 
  2281.      these tools is maintained at <A
  2282. HREF="http://www.thelinuxconsultancy.co.uk/phpeditors/"
  2283. TARGET="_top"
  2284. >PHP Editors
  2285.      List</A
  2286. >. If you wish to recommend an editor, please visit the above
  2287.      page and ask the page maintainer to add the editor to the list.  Having
  2288.      an editor with syntax highlighting can be helpful.
  2289.     </P
  2290. ></BLOCKQUOTE
  2291. ></DIV
  2292. ><DIV
  2293. CLASS="note"
  2294. ><BLOCKQUOTE
  2295. CLASS="note"
  2296. ><P
  2297. ><B
  2298. >A Note on Word Processors: </B
  2299. >
  2300.      Word processors such as StarOffice Writer, Microsoft Word and Abiword are
  2301.      not optimal for editing PHP files.  If you wish to use one for this 
  2302.      test script, you must ensure that you save the file as <SPAN
  2303. CLASS="emphasis"
  2304. ><I
  2305. CLASS="emphasis"
  2306. >plain
  2307.      text</I
  2308. ></SPAN
  2309. > or PHP will not be able to read and execute the script.
  2310.     </P
  2311. ></BLOCKQUOTE
  2312. ></DIV
  2313. ><DIV
  2314. CLASS="note"
  2315. ><BLOCKQUOTE
  2316. CLASS="note"
  2317. ><P
  2318. ><B
  2319. >A Note on Windows Notepad: </B
  2320. >
  2321.      If you are writing your PHP scripts using Windows Notepad, you will need
  2322.      to ensure that your files are saved with the .php extension. (Notepad adds
  2323.      a .txt extension to files automatically unless you take one of the
  2324.      following steps to prevent it.)  When you save the file and are prompted
  2325.      to provide a name for the file, place the filename in quotes 
  2326.      (i.e. "<TT
  2327. CLASS="filename"
  2328. >hello.php</TT
  2329. >").  Alternatively, you can click on the 
  2330.      'Text Documents' drop-down menu in the 'Save' dialog box and change the setting 
  2331.      to "All Files". You can then enter your filename without quotes.
  2332.     </P
  2333. ></BLOCKQUOTE
  2334. ></DIV
  2335. ><P
  2336. >     Now that you have successfully created a working PHP script, it is
  2337.     time to create the most famous PHP script!  Make a call to the
  2338.     <A
  2339. HREF="#function.phpinfo"
  2340. ><B
  2341. CLASS="function"
  2342. >phpinfo()</B
  2343. ></A
  2344. > function and you will see a lot of useful
  2345.     information about your system and setup such as available
  2346.     <A
  2347. HREF="#language.variables.predefined"
  2348. >predefined variables</A
  2349. >, 
  2350.     loaded PHP modules, and <A
  2351. HREF="#configuration"
  2352. >configuration</A
  2353. >
  2354.     settings. Take some time and review this important information.
  2355.    </P
  2356. ><P
  2357. >     <TABLE
  2358. WIDTH="100%"
  2359. BORDER="0"
  2360. CELLPADDING="0"
  2361. CELLSPACING="0"
  2362. CLASS="EXAMPLE"
  2363. ><TR
  2364. ><TD
  2365. ><DIV
  2366. CLASS="example"
  2367. ><A
  2368. NAME="AEN210"
  2369. ></A
  2370. ><P
  2371. ><B
  2372. >Example 2-2. Get system information from PHP</B
  2373. ></P
  2374. ><TABLE
  2375. BORDER="0"
  2376. BGCOLOR="#E0E0E0"
  2377. CELLPADDING="5"
  2378. ><TR
  2379. ><TD
  2380. ><PRE
  2381. CLASS="php"
  2382. ><?php phpinfo(); ?></PRE
  2383. ></TD
  2384. ></TR
  2385. ></TABLE
  2386. ></DIV
  2387. ></TD
  2388. ></TR
  2389. ></TABLE
  2390. >
  2391.    </P
  2392. ></DIV
  2393. ><DIV
  2394. CLASS="sect1"
  2395. ><HR><H2
  2396. CLASS="sect1"
  2397. ><A
  2398. NAME="tutorial.useful"
  2399. >Something Useful</A
  2400. ></H2
  2401. ><P
  2402. >     Let us do something more useful now. We are going to check
  2403.     what sort of browser the visitor is using.
  2404.     For that, we check the user agent string the browser
  2405.     sends as part of the HTTP request. This information is stored in a <A
  2406. HREF="#language.variables"
  2407. >variable</A
  2408. >. Variables always start
  2409.     with a dollar-sign in PHP. The variable we are interested in right now 
  2410.     is <VAR
  2411. CLASS="varname"
  2412. >$_SERVER['HTTP_USER_AGENT']</VAR
  2413. >.
  2414.    </P
  2415. ><DIV
  2416. CLASS="note"
  2417. ><BLOCKQUOTE
  2418. CLASS="note"
  2419. ><P
  2420. ><B
  2421. >Note: </B
  2422. >
  2423.      <A
  2424. HREF="#reserved.variables.server"
  2425. >$_SERVER</A
  2426. > is a 
  2427.      special reserved PHP variable that contains all web server information.
  2428.      It is known as an autoglobal (or superglobal).  See the related manual page on
  2429.      <A
  2430. HREF="#language.variables.superglobals"
  2431. >superglobals</A
  2432. >
  2433.      for more information.  These special variables were introduced in PHP 
  2434.      <A
  2435. HREF="http://www.php.net/release_4_1_0.php"
  2436. TARGET="_top"
  2437. >4.1.0</A
  2438. >.  Before this time, we used
  2439.      the older <VAR
  2440. CLASS="varname"
  2441. >$HTTP_*_VARS</VAR
  2442. > arrays instead,
  2443.      such as <VAR
  2444. CLASS="varname"
  2445. >$HTTP_SERVER_VARS</VAR
  2446. >.  Although deprecated, 
  2447.      these older variables still exist.  (See also the note on
  2448.      <A
  2449. HREF="#tutorial.oldcode"
  2450. >old code</A
  2451. >.)
  2452.     </P
  2453. ></BLOCKQUOTE
  2454. ></DIV
  2455. ><P
  2456. >     To display this variable, you can simply do:
  2457.    </P
  2458. ><P
  2459. >     <TABLE
  2460. WIDTH="100%"
  2461. BORDER="0"
  2462. CELLPADDING="0"
  2463. CELLSPACING="0"
  2464. CLASS="EXAMPLE"
  2465. ><TR
  2466. ><TD
  2467. ><DIV
  2468. CLASS="example"
  2469. ><A
  2470. NAME="AEN228"
  2471. ></A
  2472. ><P
  2473. ><B
  2474. >Example 2-3. Printing a variable (Array element)</B
  2475. ></P
  2476. ><TABLE
  2477. BORDER="0"
  2478. BGCOLOR="#E0E0E0"
  2479. CELLPADDING="5"
  2480. ><TR
  2481. ><TD
  2482. ><PRE
  2483. CLASS="php"
  2484. ><?php echo $_SERVER['HTTP_USER_AGENT']; ?></PRE
  2485. ></TD
  2486. ></TR
  2487. ></TABLE
  2488. ><P
  2489. >      A sample output of this script may be:
  2490.     </P
  2491. ><TABLE
  2492. BORDER="0"
  2493. BGCOLOR="#E0E0E0"
  2494. CELLPADDING="5"
  2495. ><TR
  2496. ><TD
  2497. ><PRE
  2498. CLASS="html"
  2499. >Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)</PRE
  2500. ></TD
  2501. ></TR
  2502. ></TABLE
  2503. ></DIV
  2504. ></TD
  2505. ></TR
  2506. ></TABLE
  2507. >
  2508.    </P
  2509. ><P
  2510. >     There are many <A
  2511. HREF="#language.types"
  2512. >types</A
  2513. > of 
  2514.     variables available in PHP.  In the above example we printed 
  2515.     an <A
  2516. HREF="#language.types.array"
  2517. >Array</A
  2518. > element.
  2519.     Arrays can be very useful.
  2520.    </P
  2521. ><P
  2522. >     <VAR
  2523. CLASS="varname"
  2524. >$_SERVER</VAR
  2525. > is just one variable that PHP automatically 
  2526.     makes available to you. A list can be seen in the 
  2527.     <A
  2528. HREF="#reserved.variables"
  2529. >Reserved Variables</A
  2530. > section 
  2531.     of the manual or you can get a complete list of them by looking at
  2532.     the output of the <A
  2533. HREF="#function.phpinfo"
  2534. ><B
  2535. CLASS="function"
  2536. >phpinfo()</B
  2537. ></A
  2538. > function used in the
  2539.     example in the previous section.
  2540.    </P
  2541. ><P
  2542. >     You can put multiple PHP statements inside a PHP tag and create
  2543.     little blocks of code that do more than just a single echo.
  2544.     For example, if you want to check for Internet Explorer you
  2545.     can do this:
  2546.    </P
  2547. ><P
  2548. >     <TABLE
  2549. WIDTH="100%"
  2550. BORDER="0"
  2551. CELLPADDING="0"
  2552. CELLSPACING="0"
  2553. CLASS="EXAMPLE"
  2554. ><TR
  2555. ><TD
  2556. ><DIV
  2557. CLASS="example"
  2558. ><A
  2559. NAME="AEN242"
  2560. ></A
  2561. ><P
  2562. ><B
  2563. >Example 2-4. Example using <A
  2564. HREF="#language.control-structures"
  2565. >control 
  2566.      structures</A
  2567. > and <A
  2568. HREF="#language.functions"
  2569. >functions</A
  2570. ></B
  2571. ></P
  2572. ><TABLE
  2573. BORDER="0"
  2574. BGCOLOR="#E0E0E0"
  2575. CELLPADDING="5"
  2576. ><TR
  2577. ><TD
  2578. ><PRE
  2579. CLASS="php"
  2580. ><?php
  2581. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
  2582.     echo 'You are using Internet Explorer.<br />';
  2583. }
  2584. ?></PRE
  2585. ></TD
  2586. ></TR
  2587. ></TABLE
  2588. ><P
  2589. >       A sample output of this script may be:
  2590.      </P
  2591. ><TABLE
  2592. BORDER="0"
  2593. BGCOLOR="#E0E0E0"
  2594. CELLPADDING="5"
  2595. ><TR
  2596. ><TD
  2597. ><PRE
  2598. CLASS="html"
  2599. >You are using Internet Explorer.<br /></PRE
  2600. ></TD
  2601. ></TR
  2602. ></TABLE
  2603. ></DIV
  2604. ></TD
  2605. ></TR
  2606. ></TABLE
  2607. >
  2608.    </P
  2609. ><P
  2610. >     Here we introduce a couple of new concepts. We have an 
  2611.     <A
  2612. HREF="#control-structures.if"
  2613. >if</A
  2614. > statement.
  2615.     If you are familiar with the basic syntax used by the C
  2616.     language, this should look logical to you. Otherwise, you
  2617.     should probably pick up an introductory PHP book and read the first
  2618.     couple of chapters, or read the <A
  2619. HREF="#langref"
  2620. >Language
  2621.     Reference</A
  2622. > part of the manual. You can find a list of PHP books
  2623.     at <A
  2624. HREF="http://www.php.net/books.php"
  2625. TARGET="_top"
  2626. >http://www.php.net/books.php</A
  2627. >.
  2628.    </P
  2629. ><P
  2630. >     The second concept we introduced was the <A
  2631. HREF="#function.strpos"
  2632. ><B
  2633. CLASS="function"
  2634. >strpos()</B
  2635. ></A
  2636. >
  2637.     function call. <A
  2638. HREF="#function.strpos"
  2639. ><B
  2640. CLASS="function"
  2641. >strpos()</B
  2642. ></A
  2643. > is a function built into
  2644.     PHP which searches a string for another string. In this case we are
  2645.     looking for <VAR
  2646. CLASS="literal"
  2647. >'MSIE'</VAR
  2648. > (so-called needle) inside
  2649.     <VAR
  2650. CLASS="varname"
  2651. >$_SERVER['HTTP_USER_AGENT']</VAR
  2652. > (so-called haystack).  If
  2653.     the needle is found inside the haystack, the function returns the position
  2654.     of the needle relative to the start of the haystack.  Otherwise, it
  2655.     returns <TT
  2656. CLASS="constant"
  2657. ><B
  2658. >FALSE</B
  2659. ></TT
  2660. >.  If it does not return <TT
  2661. CLASS="constant"
  2662. ><B
  2663. >FALSE</B
  2664. ></TT
  2665. >, the <A
  2666. HREF="#control-structures.if"
  2667. >if</A
  2668. > expression evaluates to <TT
  2669. CLASS="constant"
  2670. ><B
  2671. >TRUE</B
  2672. ></TT
  2673. >
  2674.     and the code within its {braces} is executed. Otherwise, the code is not
  2675.     run. Feel free to create similar examples, 
  2676.     with <A
  2677. HREF="#control-structures.if"
  2678. >if</A
  2679. >, 
  2680.     <A
  2681. HREF="#control-structures.else"
  2682. >else</A
  2683. >, and other 
  2684.     functions such as <A
  2685. HREF="#function.strtoupper"
  2686. ><B
  2687. CLASS="function"
  2688. >strtoupper()</B
  2689. ></A
  2690. > and 
  2691.     <A
  2692. HREF="#function.strlen"
  2693. ><B
  2694. CLASS="function"
  2695. >strlen()</B
  2696. ></A
  2697. >.  Each related manual page contains examples 
  2698.     too.  If you are unsure how to use functions, you will want to read both
  2699.     the manual page on <A
  2700. HREF="#about.prototypes"
  2701. >how to read a
  2702.     function definition</A
  2703. > and the section about  
  2704.     <A
  2705. HREF="#language.functions"
  2706. >PHP functions</A
  2707. >.
  2708.    </P
  2709. ><P
  2710. >     We can take this a step further and show how you can jump in and out
  2711.     of PHP mode even in the middle of a PHP block:
  2712.    </P
  2713. ><P
  2714. >     <TABLE
  2715. WIDTH="100%"
  2716. BORDER="0"
  2717. CELLPADDING="0"
  2718. CELLSPACING="0"
  2719. CLASS="EXAMPLE"
  2720. ><TR
  2721. ><TD
  2722. ><DIV
  2723. CLASS="example"
  2724. ><A
  2725. NAME="AEN270"
  2726. ></A
  2727. ><P
  2728. ><B
  2729. >Example 2-5. Mixing both HTML and PHP modes</B
  2730. ></P
  2731. ><TABLE
  2732. BORDER="0"
  2733. BGCOLOR="#E0E0E0"
  2734. CELLPADDING="5"
  2735. ><TR
  2736. ><TD
  2737. ><PRE
  2738. CLASS="php"
  2739. ><?php
  2740. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
  2741. ?>
  2742. <h3>strpos() must have returned non-false</h3>
  2743. <p>You are using Internet Explorer</p>
  2744. <?php
  2745. } else {
  2746. ?>
  2747. <h3>strpos() must have returned false</h3>
  2748. <p>You are not using Internet Explorer</p>
  2749. <?php
  2750. }
  2751. ?></PRE
  2752. ></TD
  2753. ></TR
  2754. ></TABLE
  2755. ><P
  2756. >       A sample output of this script may be:
  2757.      </P
  2758. ><TABLE
  2759. BORDER="0"
  2760. BGCOLOR="#E0E0E0"
  2761. CELLPADDING="5"
  2762. ><TR
  2763. ><TD
  2764. ><PRE
  2765. CLASS="html"
  2766. ><h3>strpos() must have returned non-false</h3>
  2767. <p>You are using Internet Explorer</p></PRE
  2768. ></TD
  2769. ></TR
  2770. ></TABLE
  2771. ></DIV
  2772. ></TD
  2773. ></TR
  2774. ></TABLE
  2775. >
  2776.    </P
  2777. ><P
  2778. >     Instead of using a PHP echo statement to output something, we jumped out
  2779.     of PHP mode and just sent straight HTML. The important and powerful point
  2780.     to note here is that the logical flow of the script remains intact. Only
  2781.     one of the HTML blocks will end up getting sent to the viewer depending on
  2782.     the result of <A
  2783. HREF="#function.strpos"
  2784. ><B
  2785. CLASS="function"
  2786. >strpos()</B
  2787. ></A
  2788. >.  In other words, it depends on
  2789.     whether the string <VAR
  2790. CLASS="literal"
  2791. >MSIE</VAR
  2792. > was found or not.
  2793.    </P
  2794. ></DIV
  2795. ><DIV
  2796. CLASS="sect1"
  2797. ><HR><H2
  2798. CLASS="sect1"
  2799. ><A
  2800. NAME="tutorial.forms"
  2801. >Dealing with Forms</A
  2802. ></H2
  2803. ><P
  2804. >     One of the most powerful features of PHP is the way it handles HTML
  2805.     forms. The basic concept that is important to understand is that any
  2806.     form element will automatically be available to your PHP 
  2807.     scripts.  Please read the manual section on
  2808.     <A
  2809. HREF="#language.variables.external"
  2810. >Variables from outside 
  2811.     of PHP</A
  2812. > for more information and examples on using forms 
  2813.     with PHP.  Here is an example HTML form:
  2814.    </P
  2815. ><P
  2816. >     <TABLE
  2817. WIDTH="100%"
  2818. BORDER="0"
  2819. CELLPADDING="0"
  2820. CELLSPACING="0"
  2821. CLASS="EXAMPLE"
  2822. ><TR
  2823. ><TD
  2824. ><DIV
  2825. CLASS="example"
  2826. ><A
  2827. NAME="AEN283"
  2828. ></A
  2829. ><P
  2830. ><B
  2831. >Example 2-6. A simple HTML form</B
  2832. ></P
  2833. ><TABLE
  2834. BORDER="0"
  2835. BGCOLOR="#E0E0E0"
  2836. CELLPADDING="5"
  2837. ><TR
  2838. ><TD
  2839. ><PRE
  2840. CLASS="html"
  2841. ><form action="action.php" method="post">
  2842.  <p>Your name: <input type="text" name="name" /></p>
  2843.  <p>Your age: <input type="text" name="age" /></p>
  2844.  <p><input type="submit" /></p>
  2845. </form></PRE
  2846. ></TD
  2847. ></TR
  2848. ></TABLE
  2849. ></DIV
  2850. ></TD
  2851. ></TR
  2852. ></TABLE
  2853. >
  2854.    </P
  2855. ><P
  2856. >     There is nothing special about this form. It is a straight HTML form
  2857.     with no special tags of any kind. When the user fills in this form
  2858.     and hits the submit button, the <TT
  2859. CLASS="filename"
  2860. >action.php</TT
  2861. > page
  2862.     is called. In this file you would write something like this:
  2863.    </P
  2864. ><P
  2865. >     <TABLE
  2866. WIDTH="100%"
  2867. BORDER="0"
  2868. CELLPADDING="0"
  2869. CELLSPACING="0"
  2870. CLASS="EXAMPLE"
  2871. ><TR
  2872. ><TD
  2873. ><DIV
  2874. CLASS="example"
  2875. ><A
  2876. NAME="AEN289"
  2877. ></A
  2878. ><P
  2879. ><B
  2880. >Example 2-7. Printing data from our form</B
  2881. ></P
  2882. ><TABLE
  2883. BORDER="0"
  2884. BGCOLOR="#E0E0E0"
  2885. CELLPADDING="5"
  2886. ><TR
  2887. ><TD
  2888. ><PRE
  2889. CLASS="php"
  2890. >Hi <?php echo $_POST['name']; ?>.
  2891. You are <?php echo $_POST['age']; ?> years old.</PRE
  2892. ></TD
  2893. ></TR
  2894. ></TABLE
  2895. ><P
  2896. >       A sample output of this script may be:
  2897.      </P
  2898. ><TABLE
  2899. BORDER="0"
  2900. BGCOLOR="#E0E0E0"
  2901. CELLPADDING="5"
  2902. ><TR
  2903. ><TD
  2904. ><PRE
  2905. CLASS="html"
  2906. >Hi Joe. You are 22 years old.</PRE
  2907. ></TD
  2908. ></TR
  2909. ></TABLE
  2910. ></DIV
  2911. ></TD
  2912. ></TR
  2913. ></TABLE
  2914. >
  2915.    </P
  2916. ><P
  2917. >     It should be obvious what this does. There is nothing more to it.
  2918.     The <VAR
  2919. CLASS="varname"
  2920. >$_POST['name']</VAR
  2921. > and <VAR
  2922. CLASS="varname"
  2923. >$_POST['age']</VAR
  2924. >
  2925.     variables are automatically set for you by PHP.  Earlier we
  2926.     used the <VAR
  2927. CLASS="varname"
  2928. >$_SERVER</VAR
  2929. > autoglobal; above we just 
  2930.     introduced the <A
  2931. HREF="#reserved.variables.post"
  2932. >$_POST</A
  2933. >
  2934.     autoglobal which contains all POST data.  Notice how the
  2935.     <SPAN
  2936. CLASS="emphasis"
  2937. ><I
  2938. CLASS="emphasis"
  2939. >method</I
  2940. ></SPAN
  2941. > of our form is POST.  If we used the 
  2942.     method <SPAN
  2943. CLASS="emphasis"
  2944. ><I
  2945. CLASS="emphasis"
  2946. >GET</I
  2947. ></SPAN
  2948. > then our form information would live in 
  2949.     the <A
  2950. HREF="#reserved.variables.get"
  2951. >$_GET</A
  2952. > autoglobal instead.
  2953.     You may also use the <A
  2954. HREF="#reserved.variables.request"
  2955. >$_REQUEST</A
  2956. >
  2957.     autoglobal, if you do not care about the source of your request data. It 
  2958.     contains the merged information of GET, POST and COOKIE data.  Also see the 
  2959.     <A
  2960. HREF="#function.import-request-variables"
  2961. ><B
  2962. CLASS="function"
  2963. >import_request_variables()</B
  2964. ></A
  2965. > function.
  2966.    </P
  2967. ><P
  2968. >     You can also deal with XForms input in PHP, although you will find yourself
  2969.     comfortable with the well supported HTML forms for quite some time.
  2970.     While working with XForms is not for beginners, you might be interested
  2971.     in them. We also have a <A
  2972. HREF="#features.xforms"
  2973. >short introduction
  2974.     to handling data received from XForms</A
  2975. > in our features section. 
  2976.    </P
  2977. ></DIV
  2978. ><DIV
  2979. CLASS="sect1"
  2980. ><HR><H2
  2981. CLASS="sect1"
  2982. ><A
  2983. NAME="tutorial.oldcode"
  2984. >Using old code with new versions of PHP</A
  2985. ></H2
  2986. ><P
  2987. >     Now that PHP has grown to be a popular scripting language, there are
  2988.     a lot of public repositories and libraries containing code you can reuse.
  2989.     The PHP developers have largely tried to preserve backwards compatibility, 
  2990.     so a script written for an older version will run (ideally) without changes
  2991.     in a newer version of PHP. In practice, some changes will usually be needed.
  2992.    </P
  2993. ><P
  2994. >     Two of the most important recent changes that affect old code are:
  2995.     <P
  2996. ></P
  2997. ><UL
  2998. ><LI
  2999. ><P
  3000. >        The deprecation of the old <VAR
  3001. CLASS="varname"
  3002. >$HTTP_*_VARS</VAR
  3003. > arrays
  3004.        (which need to be indicated as global when used inside a function or
  3005.        method).  The following 
  3006.        <A
  3007. HREF="#language.variables.superglobals"
  3008. >autoglobal arrays</A
  3009. >
  3010.        were introduced in PHP <A
  3011. HREF="http://www.php.net/release_4_1_0.php"
  3012. TARGET="_top"
  3013. >4.1.0</A
  3014. >. 
  3015.        They are: <VAR
  3016. CLASS="varname"
  3017. >$_GET</VAR
  3018. >, <VAR
  3019. CLASS="varname"
  3020. >$_POST</VAR
  3021. >, 
  3022.        <VAR
  3023. CLASS="varname"
  3024. >$_COOKIE</VAR
  3025. >, <VAR
  3026. CLASS="varname"
  3027. >$_SERVER</VAR
  3028. >, 
  3029.        <VAR
  3030. CLASS="varname"
  3031. >$_FILES</VAR
  3032. >, <VAR
  3033. CLASS="varname"
  3034. >$_ENV</VAR
  3035. >,
  3036.        <VAR
  3037. CLASS="varname"
  3038. >$_REQUEST</VAR
  3039. >, and <VAR
  3040. CLASS="varname"
  3041. >$_SESSION</VAR
  3042. >.  The
  3043.        older <VAR
  3044. CLASS="varname"
  3045. >$HTTP_*_VARS</VAR
  3046. > arrays, such as
  3047.        <VAR
  3048. CLASS="varname"
  3049. >$HTTP_POST_VARS</VAR
  3050. >, still exist as they have since PHP 3. 
  3051.        As of PHP 5.0.0, the long PHP 
  3052. <A
  3053. HREF="#language.variables.predefined"
  3054. >predefined variable</A
  3055. >
  3056. arrays may be disabled with the 
  3057. <A
  3058. HREF="#ini.register-long-arrays"
  3059. >register_long_arrays</A
  3060. >
  3061. directive.
  3062.       </P
  3063. ></LI
  3064. ><LI
  3065. ><P
  3066. >        External variables are no longer registered in the global scope by
  3067.        default.  In other words, as of PHP
  3068.        <A
  3069. HREF="http://www.php.net/release_4_2_0.php"
  3070. TARGET="_top"
  3071. >4.2.0</A
  3072. > the PHP directive 
  3073.        <A
  3074. HREF="#ini.register-globals"
  3075. >register_globals</A
  3076. > is 
  3077.        <SPAN
  3078. CLASS="emphasis"
  3079. ><I
  3080. CLASS="emphasis"
  3081. >off</I
  3082. ></SPAN
  3083. > by default in <TT
  3084. CLASS="filename"
  3085. >php.ini</TT
  3086. >. The preferred 
  3087.        method of accessing these values is via the autoglobal arrays mentioned
  3088.        above.  Older scripts, books, and tutorials may rely on this 
  3089.        directive being on.  If it were on, for example, one could use 
  3090.        <VAR
  3091. CLASS="varname"
  3092. >$id</VAR
  3093. > from the URL 
  3094.        <VAR
  3095. CLASS="literal"
  3096. >http://www.example.com/foo.php?id=42</VAR
  3097. >.  Whether on 
  3098.        or off, <VAR
  3099. CLASS="varname"
  3100. >$_GET['id']</VAR
  3101. > is available.
  3102.       </P
  3103. ></LI
  3104. ></UL
  3105. >
  3106.     For more details on these changes, see the section on 
  3107.     <A
  3108. HREF="#language.variables.predefined"
  3109. >predefined variables</A
  3110. >
  3111.     and links therein.
  3112.    </P
  3113. ></DIV
  3114. ><DIV
  3115. CLASS="sect1"
  3116. ><HR><H2
  3117. CLASS="sect1"
  3118. ><A
  3119. NAME="tutorial.whatsnext"
  3120. >What's next?</A
  3121. ></H2
  3122. ><P
  3123. >     With your new knowledge you should be able to understand most of 
  3124.     the manual and also the various example scripts available in the
  3125.     example archives. You can also find other examples on the php.net
  3126.     websites in the links section:
  3127.     <A
  3128. HREF="http://www.php.net/links.php"
  3129. TARGET="_top"
  3130. >http://www.php.net/links.php</A
  3131. >.
  3132.    </P
  3133. ><P
  3134. >     To view various slide presentations that show more of what PHP can do, 
  3135.     see the PHP Conference Material Sites: <A
  3136. HREF="http://conf.php.net/"
  3137. TARGET="_top"
  3138. >     http://conf.php.net/</A
  3139. > and <A
  3140. HREF="http://talks.php.net/"
  3141. TARGET="_top"
  3142. >http://talks.php.net/
  3143.     </A
  3144. >
  3145.    </P
  3146. ></DIV
  3147. ></DIV
  3148. ></DIV
  3149. ><DIV
  3150. CLASS="PART"
  3151. ><A
  3152. NAME="install"
  3153. ></A
  3154. ><DIV
  3155. CLASS="TITLEPAGE"
  3156. ><H1
  3157. CLASS="title"
  3158. >II. Installation and Configuration</H1
  3159. ><DIV
  3160. CLASS="TOC"
  3161. ><DL
  3162. ><DT
  3163. ><B
  3164. >Table of Contents</B
  3165. ></DT
  3166. ><DT
  3167. >3. <A
  3168. HREF="#install.general"
  3169. >General Installation Considerations</A
  3170. ></DT
  3171. ><DT
  3172. >4. <A
  3173. HREF="#install.unix"
  3174. >Installation on Unix systems</A
  3175. ></DT
  3176. ><DT
  3177. >5. <A
  3178. HREF="#install.macosx"
  3179. >Installation on Mac OS X</A
  3180. ></DT
  3181. ><DT
  3182. >6. <A
  3183. HREF="#install.windows"
  3184. >Installation on Windows systems</A
  3185. ></DT
  3186. ><DT
  3187. >7. <A
  3188. HREF="#install.pecl"
  3189. >Installation of PECL extensions</A
  3190. ></DT
  3191. ><DT
  3192. >8. <A
  3193. HREF="#install.problems"
  3194. >Problems?</A
  3195. ></DT
  3196. ><DT
  3197. >9. <A
  3198. HREF="#configuration"
  3199. >Runtime Configuration</A
  3200. ></DT
  3201. ></DL
  3202. ></DIV
  3203. ></DIV
  3204. ><DIV
  3205. CLASS="chapter"
  3206. ><HR><H1
  3207. ><A
  3208. NAME="install.general"
  3209. >Chapter 3. General Installation Considerations</A
  3210. ></H1
  3211. ><P
  3212. >     Before starting the installation, first you need to know what do you
  3213.     want to use PHP for. There are three main fields you
  3214.     can use PHP, as described in the
  3215.     <A
  3216. HREF="#intro-whatcando"
  3217. >What can PHP do?</A
  3218. >
  3219.     section:
  3220.     <P
  3221. ></P
  3222. ><UL
  3223. ><LI
  3224. ><P
  3225. >Server-side scripting</P
  3226. ></LI
  3227. ><LI
  3228. ><P
  3229. >Command line scripting</P
  3230. ></LI
  3231. ><LI
  3232. ><P
  3233. >Client-side GUI applications</P
  3234. ></LI
  3235. ></UL
  3236. >
  3237.    </P
  3238. ><P
  3239. >     For the first and most common form, you need three things:
  3240.     PHP itself, a web server and a web browser. You
  3241.     probably already have a web browser, and depending on
  3242.     your operating system setup, you may also have a web
  3243.     server (e.g. Apache on Linux and MacOS X; IIS on Windows).
  3244.     You may also rent webspace at a company. This way, you
  3245.     don't need to set up anything on your own, only write
  3246.     your PHP scripts, upload it to the server you rent, and
  3247.     see the results in your browser. 
  3248.    </P
  3249. ><P
  3250. >     While setting up the server and PHP on your own, you have
  3251.     two choices for the method of connecting PHP to the
  3252.     server. For many servers PHP has a direct module
  3253.     interface (also called SAPI). These servers include
  3254.     Apache, Microsoft Internet Information Server,
  3255.     Netscape and iPlanet servers. Many other servers
  3256.     have support for ISAPI, the Microsoft module
  3257.     interface (OmniHTTPd for example). If PHP has no
  3258.     module support for your web server, you can always
  3259.     use it as a CGI or FastCGI processor. This means you set up
  3260.     your server to use the CGI executable of
  3261.     PHP to process all PHP file requests on the server.
  3262.    </P
  3263. ><P
  3264. >     If you are also interested to use PHP for command line
  3265.     scripting (e.g. write scripts autogenerating some images
  3266.     for you offline, or processing text files depending
  3267.     on some arguments you pass to them), you always need
  3268.     the command line executable. For more information, read
  3269.     the section about <A
  3270. HREF="#features.commandline"
  3271. >writing
  3272.     command line PHP applications</A
  3273. >. In this case,
  3274.     you need no server and no browser.
  3275.    </P
  3276. ><P
  3277. >     With PHP you can also write desktop GUI applications
  3278.     using the PHP-GTK extension. This is a completely
  3279.     different approach than writing web pages, as you
  3280.     do not output any HTML, but manage windows and objects
  3281.     within them. For more information about PHP-GTK, please
  3282.     <A
  3283. HREF="http://gtk.php.net/"
  3284. TARGET="_top"
  3285. >visit the site dedicated to
  3286.     this extension</A
  3287. >. PHP-GTK is not included in the
  3288.     official PHP distribution.
  3289.    </P
  3290. ><P
  3291. >     From now on, this section deals with setting up PHP
  3292.     for web servers on Unix and Windows with server module
  3293.     interfaces and CGI executables. You will also find
  3294.     information on the command line executable in the
  3295.     following sections.
  3296.    </P
  3297. ><P
  3298. >     PHP source code and binary distributions for Windows can be
  3299.     found at
  3300.     <A
  3301. HREF="http://www.php.net/downloads.php"
  3302. TARGET="_top"
  3303. >http://www.php.net/downloads.php</A
  3304. >.
  3305.     We recommend you to choose a
  3306.     <A
  3307. HREF="http://www.php.net/mirrors.php"
  3308. TARGET="_top"
  3309. >mirror</A
  3310. > nearest
  3311.     to you for downloading the distributions.
  3312.    </P
  3313. ></DIV
  3314. ><DIV
  3315. CLASS="chapter"
  3316. ><HR><H1
  3317. ><A
  3318. NAME="install.unix"
  3319. >Chapter 4. Installation on Unix systems</A
  3320. ></H1
  3321. ><P
  3322. >     This section will guide you through the general configuration and
  3323.     installation of PHP on Unix systems. Be sure to investigate any
  3324.     sections specific to your platform or web server before you begin
  3325.     the process.
  3326.    </P
  3327. ><P
  3328. >     As our manual outlines in the <A
  3329. HREF="#install.general"
  3330. >General
  3331.     Installation Considerations</A
  3332. > section, we are mainly dealing with
  3333.     web centric setups of PHP in this section, although we will cover
  3334.     setting up PHP for command line usage as well.
  3335.    </P
  3336. ><P
  3337.     There are several ways to install PHP for the Unix platform, either
  3338.     with a compile and configure process, or through various
  3339.     pre-packaged methods. This documentation is mainly focused around
  3340.     the process of compiling and configuring PHP. Many Unix like systems
  3341.     have some sort of package installation system. This can assist in
  3342.     setting up a standard configuration, but if you need to have a
  3343.     different set of features (such as a secure server, or a different
  3344.     database driver), you may need to build PHP and/or your webserver.
  3345.     If you are unfamiliar with building and compiling your own software,
  3346.     it is worth checking to see whether somebody has already built a
  3347.     packaged version of PHP with the features you need.
  3348.    </P
  3349. ><P
  3350. >     Prerequisite knowledge and software for compiling:
  3351.     <P
  3352. ></P
  3353. ><UL
  3354. ><LI
  3355. ><P
  3356. >        Basic Unix skills (being able to operate "make" and a C
  3357.        compiler)
  3358.       </P
  3359. ></LI
  3360. ><LI
  3361. ><P
  3362. >        An ANSI C compiler
  3363.       </P
  3364. ></LI
  3365. ><LI
  3366. ><P
  3367. >        flex: Version 2.5.4
  3368.       </P
  3369. ></LI
  3370. ><LI
  3371. ><P
  3372. >        bison: Version 1.28 (preferred), 1.35, or 1.75
  3373.       </P
  3374. ></LI
  3375. ><LI
  3376. ><P
  3377. >        A web server
  3378.       </P
  3379. ></LI
  3380. ><LI
  3381. ><P
  3382. >        Any module specific components (such as gd, pdf libs, etc.)
  3383.       </P
  3384. ></LI
  3385. ></UL
  3386. >
  3387.    </P
  3388. ><P
  3389. >     The initial PHP setup and configuration process is controlled by the
  3390.     use of the commandline options of the <B
  3391. CLASS="command"
  3392. >configure</B
  3393. >
  3394.     script. You could get a list of all available options along with short
  3395.     explanations running <B
  3396. CLASS="command"
  3397. >./configure --help</B
  3398. >.
  3399.     Our manual documents the different options separately. You will
  3400.     find the <A
  3401. HREF="#configure"
  3402. >core options in the appendix</A
  3403. >,
  3404.     while the different extension specific options are descibed on the
  3405.     reference pages.
  3406.    </P
  3407. ><P
  3408. >     When PHP is configured, you are ready to build the module and/or
  3409.     executables. The command <B
  3410. CLASS="command"
  3411. >make</B
  3412. > should
  3413.     take care of this. If it fails and you can't figure out why, see
  3414.     the <A
  3415. HREF="#install.problems"
  3416. >Problems section</A
  3417. >.
  3418.    </P
  3419. ><DIV
  3420. CLASS="sect1"
  3421. ><HR><H2
  3422. CLASS="sect1"
  3423. ><A
  3424. NAME="install.unix.apache"
  3425. >Apache 1.3.x on Unix systems</A
  3426. ></H2
  3427. ><P
  3428. >      This section contains notes and hints specific to Apache installs
  3429.      of PHP on Unix platforms. We also have <A
  3430. HREF="#install.unix.apache2"
  3431. >instructions and notes for Apache 2
  3432.      on a separate page</A
  3433. >.
  3434.     </P
  3435. ><P
  3436. >      You can select arguments to add to the
  3437.      <B
  3438. CLASS="command"
  3439. >configure</B
  3440. > on line 10 below from the <A
  3441. HREF="#configure"
  3442. >list of core configure options</A
  3443. > and
  3444.      from extension specific options described at the respective
  3445.      places in the manual. The version numbers have been omitted here, to
  3446.      ensure the instructions are not incorrect. You will need to replace
  3447.      the 'xxx' here with the correct values from your files.
  3448.     </P
  3449. ><TABLE
  3450. WIDTH="100%"
  3451. BORDER="0"
  3452. CELLPADDING="0"
  3453. CELLSPACING="0"
  3454. CLASS="EXAMPLE"
  3455. ><TR
  3456. ><TD
  3457. ><DIV
  3458. CLASS="example"
  3459. ><A
  3460. NAME="install.unix.apache.example"
  3461. ></A
  3462. ><P
  3463. ><B
  3464. >Example 4-1. 
  3465.       Installation Instructions (Apache Shared Module Version) for PHP
  3466.      </B
  3467. ></P
  3468. ><TABLE
  3469. BORDER="0"
  3470. BGCOLOR="#E0E0E0"
  3471. CELLPADDING="5"
  3472. ><TR
  3473. ><TD
  3474. ><PRE
  3475. CLASS="screen"
  3476. >1.  gunzip apache_xxx.tar.gz
  3477. 2.  tar -xvf apache_xxx.tar
  3478. 3.  gunzip php-xxx.tar.gz
  3479. 4.  tar -xvf php-xxx.tar
  3480. 5.  cd apache_xxx
  3481. 6.  ./configure --prefix=/www --enable-module=so
  3482. 7.  make
  3483. 8.  make install
  3484. 9.  cd ../php-xxx
  3485.  
  3486. 10. Now, configure your PHP.  This is where you customize your PHP
  3487.     with various options, like which extensions will be enabled.  Do a
  3488.     ./configure --help for a list of available options.  In our example
  3489.     we'll do a simple configure with Apache 1 and MySQL support.  Your
  3490.     path to apxs may differ from our example.
  3491.  
  3492.       ./configure --with-mysql --with-apxs=/www/bin/apxs
  3493.  
  3494. 11. make
  3495. 12. make install
  3496.  
  3497.     If you decide to change your configure options after installation,
  3498.     you only need to repeat the last three steps. You only need to 
  3499.     restart apache for the new module to take effect. A recompile of
  3500.     Apache is not needed.
  3501.   
  3502.     Note that unless told otherwise, 'make install' will also install PEAR,
  3503.     various PHP tools such as phpize, install the PHP CLI, and more.
  3504.  
  3505. 13. Setup your php.ini file:
  3506.  
  3507.       cp php.ini-dist /usr/local/lib/php.ini
  3508.  
  3509.     You may edit your .ini file to set PHP options.  If you prefer your
  3510.     php.ini in another location, use --with-config-file-path=/some/path in
  3511.     step 10. 
  3512.     
  3513.     If you instead choose php.ini-recommended, be certain to read the list
  3514.     of changes within, as they affect how PHP behaves.
  3515.  
  3516. 14. Edit your httpd.conf to load the PHP module.  The path on the right hand
  3517.     side of the LoadModule statement must point to the path of the PHP
  3518.     module on your system.  The make install from above may have already
  3519.     added this for you, but be sure to check.
  3520.         
  3521.     For PHP 4:
  3522.             
  3523.       LoadModule php4_module libexec/libphp4.so
  3524.  
  3525.     For PHP 5:
  3526.                       
  3527.       LoadModule php5_module libexec/libphp5.so
  3528.       
  3529. 15. And in the AddModule section of httpd.conf, somewhere under the
  3530.     ClearModuleList, add this:
  3531.     
  3532.     For PHP 4:
  3533.     
  3534.       AddModule mod_php4.c
  3535.       
  3536.     For PHP 5:
  3537.     
  3538.       AddModule mod_php5.c
  3539.  
  3540. 16. Tell Apache to parse certain extensions as PHP.  For example,
  3541.     let's have Apache parse the .php extension as PHP.  You could
  3542.     have any extension(s) parse as PHP by simply adding more, with
  3543.     each separated by a space.  We'll add .phtml to demonstrate.
  3544.  
  3545.       AddType application/x-httpd-php .php .phtml
  3546.  
  3547.     It's also common to setup the .phps extension to show highlighted PHP
  3548.     source, this can be done with:
  3549.     
  3550.       AddType application/x-httpd-php-source .phps
  3551.  
  3552. 17. Use your normal procedure for starting the Apache server. (You must
  3553.     stop and restart the server, not just cause the server to reload by
  3554.     using a HUP or USR1 signal.)</PRE
  3555. ></TD
  3556. ></TR
  3557. ></TABLE
  3558. ></DIV
  3559. ></TD
  3560. ></TR
  3561. ></TABLE
  3562. ><P
  3563. >      Alternatively, to install PHP as a static object:
  3564.     </P
  3565. ><TABLE
  3566. WIDTH="100%"
  3567. BORDER="0"
  3568. CELLPADDING="0"
  3569. CELLSPACING="0"
  3570. CLASS="EXAMPLE"
  3571. ><TR
  3572. ><TD
  3573. ><DIV
  3574. CLASS="example"
  3575. ><A
  3576. NAME="install.unix.apache.example-static"
  3577. ></A
  3578. ><P
  3579. ><B
  3580. >Example 4-2. 
  3581.       Installation Instructions (Static Module Installation for Apache) for PHP
  3582.      </B
  3583. ></P
  3584. ><TABLE
  3585. BORDER="0"
  3586. BGCOLOR="#E0E0E0"
  3587. CELLPADDING="5"
  3588. ><TR
  3589. ><TD
  3590. ><PRE
  3591. CLASS="screen"
  3592. >1.  gunzip -c apache_1.3.x.tar.gz | tar xf -
  3593. 2.  cd apache_1.3.x
  3594. 3.  ./configure
  3595. 4.  cd ..
  3596.  
  3597. 5.  gunzip -c php-4.x.y.tar.gz | tar xf -
  3598. 6.  cd php-4.x.y
  3599. 7.  ./configure --with-mysql --with-apache=../apache_1.3.x
  3600. 8.  make
  3601. 9.  make install
  3602.  
  3603. 10. cd ../apache_1.3.x
  3604.  
  3605. 11. ./configure --prefix=/www --activate-module=src/modules/php4/libphp4.a
  3606.     (The above line is correct! Yes, we know libphp4.a does not exist at this
  3607.     stage. It isn't supposed to. It will be created.)
  3608.  
  3609. 12. make
  3610.     (you should now have an httpd binary which you can copy to your Apache bin dir if
  3611.     is is your first install then you need to "make install" as well)
  3612.  
  3613. 13. cd ../php-4.x.y
  3614. 14. cp php.ini-dist /usr/local/lib/php.ini
  3615.  
  3616. 15. You can edit /usr/local/lib/php.ini file to set PHP options.
  3617.     Edit your httpd.conf or srm.conf file and add:
  3618.     AddType application/x-httpd-php .php</PRE
  3619. ></TD
  3620. ></TR
  3621. ></TABLE
  3622. ></DIV
  3623. ></TD
  3624. ></TR
  3625. ></TABLE
  3626. ><P
  3627. >       Depending on your Apache install and Unix variant, there are many
  3628.       possible ways to stop and restart the server. Below are some typical
  3629.       lines used in restarting the server, for different apache/unix
  3630.       installations. You should replace <VAR
  3631. CLASS="literal"
  3632. >/path/to/</VAR
  3633. > with
  3634.       the path to these applications on your systems.
  3635.      </P
  3636. ><P
  3637. >       <TABLE
  3638. WIDTH="100%"
  3639. BORDER="0"
  3640. CELLPADDING="0"
  3641. CELLSPACING="0"
  3642. CLASS="EXAMPLE"
  3643. ><TR
  3644. ><TD
  3645. ><DIV
  3646. CLASS="example"
  3647. ><A
  3648. NAME="AEN412"
  3649. ></A
  3650. ><P
  3651. ><B
  3652. >Example 4-3. Example commands for restarting Apache</B
  3653. ></P
  3654. ><TABLE
  3655. BORDER="0"
  3656. BGCOLOR="#E0E0E0"
  3657. CELLPADDING="5"
  3658. ><TR
  3659. ><TD
  3660. ><PRE
  3661. CLASS="shell"
  3662. >1. Several Linux and SysV variants:
  3663. /etc/rc.d/init.d/httpd restart
  3664.  
  3665. 2. Using apachectl scripts:
  3666. /path/to/apachectl stop
  3667. /path/to/apachectl start
  3668.  
  3669. 3. httpdctl and httpsdctl (Using OpenSSL), similar to apachectl:
  3670. /path/to/httpsdctl stop
  3671. /path/to/httpsdctl start
  3672.  
  3673. 4. Using mod_ssl, or another SSL server, you may want to manually
  3674. stop and start:
  3675. /path/to/apachectl stop
  3676. /path/to/apachectl startssl</PRE
  3677. ></TD
  3678. ></TR
  3679. ></TABLE
  3680. ></DIV
  3681. ></TD
  3682. ></TR
  3683. ></TABLE
  3684. >
  3685.      </P
  3686. ><P
  3687. >       The locations of the apachectl and http(s)dctl binaries often
  3688.       vary. If your system has <VAR
  3689. CLASS="literal"
  3690. >locate</VAR
  3691. > or
  3692.       <VAR
  3693. CLASS="literal"
  3694. >whereis</VAR
  3695. > or <VAR
  3696. CLASS="literal"
  3697. >which</VAR
  3698. > commands,
  3699.       these can assist you in finding your server control programs.
  3700.      </P
  3701. ><P
  3702. >       Different examples of compiling PHP for apache are as follows:
  3703.       <DIV
  3704. CLASS="informalexample"
  3705. ><P
  3706. ></P
  3707. ><A
  3708. NAME="AEN420"
  3709. ></A
  3710. ><TABLE
  3711. BORDER="0"
  3712. BGCOLOR="#E0E0E0"
  3713. CELLPADDING="5"
  3714. ><TR
  3715. ><TD
  3716. ><PRE
  3717. CLASS="shell"
  3718. >./configure --with-apxs --with-pgsql</PRE
  3719. ></TD
  3720. ></TR
  3721. ></TABLE
  3722. ><P
  3723. ></P
  3724. ></DIV
  3725. >
  3726.      </P
  3727. ><P
  3728. >       This will create a <TT
  3729. CLASS="filename"
  3730. >libphp4.so</TT
  3731. > shared
  3732.       library that is loaded into Apache using a LoadModule line in
  3733.       Apache's <TT
  3734. CLASS="filename"
  3735. >httpd.conf</TT
  3736. > file. The PostgreSQL
  3737.       support is embedded into this <TT
  3738. CLASS="filename"
  3739. >libphp4.so</TT
  3740. >
  3741.       library.
  3742.      </P
  3743. ><P
  3744. >       <DIV
  3745. CLASS="informalexample"
  3746. ><P
  3747. ></P
  3748. ><A
  3749. NAME="AEN427"
  3750. ></A
  3751. ><TABLE
  3752. BORDER="0"
  3753. BGCOLOR="#E0E0E0"
  3754. CELLPADDING="5"
  3755. ><TR
  3756. ><TD
  3757. ><PRE
  3758. CLASS="shell"
  3759. >./configure --with-apxs --with-pgsql=shared</PRE
  3760. ></TD
  3761. ></TR
  3762. ></TABLE
  3763. ><P
  3764. ></P
  3765. ></DIV
  3766. >
  3767.      </P
  3768. ><P
  3769. >       This will create a <TT
  3770. CLASS="filename"
  3771. >libphp4.so</TT
  3772. > shared
  3773.       library for Apache, but it will also create a
  3774.       <TT
  3775. CLASS="filename"
  3776. >pgsql.so</TT
  3777. > shared library that is loaded into
  3778.       PHP either by using the extension directive in
  3779.       <TT
  3780. CLASS="filename"
  3781. >php.ini</TT
  3782. > file or by loading it explicitly in
  3783.       a script using the <A
  3784. HREF="#function.dl"
  3785. ><B
  3786. CLASS="function"
  3787. >dl()</B
  3788. ></A
  3789. > function.
  3790.      </P
  3791. ><P
  3792. >       <DIV
  3793. CLASS="informalexample"
  3794. ><P
  3795. ></P
  3796. ><A
  3797. NAME="AEN435"
  3798. ></A
  3799. ><TABLE
  3800. BORDER="0"
  3801. BGCOLOR="#E0E0E0"
  3802. CELLPADDING="5"
  3803. ><TR
  3804. ><TD
  3805. ><PRE
  3806. CLASS="shell"
  3807. >./configure --with-apache=/path/to/apache_source --with-pgsql</PRE
  3808. ></TD
  3809. ></TR
  3810. ></TABLE
  3811. ><P
  3812. ></P
  3813. ></DIV
  3814. >
  3815.      </P
  3816. ><P
  3817. >       This will create a <TT
  3818. CLASS="filename"
  3819. >libmodphp4.a</TT
  3820. > library, a
  3821.       <TT
  3822. CLASS="filename"
  3823. >mod_php4.c</TT
  3824. > and some accompanying files and
  3825.       copy this into the <VAR
  3826. CLASS="literal"
  3827. >src/modules/php4</VAR
  3828. > directory
  3829.       in the Apache source tree. Then you compile Apache using
  3830.       <VAR
  3831. CLASS="literal"
  3832. >--activate-module=src/modules/php4/libphp4.a</VAR
  3833. >
  3834.       and the Apache build system will create
  3835.       <TT
  3836. CLASS="filename"
  3837. >libphp4.a</TT
  3838. > and link it statically into the
  3839.       <TT
  3840. CLASS="filename"
  3841. >httpd</TT
  3842. > binary. The PostgreSQL support is
  3843.       included directly into this <TT
  3844. CLASS="filename"
  3845. >httpd</TT
  3846. > binary,
  3847.       so the final result here is a single <TT
  3848. CLASS="filename"
  3849. >httpd</TT
  3850. >
  3851.       binary that includes all of Apache and all of PHP.
  3852.      </P
  3853. ><P
  3854. >       <DIV
  3855. CLASS="informalexample"
  3856. ><P
  3857. ></P
  3858. ><A
  3859. NAME="AEN447"
  3860. ></A
  3861. ><TABLE
  3862. BORDER="0"
  3863. BGCOLOR="#E0E0E0"
  3864. CELLPADDING="5"
  3865. ><TR
  3866. ><TD
  3867. ><PRE
  3868. CLASS="shell"
  3869. >./configure --with-apache=/path/to/apache_source --with-pgsql=shared</PRE
  3870. ></TD
  3871. ></TR
  3872. ></TABLE
  3873. ><P
  3874. ></P
  3875. ></DIV
  3876. >
  3877.      </P
  3878. ><P
  3879. >       Same as before, except instead of including PostgreSQL support
  3880.       directly into the final <TT
  3881. CLASS="filename"
  3882. >httpd</TT
  3883. > you will get
  3884.       a <TT
  3885. CLASS="filename"
  3886. >pgsql.so</TT
  3887. > shared library that you can load
  3888.       into PHP from either the <TT
  3889. CLASS="filename"
  3890. >php.ini</TT
  3891. > file or
  3892.       directly using <A
  3893. HREF="#function.dl"
  3894. ><B
  3895. CLASS="function"
  3896. >dl()</B
  3897. ></A
  3898. >.
  3899.      </P
  3900. ><P
  3901. >       When choosing to build PHP in different ways, you should consider
  3902.       the advantages and drawbacks of each method. Building as a shared
  3903.       object will mean that you can compile apache separately, and don't
  3904.       have to recompile everything as you add to, or change, PHP.
  3905.       Building PHP into apache (static method) means that PHP will
  3906.       load and run faster. For more information, see the Apache
  3907.       <A
  3908. HREF="http://httpd.apache.org/docs/dso.html"
  3909. TARGET="_top"
  3910. >webpage on DSO support</A
  3911. >.
  3912.      </P
  3913. ><DIV
  3914. CLASS="note"
  3915. ><BLOCKQUOTE
  3916. CLASS="note"
  3917. ><P
  3918. ><B
  3919. >Note: </B
  3920. >
  3921.       Apache's default <TT
  3922. CLASS="filename"
  3923. >httpd.conf</TT
  3924. > currently ships with a section that looks
  3925.       like this:
  3926.       <DIV
  3927. CLASS="informalexample"
  3928. ><P
  3929. ></P
  3930. ><A
  3931. NAME="AEN459"
  3932. ></A
  3933. ><TABLE
  3934. BORDER="0"
  3935. BGCOLOR="#E0E0E0"
  3936. CELLPADDING="5"
  3937. ><TR
  3938. ><TD
  3939. ><PRE
  3940. CLASS="apache-conf"
  3941. >User nobody
  3942. Group "#-1"</PRE
  3943. ></TD
  3944. ></TR
  3945. ></TABLE
  3946. ><P
  3947. ></P
  3948. ></DIV
  3949. >
  3950.       Unless you change that to "Group nogroup" or something like that ("Group daemon" is
  3951.       also very common) PHP will not be able to open files.
  3952.      </P
  3953. ></BLOCKQUOTE
  3954. ></DIV
  3955. ><DIV
  3956. CLASS="note"
  3957. ><BLOCKQUOTE
  3958. CLASS="note"
  3959. ><P
  3960. ><B
  3961. >Note: </B
  3962. >
  3963.       Make sure you specify the installed version of apxs when using
  3964.       <VAR
  3965. CLASS="option"
  3966. >--with-apxs=/path/to/apxs</VAR
  3967. >.
  3968.       You must NOT use the apxs version that is in the apache sources but the one
  3969.       that is actually installed on your system.
  3970.      </P
  3971. ></BLOCKQUOTE
  3972. ></DIV
  3973. ></DIV
  3974. ><DIV
  3975. CLASS="sect1"
  3976. ><HR><H2
  3977. CLASS="sect1"
  3978. ><A
  3979. NAME="install.unix.apache2"
  3980. >Apache 2.0 on Unix systems</A
  3981. ></H2
  3982. ><P
  3983. >      This section contains notes and hints specific to Apache 2.0 installs
  3984.      of PHP on Unix systems.
  3985.     </P
  3986. ><DIV
  3987. CLASS="warning"
  3988. ><P
  3989. ></P
  3990. ><TABLE
  3991. CLASS="warning"
  3992. BORDER="1"
  3993. WIDTH="100%"
  3994. ><TR
  3995. ><TD
  3996. ALIGN="CENTER"
  3997. ><B
  3998. >Warning</B
  3999. ></TD
  4000. ></TR
  4001. ><TR
  4002. ><TD
  4003. ALIGN="LEFT"
  4004. ><P
  4005. >We do not recommend using a threaded MPM
  4006. in production with Apache2.  Use the prefork MPM instead, or use Apache1.  For information
  4007. on why, read the following 
  4008. <A
  4009. HREF="#faq.installation.apache2"
  4010. >FAQ entry</A
  4011. ></P
  4012. ></TD
  4013. ></TR
  4014. ></TABLE
  4015. ></DIV
  4016. ><P
  4017. >      You are highly encouraged to take a look at the
  4018.      <A
  4019. HREF="http://httpd.apache.org/docs-2.0/"
  4020. TARGET="_top"
  4021. >Apache Documentation</A
  4022. > to get
  4023.      a basic understanding of the Apache 2.0 Server.
  4024.     </P
  4025. ><DIV
  4026. CLASS="note"
  4027. ><BLOCKQUOTE
  4028. CLASS="note"
  4029. ><P
  4030. ><B
  4031. >PHP and Apache 2.0.x compatibility notes: </B
  4032. >
  4033.       The following versions of PHP are known to work with the most recent
  4034.       version of Apache 2.0.x:
  4035.       <P
  4036. ></P
  4037. ><UL
  4038. COMPACT="COMPACT"
  4039. ><LI
  4040. ><SPAN
  4041. >          PHP 4.3.0 or later available at
  4042.          <A
  4043. HREF="http://www.php.net/downloads.php"
  4044. TARGET="_top"
  4045. >http://www.php.net/downloads.php</A
  4046. >.
  4047.         </SPAN
  4048. ></LI
  4049. ><LI
  4050. ><SPAN
  4051. >          the latest stable development version.
  4052.          Get the source code <A
  4053. HREF="http://snaps.php.net/php4-latest.tar.gz"
  4054. TARGET="_top"
  4055. >          http://snaps.php.net/php4-latest.tar.gz</A
  4056. > or download binaries
  4057.          for Windows <A
  4058. HREF="http://snaps.php.net/win32/php4-win32-latest.zip"
  4059. TARGET="_top"
  4060. >          http://snaps.php.net/win32/php4-win32-latest.zip</A
  4061. >.
  4062.         </SPAN
  4063. ></LI
  4064. ><LI
  4065. ><SPAN
  4066. >          a prerelease version downloadable from
  4067.          <A
  4068. HREF="http://qa.php.net/"
  4069. TARGET="_top"
  4070. >http://qa.php.net/</A
  4071. >.
  4072.         </SPAN
  4073. ></LI
  4074. ><LI
  4075. ><SPAN
  4076. >          you have always the option to obtain PHP through
  4077.          <A
  4078. HREF="http://www.php.net/anoncvs.php"
  4079. TARGET="_top"
  4080. >anonymous CVS</A
  4081. >.
  4082.         </SPAN
  4083. ></LI
  4084. ></UL
  4085. >
  4086.       These versions of PHP are compatible to Apache 2.0.40 and later.
  4087.      </P
  4088. ><P
  4089. >       Apache 2.0 <VAR
  4090. CLASS="literal"
  4091. >SAPI</VAR
  4092. >-support started with PHP 4.2.0.
  4093.       PHP 4.2.3 works with Apache 2.0.39, don't use any other version of Apache with
  4094.       PHP 4.2.3. However, the recommended setup is to use PHP 4.3.0 or later with
  4095.       the most recent version of Apache2.
  4096.      </P
  4097. ><P
  4098. >       All mentioned versions of PHP will work still with
  4099.       Apache 1.3.x.
  4100.      </P
  4101. ></BLOCKQUOTE
  4102. ></DIV
  4103. ><P
  4104. >      Download the most recent version of <A
  4105. HREF="http://www.apache.org/"
  4106. TARGET="_top"
  4107. >      Apache 2.0</A
  4108. > and a fitting PHP version from the above mentioned places.
  4109.      This quick guide covers only the basics to get started with Apache 2.0
  4110.      and PHP. For more information read the
  4111.      <A
  4112. HREF="http://httpd.apache.org/docs-2.0/"
  4113. TARGET="_top"
  4114. >Apache Documentation</A
  4115. >.
  4116.      The version numbers have been omitted here, to ensure the
  4117.      instructions are not incorrect. You will need to replace the
  4118.      'NN' here with the correct values from your files.
  4119.     </P
  4120. ><TABLE
  4121. WIDTH="100%"
  4122. BORDER="0"
  4123. CELLPADDING="0"
  4124. CELLSPACING="0"
  4125. CLASS="EXAMPLE"
  4126. ><TR
  4127. ><TD
  4128. ><DIV
  4129. CLASS="example"
  4130. ><A
  4131. NAME="AEN495"
  4132. ></A
  4133. ><P
  4134. ><B
  4135. >Example 4-4. 
  4136.       Installation Instructions (Apache 2 Shared Module Version)
  4137.      </B
  4138. ></P
  4139. ><TABLE
  4140. BORDER="0"
  4141. BGCOLOR="#E0E0E0"
  4142. CELLPADDING="5"
  4143. ><TR
  4144. ><TD
  4145. ><PRE
  4146. CLASS="screen"
  4147. >1.  gzip -d httpd-2_0_NN.tar.gz
  4148. 2.  tar xvf httpd-2_0_NN.tar
  4149. 3.  gunzip php-NN.tar.gz
  4150. 4.  tar -xvf php-NN.tar
  4151. 5.  cd httpd-2_0_NN
  4152. 6.  ./configure --enable-so
  4153. 7.  make
  4154. 8.  make install
  4155.  
  4156.     Now you have Apache 2.0.NN available under /usr/local/apache2,
  4157.     configured with loadable module support and the standard MPM prefork.
  4158.     To test the installation use your normal procedure for starting
  4159.     the Apache server, e.g.:
  4160.     /usr/local/apache2/bin/apachectl start
  4161.     and stop the server to go on with the configuration for PHP:
  4162.     /usr/local/apache2/bin/apachectl stop.
  4163.  
  4164. 9.  cd ../php-NN
  4165.  
  4166. 10. Now, configure your PHP.  This is where you customize your PHP
  4167.     with various options, like which extensions will be enabled.  Do a
  4168.     ./configure --help for a list of available options.  In our example
  4169.     we'll do a simple configure with Apache 2 and MySQL support.  Your
  4170.     path to apxs may differ, in fact, the binary may even be named apxs2 on
  4171.     your system. 
  4172.     
  4173.       ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
  4174.  
  4175. 11. make
  4176. 12. make install
  4177.  
  4178.     If you decide to change your configure options after installation,
  4179.     you only need to repeat the last three steps. You only need to
  4180.     restart apache for the new module to take effect. A recompile of
  4181.     Apache is not needed.
  4182.                 
  4183.     Note that unless told otherwise, 'make install' will also install PEAR,
  4184.     various PHP tools such as phpize, install the PHP CLI, and more.
  4185.     
  4186. 13. Setup your php.ini 
  4187.     
  4188.     cp php.ini-dist /usr/local/lib/php.ini
  4189.           
  4190.     You may edit your .ini file to set PHP options.  If you prefer having
  4191.     php.ini in another location, use --with-config-file-path=/some/path in
  4192.     step 10.
  4193.     
  4194.     If you instead choose php.ini-recommended, be certain to read the list
  4195.     of changes within, as they affect how PHP behaves.
  4196.  
  4197. 14. Edit your httpd.conf to load the PHP module.  The path on the right hand
  4198.     side of the LoadModule statement must point to the path of the PHP
  4199.     module on your system.  The make install from above may have already
  4200.     added this for you, but be sure to check.
  4201.  
  4202.     For PHP 4:
  4203.   
  4204.       LoadModule php4_module libexec/libphp4.so
  4205.       
  4206.     For PHP 5:
  4207.     
  4208.       LoadModule php5_module libexec/libphp5.so
  4209.  
  4210. 15. Tell Apache to parse certain extensions as PHP.  For example,
  4211.     let's have Apache parse the .php extension as PHP.  You could
  4212.     have any extension(s) parse as PHP by simply adding more, with
  4213.     each separated by a space.  We'll add .phtml to demonstrate.
  4214.             
  4215.       AddType application/x-httpd-php .php .phtml
  4216.                   
  4217.     It's also common to setup the .phps extension to show highlighted PHP
  4218.     source, this can be done with:
  4219.     
  4220.       AddType application/x-httpd-php-source .phps
  4221.  
  4222. 16. Use your normal procedure for starting the Apache server, e.g.:
  4223.    
  4224.       /usr/local/apache2/bin/apachectl start</PRE
  4225. ></TD
  4226. ></TR
  4227. ></TABLE
  4228. ></DIV
  4229. ></TD
  4230. ></TR
  4231. ></TABLE
  4232. ><P
  4233. >      Following the steps above you will have a running Apache 2.0 with
  4234.      support for PHP as <VAR
  4235. CLASS="literal"
  4236. >SAPI</VAR
  4237. > module.
  4238.      Of course there are many more configuration options available for both,
  4239.      Apache and PHP. For more information use
  4240.      <B
  4241. CLASS="command"
  4242. >./configure --help</B
  4243. > in the corresponding source
  4244.      tree. In case you wish to build a multithreaded version of Apache 2.0
  4245.      you must overwrite the standard MPM-Module <TT
  4246. CLASS="filename"
  4247. >prefork</TT
  4248. >
  4249.      either with <TT
  4250. CLASS="filename"
  4251. >worker</TT
  4252. > or <TT
  4253. CLASS="filename"
  4254. >perchild</TT
  4255. >.
  4256.      To do so append to your configure line in step 6 above either the option
  4257.      <VAR
  4258. CLASS="option"
  4259. >--with-mpm=worker</VAR
  4260. > or
  4261.      <VAR
  4262. CLASS="option"
  4263. >--with-mpm=perchild</VAR
  4264. >. Take care about
  4265.      the consequences and understand what you are doing. For more information
  4266.      read the Apache documentation about the <A
  4267. HREF="http://httpd.apache.org/docs-2.0/mpm.html"
  4268. TARGET="_top"
  4269. >      MPM-Modules</A
  4270. >.
  4271.     </P
  4272. ><DIV
  4273. CLASS="note"
  4274. ><BLOCKQUOTE
  4275. CLASS="note"
  4276. ><P
  4277. ><B
  4278. >Note: </B
  4279. >
  4280.       If you want to use content negotiation, read
  4281.       <A
  4282. HREF="#faq.installation.apache.multiviews"
  4283. >related FAQ</A
  4284. >.
  4285.      </P
  4286. ></BLOCKQUOTE
  4287. ></DIV
  4288. ><DIV
  4289. CLASS="note"
  4290. ><BLOCKQUOTE
  4291. CLASS="note"
  4292. ><P
  4293. ><B
  4294. >Note: </B
  4295. >
  4296.       To build a multithreaded version of Apache your system must support threads.
  4297.       This also implies to build PHP with experimental
  4298.       Zend Thread Safety (ZTS). Therefore not all extensions might be available.
  4299.       The recommended setup is to build Apache with the standard
  4300.       <TT
  4301. CLASS="filename"
  4302. >prefork</TT
  4303. > MPM-Module.
  4304.      </P
  4305. ></BLOCKQUOTE
  4306. ></DIV
  4307. ></DIV
  4308. ><DIV
  4309. CLASS="sect1"
  4310. ><HR><H2
  4311. CLASS="sect1"
  4312. ><A
  4313. NAME="install.unix.caudium"
  4314. >Caudium</A
  4315. ></H2
  4316. ><P
  4317. >     PHP 4 can be built as a Pike module for the
  4318.     <A
  4319. HREF="http://caudium.net/"
  4320. TARGET="_top"
  4321. >Caudium webserver</A
  4322. >.
  4323.     Note that this is not supported with PHP 3. Follow the simple
  4324.     instructions below to install PHP 4 for Caudium.
  4325.    </P
  4326. ><TABLE
  4327. WIDTH="100%"
  4328. BORDER="0"
  4329. CELLPADDING="0"
  4330. CELLSPACING="0"
  4331. CLASS="EXAMPLE"
  4332. ><TR
  4333. ><TD
  4334. ><DIV
  4335. CLASS="example"
  4336. ><A
  4337. NAME="install.unix.caudium.instructions"
  4338. ></A
  4339. ><P
  4340. ><B
  4341. >Example 4-5. Caudium Installation Instructions</B
  4342. ></P
  4343. ><TABLE
  4344. BORDER="0"
  4345. BGCOLOR="#E0E0E0"
  4346. CELLPADDING="5"
  4347. ><TR
  4348. ><TD
  4349. ><PRE
  4350. CLASS="screen"
  4351. >1.  Make sure you have Caudium installed prior to attempting to
  4352.     install PHP 4. For PHP 4 to work correctly, you will need Pike
  4353.     7.0.268 or newer. For the sake of this example we assume that
  4354.     Caudium is installed in /opt/caudium/server/.
  4355. 2.  Change directory to php-x.y.z (where x.y.z is the version number).
  4356. 3.  ./configure --with-caudium=/opt/caudium/server
  4357. 4.  make
  4358. 5.  make install
  4359. 6.  Restart Caudium if it's currently running.
  4360. 7.  Log into the graphical configuration interface and go to the
  4361.     virtual server where you want to add PHP 4 support.
  4362. 8.  Click Add Module and locate and then add the PHP 4 Script Support module.
  4363. 9.  If the documentation says that the 'PHP 4 interpreter isn't
  4364.     available', make sure that you restarted the server. If you did
  4365.     check /opt/caudium/logs/debug/default.1 for any errors related to
  4366.     <filename>PHP4.so</filename>. Also make sure that 
  4367.     <filename>caudium/server/lib/[pike-version]/PHP4.so</filename>
  4368.     is present.
  4369. 10. Configure the PHP Script Support module if needed.</PRE
  4370. ></TD
  4371. ></TR
  4372. ></TABLE
  4373. ></DIV
  4374. ></TD
  4375. ></TR
  4376. ></TABLE
  4377. ><P
  4378. >     You can of course compile your Caudium module with support for the
  4379.     various extensions available in PHP 4. See the reference pages
  4380.     for extension specific configure options.
  4381.    </P
  4382. ><DIV
  4383. CLASS="note"
  4384. ><BLOCKQUOTE
  4385. CLASS="note"
  4386. ><P
  4387. ><B
  4388. >Note: </B
  4389. >
  4390.      When compiling PHP 4 with MySQL support you must make sure that
  4391.      the normal MySQL client code is used. Otherwise there might be
  4392.      conflicts if your Pike already has MySQL support. You do this by
  4393.      specifying a MySQL install directory the 
  4394.      <VAR
  4395. CLASS="option"
  4396. >--with-mysql</VAR
  4397. > option.
  4398.     </P
  4399. ></BLOCKQUOTE
  4400. ></DIV
  4401. ></DIV
  4402. ><DIV
  4403. CLASS="sect1"
  4404. ><HR><H2
  4405. CLASS="sect1"
  4406. ><A
  4407. NAME="install.unix.fhttpd"
  4408. >fhttpd related notes</A
  4409. ></H2
  4410. ><P
  4411. >     To build PHP as an fhttpd module, answer "yes" to "Build as an
  4412.     fhttpd module?" (the <A
  4413. HREF="#configure.with-fhttpd"
  4414. >     --with-fhttpd</A
  4415. >=<VAR
  4416. CLASS="replaceable"
  4417. >DIR</VAR
  4418. >
  4419.     option to configure) and specify the fhttpd source base
  4420.     directory.  The default directory is <TT
  4421. CLASS="filename"
  4422. >/usr/local/src/fhttpd</TT
  4423. >. If you are
  4424.     running fhttpd, building PHP as a module will give better
  4425.     performance, more control and remote execution capability.
  4426.    </P
  4427. ><DIV
  4428. CLASS="note"
  4429. ><BLOCKQUOTE
  4430. CLASS="note"
  4431. ><P
  4432. ><B
  4433. >Note: </B
  4434. >
  4435.      Support for fhttpd is no longer available as of PHP 4.3.0.
  4436.     </P
  4437. ></BLOCKQUOTE
  4438. ></DIV
  4439. ></DIV
  4440. ><DIV
  4441. CLASS="sect1"
  4442. ><HR><H2
  4443. CLASS="sect1"
  4444. ><A
  4445. NAME="install.unix.sun"
  4446. >Sun, iPlanet and Netscape servers on Sun Solaris</A
  4447. ></H2
  4448. ><P
  4449. >      This section contains notes and hints specific to Sun Java System Web Server,
  4450.      Sun ONE Web Server, iPlanet and Netscape server installs of PHP on Sun Solaris.
  4451.     </P
  4452. ><P
  4453. >      From PHP 4.3.3 on you can use PHP scripts with the
  4454.      <A
  4455. HREF="#ref.nsapi"
  4456. >NSAPI module</A
  4457. > to 
  4458.      <A
  4459. HREF="#install.unix.sun.specialpages"
  4460. >generate custom directory
  4461.      listings and error pages</A
  4462. >. Additional functions for Apache
  4463.      compatibility are also available. For support in current webservers read
  4464.      the <A
  4465. HREF="#install.unix.sun.notes"
  4466. >note about subrequests</A
  4467. >.
  4468.     </P
  4469. ><P
  4470. >      You can find more information about setting up PHP for the Netscape
  4471.      Enterprise Server (NES) here:
  4472.      <A
  4473. HREF="http://benoit.noss.free.fr/php/install-php4.html"
  4474. TARGET="_top"
  4475. >http://benoit.noss.free.fr/php/install-php4.html</A
  4476. >
  4477.     </P
  4478. ><P
  4479. >      To build PHP with Sun JSWS/Sun ONE WS/iPlanet/Netscape webservers,
  4480.      enter the proper install directory for the
  4481.      <A
  4482. HREF="#configure.with-nsapi"
  4483. >--with-nsapi=[DIR]</A
  4484. >
  4485.      option. The default directory is usually
  4486.      <TT
  4487. CLASS="filename"
  4488. >/opt/netscape/suitespot/</TT
  4489. >.
  4490.      Please also read <TT
  4491. CLASS="filename"
  4492. >/php-xxx-version/sapi/nsapi/nsapi-readme.txt</TT
  4493. >.
  4494.     </P
  4495. ><P
  4496. >      <P
  4497. ></P
  4498. ><OL
  4499. TYPE="1"
  4500. ><LI
  4501. ><P
  4502. >         Install the following packages from <A
  4503. HREF="http://www.sunfreeware.com/"
  4504. TARGET="_top"
  4505. >         http://www.sunfreeware.com/</A
  4506. > or another download site:
  4507.         <P
  4508. ></P
  4509. ><TABLE
  4510. BORDER="0"
  4511. ><TBODY
  4512. ><TR
  4513. ><TD
  4514. ><TT
  4515. CLASS="filename"
  4516. >autoconf-2.13</TT
  4517. ></TD
  4518. ></TR
  4519. ><TR
  4520. ><TD
  4521. ><TT
  4522. CLASS="filename"
  4523. >automake-1.4</TT
  4524. ></TD
  4525. ></TR
  4526. ><TR
  4527. ><TD
  4528. ><TT
  4529. CLASS="filename"
  4530. >bison-1_25-sol26-sparc-local</TT
  4531. ></TD
  4532. ></TR
  4533. ><TR
  4534. ><TD
  4535. ><TT
  4536. CLASS="filename"
  4537. >flex-2_5_4a-sol26-sparc-local</TT
  4538. ></TD
  4539. ></TR
  4540. ><TR
  4541. ><TD
  4542. ><TT
  4543. CLASS="filename"
  4544. >gcc-2_95_2-sol26-sparc-local</TT
  4545. ></TD
  4546. ></TR
  4547. ><TR
  4548. ><TD
  4549. ><TT
  4550. CLASS="filename"
  4551. >gzip-1.2.4-sol26-sparc-local</TT
  4552. ></TD
  4553. ></TR
  4554. ><TR
  4555. ><TD
  4556. ><TT
  4557. CLASS="filename"
  4558. >m4-1_4-sol26-sparc-local</TT
  4559. ></TD
  4560. ></TR
  4561. ><TR
  4562. ><TD
  4563. ><TT
  4564. CLASS="filename"
  4565. >make-3_76_1-sol26-sparc-local</TT
  4566. ></TD
  4567. ></TR
  4568. ><TR
  4569. ><TD
  4570. >           <TT
  4571. CLASS="filename"
  4572. >mysql-3.23.24-beta</TT
  4573. > (if you want mysql support)
  4574.          </TD
  4575. ></TR
  4576. ><TR
  4577. ><TD
  4578. ><TT
  4579. CLASS="filename"
  4580. >perl-5_005_03-sol26-sparc-local</TT
  4581. ></TD
  4582. ></TR
  4583. ><TR
  4584. ><TD
  4585. ><TT
  4586. CLASS="filename"
  4587. >tar-1.13</TT
  4588. > (GNU tar)</TD
  4589. ></TR
  4590. ></TBODY
  4591. ></TABLE
  4592. ><P
  4593. ></P
  4594. >
  4595.        </P
  4596. ></LI
  4597. ><LI
  4598. ><P
  4599. >         Make sure your path includes the proper directories
  4600.         <VAR
  4601. CLASS="literal"
  4602. >PATH=.:/usr/local/bin:/usr/sbin:/usr/bin:/usr/ccs/bin</VAR
  4603. >
  4604.         and make it available to your system <KBD
  4605. CLASS="userinput"
  4606. >export PATH</KBD
  4607. >.
  4608.        </P
  4609. ></LI
  4610. ><LI
  4611. ><P
  4612. >         <KBD
  4613. CLASS="userinput"
  4614. >gunzip php-x.x.x.tar.gz</KBD
  4615. > (if you have a .gz dist,
  4616.         otherwise go to 4).
  4617.        </P
  4618. ></LI
  4619. ><LI
  4620. ><P
  4621. >         <KBD
  4622. CLASS="userinput"
  4623. >tar xvf php-x.x.x.tar</KBD
  4624. >
  4625.        </P
  4626. ></LI
  4627. ><LI
  4628. ><P
  4629. >         Change to your extracted PHP directory:
  4630.         <KBD
  4631. CLASS="userinput"
  4632. >cd ../php-x.x.x </KBD
  4633. >
  4634.        </P
  4635. ></LI
  4636. ><LI
  4637. ><P
  4638. >         For the following step, make sure
  4639.         <TT
  4640. CLASS="filename"
  4641. >/opt/netscape/suitespot/</TT
  4642. > is
  4643.         where your netscape server is installed. Otherwise, change to the
  4644.         correct path and run:
  4645.         <TABLE
  4646. BORDER="0"
  4647. BGCOLOR="#E0E0E0"
  4648. CELLPADDING="5"
  4649. ><TR
  4650. ><TD
  4651. ><PRE
  4652. CLASS="shell"
  4653. >./configure --with-mysql=/usr/local/mysql \
  4654. --with-nsapi=/opt/netscape/suitespot/ \
  4655. --enable-libgcc</PRE
  4656. ></TD
  4657. ></TR
  4658. ></TABLE
  4659. >
  4660.        </P
  4661. ></LI
  4662. ><LI
  4663. ><P
  4664. >         Run <B
  4665. CLASS="command"
  4666. >make</B
  4667. > followed by <B
  4668. CLASS="command"
  4669. >make install</B
  4670. >.
  4671.        </P
  4672. ></LI
  4673. ></OL
  4674. >
  4675.     </P
  4676. ><P
  4677. >      After performing the base install and reading the appropriate readme file,
  4678.      you may need to perform some additional configuration steps.
  4679.     </P
  4680. ><DIV
  4681. CLASS="formalpara"
  4682. ><P
  4683. ><B
  4684. >Configuration Instructions for Sun/iPlanet/Netscape. </B
  4685. >
  4686.       Firstly you may need to add some paths to the <VAR
  4687. CLASS="varname"
  4688. >LD_LIBRARY_PATH</VAR
  4689. >
  4690.       environment for the server to find all the shared libs. This can best done
  4691.       in the start script for your webserver. The start script is often located
  4692.       in: <TT
  4693. CLASS="filename"
  4694. >/path/to/server/https-servername/start</TT
  4695. >.
  4696.       You may also need to edit the configuration files that are
  4697.       located in: <TT
  4698. CLASS="filename"
  4699. >/path/to/server/https-servername/config/</TT
  4700. >.
  4701.       <P
  4702. ></P
  4703. ><OL
  4704. TYPE="1"
  4705. ><LI
  4706. ><P
  4707. >          Add the following line to <TT
  4708. CLASS="filename"
  4709. >mime.types</TT
  4710. > (you can do
  4711.          that by the administration server):
  4712.          <TABLE
  4713. BORDER="0"
  4714. BGCOLOR="#E0E0E0"
  4715. CELLPADDING="5"
  4716. ><TR
  4717. ><TD
  4718. ><PRE
  4719. CLASS="screen"
  4720. >type=magnus-internal/x-httpd-php exts=php</PRE
  4721. ></TD
  4722. ></TR
  4723. ></TABLE
  4724. >
  4725.         </P
  4726. ></LI
  4727. ><LI
  4728. ><P
  4729. >          Edit <TT
  4730. CLASS="filename"
  4731. >magnus.conf</TT
  4732. > (for servers >= 6) or
  4733.          <TT
  4734. CLASS="filename"
  4735. >obj.conf</TT
  4736. > (for servers < 6) and add the following,
  4737.          shlib will vary depending on your system, it will be something like
  4738.          <TT
  4739. CLASS="filename"
  4740. >/opt/netscape/suitespot/bin/libphp4.so</TT
  4741. >. You should
  4742.          place the following lines after <VAR
  4743. CLASS="literal"
  4744. >mime types init</VAR
  4745. >.
  4746.          <TABLE
  4747. BORDER="0"
  4748. BGCOLOR="#E0E0E0"
  4749. CELLPADDING="5"
  4750. ><TR
  4751. ><TD
  4752. ><PRE
  4753. CLASS="programlisting"
  4754. >Init fn="load-modules" funcs="php4_init,php4_execute,php4_auth_trans" shlib="/opt/netscape/suitespot/bin/libphp4.so"
  4755. Init fn="php4_init" LateInit="yes" errorString="Failed to initialize PHP!" [php_ini="/path/to/php.ini"]</PRE
  4756. ></TD
  4757. ></TR
  4758. ></TABLE
  4759. >
  4760.          (PHP >= 4.3.3) The <VAR
  4761. CLASS="literal"
  4762. >php_ini</VAR
  4763. > parameter is
  4764.          optional but with it you can place your <TT
  4765. CLASS="filename"
  4766. >php.ini</TT
  4767. > in your
  4768.          webserver config directory.
  4769.         </P
  4770. ></LI
  4771. ><LI
  4772. ><P
  4773. >          Configure the default object in <TT
  4774. CLASS="filename"
  4775. >obj.conf</TT
  4776. >
  4777.          (for virtual server classes [version 6.0+] in
  4778.          their <TT
  4779. CLASS="filename"
  4780. >vserver.obj.conf</TT
  4781. >):
  4782.          <TABLE
  4783. BORDER="0"
  4784. BGCOLOR="#E0E0E0"
  4785. CELLPADDING="5"
  4786. ><TR
  4787. ><TD
  4788. ><PRE
  4789. CLASS="programlisting"
  4790. ><Object name="default">
  4791. .
  4792. .
  4793. .
  4794. .#NOTE this next line should happen after all 'ObjectType' and before all 'AddLog' lines
  4795. Service fn="php4_execute" type="magnus-internal/x-httpd-php" [inikey=value inikey=value ...]
  4796. .
  4797. .
  4798. </Object></PRE
  4799. ></TD
  4800. ></TR
  4801. ></TABLE
  4802. >
  4803.          (PHP >= 4.3.3) As additional parameters you can add some special
  4804.          <TT
  4805. CLASS="filename"
  4806. >php.ini</TT
  4807. >-values, for example you can set a
  4808.          <VAR
  4809. CLASS="literal"
  4810. >docroot="/path/to/docroot"</VAR
  4811. > specific
  4812.          to the context <VAR
  4813. CLASS="literal"
  4814. >php4_execute</VAR
  4815. > is called. For boolean
  4816.          ini-keys please use 0/1 as value, not
  4817.          <VAR
  4818. CLASS="literal"
  4819. >"On","Off",...</VAR
  4820. >
  4821.          (this will not work correctly), e.g.
  4822.          <VAR
  4823. CLASS="literal"
  4824. >zlib.output_compression=1</VAR
  4825. > instead of
  4826.          <VAR
  4827. CLASS="literal"
  4828. >zlib.output_compression="On"</VAR
  4829. >
  4830.         </P
  4831. ></LI
  4832. ><LI
  4833. ><P
  4834. >          This is only needed if you want to configure a directory that only consists of
  4835.          PHP scripts (same like a <TT
  4836. CLASS="filename"
  4837. >cgi-bin</TT
  4838. > directory):
  4839.          <TABLE
  4840. BORDER="0"
  4841. BGCOLOR="#E0E0E0"
  4842. CELLPADDING="5"
  4843. ><TR
  4844. ><TD
  4845. ><PRE
  4846. CLASS="programlisting"
  4847. ><Object name="x-httpd-php">
  4848. ObjectType fn="force-type" type="magnus-internal/x-httpd-php"
  4849. Service fn=php4_execute [inikey=value inikey=value ...]
  4850. </Object></PRE
  4851. ></TD
  4852. ></TR
  4853. ></TABLE
  4854. >
  4855.          After that you can configure a directory in the Administration server and assign it
  4856.          the style <VAR
  4857. CLASS="literal"
  4858. >x-httpd-php</VAR
  4859. >. All files in it will get executed as PHP.
  4860.          This is nice to hide PHP usage by renaming files to <TT
  4861. CLASS="filename"
  4862. >.html</TT
  4863. >.
  4864.         </P
  4865. ></LI
  4866. ><LI
  4867. ><P
  4868. >          Setup of authentication: PHP authentication cannot be used with any
  4869.          other authentication. ALL AUTHENTICATION IS PASSED TO YOUR PHP SCRIPT.
  4870.          To configure PHP Authentication for the entire server, add the
  4871.          following line to your default object:
  4872.          <TABLE
  4873. BORDER="0"
  4874. BGCOLOR="#E0E0E0"
  4875. CELLPADDING="5"
  4876. ><TR
  4877. ><TD
  4878. ><PRE
  4879. CLASS="programlisting"
  4880. ><Object name="default">
  4881. AuthTrans fn=php4_auth_trans
  4882. .
  4883. .
  4884. .
  4885. </Object></PRE
  4886. ></TD
  4887. ></TR
  4888. ></TABLE
  4889. >
  4890.         </P
  4891. ></LI
  4892. ><LI
  4893. ><P
  4894. >          To use PHP Authentication on a single directory, add the following:
  4895.          <TABLE
  4896. BORDER="0"
  4897. BGCOLOR="#E0E0E0"
  4898. CELLPADDING="5"
  4899. ><TR
  4900. ><TD
  4901. ><PRE
  4902. CLASS="programlisting"
  4903. ><Object ppath="d:\path\to\authenticated\dir\*">
  4904. AuthTrans fn=php4_auth_trans
  4905. </Object></PRE
  4906. ></TD
  4907. ></TR
  4908. ></TABLE
  4909. >
  4910.         </P
  4911. ></LI
  4912. ></OL
  4913. >
  4914.      </P
  4915. ></DIV
  4916. ><DIV
  4917. CLASS="note"
  4918. ><BLOCKQUOTE
  4919. CLASS="note"
  4920. ><P
  4921. ><B
  4922. >Note: </B
  4923. >
  4924.       The stacksize that PHP uses depends on the configuration of the webserver. If you get
  4925.       crashes with very large PHP scripts, it is recommended to raise it with the Admin Server
  4926.       (in the section "MAGNUS EDITOR").
  4927.      </P
  4928. ></BLOCKQUOTE
  4929. ></DIV
  4930. ><DIV
  4931. CLASS="sect2"
  4932. ><HR><H3
  4933. CLASS="sect2"
  4934. ><A
  4935. NAME="install.unix.sun.phpini"
  4936. >CGI environment and recommended modifications in php.ini</A
  4937. ></H3
  4938. ><P
  4939. >      Important when writing PHP scripts is the fact that Sun JSWS/Sun ONE
  4940.      WS/iPlanet/Netscape is a multithreaded web server. Because of that all
  4941.      requests are running in the same process space (the space of the webserver
  4942.      itself) and this space has only one environment. If you want to get CGI
  4943.      variables like <VAR
  4944. CLASS="literal"
  4945. >PATH_INFO</VAR
  4946. >, <VAR
  4947. CLASS="literal"
  4948. >HTTP_HOST</VAR
  4949. >
  4950.      etc. it is not the correct way to try this in the old PHP 3.x way with
  4951.      <A
  4952. HREF="#function.getenv"
  4953. ><B
  4954. CLASS="function"
  4955. >getenv()</B
  4956. ></A
  4957. > or a similar way (register globals to
  4958.      environment, <VAR
  4959. CLASS="literal"
  4960. >$_ENV</VAR
  4961. >). You would only get the environment
  4962.      of the running webserver without any valid CGI variables!
  4963.     </P
  4964. ><DIV
  4965. CLASS="note"
  4966. ><BLOCKQUOTE
  4967. CLASS="note"
  4968. ><P
  4969. ><B
  4970. >Note: </B
  4971. >
  4972.       Why are there (invalid) CGI variables in the environment?
  4973.      </P
  4974. ><P
  4975. >       Answer: This is because you started the webserver process from the admin server
  4976.       which runs the startup script of the webserver, you wanted to start, as a CGI script
  4977.       (a CGI script inside of the admin server!). This is why the environment of
  4978.       the started webserver has some CGI environment variables in it. You can test
  4979.       this by starting the webserver not from the administration server. Use
  4980.       the command line as root user and start it manually - you will see
  4981.       there are no CGI-like environment variables.
  4982.      </P
  4983. ></BLOCKQUOTE
  4984. ></DIV
  4985. ><P
  4986. >      Simply change your scripts to get CGI variables in the correct way for
  4987.      PHP 4.x by using the superglobal <VAR
  4988. CLASS="literal"
  4989. >$_SERVER</VAR
  4990. >. If you have
  4991.      older scripts which use <VAR
  4992. CLASS="literal"
  4993. >$HTTP_HOST</VAR
  4994. >, etc., you should turn
  4995.      on <VAR
  4996. CLASS="literal"
  4997. >register_globals</VAR
  4998. > in <TT
  4999. CLASS="filename"
  5000. >php.ini</TT
  5001. > and change the variable
  5002.      order too (important: remove <VAR
  5003. CLASS="literal"
  5004. >"E"</VAR
  5005. > from it,
  5006.      because you do not need the environment here):
  5007.      <TABLE
  5008. BORDER="0"
  5009. BGCOLOR="#E0E0E0"
  5010. CELLPADDING="5"
  5011. ><TR
  5012. ><TD
  5013. ><PRE
  5014. CLASS="programlisting"
  5015. >variables_order = "GPCS"
  5016. register_globals = On</PRE
  5017. ></TD
  5018. ></TR
  5019. ></TABLE
  5020. >
  5021.     </P
  5022. ></DIV
  5023. ><DIV
  5024. CLASS="sect2"
  5025. ><HR><H3
  5026. CLASS="sect2"
  5027. ><A
  5028. NAME="install.unix.sun.specialpages"
  5029. >Special use for error pages or self-made directory listings (PHP >= 4.3.3)</A
  5030. ></H3
  5031. ><P
  5032. >      You can use PHP to generate the error pages for <VAR
  5033. CLASS="literal"
  5034. >"404 Not Found"</VAR
  5035. >
  5036.      or similar. Add the following line to the object in <TT
  5037. CLASS="filename"
  5038. >obj.conf</TT
  5039. > for
  5040.      every error page you want to overwrite:
  5041.      <TABLE
  5042. BORDER="0"
  5043. BGCOLOR="#E0E0E0"
  5044. CELLPADDING="5"
  5045. ><TR
  5046. ><TD
  5047. ><PRE
  5048. CLASS="programlisting"
  5049. >Error fn="php4_execute" code=XXX script="/path/to/script.php" [inikey=value inikey=value...]</PRE
  5050. ></TD
  5051. ></TR
  5052. ></TABLE
  5053. >
  5054.      where <VAR
  5055. CLASS="literal"
  5056. >XXX</VAR
  5057. > is the HTTP error code. Please delete
  5058.      any other <VAR
  5059. CLASS="literal"
  5060. >Error</VAR
  5061. > directives which could interfere with yours.
  5062.      If you want to place a page for all errors that could exist, leave
  5063.      the <VAR
  5064. CLASS="literal"
  5065. >code</VAR
  5066. > parameter out. Your script can get the HTTP status code
  5067.      with <VAR
  5068. CLASS="literal"
  5069. >$_SERVER['ERROR_TYPE']</VAR
  5070. >.
  5071.     </P
  5072. ><P
  5073. >      Another possibility is to generate self-made directory listings.
  5074.      Just create a PHP script which displays a directory listing and
  5075.      replace the corresponding default Service line for
  5076.      <VAR
  5077. CLASS="literal"
  5078. >type="magnus-internal/directory"</VAR
  5079. >
  5080.      in <TT
  5081. CLASS="filename"
  5082. >obj.conf</TT
  5083. > with the following:
  5084.      <TABLE
  5085. BORDER="0"
  5086. BGCOLOR="#E0E0E0"
  5087. CELLPADDING="5"
  5088. ><TR
  5089. ><TD
  5090. ><PRE
  5091. CLASS="programlisting"
  5092. >Service fn="php4_execute" type="magnus-internal/directory" script="/path/to/script.php" [inikey=value inikey=value...]</PRE
  5093. ></TD
  5094. ></TR
  5095. ></TABLE
  5096. >
  5097.      For both error and directory listing pages the original URI and
  5098.      translated URI are in the variables <VAR
  5099. CLASS="literal"
  5100. >$_SERVER['PATH_INFO']</VAR
  5101. > and
  5102.      <VAR
  5103. CLASS="literal"
  5104. >$_SERVER['PATH_TRANSLATED']</VAR
  5105. >.
  5106.     </P
  5107. ></DIV
  5108. ><DIV
  5109. CLASS="sect2"
  5110. ><HR><H3
  5111. CLASS="sect2"
  5112. ><A
  5113. NAME="install.unix.sun.notes"
  5114. >Note about <A
  5115. HREF="#function.nsapi-virtual"
  5116. ><B
  5117. CLASS="function"
  5118. >nsapi_virtual()</B
  5119. ></A
  5120. > and subrequests (PHP >= 4.3.3)</A
  5121. ></H3
  5122. ><P
  5123. >      The NSAPI module now supports the <A
  5124. HREF="#function.nsapi-virtual"
  5125. ><B
  5126. CLASS="function"
  5127. >nsapi_virtual()</B
  5128. ></A
  5129. > function
  5130.      (alias: <A
  5131. HREF="#function.virtual"
  5132. ><B
  5133. CLASS="function"
  5134. >virtual()</B
  5135. ></A
  5136. >)
  5137.      to make subrequests on the webserver and insert the result in the webpage.
  5138.      This function uses some undocumented features from the NSAPI library.
  5139.      On Unix the module automatically looks for the needed functions and uses
  5140.      them if available. If not, <A
  5141. HREF="#function.nsapi-virtual"
  5142. ><B
  5143. CLASS="function"
  5144. >nsapi_virtual()</B
  5145. ></A
  5146. > is disabled.
  5147.     </P
  5148. ><DIV
  5149. CLASS="note"
  5150. ><BLOCKQUOTE
  5151. CLASS="note"
  5152. ><P
  5153. ><B
  5154. >Note: </B
  5155. >
  5156.       But be warned: Support for <A
  5157. HREF="#function.nsapi-virtual"
  5158. ><B
  5159. CLASS="function"
  5160. >nsapi_virtual()</B
  5161. ></A
  5162. > is EXPERIMENTAL!!!
  5163.      </P
  5164. ></BLOCKQUOTE
  5165. ></DIV
  5166. ></DIV
  5167. ></DIV
  5168. ><DIV
  5169. CLASS="sect1"
  5170. ><HR><H2
  5171. CLASS="sect1"
  5172. ><A
  5173. NAME="install.unix.commandline"
  5174. >CGI and commandline setups</A
  5175. ></H2
  5176. ><P
  5177. >     The default is to build PHP as a CGI program.  This creates a
  5178.     commandline interpreter, which can be used for CGI processing, or
  5179.     for non-web-related PHP scripting.  If you are running a web
  5180.     server PHP has module support for, you should generally go for
  5181.     that solution for performance reasons.  However, the CGI version
  5182.     enables users to run different PHP-enabled pages under
  5183.     different user-ids.
  5184.    </P
  5185. ><DIV
  5186. CLASS="warning"
  5187. ><P
  5188. ></P
  5189. ><TABLE
  5190. CLASS="warning"
  5191. BORDER="1"
  5192. WIDTH="100%"
  5193. ><TR
  5194. ><TD
  5195. ALIGN="CENTER"
  5196. ><B
  5197. >Warning</B
  5198. ></TD
  5199. ></TR
  5200. ><TR
  5201. ><TD
  5202. ALIGN="LEFT"
  5203. ><P
  5204. >By using the CGI setup, your server
  5205. is open to several possible attacks. Please read our
  5206. <A
  5207. HREF="#security.cgi-bin"
  5208. >CGI security section</A
  5209. > to learn how to
  5210. defend yourself from those attacks.</P
  5211. ></TD
  5212. ></TR
  5213. ></TABLE
  5214. ></DIV
  5215. ><P
  5216. >     As of PHP 4.3.0, some important additions have happened to PHP.  A new
  5217.     SAPI named CLI also exists and it has the same name as the CGI binary.
  5218.     What is installed at <VAR
  5219. CLASS="literal"
  5220. >{PREFIX}/bin/php</VAR
  5221. > depends on your
  5222.     configure line and this is described in detail in the manual section
  5223.     named <A
  5224. HREF="#features.commandline"
  5225. >Using PHP from the command
  5226.     line</A
  5227. >. For further details please read that section of the manual.
  5228.    </P
  5229. ><DIV
  5230. CLASS="sect2"
  5231. ><HR><H3
  5232. CLASS="sect2"
  5233. ><A
  5234. NAME="install.unix.commandline.testing"
  5235. >Testing</A
  5236. ></H3
  5237. ><P
  5238. >      If you have built PHP as a CGI program, you may test your build
  5239.      by typing <B
  5240. CLASS="command"
  5241. >make test</B
  5242. >.  It is always a good idea
  5243.      to test your build.  This way you may catch a problem with PHP on
  5244.      your platform early instead of having to struggle with it later.
  5245.     </P
  5246. ></DIV
  5247. ><DIV
  5248. CLASS="sect2"
  5249. ><HR><H3
  5250. CLASS="sect2"
  5251. ><A
  5252. NAME="install.unix.commandline.benchmarking"
  5253. >Benchmarking</A
  5254. ></H3
  5255. ><P
  5256. >      If you have built PHP 3 as a CGI program, you may benchmark your
  5257.      build by typing <B
  5258. CLASS="command"
  5259. >make bench</B
  5260. >. Note that if 
  5261.      <A
  5262. HREF="#ini.safe-mode"
  5263. >safe mode</A
  5264. > is on by default, the benchmark may not be able to finish if
  5265.      it takes longer then the 30 seconds allowed. This is because the
  5266.      <A
  5267. HREF="#function.set-time-limit"
  5268. ><B
  5269. CLASS="function"
  5270. >set_time_limit()</B
  5271. ></A
  5272. > can not be used in 
  5273.      <A
  5274. HREF="#ini.safe-mode"
  5275. >safe mode</A
  5276. >. Use the <A
  5277. HREF="#ini.max-execution-time"
  5278. >max_execution_time</A
  5279. >
  5280.      configuration setting to control this time for your own
  5281.      scripts. <B
  5282. CLASS="command"
  5283. >make bench</B
  5284. > ignores the <A
  5285. HREF="#configuration.file"
  5286. >configuration file</A
  5287. >.
  5288.     </P
  5289. ><DIV
  5290. CLASS="note"
  5291. ><BLOCKQUOTE
  5292. CLASS="note"
  5293. ><P
  5294. ><B
  5295. >Note: </B
  5296. >
  5297.         <B
  5298. CLASS="command"
  5299. >make bench</B
  5300. > is only available for PHP 3.
  5301.      </P
  5302. ></BLOCKQUOTE
  5303. ></DIV
  5304. ></DIV
  5305. ><DIV
  5306. CLASS="sect2"
  5307. ><HR><H3
  5308. CLASS="sect2"
  5309. ><A
  5310. NAME="install.unix.commandline.using-variables"
  5311. >Using Variables</A
  5312. ></H3
  5313. ><P
  5314. >       Some <A
  5315. HREF="#reserved.variables.server"
  5316. >server supplied
  5317.       environment variables</A
  5318. > are not defined in the 
  5319.       current <A
  5320. HREF="http://hoohoo.ncsa.uiuc.edu/cgi/env.html"
  5321. TARGET="_top"
  5322. >CGI/1.1 specification</A
  5323. >.
  5324.       Only the following variables are defined there: <VAR
  5325. CLASS="varname"
  5326. >AUTH_TYPE</VAR
  5327. >, 
  5328.       <VAR
  5329. CLASS="varname"
  5330. >CONTENT_LENGTH</VAR
  5331. >, <VAR
  5332. CLASS="varname"
  5333. >CONTENT_TYPE</VAR
  5334. >,
  5335.       <VAR
  5336. CLASS="varname"
  5337. >GATEWAY_INTERFACE</VAR
  5338. >, <VAR
  5339. CLASS="varname"
  5340. >PATH_INFO</VAR
  5341. >, 
  5342.       <VAR
  5343. CLASS="varname"
  5344. >PATH_TRANSLATED</VAR
  5345. >, <VAR
  5346. CLASS="varname"
  5347. >QUERY_STRING</VAR
  5348. >,
  5349.       <VAR
  5350. CLASS="varname"
  5351. >REMOTE_ADDR</VAR
  5352. >, <VAR
  5353. CLASS="varname"
  5354. >REMOTE_HOST</VAR
  5355. >, 
  5356.       <VAR
  5357. CLASS="varname"
  5358. >REMOTE_IDENT</VAR
  5359. >, <VAR
  5360. CLASS="varname"
  5361. >REMOTE_USER</VAR
  5362. >,
  5363.       <VAR
  5364. CLASS="varname"
  5365. >REQUEST_METHOD</VAR
  5366. >, <VAR
  5367. CLASS="varname"
  5368. >SCRIPT_NAME</VAR
  5369. >, 
  5370.       <VAR
  5371. CLASS="varname"
  5372. >SERVER_NAME</VAR
  5373. >, <VAR
  5374. CLASS="varname"
  5375. >SERVER_PORT</VAR
  5376. >,
  5377.       <VAR
  5378. CLASS="varname"
  5379. >SERVER_PROTOCOL</VAR
  5380. >, and <VAR
  5381. CLASS="varname"
  5382. >SERVER_SOFTWARE</VAR
  5383. >.
  5384.       Everything else should be treated as 'vendor extensions'.
  5385.     </P
  5386. ></DIV
  5387. ></DIV
  5388. ><DIV
  5389. CLASS="sect1"
  5390. ><HR><H2
  5391. CLASS="sect1"
  5392. ><A
  5393. NAME="install.unix.hpux"
  5394. >HP-UX specific installation notes</A
  5395. ></H2
  5396. ><P
  5397. >     This section contains notes and hints specific to installing PHP
  5398.     on HP-UX systems. (Contributed by paul_mckay at clearwater-it dot co dot
  5399.     uk).
  5400.    </P
  5401. ><DIV
  5402. CLASS="note"
  5403. ><BLOCKQUOTE
  5404. CLASS="note"
  5405. ><P
  5406. ><B
  5407. >Note: </B
  5408. >
  5409.      These tips were written for PHP 4.0.4 and Apache 1.3.9.
  5410.     </P
  5411. ></BLOCKQUOTE
  5412. ></DIV
  5413. ><P
  5414. >     <P
  5415. ></P
  5416. ><OL
  5417. TYPE="1"
  5418. ><LI
  5419. ><P
  5420. >        You need gzip, download a binary distribution from
  5421.        <TT
  5422. CLASS="filename"
  5423. >http://hpux.connect.org.uk/ftp/hpux/Gnu/gzip-1.2.4a/gzip-1.2.4a-sd-10.20.depot.Z</TT
  5424. >
  5425.        uncompress the file and install using swinstall.
  5426.       </P
  5427. ></LI
  5428. ><LI
  5429. ><P
  5430. >        You need gcc, download a binary distribution from
  5431.        <TT
  5432. CLASS="filename"
  5433. >http://gatekeep.cs.utah.edu/ftp/hpux/Gnu/gcc-2.95.2/gcc-2.95.2-sd-10.20.depot.gz</TT
  5434. >.
  5435.        uncompress this file and install gcc using swinstall.
  5436.       </P
  5437. ></LI
  5438. ><LI
  5439. ><P
  5440. >        You need the GNU binutils, you can download a binary distribution from
  5441.        <TT
  5442. CLASS="filename"
  5443. >http://hpux.connect.org.uk/ftp/hpux/Gnu/binutils-2.9.1/binutils-2.9.1-sd-10.20.depot.gz</TT
  5444. >.
  5445.        uncompress this file and install binutils using swinstall.
  5446.       </P
  5447. ></LI
  5448. ><LI
  5449. ><P
  5450. >        You now need bison, you can download a binary distribution from
  5451.        <TT
  5452. CLASS="filename"
  5453. >http://hpux.connect.org.uk/ftp/hpux/Gnu/bison-1.28/bison-1.28-sd-10.20.depot.gz</TT
  5454. >,
  5455.        install as above.
  5456.       </P
  5457. ></LI
  5458. ><LI
  5459. ><P
  5460. >        You now need flex, you need to download the source from one of the
  5461.        http://www.gnu.org mirrors. It is in the non-gnu directory of the ftp
  5462.        site.  Download the file, <B
  5463. CLASS="command"
  5464. >gunzip</B
  5465. >, then
  5466.        <B
  5467. CLASS="command"
  5468. >tar -xvf</B
  5469. > it. Go into the newly created flex directory
  5470.        and run <B
  5471. CLASS="command"
  5472. >./configure</B
  5473. >, followed by 
  5474.        <B
  5475. CLASS="command"
  5476. >make</B
  5477. >, and then <B
  5478. CLASS="command"
  5479. >make install</B
  5480. >.
  5481.       </P
  5482. ><P
  5483. >        If you have errors here, it's probably because gcc etc. are not in your
  5484.        PATH so add them to your PATH. 
  5485.       </P
  5486. ></LI
  5487. ><LI
  5488. ><P
  5489. >        Download the PHP and apache sources. 
  5490.       </P
  5491. ></LI
  5492. ><LI
  5493. ><P
  5494. >        <B
  5495. CLASS="command"
  5496. >gunzip</B
  5497. > and <B
  5498. CLASS="command"
  5499. >tar -xvf</B
  5500. > them. We
  5501.        need to hack a couple of files so that they can compile OK.
  5502.       </P
  5503. ></LI
  5504. ><LI
  5505. ><P
  5506. >        Firstly the configure file needs to be hacked because it seems to lose
  5507.        track of the fact that you are a hpux machine, there will be a better
  5508.        way of doing this but a cheap and cheerful hack is to put
  5509.        <VAR
  5510. CLASS="literal"
  5511. >lt_target=hpux10.20</VAR
  5512. > on line 47286 of the configure
  5513.        script.
  5514.       </P
  5515. ></LI
  5516. ><LI
  5517. ><P
  5518. >        Next, the Apache GuessOS file needs to be hacked. Under
  5519.        <TT
  5520. CLASS="filename"
  5521. >apache_1.3.9/src/helpers</TT
  5522. > change line 89 from <VAR
  5523. CLASS="literal"
  5524. >echo
  5525.        "hp${HPUXMACH}-hpux${HPUXVER}"; exit 0</VAR
  5526. > to: <VAR
  5527. CLASS="literal"
  5528. >echo
  5529.        "hp${HPUXMACH}-hp-hpux${HPUXVER}"; exit 0</VAR
  5530. >
  5531.       </P
  5532. ></LI
  5533. ><LI
  5534. ><P
  5535. >        You cannot install PHP as a shared object under HP-UX so you must
  5536.        compile it as a static, just follow the instructions at the Apache
  5537.        page. 
  5538.       </P
  5539. ></LI
  5540. ><LI
  5541. ><P
  5542. >        PHP and Apache should have compiled OK, but Apache won't start. you
  5543.        need to create a new user for Apache, e.g. www, or apache. You then
  5544.        change lines 252 and 253 of the <TT
  5545. CLASS="filename"
  5546. >conf/httpd.conf</TT
  5547. > in
  5548.        Apache so that instead of
  5549.       </P
  5550. ><TABLE
  5551. BORDER="0"
  5552. BGCOLOR="#E0E0E0"
  5553. CELLPADDING="5"
  5554. ><TR
  5555. ><TD
  5556. ><PRE
  5557. CLASS="screen"
  5558. >User nobody 
  5559. Group nogroup</PRE
  5560. ></TD
  5561. ></TR
  5562. ></TABLE
  5563. ><P
  5564. >        you have something like 
  5565.       </P
  5566. ><TABLE
  5567. BORDER="0"
  5568. BGCOLOR="#E0E0E0"
  5569. CELLPADDING="5"
  5570. ><TR
  5571. ><TD
  5572. ><PRE
  5573. CLASS="screen"
  5574. >User www 
  5575. Group sys</PRE
  5576. ></TD
  5577. ></TR
  5578. ></TABLE
  5579. ><P
  5580. >        This is because you can't run Apache as nobody under hp-ux. Apache and
  5581.        PHP should then work. 
  5582.       </P
  5583. ></LI
  5584. ></OL
  5585. >
  5586.  </P
  5587. ></DIV
  5588. ><DIV
  5589. CLASS="sect1"
  5590. ><HR><H2
  5591. CLASS="sect1"
  5592. ><A
  5593. NAME="install.unix.openbsd"
  5594. >OpenBSD installation notes</A
  5595. ></H2
  5596. ><P
  5597. >  This section contains notes and hints specific to installing
  5598.  PHP on <A
  5599. HREF="http://www.openbsd.org/"
  5600. TARGET="_top"
  5601. >OpenBSD 3.6</A
  5602. >.
  5603.  </P
  5604. ><DIV
  5605. CLASS="sect2"
  5606. ><HR><H3
  5607. CLASS="sect2"
  5608. ><A
  5609. NAME="install.unix.openbsd.packages"
  5610. >Using Binary Packages</A
  5611. ></H3
  5612. ><P
  5613. >     Using binary packages to install PHP on OpenBSD is the recommended
  5614.     and simplest method.  The core package has been separated from the various
  5615.     modules, and each can be installed and removed independently from the others.
  5616.     The files you need can be found on your OpenBSD CD or on the FTP site.
  5617.    </P
  5618. ><P
  5619. >     The main package you need to install is <TT
  5620. CLASS="filename"
  5621. >php4-core-4.3.8.tgz</TT
  5622. >,
  5623.     which contains the basic engine (plus gettext and iconv).  Next, take a look
  5624.     at the module packages, such as <TT
  5625. CLASS="filename"
  5626. >php4-mysql-4.3.8.tgz</TT
  5627. >
  5628.     or <TT
  5629. CLASS="filename"
  5630. >php4-imap-4.3.8.tgz</TT
  5631. >.  You need to use the <B
  5632. CLASS="command"
  5633. >phpxs</B
  5634. >
  5635.     command to activate and deactivate these modules in your <TT
  5636. CLASS="filename"
  5637. >php.ini</TT
  5638. >.
  5639.    </P
  5640. ><TABLE
  5641. WIDTH="100%"
  5642. BORDER="0"
  5643. CELLPADDING="0"
  5644. CELLSPACING="0"
  5645. CLASS="EXAMPLE"
  5646. ><TR
  5647. ><TD
  5648. ><DIV
  5649. CLASS="example"
  5650. ><A
  5651. NAME="install.unix.openbsd.ports.example"
  5652. ></A
  5653. ><P
  5654. ><B
  5655. >Example 4-6. OpenBSD Package Install Example</B
  5656. ></P
  5657. ><TABLE
  5658. BORDER="0"
  5659. BGCOLOR="#E0E0E0"
  5660. CELLPADDING="5"
  5661. ><TR
  5662. ><TD
  5663. ><PRE
  5664. CLASS="shell"
  5665. ># pkg_add php4-core-4.3.8.tgz
  5666. # /usr/local/sbin/phpxs -s
  5667. # cp /usr/local/share/doc/php4/php.ini-recommended /var/www/conf/php.ini
  5668.   (add in mysql)
  5669. # pkg_add php4-mysql-4.3.8.tgz
  5670. # /usr/local/sbin/phpxs -a mysql
  5671.   (add in imap)
  5672. # pkg_add php4-imap-4.3.8.tgz
  5673. # /usr/local/sbin/phpxs -a imap
  5674.   (remove mysql as a test)
  5675. # pkg_delete php4-mysql-4.3.8
  5676. # /usr/local/sbin/phpxs -r mysql
  5677.   (install the PEAR libraries)
  5678. # pkg_add php4-pear-4.3.8.tgz</PRE
  5679. ></TD
  5680. ></TR
  5681. ></TABLE
  5682. ></DIV
  5683. ></TD
  5684. ></TR
  5685. ></TABLE
  5686. ><P
  5687. >    Read the <A
  5688. HREF="http://www.openbsd.org/cgi-bin/man.cgi?query=packages"
  5689. TARGET="_top"
  5690. >packages(7)</A
  5691. >
  5692.    manual page for more information about binary packages on OpenBSD.
  5693.    </P
  5694. ></DIV
  5695. ><DIV
  5696. CLASS="sect2"
  5697. ><HR><H3
  5698. CLASS="sect2"
  5699. ><A
  5700. NAME="install.unix.openbsd.ports"
  5701. >Using Ports</A
  5702. ></H3
  5703. ><P
  5704. >    You can also compile up PHP from source using the <A
  5705. HREF="http://www.openbsd.org/ports.html"
  5706. TARGET="_top"
  5707. >ports tree</A
  5708. >.
  5709.    However, this is only recommended for users familiar with OpenBSD.  The PHP 4 port
  5710.    is split into two sub-directories: core and extensions.  The
  5711.    extensions directory generates sub-packages for all of the supported
  5712.    PHP modules.  If you find you do not want to create some of these modules,
  5713.    use the <B
  5714. CLASS="command"
  5715. >no_*</B
  5716. > FLAVOR.  For example, to skip building
  5717.    the imap module, set the FLAVOR to <B
  5718. CLASS="command"
  5719. >no_imap</B
  5720. >.
  5721.   </P
  5722. ></DIV
  5723. ><DIV
  5724. CLASS="sect2"
  5725. ><HR><H3
  5726. CLASS="sect2"
  5727. ><A
  5728. NAME="install.unix.openbsd.faq"
  5729. >Common Problems</A
  5730. ></H3
  5731. ><P
  5732. ></P
  5733. ><UL
  5734. ><LI
  5735. ><P
  5736. >The default install of Apache runs inside a
  5737.     <A
  5738. HREF="http://www.openbsd.org/cgi-bin/man.cgi?query=chroot"
  5739. TARGET="_top"
  5740. >chroot(2) jail</A
  5741. >, which will restrict PHP scripts to
  5742.     accessing files under <TT
  5743. CLASS="filename"
  5744. >/var/www</TT
  5745. >.  You will therefore need to create a
  5746.     <TT
  5747. CLASS="filename"
  5748. >/var/www/tmp</TT
  5749. > directory for PHP session files to be stored, or use an
  5750.     alternative session backend.  In addition, database sockets need to be placed inside the
  5751.     jail or listen on the <TT
  5752. CLASS="filename"
  5753. >localhost</TT
  5754. > interface.  If you use network functions,
  5755.     some files from <TT
  5756. CLASS="filename"
  5757. >/etc</TT
  5758. > such as <TT
  5759. CLASS="filename"
  5760. >/etc/resolv.conf</TT
  5761. > and
  5762.     <TT
  5763. CLASS="filename"
  5764. >/etc/services</TT
  5765. > will need to be moved into <TT
  5766. CLASS="filename"
  5767. >/var/www/etc</TT
  5768. >.
  5769.     The OpenBSD PEAR package automatically installs into the correct chroot directories, so 
  5770.     no special modification is needed there.  More information on the OpenBSD Apache is available
  5771.     in the <A
  5772. HREF="http://www.openbsd.org/faq/faq10.html#httpdchroot"
  5773. TARGET="_top"
  5774. >OpenBSD FAQ</A
  5775. >.
  5776.     </P
  5777. ></LI
  5778. ><LI
  5779. ><P
  5780. >     The OpenBSD 3.6 package for the <A
  5781. HREF="http://www.boutell.com/gd/"
  5782. TARGET="_top"
  5783. >gd</A
  5784. > extension requires
  5785.     XFree86 to be installed.  If you do not wish to use some of the font features that
  5786.     require X11, install the <TT
  5787. CLASS="filename"
  5788. >php4-gd-4.3.8-no_x11.tgz</TT
  5789. > package instead.
  5790.     </P
  5791. ></LI
  5792. ></UL
  5793. ></DIV
  5794. ><DIV
  5795. CLASS="sect2"
  5796. ><HR><H3
  5797. CLASS="sect2"
  5798. ><A
  5799. NAME="install.unix.openbsd.older"
  5800. >Older Releases</A
  5801. ></H3
  5802. ><P
  5803. >    Older releases of OpenBSD used the FLAVORS system to compile up
  5804.    a statically linked PHP.  Since it is hard to generate binary packages using
  5805.    this method, it is now deprecated.  You can still use the old stable
  5806.    ports trees if you wish, but they are unsupported by the OpenBSD team.
  5807.    If you have any comments about this, the current maintainer for the port
  5808.    is Anil Madhavapeddy (avsm at openbsd dot org).
  5809.   </P
  5810. ></DIV
  5811. ></DIV
  5812. ><DIV
  5813. CLASS="sect1"
  5814. ><HR><H2
  5815. CLASS="sect1"
  5816. ><A
  5817. NAME="install.unix.solaris"
  5818. >Solaris specific installation tips</A
  5819. ></H2
  5820. ><P
  5821. >   This section contains notes and hints specific to installing
  5822.   PHP on Solaris systems.
  5823.  </P
  5824. ><DIV
  5825. CLASS="sect2"
  5826. ><HR><H3
  5827. CLASS="sect2"
  5828. ><A
  5829. NAME="install.unix.solaris.required"
  5830. >Required software</A
  5831. ></H3
  5832. ><P
  5833. >    Solaris installs often lack C compilers and their related tools.
  5834.    Read <A
  5835. HREF="#faq.installation.needgnu"
  5836. >this FAQ</A
  5837.    for information on why using GNU versions for some of these
  5838.    tools is necessary.  The required software is as follows:
  5839.    <P
  5840. ></P
  5841. ><UL
  5842. ><LI
  5843. ><P
  5844. >       gcc (recommended, other C compilers may work)
  5845.      </P
  5846. ></LI
  5847. ><LI
  5848. ><P
  5849. >       make
  5850.      </P
  5851. ></LI
  5852. ><LI
  5853. ><P
  5854. >       flex
  5855.      </P
  5856. ></LI
  5857. ><LI
  5858. ><P
  5859. >       bison
  5860.      </P
  5861. ></LI
  5862. ><LI
  5863. ><P
  5864. >       m4
  5865.      </P
  5866. ></LI
  5867. ><LI
  5868. ><P
  5869. >       autoconf
  5870.      </P
  5871. ></LI
  5872. ><LI
  5873. ><P
  5874. >       automake
  5875.      </P
  5876. ></LI
  5877. ><LI
  5878. ><P
  5879. >       perl
  5880.      </P
  5881. ></LI
  5882. ><LI
  5883. ><P
  5884. >       gzip
  5885.      </P
  5886. ></LI
  5887. ><LI
  5888. ><P
  5889. >       tar
  5890.      </P
  5891. ></LI
  5892. ><LI
  5893. ><P
  5894. >       GNU sed
  5895.      </P
  5896. ></LI
  5897. ></UL
  5898. >
  5899.     In addition, you will need to install (and possibly compile) any
  5900.     additional software specific to your configuration, such as Oracle
  5901.     or MySQL.
  5902.   </P
  5903. ></DIV
  5904. ><DIV
  5905. CLASS="sect2"
  5906. ><HR><H3
  5907. CLASS="sect2"
  5908. ><A
  5909. NAME="install.unix.solaris.packages"
  5910. >Using Packages</A
  5911. ></H3
  5912. ><P
  5913. >    You can simplify the Solaris install process by using pkgadd to
  5914.    install most of your needed components. 
  5915.   </P
  5916. ></DIV
  5917. ></DIV
  5918. ><DIV
  5919. CLASS="sect1"
  5920. ><HR><H2
  5921. CLASS="sect1"
  5922. ><A
  5923. NAME="install.unix.gentoo"
  5924. >Gentoo installation notes</A
  5925. ></H2
  5926. ><P
  5927. >   This section contains notes and hints specific to installing
  5928.   PHP on <A
  5929. HREF="http://www.gentoo.org/"
  5930. TARGET="_top"
  5931. >Gentoo Linux</A
  5932. >.
  5933.  </P
  5934. ><DIV
  5935. CLASS="sect2"
  5936. ><HR><H3
  5937. CLASS="sect2"
  5938. ><A
  5939. NAME="install.unix.gentoo.portage"
  5940. >Using Portage (emerge)</A
  5941. ></H3
  5942. ><P
  5943. >     While you can just download the PHP source and compile it yourself,
  5944.     using Gentoo's packaging system is the simplest and cleanest
  5945.     method of installing PHP. If you are not familiar with building
  5946.     software on Linux, this is the way to go.
  5947.    </P
  5948. ><P
  5949. >     If you have built your Gentoo system so far, you are probably used
  5950.     to Portage already. Installing Apache and PHP is no different than 
  5951.     the other system tools.
  5952.    </P
  5953. ><P
  5954. >     The first decision you need to make is whether you want to install
  5955.     Apache 1.3.x or Apache 2.x. While both can be used with PHP, the 
  5956.     steps given bellow will use Apache 1.3.x. Another thing to consider
  5957.     is whether your local Portage tree is up to date. If you have not
  5958.     updated it recently, you need to run <B
  5959. CLASS="command"
  5960. >emerge sync</B
  5961. >
  5962.     before anything else. This way, you will be using the most recent
  5963.     stable version of Apache and PHP.
  5964.    </P
  5965. ><P
  5966. >     Now that everything is in place, you can use the following example
  5967.     to install Apache and PHP:
  5968.    </P
  5969. ><TABLE
  5970. WIDTH="100%"
  5971. BORDER="0"
  5972. CELLPADDING="0"
  5973. CELLSPACING="0"
  5974. CLASS="EXAMPLE"
  5975. ><TR
  5976. ><TD
  5977. ><DIV
  5978. CLASS="example"
  5979. ><A
  5980. NAME="install.unix.gentoo.portage.example"
  5981. ></A
  5982. ><P
  5983. ><B
  5984. >Example 4-7. Gentoo Install Example with Apache 1.3</B
  5985. ></P
  5986. ><TABLE
  5987. BORDER="0"
  5988. BGCOLOR="#E0E0E0"
  5989. CELLPADDING="5"
  5990. ><TR
  5991. ><TD
  5992. ><PRE
  5993. CLASS="shell"
  5994. ># emerge \<apache-2
  5995. # USE="-*" emerge php mod_php
  5996. # ebuild /var/db/pkg/dev-php/mod_php-<your PHP version>/mod_php-<your PHP version>.ebuild config
  5997. # nano /etc/conf.d/apache
  5998.   Add "-D PHP4" to APACHE_OPTS
  5999.   
  6000. # rc-update add apache default
  6001. # /etc/init.d/apache start</PRE
  6002. ></TD
  6003. ></TR
  6004. ></TABLE
  6005. ></DIV
  6006. ></TD
  6007. ></TR
  6008. ></TABLE
  6009. ><P
  6010. >     You can read more about emerge in the excellent 
  6011.     <A
  6012. HREF="http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?part=2&chap=1"
  6013. TARGET="_top"
  6014. >Portage Manual</A
  6015. > provided
  6016.     on the Gentoo website.
  6017.    </P
  6018. ><P
  6019. >     If you need to use Apache 2, you can simply use <B
  6020. CLASS="command"
  6021. >emerge apache</B
  6022. >
  6023.     in the last example.
  6024.    </P
  6025. ></DIV
  6026. ><DIV
  6027. CLASS="sect2"
  6028. ><HR><H3
  6029. CLASS="sect2"
  6030. ><A
  6031. NAME="install.unix.gentoo.config"
  6032. >Better control on configuration</A
  6033. ></H3
  6034. ><P
  6035. >     In the last section, PHP was emerged without any activated modules.
  6036.     As of this writing, the only module activated by default with Portage 
  6037.     is XML which is needed by <A
  6038. HREF="http://pear.php.net/"
  6039. TARGET="_top"
  6040. >PEAR</A
  6041. >.
  6042.     This may not be what you want and you will soon discover that you need 
  6043.     more activated modules, like MySQL, gettext, GD, etc.
  6044.    </P
  6045. ><P
  6046. >     When you compile PHP from source yourself, you need to activate modules
  6047.     via the <B
  6048. CLASS="command"
  6049. >configure</B
  6050. > command. With Gentoo, you can simply
  6051.     provide USE flags which will be passed to the configure script automatically.
  6052.     To see which USE flags to use with emerge, you can try:
  6053.    </P
  6054. ><TABLE
  6055. WIDTH="100%"
  6056. BORDER="0"
  6057. CELLPADDING="0"
  6058. CELLSPACING="0"
  6059. CLASS="EXAMPLE"
  6060. ><TR
  6061. ><TD
  6062. ><DIV
  6063. CLASS="example"
  6064. ><A
  6065. NAME="install.unix.gentoo.config.example"
  6066. ></A
  6067. ><P
  6068. ><B
  6069. >Example 4-8. Getting the list of valid USE flags</B
  6070. ></P
  6071. ><TABLE
  6072. BORDER="0"
  6073. BGCOLOR="#E0E0E0"
  6074. CELLPADDING="5"
  6075. ><TR
  6076. ><TD
  6077. ><PRE
  6078. CLASS="shell"
  6079. ># USE="-*" emerge -pv php
  6080.  
  6081. [ebuild  N    ] dev-php/php-4.3.6-r1  -X -berkdb -crypt -curl -debug -doc 
  6082. -fdftk -firebird -flash -freetds -gd -gd-external -gdbm -gmp -hardenedphp 
  6083. -imap -informix -ipv6 -java -jpeg -kerberos -ldap -mcal -memlimit -mssql 
  6084. -mysql -ncurses -nls -oci8 -odbc -pam -pdflib -png -postgres -qt -readline 
  6085. -snmp -spell -ssl -tiff -truetype -xml2 -yaz  3,876 kB</PRE
  6086. ></TD
  6087. ></TR
  6088. ></TABLE
  6089. ></DIV
  6090. ></TD
  6091. ></TR
  6092. ></TABLE
  6093. ><P
  6094. >     As you can see from the last output, PHP considers a lot of USE flags.
  6095.     Look at them closely and choose what you need. If you choose a flag and 
  6096.     you do not have the proper libraries, Portage will compile them for you.
  6097.     It is a good idea to use <B
  6098. CLASS="command"
  6099. >emerge -pv</B
  6100. > again to see what
  6101.     Portage will compile in accordance to your USE flags. As an example,
  6102.     if you do not have X installed and you choose to include X in the USE
  6103.     flags, Portage will compile X prior to PHP, which can take a couple
  6104.     of hours.
  6105.    </P
  6106. ><P
  6107. >     If you choose to compile PHP with MySQL, cURL and GD support, the command
  6108.     will look something like this:
  6109.    </P
  6110. ><TABLE
  6111. WIDTH="100%"
  6112. BORDER="0"
  6113. CELLPADDING="0"
  6114. CELLSPACING="0"
  6115. CLASS="EXAMPLE"
  6116. ><TR
  6117. ><TD
  6118. ><DIV
  6119. CLASS="example"
  6120. ><A
  6121. NAME="install.unix.gentoo.config.example2"
  6122. ></A
  6123. ><P
  6124. ><B
  6125. >Example 4-9. Install PHP with USE flags</B
  6126. ></P
  6127. ><TABLE
  6128. BORDER="0"
  6129. BGCOLOR="#E0E0E0"
  6130. CELLPADDING="5"
  6131. ><TR
  6132. ><TD
  6133. ><PRE
  6134. CLASS="shell"
  6135. ># USE="-* curl mysql gd" emerge php mod_php</PRE
  6136. ></TD
  6137. ></TR
  6138. ></TABLE
  6139. ></DIV
  6140. ></TD
  6141. ></TR
  6142. ></TABLE
  6143. ><P
  6144. >     As in the last example, do not forget to emerge php as well as mod_php.
  6145.     php is responsible for the command line version of PHP as mod_php is
  6146.     for the Apache module version of PHP.
  6147.    </P
  6148. ></DIV
  6149. ><DIV
  6150. CLASS="sect2"
  6151. ><HR><H3
  6152. CLASS="sect2"
  6153. ><A
  6154. NAME="install.unix.gentoo.faq"
  6155. >Common Problems</A
  6156. ></H3
  6157. ><P
  6158. ></P
  6159. ><UL
  6160. ><LI
  6161. ><P
  6162. >      If you see the PHP source instead of the result the script should
  6163.      produce, you have probably forgot to edit <TT
  6164. CLASS="filename"
  6165. >/etc/conf.d/apache</TT
  6166. >.
  6167.      Apache needs to be started with the -D PHP4 flag. To see if the flag is
  6168.      present, you should be able to see it when using 
  6169.      <B
  6170. CLASS="command"
  6171. >ps ax | grep apache</B
  6172. > while Apache is running.
  6173.     </P
  6174. ></LI
  6175. ><LI
  6176. ><P
  6177. >      Due to slotting problems, you might end up with more than one version
  6178.      of PHP installed on your system. If this is the case, you need to unmerge
  6179.      the old versions manually by using
  6180.      <B
  6181. CLASS="command"
  6182. >emerge unmerge mod_php-<old version></B
  6183. >.
  6184.     </P
  6185. ></LI
  6186. ><LI
  6187. ><P
  6188. >      If you cannot emerge PHP because of Java, try putting <B
  6189. CLASS="command"
  6190. >-*</B
  6191. >
  6192.      in front of your USE flags like in the above examples.
  6193.     </P
  6194. ></LI
  6195. ><LI
  6196. ><P
  6197. >      If you are having problems configuring Apache and PHP, you can always
  6198.      search the <A
  6199. HREF="http://forums.gentoo.org/"
  6200. TARGET="_top"
  6201. >Gentoo Forums</A
  6202. >.
  6203.      Try searching with the keywords "Apache PHP".
  6204.     </P
  6205. ></LI
  6206. ></UL
  6207. ></DIV
  6208. ></DIV
  6209. ></DIV
  6210. ><DIV
  6211. CLASS="chapter"
  6212. ><HR><H1
  6213. ><A
  6214. NAME="install.macosx"
  6215. >Chapter 5. Installation on Mac OS X</A
  6216. ></H1
  6217. ><P
  6218. >   This section contains notes and hints specific to installing
  6219.   PHP on Mac OS X. There are two slightly different versions of
  6220.   Mac OS X, Client and Server, our manual deals with installing
  6221.   PHP on both systems. Note that PHP is not available for MacOS
  6222.   9 and earlier versions. 
  6223.  </P
  6224. ><DIV
  6225. CLASS="sect1"
  6226. ><HR><H2
  6227. CLASS="sect1"
  6228. ><A
  6229. NAME="install.macosx.packages"
  6230. >Using Packages</A
  6231. ></H2
  6232. ><P
  6233. >    There are a few pre-packaged and pre-compiled versions of PHP for
  6234.    Mac OS X. This can help in setting up a standard
  6235.    configuration, but if you need to have a different set of features
  6236.    (such as a secure server, or a different database driver), you may
  6237.    need to build PHP and/or your web server yourself. If you are unfamiliar
  6238.    with building and compiling your own software, it's worth
  6239.    checking whether somebody has already built a packaged
  6240.    version of PHP with the features you need.
  6241.   </P
  6242. ></DIV
  6243. ><DIV
  6244. CLASS="sect1"
  6245. ><HR><H2
  6246. CLASS="sect1"
  6247. ><A
  6248. NAME="install.macosx.server"
  6249. >Compiling for OS X Server</A
  6250. ></H2
  6251. ><DIV
  6252. CLASS="formalpara"
  6253. ><P
  6254. ><A
  6255. NAME="install.macosx.server.compile"
  6256. ></A
  6257. ><B
  6258. >Mac OS X Server install. </B
  6259. >
  6260.     <P
  6261. ></P
  6262. ><OL
  6263. TYPE="1"
  6264. ><LI
  6265. ><P
  6266. >Get the latest distributions of Apache and PHP.</P
  6267. ></LI
  6268. ><LI
  6269. ><P
  6270. >        Untar them, and run the <B
  6271. CLASS="command"
  6272. >configure</B
  6273. > program on Apache
  6274.        like so.
  6275.        <TABLE
  6276. BORDER="0"
  6277. BGCOLOR="#E0E0E0"
  6278. CELLPADDING="5"
  6279. ><TR
  6280. ><TD
  6281. ><PRE
  6282. CLASS="shell"
  6283. >./configure --exec-prefix=/usr \
  6284. --localstatedir=/var \
  6285. --mandir=/usr/share/man \
  6286. --libexecdir=/System/Library/Apache/Modules \
  6287. --iconsdir=/System/Library/Apache/Icons \
  6288. --includedir=/System/Library/Frameworks/Apache.framework/Versions/1.3/Headers \
  6289. --enable-shared=max \
  6290. --enable-module=most \
  6291. --target=apache</PRE
  6292. ></TD
  6293. ></TR
  6294. ></TABLE
  6295. >
  6296.       </P
  6297. ></LI
  6298. ><LI
  6299. ><P
  6300. >        If you want the compiler to do some optimization, you may also want to
  6301.        add this line:
  6302.        <TABLE
  6303. BORDER="0"
  6304. BGCOLOR="#E0E0E0"
  6305. CELLPADDING="5"
  6306. ><TR
  6307. ><TD
  6308. ><PRE
  6309. CLASS="shell"
  6310. >setenv OPTIM=-O2</PRE
  6311. ></TD
  6312. ></TR
  6313. ></TABLE
  6314. >
  6315.       </P
  6316. ></LI
  6317. ><LI
  6318. ><P
  6319. >        Next, go to the PHP 4 source directory and configure it.
  6320.        <TABLE
  6321. BORDER="0"
  6322. BGCOLOR="#E0E0E0"
  6323. CELLPADDING="5"
  6324. ><TR
  6325. ><TD
  6326. ><PRE
  6327. CLASS="shell"
  6328. >./configure --prefix=/usr \
  6329.     --sysconfdir=/etc \
  6330.     --localstatedir=/var \
  6331.     --mandir=/usr/share/man \
  6332.     --with-xml \
  6333.     --with-apache=/src/apache_1.3.12</PRE
  6334. ></TD
  6335. ></TR
  6336. ></TABLE
  6337. >
  6338.        If you have any other additions (MySQL, GD, etc.), be sure to add them
  6339.        here. For the <VAR
  6340. CLASS="option"
  6341. >--with-apache</VAR
  6342. > string, put
  6343.        in the path to your apache source directory, for example
  6344.        <TT
  6345. CLASS="filename"
  6346. >/src/apache_1.3.12</TT
  6347. >.
  6348.       </P
  6349. ></LI
  6350. ><LI
  6351. ><P
  6352. >        Type <B
  6353. CLASS="command"
  6354. >make</B
  6355. > and <B
  6356. CLASS="command"
  6357. >make install</B
  6358. >. This
  6359.        will add a directory to your Apache source directory under
  6360.        <TT
  6361. CLASS="filename"
  6362. >src/modules/php4</TT
  6363. >.
  6364.       </P
  6365. ></LI
  6366. ><LI
  6367. ><P
  6368. >        Now, reconfigure Apache to build in PHP 4.
  6369.        <TABLE
  6370. BORDER="0"
  6371. BGCOLOR="#E0E0E0"
  6372. CELLPADDING="5"
  6373. ><TR
  6374. ><TD
  6375. ><PRE
  6376. CLASS="shell"
  6377. >./configure --exec-prefix=/usr \
  6378. --localstatedir=/var \
  6379. --mandir=/usr/share/man \
  6380. --libexecdir=/System/Library/Apache/Modules \
  6381. --iconsdir=/System/Library/Apache/Icons \
  6382. --includedir=/System/Library/Frameworks/Apache.framework/Versions/1.3/Headers \
  6383. --enable-shared=max \
  6384. --enable-module=most \
  6385. --target=apache \
  6386. --activate-module=src/modules/php4/libphp4.a</PRE
  6387. ></TD
  6388. ></TR
  6389. ></TABLE
  6390. >
  6391.        You may get a message telling you that <TT
  6392. CLASS="filename"
  6393. >libmodphp4.a</TT
  6394. > is
  6395.        out of date. If so, go to the <TT
  6396. CLASS="filename"
  6397. >src/modules/php4</TT
  6398. >
  6399.        directory inside your Apache source directory and run this command: 
  6400.        <B
  6401. CLASS="command"
  6402. >ranlib libmodphp4.a</B
  6403. >. Then go back to the root of the
  6404.        Apache source directory and run the above <B
  6405. CLASS="command"
  6406. >configure</B
  6407. >
  6408.        command again. That'll bring the link table up to date. Run
  6409.        <B
  6410. CLASS="command"
  6411. >make</B
  6412. > and <B
  6413. CLASS="command"
  6414. >make install</B
  6415. > again.
  6416.       </P
  6417. ></LI
  6418. ><LI
  6419. ><P
  6420. >        Copy and rename the <TT
  6421. CLASS="filename"
  6422. >php.ini-dist</TT
  6423. > file to your
  6424.        <TT
  6425. CLASS="filename"
  6426. >bin</TT
  6427. > directory from your PHP 4
  6428.        source directory: 
  6429.        <KBD
  6430. CLASS="userinput"
  6431. >cp php.ini-dist /usr/local/bin/php.ini</KBD
  6432. >
  6433.        or (if your don't have a local directory)
  6434.        <KBD
  6435. CLASS="userinput"
  6436. >cp php.ini-dist /usr/bin/php.ini</KBD
  6437. >.
  6438.       </P
  6439. ></LI
  6440. ></OL
  6441. >
  6442.    </P
  6443. ></DIV
  6444. ></DIV
  6445. ><DIV
  6446. CLASS="sect1"
  6447. ><HR><H2
  6448. CLASS="sect1"
  6449. ><A
  6450. NAME="install.macosx.client"
  6451. >Compiling for MacOS X Client</A
  6452. ></H2
  6453. ><P
  6454. >    The following instructions will help you install a PHP module for the Apache
  6455.    web server included in MacOS X. This version includes support for the MySQL
  6456.    and PostgreSQL databases. These instructions are graciously provided by
  6457.    <A
  6458. HREF="http://www.entropy.ch/software/macosx/"
  6459. TARGET="_top"
  6460. >Marc Liyanage</A
  6461. >.
  6462.   </P
  6463. ><DIV
  6464. CLASS="warning"
  6465. ><P
  6466. ></P
  6467. ><TABLE
  6468. CLASS="warning"
  6469. BORDER="1"
  6470. WIDTH="100%"
  6471. ><TR
  6472. ><TD
  6473. ALIGN="CENTER"
  6474. ><B
  6475. >Warning</B
  6476. ></TD
  6477. ></TR
  6478. ><TR
  6479. ><TD
  6480. ALIGN="LEFT"
  6481. ><P
  6482. >     Be careful when you do this, you could screw up your Apache web server!
  6483.    </P
  6484. ></TD
  6485. ></TR
  6486. ></TABLE
  6487. ></DIV
  6488. ><P
  6489. >    Do this to install:
  6490.    <P
  6491. ></P
  6492. ><OL
  6493. TYPE="1"
  6494. ><LI
  6495. ><P
  6496. >       Open a terminal window.
  6497.      </P
  6498. ></LI
  6499. ><LI
  6500. ><P
  6501. >       Type
  6502.       <KBD
  6503. CLASS="userinput"
  6504. >wget http://www.diax.ch/users/liyanage/software/macosx/libphp4.so.gz</KBD
  6505. >,
  6506.       wait for the download to finish.
  6507.      </P
  6508. ></LI
  6509. ><LI
  6510. ><P
  6511. >       Type <KBD
  6512. CLASS="userinput"
  6513. >gunzip libphp4.so.gz</KBD
  6514. >.
  6515.      </P
  6516. ></LI
  6517. ><LI
  6518. ><P
  6519. >       Type <KBD
  6520. CLASS="userinput"
  6521. >sudo apxs -i -a -n php4 libphp4.so</KBD
  6522. >
  6523.      </P
  6524. ></LI
  6525. ><LI
  6526. ><P
  6527. >       Now type <KBD
  6528. CLASS="userinput"
  6529. >sudo open -a TextEdit /etc/httpd/httpd.conf</KBD
  6530. >.
  6531.       TextEdit will open with the web server configuration file. Locate these 
  6532.       two lines towards the end of the file: (Use the Find command)
  6533.       <TABLE
  6534. BORDER="0"
  6535. BGCOLOR="#E0E0E0"
  6536. CELLPADDING="5"
  6537. ><TR
  6538. ><TD
  6539. ><PRE
  6540. CLASS="apache"
  6541. >#AddType application/x-httpd-php .php 
  6542. #AddType application/x-httpd-php-source .phps</PRE
  6543. ></TD
  6544. ></TR
  6545. ></TABLE
  6546. >
  6547.        Remove the two hash marks (<VAR
  6548. CLASS="literal"
  6549. >#</VAR
  6550. >), then save the file and
  6551.        quit TextEdit.
  6552.       </P
  6553. ></LI
  6554. ><LI
  6555. ><P
  6556. >       Finally, type <KBD
  6557. CLASS="userinput"
  6558. >sudo apachectl graceful</KBD
  6559. > to restart
  6560.       the web server.
  6561.      </P
  6562. ></LI
  6563. ></OL
  6564. >
  6565.   </P
  6566. ><P
  6567. >    PHP should now be up and running. You can test it by dropping a file into 
  6568.    your <TT
  6569. CLASS="filename"
  6570. >Sites</TT
  6571. > folder which is called
  6572.    <TT
  6573. CLASS="filename"
  6574. >test.php</TT
  6575. >. Into that file, write this line:
  6576.    <VAR
  6577. CLASS="literal"
  6578. ><?php phpinfo() ?></VAR
  6579. >.
  6580.   </P
  6581. ><P
  6582. >    Now open up <VAR
  6583. CLASS="literal"
  6584. >127.0.0.1/~your_username/test.php</VAR
  6585. > in your web
  6586.    browser. You should see a status table with information about the PHP module.
  6587.   </P
  6588. ></DIV
  6589. ></DIV
  6590. ><DIV
  6591. CLASS="chapter"
  6592. ><HR><H1
  6593. ><A
  6594. NAME="install.windows"
  6595. >Chapter 6. Installation on Windows systems</A
  6596. ></H1
  6597. ><P
  6598. >     This section applies to Windows 98/Me and Windows NT/2000/XP/2003. PHP 
  6599.     will not work on 16 bit platforms such as Windows 3.1 and sometimes 
  6600.     we refer to the supported Windows platforms as Win32. Windows 95
  6601.     is no longer supported as of PHP 4.3.0.
  6602.    </P
  6603. ><P
  6604. >     There are two main ways to install PHP for Windows: either
  6605.     <A
  6606. HREF="#install.windows.manual"
  6607. >manually</A
  6608. >
  6609.     or by using the <A
  6610. HREF="#install.windows.installer"
  6611. >installer</A
  6612. >.
  6613.    </P
  6614. ><P
  6615. >     If you have Microsoft Visual Studio, you can also 
  6616.     <A
  6617. HREF="#install.windows.building"
  6618. >build</A
  6619. >
  6620.     PHP from the original source code.
  6621.    </P
  6622. ><P
  6623. >     Once you have PHP installed on your Windows system, you may also
  6624.     want to <A
  6625. HREF="#install.windows.extensions"
  6626. >load various extensions</A
  6627. >
  6628.     for added functionality.
  6629.    </P
  6630. ><DIV
  6631. CLASS="warning"
  6632. ><P
  6633. ></P
  6634. ><TABLE
  6635. CLASS="warning"
  6636. BORDER="1"
  6637. WIDTH="100%"
  6638. ><TR
  6639. ><TD
  6640. ALIGN="CENTER"
  6641. ><B
  6642. >Warning</B
  6643. ></TD
  6644. ></TR
  6645. ><TR
  6646. ><TD
  6647. ALIGN="LEFT"
  6648. ><P
  6649. >      There are several all-in-one installers over the Internet, but none of
  6650.      those are endorsed by PHP.net, as we believe that the manual installation
  6651.      is the best choice to have your system secure and optimised.
  6652.     </P
  6653. ></TD
  6654. ></TR
  6655. ></TABLE
  6656. ></DIV
  6657. ><DIV
  6658. CLASS="sect1"
  6659. ><HR><H2
  6660. CLASS="sect1"
  6661. ><A
  6662. NAME="install.windows.installer"
  6663. >Windows Installer</A
  6664. ></H2
  6665. ><P
  6666. >      The Windows PHP installer is available from the downloads page at
  6667.      <A
  6668. HREF="http://www.php.net/downloads.php"
  6669. TARGET="_top"
  6670. >http://www.php.net/downloads.php</A
  6671. >. This
  6672.      installs the <SPAN
  6673. CLASS="emphasis"
  6674. ><I
  6675. CLASS="emphasis"
  6676. >CGI version</I
  6677. ></SPAN
  6678. > of PHP and for IIS, PWS,
  6679.      and Xitami, it configures the web server as well.  The installer does not 
  6680.      include any extra external PHP extensions (php_*.dll) as you'll only find 
  6681.      those in the Windows Zip Package and <ACRONYM
  6682. CLASS="acronym"
  6683. >PECL</ACRONYM
  6684. > downloads.
  6685.     </P
  6686. ><DIV
  6687. CLASS="note"
  6688. ><BLOCKQUOTE
  6689. CLASS="note"
  6690. ><P
  6691. ><B
  6692. >Note: </B
  6693. >
  6694.       While the Windows installer is an easy way to make PHP work, it is 
  6695.       restricted in many aspects as, for example, the automatic setup of 
  6696.       extensions is not supported.  Use of the installer isn't the preferred 
  6697.       method for installing PHP.
  6698.      </P
  6699. ></BLOCKQUOTE
  6700. ></DIV
  6701. ><P
  6702. >      First, install your selected <ACRONYM
  6703. CLASS="acronym"
  6704. >HTTP</ACRONYM
  6705. > (web) server on your 
  6706.      system, and make sure that it works.
  6707.     </P
  6708. ><P
  6709. >      Run the executable installer and follow the instructions provided by the
  6710.      installation wizard. Two types of installation are supported - standard,
  6711.      which provides sensible defaults for all the settings it can, and advanced,
  6712.      which asks questions as it goes along.
  6713.     </P
  6714. ><P
  6715. >      The installation wizard gathers enough information to set up the <TT
  6716. CLASS="filename"
  6717. >php.ini</TT
  6718. >
  6719.      file, and configure certain web servers to use PHP. One of the web servers
  6720.      the PHP installer does not configure for is Apache, so you'll need to
  6721.      configure it manually.
  6722.     </P
  6723. ><P
  6724. >      Once the installation has completed, the installer will inform you if you
  6725.      need to restart your system, restart the server, or just start using PHP.
  6726.     </P
  6727. ><DIV
  6728. CLASS="warning"
  6729. ><P
  6730. ></P
  6731. ><TABLE
  6732. CLASS="warning"
  6733. BORDER="1"
  6734. WIDTH="100%"
  6735. ><TR
  6736. ><TD
  6737. ALIGN="CENTER"
  6738. ><B
  6739. >Warning</B
  6740. ></TD
  6741. ></TR
  6742. ><TR
  6743. ><TD
  6744. ALIGN="LEFT"
  6745. ><P
  6746. >       Be aware, that this setup of PHP is not secure. If you would like to have
  6747.       a secure PHP setup, you'd better go on the manual way, and set every
  6748.       option carefully. This automatically working setup gives you an instantly
  6749.       working PHP installation, but it is not meant to be used on online servers.
  6750.      </P
  6751. ></TD
  6752. ></TR
  6753. ></TABLE
  6754. ></DIV
  6755. ></DIV
  6756. ><DIV
  6757. CLASS="sect1"
  6758. ><HR><H2
  6759. CLASS="sect1"
  6760. ><A
  6761. NAME="install.windows.manual"
  6762. >Manual Installation Steps</A
  6763. ></H2
  6764. ><P
  6765. >      This install guide will help you manually install and configure PHP with
  6766.      a web server on Microsoft Windows.  To get started you'll need to download 
  6767.      the zip binary distribution from the downloads page at
  6768.      <A
  6769. HREF="http://www.php.net/downloads.php"
  6770. TARGET="_top"
  6771. >http://www.php.net/downloads.php</A
  6772. >.
  6773.     </P
  6774. ><P
  6775. >      Although there are many all-in-one installation kits, and we also 
  6776.      distribute a PHP installer for Microsoft Windows, we recommend you take 
  6777.      the time to setup PHP yourself as this will provide you with a better 
  6778.      understanding of the system, and enables you to install PHP extensions 
  6779.      easily when needed.
  6780.     </P
  6781. ><DIV
  6782. CLASS="note"
  6783. ><BLOCKQUOTE
  6784. CLASS="note"
  6785. ><A
  6786. NAME="install.windows.manual.upgrade"
  6787. ></A
  6788. ><P
  6789. ><B
  6790. >Upgrading from a previous PHP version: </B
  6791. >
  6792.       Previous editions of the manual suggest moving various ini and 
  6793.       <ACRONYM
  6794. CLASS="acronym"
  6795. >DLL</ACRONYM
  6796. > files into your SYSTEM (i.e. 
  6797.       <TT
  6798. CLASS="filename"
  6799. >C:\WINDOWS</TT
  6800. >) folder and while this 
  6801.       simplifies the installation procedure it makes upgrading difficult.  We 
  6802.       advise you remove all of these files (like <TT
  6803. CLASS="filename"
  6804. >php.ini</TT
  6805. > and PHP related
  6806.       DLLs from the Windows SYSTEM folder) before moving on with a new 
  6807.       PHP installation.  Be sure to backup these files as you might break the
  6808.       entire system.  The old <TT
  6809. CLASS="filename"
  6810. >php.ini</TT
  6811. > might be useful in setting up the new
  6812.       PHP as well.  And as you'll soon learn, the preferred method for 
  6813.       installing PHP is to keep all PHP related files in one directory and
  6814.       have this directory available to your systems PATH.
  6815.      </P
  6816. ></BLOCKQUOTE
  6817. ></DIV
  6818. ><DIV
  6819. CLASS="note"
  6820. ><BLOCKQUOTE
  6821. CLASS="note"
  6822. ><P
  6823. ><B
  6824. >MDAC requirements: </B
  6825. >
  6826.       If you use Microsoft <SPAN
  6827. CLASS="emphasis"
  6828. ><I
  6829. CLASS="emphasis"
  6830. >Windows 98/NT4</I
  6831. ></SPAN
  6832. > download the
  6833.       latest version of the Microsoft Data Access Components (MDAC) for your
  6834.       platform. MDAC is available at <A
  6835. HREF="http://msdn.microsoft.com/data/"
  6836. TARGET="_top"
  6837. >http://msdn.microsoft.com/data/</A
  6838. >.
  6839.       This requirement exists because <A
  6840. HREF="#ref.uodbc"
  6841. >ODBC</A
  6842. > is
  6843.       built into the distributed Windows binaries.
  6844.      </P
  6845. ></BLOCKQUOTE
  6846. ></DIV
  6847. ><P
  6848. >      The following steps should be completed on all installations before any
  6849.      server specific instructions are performed:
  6850.     </P
  6851. ><P
  6852. >      Extract the distribution file into a directory of your choice. If you
  6853.      are installing PHP 4, extract to <TT
  6854. CLASS="filename"
  6855. >C:\</TT
  6856. >, as the zip file expands to a
  6857.      foldername like <TT
  6858. CLASS="filename"
  6859. >php-4.3.7-Win32</TT
  6860. >. If you are
  6861.      installing PHP 5, extract to <TT
  6862. CLASS="filename"
  6863. >C:\php</TT
  6864. > as the zip file doesn't expand as in
  6865.      PHP 4. You may choose a different location but do not have spaces in the
  6866.      path (like <TT
  6867. CLASS="filename"
  6868. >C:\Program Files\PHP</TT
  6869. >)
  6870.      as some web servers will crash if you do.
  6871.     </P
  6872. ><P
  6873. >      The directory structure extracted from the zip is different for PHP 
  6874.      versions 4 and 5 and look like as follows:
  6875.     </P
  6876. ><TABLE
  6877. WIDTH="100%"
  6878. BORDER="0"
  6879. CELLPADDING="0"
  6880. CELLSPACING="0"
  6881. CLASS="EXAMPLE"
  6882. ><TR
  6883. ><TD
  6884. ><DIV
  6885. CLASS="example"
  6886. ><A
  6887. NAME="AEN1041"
  6888. ></A
  6889. ><P
  6890. ><B
  6891. >Example 6-1. PHP 4 package structure</B
  6892. ></P
  6893. ><TABLE
  6894. BORDER="0"
  6895. BGCOLOR="#E0E0E0"
  6896. CELLPADDING="5"
  6897. ><TR
  6898. ><TD
  6899. ><PRE
  6900. CLASS="programlisting"
  6901. >c:\php
  6902.    |
  6903.    +--cli
  6904.    |  |
  6905.    |  |-php.exe           -- CLI executable - ONLY for commandline scripting
  6906.    |
  6907.    +--dlls                -- support DLLs required by some extensions
  6908.    |  |
  6909.    |  |-expat.dll
  6910.    |  |
  6911.    |  |-fdftk.dll
  6912.    |  |
  6913.    |  |-...
  6914.    |
  6915.    +--extensions          -- extension DLLs for PHP
  6916.    |  |
  6917.    |  |-php_bz2.dll
  6918.    |  |
  6919.    |  |-php_cpdf.dll
  6920.    |  |
  6921.    |  |-..
  6922.    |
  6923.    +--mibs                -- support files for SNMP
  6924.    |
  6925.    +--openssl             -- support files for Openssl
  6926.    |
  6927.    +--pdf-related         -- support files for PDF
  6928.    |
  6929.    +--sapi                -- SAPI (server module support) DLLs
  6930.    |  |
  6931.    |  |-php4activescript.dll
  6932.    |  |
  6933.    |  |-php4apache.dll
  6934.    |  |
  6935.    |  |-php4apache2.dll
  6936.    |  |
  6937.    |  |-..
  6938.    |
  6939.    +--PEAR                -- initial copy of PEAR
  6940.    |
  6941.    |
  6942.    |-go-pear.bat          -- PEAR setup script
  6943.    |
  6944.    |-..
  6945.    |
  6946.    |-php.exe              -- CGI executable
  6947.    |
  6948.    |-..
  6949.    |
  6950.    |-php.ini-dist         -- default php.ini settings
  6951.    |
  6952.    |-php.ini-recommended  -- recommended php.ini settings
  6953.    | 
  6954.    |-php4ts.dll           -- core PHP DLL
  6955.    | 
  6956.    |-...</PRE
  6957. ></TD
  6958. ></TR
  6959. ></TABLE
  6960. ></DIV
  6961. ></TD
  6962. ></TR
  6963. ></TABLE
  6964. ><P
  6965. >     Or:
  6966.     </P
  6967. ><TABLE
  6968. WIDTH="100%"
  6969. BORDER="0"
  6970. CELLPADDING="0"
  6971. CELLSPACING="0"
  6972. CLASS="EXAMPLE"
  6973. ><TR
  6974. ><TD
  6975. ><DIV
  6976. CLASS="example"
  6977. ><A
  6978. NAME="AEN1045"
  6979. ></A
  6980. ><P
  6981. ><B
  6982. >Example 6-2. PHP 5 package structure</B
  6983. ></P
  6984. ><TABLE
  6985. BORDER="0"
  6986. BGCOLOR="#E0E0E0"
  6987. CELLPADDING="5"
  6988. ><TR
  6989. ><TD
  6990. ><PRE
  6991. CLASS="programlisting"
  6992. >c:\php
  6993.    |
  6994.    +--dev
  6995.    |  |
  6996.    |  |-php5ts.lib
  6997.    |
  6998.    +--ext                 -- extension DLLs for PHP
  6999.    |  |
  7000.    |  |-php_bz2.dll
  7001.    |  |
  7002.    |  |-php_cpdf.dll
  7003.    |  |
  7004.    |  |-..
  7005.    |
  7006.    +--extras
  7007.    |  |
  7008.    |  +--mibs             -- support files for SNMP
  7009.    |  |
  7010.    |  +--openssl          -- support files for Openssl
  7011.    |  |
  7012.    |  +--pdf-related      -- support files for PDF
  7013.    |  |
  7014.    |  |-mime.magic
  7015.    |
  7016.    +--pear                -- initial copy of PEAR
  7017.    |
  7018.    |
  7019.    |-go-pear.bat          -- PEAR setup script
  7020.    |
  7021.    |-fdftk.dll
  7022.    |
  7023.    |-..
  7024.    |
  7025.    |-php-cgi.exe          -- CGI executable
  7026.    |
  7027.    |-php-win.exe          -- executes scripts without an opened command prompt
  7028.    |
  7029.    |-php.exe              -- CLI executable - ONLY for command line scripting
  7030.    |
  7031.    |-..
  7032.    |
  7033.    |-php.ini-dist         -- default php.ini settings
  7034.    |
  7035.    |-php.ini-recommended  -- recommended php.ini settings
  7036.    | 
  7037.    |-php5activescript.dll
  7038.    |
  7039.    |-php5apache.dll
  7040.    |
  7041.    |-php5apache2.dll
  7042.    |
  7043.    |-..
  7044.    |
  7045.    |-php5ts.dll           -- core PHP DLL
  7046.    | 
  7047.    |-...</PRE
  7048. ></TD
  7049. ></TR
  7050. ></TABLE
  7051. ></DIV
  7052. ></TD
  7053. ></TR
  7054. ></TABLE
  7055. ><P
  7056. >      Notice the differences and similarities.  Both PHP 4 and PHP 5 have a 
  7057.      <ACRONYM
  7058. CLASS="acronym"
  7059. >CGI</ACRONYM
  7060. > executable, a <ACRONYM
  7061. CLASS="acronym"
  7062. >CLI</ACRONYM
  7063. > executable,
  7064.      and server modules, but they are located in different folders and/or have
  7065.      different names.  While PHP 4 packages have the server modules in the 
  7066.      <TT
  7067. CLASS="filename"
  7068. >sapi</TT
  7069. > folder, PHP 5 
  7070.      distributions have no such directory and instead they're in the PHP
  7071.      folder root. The supporting DLLs for the PHP 5 extensions are also not
  7072.      in a seperate directory.
  7073.     </P
  7074. ><DIV
  7075. CLASS="note"
  7076. ><BLOCKQUOTE
  7077. CLASS="note"
  7078. ><P
  7079. ><B
  7080. >Note: </B
  7081. >
  7082.       In PHP 4, you should move all files located in the <TT
  7083. CLASS="filename"
  7084. >dll</TT
  7085. > and <TT
  7086. CLASS="filename"
  7087. >sapi</TT
  7088. > folders to the main folder (e.g.
  7089.       <TT
  7090. CLASS="filename"
  7091. >C:\php</TT
  7092. >).
  7093.      </P
  7094. ></BLOCKQUOTE
  7095. ></DIV
  7096. ><P
  7097. >      Here is a list of server modules shipped with PHP 4 and PHP 5:
  7098.      <P
  7099. ></P
  7100. ><UL
  7101. ><LI
  7102. ><P
  7103. >        <TT
  7104. CLASS="filename"
  7105. >sapi/php4activescript.dll (php5activescript.dll)</TT
  7106. >
  7107.        - <A
  7108. HREF="#install.windows.activescript"
  7109. >ActiveScript
  7110.         engine</A
  7111. >, allowing you to embed PHP in your Windows
  7112.        applications.
  7113.       </P
  7114. ></LI
  7115. ><LI
  7116. ><P
  7117. >        <TT
  7118. CLASS="filename"
  7119. >sapi/php4apache.dll (php5apache.dll)</TT
  7120. > - Apache 1.3.x module.
  7121.       </P
  7122. ></LI
  7123. ><LI
  7124. ><P
  7125. >        <TT
  7126. CLASS="filename"
  7127. >sapi/php4apache2.dll (php5apache2.dll)</TT
  7128. > - Apache 2.0.x module.
  7129.       </P
  7130. ></LI
  7131. ><LI
  7132. ><P
  7133. >        <TT
  7134. CLASS="filename"
  7135. >sapi/php4isapi.dll (php5isapi.dll)</TT
  7136. > - ISAPI Module
  7137.        for ISAPI compliant web servers like IIS 4.0/PWS 4.0 or newer.
  7138.       </P
  7139. ></LI
  7140. ><LI
  7141. ><P
  7142. >        <TT
  7143. CLASS="filename"
  7144. >sapi/php4nsapi.dll (php5nsapi.dll)</TT
  7145. > - Sun/iPlanet/Netscape
  7146.        server module.
  7147.       </P
  7148. ></LI
  7149. ><LI
  7150. ><P
  7151. >        <TT
  7152. CLASS="filename"
  7153. >sapi/php4pi3web.dll (no equivalent in PHP 5)</TT
  7154. > - Pi3Web server module.
  7155.       </P
  7156. ></LI
  7157. ></UL
  7158. >
  7159.     </P
  7160. ><P
  7161. >      Server modules provide significantly better performance and additional 
  7162.      functionality compared to the CGI binary. The CLI version is designed to
  7163.      let you use PHP for command line scripting. More information about CLI is
  7164.      available in the chapter about <A
  7165. HREF="#features.commandline"
  7166. >using
  7167.      PHP from the command line</A
  7168. >.
  7169.     </P
  7170. ><DIV
  7171. CLASS="warning"
  7172. ><P
  7173. ></P
  7174. ><TABLE
  7175. CLASS="warning"
  7176. BORDER="1"
  7177. WIDTH="100%"
  7178. ><TR
  7179. ><TD
  7180. ALIGN="CENTER"
  7181. ><B
  7182. >Warning</B
  7183. ></TD
  7184. ></TR
  7185. ><TR
  7186. ><TD
  7187. ALIGN="LEFT"
  7188. ><P
  7189. >       The SAPI modules have been significantly improved as of the 4.1 release,
  7190.       however, in older systems you may encounter server errors or other
  7191.       server modules failing, such as ASP.
  7192.      </P
  7193. ></TD
  7194. ></TR
  7195. ></TABLE
  7196. ></DIV
  7197. ><P
  7198. >      The CGI and CLI binaries, and the web server modules all require the
  7199.      <TT
  7200. CLASS="filename"
  7201. >php4ts.dll</TT
  7202. > (<TT
  7203. CLASS="filename"
  7204. >php5ts.dll</TT
  7205. >) file to
  7206.      be available to them. You have to make sure that this file can be found
  7207.      by your PHP installation. The search order for this DLL is as follows:
  7208.      <P
  7209. ></P
  7210. ><UL
  7211. ><LI
  7212. ><P
  7213. >         The same directory from where <TT
  7214. CLASS="filename"
  7215. >php.exe</TT
  7216. > is called,
  7217.         or in case you use a SAPI module, the web server's directory (e.g.
  7218.         <TT
  7219. CLASS="filename"
  7220. >C:\Program Files\Apache Group\Apache2\bin</TT
  7221. >).
  7222.        </P
  7223. ></LI
  7224. ><LI
  7225. ><P
  7226. >         Any directory in your Windows <VAR
  7227. CLASS="varname"
  7228. >PATH</VAR
  7229. > environment
  7230.         variable.
  7231.        </P
  7232. ></LI
  7233. ></UL
  7234. >
  7235.     </P
  7236. ><P
  7237. >      To make <TT
  7238. CLASS="filename"
  7239. >php4ts.dll</TT
  7240. > / <TT
  7241. CLASS="filename"
  7242. >php5ts.dll</TT
  7243. >
  7244.      available you have three options: copy the file to the Windows system
  7245.      directory, copy the file to the web server's directory, or add your PHP
  7246.      directory, <TT
  7247. CLASS="filename"
  7248. >C:\php</TT
  7249. > to the
  7250.      <VAR
  7251. CLASS="varname"
  7252. >PATH</VAR
  7253. >. For better maintenance, we advise you to follow
  7254.      the last option, add <TT
  7255. CLASS="filename"
  7256. >C:\php</TT
  7257. > to the
  7258.      <VAR
  7259. CLASS="varname"
  7260. >PATH</VAR
  7261. >, because it will be simpler to upgrade PHP in the
  7262.      future. Read more about how to add your PHP directory to
  7263.      <VAR
  7264. CLASS="varname"
  7265. >PATH</VAR
  7266. > in the <A
  7267. HREF="#faq.installation.addtopath"
  7268. >corresponding FAQ entry</A
  7269. >.
  7270.     </P
  7271. ><P
  7272. >      The next step is to set up a valid configuration file for PHP, <TT
  7273. CLASS="filename"
  7274. >php.ini</TT
  7275. >.
  7276.      There are two ini files distributed in the zip file,
  7277.      <TT
  7278. CLASS="filename"
  7279. >php.ini-dist</TT
  7280. > and
  7281.      <TT
  7282. CLASS="filename"
  7283. >php.ini-recommended</TT
  7284. >. We advise you to use
  7285.      <TT
  7286. CLASS="filename"
  7287. >php.ini-recommended</TT
  7288. >, because we optimized the
  7289.      default settings in this file for performance, and security. Read this
  7290.      well documented file carefully because it has changes from
  7291.      <TT
  7292. CLASS="filename"
  7293. >php.ini-dist</TT
  7294. > that will drastically affect your 
  7295.      setup.  Some examples are <A
  7296. HREF="#ini.display-errors"
  7297. >      display_errors</A
  7298. > being <VAR
  7299. CLASS="literal"
  7300. >off</VAR
  7301. > and 
  7302.      <A
  7303. HREF="#ini.magic-quotes-gpc"
  7304. >magic_quotes_gpc</A
  7305. > being
  7306.      <VAR
  7307. CLASS="literal"
  7308. >off</VAR
  7309. >.  In addition to reading these, study the <A
  7310. HREF="#configuration.file"
  7311. >ini settings</A
  7312. > and set every element
  7313.      manually yourself. If you would like to achieve the best security, then
  7314.      this is the way for you, although PHP works fine with these default ini
  7315.      files. Copy your chosen ini-file to a directory that PHP is able to find
  7316.      and rename it to <TT
  7317. CLASS="filename"
  7318. >php.ini</TT
  7319. >. PHP searches for <TT
  7320. CLASS="filename"
  7321. >php.ini</TT
  7322. > in the following
  7323.      locations (in order):
  7324.      <P
  7325. ></P
  7326. ><UL
  7327. ><LI
  7328. ><P
  7329. >         PHPIniDir directive (Apache 2 module only)
  7330.        </P
  7331. ></LI
  7332. ><LI
  7333. ><P
  7334. >         <VAR
  7335. CLASS="literal"
  7336. >HKEY_LOCAL_MACHINE\SOFTWARE\PHP\IniFilePath</VAR
  7337. >
  7338.        </P
  7339. ></LI
  7340. ><LI
  7341. ><P
  7342. >         The <VAR
  7343. CLASS="varname"
  7344. >PHPRC</VAR
  7345. > environment variable
  7346.        </P
  7347. ></LI
  7348. ><LI
  7349. ><P
  7350. >         Directory of PHP (for CLI), or the web server's directory (for 
  7351.         SAPI modules)
  7352.        </P
  7353. ></LI
  7354. ><LI
  7355. ><P
  7356. >         Windows directory (<TT
  7357. CLASS="filename"
  7358. >C:\windows</TT
  7359. >
  7360.         or <TT
  7361. CLASS="filename"
  7362. >C:\winnt</TT
  7363. >)
  7364.        </P
  7365. ></LI
  7366. ></UL
  7367.     </P
  7368. ><P
  7369. >      If you are running Apache 2, the simpler option is to use the PHPIniDir
  7370.      directive (read the <A
  7371. HREF="#install.windows.apache2"
  7372. >installation
  7373.      on Apache 2</A
  7374. > page), otherwise your best option is to set the
  7375.      <VAR
  7376. CLASS="varname"
  7377. >PHPRC</VAR
  7378. > environment variable. This process is explained
  7379.      in the following <A
  7380. HREF="#faq.installation.phprc"
  7381. >FAQ entry</A
  7382. >.
  7383.     </P
  7384. ><DIV
  7385. CLASS="note"
  7386. ><BLOCKQUOTE
  7387. CLASS="note"
  7388. ><P
  7389. ><B
  7390. >Note: </B
  7391. >
  7392.       If you're using NTFS on Windows NT, 2000, XP or 2003, make sure that the
  7393.       user running the web server has read permissions to your <TT
  7394. CLASS="filename"
  7395. >php.ini</TT
  7396. > (e.g.
  7397.       make it readable by Everyone).
  7398.      </P
  7399. ></BLOCKQUOTE
  7400. ></DIV
  7401. ><P
  7402. >      The following steps are optional:
  7403.      <P
  7404. ></P
  7405. ><UL
  7406. ><LI
  7407. ><P
  7408. >         Edit your new <TT
  7409. CLASS="filename"
  7410. >php.ini</TT
  7411. > file.       
  7412.         If you plan to use <A
  7413. HREF="#install.windows.omnihttpd"
  7414. >OmniHTTPd</A
  7415. >,
  7416.         do not follow the next step. Set the
  7417.         <A
  7418. HREF="#ini.doc-root"
  7419. >doc_root</A
  7420. > to point to your 
  7421.         web servers document_root. For example:
  7422.         <DIV
  7423. CLASS="informalexample"
  7424. ><P
  7425. ></P
  7426. ><A
  7427. NAME="AEN1144"
  7428. ></A
  7429. ><TABLE
  7430. BORDER="0"
  7431. BGCOLOR="#E0E0E0"
  7432. CELLPADDING="5"
  7433. ><TR
  7434. ><TD
  7435. ><PRE
  7436. CLASS="ini"
  7437. >doc_root = c:\inetpub\wwwroot // for IIS/PWS
  7438.  
  7439. doc_root = c:\apache\htdocs // for Apache</PRE
  7440. ></TD
  7441. ></TR
  7442. ></TABLE
  7443. ><P
  7444. ></P
  7445. ></DIV
  7446. >
  7447.        </P
  7448. ></LI
  7449. ><LI
  7450. ><P
  7451. >         Choose the extensions you would like to load when PHP starts. See
  7452.         the section about
  7453.         <A
  7454. HREF="#install.windows.extensions"
  7455. >Windows extensions</A
  7456. >,
  7457.         about how to set up one, and what is already built in. Note that on
  7458.         a new installation it is advisable to first get PHP working and tested
  7459.         without any extensions before enabling them in <TT
  7460. CLASS="filename"
  7461. >php.ini</TT
  7462. >.
  7463.        </P
  7464. ></LI
  7465. ><LI
  7466. ><P
  7467. >         On PWS and IIS, you can set the
  7468.         <A
  7469. HREF="#ini.browscap"
  7470. >browscap</A
  7471. > configuration setting
  7472.         to point to:
  7473.         <TT
  7474. CLASS="filename"
  7475. >c:\windows\system\inetsrv\browscap.ini</TT
  7476. > on
  7477.         Windows 9x/Me,
  7478.         <TT
  7479. CLASS="filename"
  7480. >c:\winnt\system32\inetsrv\browscap.ini</TT
  7481. > on
  7482.         NT/2000, and
  7483.         <TT
  7484. CLASS="filename"
  7485. >c:\windows\system32\inetsrv\browscap.ini</TT
  7486. >
  7487.         on XP.  For an up-to-date <TT
  7488. CLASS="filename"
  7489. >browscap.ini</TT
  7490. >, read the
  7491.         following <A
  7492. HREF="#faq.obtaining.browscap"
  7493. >FAQ</A
  7494. >.
  7495.        </P
  7496. ></LI
  7497. ></UL
  7498. >
  7499.     </P
  7500. ><P
  7501. >      PHP is now setup on your system.  The next step is to choose a web
  7502.      server, and enable it to run PHP.  Choose a webserver from the table of 
  7503.      contents.
  7504.     </P
  7505. ></DIV
  7506. ><DIV
  7507. CLASS="sect1"
  7508. ><HR><H2
  7509. CLASS="sect1"
  7510. ><A
  7511. NAME="install.windows.activescript"
  7512. >ActiveScript</A
  7513. ></H2
  7514. ><P
  7515. >      This section contains notes specific to the ActiveScript installation.
  7516.     </P
  7517. ><P
  7518. >      ActiveScript is a windows only SAPI that enables you to use PHP script in
  7519.      any ActiveScript compliant host, like Windows Script Host, ASP/ASP.NET,
  7520.      Windows Script Components or Microsoft Scriptlet control.
  7521.     </P
  7522. ><P
  7523. >      As of PHP 5.0.1, ActiveScript has been moved to the <A
  7524. HREF="http://pecl.php.net"
  7525. TARGET="_top"
  7526. >PECL</A
  7527. > repository. You may download this <ACRONYM
  7528. CLASS="acronym"
  7529. >PECL</ACRONYM
  7530. >
  7531. extension <ACRONYM
  7532. CLASS="acronym"
  7533. >DLL</ACRONYM
  7534. > from the <A
  7535. HREF="http://www.php.net/downloads.php"
  7536. TARGET="_top"
  7537. > PHP Downloads</A
  7538. > page or at <A
  7539. HREF="http://snaps.php.net/"
  7540. TARGET="_top"
  7541. > http://snaps.php.net/</A
  7542. >.
  7543.     </P
  7544. ><DIV
  7545. CLASS="note"
  7546. ><BLOCKQUOTE
  7547. CLASS="note"
  7548. ><P
  7549. ><B
  7550. >Note: </B
  7551. >
  7552.       You should read the <A
  7553. HREF="#install.windows.manual"
  7554. >manual
  7555.        installation steps</A
  7556. > first!
  7557.      </P
  7558. ></BLOCKQUOTE
  7559. ></DIV
  7560. ><P
  7561. >      After installing PHP, you should download the ActiveScript DLL
  7562.      (<TT
  7563. CLASS="filename"
  7564. >php5activescript.dll</TT
  7565. >) and place it in the main PHP
  7566.      folder (e.g. <TT
  7567. CLASS="filename"
  7568. >C:\php</TT
  7569. >).
  7570.     </P
  7571. ><P
  7572. >      After having all the files needed, you must register the DLL on your
  7573.      system. To achieve this, open a Command Prompt window (located in the
  7574.      Start Menu). Then go to your PHP directory by typing something like
  7575.      <VAR
  7576. CLASS="literal"
  7577. >cd C:\php</VAR
  7578. >. To register the DLL just type
  7579.      <VAR
  7580. CLASS="literal"
  7581. >regsvr32 php5activescript.dll</VAR
  7582. >.
  7583.     </P
  7584. ><P
  7585. >      To test if ActiveScript is working, create a new file, named
  7586.      <VAR
  7587. CLASS="literal"
  7588. >test.wsf</VAR
  7589. > (the extension is very important) and type:
  7590.      <TABLE
  7591. BORDER="0"
  7592. BGCOLOR="#E0E0E0"
  7593. CELLPADDING="5"
  7594. ><TR
  7595. ><TD
  7596. ><PRE
  7597. CLASS="programlisting"
  7598. ><job id="test">
  7599.  
  7600.  <script language="PHPScript">
  7601.   $WScript->Echo("Hello World!");
  7602.  </script>
  7603.  
  7604. </job></PRE
  7605. ></TD
  7606. ></TR
  7607. ></TABLE
  7608. >
  7609.      Save and double-click on the file. If you receive a little window saying
  7610.      "Hello World!" you're done.
  7611.     </P
  7612. ><DIV
  7613. CLASS="note"
  7614. ><BLOCKQUOTE
  7615. CLASS="note"
  7616. ><P
  7617. ><B
  7618. >Note: </B
  7619. >
  7620.       ActiveScript doesn't use the default <TT
  7621. CLASS="filename"
  7622. >php.ini</TT
  7623. > file. Instead, it will
  7624.       look only in the same directory as the .exe that caused it to load. You
  7625.       should create <TT
  7626. CLASS="filename"
  7627. >php-activescript.ini</TT
  7628. > and place it in
  7629.       that folder, if you wish to load extensions, etc.
  7630.      </P
  7631. ></BLOCKQUOTE
  7632. ></DIV
  7633. ></DIV
  7634. ><DIV
  7635. CLASS="sect1"
  7636. ><HR><H2
  7637. CLASS="sect1"
  7638. ><A
  7639. NAME="install.windows.iis"
  7640. >Microsoft IIS / PWS</A
  7641. ></H2
  7642. ><P
  7643. >     This section contains notes and hints specific to IIS (Microsoft
  7644.     Internet Information Server). We have included installation
  7645.     instructions for <A
  7646. HREF="#install.windows.iis.iis3"
  7647. >PWS/IIS 3</A
  7648. >,
  7649.     <A
  7650. HREF="#install.windows.iis.pws4"
  7651. >PWS 4 or newer</A
  7652. > and
  7653.     <A
  7654. HREF="#install.windows.iis.iis4"
  7655. >IIS 4 or newer</A
  7656. > versions.
  7657.    </P
  7658. ><DIV
  7659. CLASS="note"
  7660. ><BLOCKQUOTE
  7661. CLASS="note"
  7662. ><P
  7663. ><B
  7664. >Important for CGI users: </B
  7665. >
  7666.      Read the <A
  7667. HREF="#faq.installation.forceredirect"
  7668. >faq
  7669.      on cgi.force_redirect</A
  7670. > for important details.  This
  7671.      directive needs to be set to <VAR
  7672. CLASS="literal"
  7673. >0</VAR
  7674. >.
  7675.     </P
  7676. ></BLOCKQUOTE
  7677. ></DIV
  7678. ><DIV
  7679. CLASS="warning"
  7680. ><P
  7681. ></P
  7682. ><TABLE
  7683. CLASS="warning"
  7684. BORDER="1"
  7685. WIDTH="100%"
  7686. ><TR
  7687. ><TD
  7688. ALIGN="CENTER"
  7689. ><B
  7690. >Warning</B
  7691. ></TD
  7692. ></TR
  7693. ><TR
  7694. ><TD
  7695. ALIGN="LEFT"
  7696. ><P
  7697. >By using the CGI setup, your server
  7698. is open to several possible attacks. Please read our
  7699. <A
  7700. HREF="#security.cgi-bin"
  7701. >CGI security section</A
  7702. > to learn how to
  7703. defend yourself from those attacks.</P
  7704. ></TD
  7705. ></TR
  7706. ></TABLE
  7707. ></DIV
  7708. ><DIV
  7709. CLASS="sect2"
  7710. ><HR><H3
  7711. CLASS="sect2"
  7712. ><A
  7713. NAME="install.windows.iis.iis3"
  7714. >Windows and PWS/IIS 3</A
  7715. ></H3
  7716. ><P
  7717. >      The recommended method for configuring these servers is to use
  7718.      the REG file included with the distribution
  7719.      (<TT
  7720. CLASS="filename"
  7721. >pws-php4cgi.reg</TT
  7722. > in the SAPI folder for PHP 4, or
  7723.      <TT
  7724. CLASS="filename"
  7725. >pws-php5cgi.reg</TT
  7726. > in the main folder for PHP 5).
  7727.      You may want to edit this file and make sure
  7728.      the extensions and PHP install directories match your
  7729.      configuration. Or you can follow the steps below to do it
  7730.      manually.
  7731.     </P
  7732. ><DIV
  7733. CLASS="warning"
  7734. ><P
  7735. ></P
  7736. ><TABLE
  7737. CLASS="warning"
  7738. BORDER="1"
  7739. WIDTH="100%"
  7740. ><TR
  7741. ><TD
  7742. ALIGN="CENTER"
  7743. ><B
  7744. >Warning</B
  7745. ></TD
  7746. ></TR
  7747. ><TR
  7748. ><TD
  7749. ALIGN="LEFT"
  7750. ><P
  7751. >       These steps involve working directly with the Windows
  7752.       registry. One error here can leave your system in an unstable
  7753.       state. We highly recommend that you back up your registry
  7754.       first. The PHP Development team will not be held responsible if
  7755.       you damage your registry.
  7756.      </P
  7757. ></TD
  7758. ></TR
  7759. ></TABLE
  7760. ></DIV
  7761. ><P
  7762. >      <P
  7763. ></P
  7764. ><UL
  7765. ><LI
  7766. ><P
  7767. >         Run Regedit.
  7768.        </P
  7769. ></LI
  7770. ><LI
  7771. ><P
  7772. >         Navigate to: <VAR
  7773. CLASS="literal"
  7774. >HKEY_LOCAL_MACHINE /System
  7775.          /CurrentControlSet /Services /W3Svc /Parameters
  7776.          /ScriptMap</VAR
  7777. >.
  7778.        </P
  7779. ></LI
  7780. ><LI
  7781. ><P
  7782. >         On the edit menu select: <VAR
  7783. CLASS="literal"
  7784. >New->String Value</VAR
  7785. >.
  7786.        </P
  7787. ></LI
  7788. ><LI
  7789. ><P
  7790. >         Type in the extension you wish to use for your php
  7791.         scripts. For example <VAR
  7792. CLASS="literal"
  7793. >.php</VAR
  7794. >
  7795.        </P
  7796. ></LI
  7797. ><LI
  7798. ><P
  7799. >         Double click on the new string value and enter the path to
  7800.         <TT
  7801. CLASS="filename"
  7802. >php.exe</TT
  7803. > in the value data field. ex:
  7804.         <TT
  7805. CLASS="filename"
  7806. >C:\php\php.exe</TT
  7807. > for PHP 4, or
  7808.         <TT
  7809. CLASS="filename"
  7810. >C:\php\php-cgi.exe</TT
  7811. > for PHP 5.
  7812.        </P
  7813. ></LI
  7814. ><LI
  7815. ><P
  7816. >         Repeat these steps for each extension you wish to associate
  7817.         with PHP scripts.
  7818.        </P
  7819. ></LI
  7820. ></UL
  7821. >
  7822.     </P
  7823. ><P
  7824. >      The following steps do not affect the web server installation
  7825.      and only apply if you want your PHP scripts to be executed when
  7826.      they are run from the command line (ex. run
  7827.      <TT
  7828. CLASS="filename"
  7829. >C:\myscripts\test.php</TT
  7830. >) or by double clicking
  7831.      on them in a directory viewer window. You may wish to skip these
  7832.      steps as you might prefer the PHP files to load into a text
  7833.      editor when you double click on them.
  7834.     </P
  7835. ><P
  7836. >      <P
  7837. ></P
  7838. ><UL
  7839. ><LI
  7840. ><P
  7841. >         Navigate to: <VAR
  7842. CLASS="literal"
  7843. >HKEY_CLASSES_ROOT</VAR
  7844. >
  7845.        </P
  7846. ></LI
  7847. ><LI
  7848. ><P
  7849. >         On the edit menu select: <VAR
  7850. CLASS="literal"
  7851. >New->Key</VAR
  7852. >.
  7853.        </P
  7854. ></LI
  7855. ><LI
  7856. ><P
  7857. >         Name the key to the extension you setup in the previous
  7858.         section. ex: <VAR
  7859. CLASS="literal"
  7860. >.php</VAR
  7861. >
  7862.        </P
  7863. ></LI
  7864. ><LI
  7865. ><P
  7866. >         Highlight the new key and in the right side pane, double click 
  7867.         the "default value" and enter <VAR
  7868. CLASS="literal"
  7869. >phpfile</VAR
  7870. >.
  7871.        </P
  7872. ></LI
  7873. ><LI
  7874. ><P
  7875. >         Repeat the last step for each extension you set up in the
  7876.         previous section.
  7877.        </P
  7878. ></LI
  7879. ><LI
  7880. ><P
  7881. >         Now create another <VAR
  7882. CLASS="literal"
  7883. >New->Key</VAR
  7884. > under
  7885.         <VAR
  7886. CLASS="literal"
  7887. >HKEY_CLASSES_ROOT</VAR
  7888. > and name it
  7889.         <VAR
  7890. CLASS="literal"
  7891. >phpfile</VAR
  7892. >.
  7893.        </P
  7894. ></LI
  7895. ><LI
  7896. ><P
  7897. >         Highlight the new key <VAR
  7898. CLASS="literal"
  7899. >phpfile</VAR
  7900. > and in the
  7901.         right side pane, double click the "default value" and enter
  7902.         <VAR
  7903. CLASS="literal"
  7904. >PHP Script</VAR
  7905. >.
  7906.        </P
  7907. ></LI
  7908. ><LI
  7909. ><P
  7910. >         Right click on the <VAR
  7911. CLASS="literal"
  7912. >phpfile</VAR
  7913. > key and select
  7914.         <VAR
  7915. CLASS="literal"
  7916. >New->Key</VAR
  7917. >, name it <VAR
  7918. CLASS="literal"
  7919. >Shell</VAR
  7920. >.
  7921.        </P
  7922. ></LI
  7923. ><LI
  7924. ><P
  7925. >         Right click on the <VAR
  7926. CLASS="literal"
  7927. >Shell</VAR
  7928. > key and select
  7929.         <VAR
  7930. CLASS="literal"
  7931. >New->Key</VAR
  7932. >, name it <VAR
  7933. CLASS="literal"
  7934. >open</VAR
  7935. >.
  7936.        </P
  7937. ></LI
  7938. ><LI
  7939. ><P
  7940. >         Right click on the <VAR
  7941. CLASS="literal"
  7942. >open</VAR
  7943. > key and select
  7944.         <VAR
  7945. CLASS="literal"
  7946. >New->Key</VAR
  7947. >, name it
  7948.         <VAR
  7949. CLASS="literal"
  7950. >command</VAR
  7951. >. 
  7952.        </P
  7953. ></LI
  7954. ><LI
  7955. ><P
  7956. >         Highlight the new key <VAR
  7957. CLASS="literal"
  7958. >command</VAR
  7959. > and in the
  7960.         right side pane, double click the "default value" and enter
  7961.         the path to <TT
  7962. CLASS="filename"
  7963. >php.exe</TT
  7964. >. ex:
  7965.         <VAR
  7966. CLASS="literal"
  7967. >c:\php\php.exe -q %1</VAR
  7968. >. (don't forget the
  7969.         <VAR
  7970. CLASS="literal"
  7971. >%1</VAR
  7972. >). 
  7973.        </P
  7974. ></LI
  7975. ><LI
  7976. ><P
  7977. >         Exit Regedit.
  7978.        </P
  7979. ></LI
  7980. ><LI
  7981. ><P
  7982. >         If using PWS on Windows, reboot to reload the registry.
  7983.        </P
  7984. ></LI
  7985. ></UL
  7986. >
  7987.     </P
  7988. ><P
  7989. >      PWS and IIS 3 users now have a fully operational system. IIS 3
  7990.      users can use a nifty <A
  7991. HREF="http://www.genusa.com/iis/iiscfg.html"
  7992. TARGET="_top"
  7993. >tool</A
  7994. >
  7995.      from Steven Genusa to configure their script maps.
  7996.     </P
  7997. ></DIV
  7998. ><DIV
  7999. CLASS="sect2"
  8000. ><HR><H3
  8001. CLASS="sect2"
  8002. ><A
  8003. NAME="install.windows.iis.pws4"
  8004. >Windows and PWS 4 or newer</A
  8005. ></H3
  8006. ><P
  8007. >      When installing PHP on Windows with PWS 4 or newer version,
  8008.      you have two options. One to set up the PHP CGI binary,
  8009.      the other is to use the ISAPI module DLL.
  8010.     </P
  8011. ><P
  8012. >      If you choose the CGI binary, do the following:
  8013.      <P
  8014. ></P
  8015. ><UL
  8016. ><LI
  8017. ><P
  8018. >         Edit the enclosed <TT
  8019. CLASS="filename"
  8020. >pws-php4cgi.reg</TT
  8021. > /
  8022.         <TT
  8023. CLASS="filename"
  8024. >pws-php5cgi.reg</TT
  8025. > file (look into the SAPI folder
  8026.         for PHP 4, or in the main folder for PHP 5) to reflect the location of
  8027.         your <TT
  8028. CLASS="filename"
  8029. >php.exe</TT
  8030. > / <TT
  8031. CLASS="filename"
  8032. >php-cgi.exe</TT
  8033. >.
  8034.         Backslashes should be escaped, for example:
  8035.         <VAR
  8036. CLASS="literal"
  8037. >[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w3svc\parameters\Script
  8038.          Map] ".php"="C:\\php\\php.exe"</VAR
  8039. > (change to
  8040.         <VAR
  8041. CLASS="literal"
  8042. >C:\\php\\php-cgi.exe</VAR
  8043. > if you are using PHP 5)
  8044.         Now merge this registery file into your system; you may do 
  8045.         this by double-clicking it.
  8046.        </P
  8047. ></LI
  8048. ><LI
  8049. ><P
  8050. >         In the PWS Manager, right click on a given directory you want
  8051.         to add PHP support to, and select Properties. Check the 'Execute'
  8052.         checkbox, and confirm.
  8053.        </P
  8054. ></LI
  8055. ></UL
  8056. >
  8057.     </P
  8058. ><P
  8059. >      If you choose the ISAPI module, do the following:
  8060.      <P
  8061. ></P
  8062. ><UL
  8063. ><LI
  8064. ><P
  8065. >         Edit the enclosed <TT
  8066. CLASS="filename"
  8067. >pws-php4isapi.reg</TT
  8068. > /
  8069.         <TT
  8070. CLASS="filename"
  8071. >pws-php5isapi.reg</TT
  8072. > file (look into the SAPI folder
  8073.         for PHP 4, or in the main folder for PHP 5) to reflect the location of
  8074.         your <TT
  8075. CLASS="filename"
  8076. >php4isapi.dll</TT
  8077. > /
  8078.         <TT
  8079. CLASS="filename"
  8080. >php5isapi.dll</TT
  8081. >. Backslashes should be escaped, for example:
  8082.         <VAR
  8083. CLASS="literal"
  8084. >[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w3svc\parameters\Script
  8085.          Map] ".php"="C:\\php\\sapi\\php4isapi.dll"</VAR
  8086. > (or
  8087.         <VAR
  8088. CLASS="literal"
  8089. >C:\\php\\php5isapi.dll</VAR
  8090. > for PHP 5)
  8091.         Now merge this registery file into your system; you may do 
  8092.         this by double-clicking it.
  8093.        </P
  8094. ></LI
  8095. ><LI
  8096. ><P
  8097. >         In the PWS Manager, right click on a given directory you want to
  8098.         add PHP support to, and select Properties. Check the 'Execute'
  8099.         checkbox, and confirm. 
  8100.        </P
  8101. ></LI
  8102. ></UL
  8103. >
  8104.     </P
  8105. ></DIV
  8106. ><DIV
  8107. CLASS="sect2"
  8108. ><HR><H3
  8109. CLASS="sect2"
  8110. ><A
  8111. NAME="install.windows.iis.iis4"
  8112. >Windows NT/2000/XP and IIS 4 or newer</A
  8113. ></H3
  8114. ><P
  8115. >      To install PHP on an NT/2000/XP Server running IIS 4 or newer,
  8116.      follow these instructions. You have two options to set up
  8117.      PHP, using the CGI binary (<TT
  8118. CLASS="filename"
  8119. >php.exe</TT
  8120. > in PHP 4, or
  8121.      <TT
  8122. CLASS="filename"
  8123. >php-cgi.exe</TT
  8124. > in PHP 5) or with the ISAPI module.
  8125.     </P
  8126. ><P
  8127. >      In either case, you need to start the Microsoft Management
  8128.      Console (may appear as 'Internet Services Manager', either
  8129.      in your Windows NT 4.0 Option Pack branch or the Control
  8130.      Panel=>Administrative Tools under Windows 2000/XP). Then 
  8131.      right click on your Web server node (this will most probably
  8132.      appear as 'Default Web Server'), and select 'Properties'.
  8133.     </P
  8134. ><P
  8135. >      If you want to use the CGI binary, do the following:
  8136.      <P
  8137. ></P
  8138. ><UL
  8139. ><LI
  8140. ><P
  8141. >         Under 'Home Directory', 'Virtual Directory', or
  8142.         'Directory', click on the 'Configuration' button,
  8143.         and then enter the App Mappings tab.
  8144.        </P
  8145. ></LI
  8146. ><LI
  8147. ><P
  8148. >         Click Add, and in the Executable box, type:
  8149.         <VAR
  8150. CLASS="literal"
  8151. >C:\php\php.exe</VAR
  8152. > for PHP 4 or
  8153.         <VAR
  8154. CLASS="literal"
  8155. >C:\php\php-cgi.exe</VAR
  8156. > for PHP 5 (assuming
  8157.         that you have unziped PHP in <TT
  8158. CLASS="filename"
  8159. >c:\php\</TT
  8160. >).
  8161.         </P
  8162. ></LI
  8163. ><LI
  8164. ><P
  8165. >         In the Extension box, type the file name extension you want
  8166.         associated with PHP scripts. Leave 'Method exclusions'
  8167.         blank, and check the 'Script engine' checkbox. You may also
  8168.         like to check the 'check that file exists' box - for a small
  8169.         performance penalty, IIS (or PWS) will check that the script
  8170.         file exists and sort out authentication before firing up PHP.
  8171.         This means that you will get sensible 404 style error messages
  8172.         instead of CGI errors complaining that PHP did not output any data.
  8173.        </P
  8174. ><P
  8175. >         You must start over from the previous step for each
  8176.         extension you want associated with PHP scripts.
  8177.         <VAR
  8178. CLASS="literal"
  8179. >.php</VAR
  8180. > and <VAR
  8181. CLASS="literal"
  8182. >.phtml</VAR
  8183. >
  8184.         are common, although <VAR
  8185. CLASS="literal"
  8186. >.php3</VAR
  8187. > may be
  8188.         required for legacy applications.
  8189.        </P
  8190. ></LI
  8191. ><LI
  8192. ><P
  8193. >         Set up the appropriate security. (This is done in Internet
  8194.         Service Manager), and if your NT Server uses NTFS file system,
  8195.         add execute rights for I_USR_ to the directory that contains
  8196.         <TT
  8197. CLASS="filename"
  8198. >php.exe</TT
  8199. > / <TT
  8200. CLASS="filename"
  8201. >php-cgi.exe</TT
  8202. >.
  8203.        </P
  8204. ></LI
  8205. ></UL
  8206. >
  8207.     </P
  8208. ><P
  8209. >      To use the ISAPI module, do the following:
  8210.      <P
  8211. ></P
  8212. ><UL
  8213. ><LI
  8214. ><P
  8215. >         If you don't want to perform HTTP Authentication using PHP,
  8216.         you can (and should) skip this step. Under ISAPI Filters,
  8217.         add a new ISAPI filter. Use PHP as the filter name, and
  8218.         supply a path to the <TT
  8219. CLASS="filename"
  8220. >php4isapi.dll</TT
  8221. > /
  8222.         <TT
  8223. CLASS="filename"
  8224. >php5isapi.dll</TT
  8225. >.
  8226.        </P
  8227. ></LI
  8228. ><LI
  8229. ><P
  8230. >         Under 'Home Directory', click on the 'Configuration' button.
  8231.         Add a new entry to the Application Mappings. Use the path
  8232.         to the <TT
  8233. CLASS="filename"
  8234. >php4isapi.dll</TT
  8235. > /
  8236.         <TT
  8237. CLASS="filename"
  8238. >php5isapi.dll</TT
  8239. > as the Executable, supply
  8240.         <VAR
  8241. CLASS="literal"
  8242. >.php</VAR
  8243. > as the extension, leave 'Method exclusions'
  8244.         blank, and check the 'Script engine' checkbox.
  8245.        </P
  8246. ></LI
  8247. ><LI
  8248. ><P
  8249. >         Stop IIS completely (NET STOP iisadmin)
  8250.        </P
  8251. ></LI
  8252. ><LI
  8253. ><P
  8254. >         Start IIS again (NET START w3svc)
  8255.        </P
  8256. ></LI
  8257. ></UL
  8258. >
  8259.     </P
  8260. ><P
  8261. >      If you experience 100% CPU usage after some time, turn off the IIS
  8262.      setting <VAR
  8263. CLASS="literal"
  8264. >Cache ISAPI Application</VAR
  8265. >.
  8266.     </P
  8267. ></DIV
  8268. ></DIV
  8269. ><DIV
  8270. CLASS="sect1"
  8271. ><HR><H2
  8272. CLASS="sect1"
  8273. ><A
  8274. NAME="install.windows.apache1"
  8275. >Apache 1.3.x on Microsoft Windows</A
  8276. ></H2
  8277. ><P
  8278. >      This section contains notes and hints specific to Apache 1.3.x installs
  8279.      of PHP on Microsoft Windows systems. There are also
  8280.      <A
  8281. HREF="#install.windows.apache2"
  8282. >instructions and notes
  8283.      for Apache 2</A
  8284. > on a separate page.
  8285.     </P
  8286. ><DIV
  8287. CLASS="note"
  8288. ><BLOCKQUOTE
  8289. CLASS="note"
  8290. ><P
  8291. ><B
  8292. >Note: </B
  8293. >
  8294.       Please read the <A
  8295. HREF="#install.windows.manual"
  8296. >manual
  8297.       installation steps</A
  8298. > first!
  8299.      </P
  8300. ></BLOCKQUOTE
  8301. ></DIV
  8302. ><P
  8303. >      There are two ways to set up PHP to work with Apache 1.3.x
  8304.      on Windows. One is to use the CGI binary (<TT
  8305. CLASS="filename"
  8306. >php.exe</TT
  8307. >
  8308.      for PHP 4 and <TT
  8309. CLASS="filename"
  8310. >php-cgi.exe</TT
  8311. > for PHP 5),
  8312.      the other is to use the Apache Module DLL. In either case
  8313.      you need to edit your <TT
  8314. CLASS="filename"
  8315. >httpd.conf</TT
  8316. > to configure Apache to
  8317.      work with PHP, and then restart the server.
  8318.     </P
  8319. ><P
  8320. >      It is worth noting here that now the SAPI module has been 
  8321.      made more stable under Windows, we recommend it's use above
  8322.      the CGI binary, since it is more transparent and secure.
  8323.     </P
  8324. ><P
  8325. >      Although there can be a few variations of configuring PHP
  8326.      under Apache, these are simple enough to be used by the
  8327.      newcomer. Please consult the Apache Documentation for further
  8328.      configuration directives.
  8329.     </P
  8330. ><P
  8331. >      After changing the configuration file, remember to restart the server, for
  8332.      example, <B
  8333. CLASS="command"
  8334. >NET STOP APACHE</B
  8335. > followed by
  8336.      <B
  8337. CLASS="command"
  8338. >NET START APACHE</B
  8339. >, if you run Apache as a Windows
  8340.      Service, or use your regular shortcuts.
  8341.     </P
  8342. ><DIV
  8343. CLASS="note"
  8344. ><BLOCKQUOTE
  8345. CLASS="note"
  8346. ><P
  8347. ><B
  8348. >Note: </B
  8349. >Remember that when adding
  8350. path values in the Apache configuration files on Windows, all backslashes
  8351. such as <TT
  8352. CLASS="filename"
  8353. >c:\directory\file.ext</TT
  8354. > must be converted to
  8355. forward slashes, as <TT
  8356. CLASS="filename"
  8357. >c:/directory/file.ext</TT
  8358. >.</P
  8359. ></BLOCKQUOTE
  8360. ></DIV
  8361. ><DIV
  8362. CLASS="sect2"
  8363. ><HR><H3
  8364. CLASS="sect2"
  8365. ><A
  8366. NAME="install.windows.apache1.module"
  8367. >Installing as an Apache module</A
  8368. ></H3
  8369. ><P
  8370. >       You should add the following lines to your Apache <TT
  8371. CLASS="filename"
  8372. >httpd.conf</TT
  8373. > file:
  8374.      </P
  8375. ><P
  8376. >       <TABLE
  8377. WIDTH="100%"
  8378. BORDER="0"
  8379. CELLPADDING="0"
  8380. CELLSPACING="0"
  8381. CLASS="EXAMPLE"
  8382. ><TR
  8383. ><TD
  8384. ><DIV
  8385. CLASS="example"
  8386. ><A
  8387. NAME="AEN1374"
  8388. ></A
  8389. ><P
  8390. ><B
  8391. >Example 6-3. PHP as an Apache 1.3.x module</B
  8392. ></P
  8393. ><P
  8394. >          This assumes PHP is installed to <TT
  8395. CLASS="filename"
  8396. >c:\php</TT
  8397. >. Adjust the
  8398.          path if this is not the case.
  8399.         </P
  8400. ><P
  8401. >          For PHP 4:
  8402.         </P
  8403. ><TABLE
  8404. BORDER="0"
  8405. BGCOLOR="#E0E0E0"
  8406. CELLPADDING="5"
  8407. ><TR
  8408. ><TD
  8409. ><PRE
  8410. CLASS="apache-conf"
  8411. ># Add to the end of the LoadModule section
  8412. LoadModule php4_module "c:/php/php4apache.dll"
  8413.  
  8414. # Add to the end of the AddModule section
  8415. AddModule mod_php4.c</PRE
  8416. ></TD
  8417. ></TR
  8418. ></TABLE
  8419. ><P
  8420. >         For PHP 5:
  8421.        </P
  8422. ><TABLE
  8423. BORDER="0"
  8424. BGCOLOR="#E0E0E0"
  8425. CELLPADDING="5"
  8426. ><TR
  8427. ><TD
  8428. ><PRE
  8429. CLASS="apache-conf"
  8430. ># Add to the end of the LoadModule section
  8431. LoadModule php5_module "c:/php/php5apache.dll"
  8432.  
  8433. # Add to the end of the AddModule section
  8434. AddModule mod_php5.c</PRE
  8435. ></TD
  8436. ></TR
  8437. ></TABLE
  8438. ><P
  8439. >         For both:
  8440.        </P
  8441. ><TABLE
  8442. BORDER="0"
  8443. BGCOLOR="#E0E0E0"
  8444. CELLPADDING="5"
  8445. ><TR
  8446. ><TD
  8447. ><PRE
  8448. CLASS="apache-conf"
  8449. ># Add this line inside the <IfModule mod_mime.c> conditional brace
  8450. AddType application/x-httpd-php .php
  8451.  
  8452. # For syntax highlighted .phps files, also add
  8453. AddType application/x-httpd-php-source .phps</PRE
  8454. ></TD
  8455. ></TR
  8456. ></TABLE
  8457. ></DIV
  8458. ></TD
  8459. ></TR
  8460. ></TABLE
  8461. >
  8462.      </P
  8463. ></DIV
  8464. ><DIV
  8465. CLASS="sect2"
  8466. ><HR><H3
  8467. CLASS="sect2"
  8468. ><A
  8469. NAME="install.windows.apache1.cgi"
  8470. >Installing as a CGI binary</A
  8471. ></H3
  8472. ><P
  8473. >       If you unzipped the PHP package to <TT
  8474. CLASS="filename"
  8475. >C:\php\</TT
  8476. > as described
  8477.       in the <A
  8478. HREF="#install.windows.manual"
  8479. >Manual
  8480.       Installation Steps</A
  8481. > section, you need to insert
  8482.       these lines to your Apache configuration file to set
  8483.       up the CGI binary:
  8484.       <TABLE
  8485. WIDTH="100%"
  8486. BORDER="0"
  8487. CELLPADDING="0"
  8488. CELLSPACING="0"
  8489. CLASS="EXAMPLE"
  8490. ><TR
  8491. ><TD
  8492. ><DIV
  8493. CLASS="example"
  8494. ><A
  8495. NAME="AEN1389"
  8496. ></A
  8497. ><P
  8498. ><B
  8499. >Example 6-4. PHP and Apache 1.3.x as CGI</B
  8500. ></P
  8501. ><TABLE
  8502. BORDER="0"
  8503. BGCOLOR="#E0E0E0"
  8504. CELLPADDING="5"
  8505. ><TR
  8506. ><TD
  8507. ><PRE
  8508. CLASS="apache-conf"
  8509. >ScriptAlias /php/ "c:/php/"
  8510. AddType application/x-httpd-php .php
  8511.  
  8512. # For PHP 4
  8513. Action application/x-httpd-php "/php/php.exe"
  8514.  
  8515. # For PHP 5
  8516. Action application/x-httpd-php "/php/php-cgi.exe"
  8517.  
  8518. # specify the directory where php.ini is
  8519. SetEnv PHPRC C:/php</PRE
  8520. ></TD
  8521. ></TR
  8522. ></TABLE
  8523. ></DIV
  8524. ></TD
  8525. ></TR
  8526. ></TABLE
  8527. >
  8528.       Note that the second line in the list above can be found
  8529.       in the actual versions of <TT
  8530. CLASS="filename"
  8531. >httpd.conf</TT
  8532. >, but it is commented out. Remember
  8533.       also to substitute the <TT
  8534. CLASS="filename"
  8535. >c:/php/</TT
  8536. > for your actual path to
  8537.       PHP.
  8538.      </P
  8539. ><DIV
  8540. CLASS="warning"
  8541. ><P
  8542. ></P
  8543. ><TABLE
  8544. CLASS="warning"
  8545. BORDER="1"
  8546. WIDTH="100%"
  8547. ><TR
  8548. ><TD
  8549. ALIGN="CENTER"
  8550. ><B
  8551. >Warning</B
  8552. ></TD
  8553. ></TR
  8554. ><TR
  8555. ><TD
  8556. ALIGN="LEFT"
  8557. ><P
  8558. >By using the CGI setup, your server
  8559. is open to several possible attacks. Please read our
  8560. <A
  8561. HREF="#security.cgi-bin"
  8562. >CGI security section</A
  8563. > to learn how to
  8564. defend yourself from those attacks.</P
  8565. ></TD
  8566. ></TR
  8567. ></TABLE
  8568. ></DIV
  8569. ><P
  8570. >       If you would like to present PHP source files syntax highlighted, there
  8571.       is no such convenient option as with the module version of PHP.
  8572.       If you chose to configure Apache to use PHP as a CGI binary, you
  8573.       will need to use the <A
  8574. HREF="#function.highlight-file"
  8575. ><B
  8576. CLASS="function"
  8577. >highlight_file()</B
  8578. ></A
  8579. > function. To
  8580.       do this simply create a PHP script file and add this code: 
  8581.       <VAR
  8582. CLASS="literal"
  8583. ><?php highlight_file('some_php_script.php'); ?></VAR
  8584. >.
  8585.      </P
  8586. ></DIV
  8587. ></DIV
  8588. ><DIV
  8589. CLASS="sect1"
  8590. ><HR><H2
  8591. CLASS="sect1"
  8592. ><A
  8593. NAME="install.windows.apache2"
  8594. >Apache 2.0.x on Microsoft Windows</A
  8595. ></H2
  8596. ><P
  8597. >      This section contains notes and hints specific to Apache 2.0.x installs
  8598.      of PHP on Microsoft Windows systems. We also
  8599.      have <A
  8600. HREF="#install.windows.apache1"
  8601. >instructions and notes
  8602.      for Apache 1.3.x users on a separate page</A
  8603. >.
  8604.     </P
  8605. ><DIV
  8606. CLASS="note"
  8607. ><BLOCKQUOTE
  8608. CLASS="note"
  8609. ><P
  8610. ><B
  8611. >Note: </B
  8612. >
  8613.       You should read the <A
  8614. HREF="#install.windows.manual"
  8615. >manual
  8616.        installation steps</A
  8617. > first!
  8618.      </P
  8619. ></BLOCKQUOTE
  8620. ></DIV
  8621. ><DIV
  8622. CLASS="warning"
  8623. ><P
  8624. ></P
  8625. ><TABLE
  8626. CLASS="warning"
  8627. BORDER="1"
  8628. WIDTH="100%"
  8629. ><TR
  8630. ><TD
  8631. ALIGN="CENTER"
  8632. ><B
  8633. >Warning</B
  8634. ></TD
  8635. ></TR
  8636. ><TR
  8637. ><TD
  8638. ALIGN="LEFT"
  8639. ><P
  8640. >We do not recommend using a threaded MPM
  8641. in production with Apache2.  Use the prefork MPM instead, or use Apache1.  For information
  8642. on why, read the following 
  8643. <A
  8644. HREF="#faq.installation.apache2"
  8645. >FAQ entry</A
  8646. ></P
  8647. ></TD
  8648. ></TR
  8649. ></TABLE
  8650. ></DIV
  8651. ><P
  8652. >      You are highly encouraged to take a look at the
  8653.      <A
  8654. HREF="http://httpd.apache.org/docs-2.0/"
  8655. TARGET="_top"
  8656. >Apache Documentation</A
  8657. > to get
  8658.      a basic understanding of the Apache 2.0.x Server. Also consider to
  8659.      read the <A
  8660. HREF="http://httpd.apache.org/docs-2.0/platform/windows.html"
  8661. TARGET="_top"
  8662. >Windows specific
  8663.      notes</A
  8664. > for Apache 2.0.x before reading on here.
  8665.     </P
  8666. ><DIV
  8667. CLASS="note"
  8668. ><BLOCKQUOTE
  8669. CLASS="note"
  8670. ><P
  8671. ><B
  8672. >PHP and Apache 2.0.x compatibility notes: </B
  8673. >
  8674.       The following versions of PHP are known to work with the most recent
  8675.       version of Apache 2.0.x:
  8676.       <P
  8677. ></P
  8678. ><UL
  8679. COMPACT="COMPACT"
  8680. ><LI
  8681. ><SPAN
  8682. >          PHP 4.3.0 or later available at
  8683.          <A
  8684. HREF="http://www.php.net/downloads.php"
  8685. TARGET="_top"
  8686. >http://www.php.net/downloads.php</A
  8687. >.
  8688.         </SPAN
  8689. ></LI
  8690. ><LI
  8691. ><SPAN
  8692. >          the latest stable development version.
  8693.          Get the source code <A
  8694. HREF="http://snaps.php.net/php4-latest.tar.gz"
  8695. TARGET="_top"
  8696. >          http://snaps.php.net/php4-latest.tar.gz</A
  8697. > or download binaries
  8698.          for Windows <A
  8699. HREF="http://snaps.php.net/win32/php4-win32-latest.zip"
  8700. TARGET="_top"
  8701. >          http://snaps.php.net/win32/php4-win32-latest.zip</A
  8702. >.
  8703.         </SPAN
  8704. ></LI
  8705. ><LI
  8706. ><SPAN
  8707. >          a prerelease version downloadable from
  8708.          <A
  8709. HREF="http://qa.php.net/"
  8710. TARGET="_top"
  8711. >http://qa.php.net/</A
  8712. >.
  8713.         </SPAN
  8714. ></LI
  8715. ><LI
  8716. ><SPAN
  8717. >          you have always the option to obtain PHP through
  8718.          <A
  8719. HREF="http://www.php.net/anoncvs.php"
  8720. TARGET="_top"
  8721. >anonymous CVS</A
  8722. >.
  8723.         </SPAN
  8724. ></LI
  8725. ></UL
  8726. >
  8727.       These versions of PHP are compatible to Apache 2.0.40 and later.
  8728.      </P
  8729. ><P
  8730. >       Apache 2.0 <VAR
  8731. CLASS="literal"
  8732. >SAPI</VAR
  8733. >-support started with PHP 4.2.0.
  8734.       PHP 4.2.3 works with Apache 2.0.39, don't use any other version of Apache with
  8735.       PHP 4.2.3. However, the recommended setup is to use PHP 4.3.0 or later with
  8736.       the most recent version of Apache2.
  8737.      </P
  8738. ><P
  8739. >       All mentioned versions of PHP will work still with
  8740.       Apache 1.3.x.
  8741.      </P
  8742. ></BLOCKQUOTE
  8743. ></DIV
  8744. ><DIV
  8745. CLASS="warning"
  8746. ><P
  8747. ></P
  8748. ><TABLE
  8749. CLASS="warning"
  8750. BORDER="1"
  8751. WIDTH="100%"
  8752. ><TR
  8753. ><TD
  8754. ALIGN="CENTER"
  8755. ><B
  8756. >Warning</B
  8757. ></TD
  8758. ></TR
  8759. ><TR
  8760. ><TD
  8761. ALIGN="LEFT"
  8762. ><P
  8763. >       Apache 2.0.x is designed to run on Windows NT 4.0, Windows 2000 or
  8764.       Windows XP. At this time, support for Windows 9x is incomplete.
  8765.       Apache 2.0.x is not expected to work on those platforms at this time.
  8766.      </P
  8767. ></TD
  8768. ></TR
  8769. ></TABLE
  8770. ></DIV
  8771. ><P
  8772. >      Download the most recent version of <A
  8773. HREF="http://www.apache.org/"
  8774. TARGET="_top"
  8775. >      Apache 2.0.x</A
  8776. > and a fitting PHP version.
  8777.      Follow the <A
  8778. HREF="#install.windows.manual"
  8779. >Manual Installation
  8780.      Steps</A
  8781. > and come back to go on with the integration of PHP and Apache.
  8782.     </P
  8783. ><P
  8784. >      There are two ways to set up PHP to work with Apache 2.0.x on Windows.
  8785.      One is to use the CGI binary the other is to use the Apache module DLL.
  8786.      In either case you need to edit your <TT
  8787. CLASS="filename"
  8788. >httpd.conf</TT
  8789. > to configure Apache
  8790.      to work with PHP and then restart the server.
  8791.     </P
  8792. ><DIV
  8793. CLASS="note"
  8794. ><BLOCKQUOTE
  8795. CLASS="note"
  8796. ><P
  8797. ><B
  8798. >Note: </B
  8799. >Remember that when adding
  8800. path values in the Apache configuration files on Windows, all backslashes
  8801. such as <TT
  8802. CLASS="filename"
  8803. >c:\directory\file.ext</TT
  8804. > must be converted to
  8805. forward slashes, as <TT
  8806. CLASS="filename"
  8807. >c:/directory/file.ext</TT
  8808. >.</P
  8809. ></BLOCKQUOTE
  8810. ></DIV
  8811. ><DIV
  8812. CLASS="sect2"
  8813. ><HR><H3
  8814. CLASS="sect2"
  8815. ><A
  8816. NAME="install.windows.apache2.cgi"
  8817. >Installing as a CGI binary</A
  8818. ></H3
  8819. ><P
  8820. >       You need to insert these three lines to your Apache <TT
  8821. CLASS="filename"
  8822. >httpd.conf</TT
  8823. >
  8824.       configuration file to set up the CGI binary:
  8825.       <TABLE
  8826. WIDTH="100%"
  8827. BORDER="0"
  8828. CELLPADDING="0"
  8829. CELLSPACING="0"
  8830. CLASS="EXAMPLE"
  8831. ><TR
  8832. ><TD
  8833. ><DIV
  8834. CLASS="example"
  8835. ><A
  8836. NAME="AEN1448"
  8837. ></A
  8838. ><P
  8839. ><B
  8840. >Example 6-5. PHP and Apache 2.0 as CGI</B
  8841. ></P
  8842. ><TABLE
  8843. BORDER="0"
  8844. BGCOLOR="#E0E0E0"
  8845. CELLPADDING="5"
  8846. ><TR
  8847. ><TD
  8848. ><PRE
  8849. CLASS="apache-conf"
  8850. >ScriptAlias /php/ "c:/php/"
  8851. AddType application/x-httpd-php .php
  8852.  
  8853. # For PHP 4
  8854. Action application/x-httpd-php "/php/php.exe"
  8855.  
  8856. # For PHP 5
  8857. Action application/x-httpd-php "/php/php-cgi.exe"</PRE
  8858. ></TD
  8859. ></TR
  8860. ></TABLE
  8861. ></DIV
  8862. ></TD
  8863. ></TR
  8864. ></TABLE
  8865. >
  8866.      </P
  8867. ><DIV
  8868. CLASS="warning"
  8869. ><P
  8870. ></P
  8871. ><TABLE
  8872. CLASS="warning"
  8873. BORDER="1"
  8874. WIDTH="100%"
  8875. ><TR
  8876. ><TD
  8877. ALIGN="CENTER"
  8878. ><B
  8879. >Warning</B
  8880. ></TD
  8881. ></TR
  8882. ><TR
  8883. ><TD
  8884. ALIGN="LEFT"
  8885. ><P
  8886. >By using the CGI setup, your server
  8887. is open to several possible attacks. Please read our
  8888. <A
  8889. HREF="#security.cgi-bin"
  8890. >CGI security section</A
  8891. > to learn how to
  8892. defend yourself from those attacks.</P
  8893. ></TD
  8894. ></TR
  8895. ></TABLE
  8896. ></DIV
  8897. ></DIV
  8898. ><DIV
  8899. CLASS="sect2"
  8900. ><HR><H3
  8901. CLASS="sect2"
  8902. ><A
  8903. NAME="install.windows.apache2.module"
  8904. >Installing as an Apache module</A
  8905. ></H3
  8906. ><P
  8907. >       You need to insert these two lines to your
  8908.       Apache <TT
  8909. CLASS="filename"
  8910. >httpd.conf</TT
  8911. > configuration file to set up the
  8912.       PHP module for Apache 2.0:
  8913.       <TABLE
  8914. WIDTH="100%"
  8915. BORDER="0"
  8916. CELLPADDING="0"
  8917. CELLSPACING="0"
  8918. CLASS="EXAMPLE"
  8919. ><TR
  8920. ><TD
  8921. ><DIV
  8922. CLASS="example"
  8923. ><A
  8924. NAME="AEN1458"
  8925. ></A
  8926. ><P
  8927. ><B
  8928. >Example 6-6. PHP and Apache 2.0 as Module</B
  8929. ></P
  8930. ><TABLE
  8931. BORDER="0"
  8932. BGCOLOR="#E0E0E0"
  8933. CELLPADDING="5"
  8934. ><TR
  8935. ><TD
  8936. ><PRE
  8937. CLASS="apache-conf"
  8938. ># For PHP 4 do something like this:
  8939. LoadModule php4_module "c:/php/php4apache2.dll"
  8940. AddType application/x-httpd-php .php
  8941.  
  8942. # For PHP 5 do something like this:
  8943. LoadModule php5_module "c:/php/php5apache2.dll"
  8944. AddType application/x-httpd-php .php
  8945.  
  8946. # configure the path to php.ini
  8947. PHPIniDir "C:/php"</PRE
  8948. ></TD
  8949. ></TR
  8950. ></TABLE
  8951. ></DIV
  8952. ></TD
  8953. ></TR
  8954. ></TABLE
  8955. >
  8956.      </P
  8957. ><DIV
  8958. CLASS="note"
  8959. ><BLOCKQUOTE
  8960. CLASS="note"
  8961. ><P
  8962. ><B
  8963. >Note: </B
  8964. >
  8965.        Remember to substitute the <TT
  8966. CLASS="filename"
  8967. >c:/php/</TT
  8968. > for your actual
  8969.        path to PHP in the above examples. Take care to use
  8970.        either <TT
  8971. CLASS="filename"
  8972. >php4apache2.dll</TT
  8973. > or 
  8974.        <TT
  8975. CLASS="filename"
  8976. >php5apache2.dll</TT
  8977. > in your LoadModule directive and
  8978.        <SPAN
  8979. CLASS="emphasis"
  8980. ><I
  8981. CLASS="emphasis"
  8982. >not</I
  8983. ></SPAN
  8984. > <TT
  8985. CLASS="filename"
  8986. >php4apache.dll</TT
  8987. > or 
  8988.        <TT
  8989. CLASS="filename"
  8990. >php5apache.dll</TT
  8991. > as the latter ones are designed to
  8992.        run with <A
  8993. HREF="#install.windows.apache1"
  8994. >Apache 1.3.x</A
  8995. >.
  8996.       </P
  8997. ></BLOCKQUOTE
  8998. ></DIV
  8999. ><DIV
  9000. CLASS="note"
  9001. ><BLOCKQUOTE
  9002. CLASS="note"
  9003. ><P
  9004. ><B
  9005. >Note: </B
  9006. >
  9007.        If you want to use content negotiation, read
  9008.        <A
  9009. HREF="#faq.installation.apache.multiviews"
  9010. >related FAQ</A
  9011. >.
  9012.       </P
  9013. ></BLOCKQUOTE
  9014. ></DIV
  9015. ><DIV
  9016. CLASS="warning"
  9017. ><P
  9018. ></P
  9019. ><TABLE
  9020. CLASS="warning"
  9021. BORDER="1"
  9022. WIDTH="100%"
  9023. ><TR
  9024. ><TD
  9025. ALIGN="CENTER"
  9026. ><B
  9027. >Warning</B
  9028. ></TD
  9029. ></TR
  9030. ><TR
  9031. ><TD
  9032. ALIGN="LEFT"
  9033. ><P
  9034. >        Don't mix up your installation with DLL files from
  9035.        <SPAN
  9036. CLASS="emphasis"
  9037. ><I
  9038. CLASS="emphasis"
  9039. >different PHP versions</I
  9040. ></SPAN
  9041. >. You have the only choice
  9042.        to use the DLL's and extensions that ship with your downloaded PHP version.
  9043.       </P
  9044. ></TD
  9045. ></TR
  9046. ></TABLE
  9047. ></DIV
  9048. ></DIV
  9049. ></DIV
  9050. ><DIV
  9051. CLASS="sect1"
  9052. ><HR><H2
  9053. CLASS="sect1"
  9054. ><A
  9055. NAME="install.windows.sun"
  9056. >Sun, iPlanet and Netscape servers on Microsoft Windows</A
  9057. ></H2
  9058. ><P
  9059. >     This section contains notes and hints specific to Sun Java System Web Server,
  9060.     Sun ONE Web Server, iPlanet and Netscape server installs of PHP on Windows.
  9061.    </P
  9062. ><P
  9063. >     From PHP 4.3.3 on you can use PHP scripts with the
  9064.     <A
  9065. HREF="#ref.nsapi"
  9066. >NSAPI module</A
  9067. > to 
  9068.     <A
  9069. HREF="#install.windows.sun.specialpages"
  9070. >generate custom
  9071.     directory listings and error pages</A
  9072. >. Additional functions for
  9073.     Apache compatibility are also available. For support in current webservers
  9074.     read the <A
  9075. HREF="#install.windows.sun.notes"
  9076. >note about
  9077.     subrequests</A
  9078. >.
  9079.    </P
  9080. ><DIV
  9081. CLASS="sect2"
  9082. ><HR><H3
  9083. CLASS="sect2"
  9084. ><A
  9085. NAME="install.windows.sun.cgi"
  9086. >CGI setup on Sun, iPlanet and Netscape servers</A
  9087. ></H3
  9088. ><P
  9089. >      To install PHP as a CGI handler, do the following:
  9090.     </P
  9091. ><P
  9092. ></P
  9093. ><UL
  9094. ><LI
  9095. ><P
  9096. >        Copy <TT
  9097. CLASS="filename"
  9098. >php4ts.dll</TT
  9099. > to your systemroot
  9100.        (the directory where you installed Windows)
  9101.       </P
  9102. ></LI
  9103. ><LI
  9104. ><P
  9105. >        Make a file association from the command line.
  9106.        Type the following two lines:
  9107.        <TABLE
  9108. BORDER="0"
  9109. BGCOLOR="#E0E0E0"
  9110. CELLPADDING="5"
  9111. ><TR
  9112. ><TD
  9113. ><PRE
  9114. CLASS="shell"
  9115. >assoc .php=PHPScript
  9116. ftype PHPScript=c:\php\php.exe %1 %*</PRE
  9117. ></TD
  9118. ></TR
  9119. ></TABLE
  9120. >
  9121.       </P
  9122. ></LI
  9123. ><LI
  9124. ><P
  9125. >        In the Netscape Enterprise Administration Server create
  9126.        a dummy shellcgi directory and remove it just after (this
  9127.        step creates 5 important lines in obj.conf and allow the
  9128.        web server to handle shellcgi scripts).
  9129.       </P
  9130. ></LI
  9131. ><LI
  9132. ><P
  9133. >        In the Netscape Enterprise Administration Server create
  9134.        a new mime type (Category: type,
  9135.        Content-Type: magnus-internal/shellcgi, File Suffix:php).
  9136.       </P
  9137. ></LI
  9138. ><LI
  9139. ><P
  9140. >        Do it for each web server instance you want PHP to run
  9141.       </P
  9142. ></LI
  9143. ></UL
  9144. ><P
  9145. >      More details about setting up
  9146.      PHP as a CGI executable can be found here:
  9147.      <A
  9148. HREF="http://benoit.noss.free.fr/php/install-php.html"
  9149. TARGET="_top"
  9150. >http://benoit.noss.free.fr/php/install-php.html</A
  9151. >
  9152.     </P
  9153. ></DIV
  9154. ><DIV
  9155. CLASS="sect2"
  9156. ><HR><H3
  9157. CLASS="sect2"
  9158. ><A
  9159. NAME="install.windows.sun.nsapi"
  9160. >NSAPI setup on Sun, iPlanet and Netscape servers</A
  9161. ></H3
  9162. ><P
  9163. >      To install PHP with NSAPI, do the following:
  9164.     </P
  9165. ><P
  9166. ></P
  9167. ><UL
  9168. ><LI
  9169. ><P
  9170. >        Copy <TT
  9171. CLASS="filename"
  9172. >php4ts.dll</TT
  9173. > to your systemroot
  9174.        (the directory where you installed Windows)
  9175.       </P
  9176. ></LI
  9177. ><LI
  9178. ><P
  9179. >        Make a file association from the command line.
  9180.        Type the following two lines:
  9181.        <TABLE
  9182. BORDER="0"
  9183. BGCOLOR="#E0E0E0"
  9184. CELLPADDING="5"
  9185. ><TR
  9186. ><TD
  9187. ><PRE
  9188. CLASS="shell"
  9189. >assoc .php=PHPScript
  9190. ftype PHPScript=c:\php\php.exe %1 %*</PRE
  9191. ></TD
  9192. ></TR
  9193. ></TABLE
  9194. >
  9195.       </P
  9196. ></LI
  9197. ><LI
  9198. ><P
  9199. >        In the Netscape Enterprise Administration Server create
  9200.        a new mime type (Category: type,
  9201.        Content-Type: magnus-internal/x-httpd-php, File Suffix: php).
  9202.       </P
  9203. ></LI
  9204. ><LI
  9205. ><P
  9206. >         Edit <TT
  9207. CLASS="filename"
  9208. >magnus.conf</TT
  9209. > (for servers >= 6) or
  9210.         <TT
  9211. CLASS="filename"
  9212. >obj.conf</TT
  9213. > (for servers < 6) and add the following:
  9214.         You should
  9215.         place the lines after <VAR
  9216. CLASS="literal"
  9217. >mime types init</VAR
  9218. >.
  9219.        <TABLE
  9220. BORDER="0"
  9221. BGCOLOR="#E0E0E0"
  9222. CELLPADDING="5"
  9223. ><TR
  9224. ><TD
  9225. ><PRE
  9226. CLASS="programlisting"
  9227. >Init fn="load-modules" funcs="php4_init,php4_execute,php4_auth_trans" shlib="c:/php/sapi/php4nsapi.dll"
  9228. Init fn="php4_init" LateInit="yes" errorString="Failed to initialise PHP!" [php_ini="c:/path/to/php.ini"]</PRE
  9229. ></TD
  9230. ></TR
  9231. ></TABLE
  9232. >
  9233.        (PHP >= 4.3.3) The <VAR
  9234. CLASS="literal"
  9235. >php_ini</VAR
  9236. > parameter is
  9237.        optional but with it you can place your
  9238.        <TT
  9239. CLASS="filename"
  9240. >php.ini</TT
  9241. > in your webserver config directory.
  9242.       </P
  9243. ></LI
  9244. ><LI
  9245. ><P
  9246. >        Configure the default object in <TT
  9247. CLASS="filename"
  9248. >obj.conf</TT
  9249. >
  9250.        (for virtual server classes [Sun Web Server 6.0+] in
  9251.        their <TT
  9252. CLASS="filename"
  9253. >vserver.obj.conf</TT
  9254. >):
  9255.        In the <VAR
  9256. CLASS="literal"
  9257. ><Object name="default"></VAR
  9258. >
  9259.        section, place this line necessarily after all 'ObjectType'
  9260.        and before all 'AddLog' lines:
  9261.        <TABLE
  9262. BORDER="0"
  9263. BGCOLOR="#E0E0E0"
  9264. CELLPADDING="5"
  9265. ><TR
  9266. ><TD
  9267. ><PRE
  9268. CLASS="programlisting"
  9269. >Service fn="php4_execute" type="magnus-internal/x-httpd-php" [inikey=value inikey=value ...]</PRE
  9270. ></TD
  9271. ></TR
  9272. ></TABLE
  9273. >
  9274.        (PHP >= 4.3.3) As additional parameters you can add some special
  9275.        <TT
  9276. CLASS="filename"
  9277. >php.ini</TT
  9278. >-values, for example you
  9279.        can set a <VAR
  9280. CLASS="literal"
  9281. >docroot="/path/to/docroot"</VAR
  9282. >
  9283.        specific to the context <VAR
  9284. CLASS="literal"
  9285. >php4_execute</VAR
  9286. >
  9287.        is called. For boolean ini-keys please use 0/1 as value,
  9288.        not <VAR
  9289. CLASS="literal"
  9290. >"On","Off",...</VAR
  9291. >
  9292.        (this will not work correctly), e.g.
  9293.        <VAR
  9294. CLASS="literal"
  9295. >zlib.output_compression=1</VAR
  9296. > instead of
  9297.        <VAR
  9298. CLASS="literal"
  9299. >zlib.output_compression="On"</VAR
  9300. >
  9301.       </P
  9302. ></LI
  9303. ><LI
  9304. ><P
  9305. >        This is only needed if you want to configure a directory that only consists of
  9306.        PHP scripts (same like a cgi-bin directory):
  9307.        <TABLE
  9308. BORDER="0"
  9309. BGCOLOR="#E0E0E0"
  9310. CELLPADDING="5"
  9311. ><TR
  9312. ><TD
  9313. ><PRE
  9314. CLASS="programlisting"
  9315. ><Object name="x-httpd-php">
  9316. ObjectType fn="force-type" type="magnus-internal/x-httpd-php"
  9317. Service fn=php4_execute [inikey=value inikey=value ...]
  9318. </Object></PRE
  9319. ></TD
  9320. ></TR
  9321. ></TABLE
  9322. >
  9323.        After that you can configure a directory in the Administration server and assign it
  9324.        the style <VAR
  9325. CLASS="literal"
  9326. >x-httpd-php</VAR
  9327. >. All files in it will get executed as PHP.
  9328.        This is nice to hide PHP usage by renaming files to <TT
  9329. CLASS="filename"
  9330. >.html</TT
  9331. >.
  9332.       </P
  9333. ></LI
  9334. ><LI
  9335. ><P
  9336. >        Restart your web service and apply changes
  9337.       </P
  9338. ></LI
  9339. ><LI
  9340. ><P
  9341. >        Do it for each web server instance you want PHP to run
  9342.       </P
  9343. ></LI
  9344. ></UL
  9345. ><DIV
  9346. CLASS="note"
  9347. ><BLOCKQUOTE
  9348. CLASS="note"
  9349. ><P
  9350. ><B
  9351. >Note: </B
  9352. >
  9353.       More details about setting up
  9354.       PHP as an NSAPI filter can be found here:
  9355.       <A
  9356. HREF="http://benoit.noss.free.fr/php/install-php4.html"
  9357. TARGET="_top"
  9358. >http://benoit.noss.free.fr/php/install-php4.html</A
  9359. >
  9360.      </P
  9361. ></BLOCKQUOTE
  9362. ></DIV
  9363. ><DIV
  9364. CLASS="note"
  9365. ><BLOCKQUOTE
  9366. CLASS="note"
  9367. ><P
  9368. ><B
  9369. >Note: </B
  9370. >
  9371.       The stacksize that PHP uses depends on the configuration of the webserver. If you get
  9372.       crashes with very large PHP scripts, it is recommended to raise it with the Admin Server
  9373.       (in the section "MAGNUS EDITOR").
  9374.      </P
  9375. ></BLOCKQUOTE
  9376. ></DIV
  9377. ></DIV
  9378. ><DIV
  9379. CLASS="sect2"
  9380. ><HR><H3
  9381. CLASS="sect2"
  9382. ><A
  9383. NAME="install.windows.sun.phpini"
  9384. >CGI environment and recommended modifications in php.ini</A
  9385. ></H3
  9386. ><P
  9387. >      Important when writing PHP scripts is the fact that Sun JSWS/Sun ONE
  9388.      WS/iPlanet/Netscape is a multithreaded web server. Because of that all
  9389.      requests are running in the same process space (the space of the webserver
  9390.      itself) and this space has only one environment. If you want to get CGI
  9391.      variables like <VAR
  9392. CLASS="literal"
  9393. >PATH_INFO</VAR
  9394. >, <VAR
  9395. CLASS="literal"
  9396. >HTTP_HOST</VAR
  9397. >
  9398.      etc. it is not the correct way to try this in the old PHP 3.x way with
  9399.      <A
  9400. HREF="#function.getenv"
  9401. ><B
  9402. CLASS="function"
  9403. >getenv()</B
  9404. ></A
  9405. > or a similar way (register globals to
  9406.      environment, <VAR
  9407. CLASS="literal"
  9408. >$_ENV</VAR
  9409. >). You would only get the environment
  9410.      of the running webserver without any valid CGI variables!
  9411.     </P
  9412. ><DIV
  9413. CLASS="note"
  9414. ><BLOCKQUOTE
  9415. CLASS="note"
  9416. ><P
  9417. ><B
  9418. >Note: </B
  9419. >
  9420.       Why are there (invalid) CGI variables in the environment?
  9421.      </P
  9422. ><P
  9423. >       Answer: This is because you started the webserver process from the admin server
  9424.       which runs the startup script of the webserver, you wanted to start, as a CGI script
  9425.       (a CGI script inside of the admin server!). This is why the environment of
  9426.       the started webserver has some CGI environment variables in it. You can test
  9427.       this by starting the webserver not from the administration server. Use
  9428.       the command line as root user and start it manually - you will see
  9429.       there are no CGI-like environment variables.
  9430.      </P
  9431. ></BLOCKQUOTE
  9432. ></DIV
  9433. ><P
  9434. >      Simply change your scripts to get CGI variables in the correct way for
  9435.      PHP 4.x by using the superglobal <VAR
  9436. CLASS="literal"
  9437. >$_SERVER</VAR
  9438. >. If you have
  9439.      older scripts which use <VAR
  9440. CLASS="literal"
  9441. >$HTTP_HOST</VAR
  9442. >, etc., you should turn
  9443.      on <VAR
  9444. CLASS="literal"
  9445. >register_globals</VAR
  9446. > in <TT
  9447. CLASS="filename"
  9448. >php.ini</TT
  9449. > and change the variable
  9450.      order too (important: remove <VAR
  9451. CLASS="literal"
  9452. >"E"</VAR
  9453. > from it,
  9454.      because you do not need the environment here):
  9455.      <TABLE
  9456. BORDER="0"
  9457. BGCOLOR="#E0E0E0"
  9458. CELLPADDING="5"
  9459. ><TR
  9460. ><TD
  9461. ><PRE
  9462. CLASS="programlisting"
  9463. >variables_order = "GPCS"
  9464. register_globals = On</PRE
  9465. ></TD
  9466. ></TR
  9467. ></TABLE
  9468. >
  9469.     </P
  9470. ></DIV
  9471. ><DIV
  9472. CLASS="sect2"
  9473. ><HR><H3
  9474. CLASS="sect2"
  9475. ><A
  9476. NAME="install.windows.sun.specialpages"
  9477. >Special use for error pages or self-made directory listings (PHP >= 4.3.3)</A
  9478. ></H3
  9479. ><P
  9480. >      You can use PHP to generate the error pages for <VAR
  9481. CLASS="literal"
  9482. >"404 Not Found"</VAR
  9483. >
  9484.      or similar. Add the following line to the object in <TT
  9485. CLASS="filename"
  9486. >obj.conf</TT
  9487. > for
  9488.      every error page you want to overwrite:
  9489.      <TABLE
  9490. BORDER="0"
  9491. BGCOLOR="#E0E0E0"
  9492. CELLPADDING="5"
  9493. ><TR
  9494. ><TD
  9495. ><PRE
  9496. CLASS="programlisting"
  9497. >Error fn="php4_execute" code=XXX script="/path/to/script.php" [inikey=value inikey=value...]</PRE
  9498. ></TD
  9499. ></TR
  9500. ></TABLE
  9501. >
  9502.      where <VAR
  9503. CLASS="literal"
  9504. >XXX</VAR
  9505. > is the HTTP error code. Please delete
  9506.      any other <VAR
  9507. CLASS="literal"
  9508. >Error</VAR
  9509. > directives which could interfere with yours.
  9510.      If you want to place a page for all errors that could exist, leave
  9511.      the <VAR
  9512. CLASS="literal"
  9513. >code</VAR
  9514. > parameter out. Your script can get the HTTP status code
  9515.      with <VAR
  9516. CLASS="literal"
  9517. >$_SERVER['ERROR_TYPE']</VAR
  9518. >.
  9519.     </P
  9520. ><P
  9521. >      Another possibility is to generate self-made directory listings.
  9522.      Just create a PHP script which displays a directory listing and
  9523.      replace the corresponding default Service line for
  9524.      <VAR
  9525. CLASS="literal"
  9526. >type="magnus-internal/directory"</VAR
  9527. >
  9528.      in <TT
  9529. CLASS="filename"
  9530. >obj.conf</TT
  9531. > with the following:
  9532.      <TABLE
  9533. BORDER="0"
  9534. BGCOLOR="#E0E0E0"
  9535. CELLPADDING="5"
  9536. ><TR
  9537. ><TD
  9538. ><PRE
  9539. CLASS="programlisting"
  9540. >Service fn="php4_execute" type="magnus-internal/directory" script="/path/to/script.php" [inikey=value inikey=value...]</PRE
  9541. ></TD
  9542. ></TR
  9543. ></TABLE
  9544. >
  9545.      For both error and directory listing pages the original URI and
  9546.      translated URI are in the variables <VAR
  9547. CLASS="literal"
  9548. >$_SERVER['PATH_INFO']</VAR
  9549. > and
  9550.      <VAR
  9551. CLASS="literal"
  9552. >$_SERVER['PATH_TRANSLATED']</VAR
  9553. >.
  9554.     </P
  9555. ></DIV
  9556. ><DIV
  9557. CLASS="sect2"
  9558. ><HR><H3
  9559. CLASS="sect2"
  9560. ><A
  9561. NAME="install.windows.sun.notes"
  9562. >Note about <A
  9563. HREF="#function.nsapi-virtual"
  9564. ><B
  9565. CLASS="function"
  9566. >nsapi_virtual()</B
  9567. ></A
  9568. > and subrequests (PHP >= 4.3.3)</A
  9569. ></H3
  9570. ><P
  9571. >      The NSAPI module now supports the <A
  9572. HREF="#function.nsapi-virtual"
  9573. ><B
  9574. CLASS="function"
  9575. >nsapi_virtual()</B
  9576. ></A
  9577. > function
  9578.      (alias: <A
  9579. HREF="#function.virtual"
  9580. ><B
  9581. CLASS="function"
  9582. >virtual()</B
  9583. ></A
  9584. >)
  9585.      to make subrequests on the webserver and insert the result in the webpage.
  9586.      The problem is, that this function uses some undocumented features from
  9587.      the NSAPI library.
  9588.     </P
  9589. ><P
  9590. >      Under Unix this is not a problem, because the module automatically looks
  9591.      for the needed functions and uses them if available.
  9592.      If not, <A
  9593. HREF="#function.nsapi-virtual"
  9594. ><B
  9595. CLASS="function"
  9596. >nsapi_virtual()</B
  9597. ></A
  9598. > is disabled.
  9599.     </P
  9600. ><P
  9601. >      Under Windows limitations in the DLL handling need the use of a automatic
  9602.      detection of the most recent <TT
  9603. CLASS="filename"
  9604. >ns-httpdXX.dll</TT
  9605. > file.
  9606.      This is tested for servers till version 6.1. If a newer version of the
  9607.      Sun server is used, the detection fails and <A
  9608. HREF="#function.nsapi-virtual"
  9609. ><B
  9610. CLASS="function"
  9611. >nsapi_virtual()</B
  9612. ></A
  9613. >
  9614.      is disabled.
  9615.     </P
  9616. ><P
  9617. >      If this is the case, try the following:
  9618.      Add the following parameter to <VAR
  9619. CLASS="literal"
  9620. >php4_init</VAR
  9621. > in
  9622.      <TT
  9623. CLASS="filename"
  9624. >magnus.conf</TT
  9625. >/<TT
  9626. CLASS="filename"
  9627. >obj.conf</TT
  9628. >:
  9629.      <TABLE
  9630. BORDER="0"
  9631. BGCOLOR="#E0E0E0"
  9632. CELLPADDING="5"
  9633. ><TR
  9634. ><TD
  9635. ><PRE
  9636. CLASS="programlisting"
  9637. >Init fn=php4_init ... server_lib="ns-httpdXX.dll"</PRE
  9638. ></TD
  9639. ></TR
  9640. ></TABLE
  9641. >
  9642.      where <VAR
  9643. CLASS="literal"
  9644. >XX</VAR
  9645. > is the correct DLL version number.
  9646.      To get it, look in the server-root for the correct DLL name. The
  9647.      DLL with the biggest filesize is the right one.
  9648.     </P
  9649. ><P
  9650. >      You can check the status by using the <A
  9651. HREF="#function.phpinfo"
  9652. ><B
  9653. CLASS="function"
  9654. >phpinfo()</B
  9655. ></A
  9656. > function.
  9657.     </P
  9658. ><DIV
  9659. CLASS="note"
  9660. ><BLOCKQUOTE
  9661. CLASS="note"
  9662. ><P
  9663. ><B
  9664. >Note: </B
  9665. >
  9666.       But be warned: Support for <A
  9667. HREF="#function.nsapi-virtual"
  9668. ><B
  9669. CLASS="function"
  9670. >nsapi_virtual()</B
  9671. ></A
  9672. > is EXPERIMENTAL!!!
  9673.      </P
  9674. ></BLOCKQUOTE
  9675. ></DIV
  9676. ></DIV
  9677. ></DIV
  9678. ><DIV
  9679. CLASS="sect1"
  9680. ><HR><H2
  9681. CLASS="sect1"
  9682. ><A
  9683. NAME="install.windows.omnihttpd"
  9684. >OmniHTTPd Server</A
  9685. ></H2
  9686. ><P
  9687. >     This section contains notes and hints specific to
  9688.     <A
  9689. HREF="http://www.omnicron.ca/"
  9690. TARGET="_top"
  9691. >OmniHTTPd</A
  9692. > on Windows.
  9693.    </P
  9694. ><DIV
  9695. CLASS="note"
  9696. ><BLOCKQUOTE
  9697. CLASS="note"
  9698. ><P
  9699. ><B
  9700. >Note: </B
  9701. >
  9702.      You should read the <A
  9703. HREF="#install.windows.manual"
  9704. >manual
  9705.      installation steps</A
  9706. > first!
  9707.     </P
  9708. ></BLOCKQUOTE
  9709. ></DIV
  9710. ><DIV
  9711. CLASS="warning"
  9712. ><P
  9713. ></P
  9714. ><TABLE
  9715. CLASS="warning"
  9716. BORDER="1"
  9717. WIDTH="100%"
  9718. ><TR
  9719. ><TD
  9720. ALIGN="CENTER"
  9721. ><B
  9722. >Warning</B
  9723. ></TD
  9724. ></TR
  9725. ><TR
  9726. ><TD
  9727. ALIGN="LEFT"
  9728. ><P
  9729. >By using the CGI setup, your server
  9730. is open to several possible attacks. Please read our
  9731. <A
  9732. HREF="#security.cgi-bin"
  9733. >CGI security section</A
  9734. > to learn how to
  9735. defend yourself from those attacks.</P
  9736. ></TD
  9737. ></TR
  9738. ></TABLE
  9739. ></DIV
  9740. ><P
  9741. >     You need to complete the following steps to make PHP
  9742.     work with OmniHTTPd. This is a CGI executable setup.
  9743.     SAPI is supported by OmniHTTPd, but some tests have shown
  9744.     that it is not so stable to use PHP as an ISAPI module.
  9745.    </P
  9746. ><DIV
  9747. CLASS="note"
  9748. ><BLOCKQUOTE
  9749. CLASS="note"
  9750. ><P
  9751. ><B
  9752. >Important for CGI users: </B
  9753. >
  9754.      Read the <A
  9755. HREF="#faq.installation.forceredirect"
  9756. >faq
  9757.      on cgi.force_redirect</A
  9758. > for important details.  This
  9759.      directive needs to be set to <VAR
  9760. CLASS="literal"
  9761. >0</VAR
  9762. >.
  9763.     </P
  9764. ></BLOCKQUOTE
  9765. ></DIV
  9766. ><P
  9767. >     <P
  9768. ></P
  9769. ><OL
  9770. TYPE="1"
  9771. ><LI
  9772. ><P
  9773. >        Install OmniHTTPd server.
  9774.       </P
  9775. ></LI
  9776. ><LI
  9777. ><P
  9778. >        Right click on the blue OmniHTTPd icon in the system
  9779.        tray and select <VAR
  9780. CLASS="literal"
  9781. >Properties</VAR
  9782. >
  9783.       </P
  9784. ></LI
  9785. ><LI
  9786. ><P
  9787. >        Click on <VAR
  9788. CLASS="literal"
  9789. >Web Server Global Settings</VAR
  9790. >
  9791.       </P
  9792. ></LI
  9793. ><LI
  9794. ><P
  9795. >        On the 'External' tab, enter: <VAR
  9796. CLASS="literal"
  9797. >virtual = .php
  9798.         | actual = c:\php\php.exe</VAR
  9799. > (use
  9800.        <TT
  9801. CLASS="filename"
  9802. >php-cgi.exe</TT
  9803. > if installing PHP 5), and use the Add
  9804.        button.
  9805.       </P
  9806. ></LI
  9807. ><LI
  9808. ><P
  9809. >        On the <VAR
  9810. CLASS="literal"
  9811. >Mime</VAR
  9812. > tab, enter:
  9813.        <VAR
  9814. CLASS="literal"
  9815. >virtual = wwwserver/stdcgi | actual = .php</VAR
  9816. >,
  9817.        and use the Add button.
  9818.       </P
  9819. ></LI
  9820. ><LI
  9821. ><P
  9822. >        Click <VAR
  9823. CLASS="literal"
  9824. >OK</VAR
  9825. >
  9826.       </P
  9827. ></LI
  9828. ></OL
  9829. >
  9830.    </P
  9831. ><P
  9832. >     Repeat steps 2 - 6 for each extension you want to associate with PHP.
  9833.    </P
  9834. ><DIV
  9835. CLASS="note"
  9836. ><BLOCKQUOTE
  9837. CLASS="note"
  9838. ><P
  9839. ><B
  9840. >Note: </B
  9841. >
  9842.      Some OmniHTTPd packages come with built in PHP support.
  9843.      You can choose at setup time to do a custom setup, and
  9844.      uncheck the PHP component. We recommend you to use the latest
  9845.      PHP binaries. Some OmniHTTPd servers come with PHP 4 beta
  9846.      distributions, so you should choose not to set up
  9847.      the built in support, but install your own. If the server
  9848.      is already on your machine, use the Replace button in Step
  9849.      4 and 5 to set the new, correct information.
  9850.     </P
  9851. ></BLOCKQUOTE
  9852. ></DIV
  9853. ></DIV
  9854. ><DIV
  9855. CLASS="sect1"
  9856. ><HR><H2
  9857. CLASS="sect1"
  9858. ><A
  9859. NAME="install.windows.sambar"
  9860. >Sambar Server on Microsoft Windows</A
  9861. ></H2
  9862. ><P
  9863. >      This section contains notes and hints specific to the
  9864.      <A
  9865. HREF="http://www.sambar.com/"
  9866. TARGET="_top"
  9867. >Sambar Server</A
  9868. > for Windows.
  9869.     </P
  9870. ><DIV
  9871. CLASS="note"
  9872. ><BLOCKQUOTE
  9873. CLASS="note"
  9874. ><P
  9875. ><B
  9876. >Note: </B
  9877. >
  9878.       You should read the <A
  9879. HREF="#install.windows.manual"
  9880. >manual
  9881.        installation steps</A
  9882. > first!
  9883.      </P
  9884. ></BLOCKQUOTE
  9885. ></DIV
  9886. ><P
  9887. >      This list describes how to set up the ISAPI module to
  9888.      work with the Sambar server on Windows.
  9889.     </P
  9890. ><P
  9891. >      <P
  9892. ></P
  9893. ><UL
  9894. ><LI
  9895. ><P
  9896. >         Find the file called <TT
  9897. CLASS="filename"
  9898. >mappings.ini</TT
  9899. > (in the config
  9900.         directory) in the Sambar install directory.
  9901.        </P
  9902. ></LI
  9903. ><LI
  9904. ><P
  9905. >         Open <TT
  9906. CLASS="filename"
  9907. >mappings.ini</TT
  9908. > and add the following line
  9909.         under <VAR
  9910. CLASS="literal"
  9911. >[ISAPI]</VAR
  9912. >:
  9913.       <TABLE
  9914. WIDTH="100%"
  9915. BORDER="0"
  9916. CELLPADDING="0"
  9917. CELLSPACING="0"
  9918. CLASS="EXAMPLE"
  9919. ><TR
  9920. ><TD
  9921. ><DIV
  9922. CLASS="example"
  9923. ><A
  9924. NAME="AEN1659"
  9925. ></A
  9926. ><P
  9927. ><B
  9928. >Example 6-7. ISAPI configuration of Sambar</B
  9929. ></P
  9930. ><TABLE
  9931. BORDER="0"
  9932. BGCOLOR="#E0E0E0"
  9933. CELLPADDING="5"
  9934. ><TR
  9935. ><TD
  9936. ><PRE
  9937. CLASS="programlisting"
  9938. >#for PHP 4
  9939. *.php = c:\php\php4isapi.dll
  9940.  
  9941. #for PHP 5
  9942. *.php = c:\php\php5isapi.dll</PRE
  9943. ></TD
  9944. ></TR
  9945. ></TABLE
  9946. ></DIV
  9947. ></TD
  9948. ></TR
  9949. ></TABLE
  9950. >
  9951.         (This line assumes that PHP was installed in
  9952.         <TT
  9953. CLASS="filename"
  9954. >c:\php</TT
  9955. >.)
  9956.        </P
  9957. ></LI
  9958. ><LI
  9959. ><P
  9960. >         Now restart the Sambar server for the changes to take effect.
  9961.        </P
  9962. ></LI
  9963. ></UL
  9964. >
  9965.     </P
  9966. ></DIV
  9967. ><DIV
  9968. CLASS="sect1"
  9969. ><HR><H2
  9970. CLASS="sect1"
  9971. ><A
  9972. NAME="install.windows.xitami"
  9973. >Xitami on Microsoft Windows</A
  9974. ></H2
  9975. ><P
  9976. >      This section contains notes and hints specific to
  9977.      <A
  9978. HREF="http://www.xitami.com/"
  9979. TARGET="_top"
  9980. >Xitami</A
  9981. > on Windows.
  9982.     </P
  9983. ><DIV
  9984. CLASS="note"
  9985. ><BLOCKQUOTE
  9986. CLASS="note"
  9987. ><P
  9988. ><B
  9989. >Note: </B
  9990. >
  9991.       You should read the <A
  9992. HREF="#install.windows.manual"
  9993. >manual
  9994.        installation steps</A
  9995. > first!
  9996.      </P
  9997. ></BLOCKQUOTE
  9998. ></DIV
  9999. ><P
  10000. >      This list describes how to set up the PHP CGI binary
  10001.      to work with Xitami on Windows.
  10002.     </P
  10003. ><DIV
  10004. CLASS="note"
  10005. ><BLOCKQUOTE
  10006. CLASS="note"
  10007. ><P
  10008. ><B
  10009. >Important for CGI users: </B
  10010. >
  10011.       Read the <A
  10012. HREF="#faq.installation.forceredirect"
  10013. >faq
  10014.       on cgi.force_redirect</A
  10015. > for important details.  This
  10016.       directive needs to be set to <VAR
  10017. CLASS="literal"
  10018. >0</VAR
  10019. >.
  10020.       If you want to use <VAR
  10021. CLASS="literal"
  10022. >$_SERVER['PHP_SELF']</VAR
  10023. > you have to
  10024.       enable the <A
  10025. HREF="#ini.cgi.fix-pathinfo"
  10026. >cgi.fix_pathinfo</A
  10027. >
  10028.       directive.
  10029.      </P
  10030. ></BLOCKQUOTE
  10031. ></DIV
  10032. ><DIV
  10033. CLASS="warning"
  10034. ><P
  10035. ></P
  10036. ><TABLE
  10037. CLASS="warning"
  10038. BORDER="1"
  10039. WIDTH="100%"
  10040. ><TR
  10041. ><TD
  10042. ALIGN="CENTER"
  10043. ><B
  10044. >Warning</B
  10045. ></TD
  10046. ></TR
  10047. ><TR
  10048. ><TD
  10049. ALIGN="LEFT"
  10050. ><P
  10051. >By using the CGI setup, your server
  10052. is open to several possible attacks. Please read our
  10053. <A
  10054. HREF="#security.cgi-bin"
  10055. >CGI security section</A
  10056. > to learn how to
  10057. defend yourself from those attacks.</P
  10058. ></TD
  10059. ></TR
  10060. ></TABLE
  10061. ></DIV
  10062. ><P
  10063. >      <P
  10064. ></P
  10065. ><UL
  10066. ><LI
  10067. ><P
  10068. >         Make sure the webserver is running, and point
  10069.         your browser to xitamis admin console
  10070.         (usually <VAR
  10071. CLASS="literal"
  10072. >http://127.0.0.1/admin</VAR
  10073. >),
  10074.         and click on Configuration.
  10075.        </P
  10076. ></LI
  10077. ><LI
  10078. ><P
  10079. >         Navigate to the Filters, and put the
  10080.         extension which PHP should parse (i.e. .php)
  10081.         into the field File extensions (.xxx).
  10082.        </P
  10083. ></LI
  10084. ><LI
  10085. ><P
  10086. >         In Filter command or script put the path and name
  10087.         of your PHP CGI executable i.e. <TT
  10088. CLASS="filename"
  10089. >C:\php\php.exe</TT
  10090. >
  10091.         for PHP 4, or <TT
  10092. CLASS="filename"
  10093. >C:\php\php-cgi.exe</TT
  10094. > for PHP 5.
  10095.        </P
  10096. ></LI
  10097. ><LI
  10098. ><P
  10099. >         Press the 'Save' icon.
  10100.        </P
  10101. ></LI
  10102. ><LI
  10103. ><P
  10104. >         Restart the server to reflect changes.
  10105.        </P
  10106. ></LI
  10107. ></UL
  10108. >
  10109.     </P
  10110. ></DIV
  10111. ><DIV
  10112. CLASS="sect1"
  10113. ><HR><H2
  10114. CLASS="sect1"
  10115. ><A
  10116. NAME="install.windows.building"
  10117. >Building from source</A
  10118. ></H2
  10119. ><P
  10120. >      Before getting started, it is worthwhile answering the question:
  10121.      "Why is building on Windows so hard?" Two reasons come to mind:
  10122.     </P
  10123. ><P
  10124. ></P
  10125. ><OL
  10126. TYPE="1"
  10127. ><LI
  10128. ><P
  10129. >        Windows does not (yet) enjoy a large community of developers
  10130.        who are willing to freely share their source. As a direct
  10131.        result, the necessary investment in infrastructure required
  10132.        to support such development hasn't been made. By and large,
  10133.        what is available has been made possible by the porting of
  10134.        necessary utilities from Unix. Don't be surprised if some of
  10135.        this heritage shows through from time to time.
  10136.       </P
  10137. ></LI
  10138. ><LI
  10139. ><P
  10140. >        Pretty much all of the instructions that follow are of the
  10141.        "set and forget" variety. So sit back and try follow the
  10142.        instructions below as faithfully as you can.
  10143.       </P
  10144. ></LI
  10145. ></OL
  10146. ><DIV
  10147. CLASS="sect2"
  10148. ><HR><H3
  10149. CLASS="sect2"
  10150. ><A
  10151. NAME="install.windows.building.requirement"
  10152. >Requirements</A
  10153. ></H3
  10154. ><P
  10155. >       To compile and build PHP you need a Microsoft
  10156.       Development Environment. Microsoft Visual C++ 6.0 is recommended.
  10157.       To extract the downloaded files you need a extraction utility
  10158.       (e.g.: Winzip). If you don't already have an unzip utility, you
  10159.       can get a free version from <A
  10160. HREF="http://www.info-zip.org/pub/infozip/"
  10161. TARGET="_top"
  10162. >InfoZip</A
  10163. >.
  10164.      </P
  10165. ><P
  10166. >       Before you get started, you have to download...
  10167.      </P
  10168. ><P
  10169. ></P
  10170. ><UL
  10171. ><LI
  10172. ><P
  10173. >         ..the win32 buildtools from the PHP site
  10174.         at <A
  10175. HREF="http://www.php.net/extra/win32build.zip"
  10176. TARGET="_top"
  10177. >http://www.php.net/extra/win32build.zip</A
  10178. >.
  10179.        </P
  10180. ></LI
  10181. ><LI
  10182. ><P
  10183. >         ..the source code for the DNS name resolver used by PHP from
  10184.         <A
  10185. HREF="http://www.php.net/extra/bindlib_w32.zip"
  10186. TARGET="_top"
  10187. >         http://www.php.net/extra/bindlib_w32.zip</A
  10188. >. This is a replacement for the 
  10189.         <TT
  10190. CLASS="filename"
  10191. >resolv.lib</TT
  10192. > library included in <TT
  10193. CLASS="filename"
  10194. >         win32build.zip</TT
  10195. >.
  10196.        </P
  10197. ></LI
  10198. ><LI
  10199. ><P
  10200. >         If you plan to compile PHP as a Apache
  10201.         module you will also need the <A
  10202. HREF="http://www.apache.org/dist/httpd/"
  10203. TARGET="_top"
  10204. >Apache
  10205.         sources</A
  10206. >.
  10207.        </P
  10208. ></LI
  10209. ></UL
  10210. ><P
  10211. >       Finally, you are going to need the source to PHP itself. You can get
  10212.       the latest development version using <A
  10213. HREF="http://www.php.net/anoncvs.php"
  10214. TARGET="_top"
  10215. >       anonymous CVS</A
  10216. >, a <A
  10217. HREF="http://snaps.php.net/"
  10218. TARGET="_top"
  10219. >       snapshot</A
  10220. > or the most recent released <A
  10221. HREF="http://www.php.net/downloads.php"
  10222. TARGET="_top"
  10223. >       source</A
  10224. > tarball.
  10225.      </P
  10226. ></DIV
  10227. ><DIV
  10228. CLASS="sect2"
  10229. ><HR><H3
  10230. CLASS="sect2"
  10231. ><A
  10232. NAME="install.windows.building.install"
  10233. >Putting it all together</A
  10234. ></H3
  10235. ><P
  10236. >       After downloading the required packages you have to extract them in a
  10237.       proper place.
  10238.       <P
  10239. ></P
  10240. ><UL
  10241. ><LI
  10242. ><P
  10243. >          Create a working directory where all files end up after extracting, e.g:
  10244.          <TT
  10245. CLASS="filename"
  10246. >C:\work</TT
  10247. >.
  10248.         </P
  10249. ></LI
  10250. ><LI
  10251. ><P
  10252. >          Create the directory <TT
  10253. CLASS="filename"
  10254. >win32build</TT
  10255. > under your
  10256.          working directory (<TT
  10257. CLASS="filename"
  10258. >C:\work</TT
  10259. >) and unzip <TT
  10260. CLASS="filename"
  10261. >          win32build.zip</TT
  10262. > into it.
  10263.         </P
  10264. ></LI
  10265. ><LI
  10266. ><P
  10267. >          Create the directory <TT
  10268. CLASS="filename"
  10269. >bindlib_w32</TT
  10270. > under your
  10271.          working directory (<TT
  10272. CLASS="filename"
  10273. >C:\work</TT
  10274. >) and unzip <TT
  10275. CLASS="filename"
  10276. >          bindlib_w32.zip</TT
  10277. > into it.
  10278.         </P
  10279. ></LI
  10280. ><LI
  10281. ><P
  10282. >          Extract the downloaded PHP source code into your working directory
  10283.          (<TT
  10284. CLASS="filename"
  10285. >C:\work</TT
  10286. >).
  10287.         </P
  10288. ></LI
  10289. ></UL
  10290. >
  10291.       Following this steps your directory structure looks like this:
  10292.       <DIV
  10293. CLASS="informalexample"
  10294. ><P
  10295. ></P
  10296. ><A
  10297. NAME="AEN1747"
  10298. ></A
  10299. ><TABLE
  10300. BORDER="0"
  10301. BGCOLOR="#E0E0E0"
  10302. CELLPADDING="5"
  10303. ><TR
  10304. ><TD
  10305. ><PRE
  10306. CLASS="programlisting"
  10307. >+--c:\work
  10308. |  |
  10309. |  +--bindlib_w32
  10310. |  |  |
  10311. |  |  +--arpa
  10312. |  |  |
  10313. |  |  +--conf
  10314. |  |  |
  10315. |  |  +--...
  10316. |  |
  10317. |  +--php-4.x.x
  10318. |  |  |
  10319. |  |  +--build
  10320. |  |  |
  10321. |  |  +--...
  10322. |  |  |
  10323. |  |  +--win32
  10324. |  |  |
  10325. |  |  +--...
  10326. |  |
  10327. |  +--win32build
  10328. |  |  |
  10329. |  |  +--bin
  10330. |  |  |
  10331. |  |  +--include
  10332. |  |  |
  10333. |  |  +--lib</PRE
  10334. ></TD
  10335. ></TR
  10336. ></TABLE
  10337. ><P
  10338. ></P
  10339. ></DIV
  10340. >
  10341.       Create the directories <TT
  10342. CLASS="filename"
  10343. >c:\usr\local\lib</TT
  10344. >. Copy
  10345.       <TT
  10346. CLASS="filename"
  10347. >bison.simple</TT
  10348. > from <TT
  10349. CLASS="filename"
  10350. >c:\work\win32build\bin
  10351.       </TT
  10352. > to <TT
  10353. CLASS="filename"
  10354. >c:\usr\local\lib</TT
  10355. >.
  10356.      </P
  10357. ><DIV
  10358. CLASS="note"
  10359. ><BLOCKQUOTE
  10360. CLASS="note"
  10361. ><P
  10362. ><B
  10363. >Note: </B
  10364. >
  10365.        <A
  10366. HREF="http://www.cygwin.com/"
  10367. TARGET="_top"
  10368. >Cygwin</A
  10369. > users may omit the last
  10370.        step. A properly installed Cygwin environment provides the mandatory
  10371.        files <TT
  10372. CLASS="filename"
  10373. >bison.simple</TT
  10374. > and
  10375.        <TT
  10376. CLASS="filename"
  10377. >bison.exe</TT
  10378. >.
  10379.       </P
  10380. ></BLOCKQUOTE
  10381. ></DIV
  10382. ></DIV
  10383. ><DIV
  10384. CLASS="sect2"
  10385. ><HR><H3
  10386. CLASS="sect2"
  10387. ><A
  10388. NAME="install.windows.building.configure"
  10389. >Configure MVC ++</A
  10390. ></H3
  10391. ><P
  10392. >        The next step is to configure MVC ++ to prepare for compiling.
  10393.        Launch Microsoft Visual C++, and from the menu select
  10394.        Tools => Options. In the dialog, select the
  10395.        directories tab. Sequentially change the dropdown
  10396.        to Executables, Includes, and Library files. Your entries should look
  10397.        like this:
  10398.        <P
  10399. ></P
  10400. ><UL
  10401. ><LI
  10402. ><P
  10403. >           Executable files: <TT
  10404. CLASS="filename"
  10405. >c:\work\win32build\bin</TT
  10406. >,
  10407.           Cygwin users: <TT
  10408. CLASS="filename"
  10409. >cygwin\bin</TT
  10410. >
  10411.          </P
  10412. ></LI
  10413. ><LI
  10414. ><P
  10415. >           Include files: <TT
  10416. CLASS="filename"
  10417. >c:\work\win32build\include</TT
  10418. >
  10419.          </P
  10420. ></LI
  10421. ><LI
  10422. ><P
  10423. >           Library files: <TT
  10424. CLASS="filename"
  10425. >c:\work\win32build\lib</TT
  10426. >
  10427.          </P
  10428. ></LI
  10429. ></UL
  10430. >
  10431.       </P
  10432. ></DIV
  10433. ><DIV
  10434. CLASS="sect2"
  10435. ><HR><H3
  10436. CLASS="sect2"
  10437. ><A
  10438. NAME="install.windows.building.resolvlib"
  10439. >Build resolv.lib</A
  10440. ></H3
  10441. ><P
  10442. >        You must build the <TT
  10443. CLASS="filename"
  10444. >resolv.lib</TT
  10445. > library.
  10446.        Decide whether you want to have debug symbols available
  10447.        (bindlib - Win32 Debug) or not (bindlib - Win32 Release).
  10448.        Build the appropriate configuration: 
  10449.        <P
  10450. ></P
  10451. ><UL
  10452. ><LI
  10453. ><P
  10454. >           For GUI users, launch VC++, and then select File => Open
  10455.           Workspace, navigate to <TT
  10456. CLASS="filename"
  10457. >c:\work\bindlib_w32</TT
  10458. > and
  10459.           select <TT
  10460. CLASS="filename"
  10461. >bindlib.dsw</TT
  10462. >. Then select Build=>Set
  10463.           Active Configuration and select the desired configuration.
  10464.           Finally select Build=>Rebuild All.
  10465.          </P
  10466. ></LI
  10467. ><LI
  10468. ><P
  10469. >           For command line users, make sure that you either have the C++
  10470.           environment variables registered, or have run
  10471.           <B
  10472. CLASS="command"
  10473. >vcvars.bat</B
  10474. >, and then execute one of the following
  10475.           commands:
  10476.          </P
  10477. ><P
  10478. ></P
  10479. ><UL
  10480. ><LI
  10481. ><P
  10482. >             <KBD
  10483. CLASS="userinput"
  10484. >msdev bindlib.dsp /MAKE "bindlib - Win32 Debug"</KBD
  10485. >
  10486.            </P
  10487. ></LI
  10488. ><LI
  10489. ><P
  10490. >             <KBD
  10491. CLASS="userinput"
  10492. >msdev bindlib.dsp /MAKE "bindlib - Win32 Release"</KBD
  10493. >
  10494.            </P
  10495. ></LI
  10496. ></UL
  10497. ></LI
  10498. ></UL
  10499. >
  10500.        At this point, you should have a usable <TT
  10501. CLASS="filename"
  10502. >resolv.lib</TT
  10503. >
  10504.        in either your <TT
  10505. CLASS="filename"
  10506. >c:\work\bindlib_w32\Debug</TT
  10507. > or
  10508.        <TT
  10509. CLASS="filename"
  10510. >Release</TT
  10511. > subdirectories. Copy this file into your
  10512.        <TT
  10513. CLASS="filename"
  10514. >c:\work\win32build\lib</TT
  10515. > directory over the file
  10516.        by the same name found in there.
  10517.       </P
  10518. ></DIV
  10519. ><DIV
  10520. CLASS="sect2"
  10521. ><HR><H3
  10522. CLASS="sect2"
  10523. ><A
  10524. NAME="install.windows.building.compile"
  10525. >Compiling</A
  10526. ></H3
  10527. ><P
  10528. >       The best way to get started is to build the CGI version.
  10529.      </P
  10530. ><P
  10531. ></P
  10532. ><UL
  10533. ><LI
  10534. ><P
  10535. >         For GUI users, launch VC++, and then select File => Open
  10536.         Workspace and select <TT
  10537. CLASS="filename"
  10538. >c:\work\php-4.x.x\win32\php4ts.dsw
  10539.         </TT
  10540. >. Then select Build=>Set Active
  10541.         Configuration and select the desired configuration, either
  10542.         <VAR
  10543. CLASS="literal"
  10544. >php4ts - Win32 Debug_TS</VAR
  10545. > or
  10546.         <VAR
  10547. CLASS="literal"
  10548. >php4ts - Win32 Release_TS</VAR
  10549. >. Finally select
  10550.         Build=>Rebuild All.
  10551.        </P
  10552. ></LI
  10553. ><LI
  10554. ><P
  10555. >         For command line users, make sure that you either have
  10556.         the C++ environment variables registered, or have run
  10557.         <B
  10558. CLASS="command"
  10559. >vcvars.bat</B
  10560. >, and then execute one of the
  10561.         following commands from the <TT
  10562. CLASS="filename"
  10563. >c:\work\php-4.x.x\win32
  10564.         </TT
  10565. > directory:
  10566.        </P
  10567. ><P
  10568. ></P
  10569. ><UL
  10570. ><LI
  10571. ><P
  10572. >           <KBD
  10573. CLASS="userinput"
  10574. >msdev php4ts.dsp /MAKE "php4ts - Win32 Debug_TS"</KBD
  10575. >
  10576.          </P
  10577. ></LI
  10578. ><LI
  10579. ><P
  10580. >           <KBD
  10581. CLASS="userinput"
  10582. >msdev php4ts.dsp /MAKE "php4ts - Win32 Release_TS"</KBD
  10583. >
  10584.          </P
  10585. ></LI
  10586. ><LI
  10587. ><P
  10588. >           At this point, you should have a usable <TT
  10589. CLASS="filename"
  10590. >php.exe</TT
  10591. >
  10592.           in either your <TT
  10593. CLASS="filename"
  10594. >c:\work\php-4.x.x.\Debug_TS</TT
  10595. > or
  10596.           <TT
  10597. CLASS="filename"
  10598. >Release_TS</TT
  10599. > subdirectories.
  10600.          </P
  10601. ></LI
  10602. ></UL
  10603. ></LI
  10604. ></UL
  10605. ><P
  10606. >       It is possible to do minor customization to the build process by editing
  10607.       the <TT
  10608. CLASS="filename"
  10609. >main/config.win32.h</TT
  10610. > file. For example you can
  10611.       change the default location of <TT
  10612. CLASS="filename"
  10613. >php.ini</TT
  10614. >, the built-in extensions, and the
  10615.       default location for your extensions.
  10616.      </P
  10617. ><P
  10618. >       Next you may want to build the CLI version which is designed to use
  10619.       <A
  10620. HREF="#features.commandline"
  10621. >PHP from the command line</A
  10622. >.
  10623.       The steps are the same as for building the CGI version, except you have
  10624.       to select the <VAR
  10625. CLASS="literal"
  10626. >php4ts_cli - Win32 Debug_TS</VAR
  10627. > or
  10628.       <VAR
  10629. CLASS="literal"
  10630. >php4ts_cli - Win32 Release_TS</VAR
  10631. > project file. After a
  10632.       successful compiling run you will find the <TT
  10633. CLASS="filename"
  10634. >php.exe</TT
  10635. >
  10636.       in either the directory <TT
  10637. CLASS="filename"
  10638. >Release_TS\cli\</TT
  10639. > or
  10640.       <TT
  10641. CLASS="filename"
  10642. >Debug_TS\cli\</TT
  10643. >.
  10644.      </P
  10645. ><DIV
  10646. CLASS="note"
  10647. ><BLOCKQUOTE
  10648. CLASS="note"
  10649. ><P
  10650. ><B
  10651. >Note: </B
  10652. >
  10653.        If you want to use PEAR and the comfortable command line installer,
  10654.        the CLI-SAPI is mandatory. For more information about PEAR and the
  10655.        installer read the documentation at the <A
  10656. HREF="http://pear.php.net/manual/"
  10657. TARGET="_top"
  10658. >        PEAR</A
  10659. > website.
  10660.       </P
  10661. ></BLOCKQUOTE
  10662. ></DIV
  10663. ><P
  10664. >       In order to build the SAPI module (<TT
  10665. CLASS="filename"
  10666. >php4isapi.dll</TT
  10667. >)
  10668.       for integrating PHP with Microsoft IIS, set your active configuration to
  10669.       <TT
  10670. CLASS="filename"
  10671. >php4isapi-whatever-config</TT
  10672. > and build the desired dll.
  10673.      </P
  10674. ></DIV
  10675. ></DIV
  10676. ><DIV
  10677. CLASS="sect1"
  10678. ><HR><H2
  10679. CLASS="sect1"
  10680. ><A
  10681. NAME="install.windows.extensions"
  10682. >Installation of extensions on Windows</A
  10683. ></H2
  10684. ><P
  10685. >      After installing PHP and a webserver on Windows, you will probably want to
  10686.      install some extensions for added functionality. You can choose which
  10687.      extensions you would like to load when PHP starts by modifying your
  10688.      <TT
  10689. CLASS="filename"
  10690. >php.ini</TT
  10691. >. You can also load a module dynamically in your script using
  10692.      <A
  10693. HREF="#function.dl"
  10694. ><B
  10695. CLASS="function"
  10696. >dl()</B
  10697. ></A
  10698. >.
  10699.     </P
  10700. ><P
  10701. >      The DLLs for PHP extensions are prefixed with <VAR
  10702. CLASS="literal"
  10703. >php_</VAR
  10704. >.
  10705.     </P
  10706. ><DIV
  10707. CLASS="note"
  10708. ><BLOCKQUOTE
  10709. CLASS="note"
  10710. ><P
  10711. ><B
  10712. >Note: </B
  10713. >
  10714.       In PHP 4.3.1 BCMath, Calendar, COM, Ctype, FTP, MySQL, ODBC, Overload,
  10715.       PCRE, Session, Tokenizer, WDDX, XML and Zlib support is
  10716.       <SPAN
  10717. CLASS="emphasis"
  10718. ><I
  10719. CLASS="emphasis"
  10720. >built in</I
  10721. ></SPAN
  10722. >. You don't need to load any additional
  10723.       extensions in order to use these functions. See your distributions
  10724.       <TT
  10725. CLASS="filename"
  10726. >README.txt</TT
  10727. > or <TT
  10728. CLASS="filename"
  10729. >install.txt</TT
  10730. >
  10731.       or <A
  10732. HREF="#install.windows.extensions.overview"
  10733. >this table</A
  10734. >
  10735.       for a list of built in modules.
  10736.      </P
  10737. ></BLOCKQUOTE
  10738. ></DIV
  10739. ><P
  10740. >      The default location PHP searches for extensions is
  10741.      <TT
  10742. CLASS="filename"
  10743. >c:\php4\extensions</TT
  10744. > in PHP 4 and
  10745.      <TT
  10746. CLASS="filename"
  10747. >c:\php5</TT
  10748. > in PHP 5. To change this
  10749.      setting to reflect your setup of PHP edit your <TT
  10750. CLASS="filename"
  10751. >php.ini</TT
  10752. > file:
  10753.      <P
  10754. ></P
  10755. ><UL
  10756. ><LI
  10757. ><P
  10758. >         You will need to change the
  10759.         <A
  10760. HREF="#ini.extension-dir"
  10761. >extension_dir</A
  10762. > setting to
  10763.         point to the directory where your extensions lives, or where you have
  10764.         placed your <TT
  10765. CLASS="filename"
  10766. >php_*.dll</TT
  10767. > files. Please do not
  10768.         forget the last backslash. For example:
  10769.         <DIV
  10770. CLASS="informalexample"
  10771. ><P
  10772. ></P
  10773. ><A
  10774. NAME="AEN1858"
  10775. ></A
  10776. ><TABLE
  10777. BORDER="0"
  10778. BGCOLOR="#E0E0E0"
  10779. CELLPADDING="5"
  10780. ><TR
  10781. ><TD
  10782. ><PRE
  10783. CLASS="ini"
  10784. >extension_dir = c:/php/extensions/</PRE
  10785. ></TD
  10786. ></TR
  10787. ></TABLE
  10788. ><P
  10789. ></P
  10790. ></DIV
  10791. >
  10792.        </P
  10793. ></LI
  10794. ><LI
  10795. ><P
  10796. >        Enable the extension(s) in <TT
  10797. CLASS="filename"
  10798. >php.ini</TT
  10799. > you want to use by uncommenting the
  10800.        <VAR
  10801. CLASS="literal"
  10802. >extension=php_*.dll</VAR
  10803. > lines in <TT
  10804. CLASS="filename"
  10805. >php.ini</TT
  10806. >. This is done
  10807.        by deleting the leading ; form the extension you want to load. 
  10808.        <TABLE
  10809. WIDTH="100%"
  10810. BORDER="0"
  10811. CELLPADDING="0"
  10812. CELLSPACING="0"
  10813. CLASS="EXAMPLE"
  10814. ><TR
  10815. ><TD
  10816. ><DIV
  10817. CLASS="example"
  10818. ><A
  10819. NAME="AEN1865"
  10820. ></A
  10821. ><P
  10822. ><B
  10823. >Example 6-8. Enable <A
  10824. HREF="#ref.bzip2"
  10825. >Bzip2</A
  10826. > extension for PHP-Windows</B
  10827. ></P
  10828. ><TABLE
  10829. BORDER="0"
  10830. BGCOLOR="#E0E0E0"
  10831. CELLPADDING="5"
  10832. ><TR
  10833. ><TD
  10834. ><PRE
  10835. CLASS="ini"
  10836. >// change the following line from ...
  10837. ;extension=php_bz2.dll
  10838.  
  10839. // ... to
  10840. extension=php_bz2.dll</PRE
  10841. ></TD
  10842. ></TR
  10843. ></TABLE
  10844. ></DIV
  10845. ></TD
  10846. ></TR
  10847. ></TABLE
  10848. >      
  10849.        </P
  10850. ></LI
  10851. ><LI
  10852. ><P
  10853. >         Some of the extensions need extra DLLs to work. Couple of them can be
  10854.         found in the distribution package, in the
  10855.         <TT
  10856. CLASS="filename"
  10857. >C:\php\dlls\</TT
  10858. > folder in PHP 4 or
  10859.         in the main folder in PHP 5, but some, for example Oracle
  10860.         (<TT
  10861. CLASS="filename"
  10862. >php_oci8.dll</TT
  10863. >) require DLLs which are not bundled
  10864.         with the distribution package. If you are installing PHP 4, copy the
  10865.         bundled DLLs from <TT
  10866. CLASS="filename"
  10867. >C:\php\dlls</TT
  10868. >
  10869.         folder to the main <TT
  10870. CLASS="filename"
  10871. >C:\php</TT
  10872. >
  10873.         folder. Don't forget to include <TT
  10874. CLASS="filename"
  10875. >C:\php</TT
  10876. > in the system
  10877.         <VAR
  10878. CLASS="varname"
  10879. >PATH</VAR
  10880. > (this process is explained in a separate <A
  10881. HREF="#faq.installation.addtopath"
  10882. >FAQ entry</A
  10883. >).
  10884.        </P
  10885. ></LI
  10886. ></UL
  10887. >
  10888.    <DIV
  10889. CLASS="note"
  10890. ><BLOCKQUOTE
  10891. CLASS="note"
  10892. ><P
  10893. ><B
  10894. >Note: </B
  10895. >
  10896.      If you are running a server module version of PHP
  10897.      remember to restart your webserver to reflect your changes to <TT
  10898. CLASS="filename"
  10899. >php.ini</TT
  10900. >.
  10901.     </P
  10902. ></BLOCKQUOTE
  10903. ></DIV
  10904. >
  10905.    </P
  10906. ><P
  10907. >     The following table describes some of the extensions available and required
  10908.     additional dlls.   
  10909.     <DIV
  10910. CLASS="table"
  10911. ><A
  10912. NAME="install.windows.extensions.overview"
  10913. ></A
  10914. ><P
  10915. ><B
  10916. >Table 6-1. PHP Extensions</B
  10917. ></P
  10918. ><TABLE
  10919. BORDER="1"
  10920. CLASS="CALSTABLE"
  10921. ><COL><COL><COL><THEAD
  10922. ><TR
  10923. ><TH
  10924. >Extension</TH
  10925. ><TH
  10926. >Description</TH
  10927. ><TH
  10928. >Notes</TH
  10929. ></TR
  10930. ></THEAD
  10931. ><TBODY
  10932. ><TR
  10933. ><TD
  10934. >php_bz2.dll</TD
  10935. ><TD
  10936. ><A
  10937. HREF="#ref.bzip2"
  10938. >bzip2</A
  10939. > compression functions</TD
  10940. ><TD
  10941. >None</TD
  10942. ></TR
  10943. ><TR
  10944. ><TD
  10945. >php_calendar.dll</TD
  10946. ><TD
  10947. ><A
  10948. HREF="#ref.calendar"
  10949. >Calendar</A
  10950. > conversion functions</TD
  10951. ><TD
  10952. >Built in since PHP 4.0.3</TD
  10953. ></TR
  10954. ><TR
  10955. ><TD
  10956. >php_cpdf.dll</TD
  10957. ><TD
  10958. ><A
  10959. HREF="#ref.cpdf"
  10960. >ClibPDF</A
  10961. > functions</TD
  10962. ><TD
  10963. >None</TD
  10964. ></TR
  10965. ><TR
  10966. ><TD
  10967. >php_crack.dll</TD
  10968. ><TD
  10969. ><A
  10970. HREF="#ref.crack"
  10971. >Crack</A
  10972. > functions</TD
  10973. ><TD
  10974. >None</TD
  10975. ></TR
  10976. ><TR
  10977. ><TD
  10978. >php_ctype.dll</TD
  10979. ><TD
  10980. ><A
  10981. HREF="#ref.ctype"
  10982. >ctype</A
  10983. > family functions</TD
  10984. ><TD
  10985. >Built in since PHP 4.3.0</TD
  10986. ></TR
  10987. ><TR
  10988. ><TD
  10989. >php_curl.dll</TD
  10990. ><TD
  10991. ><A
  10992. HREF="#ref.curl"
  10993. >CURL</A
  10994. >, Client URL library functions</TD
  10995. ><TD
  10996. >Requires: <TT
  10997. CLASS="filename"
  10998. >libeay32.dll</TT
  10999. >, 
  11000.    <TT
  11001. CLASS="filename"
  11002. >ssleay32.dll</TT
  11003. > (bundled)</TD
  11004. ></TR
  11005. ><TR
  11006. ><TD
  11007. >php_cybercash.dll</TD
  11008. ><TD
  11009. ><A
  11010. HREF="#ref.cybercash"
  11011. >Cybercash</A
  11012. > payment functions</TD
  11013. ><TD
  11014. >PHP <= 4.2.0</TD
  11015. ></TR
  11016. ><TR
  11017. ><TD
  11018. >php_db.dll</TD
  11019. ><TD
  11020. ><A
  11021. HREF="#ref.dbm"
  11022. >DBM</A
  11023. > functions</TD
  11024. ><TD
  11025. >Deprecated. Use DBA instead (<TT
  11026. CLASS="filename"
  11027. >php_dba.dll</TT
  11028. >)</TD
  11029. ></TR
  11030. ><TR
  11031. ><TD
  11032. >php_dba.dll</TD
  11033. ><TD
  11034. ><A
  11035. HREF="#ref.dba"
  11036. >DBA</A
  11037. >: DataBase (dbm-style) 
  11038.    Abstraction layer functions</TD
  11039. ><TD
  11040. >None</TD
  11041. ></TR
  11042. ><TR
  11043. ><TD
  11044. >php_dbase.dll</TD
  11045. ><TD
  11046. ><A
  11047. HREF="#ref.dbase"
  11048. >dBase</A
  11049. > functions</TD
  11050. ><TD
  11051. >None</TD
  11052. ></TR
  11053. ><TR
  11054. ><TD
  11055. >php_dbx.dll</TD
  11056. ><TD
  11057. ><A
  11058. HREF="#ref.dbx"
  11059. >dbx</A
  11060. > functions</TD
  11061. ><TD
  11062. > </TD
  11063. ></TR
  11064. ><TR
  11065. ><TD
  11066. >php_domxml.dll</TD
  11067. ><TD
  11068. ><A
  11069. HREF="#ref.domxml"
  11070. >DOM XML</A
  11071. > functions</TD
  11072. ><TD
  11073. >     PHP <= 4.2.0 requires: <TT
  11074. CLASS="filename"
  11075. >libxml2.dll</TT
  11076. > (bundled)
  11077.     PHP >= 4.3.0 requires: <TT
  11078. CLASS="filename"
  11079. >iconv.dll</TT
  11080. > (bundled)
  11081.    </TD
  11082. ></TR
  11083. ><TR
  11084. ><TD
  11085. >php_dotnet.dll</TD
  11086. ><TD
  11087. ><A
  11088. HREF="#ref.dotnet"
  11089. >.NET</A
  11090. > functions</TD
  11091. ><TD
  11092. >PHP <= 4.1.1</TD
  11093. ></TR
  11094. ><TR
  11095. ><TD
  11096. >php_exif.dll</TD
  11097. ><TD
  11098. ><A
  11099. HREF="#ref.exif"
  11100. >EXIF</A
  11101. > functions</TD
  11102. ><TD
  11103. >     <A
  11104. HREF="#ref.mbstring"
  11105. >php_mbstring.dll</A
  11106. >. And, 
  11107.     <TT
  11108. CLASS="filename"
  11109. >php_exif.dll</TT
  11110. > must be loaded <VAR
  11111. CLASS="literal"
  11112. >after</VAR
  11113.     <TT
  11114. CLASS="filename"
  11115. >php_mbstring.dll</TT
  11116. > in <TT
  11117. CLASS="filename"
  11118. >php.ini</TT
  11119. >.
  11120.    </TD
  11121. ></TR
  11122. ><TR
  11123. ><TD
  11124. >php_fbsql.dll</TD
  11125. ><TD
  11126. ><A
  11127. HREF="#ref.fbsql"
  11128. >FrontBase</A
  11129. > functions</TD
  11130. ><TD
  11131. >PHP <= 4.2.0</TD
  11132. ></TR
  11133. ><TR
  11134. ><TD
  11135. >php_fdf.dll</TD
  11136. ><TD
  11137. ><A
  11138. HREF="#ref.fdf"
  11139. >FDF</A
  11140. >: Forms Data Format functions.</TD
  11141. ><TD
  11142. >Requires: <TT
  11143. CLASS="filename"
  11144. >fdftk.dll</TT
  11145. > (bundled)</TD
  11146. ></TR
  11147. ><TR
  11148. ><TD
  11149. >php_filepro.dll</TD
  11150. ><TD
  11151. ><A
  11152. HREF="#ref.filepro"
  11153. >filePro</A
  11154. > functions</TD
  11155. ><TD
  11156. >Read-only access</TD
  11157. ></TR
  11158. ><TR
  11159. ><TD
  11160. >php_ftp.dll</TD
  11161. ><TD
  11162. ><A
  11163. HREF="#ref.ftp"
  11164. >FTP</A
  11165. > functions</TD
  11166. ><TD
  11167. >Built-in since PHP 4.0.3</TD
  11168. ></TR
  11169. ><TR
  11170. ><TD
  11171. >php_gd.dll</TD
  11172. ><TD
  11173. ><A
  11174. HREF="#ref.image"
  11175. >GD</A
  11176. > library image functions</TD
  11177. ><TD
  11178. >     Removed in PHP 4.3.2.  Also note that truecolor functions are not
  11179.     available in GD1, instead, use <TT
  11180. CLASS="filename"
  11181. >php_gd2.dll</TT
  11182. >.
  11183.    </TD
  11184. ></TR
  11185. ><TR
  11186. ><TD
  11187. >php_gd2.dll</TD
  11188. ><TD
  11189. ><A
  11190. HREF="#ref.image"
  11191. >GD</A
  11192. > library image functions</TD
  11193. ><TD
  11194. >GD2</TD
  11195. ></TR
  11196. ><TR
  11197. ><TD
  11198. >php_gettext.dll</TD
  11199. ><TD
  11200. ><A
  11201. HREF="#ref.gettext"
  11202. >Gettext</A
  11203. > functions</TD
  11204. ><TD
  11205. >     PHP <= 4.2.0 requires <TT
  11206. CLASS="filename"
  11207. >gnu_gettext.dll</TT
  11208. > (bundled), 
  11209.     PHP >= 4.2.3 requires <TT
  11210. CLASS="filename"
  11211. >libintl-1.dll</TT
  11212. >,
  11213.     <TT
  11214. CLASS="filename"
  11215. >iconv.dll</TT
  11216. > (bundled).
  11217.    </TD
  11218. ></TR
  11219. ><TR
  11220. ><TD
  11221. >php_hyperwave.dll</TD
  11222. ><TD
  11223. ><A
  11224. HREF="#ref.hw"
  11225. >HyperWave</A
  11226. > functions</TD
  11227. ><TD
  11228. >None</TD
  11229. ></TR
  11230. ><TR
  11231. ><TD
  11232. >php_iconv.dll</TD
  11233. ><TD
  11234. ><A
  11235. HREF="#ref.iconv"
  11236. >ICONV</A
  11237. > characterset conversion</TD
  11238. ><TD
  11239. >Requires: <TT
  11240. CLASS="filename"
  11241. >iconv-1.3.dll</TT
  11242. > (bundled), 
  11243.    PHP >=4.2.1 <TT
  11244. CLASS="filename"
  11245. >iconv.dll</TT
  11246. ></TD
  11247. ></TR
  11248. ><TR
  11249. ><TD
  11250. >php_ifx.dll</TD
  11251. ><TD
  11252. ><A
  11253. HREF="#ref.ifx"
  11254. >Informix</A
  11255. > functions</TD
  11256. ><TD
  11257. >Requires: Informix libraries</TD
  11258. ></TR
  11259. ><TR
  11260. ><TD
  11261. >php_iisfunc.dll</TD
  11262. ><TD
  11263. >IIS management functions</TD
  11264. ><TD
  11265. >None</TD
  11266. ></TR
  11267. ><TR
  11268. ><TD
  11269. >php_imap.dll</TD
  11270. ><TD
  11271. ><A
  11272. HREF="#ref.imap"
  11273. >IMAP</A
  11274. > POP3 and NNTP functions</TD
  11275. ><TD
  11276. >None</TD
  11277. ></TR
  11278. ><TR
  11279. ><TD
  11280. >php_ingres.dll</TD
  11281. ><TD
  11282. ><A
  11283. HREF="#ref.ingres"
  11284. >Ingres II</A
  11285. > functions</TD
  11286. ><TD
  11287. >Requires: Ingres II libraries</TD
  11288. ></TR
  11289. ><TR
  11290. ><TD
  11291. >php_interbase.dll</TD
  11292. ><TD
  11293. ><A
  11294. HREF="#ref.ibase"
  11295. >InterBase</A
  11296. > functions</TD
  11297. ><TD
  11298. >Requires: <TT
  11299. CLASS="filename"
  11300. >gds32.dll</TT
  11301. > (bundled)</TD
  11302. ></TR
  11303. ><TR
  11304. ><TD
  11305. >php_java.dll</TD
  11306. ><TD
  11307. ><A
  11308. HREF="#ref.java"
  11309. >Java</A
  11310. > functions</TD
  11311. ><TD
  11312. >PHP <= 4.0.6 requires: <TT
  11313. CLASS="filename"
  11314. >jvm.dll</TT
  11315. > (bundled)</TD
  11316. ></TR
  11317. ><TR
  11318. ><TD
  11319. >php_ldap.dll</TD
  11320. ><TD
  11321. ><A
  11322. HREF="#ref.ldap"
  11323. >LDAP</A
  11324. > functions</TD
  11325. ><TD
  11326. >     PHP <= 4.2.0 requires <TT
  11327. CLASS="filename"
  11328. >libsasl.dll</TT
  11329. > (bundled), 
  11330.     PHP >= 4.3.0 requires <TT
  11331. CLASS="filename"
  11332. >libeay32.dll</TT
  11333. >, 
  11334.     <TT
  11335. CLASS="filename"
  11336. >ssleay32.dll</TT
  11337. > (bundled)
  11338.    </TD
  11339. ></TR
  11340. ><TR
  11341. ><TD
  11342. >php_mbstring.dll</TD
  11343. ><TD
  11344. ><A
  11345. HREF="#ref.mbstring"
  11346. >Multi-Byte String</A
  11347. > functions</TD
  11348. ><TD
  11349. >None</TD
  11350. ></TR
  11351. ><TR
  11352. ><TD
  11353. >php_mcrypt.dll</TD
  11354. ><TD
  11355. ><A
  11356. HREF="#ref.mcrypt"
  11357. >Mcrypt Encryption</A
  11358. > functions</TD
  11359. ><TD
  11360. >Requires: <TT
  11361. CLASS="filename"
  11362. >libmcrypt.dll</TT
  11363. ></TD
  11364. ></TR
  11365. ><TR
  11366. ><TD
  11367. >php_mhash.dll</TD
  11368. ><TD
  11369. ><A
  11370. HREF="#ref.mhash"
  11371. >Mhash</A
  11372. > functions</TD
  11373. ><TD
  11374. >PHP >= 4.3.0 requires: <TT
  11375. CLASS="filename"
  11376. >libmhash.dll</TT
  11377. > (bundled)</TD
  11378. ></TR
  11379. ><TR
  11380. ><TD
  11381. >php_mime_magic.dll</TD
  11382. ><TD
  11383. ><A
  11384. HREF="#ref.mime-magic"
  11385. >Mimetype</A
  11386. > functions</TD
  11387. ><TD
  11388. >Requires: <TT
  11389. CLASS="filename"
  11390. >magic.mime</TT
  11391. > (bundled)</TD
  11392. ></TR
  11393. ><TR
  11394. ><TD
  11395. >php_ming.dll</TD
  11396. ><TD
  11397. ><A
  11398. HREF="#ref.ming"
  11399. >Ming</A
  11400. > functions for Flash</TD
  11401. ><TD
  11402. >None</TD
  11403. ></TR
  11404. ><TR
  11405. ><TD
  11406. >php_msql.dll</TD
  11407. ><TD
  11408. ><A
  11409. HREF="#ref.msql"
  11410. >mSQL</A
  11411. > functions</TD
  11412. ><TD
  11413. >Requires: <TT
  11414. CLASS="filename"
  11415. >msql.dll</TT
  11416. > (bundled)</TD
  11417. ></TR
  11418. ><TR
  11419. ><TD
  11420. >php_mssql.dll</TD
  11421. ><TD
  11422. ><A
  11423. HREF="#ref.mssql"
  11424. >MSSQL</A
  11425. > functions</TD
  11426. ><TD
  11427. >Requires: <TT
  11428. CLASS="filename"
  11429. >ntwdblib.dll</TT
  11430. > (bundled)</TD
  11431. ></TR
  11432. ><TR
  11433. ><TD
  11434. >php_mysql.dll</TD
  11435. ><TD
  11436. ><A
  11437. HREF="#ref.mysql"
  11438. >MySQL</A
  11439. > functions</TD
  11440. ><TD
  11441. >PHP >= 5.0.0, requires <TT
  11442. CLASS="filename"
  11443. >libmysql.dll</TT
  11444. >
  11445.     (bundled)</TD
  11446. ></TR
  11447. ><TR
  11448. ><TD
  11449. >php_mysqli.dll</TD
  11450. ><TD
  11451. ><A
  11452. HREF="#ref.mysqli"
  11453. >MySQLi</A
  11454. > functions</TD
  11455. ><TD
  11456. >PHP >= 5.0.0, requires <TT
  11457. CLASS="filename"
  11458. >libmysql.dll</TT
  11459. >
  11460.     (<TT
  11461. CLASS="filename"
  11462. >libmysqli.dll</TT
  11463. > in PHP <= 5.0.2) (bundled)</TD
  11464. ></TR
  11465. ><TR
  11466. ><TD
  11467. >php_oci8.dll</TD
  11468. ><TD
  11469. ><A
  11470. HREF="#ref.oci8"
  11471. >Oracle 8</A
  11472. > functions</TD
  11473. ><TD
  11474. >Requires: Oracle 8.1+ client libraries</TD
  11475. ></TR
  11476. ><TR
  11477. ><TD
  11478. >php_openssl.dll</TD
  11479. ><TD
  11480. ><A
  11481. HREF="#ref.openssl"
  11482. >OpenSSL</A
  11483. > functions</TD
  11484. ><TD
  11485. >Requires: <TT
  11486. CLASS="filename"
  11487. >libeay32.dll</TT
  11488. > (bundled)</TD
  11489. ></TR
  11490. ><TR
  11491. ><TD
  11492. >php_oracle.dll</TD
  11493. ><TD
  11494. ><A
  11495. HREF="#ref.oracle"
  11496. >Oracle</A
  11497. > functions</TD
  11498. ><TD
  11499. >Requires: Oracle 7 client libraries</TD
  11500. ></TR
  11501. ><TR
  11502. ><TD
  11503. >php_overload.dll</TD
  11504. ><TD
  11505. ><A
  11506. HREF="#ref.overload"
  11507. >Object overloading</A
  11508. > functions</TD
  11509. ><TD
  11510. >Built in since PHP 4.3.0</TD
  11511. ></TR
  11512. ><TR
  11513. ><TD
  11514. >php_pdf.dll</TD
  11515. ><TD
  11516. ><A
  11517. HREF="#ref.pdf"
  11518. >PDF</A
  11519. > functions</TD
  11520. ><TD
  11521. >None</TD
  11522. ></TR
  11523. ><TR
  11524. ><TD
  11525. >php_pgsql.dll</TD
  11526. ><TD
  11527. ><A
  11528. HREF="#ref.pgsql"
  11529. >PostgreSQL</A
  11530. > functions</TD
  11531. ><TD
  11532. >None</TD
  11533. ></TR
  11534. ><TR
  11535. ><TD
  11536. >php_printer.dll</TD
  11537. ><TD
  11538. ><A
  11539. HREF="#ref.printer"
  11540. >Printer</A
  11541. > functions</TD
  11542. ><TD
  11543. >None</TD
  11544. ></TR
  11545. ><TR
  11546. ><TD
  11547. >php_shmop.dll</TD
  11548. ><TD
  11549. ><A
  11550. HREF="#ref.shmop"
  11551. >Shared Memory</A
  11552. > functions</TD
  11553. ><TD
  11554. >None</TD
  11555. ></TR
  11556. ><TR
  11557. ><TD
  11558. >php_snmp.dll</TD
  11559. ><TD
  11560. ><A
  11561. HREF="#ref.snmp"
  11562. >SNMP</A
  11563. > get and walk functions</TD
  11564. ><TD
  11565. >NT only!</TD
  11566. ></TR
  11567. ><TR
  11568. ><TD
  11569. >php_soap.dll</TD
  11570. ><TD
  11571. ><A
  11572. HREF="#ref.soap"
  11573. >SOAP</A
  11574. > functions</TD
  11575. ><TD
  11576. >PHP >= 5.0.0</TD
  11577. ></TR
  11578. ><TR
  11579. ><TD
  11580. >php_sockets.dll</TD
  11581. ><TD
  11582. ><A
  11583. HREF="#ref.sockets"
  11584. >Socket</A
  11585. > functions</TD
  11586. ><TD
  11587. >None</TD
  11588. ></TR
  11589. ><TR
  11590. ><TD
  11591. >php_sybase_ct.dll</TD
  11592. ><TD
  11593. ><A
  11594. HREF="#ref.sybase"
  11595. >Sybase</A
  11596. > functions</TD
  11597. ><TD
  11598. >Requires: Sybase client libraries</TD
  11599. ></TR
  11600. ><TR
  11601. ><TD
  11602. >php_tidy.dll</TD
  11603. ><TD
  11604. ><A
  11605. HREF="#ref.tidy"
  11606. >Tidy</A
  11607. > functions</TD
  11608. ><TD
  11609. >PHP >= 5.0.0</TD
  11610. ></TR
  11611. ><TR
  11612. ><TD
  11613. >php_tokenizer.dll</TD
  11614. ><TD
  11615. ><A
  11616. HREF="#ref.tokenizer"
  11617. >Tokenizer</A
  11618. > functions</TD
  11619. ><TD
  11620. >Built in since PHP 4.3.0</TD
  11621. ></TR
  11622. ><TR
  11623. ><TD
  11624. >php_w32api.dll</TD
  11625. ><TD
  11626. ><A
  11627. HREF="#ref.w32api"
  11628. >W32api</A
  11629. > functions</TD
  11630. ><TD
  11631. >None</TD
  11632. ></TR
  11633. ><TR
  11634. ><TD
  11635. >php_xmlrpc.dll</TD
  11636. ><TD
  11637. ><A
  11638. HREF="#ref.xmlrpc"
  11639. >XML-RPC</A
  11640. > functions</TD
  11641. ><TD
  11642. >PHP >= 4.2.1 requires: <TT
  11643. CLASS="filename"
  11644. >iconv.dll</TT
  11645. > (bundled)</TD
  11646. ></TR
  11647. ><TR
  11648. ><TD
  11649. >php_xslt.dll</TD
  11650. ><TD
  11651. ><A
  11652. HREF="#ref.xslt"
  11653. >XSLT</A
  11654. > functions</TD
  11655. ><TD
  11656. >     PHP <= 4.2.0 requires <TT
  11657. CLASS="filename"
  11658. >sablot.dll</TT
  11659. >,
  11660.     <TT
  11661. CLASS="filename"
  11662. >expat.dll</TT
  11663. > (bundled). PHP >= 4.2.1 requires
  11664.     <TT
  11665. CLASS="filename"
  11666. >sablot.dll</TT
  11667. >, <TT
  11668. CLASS="filename"
  11669. >expat.dll</TT
  11670. >,
  11671.     <TT
  11672. CLASS="filename"
  11673. >iconv.dll</TT
  11674. > (bundled).
  11675.    </TD
  11676. ></TR
  11677. ><TR
  11678. ><TD
  11679. >php_yaz.dll</TD
  11680. ><TD
  11681. ><A
  11682. HREF="#ref.yaz"
  11683. >YAZ</A
  11684. > functions</TD
  11685. ><TD
  11686. >Requires: <TT
  11687. CLASS="filename"
  11688. >yaz.dll</TT
  11689. > (bundled)</TD
  11690. ></TR
  11691. ><TR
  11692. ><TD
  11693. >php_zip.dll</TD
  11694. ><TD
  11695. ><A
  11696. HREF="#ref.zip"
  11697. >Zip File</A
  11698. > functions</TD
  11699. ><TD
  11700. >Read only access</TD
  11701. ></TR
  11702. ><TR
  11703. ><TD
  11704. >php_zlib.dll</TD
  11705. ><TD
  11706. ><A
  11707. HREF="#ref.zlib"
  11708. >ZLib</A
  11709. > compression functions</TD
  11710. ><TD
  11711. >Built in since PHP 4.3.0</TD
  11712. ></TR
  11713. ></TBODY
  11714. ></TABLE
  11715. ></DIV
  11716. >
  11717.     </P
  11718. ></DIV
  11719. ></DIV
  11720. ><DIV
  11721. CLASS="chapter"
  11722. ><HR><H1
  11723. ><A
  11724. NAME="install.pecl"
  11725. >Chapter 7. Installation of PECL extensions</A
  11726. ></H1
  11727. ><DIV
  11728. CLASS="sect1"
  11729. ><H2
  11730. CLASS="sect1"
  11731. ><A
  11732. NAME="install.pecl.intro"
  11733. >Introduction to PECL Installations</A
  11734. ></H2
  11735. ><P
  11736. >    PHP extensions may be installed in a variety of ways. 
  11737.    <A
  11738. HREF="http://pecl.php.net"
  11739. TARGET="_top"
  11740. >PECL</A
  11741. > is a repository of PHP extensions 
  11742.    living within the <A
  11743. HREF="http://pear.php.net/"
  11744. TARGET="_top"
  11745. >PEAR</A
  11746. > structure, and 
  11747.    the following demonstrates how to install these extensions.
  11748.   </P
  11749. ><P
  11750. >    These instructions assume <VAR
  11751. CLASS="literal"
  11752. >/your/phpsrcdir/</VAR
  11753. > is the path
  11754.    to the PHP source, and <VAR
  11755. CLASS="literal"
  11756. >extname</VAR
  11757. > is the name of the
  11758.    PECL extension.  Adjust accordingly.  These instructions also assume a 
  11759.    familiarity with the <A
  11760. HREF="http://pear.php.net/manual/en/installation.cli.php"
  11761. TARGET="_top"
  11762. >pear command</A
  11763. >.
  11764.   </P
  11765. ><P
  11766. >    Shared extensions may be installed by including them inside of <TT
  11767. CLASS="filename"
  11768. >php.ini</TT
  11769. >
  11770.    using the <A
  11771. HREF="#ini.extension"
  11772. >extension</A
  11773. > PHP directive. See
  11774.    also the <A
  11775. HREF="#ini.extension-dir"
  11776. >extensions_dir</A
  11777. >
  11778.    directive, and <A
  11779. HREF="#function.dl"
  11780. ><B
  11781. CLASS="function"
  11782. >dl()</B
  11783. ></A
  11784. >.  The installation methods described
  11785.    below do not automatically configure PHP to include these extensions, this 
  11786.    step must be done manually.
  11787.   </P
  11788. ><P
  11789. >    When building PHP modules, it's important to have the appropriate versions 
  11790.    of the required tools (autoconf, automake, libtool, etc.) See the 
  11791.    <A
  11792. HREF="http://www.php.net/anoncvs.php"
  11793. TARGET="_top"
  11794. >Anonymous CVS Instructions</A
  11795. > for 
  11796.    details on the required tools, and required versions.
  11797.   </P
  11798. ></DIV
  11799. ><DIV
  11800. CLASS="sect1"
  11801. ><HR><H2
  11802. CLASS="sect1"
  11803. ><A
  11804. NAME="install.pecl.downloads"
  11805. >Downloading PECL extensions</A
  11806. ></H2
  11807. ><P
  11808. >    There are several options for downloading PECL extensions, such as:
  11809.   </P
  11810. ><P
  11811. ></P
  11812. ><UL
  11813. ><LI
  11814. ><P
  11815. >      <A
  11816. HREF="http://pecl.php.net"
  11817. TARGET="_top"
  11818. >http://pecl.php.net</A
  11819. >
  11820.     </P
  11821. ><P
  11822. >      Listed here is information like the ChangeLog, release information, 
  11823.      requirements, revisions, etc.  Although not every PECL extension has a
  11824.      webpage, most do.
  11825.     </P
  11826. ></LI
  11827. ><LI
  11828. ><P
  11829. >      <VAR
  11830. CLASS="literal"
  11831. >pear download extname</VAR
  11832. >
  11833.     </P
  11834. ><P
  11835. >      The <A
  11836. HREF="http://pear.php.net/manual/en/installation.cli.php"
  11837. TARGET="_top"
  11838. >pear command</A
  11839. > may also be used 
  11840.      to download source files.  Specific revisions may also be specified.
  11841.     </P
  11842. ></LI
  11843. ><LI
  11844. ><P
  11845. >      <ACRONYM
  11846. CLASS="acronym"
  11847. >CVS</ACRONYM
  11848. >
  11849.     </P
  11850. ><P
  11851. >      All PECL files reside in <ACRONYM
  11852. CLASS="acronym"
  11853. >CVS</ACRONYM
  11854. >.  A web-based view may
  11855.      be seen at <A
  11856. HREF="http://cvs.php.net/pecl/"
  11857. TARGET="_top"
  11858. >http://cvs.php.net/pecl/</A
  11859. >.  
  11860.      To download straight from <ACRONYM
  11861. CLASS="acronym"
  11862. >CVS</ACRONYM
  11863. >, consider the following 
  11864.      where <SPAN
  11865. CLASS="emphasis"
  11866. ><I
  11867. CLASS="emphasis"
  11868. >phpfi</I
  11869. ></SPAN
  11870. > is the password for user 
  11871.      <SPAN
  11872. CLASS="emphasis"
  11873. ><I
  11874. CLASS="emphasis"
  11875. >cvsread</I
  11876. ></SPAN
  11877. >:
  11878.     </P
  11879. ><P
  11880. >      <TABLE
  11881. BORDER="0"
  11882. BGCOLOR="#E0E0E0"
  11883. CELLPADDING="5"
  11884. ><TR
  11885. ><TD
  11886. ><PRE
  11887. CLASS="screen"
  11888. >$ cvs -d:pserver:cvsread@cvs.php.net:/repository login 
  11889. $ cvs -d:pserver:cvsread@cvs.php.net:/repository co pecl/extname</PRE
  11890. ></TD
  11891. ></TR
  11892. ></TABLE
  11893. >
  11894.     </P
  11895. ></LI
  11896. ><LI
  11897. ><P
  11898. >      Windows downloads
  11899.     </P
  11900. ><P
  11901. >      Windows users may find compiled PECL binaries by downloading the
  11902.      <SPAN
  11903. CLASS="emphasis"
  11904. ><I
  11905. CLASS="emphasis"
  11906. >Collection of PECL modules</I
  11907. ></SPAN
  11908. > from the 
  11909.      <A
  11910. HREF="http://www.php.net/downloads.php"
  11911. TARGET="_top"
  11912. >PHP Downloads</A
  11913. > page, and by
  11914.      retrieving a <A
  11915. HREF="http://snaps.php.net/"
  11916. TARGET="_top"
  11917. >PECL Snapshot</A
  11918. >.  To
  11919.      compile PHP under Windows, read the 
  11920.      <A
  11921. HREF="http://cvs.php.net/co.php/php-src/README.WIN32-BUILD-SYSTEM"
  11922. TARGET="_top"
  11923. >Win32 Build README</A
  11924. >.
  11925.     </P
  11926. ></LI
  11927. ></UL
  11928. ></DIV
  11929. ><DIV
  11930. CLASS="sect1"
  11931. ><HR><H2
  11932. CLASS="sect1"
  11933. ><A
  11934. NAME="install.pecl.windows"
  11935. >PECL for Windows users</A
  11936. ></H2
  11937. ><P
  11938. >    Like with any other PHP extension <ACRONYM
  11939. CLASS="acronym"
  11940. >DLL</ACRONYM
  11941. >, to install move 
  11942.    the PECL extension DLLs into the <A
  11943. HREF="#ini.extension-dir"
  11944. >    extension_dir</A
  11945. > folder and include them within <TT
  11946. CLASS="filename"
  11947. >php.ini</TT
  11948. >.  For 
  11949.    example:
  11950.   </P
  11951. ><P
  11952. >    <TABLE
  11953. BORDER="0"
  11954. BGCOLOR="#E0E0E0"
  11955. CELLPADDING="5"
  11956. ><TR
  11957. ><TD
  11958. ><PRE
  11959. CLASS="screen"
  11960. >extension=php_extname.dll</PRE
  11961. ></TD
  11962. ></TR
  11963. ></TABLE
  11964. >
  11965.   </P
  11966. ><P
  11967. >    After doing this, restart the web server.
  11968.   </P
  11969. ></DIV
  11970. ><DIV
  11971. CLASS="sect1"
  11972. ><HR><H2
  11973. CLASS="sect1"
  11974. ><A
  11975. NAME="install.pecl.pear"
  11976. >Compiling shared PECL extensions with PEAR</A
  11977. ></H2
  11978. ><P
  11979. >    PEAR makes it easy to create shared PHP extensions. Using the 
  11980.    <A
  11981. HREF="http://pear.php.net/manual/en/installation.cli.php"
  11982. TARGET="_top"
  11983. >pear command</A
  11984. >, do the following:
  11985.   </P
  11986. ><P
  11987. >    <TABLE
  11988. BORDER="0"
  11989. BGCOLOR="#E0E0E0"
  11990. CELLPADDING="5"
  11991. ><TR
  11992. ><TD
  11993. ><PRE
  11994. CLASS="screen"
  11995. >$ pear install extname</PRE
  11996. ></TD
  11997. ></TR
  11998. ></TABLE
  11999. >
  12000.   </P
  12001. ><P
  12002. >    That will download the source for <SPAN
  12003. CLASS="emphasis"
  12004. ><I
  12005. CLASS="emphasis"
  12006. >extname</I
  12007. ></SPAN
  12008. >, and 
  12009.    compile it on the system. This results in an 
  12010.    <TT
  12011. CLASS="filename"
  12012. >extname.so</TT
  12013. > file that may then be included in <TT
  12014. CLASS="filename"
  12015. >php.ini</TT
  12016. >
  12017.   </P
  12018. ><P
  12019. >    In case the systems <SPAN
  12020. CLASS="emphasis"
  12021. ><I
  12022. CLASS="emphasis"
  12023. >preferred_state</I
  12024. ></SPAN
  12025. > is set higher than
  12026.    an available <SPAN
  12027. CLASS="emphasis"
  12028. ><I
  12029. CLASS="emphasis"
  12030. >extname</I
  12031. ></SPAN
  12032. > version, like it's set to stable 
  12033.    and the extension is still in beta, either alter the 
  12034.    <SPAN
  12035. CLASS="emphasis"
  12036. ><I
  12037. CLASS="emphasis"
  12038. >preferred_state</I
  12039. ></SPAN
  12040. > via <VAR
  12041. CLASS="literal"
  12042. >pear config-set</VAR
  12043.    or specify a specific version of the PECL extension. For example:
  12044.   </P
  12045. ><P
  12046. >    <TABLE
  12047. BORDER="0"
  12048. BGCOLOR="#E0E0E0"
  12049. CELLPADDING="5"
  12050. ><TR
  12051. ><TD
  12052. ><PRE
  12053. CLASS="screen"
  12054. >$ pear install extname-0.1.1</PRE
  12055. ></TD
  12056. ></TR
  12057. ></TABLE
  12058. >
  12059.   </P
  12060. ><P
  12061. >    Regardless, pear will copy this <TT
  12062. CLASS="filename"
  12063. >extname.so</TT
  12064. > into the 
  12065.    <A
  12066. HREF="#ini.extension-dir"
  12067. >extensions directory</A
  12068. >. Adjust  
  12069.    <TT
  12070. CLASS="filename"
  12071. >php.ini</TT
  12072. > accordingly.
  12073.   </P
  12074. ></DIV
  12075. ><DIV
  12076. CLASS="sect1"
  12077. ><HR><H2
  12078. CLASS="sect1"
  12079. ><A
  12080. NAME="install.pecl.phpize"
  12081. >Compiling shared PECL extensions with phpize</A
  12082. ></H2
  12083. ><P
  12084. >    If using pear is not an option, like for building shared PECL extensions
  12085.    from <ACRONYM
  12086. CLASS="acronym"
  12087. >CVS</ACRONYM
  12088. >, or for unreleased PECL packages, then creating 
  12089.    a shared extension may also be done by manually using the 
  12090.    <VAR
  12091. CLASS="literal"
  12092. >phpize</VAR
  12093. > command. The pear command essentially does this 
  12094.    but it may also be done manually. Assuming the source file is named 
  12095.    <TT
  12096. CLASS="filename"
  12097. >extname.tgz</TT
  12098. >, and that it was downloaded into the 
  12099.    current directory, consider the following:
  12100.   </P
  12101. ><P
  12102. >    <TABLE
  12103. BORDER="0"
  12104. BGCOLOR="#E0E0E0"
  12105. CELLPADDING="5"
  12106. ><TR
  12107. ><TD
  12108. ><PRE
  12109. CLASS="screen"
  12110. >$ pear download extname
  12111. $ gzip -d < extname.tgz | tar -xvf -
  12112. $ cd extname
  12113. $ phpize
  12114. $ ./configure && make</PRE
  12115. ></TD
  12116. ></TR
  12117. ></TABLE
  12118. >
  12119.   </P
  12120. ><P
  12121. >    Upon success, this will create <TT
  12122. CLASS="filename"
  12123. >extname.so</TT
  12124. > and put it 
  12125.    into the <TT
  12126. CLASS="filename"
  12127. >modules/</TT
  12128. > and/or <TT
  12129. CLASS="filename"
  12130. >.libs/</TT
  12131.    directory within the <TT
  12132. CLASS="filename"
  12133. >extname/</TT
  12134. > source. Move this 
  12135.    shared extension (<TT
  12136. CLASS="filename"
  12137. >extname.so</TT
  12138. >) into the PHP 
  12139.    <A
  12140. HREF="#ini.extension-dir"
  12141. >extensions directory</A
  12142. >, and adjust
  12143.    <TT
  12144. CLASS="filename"
  12145. >php.ini</TT
  12146. > accordingly.
  12147.   </P
  12148. ></DIV
  12149. ><DIV
  12150. CLASS="sect1"
  12151. ><HR><H2
  12152. CLASS="sect1"
  12153. ><A
  12154. NAME="install.pecl.static"
  12155. >Compiling PECL extensions statically into PHP</A
  12156. ></H2
  12157. ><P
  12158. >    To statically include the extension within the PHP build, put the 
  12159.    extensions source into the <TT
  12160. CLASS="filename"
  12161. >ext/</TT
  12162. > directory found in 
  12163.    the PHP source. For example:
  12164.   </P
  12165. ><P
  12166. >    <TABLE
  12167. BORDER="0"
  12168. BGCOLOR="#E0E0E0"
  12169. CELLPADDING="5"
  12170. ><TR
  12171. ><TD
  12172. ><PRE
  12173. CLASS="screen"
  12174. >$ cd /your/phpsrcdir/ext
  12175. $ pear download extname
  12176. $ gzip -d < extname.tgz | tar -xvf -
  12177. $ mv extname-x.x.x extname
  12178. $ rm package.xml</PRE
  12179. ></TD
  12180. ></TR
  12181. ></TABLE
  12182. >
  12183.   </P
  12184. ><P
  12185. >    This will result in the following directory:
  12186.   </P
  12187. ><P
  12188. >    <TABLE
  12189. BORDER="0"
  12190. BGCOLOR="#E0E0E0"
  12191. CELLPADDING="5"
  12192. ><TR
  12193. ><TD
  12194. ><PRE
  12195. CLASS="screen"
  12196. >/your/phpsrcdir/ext/extname</PRE
  12197. ></TD
  12198. ></TR
  12199. ></TABLE
  12200. >
  12201.   </P
  12202. ><P
  12203. >    From here, build PHP as normal:
  12204.   </P
  12205. ><P
  12206. >    <TABLE
  12207. BORDER="0"
  12208. BGCOLOR="#E0E0E0"
  12209. CELLPADDING="5"
  12210. ><TR
  12211. ><TD
  12212. ><PRE
  12213. CLASS="screen"
  12214. >$ cd /your/phpsrcdir 
  12215. $ ./buildconf
  12216. $ ./configure --help
  12217. $ ./configure --with-extname --enable-someotherext --with-foobar
  12218. $ make
  12219. $ make install</PRE
  12220. ></TD
  12221. ></TR
  12222. ></TABLE
  12223. >
  12224.   </P
  12225. ><P
  12226. >    Whether <VAR
  12227. CLASS="literal"
  12228. >--enable-extname</VAR
  12229. > or <VAR
  12230. CLASS="literal"
  12231. >--with-extname
  12232.    </VAR
  12233. > is used depends on the extension. Typically an extension that 
  12234.    does not require external libraries uses <VAR
  12235. CLASS="literal"
  12236. >--enable</VAR
  12237. >. To be
  12238.    sure, run the following after buildconf:
  12239.   </P
  12240. ><P
  12241. >    <TABLE
  12242. BORDER="0"
  12243. BGCOLOR="#E0E0E0"
  12244. CELLPADDING="5"
  12245. ><TR
  12246. ><TD
  12247. ><PRE
  12248. CLASS="screen"
  12249. >$ ./configure --help | grep extname</PRE
  12250. ></TD
  12251. ></TR
  12252. ></TABLE
  12253. >
  12254.   </P
  12255. ></DIV
  12256. ></DIV
  12257. ><DIV
  12258. CLASS="chapter"
  12259. ><HR><H1
  12260. ><A
  12261. NAME="install.problems"
  12262. >Chapter 8. Problems?</A
  12263. ></H1
  12264. ><DIV
  12265. CLASS="sect1"
  12266. ><H2
  12267. CLASS="sect1"
  12268. ><A
  12269. NAME="install.problems.faq"
  12270. >Read the FAQ</A
  12271. ></H2
  12272. ><P
  12273. >      Some problems are more common than others. The most
  12274.      common ones are listed in the <A
  12275. HREF="#faq"
  12276. >PHP
  12277.      FAQ</A
  12278. >, part of this manual.
  12279.     </P
  12280. ></DIV
  12281. ><DIV
  12282. CLASS="sect1"
  12283. ><HR><H2
  12284. CLASS="sect1"
  12285. ><A
  12286. NAME="install.problems.support"
  12287. >Other problems</A
  12288. ></H2
  12289. ><P
  12290. >      If you are still stuck, someone on the PHP installation mailing list may be
  12291.      able to help you. You should check out the archive first, in case
  12292.      someone already answered someone else who had the same problem as
  12293.      you. The archives are available from the support page on <A
  12294. HREF="http://www.php.net/support.php"
  12295. TARGET="_top"
  12296. >http://www.php.net/support.php</A
  12297. >. To subscribe to the PHP installation
  12298.      mailing list, send an empty mail to <A
  12299. HREF="mailto:php-install-subscribe@lists.php.net"
  12300. TARGET="_top"
  12301. >php-install-subscribe@lists.php.net</A
  12302. >.
  12303.      The mailing list address is <A
  12304. HREF="mailto:php-install@lists.php.net"
  12305. TARGET="_top"
  12306. >php-install@lists.php.net</A
  12307. >.
  12308.     </P
  12309. ><P
  12310. >      If you want to get help on the mailing list, please try to be
  12311.      precise and give the necessary details about your environment
  12312.      (which operating system, what PHP version, what web server, if
  12313.      you are running PHP as CGI or a server module, <A
  12314. HREF="#ini.safe-mode"
  12315. >safe mode</A
  12316. >, etc...), and
  12317.      preferably enough code to make others able to reproduce and test
  12318.      your problem.
  12319.     </P
  12320. ></DIV
  12321. ><DIV
  12322. CLASS="sect1"
  12323. ><HR><H2
  12324. CLASS="sect1"
  12325. ><A
  12326. NAME="install.problems.bugs"
  12327. >Bug reports</A
  12328. ></H2
  12329. ><P
  12330. >      If you think you have found a bug in PHP, please report it.  The
  12331.      PHP developers probably don't know about it, and unless you
  12332.      report it, chances are it won't be fixed. You can report bugs
  12333.      using the bug-tracking system at <A
  12334. HREF="http://bugs.php.net/"
  12335. TARGET="_top"
  12336. >http://bugs.php.net/</A
  12337. >. Please do not
  12338.      send bug reports in mailing list or personal letters. The
  12339.      bug system is also suitable to submit feature requests.
  12340.     </P
  12341. ><P
  12342. >      Read the <A
  12343. HREF="http://bugs.php.net/how-to-report.php"
  12344. TARGET="_top"
  12345. >How to report a bug</A
  12346.      document before submitting any bug reports!
  12347.     </P
  12348. ></DIV
  12349. ></DIV
  12350. ><DIV
  12351. CLASS="chapter"
  12352. ><HR><H1
  12353. ><A
  12354. NAME="configuration"
  12355. >Chapter 9. Runtime Configuration</A
  12356. ></H1
  12357. ><DIV
  12358. CLASS="sect1"
  12359. ><H2
  12360. CLASS="sect1"
  12361. ><A
  12362. NAME="configuration.file"
  12363. >The configuration file</A
  12364. ></H2
  12365. ><P
  12366. >     The configuration file (called <TT
  12367. CLASS="filename"
  12368. >php3.ini</TT
  12369. > in
  12370.     PHP 3, and simply <TT
  12371. CLASS="filename"
  12372. >php.ini</TT
  12373. > as of PHP 4)
  12374.     is read when PHP starts up. For the server module versions of PHP,
  12375.     this happens only once when the web server is started. For the
  12376.     <ACRONYM
  12377. CLASS="acronym"
  12378. >CGI</ACRONYM
  12379. > and <ACRONYM
  12380. CLASS="acronym"
  12381. >CLI</ACRONYM
  12382. > version, it happens on
  12383.     every invocation.
  12384.    </P
  12385. ><P
  12386. >     The default location of <TT
  12387. CLASS="filename"
  12388. >php.ini</TT
  12389. > is a compile time option (see the <A
  12390. HREF="#faq.installation.phpini"
  12391. >FAQ</A
  12392. > entry), but can be changed
  12393.     for the <ACRONYM
  12394. CLASS="acronym"
  12395. >CGI</ACRONYM
  12396. > and <ACRONYM
  12397. CLASS="acronym"
  12398. >CLI</ACRONYM
  12399. > version with the
  12400.     <VAR
  12401. CLASS="literal"
  12402. >-c</VAR
  12403. > command line switch, see the chapter about using
  12404.     PHP from the <A
  12405. HREF="#features.commandline"
  12406. >command line</A
  12407. >. You can also use the
  12408.     environment variable <VAR
  12409. CLASS="varname"
  12410. >PHPRC</VAR
  12411. > for an additional path to
  12412.     search for a <TT
  12413. CLASS="filename"
  12414. >php.ini</TT
  12415. > file.
  12416.    </P
  12417. ><P
  12418. >     If <TT
  12419. CLASS="filename"
  12420. >php-SAPI.ini</TT
  12421. > exists (where SAPI is used SAPI, so the
  12422.     filename is e.g. <TT
  12423. CLASS="filename"
  12424. >php-cli.ini</TT
  12425. > or
  12426.     <TT
  12427. CLASS="filename"
  12428. >php-apache.ini</TT
  12429. >), it's used instead of <TT
  12430. CLASS="filename"
  12431. >php.ini</TT
  12432. >.
  12433.    </P
  12434. ><DIV
  12435. CLASS="note"
  12436. ><BLOCKQUOTE
  12437. CLASS="note"
  12438. ><P
  12439. ><B
  12440. >Note: </B
  12441. >
  12442.      The Apache web server changes the directory to root at startup causing
  12443.      PHP to attempt to read <TT
  12444. CLASS="filename"
  12445. >php.ini</TT
  12446. > from the root filesystem if it exists.
  12447.     </P
  12448. ></BLOCKQUOTE
  12449. ></DIV
  12450. ><P
  12451. >     The <TT
  12452. CLASS="filename"
  12453. >php.ini</TT
  12454. > directives handled by extensions are documented respectively
  12455.     on the pages of the extensions themselves. The <A
  12456. HREF="#ini"
  12457. >list of
  12458.     the core directives</A
  12459. > is available in the appendix. Probably not all
  12460.     the PHP directives are documented in the manual though. For a completel list
  12461.     of directives available in your PHP version, please read your well commented
  12462.     <TT
  12463. CLASS="filename"
  12464. >php.ini</TT
  12465. > file. Alternatively, you may find the
  12466.     <A
  12467. HREF="http://cvs.php.net/co.php/php-src/php.ini-dist"
  12468. TARGET="_top"
  12469. >the latest <TT
  12470. CLASS="filename"
  12471. >php.ini</TT
  12472. ></A
  12473. > from CVS
  12474.     helpful too.
  12475.    </P
  12476. ><P
  12477. >     <TABLE
  12478. WIDTH="100%"
  12479. BORDER="0"
  12480. CELLPADDING="0"
  12481. CELLSPACING="0"
  12482. CLASS="EXAMPLE"
  12483. ><TR
  12484. ><TD
  12485. ><DIV
  12486. CLASS="example"
  12487. ><A
  12488. NAME="AEN2389"
  12489. ></A
  12490. ><P
  12491. ><B
  12492. >Example 9-1. <TT
  12493. CLASS="filename"
  12494. >php.ini</TT
  12495. > example</B
  12496. ></P
  12497. ><TABLE
  12498. BORDER="0"
  12499. BGCOLOR="#E0E0E0"
  12500. CELLPADDING="5"
  12501. ><TR
  12502. ><TD
  12503. ><PRE
  12504. CLASS="ini"
  12505. >; any text on a line after an unquoted semicolon (;) is ignored
  12506. [php] ; section markers (text within square brackets) are also ignored
  12507. ; Boolean values can be set to either:
  12508. ;    true, on, yes
  12509. ; or false, off, no, none
  12510. register_globals = off
  12511. track_errors = yes
  12512.  
  12513. ; you can enclose strings in double-quotes
  12514. include_path = ".:/usr/local/lib/php"
  12515.  
  12516. ; backslashes are treated the same as any other character
  12517. include_path = ".;c:\php\lib"</PRE
  12518. ></TD
  12519. ></TR
  12520. ></TABLE
  12521. ></DIV
  12522. ></TD
  12523. ></TR
  12524. ></TABLE
  12525. >
  12526.    </P
  12527. ></DIV
  12528. ><DIV
  12529. CLASS="sect1"
  12530. ><HR><H2
  12531. CLASS="sect1"
  12532. ><A
  12533. NAME="configuration.changes"
  12534. >How to change configuration settings</A
  12535. ></H2
  12536. ><DIV
  12537. CLASS="sect2"
  12538. ><H3
  12539. CLASS="sect2"
  12540. ><A
  12541. NAME="configuration.changes.apache"
  12542. >Running PHP as an Apache module</A
  12543. ></H3
  12544. ><P
  12545. >      When using PHP as an Apache module, you can also change the
  12546.      configuration settings using directives in Apache configuration
  12547.      files (e.g. <TT
  12548. CLASS="filename"
  12549. >httpd.conf</TT
  12550. >) and <TT
  12551. CLASS="filename"
  12552. >.htaccess</TT
  12553. > files. You will need
  12554.      "AllowOverride Options" or "AllowOverride All" privileges to do so.
  12555.     </P
  12556. ><P
  12557. >      With PHP 4 and PHP 5, there are several Apache directives that allow you
  12558.      to change the PHP configuration from within the Apache configuration
  12559.      files.  For a listing of which directives are 
  12560.      <TT
  12561. CLASS="constant"
  12562. ><B
  12563. >PHP_INI_ALL</B
  12564. ></TT
  12565. >, <TT
  12566. CLASS="constant"
  12567. ><B
  12568. >PHP_INI_PERDIR</B
  12569. ></TT
  12570. >, 
  12571.      or <TT
  12572. CLASS="constant"
  12573. ><B
  12574. >PHP_INI_SYSTEM</B
  12575. ></TT
  12576. >, have a look at the table
  12577.      found within the <A
  12578. HREF="#function.ini-set"
  12579. ><B
  12580. CLASS="function"
  12581. >ini_set()</B
  12582. ></A
  12583. > documentation.
  12584.     </P
  12585. ><DIV
  12586. CLASS="note"
  12587. ><BLOCKQUOTE
  12588. CLASS="note"
  12589. ><P
  12590. ><B
  12591. >Note: </B
  12592. >
  12593.       With PHP 3, there are Apache directives that correspond to each
  12594.       configuration setting in the <TT
  12595. CLASS="filename"
  12596. >php3.ini</TT
  12597. > name,
  12598.       except the name is prefixed by "php3_".
  12599.      </P
  12600. ></BLOCKQUOTE
  12601. ></DIV
  12602. ><P
  12603. >      <P
  12604. ></P
  12605. ><DIV
  12606. CLASS="variablelist"
  12607. ><DL
  12608. ><DT
  12609. ><SPAN
  12610. CLASS="systemitem"
  12611. >php_value</SPAN
  12612. >
  12613.         <VAR
  12614. CLASS="parameter"
  12615. >name</VAR
  12616. >
  12617.         <VAR
  12618. CLASS="parameter"
  12619. >value</VAR
  12620. ></DT
  12621. ><DD
  12622. ><P
  12623. >          Sets the value of the specified directive.
  12624.          Can be used only with <TT
  12625. CLASS="constant"
  12626. ><B
  12627. >PHP_INI_ALL</B
  12628. ></TT
  12629. > and <TT
  12630. CLASS="constant"
  12631. ><B
  12632. >PHP_INI_PERDIR</B
  12633. ></TT
  12634. > type directives.
  12635.          To clear a previously set value use <VAR
  12636. CLASS="literal"
  12637. >none</VAR
  12638. > as the value.
  12639.         </P
  12640. ><DIV
  12641. CLASS="note"
  12642. ><BLOCKQUOTE
  12643. CLASS="note"
  12644. ><P
  12645. ><B
  12646. >Note: </B
  12647. >
  12648.           Don't use <SPAN
  12649. CLASS="systemitem"
  12650. >php_value</SPAN
  12651. > to set boolean values.
  12652.           <SPAN
  12653. CLASS="systemitem"
  12654. >php_flag</SPAN
  12655. > (see below) should be used instead.
  12656.          </P
  12657. ></BLOCKQUOTE
  12658. ></DIV
  12659. ></DD
  12660. ><DT
  12661. ><SPAN
  12662. CLASS="systemitem"
  12663. >php_flag</SPAN
  12664. >
  12665.         <VAR
  12666. CLASS="parameter"
  12667. >name</VAR
  12668. >
  12669.         <VAR
  12670. CLASS="parameter"
  12671. >on|off</VAR
  12672. ></DT
  12673. ><DD
  12674. ><P
  12675. >          Used to set a boolean configuration directive.
  12676.          Can be used only with <TT
  12677. CLASS="constant"
  12678. ><B
  12679. >PHP_INI_ALL</B
  12680. ></TT
  12681. > and
  12682.          <TT
  12683. CLASS="constant"
  12684. ><B
  12685. >PHP_INI_PERDIR</B
  12686. ></TT
  12687. > type directives.
  12688.         </P
  12689. ></DD
  12690. ><DT
  12691. ><SPAN
  12692. CLASS="systemitem"
  12693. >php_admin_value</SPAN
  12694. >
  12695.         <VAR
  12696. CLASS="parameter"
  12697. >name</VAR
  12698. >
  12699.         <VAR
  12700. CLASS="parameter"
  12701. >value</VAR
  12702. ></DT
  12703. ><DD
  12704. ><P
  12705. >          Sets the value of the specified directive.
  12706.          This <SPAN
  12707. CLASS="emphasis"
  12708. ><I
  12709. CLASS="emphasis"
  12710. >can not be used</I
  12711. ></SPAN
  12712. > in <TT
  12713. CLASS="filename"
  12714. >.htaccess</TT
  12715. > files.
  12716.          Any directive type set with <SPAN
  12717. CLASS="systemitem"
  12718. >php_admin_value</SPAN
  12719. >
  12720.          can not be overridden by <TT
  12721. CLASS="filename"
  12722. >.htaccess</TT
  12723. > or virtualhost directives.
  12724.          To clear a previously set value use <VAR
  12725. CLASS="literal"
  12726. >none</VAR
  12727. > as the value.
  12728.         </P
  12729. ></DD
  12730. ><DT
  12731. ><SPAN
  12732. CLASS="systemitem"
  12733. >php_admin_flag</SPAN
  12734. >
  12735.         <VAR
  12736. CLASS="parameter"
  12737. >name</VAR
  12738. >
  12739.         <VAR
  12740. CLASS="parameter"
  12741. >on|off</VAR
  12742. ></DT
  12743. ><DD
  12744. ><P
  12745. >          Used to set a boolean configuration directive.
  12746.          This <SPAN
  12747. CLASS="emphasis"
  12748. ><I
  12749. CLASS="emphasis"
  12750. >can not be used</I
  12751. ></SPAN
  12752. > in <TT
  12753. CLASS="filename"
  12754. >.htaccess</TT
  12755. > files.
  12756.          Any directive type set with <SPAN
  12757. CLASS="systemitem"
  12758. >php_admin_flag</SPAN
  12759. >
  12760.          can not be overridden by <TT
  12761. CLASS="filename"
  12762. >.htaccess</TT
  12763. > or virtualhost directives.
  12764.         </P
  12765. ></DD
  12766. ></DL
  12767. ></DIV
  12768. >
  12769.     </P
  12770. ><P
  12771. >      <TABLE
  12772. WIDTH="100%"
  12773. BORDER="0"
  12774. CELLPADDING="0"
  12775. CELLSPACING="0"
  12776. CLASS="EXAMPLE"
  12777. ><TR
  12778. ><TD
  12779. ><DIV
  12780. CLASS="example"
  12781. ><A
  12782. NAME="AEN2457"
  12783. ></A
  12784. ><P
  12785. ><B
  12786. >Example 9-2. Apache configuration example</B
  12787. ></P
  12788. ><TABLE
  12789. BORDER="0"
  12790. BGCOLOR="#E0E0E0"
  12791. CELLPADDING="5"
  12792. ><TR
  12793. ><TD
  12794. ><PRE
  12795. CLASS="ini"
  12796. ><IfModule mod_php5.c>
  12797.   php_value include_path ".:/usr/local/lib/php"
  12798.   php_admin_flag safe_mode on
  12799. </IfModule>
  12800. <IfModule mod_php4.c>
  12801.   php_value include_path ".:/usr/local/lib/php"
  12802.   php_admin_flag safe_mode on
  12803. </IfModule>
  12804. <IfModule mod_php3.c>
  12805.   php3_include_path ".:/usr/local/lib/php"
  12806.   php3_safe_mode on
  12807. </IfModule></PRE
  12808. ></TD
  12809. ></TR
  12810. ></TABLE
  12811. ></DIV
  12812. ></TD
  12813. ></TR
  12814. ></TABLE
  12815. >
  12816.     </P
  12817. ><DIV
  12818. CLASS="caution"
  12819. ><P
  12820. ></P
  12821. ><TABLE
  12822. CLASS="caution"
  12823. BORDER="1"
  12824. WIDTH="100%"
  12825. ><TR
  12826. ><TD
  12827. ALIGN="CENTER"
  12828. ><B
  12829. >Caution</B
  12830. ></TD
  12831. ></TR
  12832. ><TR
  12833. ><TD
  12834. ALIGN="LEFT"
  12835. ><P
  12836. >       PHP constants do not exist outside of PHP. For example, in 
  12837.       <TT
  12838. CLASS="filename"
  12839. >httpd.conf</TT
  12840. > you can not use PHP constants 
  12841.       such as <TT
  12842. CLASS="constant"
  12843. ><B
  12844. >E_ALL</B
  12845. ></TT
  12846. > or <TT
  12847. CLASS="constant"
  12848. ><B
  12849. >E_NOTICE</B
  12850. ></TT
  12851.       to set the <A
  12852. HREF="#ini.error-reporting"
  12853. >error_reporting</A
  12854.       directive as they will have no meaning and will evaluate to
  12855.       <SPAN
  12856. CLASS="emphasis"
  12857. ><I
  12858. CLASS="emphasis"
  12859. >0</I
  12860. ></SPAN
  12861. >. Use the associated bitmask values instead.
  12862.       These constants can be used in <TT
  12863. CLASS="filename"
  12864. >php.ini</TT
  12865. >
  12866.      </P
  12867. ></TD
  12868. ></TR
  12869. ></TABLE
  12870. ></DIV
  12871. ></DIV
  12872. ><DIV
  12873. CLASS="sect2"
  12874. ><HR><H3
  12875. CLASS="sect2"
  12876. ><A
  12877. NAME="configuration.changes.windows"
  12878. >Changing PHP configuration via the Windows registry</A
  12879. ></H3
  12880. ><P
  12881. >       When running PHP on Windows, the configuration values can be
  12882.       modified on a per-directory basis using the Windows registry. The
  12883.       configuration values are stored in the registry key 
  12884.       <VAR
  12885. CLASS="literal"
  12886. >HKLM\SOFTWARE\PHP\Per Directory Values</VAR
  12887. >, 
  12888.       in the sub-keys corresponding to the path names. For example, configuration 
  12889.       values for the directory <VAR
  12890. CLASS="literal"
  12891. >c:\inetpub\wwwroot</VAR
  12892. > would
  12893.       be stored in the key <VAR
  12894. CLASS="literal"
  12895. >HKLM\SOFTWARE\PHP\Per Directory
  12896.       Values\c\inetpub\wwwroot</VAR
  12897. >. The settings for the
  12898.       directory would be active for any script running from this
  12899.       directory or any subdirectory of it. The values under the key
  12900.       should have the name of the PHP configuration directive and the
  12901.       string value. PHP constants in the values are not parsed. 
  12902.      </P
  12903. ></DIV
  12904. ><DIV
  12905. CLASS="sect2"
  12906. ><HR><H3
  12907. CLASS="sect2"
  12908. ><A
  12909. NAME="configuration.changes.other"
  12910. >Other interfaces to PHP</A
  12911. ></H3
  12912. ><P
  12913. >      Regardless of how you run PHP, you can change certain values at runtime
  12914.      of your scripts through <A
  12915. HREF="#function.ini-set"
  12916. ><B
  12917. CLASS="function"
  12918. >ini_set()</B
  12919. ></A
  12920. >. See the documentation
  12921.      on the <A
  12922. HREF="#function.ini-set"
  12923. ><B
  12924. CLASS="function"
  12925. >ini_set()</B
  12926. ></A
  12927. > page for more information.
  12928.     </P
  12929. ><P
  12930. >      If you are interested in a complete list of configuration settings
  12931.      on your system with their current values, you can execute the
  12932.      <A
  12933. HREF="#function.phpinfo"
  12934. ><B
  12935. CLASS="function"
  12936. >phpinfo()</B
  12937. ></A
  12938. > function, and review the resulting
  12939.      page. You can also access the values of individual configuration
  12940.      directives at runtime using <A
  12941. HREF="#function.ini-get"
  12942. ><B
  12943. CLASS="function"
  12944. >ini_get()</B
  12945. ></A
  12946. > or
  12947.      <A
  12948. HREF="#function.get-cfg-var"
  12949. ><B
  12950. CLASS="function"
  12951. >get_cfg_var()</B
  12952. ></A
  12953. >.
  12954.     </P
  12955. ></DIV
  12956. ></DIV
  12957. ></DIV
  12958. ></DIV
  12959. ><DIV
  12960. CLASS="PART"
  12961. ><A
  12962. NAME="langref"
  12963. ></A
  12964. ><DIV
  12965. CLASS="TITLEPAGE"
  12966. ><H1
  12967. CLASS="title"
  12968. >III. Language Reference</H1
  12969. ><DIV
  12970. CLASS="TOC"
  12971. ><DL
  12972. ><DT
  12973. ><B
  12974. >Table of Contents</B
  12975. ></DT
  12976. ><DT
  12977. >10. <A
  12978. HREF="#language.basic-syntax"
  12979. >Basic syntax</A
  12980. ></DT
  12981. ><DT
  12982. >11. <A
  12983. HREF="#language.types"
  12984. >Types</A
  12985. ></DT
  12986. ><DT
  12987. >12. <A
  12988. HREF="#language.variables"
  12989. >Variables</A
  12990. ></DT
  12991. ><DT
  12992. >13. <A
  12993. HREF="#language.constants"
  12994. >Constants</A
  12995. ></DT
  12996. ><DT
  12997. >14. <A
  12998. HREF="#language.expressions"
  12999. >Expressions</A
  13000. ></DT
  13001. ><DT
  13002. >15. <A
  13003. HREF="#language.operators"
  13004. >Operators</A
  13005. ></DT
  13006. ><DT
  13007. >16. <A
  13008. HREF="#language.control-structures"
  13009. >Control Structures</A
  13010. ></DT
  13011. ><DT
  13012. >17. <A
  13013. HREF="#language.functions"
  13014. >Functions</A
  13015. ></DT
  13016. ><DT
  13017. >18. <A
  13018. HREF="#language.oop"
  13019. >Classes and Objects (PHP 4)</A
  13020. ></DT
  13021. ><DT
  13022. >19. <A
  13023. HREF="#language.oop5"
  13024. >Classes and Objects (PHP 5)</A
  13025. ></DT
  13026. ><DT
  13027. >20. <A
  13028. HREF="#language.exceptions"
  13029. >Exceptions</A
  13030. ></DT
  13031. ><DT
  13032. >21. <A
  13033. HREF="#language.references"
  13034. >References Explained</A
  13035. ></DT
  13036. ></DL
  13037. ></DIV
  13038. ></DIV
  13039. ><DIV
  13040. CLASS="chapter"
  13041. ><HR><H1
  13042. ><A
  13043. NAME="language.basic-syntax"
  13044. >Chapter 10. Basic syntax</A
  13045. ></H1
  13046. ><DIV
  13047. CLASS="sect1"
  13048. ><H2
  13049. CLASS="sect1"
  13050. ><A
  13051. NAME="language.basic-syntax.phpmode"
  13052. >Escaping from HTML</A
  13053. ></H2
  13054. ><P
  13055. >     When PHP parses a file, it simply passes the text of the file
  13056.     through until it encounters one of the special tags which tell it
  13057.     to start interpreting the text as PHP code. The parser then
  13058.     executes all the code it finds, up until it runs into a PHP
  13059.     closing tag, which tells the parser to just start passing the text
  13060.     through again. This is the mechanism which allows you to embed PHP
  13061.     code inside HTML: everything outside the PHP tags is left utterly
  13062.     alone, while everything inside is parsed as code.
  13063.    </P
  13064. ><P
  13065. >     There are four sets of tags which can be used to denote blocks of
  13066.     PHP code. Of these, only two (<?php. . .?> and <script
  13067.     language="php">. . .</script>) are always available; the
  13068.     others can be turned on or off from the
  13069.     <TT
  13070. CLASS="filename"
  13071. >php.ini</TT
  13072. > configuration file. While the
  13073.     short-form tags and ASP-style tags may be convenient, they are not
  13074.     as portable as the longer versions. Also, if you intend to embed
  13075.     PHP code in XML or XHTML, you will need to use the
  13076.     <?php. . .?> form to conform to the XML.
  13077.    </P
  13078. ><P
  13079. >     The tags supported by PHP are:
  13080.    </P
  13081. ><P
  13082. >      <TABLE
  13083. WIDTH="100%"
  13084. BORDER="0"
  13085. CELLPADDING="0"
  13086. CELLSPACING="0"
  13087. CLASS="EXAMPLE"
  13088. ><TR
  13089. ><TD
  13090. ><DIV
  13091. CLASS="example"
  13092. ><A
  13093. NAME="AEN2494"
  13094. ></A
  13095. ><P
  13096. ><B
  13097. >Example 10-1. Ways of escaping from HTML</B
  13098. ></P
  13099. ><TABLE
  13100. BORDER="0"
  13101. BGCOLOR="#E0E0E0"
  13102. CELLPADDING="5"
  13103. ><TR
  13104. ><TD
  13105. ><PRE
  13106. CLASS="php"
  13107. >1.  <?php echo("if you want to serve XHTML or XML documents, do like this\n"); ?>
  13108.  
  13109. 2.  <? echo ("this is the simplest, an SGML processing instruction\n"); ?>
  13110.     <?= expression ?> This is a shortcut for "<? echo expression ?>"
  13111.     
  13112. 3.  <script language="php">
  13113.         echo ("some editors (like FrontPage) don't
  13114.               like processing instructions");
  13115.     </script>
  13116.  
  13117. 4.  <% echo ("You may optionally use ASP-style tags"); %>
  13118.     <%= $variable; # This is a shortcut for "<% echo . . ." %></PRE
  13119. ></TD
  13120. ></TR
  13121. ></TABLE
  13122. ></DIV
  13123. ></TD
  13124. ></TR
  13125. ></TABLE
  13126. >
  13127.     </P
  13128. ><P
  13129. >     The first way, <?php. . .?>, is the preferred method, as it
  13130.     allows the use of PHP in XML-conformant code such as XHTML.
  13131.    </P
  13132. ><P
  13133. >     The second way is not available always. Short tags are available
  13134.     only when they have been enabled. This can be done via the
  13135.     <B
  13136. CLASS="function"
  13137. >short_tags()</B
  13138. > function (PHP 3 only), by enabling
  13139.     the <A
  13140. HREF="#ini.short-open-tag"
  13141. >short_open_tag</A
  13142. >
  13143.     configuration setting in the PHP config file, or by compiling PHP
  13144.     with the <VAR
  13145. CLASS="option"
  13146. >--enable-short-tags</VAR
  13147. > option to
  13148.     <B
  13149. CLASS="command"
  13150. >configure</B
  13151. >. Even if it is enabled by default in
  13152.     <TT
  13153. CLASS="filename"
  13154. >php.ini-dist</TT
  13155. >, use of short tags are discouraged.
  13156.    </P
  13157. ><P
  13158. >     The third way is always available and safe like the first one. However,
  13159.     the first is the preferred and most used one.
  13160.    </P
  13161. ><P
  13162. >     The fourth way is only available if <SPAN
  13163. CLASS="productname"
  13164. >ASP</SPAN
  13165.     tags have been enabled using the <A
  13166. HREF="#ini.asp-tags"
  13167. >asp_tags</A
  13168. >
  13169.     configuration setting.
  13170.     <DIV
  13171. CLASS="note"
  13172. ><BLOCKQUOTE
  13173. CLASS="note"
  13174. ><P
  13175. ><B
  13176. >Note: </B
  13177. >Support for <SPAN
  13178. CLASS="productname"
  13179. >ASP</SPAN
  13180. > tags was added in 3.0.4.</P
  13181. ></BLOCKQUOTE
  13182. ></DIV
  13183. >
  13184.    </P
  13185. ><P
  13186. >     <DIV
  13187. CLASS="note"
  13188. ><BLOCKQUOTE
  13189. CLASS="note"
  13190. ><P
  13191. ><B
  13192. >Note: </B
  13193. >
  13194.       Using short tags should be avoided when developing applications
  13195.       or libraries that are meant for redistribution, or deployment on
  13196.       PHP servers which are not under your control, because short tags
  13197.       may not be supported on the target server.  For portable,
  13198.       redistributable code, be sure not to use short tags.
  13199.      </P
  13200. ></BLOCKQUOTE
  13201. ></DIV
  13202. >
  13203.    </P
  13204. ><P
  13205. >     The closing tag for the block will include the immediately
  13206.     trailing newline if one is present. Also, the closing tag
  13207.     automatically implies a semicolon; you do not need to have a
  13208.     semicolon terminating the last line of a PHP block.
  13209.     Closing tag of a PHP block at the end of a file is optional.
  13210.    </P
  13211. ><P
  13212. >     PHP allows you to use structures like this:
  13213.     <TABLE
  13214. WIDTH="100%"
  13215. BORDER="0"
  13216. CELLPADDING="0"
  13217. CELLSPACING="0"
  13218. CLASS="EXAMPLE"
  13219. ><TR
  13220. ><TD
  13221. ><DIV
  13222. CLASS="example"
  13223. ><A
  13224. NAME="AEN2516"
  13225. ></A
  13226. ><P
  13227. ><B
  13228. >Example 10-2. Advanced escaping</B
  13229. ></P
  13230. ><TABLE
  13231. BORDER="0"
  13232. BGCOLOR="#E0E0E0"
  13233. CELLPADDING="5"
  13234. ><TR
  13235. ><TD
  13236. ><PRE
  13237. CLASS="php"
  13238. ><?php
  13239. if ($expression) { 
  13240.     ?>
  13241.     <strong>This is true.</strong>
  13242.     <?php 
  13243. } else { 
  13244.     ?>
  13245.     <strong>This is false.</strong>
  13246.     <?php 
  13247. }
  13248. ?></PRE
  13249. ></TD
  13250. ></TR
  13251. ></TABLE
  13252. ></DIV
  13253. ></TD
  13254. ></TR
  13255. ></TABLE
  13256. >
  13257.     This works as expected, because when PHP hits the ?> closing
  13258.     tags, it simply starts outputting whatever it finds until it hits
  13259.     another opening tag. The example given here is contrived, of
  13260.     course, but for outputting large blocks of text, dropping out of
  13261.     PHP parsing mode is generally more efficient than sending all of
  13262.     the text through <A
  13263. HREF="#function.echo"
  13264. ><B
  13265. CLASS="function"
  13266. >echo()</B
  13267. ></A
  13268. > or
  13269.     <A
  13270. HREF="#function.print"
  13271. ><B
  13272. CLASS="function"
  13273. >print()</B
  13274. ></A
  13275. > or somesuch.
  13276.    </P
  13277. ></DIV
  13278. ><DIV
  13279. CLASS="sect1"
  13280. ><HR><H2
  13281. CLASS="sect1"
  13282. ><A
  13283. NAME="language.basic-syntax.instruction-separation"
  13284. >Instruction separation</A
  13285. ></H2
  13286. ><P
  13287. >     Instructions are separated the same as in C or Perl - terminate
  13288.     each statement with a semicolon.
  13289.    </P
  13290. ><P
  13291. >     The closing tag (?>) also implies the end of the statement, so
  13292.     the following are equivalent:
  13293.  
  13294.     <DIV
  13295. CLASS="informalexample"
  13296. ><P
  13297. ></P
  13298. ><A
  13299. NAME="AEN2525"
  13300. ></A
  13301. ><TABLE
  13302. BORDER="0"
  13303. BGCOLOR="#E0E0E0"
  13304. CELLPADDING="5"
  13305. ><TR
  13306. ><TD
  13307. ><PRE
  13308. CLASS="php"
  13309. ><?php
  13310.     echo "This is a test";
  13311. ?>
  13312.  
  13313. <?php echo "This is a test" ?></PRE
  13314. ></TD
  13315. ></TR
  13316. ></TABLE
  13317. ><P
  13318. ></P
  13319. ></DIV
  13320. >
  13321.    </P
  13322. ></DIV
  13323. ><DIV
  13324. CLASS="sect1"
  13325. ><HR><H2
  13326. CLASS="sect1"
  13327. ><A
  13328. NAME="language.basic-syntax.comments"
  13329. >Comments</A
  13330. ></H2
  13331. ><P
  13332. >     PHP supports 'C', 'C++' and Unix shell-style comments. For example:
  13333.  
  13334.     <DIV
  13335. CLASS="informalexample"
  13336. ><P
  13337. ></P
  13338. ><A
  13339. NAME="AEN2530"
  13340. ></A
  13341. ><TABLE
  13342. BORDER="0"
  13343. BGCOLOR="#E0E0E0"
  13344. CELLPADDING="5"
  13345. ><TR
  13346. ><TD
  13347. ><PRE
  13348. CLASS="php"
  13349. ><?php
  13350.     echo "This is a test"; // This is a one-line c++ style comment
  13351.     /* This is a multi line comment
  13352.        yet another line of comment */
  13353.     echo "This is yet another test";
  13354.     echo "One Final Test"; # This is shell-style style comment
  13355. ?></PRE
  13356. ></TD
  13357. ></TR
  13358. ></TABLE
  13359. ><P
  13360. ></P
  13361. ></DIV
  13362. >
  13363.    </P
  13364. ><P
  13365. >     The "one-line" comment styles actually only comment to the end of
  13366.     the line or the current block of PHP code, whichever comes
  13367.     first.
  13368.    </P
  13369. ><P
  13370. >     <DIV
  13371. CLASS="informalexample"
  13372. ><P
  13373. ></P
  13374. ><A
  13375. NAME="AEN2534"
  13376. ></A
  13377. ><TABLE
  13378. BORDER="0"
  13379. BGCOLOR="#E0E0E0"
  13380. CELLPADDING="5"
  13381. ><TR
  13382. ><TD
  13383. ><PRE
  13384. CLASS="php"
  13385. ><h1>This is an <?php # echo "simple";?> example.</h1>
  13386. <p>The header above will say 'This is an example'.</PRE
  13387. ></TD
  13388. ></TR
  13389. ></TABLE
  13390. ><P
  13391. ></P
  13392. ></DIV
  13393.    </P
  13394. ><P
  13395. >     You should be careful not to nest 'C' style comments, which can
  13396.     happen when commenting out large blocks.
  13397.    </P
  13398. ><P
  13399. >     <DIV
  13400. CLASS="informalexample"
  13401. ><P
  13402. ></P
  13403. ><A
  13404. NAME="AEN2538"
  13405. ></A
  13406. ><TABLE
  13407. BORDER="0"
  13408. BGCOLOR="#E0E0E0"
  13409. CELLPADDING="5"
  13410. ><TR
  13411. ><TD
  13412. ><PRE
  13413. CLASS="php"
  13414. ><?php
  13415.  /* 
  13416.     echo "This is a test"; /* This comment will cause a problem */
  13417.  */
  13418. ?></PRE
  13419. ></TD
  13420. ></TR
  13421. ></TABLE
  13422. ><P
  13423. ></P
  13424. ></DIV
  13425. >
  13426.    </P
  13427. ><P
  13428. >     The one-line comment styles actually only comment to the end
  13429.     of the line or the current block of PHP code, whichever comes first.
  13430.     This means that HTML code after <VAR
  13431. CLASS="literal"
  13432. >// ?></VAR
  13433. > WILL be printed:
  13434.     ?> skips out of the PHP mode and returns to HTML mode, and
  13435.     <VAR
  13436. CLASS="literal"
  13437. >//</VAR
  13438. > cannot influence that.
  13439.     If <A
  13440. HREF="#ini.asp-tags"
  13441. >asp_tags</A
  13442. > configuration directive
  13443.     is enabled, it behaves the same with <VAR
  13444. CLASS="literal"
  13445. >// %></VAR
  13446. >.
  13447.    </P
  13448. ></DIV
  13449. ></DIV
  13450. ><DIV
  13451. CLASS="chapter"
  13452. ><HR><H1
  13453. ><A
  13454. NAME="language.types"
  13455. >Chapter 11. Types</A
  13456. ></H1
  13457. ><DIV
  13458. CLASS="sect1"
  13459. ><H2
  13460. CLASS="sect1"
  13461. ><A
  13462. NAME="language.types.intro"
  13463. >Introduction</A
  13464. ></H2
  13465. ><P
  13466. >    PHP supports eight primitive types.
  13467.   </P
  13468. ><P
  13469. >    Four scalar types:
  13470.  
  13471.    <P
  13472. ></P
  13473. ><UL
  13474. ><LI
  13475. ><P
  13476. >       <A
  13477. HREF="#language.types.boolean"
  13478. ><B
  13479. CLASS="type"
  13480. >boolean</B
  13481. ></A
  13482. >
  13483.      </P
  13484. ></LI
  13485. ><LI
  13486. ><P
  13487. >       <A
  13488. HREF="#language.types.integer"
  13489. ><B
  13490. CLASS="type"
  13491. >integer</B
  13492. ></A
  13493. >
  13494.      </P
  13495. ></LI
  13496. ><LI
  13497. ><P
  13498. >       <A
  13499. HREF="#language.types.float"
  13500. ><B
  13501. CLASS="type"
  13502. >float</B
  13503. ></A
  13504. > (floating-point number, aka '<A
  13505. HREF="#language.types.float"
  13506. ><B
  13507. CLASS="type"
  13508. >double</B
  13509. ></A
  13510. >')
  13511.      </P
  13512. ></LI
  13513. ><LI
  13514. ><P
  13515. >       <A
  13516. HREF="#language.types.string"
  13517. ><B
  13518. CLASS="type"
  13519. >string</B
  13520. ></A
  13521. >
  13522.      </P
  13523. ></LI
  13524. ></UL
  13525. >
  13526.  
  13527.    Two compound types:
  13528.  
  13529.    <P
  13530. ></P
  13531. ><UL
  13532. ><LI
  13533. ><P
  13534. >       <A
  13535. HREF="#language.types.array"
  13536. ><B
  13537. CLASS="type"
  13538. >array</B
  13539. ></A
  13540. >
  13541.      </P
  13542. ></LI
  13543. ><LI
  13544. ><P
  13545. >       <A
  13546. HREF="#language.types.object"
  13547. ><B
  13548. CLASS="type"
  13549. >object</B
  13550. ></A
  13551. >
  13552.      </P
  13553. ></LI
  13554. ></UL
  13555. >
  13556.  
  13557.    And finally two special types:
  13558.  
  13559.    <P
  13560. ></P
  13561. ><UL
  13562. ><LI
  13563. ><P
  13564. >       <A
  13565. HREF="#language.types.resource"
  13566. ><B
  13567. CLASS="type"
  13568. >resource</B
  13569. ></A
  13570. >
  13571.      </P
  13572. ></LI
  13573. ><LI
  13574. ><P
  13575. >       <A
  13576. HREF="#language.types.null"
  13577. ><B
  13578. CLASS="type"
  13579. >NULL</B
  13580. ></A
  13581. >
  13582.      </P
  13583. ></LI
  13584. ></UL
  13585. >
  13586.  
  13587.    This manual also introduces some 
  13588.    <A
  13589. HREF="#language.pseudo-types"
  13590. >pseudo-types</A
  13591.    for readability reasons:
  13592.  
  13593.    <P
  13594. ></P
  13595. ><UL
  13596. ><LI
  13597. ><P
  13598. >       <A
  13599. HREF="#language.types.mixed"
  13600. ><B
  13601. CLASS="type"
  13602. >mixed</B
  13603. ></A
  13604. >
  13605.      </P
  13606. ></LI
  13607. ><LI
  13608. ><P
  13609. >       <A
  13610. HREF="#language.types.number"
  13611. ><B
  13612. CLASS="type"
  13613. >number</B
  13614. ></A
  13615. >
  13616.      </P
  13617. ></LI
  13618. ><LI
  13619. ><P
  13620. >       <A
  13621. HREF="#language.types.callback"
  13622. ><B
  13623. CLASS="type"
  13624. >callback</B
  13625. ></A
  13626. >
  13627.      </P
  13628. ></LI
  13629. ></UL
  13630. >
  13631.    You may also find some references to the type "double". Consider
  13632.    double the same as float, the two names exist only for historic
  13633.    reasons. 
  13634.   </P
  13635. ><P
  13636. >    The type of a variable is usually not set by the programmer;
  13637.    rather, it is decided at runtime by PHP depending on the context in
  13638.    which that variable is used.
  13639.   </P
  13640. ><DIV
  13641. CLASS="note"
  13642. ><BLOCKQUOTE
  13643. CLASS="note"
  13644. ><P
  13645. ><B
  13646. >Note: </B
  13647. >
  13648.     If you want to check out the type and value of a certain <A
  13649. HREF="#language.expressions"
  13650. >expression</A
  13651. >, use
  13652.     <A
  13653. HREF="#function.var-dump"
  13654. ><B
  13655. CLASS="function"
  13656. >var_dump()</B
  13657. ></A
  13658. >.
  13659.    </P
  13660. ><P
  13661. ><B
  13662. >Note: </B
  13663. >
  13664.     If you simply want a human-readable representation of the type for
  13665.     debugging, use <A
  13666. HREF="#function.gettype"
  13667. ><B
  13668. CLASS="function"
  13669. >gettype()</B
  13670. ></A
  13671. >. To check for a certain type,
  13672.     do <SPAN
  13673. CLASS="emphasis"
  13674. ><I
  13675. CLASS="emphasis"
  13676. >not</I
  13677. ></SPAN
  13678. > use <A
  13679. HREF="#function.gettype"
  13680. ><B
  13681. CLASS="function"
  13682. >gettype()</B
  13683. ></A
  13684. >, but use the
  13685.     <VAR
  13686. CLASS="literal"
  13687. >is_<VAR
  13688. CLASS="replaceable"
  13689. >type</VAR
  13690. ></VAR
  13691. > functions. Some
  13692.     examples:
  13693.     <DIV
  13694. CLASS="informalexample"
  13695. ><P
  13696. ></P
  13697. ><A
  13698. NAME="AEN2601"
  13699. ></A
  13700. ><TABLE
  13701. BORDER="0"
  13702. BGCOLOR="#E0E0E0"
  13703. CELLPADDING="5"
  13704. ><TR
  13705. ><TD
  13706. ><PRE
  13707. CLASS="php"
  13708. ><?php
  13709. $bool = TRUE;   // a boolean
  13710. $str  = "foo";  // a string
  13711. $int  = 12;     // an integer
  13712.  
  13713. echo gettype($bool); // prints out "boolean"
  13714. echo gettype($str);  // prints out "string"
  13715.  
  13716. // If this is an integer, increment it by four
  13717. if (is_int($int)) {
  13718.     $int += 4;
  13719. }
  13720.  
  13721. // If $bool is a string, print it out
  13722. // (does not print out anything)
  13723. if (is_string($bool)) {
  13724.     echo "String: $bool";
  13725. }
  13726. ?></PRE
  13727. ></TD
  13728. ></TR
  13729. ></TABLE
  13730. ><P
  13731. ></P
  13732. ></DIV
  13733. >
  13734.    </P
  13735. ></BLOCKQUOTE
  13736. ></DIV
  13737. ><P
  13738. >    If you would like to force a variable to be converted to a certain
  13739.    type, you may either <A
  13740. HREF="#language.types.typecasting"
  13741. >cast</A
  13742. > the variable or
  13743.    use the <A
  13744. HREF="#function.settype"
  13745. ><B
  13746. CLASS="function"
  13747. >settype()</B
  13748. ></A
  13749. > function on it.
  13750.   </P
  13751. ><P
  13752. >    Note that a variable may be evaluated with different values in certain
  13753.    situations, depending on what type it is at the time. For more
  13754.    information, see the section on <A
  13755. HREF="#language.types.type-juggling"
  13756. >Type Juggling</A
  13757. >.  Also, you 
  13758.    may be interested in viewing 
  13759.    <A
  13760. HREF="#types.comparisons"
  13761. >the type comparison tables</A
  13762. >,
  13763.    as they show examples of various type related comparisons.
  13764.   </P
  13765. ></DIV
  13766. ><DIV
  13767. CLASS="sect1"
  13768. ><HR><H2
  13769. CLASS="sect1"
  13770. ><A
  13771. NAME="language.types.boolean"
  13772. >Booleans</A
  13773. ></H2
  13774. ><P
  13775. >      This is the easiest type. A <A
  13776. HREF="#language.types.boolean"
  13777. ><B
  13778. CLASS="type"
  13779. >boolean</B
  13780. ></A
  13781. > expresses a 
  13782.      truth value. It can be either <TT
  13783. CLASS="constant"
  13784. ><B
  13785. >TRUE</B
  13786. ></TT
  13787. > or <TT
  13788. CLASS="constant"
  13789. ><B
  13790. >FALSE</B
  13791. ></TT
  13792. >. 
  13793.     </P
  13794. ><DIV
  13795. CLASS="note"
  13796. ><BLOCKQUOTE
  13797. CLASS="note"
  13798. ><P
  13799. ><B
  13800. >Note: </B
  13801. >
  13802.       The boolean type was introduced in PHP 4.
  13803.      </P
  13804. ></BLOCKQUOTE
  13805. ></DIV
  13806. ><DIV
  13807. CLASS="sect2"
  13808. ><HR><H3
  13809. CLASS="sect2"
  13810. ><A
  13811. NAME="language.types.boolean.syntax"
  13812. >Syntax</A
  13813. ></H3
  13814. ><P
  13815. >       To specify a boolean literal, use either the keyword <TT
  13816. CLASS="constant"
  13817. ><B
  13818. >TRUE</B
  13819. ></TT
  13820.       or <TT
  13821. CLASS="constant"
  13822. ><B
  13823. >FALSE</B
  13824. ></TT
  13825. >. Both are case-insensitive.
  13826.       <DIV
  13827. CLASS="informalexample"
  13828. ><P
  13829. ></P
  13830. ><A
  13831. NAME="AEN2622"
  13832. ></A
  13833. ><TABLE
  13834. BORDER="0"
  13835. BGCOLOR="#E0E0E0"
  13836. CELLPADDING="5"
  13837. ><TR
  13838. ><TD
  13839. ><PRE
  13840. CLASS="php"
  13841. ><?php
  13842. $foo = True; // assign the value TRUE to $foo
  13843. ?></PRE
  13844. ></TD
  13845. ></TR
  13846. ></TABLE
  13847. ><P
  13848. ></P
  13849. ></DIV
  13850. >
  13851.      </P
  13852. ><P
  13853. >       Usually you 
  13854.       use some kind of <A
  13855. HREF="#language.operators"
  13856. >operator</A
  13857. >
  13858.       which returns a <A
  13859. HREF="#language.types.boolean"
  13860. ><B
  13861. CLASS="type"
  13862. >boolean</B
  13863. ></A
  13864. > value, and then pass it 
  13865.       on to a <A
  13866. HREF="#language.control-structures"
  13867. >control
  13868.       structure</A
  13869. >.
  13870.       <DIV
  13871. CLASS="informalexample"
  13872. ><P
  13873. ></P
  13874. ><A
  13875. NAME="AEN2628"
  13876. ></A
  13877. ><TABLE
  13878. BORDER="0"
  13879. BGCOLOR="#E0E0E0"
  13880. CELLPADDING="5"
  13881. ><TR
  13882. ><TD
  13883. ><PRE
  13884. CLASS="php"
  13885. ><?php
  13886. // == is an operator which test
  13887. // equality and returns a boolean
  13888. if ($action == "show_version") {
  13889.     echo "The version is 1.23";
  13890. }
  13891.  
  13892. // this is not necessary...
  13893. if ($show_separators == TRUE) {
  13894.     echo "<hr>\n";
  13895. }
  13896.  
  13897. // ...because you can simply type
  13898. if ($show_separators) {
  13899.     echo "<hr>\n";
  13900. }
  13901. ?></PRE
  13902. ></TD
  13903. ></TR
  13904. ></TABLE
  13905. ><P
  13906. ></P
  13907. ></DIV
  13908. >
  13909.      </P
  13910. ></DIV
  13911. ><DIV
  13912. CLASS="sect2"
  13913. ><HR><H3
  13914. CLASS="sect2"
  13915. ><A
  13916. NAME="language.types.boolean.casting"
  13917. >Converting to boolean</A
  13918. ></H3
  13919. ><P
  13920. >        To explicitly convert a value to <A
  13921. HREF="#language.types.boolean"
  13922. ><B
  13923. CLASS="type"
  13924. >boolean</B
  13925. ></A
  13926. >, use either
  13927.        the <VAR
  13928. CLASS="literal"
  13929. >(bool)</VAR
  13930. > or the <VAR
  13931. CLASS="literal"
  13932. >(boolean)</VAR
  13933. > cast.
  13934.        However, in most cases you do not need to use the cast, since a value
  13935.        will be automatically converted if an operator, function or 
  13936.        control structure requires a <A
  13937. HREF="#language.types.boolean"
  13938. ><B
  13939. CLASS="type"
  13940. >boolean</B
  13941. ></A
  13942. > argument.
  13943.       </P
  13944. ><P
  13945. >        See also <A
  13946. HREF="#language.types.type-juggling"
  13947. >Type Juggling</A
  13948. >.
  13949.       </P
  13950. ><P
  13951. >        When converting to <A
  13952. HREF="#language.types.boolean"
  13953. ><B
  13954. CLASS="type"
  13955. >boolean</B
  13956. ></A
  13957. >, the following values 
  13958.        are considered <TT
  13959. CLASS="constant"
  13960. ><B
  13961. >FALSE</B
  13962. ></TT
  13963. >:
  13964.   
  13965.        <P
  13966. ></P
  13967. ><UL
  13968. ><LI
  13969. ><P
  13970. >the <A
  13971. HREF="#language.types.boolean"
  13972. >boolean</A
  13973.           <TT
  13974. CLASS="constant"
  13975. ><B
  13976. >FALSE</B
  13977. ></TT
  13978. > itself</P
  13979. ></LI
  13980. ><LI
  13981. ><P
  13982. >the <A
  13983. HREF="#language.types.integer"
  13984. >integer</A
  13985. > 0 (zero) </P
  13986. ></LI
  13987. ><LI
  13988. ><P
  13989. >the <A
  13990. HREF="#language.types.float"
  13991. >float</A
  13992.          0.0 (zero) </P
  13993. ></LI
  13994. ><LI
  13995. ><P
  13996. >the empty <A
  13997. HREF="#language.types.string"
  13998. >string</A
  13999. >, and the <A
  14000. HREF="#language.types.string"
  14001. >string</A
  14002. >
  14003.            "0"</P
  14004. ></LI
  14005. ><LI
  14006. ><P
  14007. >an <A
  14008. HREF="#language.types.array"
  14009. >array</A
  14010.          with zero elements</P
  14011. ></LI
  14012. ><LI
  14013. ><P
  14014. >an <A
  14015. HREF="#language.types.object"
  14016. >object</A
  14017.          with zero member variables (PHP 4 only)</P
  14018. ></LI
  14019. ><LI
  14020. ><P
  14021. >the special type <A
  14022. HREF="#language.types.null"
  14023. >NULL</A
  14024. > (including unset variables)
  14025.          </P
  14026. ></LI
  14027. ></UL
  14028. >
  14029.        
  14030.        Every other value is considered <TT
  14031. CLASS="constant"
  14032. ><B
  14033. >TRUE</B
  14034. ></TT
  14035. > (including any 
  14036.        <A
  14037. HREF="#language.types.resource"
  14038. >resource</A
  14039. >).
  14040.        <DIV
  14041. CLASS="warning"
  14042. ><P
  14043. ></P
  14044. ><TABLE
  14045. CLASS="warning"
  14046. BORDER="1"
  14047. WIDTH="100%"
  14048. ><TR
  14049. ><TD
  14050. ALIGN="CENTER"
  14051. ><B
  14052. >Warning</B
  14053. ></TD
  14054. ></TR
  14055. ><TR
  14056. ><TD
  14057. ALIGN="LEFT"
  14058. ><P
  14059. >          <VAR
  14060. CLASS="literal"
  14061. >-1</VAR
  14062. > is considered  
  14063.          <TT
  14064. CLASS="constant"
  14065. ><B
  14066. >TRUE</B
  14067. ></TT
  14068. >, like any other non-zero (whether negative
  14069.          or positive) number!
  14070.         </P
  14071. ></TD
  14072. ></TR
  14073. ></TABLE
  14074. ></DIV
  14075. >
  14076.        <DIV
  14077. CLASS="informalexample"
  14078. ><P
  14079. ></P
  14080. ><A
  14081. NAME="AEN2672"
  14082. ></A
  14083. ><TABLE
  14084. BORDER="0"
  14085. BGCOLOR="#E0E0E0"
  14086. CELLPADDING="5"
  14087. ><TR
  14088. ><TD
  14089. ><PRE
  14090. CLASS="php"
  14091. ><?php
  14092. var_dump((bool) "");        // bool(false)
  14093. var_dump((bool) 1);         // bool(true)
  14094. var_dump((bool) -2);        // bool(true)
  14095. var_dump((bool) "foo");     // bool(true)
  14096. var_dump((bool) 2.3e5);     // bool(true)
  14097. var_dump((bool) array(12)); // bool(true)
  14098. var_dump((bool) array());   // bool(false)
  14099. var_dump((bool) "false");   // bool(true)
  14100. ?></PRE
  14101. ></TD
  14102. ></TR
  14103. ></TABLE
  14104. ><P
  14105. ></P
  14106. ></DIV
  14107. >
  14108.       </P
  14109. ></DIV
  14110. ></DIV
  14111. ><DIV
  14112. CLASS="sect1"
  14113. ><HR><H2
  14114. CLASS="sect1"
  14115. ><A
  14116. NAME="language.types.integer"
  14117. >Integers</A
  14118. ></H2
  14119. ><P
  14120. >      An <A
  14121. HREF="#language.types.integer"
  14122. ><B
  14123. CLASS="type"
  14124. >integer</B
  14125. ></A
  14126. > is a number of the set 
  14127.      Z = {..., -2, -1, 0, 1, 2, ...}.
  14128.     </P
  14129. ><P
  14130. >      See also:
  14131.      <A
  14132. HREF="#ref.gmp"
  14133. >Arbitrary length integer / GMP</A
  14134. >,
  14135.      <A
  14136. HREF="#language.types.float"
  14137. >Floating point numbers</A
  14138. >, and
  14139.      <A
  14140. HREF="#ref.bc"
  14141. >Arbitrary precision / BCMath</A
  14142. >
  14143.     </P
  14144. ><DIV
  14145. CLASS="sect2"
  14146. ><HR><H3
  14147. CLASS="sect2"
  14148. ><A
  14149. NAME="language.types.integer.syntax"
  14150. >Syntax</A
  14151. ></H3
  14152. ><P
  14153. >       Integers can be specified in decimal (10-based), hexadecimal (16-based)
  14154.       or octal (8-based) notation, optionally preceded by a sign (- or +).
  14155.      </P
  14156. ><P
  14157. >       If you use the octal notation, you must precede the number with a 
  14158.       <VAR
  14159. CLASS="literal"
  14160. >0</VAR
  14161. > (zero), to use hexadecimal notation precede
  14162.       the number with <VAR
  14163. CLASS="literal"
  14164. >0x</VAR
  14165. >.
  14166.       <TABLE
  14167. WIDTH="100%"
  14168. BORDER="0"
  14169. CELLPADDING="0"
  14170. CELLSPACING="0"
  14171. CLASS="EXAMPLE"
  14172. ><TR
  14173. ><TD
  14174. ><DIV
  14175. CLASS="example"
  14176. ><A
  14177. NAME="AEN2688"
  14178. ></A
  14179. ><P
  14180. ><B
  14181. >Example 11-1. Integer literals</B
  14182. ></P
  14183. ><TABLE
  14184. BORDER="0"
  14185. BGCOLOR="#E0E0E0"
  14186. CELLPADDING="5"
  14187. ><TR
  14188. ><TD
  14189. ><PRE
  14190. CLASS="php"
  14191. ><?php
  14192. $a = 1234; // decimal number
  14193. $a = -123; // a negative number
  14194. $a = 0123; // octal number (equivalent to 83 decimal)
  14195. $a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
  14196. ?></PRE
  14197. ></TD
  14198. ></TR
  14199. ></TABLE
  14200. ></DIV
  14201. ></TD
  14202. ></TR
  14203. ></TABLE
  14204. >
  14205.       Formally the possible structure for integer literals is:
  14206.       <DIV
  14207. CLASS="informalexample"
  14208. ><P
  14209. ></P
  14210. ><A
  14211. NAME="AEN2691"
  14212. ></A
  14213. ><TABLE
  14214. BORDER="0"
  14215. BGCOLOR="#E0E0E0"
  14216. CELLPADDING="5"
  14217. ><TR
  14218. ><TD
  14219. ><PRE
  14220. CLASS="programlisting"
  14221. >decimal     : [1-9][0-9]*
  14222.             | 0
  14223.  
  14224. hexadecimal : 0[xX][0-9a-fA-F]+
  14225.  
  14226. octal       : 0[0-7]+
  14227.  
  14228. integer     : [+-]?decimal
  14229.             | [+-]?hexadecimal
  14230.             | [+-]?octal</PRE
  14231. ></TD
  14232. ></TR
  14233. ></TABLE
  14234. ><P
  14235. ></P
  14236. ></DIV
  14237. >
  14238.       The size of an integer is platform-dependent, although a 
  14239.       maximum value of about two billion is the usual value 
  14240.       (that's 32 bits signed). PHP does not support unsigned
  14241.       integers.
  14242.      </P
  14243. ><DIV
  14244. CLASS="warning"
  14245. ><P
  14246. ></P
  14247. ><TABLE
  14248. CLASS="warning"
  14249. BORDER="1"
  14250. WIDTH="100%"
  14251. ><TR
  14252. ><TD
  14253. ALIGN="CENTER"
  14254. ><B
  14255. >Warning</B
  14256. ></TD
  14257. ></TR
  14258. ><TR
  14259. ><TD
  14260. ALIGN="LEFT"
  14261. ><P
  14262. >        If invalid digit is passed to octal integer (i.e. 8 or 9), the rest of
  14263.        the number is ignored.
  14264.        <TABLE
  14265. WIDTH="100%"
  14266. BORDER="0"
  14267. CELLPADDING="0"
  14268. CELLSPACING="0"
  14269. CLASS="EXAMPLE"
  14270. ><TR
  14271. ><TD
  14272. ><DIV
  14273. CLASS="example"
  14274. ><A
  14275. NAME="AEN2695"
  14276. ></A
  14277. ><P
  14278. ><B
  14279. >Example 11-2. Octal weirdness</B
  14280. ></P
  14281. ><TABLE
  14282. BORDER="0"
  14283. BGCOLOR="#E0E0E0"
  14284. CELLPADDING="5"
  14285. ><TR
  14286. ><TD
  14287. ><PRE
  14288. CLASS="php"
  14289. ><?php
  14290. var_dump(01090); // 010 octal = 8 decimal
  14291. ?></PRE
  14292. ></TD
  14293. ></TR
  14294. ></TABLE
  14295. ></DIV
  14296. ></TD
  14297. ></TR
  14298. ></TABLE
  14299. >
  14300.       </P
  14301. ></TD
  14302. ></TR
  14303. ></TABLE
  14304. ></DIV
  14305. ></DIV
  14306. ><DIV
  14307. CLASS="sect2"
  14308. ><HR><H3
  14309. CLASS="sect2"
  14310. ><A
  14311. NAME="language.types.integer.overflow"
  14312. >Integer overflow</A
  14313. ></H3
  14314. ><P
  14315. >       If you specify a number beyond the bounds of the <A
  14316. HREF="#language.types.integer"
  14317. ><B
  14318. CLASS="type"
  14319. >integer</B
  14320. ></A
  14321. >
  14322.       type, it will be interpreted as a <A
  14323. HREF="#language.types.float"
  14324. ><B
  14325. CLASS="type"
  14326. >float</B
  14327. ></A
  14328. > instead. Also, if
  14329.       you perform an operation that results in a number beyond the bounds of
  14330.       the <A
  14331. HREF="#language.types.integer"
  14332. ><B
  14333. CLASS="type"
  14334. >integer</B
  14335. ></A
  14336. > type, a <A
  14337. HREF="#language.types.float"
  14338. ><B
  14339. CLASS="type"
  14340. >float</B
  14341. ></A
  14342. > will be returned
  14343.       instead.
  14344.  
  14345.       <DIV
  14346. CLASS="informalexample"
  14347. ><P
  14348. ></P
  14349. ><A
  14350. NAME="AEN2705"
  14351. ></A
  14352. ><TABLE
  14353. BORDER="0"
  14354. BGCOLOR="#E0E0E0"
  14355. CELLPADDING="5"
  14356. ><TR
  14357. ><TD
  14358. ><PRE
  14359. CLASS="php"
  14360. ><?php
  14361. $large_number =  2147483647;
  14362. var_dump($large_number);
  14363. // output: int(2147483647)
  14364.  
  14365. $large_number =  2147483648;
  14366. var_dump($large_number);
  14367. // output: float(2147483648)
  14368.  
  14369. // this goes also for hexadecimal specified integers:
  14370. var_dump( 0x80000000 );
  14371. // output: float(2147483648)
  14372.  
  14373. $million = 1000000;
  14374. $large_number =  50000 * $million;
  14375. var_dump($large_number);
  14376. // output: float(50000000000)
  14377. ?></PRE
  14378. ></TD
  14379. ></TR
  14380. ></TABLE
  14381. ><P
  14382. ></P
  14383. ></DIV
  14384. >
  14385.       <DIV
  14386. CLASS="warning"
  14387. ><P
  14388. ></P
  14389. ><TABLE
  14390. CLASS="warning"
  14391. BORDER="1"
  14392. WIDTH="100%"
  14393. ><TR
  14394. ><TD
  14395. ALIGN="CENTER"
  14396. ><B
  14397. >Warning</B
  14398. ></TD
  14399. ></TR
  14400. ><TR
  14401. ><TD
  14402. ALIGN="LEFT"
  14403. ><P
  14404. >         Unfortunately, there was a bug in PHP so that this
  14405.         does not always work correctly when there are negative numbers
  14406.         involved. For example: when you do <VAR
  14407. CLASS="literal"
  14408. >-50000 *
  14409.         $million</VAR
  14410. >, the result will be
  14411.         <VAR
  14412. CLASS="literal"
  14413. >-429496728</VAR
  14414. >. However, when both operands are
  14415.         positive there is no problem.
  14416.        </P
  14417. ><P
  14418. >         This is solved in PHP 4.1.0.
  14419.        </P
  14420. ></TD
  14421. ></TR
  14422. ></TABLE
  14423. ></DIV
  14424. >
  14425.      </P
  14426. ><P
  14427. >       There is no integer division operator in PHP.
  14428.       <VAR
  14429. CLASS="literal"
  14430. >1/2</VAR
  14431. > yields the <A
  14432. HREF="#language.types.float"
  14433. ><B
  14434. CLASS="type"
  14435. >float</B
  14436. ></A
  14437. >
  14438.       <VAR
  14439. CLASS="literal"
  14440. >0.5</VAR
  14441. >. You can cast the value to
  14442.       an integer to always round it downwards, or you can
  14443.       use the <A
  14444. HREF="#function.round"
  14445. ><B
  14446. CLASS="function"
  14447. >round()</B
  14448. ></A
  14449. > function.
  14450.       <DIV
  14451. CLASS="informalexample"
  14452. ><P
  14453. ></P
  14454. ><A
  14455. NAME="AEN2717"
  14456. ></A
  14457. ><TABLE
  14458. BORDER="0"
  14459. BGCOLOR="#E0E0E0"
  14460. CELLPADDING="5"
  14461. ><TR
  14462. ><TD
  14463. ><PRE
  14464. CLASS="php"
  14465. ><?php
  14466. var_dump(25/7);         // float(3.5714285714286) 
  14467. var_dump((int) (25/7)); // int(3)
  14468. var_dump(round(25/7));  // float(4) 
  14469. ?></PRE
  14470. ></TD
  14471. ></TR
  14472. ></TABLE
  14473. ><P
  14474. ></P
  14475. ></DIV
  14476. >
  14477.      </P
  14478. ></DIV
  14479. ><DIV
  14480. CLASS="sect2"
  14481. ><HR><H3
  14482. CLASS="sect2"
  14483. ><A
  14484. NAME="language.types.integer.casting"
  14485. >Converting to integer</A
  14486. ></H3
  14487. ><P
  14488. >        To explicitly convert a value to <A
  14489. HREF="#language.types.integer"
  14490. ><B
  14491. CLASS="type"
  14492. >integer</B
  14493. ></A
  14494. >, use either
  14495.        the <VAR
  14496. CLASS="literal"
  14497. >(int)</VAR
  14498. > or the <VAR
  14499. CLASS="literal"
  14500. >(integer)</VAR
  14501. > cast.
  14502.        However, in most cases you do not need to use the cast, since a value
  14503.        will be automatically converted if an operator, function or 
  14504.        control structure requires an <A
  14505. HREF="#language.types.integer"
  14506. ><B
  14507. CLASS="type"
  14508. >integer</B
  14509. ></A
  14510. > argument.
  14511.        You can also convert a value to integer with the function
  14512.        <A
  14513. HREF="#function.intval"
  14514. ><B
  14515. CLASS="function"
  14516. >intval()</B
  14517. ></A
  14518. >.
  14519.       </P
  14520. ><P
  14521. >        See also <A
  14522. HREF="#language.types.type-juggling"
  14523. >type-juggling</A
  14524. >.
  14525.       </P
  14526. ><DIV
  14527. CLASS="sect3"
  14528. ><HR><H4
  14529. CLASS="sect3"
  14530. ><A
  14531. NAME="language.types.integer.casting.from-boolean"
  14532. >From <A
  14533. HREF="#language.types.boolean"
  14534. >booleans</A
  14535. ></A
  14536. ></H4
  14537. ><P
  14538. >         <TT
  14539. CLASS="constant"
  14540. ><B
  14541. >FALSE</B
  14542. ></TT
  14543. > will yield 
  14544.         <VAR
  14545. CLASS="literal"
  14546. >0</VAR
  14547. > (zero), and <TT
  14548. CLASS="constant"
  14549. ><B
  14550. >TRUE</B
  14551. ></TT
  14552.         will yield <VAR
  14553. CLASS="literal"
  14554. >1</VAR
  14555. > (one).
  14556.        </P
  14557. ></DIV
  14558. ><DIV
  14559. CLASS="sect3"
  14560. ><HR><H4
  14561. CLASS="sect3"
  14562. ><A
  14563. NAME="language.types.integer.casting.from-float"
  14564. >From <A
  14565. HREF="#language.types.float"
  14566. >floating point numbers</A
  14567. ></A
  14568. ></H4
  14569. ><P
  14570. >         When converting from float to integer, the number will
  14571.         be rounded <SPAN
  14572. CLASS="emphasis"
  14573. ><I
  14574. CLASS="emphasis"
  14575. >towards zero</I
  14576. ></SPAN
  14577. >.
  14578.        </P
  14579. ><P
  14580. >         If the float is beyond the boundaries of integer
  14581.         (usually <VAR
  14582. CLASS="literal"
  14583. >+/- 2.15e+9 = 2^31</VAR
  14584. >), 
  14585.         the result is undefined, since the float hasn't
  14586.         got enough precision to give an exact integer result.
  14587.         No warning, not even a notice will be issued in this 
  14588.         case!
  14589.        </P
  14590. ><DIV
  14591. CLASS="warning"
  14592. ><P
  14593. ></P
  14594. ><TABLE
  14595. CLASS="warning"
  14596. BORDER="1"
  14597. WIDTH="100%"
  14598. ><TR
  14599. ><TD
  14600. ALIGN="CENTER"
  14601. ><B
  14602. >Warning</B
  14603. ></TD
  14604. ></TR
  14605. ><TR
  14606. ><TD
  14607. ALIGN="LEFT"
  14608. ><P
  14609. >         Never cast an unknown fraction to <A
  14610. HREF="#language.types.integer"
  14611. ><B
  14612. CLASS="type"
  14613. >integer</B
  14614. ></A
  14615. >, as this can
  14616.         sometimes lead to unexpected results.
  14617.         <DIV
  14618. CLASS="informalexample"
  14619. ><P
  14620. ></P
  14621. ><A
  14622. NAME="AEN2747"
  14623. ></A
  14624. ><TABLE
  14625. BORDER="0"
  14626. BGCOLOR="#E0E0E0"
  14627. CELLPADDING="5"
  14628. ><TR
  14629. ><TD
  14630. ><PRE
  14631. CLASS="php"
  14632. ><?php
  14633. echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
  14634. ?></PRE
  14635. ></TD
  14636. ></TR
  14637. ></TABLE
  14638. ><P
  14639. ></P
  14640. ></DIV
  14641. >
  14642.         
  14643.         See for more information the <A
  14644. HREF="#warn.float-precision"
  14645. >warning 
  14646.         about float-precision</A
  14647. >.
  14648.        </P
  14649. ></TD
  14650. ></TR
  14651. ></TABLE
  14652. ></DIV
  14653. ></DIV
  14654. ><DIV
  14655. CLASS="sect3"
  14656. ><HR><H4
  14657. CLASS="sect3"
  14658. ><A
  14659. NAME="language.types.integer.casting.from-string"
  14660. >From strings</A
  14661. ></H4
  14662. ><P
  14663. >         See <A
  14664. HREF="#language.types.string.conversion"
  14665. >String 
  14666.         conversion to numbers</A
  14667. >
  14668.        </P
  14669. ></DIV
  14670. ><DIV
  14671. CLASS="sect3"
  14672. ><HR><H4
  14673. CLASS="sect3"
  14674. ><A
  14675. NAME="language.types.integer.casting.from-other"
  14676. >From other types</A
  14677. ></H4
  14678. ><P
  14679. >         <DIV
  14680. CLASS="caution"
  14681. ><P
  14682. ></P
  14683. ><TABLE
  14684. CLASS="caution"
  14685. BORDER="1"
  14686. WIDTH="100%"
  14687. ><TR
  14688. ><TD
  14689. ALIGN="CENTER"
  14690. ><B
  14691. >Caution</B
  14692. ></TD
  14693. ></TR
  14694. ><TR
  14695. ><TD
  14696. ALIGN="LEFT"
  14697. ><P
  14698. >           Behaviour of converting to integer is undefined for other
  14699.           types. Currently, the behaviour is the same as if the value
  14700.           was first <A
  14701. HREF="#language.types.boolean.casting"
  14702. >converted to boolean</A
  14703. >. However, do
  14704.           <SPAN
  14705. CLASS="emphasis"
  14706. ><I
  14707. CLASS="emphasis"
  14708. >not</I
  14709. ></SPAN
  14710. > rely on this behaviour, as it can
  14711.           change without notice.
  14712.          </P
  14713. ></TD
  14714. ></TR
  14715. ></TABLE
  14716. ></DIV
  14717. >
  14718.        </P
  14719. ></DIV
  14720. ></DIV
  14721. ></DIV
  14722. ><DIV
  14723. CLASS="sect1"
  14724. ><HR><H2
  14725. CLASS="sect1"
  14726. ><A
  14727. NAME="language.types.float"
  14728. >Floating point numbers</A
  14729. ></H2
  14730. ><P
  14731. >     Floating point numbers (AKA "floats", "doubles" or "real numbers") can be 
  14732.     specified using any of the following syntaxes: 
  14733.     <DIV
  14734. CLASS="informalexample"
  14735. ><P
  14736. ></P
  14737. ><A
  14738. NAME="AEN2764"
  14739. ></A
  14740. ><TABLE
  14741. BORDER="0"
  14742. BGCOLOR="#E0E0E0"
  14743. CELLPADDING="5"
  14744. ><TR
  14745. ><TD
  14746. ><PRE
  14747. CLASS="php"
  14748. ><?php
  14749. $a = 1.234; 
  14750. $b = 1.2e3; 
  14751. $c = 7E-10;
  14752. ?></PRE
  14753. ></TD
  14754. ></TR
  14755. ></TABLE
  14756. ><P
  14757. ></P
  14758. ></DIV
  14759. >
  14760.     Formally:
  14761.     <DIV
  14762. CLASS="informalexample"
  14763. ><P
  14764. ></P
  14765. ><A
  14766. NAME="AEN2766"
  14767. ></A
  14768. ><TABLE
  14769. BORDER="0"
  14770. BGCOLOR="#E0E0E0"
  14771. CELLPADDING="5"
  14772. ><TR
  14773. ><TD
  14774. ><PRE
  14775. CLASS="programlisting"
  14776. >LNUM          [0-9]+
  14777. DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
  14778. EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})</PRE
  14779. ></TD
  14780. ></TR
  14781. ></TABLE
  14782. ><P
  14783. ></P
  14784. ></DIV
  14785. >
  14786.     The size of a float is platform-dependent, 
  14787.     although a maximum of ~1.8e308 with a precision of roughly 14 
  14788.     decimal digits is a common value (that's 64 bit IEEE format).
  14789.    </P
  14790. ><DIV
  14791. CLASS="warning"
  14792. ><P
  14793. ></P
  14794. ><TABLE
  14795. CLASS="warning"
  14796. BORDER="1"
  14797. WIDTH="100%"
  14798. ><TR
  14799. ><TD
  14800. ALIGN="CENTER"
  14801. ><B
  14802. ><A
  14803. NAME="warn.float-precision"
  14804. ></A
  14805. >Floating point precision</B
  14806. ></TD
  14807. ></TR
  14808. ><TR
  14809. ><TD
  14810. ALIGN="LEFT"
  14811. ><P
  14812. >      It is quite usual that simple decimal fractions like
  14813.      <VAR
  14814. CLASS="literal"
  14815. >0.1</VAR
  14816. > or <VAR
  14817. CLASS="literal"
  14818. >0.7</VAR
  14819. > cannot be
  14820.      converted into their internal binary counterparts without a
  14821.      little loss of precision. This can lead to confusing results: for
  14822.      example, <VAR
  14823. CLASS="literal"
  14824. >floor((0.1+0.7)*10)</VAR
  14825. > will usually
  14826.      return <VAR
  14827. CLASS="literal"
  14828. >7</VAR
  14829. > instead of the expected
  14830.      <VAR
  14831. CLASS="literal"
  14832. >8</VAR
  14833. > as the result of the internal representation
  14834.      really being something like <VAR
  14835. CLASS="literal"
  14836. >7.9999999999...</VAR
  14837. >.
  14838.     </P
  14839. ><P
  14840. >      This is related to the fact that it is impossible to exactly
  14841.      express some fractions in decimal notation with a finite number
  14842.      of digits. For instance, <VAR
  14843. CLASS="literal"
  14844. >1/3</VAR
  14845. > in decimal form
  14846.      becomes <VAR
  14847. CLASS="literal"
  14848. >0.3333333. . .</VAR
  14849. >.
  14850.     </P
  14851. ><P
  14852. >      So never trust floating number results to the last digit and
  14853.      never compare floating point numbers for equality. If you really
  14854.      need higher precision, you should use the <A
  14855. HREF="#ref.bc"
  14856. >arbitrary precision math functions</A
  14857. >
  14858.      or <A
  14859. HREF="#ref.gmp"
  14860. >gmp</A
  14861. > functions instead.
  14862.     </P
  14863. ></TD
  14864. ></TR
  14865. ></TABLE
  14866. ></DIV
  14867. ><DIV
  14868. CLASS="sect2"
  14869. ><HR><H3
  14870. CLASS="sect2"
  14871. ><A
  14872. NAME="language.types.float.casting"
  14873. >Converting to float</A
  14874. ></H3
  14875. ><P
  14876. >      For information on when and how strings are converted to floats,
  14877.      see the section titled <A
  14878. HREF="#language.types.string.conversion"
  14879. >String
  14880.      conversion to numbers</A
  14881. >. For values of other types, the conversion
  14882.      is the same as if the value would have been converted to integer
  14883.      and then to float. See the <A
  14884. HREF="#language.types.integer.casting"
  14885. >Converting
  14886.      to integer</A
  14887. > section for more information.
  14888.      As of PHP 5, notice is thrown if you try to convert object to float.
  14889.     </P
  14890. ></DIV
  14891. ></DIV
  14892. ><DIV
  14893. CLASS="sect1"
  14894. ><HR><H2
  14895. CLASS="sect1"
  14896. ><A
  14897. NAME="language.types.string"
  14898. >Strings</A
  14899. ></H2
  14900. ><P
  14901. >     A <A
  14902. HREF="#language.types.string"
  14903. ><B
  14904. CLASS="type"
  14905. >string</B
  14906. ></A
  14907. > is series of characters. In PHP,
  14908.     a character is the same as a byte, that is, there are exactly
  14909.     256 different characters possible. This also implies that PHP
  14910.     has no native support of Unicode. See <A
  14911. HREF="#function.utf8-encode"
  14912. ><B
  14913. CLASS="function"
  14914. >utf8_encode()</B
  14915. ></A
  14916. >
  14917.     and <A
  14918. HREF="#function.utf8-decode"
  14919. ><B
  14920. CLASS="function"
  14921. >utf8_decode()</B
  14922. ></A
  14923. > for some Unicode support.
  14924.    </P
  14925. ><DIV
  14926. CLASS="note"
  14927. ><BLOCKQUOTE
  14928. CLASS="note"
  14929. ><P
  14930. ><B
  14931. >Note: </B
  14932. >
  14933.      It is no problem for a string to become very large. 
  14934.      There is no practical bound to the size
  14935.      of strings imposed by PHP, so there is no reason at all
  14936.      to worry about long strings.
  14937.     </P
  14938. ></BLOCKQUOTE
  14939. ></DIV
  14940. ><DIV
  14941. CLASS="sect2"
  14942. ><HR><H3
  14943. CLASS="sect2"
  14944. ><A
  14945. NAME="language.types.string.syntax"
  14946. >Syntax</A
  14947. ></H3
  14948. ><P
  14949. >      A string literal can be specified in three different
  14950.      ways.
  14951.      <P
  14952. ></P
  14953. ><UL
  14954. ><LI
  14955. ><P
  14956. >         <A
  14957. HREF="#language.types.string.syntax.single"
  14958. >single quoted</A
  14959. >
  14960.        </P
  14961. ></LI
  14962. ><LI
  14963. ><P
  14964. >         <A
  14965. HREF="#language.types.string.syntax.double"
  14966. >double quoted</A
  14967. >
  14968.        </P
  14969. ></LI
  14970. ><LI
  14971. ><P
  14972. >         <A
  14973. HREF="#language.types.string.syntax.heredoc"
  14974. >heredoc syntax</A
  14975. >
  14976.        </P
  14977. ></LI
  14978. ></UL
  14979. >
  14980.     </P
  14981. ><DIV
  14982. CLASS="sect3"
  14983. ><HR><H4
  14984. CLASS="sect3"
  14985. ><A
  14986. NAME="language.types.string.syntax.single"
  14987. >Single quoted</A
  14988. ></H4
  14989. ><P
  14990. >       The easiest way to specify a simple string is to
  14991.       enclose it in single quotes (the character <VAR
  14992. CLASS="literal"
  14993. >'</VAR
  14994. >). 
  14995.      </P
  14996. ><P
  14997. >       To specify a literal single
  14998.       quote, you will need to escape it with a backslash
  14999.       (<VAR
  15000. CLASS="literal"
  15001. >\</VAR
  15002. >), like in many other languages.
  15003.       If a backslash needs to occur before a single quote or at 
  15004.       the end of the string, you need to double it.
  15005.       Note that if you try to escape any
  15006.       other character, the backslash will also be printed! So
  15007.       usually there is no need to escape the backslash itself.
  15008.       <DIV
  15009. CLASS="note"
  15010. ><BLOCKQUOTE
  15011. CLASS="note"
  15012. ><P
  15013. ><B
  15014. >Note: </B
  15015. >
  15016.         In PHP 3, a warning will
  15017.         be issued at the <VAR
  15018. CLASS="literal"
  15019. >E_NOTICE</VAR
  15020. > level when this
  15021.         happens.
  15022.        </P
  15023. ></BLOCKQUOTE
  15024. ></DIV
  15025. >
  15026.       <DIV
  15027. CLASS="note"
  15028. ><BLOCKQUOTE
  15029. CLASS="note"
  15030. ><P
  15031. ><B
  15032. >Note: </B
  15033. >
  15034.         Unlike the two other syntaxes, <A
  15035. HREF="#language.variables"
  15036. >variables</A
  15037. > and escape sequences
  15038.         for special characters will <SPAN
  15039. CLASS="emphasis"
  15040. ><I
  15041. CLASS="emphasis"
  15042. >not</I
  15043. ></SPAN
  15044. > be expanded 
  15045.         when they occur in single quoted strings.
  15046.        </P
  15047. ></BLOCKQUOTE
  15048. ></DIV
  15049. >
  15050.       <DIV
  15051. CLASS="informalexample"
  15052. ><P
  15053. ></P
  15054. ><A
  15055. NAME="AEN2822"
  15056. ></A
  15057. ><TABLE
  15058. BORDER="0"
  15059. BGCOLOR="#E0E0E0"
  15060. CELLPADDING="5"
  15061. ><TR
  15062. ><TD
  15063. ><PRE
  15064. CLASS="php"
  15065. ><?php
  15066. echo 'this is a simple string';
  15067.  
  15068. echo 'You can also have embedded newlines in 
  15069. strings this way as it is
  15070. okay to do';
  15071.  
  15072. // Outputs: Arnold once said: "I'll be back"
  15073. echo 'Arnold once said: "I\'ll be back"';
  15074.  
  15075. // Outputs: You deleted C:\*.*?
  15076. echo 'You deleted C:\\*.*?';
  15077.  
  15078. // Outputs: You deleted C:\*.*?
  15079. echo 'You deleted C:\*.*?';
  15080.  
  15081. // Outputs: This will not expand: \n a newline
  15082. echo 'This will not expand: \n a newline';
  15083.  
  15084. // Outputs: Variables do not $expand $either
  15085. echo 'Variables do not $expand $either';
  15086. ?></PRE
  15087. ></TD
  15088. ></TR
  15089. ></TABLE
  15090. ><P
  15091. ></P
  15092. ></DIV
  15093. >
  15094.      </P
  15095. ></DIV
  15096. ><DIV
  15097. CLASS="sect3"
  15098. ><HR><H4
  15099. CLASS="sect3"
  15100. ><A
  15101. NAME="language.types.string.syntax.double"
  15102. >Double quoted</A
  15103. ></H4
  15104. ><P
  15105. >       If the string is enclosed in double-quotes ("),
  15106.       PHP understands more escape sequences for special
  15107.       characters:
  15108.      </P
  15109. ><DIV
  15110. CLASS="table"
  15111. ><A
  15112. NAME="AEN2827"
  15113. ></A
  15114. ><P
  15115. ><B
  15116. >Table 11-1. Escaped characters</B
  15117. ></P
  15118. ><TABLE
  15119. BORDER="1"
  15120. CLASS="CALSTABLE"
  15121. ><COL><COL><THEAD
  15122. ><TR
  15123. ><TH
  15124. >sequence</TH
  15125. ><TH
  15126. >meaning</TH
  15127. ></TR
  15128. ></THEAD
  15129. ><TBODY
  15130. ><TR
  15131. ><TD
  15132. ><VAR
  15133. CLASS="literal"
  15134. >\n</VAR
  15135. ></TD
  15136. ><TD
  15137. >linefeed (LF or 0x0A (10) in ASCII)</TD
  15138. ></TR
  15139. ><TR
  15140. ><TD
  15141. ><VAR
  15142. CLASS="literal"
  15143. >\r</VAR
  15144. ></TD
  15145. ><TD
  15146. >carriage return (CR or 0x0D (13) in ASCII)</TD
  15147. ></TR
  15148. ><TR
  15149. ><TD
  15150. ><VAR
  15151. CLASS="literal"
  15152. >\t</VAR
  15153. ></TD
  15154. ><TD
  15155. >horizontal tab (HT or 0x09 (9) in ASCII)</TD
  15156. ></TR
  15157. ><TR
  15158. ><TD
  15159. ><VAR
  15160. CLASS="literal"
  15161. >\\</VAR
  15162. ></TD
  15163. ><TD
  15164. >backslash</TD
  15165. ></TR
  15166. ><TR
  15167. ><TD
  15168. ><VAR
  15169. CLASS="literal"
  15170. >\$</VAR
  15171. ></TD
  15172. ><TD
  15173. >dollar sign</TD
  15174. ></TR
  15175. ><TR
  15176. ><TD
  15177. ><VAR
  15178. CLASS="literal"
  15179. >\"</VAR
  15180. ></TD
  15181. ><TD
  15182. >double-quote</TD
  15183. ></TR
  15184. ><TR
  15185. ><TD
  15186. ><VAR
  15187. CLASS="literal"
  15188. >\[0-7]{1,3}</VAR
  15189. ></TD
  15190. ><TD
  15191. >           the sequence of characters matching the regular
  15192.           expression is a character in octal notation
  15193.          </TD
  15194. ></TR
  15195. ><TR
  15196. ><TD
  15197. ><VAR
  15198. CLASS="literal"
  15199. >\x[0-9A-Fa-f]{1,2}</VAR
  15200. ></TD
  15201. ><TD
  15202. >           the sequence of characters matching the regular
  15203.           expression is a character in hexadecimal notation
  15204.          </TD
  15205. ></TR
  15206. ></TBODY
  15207. ></TABLE
  15208. ></DIV
  15209. ><P
  15210. >       Again, if you try to escape any other character, the
  15211.       backslash will be printed too!
  15212.      </P
  15213. ><P
  15214. >       But the most important feature of double-quoted strings
  15215.       is the fact that variable names will be expanded.
  15216.       See <A
  15217. HREF="#language.types.string.parsing"
  15218. >string 
  15219.       parsing</A
  15220. > for details.
  15221.      </P
  15222. ></DIV
  15223. ><DIV
  15224. CLASS="sect3"
  15225. ><HR><H4
  15226. CLASS="sect3"
  15227. ><A
  15228. NAME="language.types.string.syntax.heredoc"
  15229. >Heredoc</A
  15230. ></H4
  15231. ><P
  15232. >       Another way to delimit strings is by using heredoc syntax
  15233.       ("<<<").  One should provide an identifier after
  15234.       <VAR
  15235. CLASS="literal"
  15236. ><<<</VAR
  15237. >, then the string, and then the
  15238.       same identifier to close the quotation. 
  15239.      </P
  15240. ><P
  15241. >       The closing identifier <SPAN
  15242. CLASS="emphasis"
  15243. ><I
  15244. CLASS="emphasis"
  15245. >must</I
  15246. ></SPAN
  15247. > begin in the
  15248.       first column of the line. Also, the identifier used must follow
  15249.       the same naming rules as any other label in PHP: it must contain
  15250.       only alphanumeric characters and underscores, and must start with
  15251.       a non-digit character or underscore.
  15252.      </P
  15253. ><DIV
  15254. CLASS="warning"
  15255. ><P
  15256. ></P
  15257. ><TABLE
  15258. CLASS="warning"
  15259. BORDER="1"
  15260. WIDTH="100%"
  15261. ><TR
  15262. ><TD
  15263. ALIGN="CENTER"
  15264. ><B
  15265. >Warning</B
  15266. ></TD
  15267. ></TR
  15268. ><TR
  15269. ><TD
  15270. ALIGN="LEFT"
  15271. ><P
  15272. >        It is very important to note that the line with the closing
  15273.        identifier contains no other characters, except 
  15274.        <SPAN
  15275. CLASS="emphasis"
  15276. ><I
  15277. CLASS="emphasis"
  15278. >possibly</I
  15279. ></SPAN
  15280. > a semicolon (<VAR
  15281. CLASS="literal"
  15282. >;</VAR
  15283. >).
  15284.        That means especially that the identifier 
  15285.        <SPAN
  15286. CLASS="emphasis"
  15287. ><I
  15288. CLASS="emphasis"
  15289. >may not be indented</I
  15290. ></SPAN
  15291. >, and there
  15292.        may not be any spaces or tabs after or before the semicolon.
  15293.        It's also important to realize that the first character before
  15294.        the closing identifier must be a newline as defined by your
  15295.        operating system. This is <VAR
  15296. CLASS="literal"
  15297. >\r</VAR
  15298. > on Macintosh
  15299.        for example.
  15300.       </P
  15301. ><P
  15302. >        If this rule is broken and the closing identifier is not "clean"
  15303.        then it's not considered to be a closing identifier and PHP 
  15304.        will continue looking for one.  If in this case a proper closing 
  15305.        identifier is not found then a parse error will result with the 
  15306.        line number being at the end of the script.
  15307.       </P
  15308. ></TD
  15309. ></TR
  15310. ></TABLE
  15311. ></DIV
  15312. ><P
  15313. >       Heredoc text behaves just like a double-quoted string, without
  15314.       the double-quotes. This means that you do not need to escape quotes
  15315.       in your here docs, but you can still use the escape codes listed
  15316.       above. Variables are expanded, but the same care must be taken
  15317.       when expressing complex variables inside a here doc as with
  15318.       strings.
  15319.       <TABLE
  15320. WIDTH="100%"
  15321. BORDER="0"
  15322. CELLPADDING="0"
  15323. CELLSPACING="0"
  15324. CLASS="EXAMPLE"
  15325. ><TR
  15326. ><TD
  15327. ><DIV
  15328. CLASS="example"
  15329. ><A
  15330. NAME="AEN2884"
  15331. ></A
  15332. ><P
  15333. ><B
  15334. >Example 11-3. Heredoc string quoting example</B
  15335. ></P
  15336. ><TABLE
  15337. BORDER="0"
  15338. BGCOLOR="#E0E0E0"
  15339. CELLPADDING="5"
  15340. ><TR
  15341. ><TD
  15342. ><PRE
  15343. CLASS="php"
  15344. ><?php
  15345. $str = <<<EOD
  15346. Example of string
  15347. spanning multiple lines
  15348. using heredoc syntax.
  15349. EOD;
  15350.  
  15351. /* More complex example, with variables. */
  15352. class foo
  15353. {
  15354.     var $foo;
  15355.     var $bar;
  15356.  
  15357.     function foo()
  15358.     {
  15359.         $this->foo = 'Foo';
  15360.         $this->bar = array('Bar1', 'Bar2', 'Bar3');
  15361.     }
  15362. }
  15363.  
  15364. $foo = new foo();
  15365. $name = 'MyName';
  15366.  
  15367. echo <<<EOT
  15368. My name is "$name". I am printing some $foo->foo.
  15369. Now, I am printing some {$foo->bar[1]}.
  15370. This should print a capital 'A': \x41
  15371. EOT;
  15372. ?></PRE
  15373. ></TD
  15374. ></TR
  15375. ></TABLE
  15376. ></DIV
  15377. ></TD
  15378. ></TR
  15379. ></TABLE
  15380. >
  15381.      </P
  15382. ><DIV
  15383. CLASS="note"
  15384. ><BLOCKQUOTE
  15385. CLASS="note"
  15386. ><P
  15387. ><B
  15388. >Note: </B
  15389. >
  15390.        Heredoc support was added in PHP 4.
  15391.       </P
  15392. ></BLOCKQUOTE
  15393. ></DIV
  15394. ></DIV
  15395. ><DIV
  15396. CLASS="sect3"
  15397. ><HR><H4
  15398. CLASS="sect3"
  15399. ><A
  15400. NAME="language.types.string.parsing"
  15401. >Variable parsing</A
  15402. ></H4
  15403. ><P
  15404. >       When a string is specified in double quotes or with
  15405.       heredoc, <A
  15406. HREF="#language.variables"
  15407. >variables</A
  15408. > are 
  15409.       parsed within it. 
  15410.      </P
  15411. ><P
  15412. >       There are two types of syntax: a 
  15413.       <A
  15414. HREF="#language.types.string.parsing.simple"
  15415. >simple</A
  15416. >
  15417.       one and a 
  15418.       <A
  15419. HREF="#language.types.string.parsing.complex"
  15420. >complex</A
  15421. >
  15422.       one.
  15423.       The simple syntax is the most common and convenient. It provides a way
  15424.       to parse a variable, an <A
  15425. HREF="#language.types.array"
  15426. ><B
  15427. CLASS="type"
  15428. >array</B
  15429. ></A
  15430. > value, or an 
  15431.       object property.
  15432.      </P
  15433. ><P
  15434. >       The complex syntax was introduced in PHP 4, and can be recognised
  15435.       by the curly braces surrounding the expression.
  15436.      </P
  15437. ><DIV
  15438. CLASS="sect4"
  15439. ><HR><H5
  15440. CLASS="sect4"
  15441. ><A
  15442. NAME="language.types.string.parsing.simple"
  15443. >Simple syntax</A
  15444. ></H5
  15445. ><P
  15446. >        If a dollar sign (<VAR
  15447. CLASS="literal"
  15448. >$</VAR
  15449. >) is encountered, the
  15450.        parser will greedily take as many tokens as possible to form a
  15451.        valid variable name. Enclose the variable name in curly
  15452.        braces if you want to explicitly specify the end of the name.
  15453.       </P
  15454. ><DIV
  15455. CLASS="informalexample"
  15456. ><P
  15457. ></P
  15458. ><A
  15459. NAME="AEN2903"
  15460. ></A
  15461. ><TABLE
  15462. BORDER="0"
  15463. BGCOLOR="#E0E0E0"
  15464. CELLPADDING="5"
  15465. ><TR
  15466. ><TD
  15467. ><PRE
  15468. CLASS="php"
  15469. ><?php
  15470. $beer = 'Heineken';
  15471. echo "$beer's taste is great"; // works, "'" is an invalid character for varnames
  15472. echo "He drank some $beers";   // won't work, 's' is a valid character for varnames
  15473. echo "He drank some ${beer}s"; // works
  15474. echo "He drank some {$beer}s"; // works
  15475. ?></PRE
  15476. ></TD
  15477. ></TR
  15478. ></TABLE
  15479. ><P
  15480. ></P
  15481. ></DIV
  15482. ><P
  15483. >        Similarly, you can also have an <A
  15484. HREF="#language.types.array"
  15485. ><B
  15486. CLASS="type"
  15487. >array</B
  15488. ></A
  15489. > index or an 
  15490.        object property parsed. With array indices, the closing square 
  15491.        bracket (<VAR
  15492. CLASS="literal"
  15493. >]</VAR
  15494. >) marks the end of the index. For
  15495.        object properties the same rules apply as to simple variables,
  15496.        though with object properties there doesn't exist a trick like
  15497.        the one with variables.
  15498.        
  15499.        
  15500.       
  15501.       </P
  15502. ><DIV
  15503. CLASS="informalexample"
  15504. ><P
  15505. ></P
  15506. ><A
  15507. NAME="AEN2909"
  15508. ></A
  15509. ><TABLE
  15510. BORDER="0"
  15511. BGCOLOR="#E0E0E0"
  15512. CELLPADDING="5"
  15513. ><TR
  15514. ><TD
  15515. ><PRE
  15516. CLASS="php"
  15517. ><?php
  15518. // These examples are specific to using arrays inside of strings.
  15519. // When outside of a string, always quote your array string keys 
  15520. // and do not use {braces} when outside of strings either.
  15521.  
  15522. // Let's show all errors
  15523. error_reporting(E_ALL);
  15524.  
  15525. $fruits = array('strawberry' => 'red', 'banana' => 'yellow');
  15526.  
  15527. // Works but note that this works differently outside string-quotes
  15528. echo "A banana is $fruits[banana].";
  15529.  
  15530. // Works
  15531. echo "A banana is {$fruits['banana']}.";
  15532.  
  15533. // Works but PHP looks for a constant named banana first
  15534. // as described below.
  15535. echo "A banana is {$fruits[banana]}.";
  15536.  
  15537. // Won't work, use braces.  This results in a parse error.
  15538. echo "A banana is $fruits['banana'].";
  15539.  
  15540. // Works
  15541. echo "A banana is " . $fruits['banana'] . ".";
  15542.  
  15543. // Works
  15544. echo "This square is $square->width meters broad.";
  15545.  
  15546. // Won't work. For a solution, see the complex syntax.
  15547. echo "This square is $square->width00 centimeters broad.";
  15548. ?></PRE
  15549. ></TD
  15550. ></TR
  15551. ></TABLE
  15552. ><P
  15553. ></P
  15554. ></DIV
  15555. ><P
  15556. >        For anything more complex, you should use the complex syntax.
  15557.       </P
  15558. ></DIV
  15559. ><DIV
  15560. CLASS="sect4"
  15561. ><HR><H5
  15562. CLASS="sect4"
  15563. ><A
  15564. NAME="language.types.string.parsing.complex"
  15565. >Complex (curly) syntax</A
  15566. ></H5
  15567. ><P
  15568. >        This isn't called complex because the syntax is complex,
  15569.        but because you can include complex expressions this way.
  15570.      </P
  15571. ><P
  15572. >       In fact, you can include any value that is in the namespace
  15573.       in strings with this syntax. You simply write the expression
  15574.       the same way as you would outside the string, and then include
  15575.       it in { and }. Since you can't escape '{', this syntax will
  15576.       only be recognised when the $ is immediately following the {.
  15577.       (Use "{\$" or "\{$" to get a literal "{$").
  15578.       Some examples to make it clear:
  15579.      </P
  15580. ><DIV
  15581. CLASS="informalexample"
  15582. ><P
  15583. ></P
  15584. ><A
  15585. NAME="AEN2916"
  15586. ></A
  15587. ><TABLE
  15588. BORDER="0"
  15589. BGCOLOR="#E0E0E0"
  15590. CELLPADDING="5"
  15591. ><TR
  15592. ><TD
  15593. ><PRE
  15594. CLASS="php"
  15595. ><?php
  15596. // Let's show all errors
  15597. error_reporting(E_ALL);
  15598.  
  15599. $great = 'fantastic';
  15600.  
  15601. // Won't work, outputs: This is { fantastic}
  15602. echo "This is { $great}";
  15603.  
  15604. // Works, outputs: This is fantastic
  15605. echo "This is {$great}";
  15606. echo "This is ${great}";
  15607.  
  15608. // Works
  15609. echo "This square is {$square->width}00 centimeters broad."; 
  15610.  
  15611. // Works
  15612. echo "This works: {$arr[4][3]}";
  15613.  
  15614. // This is wrong for the same reason as $foo[bar] is wrong 
  15615. // outside a string.  In other words, it will still work but
  15616. // because PHP first looks for a constant named foo, it will
  15617. // throw an error of level E_NOTICE (undefined constant).
  15618. echo "This is wrong: {$arr[foo][3]}"; 
  15619.  
  15620. // Works.  When using multi-dimensional arrays, always use
  15621. // braces around arrays when inside of strings
  15622. echo "This works: {$arr['foo'][3]}";
  15623.  
  15624. // Works.
  15625. echo "This works: " . $arr['foo'][3];
  15626.  
  15627. echo "You can even write {$obj->values[3]->name}";
  15628.  
  15629. echo "This is the value of the var named $name: {${$name}}";
  15630. ?></PRE
  15631. ></TD
  15632. ></TR
  15633. ></TABLE
  15634. ><P
  15635. ></P
  15636. ></DIV
  15637. ></DIV
  15638. ></DIV
  15639. ><DIV
  15640. CLASS="sect3"
  15641. ><HR><H4
  15642. CLASS="sect3"
  15643. ><A
  15644. NAME="language.types.string.substr"
  15645. >String access and modification by character</A
  15646. ></H4
  15647. ><P
  15648. >       Characters within strings may be accessed and modified by specifying the
  15649.       zero-based offset of the desired character after the string 
  15650.       in curly braces.
  15651.      </P
  15652. ><DIV
  15653. CLASS="note"
  15654. ><BLOCKQUOTE
  15655. CLASS="note"
  15656. ><P
  15657. ><B
  15658. >Note: </B
  15659. >
  15660.        For backwards compatibility, you can still use array-brackets
  15661.        for the same purpose. However, this syntax is deprecated as
  15662.        of PHP 4.
  15663.       </P
  15664. ></BLOCKQUOTE
  15665. ></DIV
  15666. ><P
  15667. >       <TABLE
  15668. WIDTH="100%"
  15669. BORDER="0"
  15670. CELLPADDING="0"
  15671. CELLSPACING="0"
  15672. CLASS="EXAMPLE"
  15673. ><TR
  15674. ><TD
  15675. ><DIV
  15676. CLASS="example"
  15677. ><A
  15678. NAME="AEN2924"
  15679. ></A
  15680. ><P
  15681. ><B
  15682. >Example 11-4. Some string examples</B
  15683. ></P
  15684. ><TABLE
  15685. BORDER="0"
  15686. BGCOLOR="#E0E0E0"
  15687. CELLPADDING="5"
  15688. ><TR
  15689. ><TD
  15690. ><PRE
  15691. CLASS="php"
  15692. ><?php
  15693. // Get the first character of a string
  15694. $str = 'This is a test.';
  15695. $first = $str{0};
  15696.  
  15697. // Get the third character of a string
  15698. $third = $str{2};
  15699.  
  15700. // Get the last character of a string.
  15701. $str = 'This is still a test.';
  15702. $last = $str{strlen($str)-1}; 
  15703.  
  15704. // Modify the last character of a string
  15705. $str = 'Look at the sea';
  15706. $str{strlen($str)-1} = 'e';
  15707.           
  15708. ?></PRE
  15709. ></TD
  15710. ></TR
  15711. ></TABLE
  15712. ></DIV
  15713. ></TD
  15714. ></TR
  15715. ></TABLE
  15716. >
  15717.      </P
  15718. ></DIV
  15719. ></DIV
  15720. ><DIV
  15721. CLASS="sect2"
  15722. ><HR><H3
  15723. CLASS="sect2"
  15724. ><A
  15725. NAME="language.types.string.useful-funcs"
  15726. >Useful functions and operators</A
  15727. ></H3
  15728. ><P
  15729. >      Strings may be concatenated using the '.' (dot) operator. Note
  15730.      that the '+' (addition) operator will not work for this. Please
  15731.      see <A
  15732. HREF="#language.operators.string"
  15733. >String
  15734.      operators</A
  15735. > for more information.
  15736.     </P
  15737. ><P
  15738. >      There are a lot of useful functions for string modification.
  15739.     </P
  15740. ><P
  15741. >      See the <A
  15742. HREF="#ref.strings"
  15743. >string functions section</A
  15744.      for general functions, the regular expression functions for
  15745.      advanced find&replacing (in two tastes: 
  15746.      <A
  15747. HREF="#ref.pcre"
  15748. >Perl</A
  15749. > and 
  15750.      <A
  15751. HREF="#ref.regex"
  15752. >POSIX extended</A
  15753. >).
  15754.     </P
  15755. ><P
  15756. >      There are also <A
  15757. HREF="#ref.url"
  15758. >functions for URL-strings</A
  15759. >,
  15760.      and functions to encrypt/decrypt strings 
  15761.      (<A
  15762. HREF="#ref.mcrypt"
  15763. >mcrypt</A
  15764. > and 
  15765.      <A
  15766. HREF="#ref.mhash"
  15767. >mhash</A
  15768. >).
  15769.     </P
  15770. ><P
  15771. >      Finally, if you still didn't find what you're looking for,
  15772.      see also the <A
  15773. HREF="#ref.ctype"
  15774. >character type functions</A
  15775. >.
  15776.     </P
  15777. ></DIV
  15778. ><DIV
  15779. CLASS="sect2"
  15780. ><HR><H3
  15781. CLASS="sect2"
  15782. ><A
  15783. NAME="language.types.string.casting"
  15784. >Converting to string</A
  15785. ></H3
  15786. ><P
  15787. >      You can convert a value to a string using the <VAR
  15788. CLASS="literal"
  15789. >(string)</VAR
  15790. >
  15791.      cast, or the <A
  15792. HREF="#function.strval"
  15793. ><B
  15794. CLASS="function"
  15795. >strval()</B
  15796. ></A
  15797. > function. String conversion
  15798.      is automatically done in the scope of an expression for you where a
  15799.      string is needed. This happens when you use the <A
  15800. HREF="#function.echo"
  15801. ><B
  15802. CLASS="function"
  15803. >echo()</B
  15804. ></A
  15805. >
  15806.      or <A
  15807. HREF="#function.print"
  15808. ><B
  15809. CLASS="function"
  15810. >print()</B
  15811. ></A
  15812. > functions, or when you compare a variable
  15813.      value to a string.  Reading the manual sections on <A
  15814. HREF="#language.types"
  15815. >Types</A
  15816. > and <A
  15817. HREF="#language.types.type-juggling"
  15818. >Type Juggling</A
  15819. > will make
  15820.      the following clearer.  See also <A
  15821. HREF="#function.settype"
  15822. ><B
  15823. CLASS="function"
  15824. >settype()</B
  15825. ></A
  15826. >.
  15827.     </P
  15828. ><P
  15829. >      A <A
  15830. HREF="#language.types.boolean"
  15831. ><B
  15832. CLASS="type"
  15833. >boolean</B
  15834. ></A
  15835. > <TT
  15836. CLASS="constant"
  15837. ><B
  15838. >TRUE</B
  15839. ></TT
  15840. > value is converted to the string <VAR
  15841. CLASS="literal"
  15842. >"1"</VAR
  15843. >,
  15844.      the <TT
  15845. CLASS="constant"
  15846. ><B
  15847. >FALSE</B
  15848. ></TT
  15849. > value is represented as <VAR
  15850. CLASS="literal"
  15851. >""</VAR
  15852. > (empty string).
  15853.      This way you can convert back and forth between boolean and string values.
  15854.     </P
  15855. ><P
  15856.      An <A
  15857. HREF="#language.types.integer"
  15858. ><B
  15859. CLASS="type"
  15860. >integer</B
  15861. ></A
  15862. > or a floating point number (<A
  15863. HREF="#language.types.float"
  15864. ><B
  15865. CLASS="type"
  15866. >float</B
  15867. ></A
  15868. >) 
  15869.      is converted to a string representing the number with its digits
  15870.      (including the exponent part for floating point numbers).
  15871.     </P
  15872. ><P
  15873. >      Arrays are always converted to the string <VAR
  15874. CLASS="literal"
  15875. >"Array"</VAR
  15876. >,
  15877.      so you cannot dump out the contents of an <A
  15878. HREF="#language.types.array"
  15879. ><B
  15880. CLASS="type"
  15881. >array</B
  15882. ></A
  15883. > with 
  15884.      <A
  15885. HREF="#function.echo"
  15886. ><B
  15887. CLASS="function"
  15888. >echo()</B
  15889. ></A
  15890. > or <A
  15891. HREF="#function.print"
  15892. ><B
  15893. CLASS="function"
  15894. >print()</B
  15895. ></A
  15896. > to see what is inside 
  15897.      them.  To view one element, you'd do something like <VAR
  15898. CLASS="literal"
  15899. >      echo $arr['foo']</VAR
  15900. >.  See below for tips on dumping/viewing the 
  15901.      entire contents.
  15902.     </P
  15903. ><P
  15904. >      Objects are always converted to the string <VAR
  15905. CLASS="literal"
  15906. >"Object"</VAR
  15907. >.
  15908.      If you would like to print out the member variable values of an 
  15909.      <A
  15910. HREF="#language.types.object"
  15911. ><B
  15912. CLASS="type"
  15913. >object</B
  15914. ></A
  15915. > for debugging reasons, read the paragraphs 
  15916.      below. If you would like to find out the class name of which an object 
  15917.      is an instance of, use <A
  15918. HREF="#function.get-class"
  15919. ><B
  15920. CLASS="function"
  15921. >get_class()</B
  15922. ></A
  15923. >.
  15924.      As of PHP 5, __toString() method is used if applicable.
  15925.     </P
  15926. ><P
  15927. >      Resources are always converted to strings with the structure
  15928.      <VAR
  15929. CLASS="literal"
  15930. >"Resource id #1"</VAR
  15931. > where <VAR
  15932. CLASS="literal"
  15933. >1</VAR
  15934. > is
  15935.      the unique number of the <A
  15936. HREF="#language.types.resource"
  15937. ><B
  15938. CLASS="type"
  15939. >resource</B
  15940. ></A
  15941. > assigned by PHP during runtime.
  15942.      If you would like to get the type of the resource, use
  15943.      <A
  15944. HREF="#function.get-resource-type"
  15945. ><B
  15946. CLASS="function"
  15947. >get_resource_type()</B
  15948. ></A
  15949. >.
  15950.     </P
  15951. ><P
  15952. >      <TT
  15953. CLASS="constant"
  15954. ><B
  15955. >NULL</B
  15956. ></TT
  15957. > is always converted to an empty string.
  15958.     </P
  15959. ><P
  15960. >      As you can see above, printing out the arrays, objects or resources does not
  15961.      provide you any useful information about the values themselves. Look at the
  15962.      functions <A
  15963. HREF="#function.print-r"
  15964. ><B
  15965. CLASS="function"
  15966. >print_r()</B
  15967. ></A
  15968. > and <A
  15969. HREF="#function.var-dump"
  15970. ><B
  15971. CLASS="function"
  15972. >var_dump()</B
  15973. ></A
  15974. >
  15975.      for better ways to print out values for debugging.
  15976.     </P
  15977. ><P
  15978. >      You can also convert PHP values to strings to store them permanently. This
  15979.      method is called serialization, and can be done with the function
  15980.      <A
  15981. HREF="#function.serialize"
  15982. ><B
  15983. CLASS="function"
  15984. >serialize()</B
  15985. ></A
  15986. >. You can also serialize PHP values to
  15987.      XML structures, if you have <A
  15988. HREF="#ref.wddx"
  15989. >WDDX</A
  15990. > support
  15991.      in your PHP setup.
  15992.     </P
  15993. ></DIV
  15994. ><DIV
  15995. CLASS="sect2"
  15996. ><HR><H3
  15997. CLASS="sect2"
  15998. ><A
  15999. NAME="language.types.string.conversion"
  16000. >String conversion to numbers</A
  16001. ></H3
  16002. ><P
  16003. >      When a string is evaluated as a numeric value, the resulting
  16004.      value and type are determined as follows.
  16005.     </P
  16006. ><P
  16007. >      The string will evaluate as a <A
  16008. HREF="#language.types.float"
  16009. ><B
  16010. CLASS="type"
  16011. >float</B
  16012. ></A
  16013. > if it contains any of the
  16014.      characters '.', 'e', or 'E'. Otherwise, it will evaluate as an
  16015.      integer.
  16016.     </P
  16017. ><P
  16018. >      The value is given by the initial portion of the string. If the
  16019.      string starts with valid numeric data, this will be the value
  16020.      used. Otherwise, the value will be 0 (zero). Valid numeric data
  16021.      is an optional sign, followed by one or more digits (optionally
  16022.      containing a decimal point), followed by an optional
  16023.      exponent. The exponent is an 'e' or 'E' followed by one or more
  16024.      digits.
  16025.     </P
  16026. ><DIV
  16027. CLASS="informalexample"
  16028. ><P
  16029. ></P
  16030. ><A
  16031. NAME="AEN2990"
  16032. ></A
  16033. ><TABLE
  16034. BORDER="0"
  16035. BGCOLOR="#E0E0E0"
  16036. CELLPADDING="5"
  16037. ><TR
  16038. ><TD
  16039. ><PRE
  16040. CLASS="php"
  16041. ><?php
  16042. $foo = 1 + "10.5";                // $foo is float (11.5)
  16043. $foo = 1 + "-1.3e3";              // $foo is float (-1299)
  16044. $foo = 1 + "bob-1.3e3";           // $foo is integer (1)
  16045. $foo = 1 + "bob3";                // $foo is integer (1)
  16046. $foo = 1 + "10 Small Pigs";       // $foo is integer (11)
  16047. $foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
  16048. $foo = "10.0 pigs " + 1;          // $foo is float (11)
  16049. $foo = "10.0 pigs " + 1.0;        // $foo is float (11)     
  16050. ?></PRE
  16051. ></TD
  16052. ></TR
  16053. ></TABLE
  16054. ><P
  16055. ></P
  16056. ></DIV
  16057. ><P
  16058. >      For more information on this conversion, see the Unix manual page
  16059.      for strtod(3).
  16060.     </P
  16061. ><P
  16062. >      If you would like to test any of the examples in this section,
  16063.      you can cut and paste the examples and insert the following line
  16064.      to see for yourself what's going on:
  16065.      <DIV
  16066. CLASS="informalexample"
  16067. ><P
  16068. ></P
  16069. ><A
  16070. NAME="AEN2994"
  16071. ></A
  16072. ><TABLE
  16073. BORDER="0"
  16074. BGCOLOR="#E0E0E0"
  16075. CELLPADDING="5"
  16076. ><TR
  16077. ><TD
  16078. ><PRE
  16079. CLASS="php"
  16080. ><?php
  16081. echo "\$foo==$foo; type is " . gettype ($foo) . "<br />\n";
  16082. ?></PRE
  16083. ></TD
  16084. ></TR
  16085. ></TABLE
  16086. ><P
  16087. ></P
  16088. ></DIV
  16089. >
  16090.     </P
  16091. ><P
  16092. >      Do not expect to get the code of one character by converting it
  16093.      to integer (as you would do in C for example). Use the functions
  16094.      <A
  16095. HREF="#function.ord"
  16096. ><B
  16097. CLASS="function"
  16098. >ord()</B
  16099. ></A
  16100. > and <A
  16101. HREF="#function.chr"
  16102. ><B
  16103. CLASS="function"
  16104. >chr()</B
  16105. ></A
  16106. > to convert
  16107.      between charcodes and characters.
  16108.     </P
  16109. ></DIV
  16110. ></DIV
  16111. ><DIV
  16112. CLASS="sect1"
  16113. ><HR><H2
  16114. CLASS="sect1"
  16115. ><A
  16116. NAME="language.types.array"
  16117. >Arrays</A
  16118. ></H2
  16119. ><P
  16120. >     An array in PHP is actually an ordered map. A map is a type that
  16121.     maps <SPAN
  16122. CLASS="emphasis"
  16123. ><I
  16124. CLASS="emphasis"
  16125. >values</I
  16126. ></SPAN
  16127. > to <SPAN
  16128. CLASS="emphasis"
  16129. ><I
  16130. CLASS="emphasis"
  16131. >keys</I
  16132. ></SPAN
  16133. >. 
  16134.     This type is optimized in several ways,
  16135.     so you can use it as a real array, or a list (vector), 
  16136.     hashtable (which is an implementation of a map), 
  16137.     dictionary, collection,
  16138.     stack, queue and probably more. Because you can have another
  16139.     PHP array as a value, you can also quite easily simulate
  16140.     trees.
  16141.    </P
  16142. ><P
  16143. >     Explanation of those data structures is beyond the scope of this
  16144.     manual, but you'll find at least one example for each of them.
  16145.     For more information we refer you to external literature about
  16146.     this broad topic.
  16147.    </P
  16148. ><DIV
  16149. CLASS="sect2"
  16150. ><HR><H3
  16151. CLASS="sect2"
  16152. ><A
  16153. NAME="language.types.array.syntax"
  16154. >Syntax</A
  16155. ></H3
  16156. ><DIV
  16157. CLASS="sect3"
  16158. ><H4
  16159. CLASS="sect3"
  16160. ><A
  16161. NAME="language.types.array.syntax.array-func"
  16162. >Specifying with <A
  16163. HREF="#function.array"
  16164. ><B
  16165. CLASS="function"
  16166. >array()</B
  16167. ></A
  16168. ></A
  16169. ></H4
  16170. ><P
  16171. >       An <A
  16172. HREF="#language.types.array"
  16173. ><B
  16174. CLASS="type"
  16175. >array</B
  16176. ></A
  16177. > can be created by the <A
  16178. HREF="#function.array"
  16179. ><B
  16180. CLASS="function"
  16181. >array()</B
  16182. ></A
  16183.       language-construct. It takes a certain number of comma-separated
  16184.       <VAR
  16185. CLASS="literal"
  16186. ><VAR
  16187. CLASS="replaceable"
  16188. >key</VAR
  16189. > => <VAR
  16190. CLASS="replaceable"
  16191. >value</VAR
  16192. ></VAR
  16193. >
  16194.       pairs.
  16195.      </P
  16196. ><P
  16197. >       <TABLE
  16198. BORDER="0"
  16199. BGCOLOR="#E0E0E0"
  16200. CELLPADDING="5"
  16201. ><TR
  16202. ><TD
  16203. ><PRE
  16204. CLASS="synopsis"
  16205. >array( [<VAR
  16206. CLASS="replaceable"
  16207. >key</VAR
  16208. > =>] <VAR
  16209. CLASS="replaceable"
  16210. >value</VAR
  16211. >
  16212.      , ...
  16213.      )
  16214. // <VAR
  16215. CLASS="replaceable"
  16216. >key</VAR
  16217. > may be an <A
  16218. HREF="#language.types.integer"
  16219. ><B
  16220. CLASS="type"
  16221. >integer</B
  16222. ></A
  16223. > or <A
  16224. HREF="#language.types.string"
  16225. ><B
  16226. CLASS="type"
  16227. >string</B
  16228. ></A
  16229. >
  16230. // <VAR
  16231. CLASS="replaceable"
  16232. >value</VAR
  16233. > may be any value</PRE
  16234. ></TD
  16235. ></TR
  16236. ></TABLE
  16237. >
  16238.      </P
  16239. ><P
  16240. >       <DIV
  16241. CLASS="informalexample"
  16242. ><P
  16243. ></P
  16244. ><A
  16245. NAME="AEN3026"
  16246. ></A
  16247. ><TABLE
  16248. BORDER="0"
  16249. BGCOLOR="#E0E0E0"
  16250. CELLPADDING="5"
  16251. ><TR
  16252. ><TD
  16253. ><PRE
  16254. CLASS="php"
  16255. ><?php
  16256. $arr = array("foo" => "bar", 12 => true);
  16257.  
  16258. echo $arr["foo"]; // bar
  16259. echo $arr[12];    // 1
  16260. ?></PRE
  16261. ></TD
  16262. ></TR
  16263. ></TABLE
  16264. ><P
  16265. ></P
  16266. ></DIV
  16267. >
  16268.      </P
  16269. ><P
  16270. >       A <VAR
  16271. CLASS="varname"
  16272. >key</VAR
  16273. > may be either an
  16274.       <VAR
  16275. CLASS="literal"
  16276. >integer</VAR
  16277. > or a <A
  16278. HREF="#language.types.string"
  16279. ><B
  16280. CLASS="type"
  16281. >string</B
  16282. ></A
  16283. >. If a key is
  16284.       the standard representation of an <A
  16285. HREF="#language.types.integer"
  16286. ><B
  16287. CLASS="type"
  16288. >integer</B
  16289. ></A
  16290. >, it will
  16291.       be interpreted as such (i.e.  <VAR
  16292. CLASS="literal"
  16293. >"8"</VAR
  16294. > will be
  16295.       interpreted as <VAR
  16296. CLASS="literal"
  16297. >8</VAR
  16298. >, while
  16299.       <VAR
  16300. CLASS="literal"
  16301. >"08"</VAR
  16302. > will be interpreted as
  16303.       <VAR
  16304. CLASS="literal"
  16305. >"08"</VAR
  16306. >). There are no different indexed and
  16307.       associative array types in PHP; there is only one array type,
  16308.       which can both contain integer and string indices.
  16309.      </P
  16310. ><P
  16311. >       A value can be of any PHP type.
  16312.       <DIV
  16313. CLASS="informalexample"
  16314. ><P
  16315. ></P
  16316. ><A
  16317. NAME="AEN3038"
  16318. ></A
  16319. ><TABLE
  16320. BORDER="0"
  16321. BGCOLOR="#E0E0E0"
  16322. CELLPADDING="5"
  16323. ><TR
  16324. ><TD
  16325. ><PRE
  16326. CLASS="php"
  16327. ><?php
  16328. $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
  16329.  
  16330. echo $arr["somearray"][6];    // 5
  16331. echo $arr["somearray"][13];   // 9
  16332. echo $arr["somearray"]["a"];  // 42
  16333. ?></PRE
  16334. ></TD
  16335. ></TR
  16336. ></TABLE
  16337. ><P
  16338. ></P
  16339. ></DIV
  16340. >
  16341.      </P
  16342. ><P
  16343. >       If you do not specify a key for a given value, then the maximum
  16344.       of the integer indices is taken, and the new key will be that
  16345.       maximum value + 1. If you specify a key that already has a value
  16346.       assigned to it, that value will be overwritten.
  16347.       <DIV
  16348. CLASS="informalexample"
  16349. ><P
  16350. ></P
  16351. ><A
  16352. NAME="AEN3041"
  16353. ></A
  16354. ><TABLE
  16355. BORDER="0"
  16356. BGCOLOR="#E0E0E0"
  16357. CELLPADDING="5"
  16358. ><TR
  16359. ><TD
  16360. ><PRE
  16361. CLASS="php"
  16362. ><?php
  16363. // This array is the same as ...
  16364. array(5 => 43, 32, 56, "b" => 12);
  16365.  
  16366. // ...this array
  16367. array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
  16368. ?></PRE
  16369. ></TD
  16370. ></TR
  16371. ></TABLE
  16372. ><P
  16373. ></P
  16374. ></DIV
  16375. >
  16376.      </P
  16377. ><DIV
  16378. CLASS="warning"
  16379. ><P
  16380. ></P
  16381. ><TABLE
  16382. CLASS="warning"
  16383. BORDER="1"
  16384. WIDTH="100%"
  16385. ><TR
  16386. ><TD
  16387. ALIGN="CENTER"
  16388. ><B
  16389. >Warning</B
  16390. ></TD
  16391. ></TR
  16392. ><TR
  16393. ><TD
  16394. ALIGN="LEFT"
  16395. ><P
  16396. >          As of PHP 4.3.0, the index generation behaviour described
  16397.          above has changed. Now, if you append to an array in which
  16398.          the current maximum key is negative, then the next key
  16399.          created will be zero (<VAR
  16400. CLASS="literal"
  16401. >0</VAR
  16402. >). Before, the new
  16403.          index would have been set to the largest existing key + 1,
  16404.          the same as positive indices are.
  16405.       </P
  16406. ></TD
  16407. ></TR
  16408. ></TABLE
  16409. ></DIV
  16410. ><P
  16411. >       Using <TT
  16412. CLASS="constant"
  16413. ><B
  16414. >TRUE</B
  16415. ></TT
  16416. > as a key will evaluate to <A
  16417. HREF="#language.types.integer"
  16418. ><B
  16419. CLASS="type"
  16420. >integer</B
  16421. ></A
  16422. >
  16423.       <VAR
  16424. CLASS="literal"
  16425. >1</VAR
  16426. > as key. Using <TT
  16427. CLASS="constant"
  16428. ><B
  16429. >FALSE</B
  16430. ></TT
  16431. > as a key will evaluate
  16432.       to <A
  16433. HREF="#language.types.integer"
  16434. ><B
  16435. CLASS="type"
  16436. >integer</B
  16437. ></A
  16438. > <VAR
  16439. CLASS="literal"
  16440. >0</VAR
  16441. > as key. Using
  16442.       <VAR
  16443. CLASS="literal"
  16444. >NULL</VAR
  16445. > as a key will evaluate to the empty
  16446.       string. Using the empty string as key will create (or overwrite)
  16447.       a key with the empty string and its value; it is not the same as
  16448.       using empty brackets.
  16449.      </P
  16450. ><P
  16451. >       You cannot use arrays or objects as keys. Doing so will result in a
  16452.       warning: <VAR
  16453. CLASS="literal"
  16454. >Illegal offset type</VAR
  16455. >.
  16456.      </P
  16457. ></DIV
  16458. ><DIV
  16459. CLASS="sect3"
  16460. ><HR><H4
  16461. CLASS="sect3"
  16462. ><A
  16463. NAME="language.types.array.syntax.modifying"
  16464. >Creating/modifying with square-bracket syntax</A
  16465. ></H4
  16466. ><P
  16467. >       You can also modify an existing array by explicitly setting
  16468.       values in it.
  16469.      </P
  16470. ><P
  16471. >       This is done by assigning values to the array while specifying the 
  16472.       key in brackets. You can also omit the key, add an empty pair
  16473.       of brackets ("<VAR
  16474. CLASS="literal"
  16475. >[]</VAR
  16476. >") to the variable name in that case.
  16477.       <TABLE
  16478. BORDER="0"
  16479. BGCOLOR="#E0E0E0"
  16480. CELLPADDING="5"
  16481. ><TR
  16482. ><TD
  16483. ><PRE
  16484. CLASS="synopsis"
  16485. >$arr[<VAR
  16486. CLASS="replaceable"
  16487. >key</VAR
  16488. >] = <VAR
  16489. CLASS="replaceable"
  16490. >value</VAR
  16491. >;
  16492. $arr[] = <VAR
  16493. CLASS="replaceable"
  16494. >value</VAR
  16495. >;
  16496. // <VAR
  16497. CLASS="replaceable"
  16498. >key</VAR
  16499. > may be an <A
  16500. HREF="#language.types.integer"
  16501. ><B
  16502. CLASS="type"
  16503. >integer</B
  16504. ></A
  16505. > or <A
  16506. HREF="#language.types.string"
  16507. ><B
  16508. CLASS="type"
  16509. >string</B
  16510. ></A
  16511. >
  16512. // <VAR
  16513. CLASS="replaceable"
  16514. >value</VAR
  16515. > may be any value</PRE
  16516. ></TD
  16517. ></TR
  16518. ></TABLE
  16519. >
  16520.       If <VAR
  16521. CLASS="varname"
  16522. >$arr</VAR
  16523. > doesn't exist yet, it will be created. 
  16524.       So this is also an alternative way to specify an array.
  16525.       To change a certain value, just assign a new value
  16526.       to an element specified with its key. If you want to
  16527.       remove a key/value pair, you need to <A
  16528. HREF="#function.unset"
  16529. ><B
  16530. CLASS="function"
  16531. >unset()</B
  16532. ></A
  16533. > it. 
  16534.       <DIV
  16535. CLASS="informalexample"
  16536. ><P
  16537. ></P
  16538. ><A
  16539. NAME="AEN3071"
  16540. ></A
  16541. ><TABLE
  16542. BORDER="0"
  16543. BGCOLOR="#E0E0E0"
  16544. CELLPADDING="5"
  16545. ><TR
  16546. ><TD
  16547. ><PRE
  16548. CLASS="php"
  16549. ><?php
  16550. $arr = array(5 => 1, 12 => 2);
  16551.  
  16552. $arr[] = 56;    // This is the same as $arr[13] = 56;
  16553.                 // at this point of the script
  16554.  
  16555. $arr["x"] = 42; // This adds a new element to
  16556.                 // the array with key "x"
  16557.                 
  16558. unset($arr[5]); // This removes the element from the array
  16559.  
  16560. unset($arr);    // This deletes the whole array
  16561. ?></PRE
  16562. ></TD
  16563. ></TR
  16564. ></TABLE
  16565. ><P
  16566. ></P
  16567. ></DIV
  16568. >
  16569.      </P
  16570. ><DIV
  16571. CLASS="note"
  16572. ><BLOCKQUOTE
  16573. CLASS="note"
  16574. ><P
  16575. ><B
  16576. >Note: </B
  16577. >
  16578.        As mentioned above, if you provide the brackets with no key
  16579.        specified, then the maximum of the existing integer indices is
  16580.        taken, and the new key will be that maximum value + 1 . If no
  16581.        integer indices exist yet, the key will be <VAR
  16582. CLASS="literal"
  16583. >0</VAR
  16584. >
  16585.        (zero). If you specify a key that already has a value assigned
  16586.        to it, that value will be overwritten.
  16587.       </P
  16588. ><P
  16589. >        <DIV
  16590. CLASS="warning"
  16591. ><P
  16592. ></P
  16593. ><TABLE
  16594. CLASS="warning"
  16595. BORDER="1"
  16596. WIDTH="100%"
  16597. ><TR
  16598. ><TD
  16599. ALIGN="CENTER"
  16600. ><B
  16601. >Warning</B
  16602. ></TD
  16603. ></TR
  16604. ><TR
  16605. ><TD
  16606. ALIGN="LEFT"
  16607. ><P
  16608. >          As of PHP 4.3.0, the index generation behaviour described
  16609.          above has changed. Now, if you append to an array in which
  16610.          the current maximum key is negative, then the next key
  16611.          created will be zero (<VAR
  16612. CLASS="literal"
  16613. >0</VAR
  16614. >). Before, the new
  16615.          index would have been set to the largest existing key + 1,
  16616.          the same as positive indices are.
  16617.         </P
  16618. ></TD
  16619. ></TR
  16620. ></TABLE
  16621. ></DIV
  16622. >
  16623.       </P
  16624. ><P
  16625. >        Note that the maximum integer key used for this <SPAN
  16626. CLASS="emphasis"
  16627. ><I
  16628. CLASS="emphasis"
  16629. >need
  16630.        not currently exist in the array</I
  16631. ></SPAN
  16632. >. It simply must
  16633.        have existed in the array at some time since the last time the
  16634.        array was re-indexed. The following example illustrates:
  16635.       </P
  16636. ><DIV
  16637. CLASS="informalexample"
  16638. ><P
  16639. ></P
  16640. ><A
  16641. NAME="AEN3082"
  16642. ></A
  16643. ><TABLE
  16644. BORDER="0"
  16645. BGCOLOR="#E0E0E0"
  16646. CELLPADDING="5"
  16647. ><TR
  16648. ><TD
  16649. ><PRE
  16650. CLASS="php"
  16651. ><?php
  16652. // Create a simple array.
  16653. $array = array(1, 2, 3, 4, 5);
  16654. print_r($array);
  16655.  
  16656. // Now delete every item, but leave the array itself intact:
  16657. foreach ($array as $i => $value) {
  16658.     unset($array[$i]);
  16659. }
  16660. print_r($array);
  16661.  
  16662. // Append an item (note that the new key is 5, instead of 0 as you
  16663. // might expect).
  16664. $array[] = 6;
  16665. print_r($array);
  16666.  
  16667. // Re-index:
  16668. $array = array_values($array);
  16669. $array[] = 7;
  16670. print_r($array);
  16671. ?></PRE
  16672. ></TD
  16673. ></TR
  16674. ></TABLE
  16675. ><P
  16676. >         The above example would produce the following output:
  16677.         <TABLE
  16678. BORDER="0"
  16679. BGCOLOR="#E0E0E0"
  16680. CELLPADDING="5"
  16681. ><TR
  16682. ><TD
  16683. ><PRE
  16684. CLASS="screen"
  16685. >Array
  16686. (
  16687.     [0] => 1
  16688.     [1] => 2
  16689.     [2] => 3
  16690.     [3] => 4
  16691.     [4] => 5
  16692. )
  16693. Array
  16694. (
  16695. )
  16696. Array
  16697. (
  16698.     [5] => 6
  16699. )
  16700. Array
  16701. (
  16702.     [0] => 6
  16703.     [1] => 7
  16704. )</PRE
  16705. ></TD
  16706. ></TR
  16707. ></TABLE
  16708. >
  16709.        </P
  16710. ><P
  16711. ></P
  16712. ></DIV
  16713. ></BLOCKQUOTE
  16714. ></DIV
  16715. ></DIV
  16716. ></DIV
  16717. ><DIV
  16718. CLASS="sect2"
  16719. ><HR><H3
  16720. CLASS="sect2"
  16721. ><A
  16722. NAME="language.types.array.useful-funcs"
  16723. >Useful functions</A
  16724. ></H3
  16725. ><P
  16726. >      There are quite a few useful functions for working with arrays.
  16727.      See the <A
  16728. HREF="#ref.array"
  16729. >array functions</A
  16730. > section.
  16731.     </P
  16732. ><DIV
  16733. CLASS="note"
  16734. ><BLOCKQUOTE
  16735. CLASS="note"
  16736. ><P
  16737. ><B
  16738. >Note: </B
  16739. >
  16740.       The <A
  16741. HREF="#function.unset"
  16742. ><B
  16743. CLASS="function"
  16744. >unset()</B
  16745. ></A
  16746. > function allows unsetting keys of an 
  16747.       array. Be aware that the array will NOT be reindexed. If you only
  16748.       use "usual integer indices" (starting from zero, increasing by one),
  16749.       you can achieve the reindex effect by using <A
  16750. HREF="#function.array-values"
  16751. ><B
  16752. CLASS="function"
  16753. >array_values()</B
  16754. ></A
  16755. >.
  16756.       <DIV
  16757. CLASS="informalexample"
  16758. ><P
  16759. ></P
  16760. ><A
  16761. NAME="AEN3094"
  16762. ></A
  16763. ><TABLE
  16764. BORDER="0"
  16765. BGCOLOR="#E0E0E0"
  16766. CELLPADDING="5"
  16767. ><TR
  16768. ><TD
  16769. ><PRE
  16770. CLASS="php"
  16771. ><?php
  16772. $a = array(1 => 'one', 2 => 'two', 3 => 'three');
  16773. unset($a[2]);
  16774. /* will produce an array that would have been defined as
  16775.    $a = array(1 => 'one', 3 => 'three');
  16776.    and NOT
  16777.    $a = array(1 => 'one', 2 =>'three');
  16778. */
  16779.  
  16780. $b = array_values($a);
  16781. // Now $b is array(0 => 'one', 1 =>'three')
  16782. ?></PRE
  16783. ></TD
  16784. ></TR
  16785. ></TABLE
  16786. ><P
  16787. ></P
  16788. ></DIV
  16789. >
  16790.      
  16791.      </P
  16792. ></BLOCKQUOTE
  16793. ></DIV
  16794. ><P
  16795. >      The <A
  16796. HREF="#control-structures.foreach"
  16797. >foreach</A
  16798.      control structure exists specifically for arrays. It 
  16799.      provides an easy way to traverse an array.
  16800.     </P
  16801. ></DIV
  16802. ><DIV
  16803. CLASS="sect2"
  16804. ><HR><H3
  16805. CLASS="sect2"
  16806. ><A
  16807. NAME="language.types.array.donts"
  16808. >Array do's and don'ts</A
  16809. ></H3
  16810. ><DIV
  16811. CLASS="sect3"
  16812. ><H4
  16813. CLASS="sect3"
  16814. ><A
  16815. NAME="language.types.array.foo-bar"
  16816. >Why is <VAR
  16817. CLASS="literal"
  16818. >$foo[bar]</VAR
  16819. > wrong?</A
  16820. ></H4
  16821. ><P
  16822. >       You should always use quotes around a string literal
  16823.       array index.  For example, use $foo['bar'] and not
  16824.       $foo[bar]. But why is $foo[bar] wrong? You might have seen the
  16825.       following syntax in old scripts:
  16826.       <DIV
  16827. CLASS="informalexample"
  16828. ><P
  16829. ></P
  16830. ><A
  16831. NAME="AEN3104"
  16832. ></A
  16833. ><TABLE
  16834. BORDER="0"
  16835. BGCOLOR="#E0E0E0"
  16836. CELLPADDING="5"
  16837. ><TR
  16838. ><TD
  16839. ><PRE
  16840. CLASS="php"
  16841. ><?php
  16842. $foo[bar] = 'enemy';
  16843. echo $foo[bar];
  16844. // etc
  16845. ?></PRE
  16846. ></TD
  16847. ></TR
  16848. ></TABLE
  16849. ><P
  16850. ></P
  16851. ></DIV
  16852. >
  16853.       This is wrong, but it works. Then, why is it wrong? The reason
  16854.       is that this code has an undefined constant (bar) rather than a
  16855.       string ('bar' - notice the quotes), and PHP may in future define
  16856.       constants which, unfortunately for your code, have the same
  16857.       name.  It works because PHP automatically converts a
  16858.       <SPAN
  16859. CLASS="emphasis"
  16860. ><I
  16861. CLASS="emphasis"
  16862. >bare string</I
  16863. ></SPAN
  16864. > (an unquoted string which does
  16865.       not correspond to any known symbol) into a string which contains
  16866.       the bare string. For instance, if there is no defined constant
  16867.       named <TT
  16868. CLASS="constant"
  16869. ><B
  16870. >bar</B
  16871. ></TT
  16872. >, then PHP will substitute in the
  16873.       string <VAR
  16874. CLASS="literal"
  16875. >'bar'</VAR
  16876. > and use that.
  16877.      </P
  16878. ><DIV
  16879. CLASS="note"
  16880. ><BLOCKQUOTE
  16881. CLASS="note"
  16882. ><P
  16883. ><B
  16884. >Note: </B
  16885. >
  16886.        This does not mean to <SPAN
  16887. CLASS="emphasis"
  16888. ><I
  16889. CLASS="emphasis"
  16890. >always</I
  16891. ></SPAN
  16892. > quote the
  16893.        key. You do not want to quote keys which are <A
  16894. HREF="#language.constants"
  16895. >constants</A
  16896. > or <A
  16897. HREF="#language.variables"
  16898. >variables</A
  16899. >, as this will
  16900.        prevent PHP from interpreting them.
  16901.       </P
  16902. ><DIV
  16903. CLASS="informalexample"
  16904. ><P
  16905. ></P
  16906. ><A
  16907. NAME="AEN3114"
  16908. ></A
  16909. ><TABLE
  16910. BORDER="0"
  16911. BGCOLOR="#E0E0E0"
  16912. CELLPADDING="5"
  16913. ><TR
  16914. ><TD
  16915. ><PRE
  16916. CLASS="php"
  16917. ><?php
  16918. error_reporting(E_ALL);
  16919. ini_set('display_errors', true);
  16920. ini_set('html_errors', false);
  16921. // Simple array:
  16922. $array = array(1, 2);
  16923. $count = count($array);
  16924. for ($i = 0; $i < $count; $i++) {
  16925.     echo "\nChecking $i: \n";
  16926.     echo "Bad: " . $array['$i'] . "\n";
  16927.     echo "Good: " . $array[$i] . "\n";
  16928.     echo "Bad: {$array['$i']}\n";
  16929.     echo "Good: {$array[$i]}\n";
  16930. }
  16931. ?></PRE
  16932. ></TD
  16933. ></TR
  16934. ></TABLE
  16935. ><P
  16936. ></P
  16937. ></DIV
  16938. ><P
  16939. ><B
  16940. >Note: </B
  16941. >
  16942.        The output from the above is:
  16943.        <TABLE
  16944. BORDER="0"
  16945. BGCOLOR="#E0E0E0"
  16946. CELLPADDING="5"
  16947. ><TR
  16948. ><TD
  16949. ><PRE
  16950. CLASS="screen"
  16951. >Checking 0: 
  16952. Notice: Undefined index:  $i in /path/to/script.html on line 9
  16953. Bad: 
  16954. Good: 1
  16955. Notice: Undefined index:  $i in /path/to/script.html on line 11
  16956. Bad: 
  16957. Good: 1
  16958.  
  16959. Checking 1: 
  16960. Notice: Undefined index:  $i in /path/to/script.html on line 9
  16961. Bad: 
  16962. Good: 2
  16963. Notice: Undefined index:  $i in /path/to/script.html on line 11
  16964. Bad: 
  16965. Good: 2</PRE
  16966. ></TD
  16967. ></TR
  16968. ></TABLE
  16969. >
  16970.       </P
  16971. ></BLOCKQUOTE
  16972. ></DIV
  16973. ><P
  16974. >       More examples to demonstrate this fact:
  16975.       <DIV
  16976. CLASS="informalexample"
  16977. ><P
  16978. ></P
  16979. ><A
  16980. NAME="AEN3119"
  16981. ></A
  16982. ><TABLE
  16983. BORDER="0"
  16984. BGCOLOR="#E0E0E0"
  16985. CELLPADDING="5"
  16986. ><TR
  16987. ><TD
  16988. ><PRE
  16989. CLASS="php"
  16990. ><?php
  16991. // Let's show all errors
  16992. error_reporting(E_ALL);
  16993.  
  16994. $arr = array('fruit' => 'apple', 'veggie' => 'carrot');
  16995.  
  16996. // Correct
  16997. print $arr['fruit'];  // apple
  16998. print $arr['veggie']; // carrot
  16999.  
  17000. // Incorrect.  This works but also throws a PHP error of
  17001. // level E_NOTICE because of an undefined constant named fruit
  17002. // 
  17003. // Notice: Use of undefined constant fruit - assumed 'fruit' in...
  17004. print $arr[fruit];    // apple
  17005.  
  17006. // Let's define a constant to demonstrate what's going on.  We
  17007. // will assign value 'veggie' to a constant named fruit.
  17008. define('fruit', 'veggie');
  17009.  
  17010. // Notice the difference now
  17011. print $arr['fruit'];  // apple
  17012. print $arr[fruit];    // carrot
  17013.  
  17014. // The following is okay as it's inside a string.  Constants are not
  17015. // looked for within strings so no E_NOTICE error here
  17016. print "Hello $arr[fruit]";      // Hello apple
  17017.  
  17018. // With one exception, braces surrounding arrays within strings
  17019. // allows constants to be looked for
  17020. print "Hello {$arr[fruit]}";    // Hello carrot
  17021. print "Hello {$arr['fruit']}";  // Hello apple
  17022.  
  17023. // This will not work, results in a parse error such as:
  17024. // Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
  17025. // This of course applies to using autoglobals in strings as well
  17026. print "Hello $arr['fruit']";
  17027. print "Hello $_GET['foo']";
  17028.  
  17029. // Concatenation is another option
  17030. print "Hello " . $arr['fruit']; // Hello apple
  17031. ?></PRE
  17032. ></TD
  17033. ></TR
  17034. ></TABLE
  17035. ><P
  17036. ></P
  17037. ></DIV
  17038. >
  17039.      </P
  17040. ><P
  17041. >       When you turn <A
  17042. HREF="#function.error-reporting"
  17043. ><B
  17044. CLASS="function"
  17045. >error_reporting()</B
  17046. ></A
  17047. > up to show
  17048.       <TT
  17049. CLASS="constant"
  17050. ><B
  17051. >E_NOTICE</B
  17052. ></TT
  17053. > level errors (such as setting
  17054.       it to <TT
  17055. CLASS="constant"
  17056. ><B
  17057. >E_ALL</B
  17058. ></TT
  17059. >) then you will see these
  17060.       errors.  By default, <A
  17061. HREF="#ini.error-reporting"
  17062. >       error_reporting</A
  17063. > is turned down to not show them.
  17064.      </P
  17065. ><P
  17066. >       As stated in the <A
  17067. HREF="#language.types.array.syntax"
  17068. >syntax</A
  17069. > section,
  17070.       there must be an expression between the square brackets
  17071.       ('<VAR
  17072. CLASS="literal"
  17073. >[</VAR
  17074. >' and '<VAR
  17075. CLASS="literal"
  17076. >]</VAR
  17077. >').  That means
  17078.       that you can write things like this:
  17079.       <DIV
  17080. CLASS="informalexample"
  17081. ><P
  17082. ></P
  17083. ><A
  17084. NAME="AEN3130"
  17085. ></A
  17086. ><TABLE
  17087. BORDER="0"
  17088. BGCOLOR="#E0E0E0"
  17089. CELLPADDING="5"
  17090. ><TR
  17091. ><TD
  17092. ><PRE
  17093. CLASS="php"
  17094. ><?php
  17095. echo $arr[somefunc($bar)];
  17096. ?></PRE
  17097. ></TD
  17098. ></TR
  17099. ></TABLE
  17100. ><P
  17101. ></P
  17102. ></DIV
  17103. >
  17104.       This is an example of using a function return value
  17105.       as the array index. PHP also knows about constants,
  17106.       as you may have seen the <VAR
  17107. CLASS="literal"
  17108. >E_*</VAR
  17109. > ones
  17110.       before.      
  17111.  
  17112.       <DIV
  17113. CLASS="informalexample"
  17114. ><P
  17115. ></P
  17116. ><A
  17117. NAME="AEN3133"
  17118. ></A
  17119. ><TABLE
  17120. BORDER="0"
  17121. BGCOLOR="#E0E0E0"
  17122. CELLPADDING="5"
  17123. ><TR
  17124. ><TD
  17125. ><PRE
  17126. CLASS="php"
  17127. ><?php
  17128. $error_descriptions[E_ERROR]   = "A fatal error has occured";
  17129. $error_descriptions[E_WARNING] = "PHP issued a warning";
  17130. $error_descriptions[E_NOTICE]  = "This is just an informal notice";
  17131. ?></PRE
  17132. ></TD
  17133. ></TR
  17134. ></TABLE
  17135. ><P
  17136. ></P
  17137. ></DIV
  17138. >
  17139.       Note that <VAR
  17140. CLASS="literal"
  17141. >E_ERROR</VAR
  17142. > is also a valid identifier, 
  17143.       just like <VAR
  17144. CLASS="literal"
  17145. >bar</VAR
  17146. > in the first example. But the last
  17147.       example is in fact the same as writing:
  17148.       <DIV
  17149. CLASS="informalexample"
  17150. ><P
  17151. ></P
  17152. ><A
  17153. NAME="AEN3137"
  17154. ></A
  17155. ><TABLE
  17156. BORDER="0"
  17157. BGCOLOR="#E0E0E0"
  17158. CELLPADDING="5"
  17159. ><TR
  17160. ><TD
  17161. ><PRE
  17162. CLASS="php"
  17163. ><?php
  17164. $error_descriptions[1] = "A fatal error has occured";
  17165. $error_descriptions[2] = "PHP issued a warning";
  17166. $error_descriptions[8] = "This is just an informal notice";
  17167. ?></PRE
  17168. ></TD
  17169. ></TR
  17170. ></TABLE
  17171. ><P
  17172. ></P
  17173. ></DIV
  17174. >
  17175.       because <VAR
  17176. CLASS="literal"
  17177. >E_ERROR</VAR
  17178. > equals <VAR
  17179. CLASS="literal"
  17180. >1</VAR
  17181. >, etc.
  17182.      </P
  17183. ><P
  17184. >       As we already explained in the above examples, 
  17185.       <VAR
  17186. CLASS="literal"
  17187. >$foo[bar]</VAR
  17188. > still works but is wrong.
  17189.       It works, because <VAR
  17190. CLASS="literal"
  17191. >bar</VAR
  17192. > is due to its syntax
  17193.       expected to be a constant expression. However, in this case no 
  17194.       constant with the name <VAR
  17195. CLASS="literal"
  17196. >bar</VAR
  17197. > exists. PHP now
  17198.       assumes that you meant <VAR
  17199. CLASS="literal"
  17200. >bar</VAR
  17201. > literally, 
  17202.       as the string <VAR
  17203. CLASS="literal"
  17204. >"bar"</VAR
  17205. >, but that you forgot
  17206.       to write the quotes.
  17207.      </P
  17208. ><DIV
  17209. CLASS="sect4"
  17210. ><HR><H5
  17211. CLASS="sect4"
  17212. ><A
  17213. NAME="AEN3147"
  17214. >So why is it bad then?</A
  17215. ></H5
  17216. ><P
  17217. >        At some point in the future, the PHP team might want to add another
  17218.        constant or keyword, or you may introduce another constant into your
  17219.        application, and then you get in trouble. For example,
  17220.        you already cannot use the words <VAR
  17221. CLASS="literal"
  17222. >empty</VAR
  17223. > and 
  17224.        <VAR
  17225. CLASS="literal"
  17226. >default</VAR
  17227. > this way, since they are special 
  17228.        <A
  17229. HREF="#reserved"
  17230. >reserved keywords</A
  17231. >.
  17232.       </P
  17233. ><DIV
  17234. CLASS="note"
  17235. ><BLOCKQUOTE
  17236. CLASS="note"
  17237. ><P
  17238. ><B
  17239. >Note: </B
  17240. >
  17241.         To reiterate, inside a double-quoted <A
  17242. HREF="#language.types.string"
  17243. ><B
  17244. CLASS="type"
  17245. >string</B
  17246. ></A
  17247. >, it's
  17248.         valid to not surround array indexes with quotes so
  17249.         <VAR
  17250. CLASS="literal"
  17251. >"$foo[bar]"</VAR
  17252. > is valid.  See the above 
  17253.         examples for details on why as well as the section on
  17254.         <A
  17255. HREF="#language.types.string.parsing"
  17256. >variable parsing 
  17257.         in strings</A
  17258. >.
  17259.        </P
  17260. ></BLOCKQUOTE
  17261. ></DIV
  17262. ></DIV
  17263. ></DIV
  17264. ></DIV
  17265. ><DIV
  17266. CLASS="sect2"
  17267. ><HR><H3
  17268. CLASS="sect2"
  17269. ><A
  17270. NAME="language.types.array.casting"
  17271. >Converting to array</A
  17272. ></H3
  17273. ><P
  17274. >      For any of the types: <A
  17275. HREF="#language.types.integer"
  17276. ><B
  17277. CLASS="type"
  17278. >integer</B
  17279. ></A
  17280. >, <A
  17281. HREF="#language.types.float"
  17282. ><B
  17283. CLASS="type"
  17284. >float</B
  17285. ></A
  17286. >,
  17287.      <A
  17288. HREF="#language.types.string"
  17289. ><B
  17290. CLASS="type"
  17291. >string</B
  17292. ></A
  17293. >, <A
  17294. HREF="#language.types.boolean"
  17295. ><B
  17296. CLASS="type"
  17297. >boolean</B
  17298. ></A
  17299. > and <A
  17300. HREF="#language.types.resource"
  17301. ><B
  17302. CLASS="type"
  17303. >resource</B
  17304. ></A
  17305. >,
  17306.      if you convert a value to an <A
  17307. HREF="#language.types.array"
  17308. ><B
  17309. CLASS="type"
  17310. >array</B
  17311. ></A
  17312. >, you get an array 
  17313.      with one element (with index 0), which is the scalar value you 
  17314.      started with.
  17315.     </P
  17316. ><P
  17317. >      If you convert an <A
  17318. HREF="#language.types.object"
  17319. ><B
  17320. CLASS="type"
  17321. >object</B
  17322. ></A
  17323. > to an array, you get the
  17324.      properties (member variables) of that object as the array's elements. 
  17325.      The keys are the member variable names.
  17326.     </P
  17327. ><P
  17328. >      If you convert a <TT
  17329. CLASS="constant"
  17330. ><B
  17331. >NULL</B
  17332. ></TT
  17333. > value to an array, you get an empty array.
  17334.     </P
  17335. ></DIV
  17336. ><DIV
  17337. CLASS="sect2"
  17338. ><HR><H3
  17339. CLASS="sect2"
  17340. ><A
  17341. NAME="language.types.array.comparing"
  17342. >Comparing</A
  17343. ></H3
  17344. ><P
  17345. >      It is possible to compare arrays by <A
  17346. HREF="#function.array-diff"
  17347. ><B
  17348. CLASS="function"
  17349. >array_diff()</B
  17350. ></A
  17351. > and
  17352.      by <A
  17353. HREF="#language.operators.array"
  17354. >Array operators</A
  17355. >.
  17356.     </P
  17357. ></DIV
  17358. ><DIV
  17359. CLASS="sect2"
  17360. ><HR><H3
  17361. CLASS="sect2"
  17362. ><A
  17363. NAME="language.types.array.examples"
  17364. >Examples</A
  17365. ></H3
  17366. ><P
  17367. >      The array type in PHP is very versatile, so here will be some 
  17368.      examples to show you the full power of arrays.
  17369.     </P
  17370. ><P
  17371. >       <DIV
  17372. CLASS="informalexample"
  17373. ><P
  17374. ></P
  17375. ><A
  17376. NAME="AEN3180"
  17377. ></A
  17378. ><TABLE
  17379. BORDER="0"
  17380. BGCOLOR="#E0E0E0"
  17381. CELLPADDING="5"
  17382. ><TR
  17383. ><TD
  17384. ><PRE
  17385. CLASS="php"
  17386. ><?php
  17387. // this
  17388. $a = array( 'color' => 'red',
  17389.             'taste' => 'sweet',
  17390.             'shape' => 'round',
  17391.             'name'  => 'apple',
  17392.                        4        // key will be 0
  17393.           );
  17394.  
  17395. // is completely equivalent with
  17396. $a['color'] = 'red';
  17397. $a['taste'] = 'sweet';
  17398. $a['shape'] = 'round';
  17399. $a['name']  = 'apple';
  17400. $a[]        = 4;        // key will be 0
  17401.  
  17402. $b[] = 'a';
  17403. $b[] = 'b';
  17404. $b[] = 'c';
  17405. // will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'),
  17406. // or simply array('a', 'b', 'c')
  17407. ?></PRE
  17408. ></TD
  17409. ></TR
  17410. ></TABLE
  17411. ><P
  17412. ></P
  17413. ></DIV
  17414. >
  17415.      </P
  17416. ><TABLE
  17417. WIDTH="100%"
  17418. BORDER="0"
  17419. CELLPADDING="0"
  17420. CELLSPACING="0"
  17421. CLASS="EXAMPLE"
  17422. ><TR
  17423. ><TD
  17424. ><DIV
  17425. CLASS="example"
  17426. ><A
  17427. NAME="AEN3182"
  17428. ></A
  17429. ><P
  17430. ><B
  17431. >Example 11-5. Using array()</B
  17432. ></P
  17433. ><TABLE
  17434. BORDER="0"
  17435. BGCOLOR="#E0E0E0"
  17436. CELLPADDING="5"
  17437. ><TR
  17438. ><TD
  17439. ><PRE
  17440. CLASS="php"
  17441. ><?php
  17442. // Array as (property-)map
  17443. $map = array( 'version'    => 4,
  17444.               'OS'         => 'Linux',
  17445.               'lang'       => 'english',
  17446.               'short_tags' => true
  17447.             );
  17448.             
  17449. // strictly numerical keys
  17450. $array = array( 7,
  17451.                 8,
  17452.                 0,
  17453.                 156,
  17454.                 -10
  17455.               );
  17456. // this is the same as array(0 => 7, 1 => 8, ...)
  17457.  
  17458. $switching = array(         10, // key = 0
  17459.                     5    =>  6,
  17460.                     3    =>  7, 
  17461.                     'a'  =>  4,
  17462.                             11, // key = 6 (maximum of integer-indices was 5)
  17463.                     '8'  =>  2, // key = 8 (integer!)
  17464.                     '02' => 77, // key = '02'
  17465.                     0    => 12  // the value 10 will be overwritten by 12
  17466.                   );
  17467.                   
  17468. // empty array
  17469. $empty = array();         
  17470. ?></PRE
  17471. ></TD
  17472. ></TR
  17473. ></TABLE
  17474. ></DIV
  17475. ></TD
  17476. ></TR
  17477. ></TABLE
  17478. ><TABLE
  17479. WIDTH="100%"
  17480. BORDER="0"
  17481. CELLPADDING="0"
  17482. CELLSPACING="0"
  17483. CLASS="EXAMPLE"
  17484. ><TR
  17485. ><TD
  17486. ><DIV
  17487. CLASS="example"
  17488. ><A
  17489. NAME="language.types.array.examples.loop"
  17490. ></A
  17491. ><P
  17492. ><B
  17493. >Example 11-6. Collection</B
  17494. ></P
  17495. ><TABLE
  17496. BORDER="0"
  17497. BGCOLOR="#E0E0E0"
  17498. CELLPADDING="5"
  17499. ><TR
  17500. ><TD
  17501. ><PRE
  17502. CLASS="php"
  17503. ><?php
  17504. $colors = array('red', 'blue', 'green', 'yellow');
  17505.  
  17506. foreach ($colors as $color) {
  17507.     echo "Do you like $color?\n";
  17508. }
  17509.  
  17510. ?></PRE
  17511. ></TD
  17512. ></TR
  17513. ></TABLE
  17514. ><P
  17515. >       This will output:
  17516.      </P
  17517. ><TABLE
  17518. BORDER="0"
  17519. BGCOLOR="#E0E0E0"
  17520. CELLPADDING="5"
  17521. ><TR
  17522. ><TD
  17523. ><PRE
  17524. CLASS="screen"
  17525. >Do you like red?
  17526. Do you like blue?
  17527. Do you like green?
  17528. Do you like yellow?</PRE
  17529. ></TD
  17530. ></TR
  17531. ></TABLE
  17532. ></DIV
  17533. ></TD
  17534. ></TR
  17535. ></TABLE
  17536. ><P
  17537. >      Note that it is currently not possible to change the values of the array
  17538.      directly in such a loop. 
  17539.       
  17540.      A workaround is the following: 
  17541.      <TABLE
  17542. WIDTH="100%"
  17543. BORDER="0"
  17544. CELLPADDING="0"
  17545. CELLSPACING="0"
  17546. CLASS="EXAMPLE"
  17547. ><TR
  17548. ><TD
  17549. ><DIV
  17550. CLASS="example"
  17551. ><A
  17552. NAME="language.types.array.examples.changeloop"
  17553. ></A
  17554. ><P
  17555. ><B
  17556. >Example 11-7. Collection</B
  17557. ></P
  17558. ><TABLE
  17559. BORDER="0"
  17560. BGCOLOR="#E0E0E0"
  17561. CELLPADDING="5"
  17562. ><TR
  17563. ><TD
  17564. ><PRE
  17565. CLASS="php"
  17566. ><?php
  17567. foreach ($colors as $key => $color) {
  17568.     // won't work:
  17569.     //$color = strtoupper($color);
  17570.     
  17571.     // works:
  17572.     $colors[$key] = strtoupper($color);
  17573. }
  17574. print_r($colors);
  17575. ?></PRE
  17576. ></TD
  17577. ></TR
  17578. ></TABLE
  17579. ><P
  17580. >        This will output:
  17581.       </P
  17582. ><TABLE
  17583. BORDER="0"
  17584. BGCOLOR="#E0E0E0"
  17585. CELLPADDING="5"
  17586. ><TR
  17587. ><TD
  17588. ><PRE
  17589. CLASS="screen"
  17590. >Array
  17591. (
  17592.     [0] => RED
  17593.     [1] => BLUE
  17594.     [2] => GREEN
  17595.     [3] => YELLOW
  17596. )</PRE
  17597. ></TD
  17598. ></TR
  17599. ></TABLE
  17600. ></DIV
  17601. ></TD
  17602. ></TR
  17603. ></TABLE
  17604. >
  17605.     </P
  17606. ><P
  17607. >      This example creates a one-based array.
  17608.      <TABLE
  17609. WIDTH="100%"
  17610. BORDER="0"
  17611. CELLPADDING="0"
  17612. CELLSPACING="0"
  17613. CLASS="EXAMPLE"
  17614. ><TR
  17615. ><TD
  17616. ><DIV
  17617. CLASS="example"
  17618. ><A
  17619. NAME="AEN3197"
  17620. ></A
  17621. ><P
  17622. ><B
  17623. >Example 11-8. One-based index</B
  17624. ></P
  17625. ><TABLE
  17626. BORDER="0"
  17627. BGCOLOR="#E0E0E0"
  17628. CELLPADDING="5"
  17629. ><TR
  17630. ><TD
  17631. ><PRE
  17632. CLASS="php"
  17633. ><?php
  17634. $firstquarter  = array(1 => 'January', 'February', 'March');
  17635. print_r($firstquarter);
  17636. ?></PRE
  17637. ></TD
  17638. ></TR
  17639. ></TABLE
  17640. ><P
  17641. >        This will output:
  17642.       </P
  17643. ><TABLE
  17644. BORDER="0"
  17645. BGCOLOR="#E0E0E0"
  17646. CELLPADDING="5"
  17647. ><TR
  17648. ><TD
  17649. ><PRE
  17650. CLASS="screen"
  17651. >Array 
  17652. (
  17653.     [1] => 'January'
  17654.     [2] => 'February'
  17655.     [3] => 'March'
  17656. )</PRE
  17657. ></TD
  17658. ></TR
  17659. ></TABLE
  17660. ></DIV
  17661. ></TD
  17662. ></TR
  17663. ></TABLE
  17664. >
  17665.     </P
  17666. ><TABLE
  17667. WIDTH="100%"
  17668. BORDER="0"
  17669. CELLPADDING="0"
  17670. CELLSPACING="0"
  17671. CLASS="EXAMPLE"
  17672. ><TR
  17673. ><TD
  17674. ><DIV
  17675. CLASS="example"
  17676. ><A
  17677. NAME="AEN3202"
  17678. ></A
  17679. ><P
  17680. ><B
  17681. >Example 11-9. Filling an array</B
  17682. ></P
  17683. ><TABLE
  17684. BORDER="0"
  17685. BGCOLOR="#E0E0E0"
  17686. CELLPADDING="5"
  17687. ><TR
  17688. ><TD
  17689. ><PRE
  17690. CLASS="php"
  17691. ><?php
  17692. // fill an array with all items from a directory
  17693. $handle = opendir('.');
  17694. while (false !== ($file = readdir($handle))) {
  17695.     $files[] = $file;
  17696. }
  17697. closedir($handle); 
  17698. ?></PRE
  17699. ></TD
  17700. ></TR
  17701. ></TABLE
  17702. ></DIV
  17703. ></TD
  17704. ></TR
  17705. ></TABLE
  17706. ><P
  17707. >      Arrays are ordered. You can also change the order using various
  17708.      sorting functions. See the <A
  17709. HREF="#ref.array"
  17710. >array
  17711.      functions</A
  17712. > section for more information. You can count
  17713.      the number of items in an array using the
  17714.      <A
  17715. HREF="#function.count"
  17716. ><B
  17717. CLASS="function"
  17718. >count()</B
  17719. ></A
  17720. > function.
  17721.     </P
  17722. ><TABLE
  17723. WIDTH="100%"
  17724. BORDER="0"
  17725. CELLPADDING="0"
  17726. CELLSPACING="0"
  17727. CLASS="EXAMPLE"
  17728. ><TR
  17729. ><TD
  17730. ><DIV
  17731. CLASS="example"
  17732. ><A
  17733. NAME="AEN3208"
  17734. ></A
  17735. ><P
  17736. ><B
  17737. >Example 11-10. Sorting an array</B
  17738. ></P
  17739. ><TABLE
  17740. BORDER="0"
  17741. BGCOLOR="#E0E0E0"
  17742. CELLPADDING="5"
  17743. ><TR
  17744. ><TD
  17745. ><PRE
  17746. CLASS="php"
  17747. ><?php
  17748. sort($files);
  17749. print_r($files);
  17750. ?></PRE
  17751. ></TD
  17752. ></TR
  17753. ></TABLE
  17754. ></DIV
  17755. ></TD
  17756. ></TR
  17757. ></TABLE
  17758. ><P
  17759. >      Because the value of an array can be anything, it can also be
  17760.      another array. This way you can make recursive and
  17761.      multi-dimensional arrays.
  17762.     </P
  17763. ><TABLE
  17764. WIDTH="100%"
  17765. BORDER="0"
  17766. CELLPADDING="0"
  17767. CELLSPACING="0"
  17768. CLASS="EXAMPLE"
  17769. ><TR
  17770. ><TD
  17771. ><DIV
  17772. CLASS="example"
  17773. ><A
  17774. NAME="AEN3212"
  17775. ></A
  17776. ><P
  17777. ><B
  17778. >Example 11-11. Recursive and multi-dimensional arrays</B
  17779. ></P
  17780. ><TABLE
  17781. BORDER="0"
  17782. BGCOLOR="#E0E0E0"
  17783. CELLPADDING="5"
  17784. ><TR
  17785. ><TD
  17786. ><PRE
  17787. CLASS="php"
  17788. ><?php
  17789. $fruits = array ( "fruits"  => array ( "a" => "orange",
  17790.                                        "b" => "banana",
  17791.                                        "c" => "apple"
  17792.                                      ),
  17793.                   "numbers" => array ( 1,
  17794.                                        2,
  17795.                                        3,
  17796.                                        4,
  17797.                                        5,
  17798.                                        6
  17799.                                      ),
  17800.                   "holes"   => array (      "first",
  17801.                                        5 => "second",
  17802.                                             "third"
  17803.                                      )
  17804.                 );
  17805.  
  17806. // Some examples to address values in the array above 
  17807. echo $fruits["holes"][5];    // prints "second"
  17808. echo $fruits["fruits"]["a"]; // prints "orange"
  17809. unset($fruits["holes"][0]);  // remove "first"
  17810.  
  17811. // Create a new multi-dimensional array
  17812. $juices["apple"]["green"] = "good"; 
  17813. ?></PRE
  17814. ></TD
  17815. ></TR
  17816. ></TABLE
  17817. ></DIV
  17818. ></TD
  17819. ></TR
  17820. ></TABLE
  17821. ><P
  17822. >      You should be aware that array assignment always involves
  17823.      value copying. You need to use the reference operator to copy
  17824.      an array by reference.
  17825.      <DIV
  17826. CLASS="informalexample"
  17827. ><P
  17828. ></P
  17829. ><A
  17830. NAME="AEN3216"
  17831. ></A
  17832. ><TABLE
  17833. BORDER="0"
  17834. BGCOLOR="#E0E0E0"
  17835. CELLPADDING="5"
  17836. ><TR
  17837. ><TD
  17838. ><PRE
  17839. CLASS="php"
  17840. ><?php
  17841. $arr1 = array(2, 3);
  17842. $arr2 = $arr1;
  17843. $arr2[] = 4; // $arr2 is changed,
  17844.              // $arr1 is still array(2, 3)
  17845.              
  17846. $arr3 = &$arr1;
  17847. $arr3[] = 4; // now $arr1 and $arr3 are the same
  17848. ?></PRE
  17849. ></TD
  17850. ></TR
  17851. ></TABLE
  17852. ><P
  17853. ></P
  17854. ></DIV
  17855. >
  17856.     </P
  17857. ></DIV
  17858. ></DIV
  17859. ><DIV
  17860. CLASS="sect1"
  17861. ><HR><H2
  17862. CLASS="sect1"
  17863. ><A
  17864. NAME="language.types.object"
  17865. >Objects</A
  17866. ></H2
  17867. ><DIV
  17868. CLASS="sect2"
  17869. ><H3
  17870. CLASS="sect2"
  17871. ><A
  17872. NAME="language.types.object.init"
  17873. >Object Initialization</A
  17874. ></H3
  17875. ><P
  17876. >      To initialize an object, you use the <VAR
  17877. CLASS="literal"
  17878. >new</VAR
  17879. >
  17880.      statement to instantiate the object to a variable.
  17881.  
  17882.      <DIV
  17883. CLASS="informalexample"
  17884. ><P
  17885. ></P
  17886. ><A
  17887. NAME="AEN3224"
  17888. ></A
  17889. ><TABLE
  17890. BORDER="0"
  17891. BGCOLOR="#E0E0E0"
  17892. CELLPADDING="5"
  17893. ><TR
  17894. ><TD
  17895. ><PRE
  17896. CLASS="php"
  17897. ><?php
  17898. class foo
  17899. {
  17900.     function do_foo()
  17901.     {
  17902.         echo "Doing foo."; 
  17903.     }
  17904. }
  17905.  
  17906. $bar = new foo;
  17907. $bar->do_foo();
  17908. ?></PRE
  17909. ></TD
  17910. ></TR
  17911. ></TABLE
  17912. ><P
  17913. ></P
  17914. ></DIV
  17915. >
  17916.     </P
  17917. ><P
  17918. >      For a full discussion, please read the section <A
  17919. HREF="#language.oop"
  17920. >Classes and Objects</A
  17921. >.
  17922.     </P
  17923. ></DIV
  17924. ><DIV
  17925. CLASS="sect2"
  17926. ><HR><H3
  17927. CLASS="sect2"
  17928. ><A
  17929. NAME="language.types.object.casting"
  17930. >Converting to object</A
  17931. ></H3
  17932. ><P
  17933. >      If an object is converted to an object, it is not modified. If a value
  17934.      of any other type is converted to an object, a new instance of the
  17935.      <VAR
  17936. CLASS="literal"
  17937. >stdClass</VAR
  17938. > built in class is created. If the value
  17939.      was null, the new instance will be empty. For any other value, a
  17940.      member variable named <VAR
  17941. CLASS="literal"
  17942. >scalar</VAR
  17943. > will contain the
  17944.      value.
  17945.      <DIV
  17946. CLASS="informalexample"
  17947. ><P
  17948. ></P
  17949. ><A
  17950. NAME="AEN3233"
  17951. ></A
  17952. ><TABLE
  17953. BORDER="0"
  17954. BGCOLOR="#E0E0E0"
  17955. CELLPADDING="5"
  17956. ><TR
  17957. ><TD
  17958. ><PRE
  17959. CLASS="php"
  17960. ><?php
  17961. $obj = (object) 'ciao';
  17962. echo $obj->scalar;  // outputs 'ciao'
  17963. ?></PRE
  17964. ></TD
  17965. ></TR
  17966. ></TABLE
  17967. ><P
  17968. ></P
  17969. ></DIV
  17970. >
  17971.     </P
  17972. ></DIV
  17973. ></DIV
  17974. ><DIV
  17975. CLASS="sect1"
  17976. ><HR><H2
  17977. CLASS="sect1"
  17978. ><A
  17979. NAME="language.types.resource"
  17980. >Resource</A
  17981. ></H2
  17982. ><P
  17983. >      A resource is a special variable, holding
  17984.      a reference to an external resource. Resources
  17985.      are created and used by special functions.
  17986.      See the <A
  17987. HREF="#resource"
  17988. >appendix</A
  17989.      for a listing of all these
  17990.      functions and the corresponding resource types.
  17991.     </P
  17992. ><DIV
  17993. CLASS="note"
  17994. ><BLOCKQUOTE
  17995. CLASS="note"
  17996. ><P
  17997. ><B
  17998. >Note: </B
  17999. >
  18000.       The resource type was introduced in PHP 4
  18001.      </P
  18002. ></BLOCKQUOTE
  18003. ></DIV
  18004. ><DIV
  18005. CLASS="sect2"
  18006. ><HR><H3
  18007. CLASS="sect2"
  18008. ><A
  18009. NAME="language.types.resource.casting"
  18010. >Converting to resource</A
  18011. ></H3
  18012. ><P
  18013. >      As resource types hold special handlers to opened
  18014.      files, database connections, image canvas areas and
  18015.      the like, you cannot convert any value to a resource.
  18016.     </P
  18017. ></DIV
  18018. ><DIV
  18019. CLASS="sect2"
  18020. ><HR><H3
  18021. CLASS="sect2"
  18022. ><A
  18023. NAME="language.types.resource.self-destruct"
  18024. >Freeing resources</A
  18025. ></H3
  18026. ><P
  18027. >      Due to the reference-counting system introduced
  18028.      with PHP 4's Zend Engine, it is automatically detected
  18029.      when a resource is no longer referred to (just
  18030.      like Java). When this is
  18031.      the case, all resources that were in use for this 
  18032.      resource are made free by the garbage collector.
  18033.      For this reason, it is rarely ever necessary to
  18034.      free the memory manually by using some free_result
  18035.      function.
  18036.      <DIV
  18037. CLASS="note"
  18038. ><BLOCKQUOTE
  18039. CLASS="note"
  18040. ><P
  18041. ><B
  18042. >Note: </B
  18043. >
  18044.        Persistent database links are special, they 
  18045.        are <SPAN
  18046. CLASS="emphasis"
  18047. ><I
  18048. CLASS="emphasis"
  18049. >not</I
  18050. ></SPAN
  18051. > destroyed by the 
  18052.        garbage collector. See also the section about <A
  18053. HREF="#features.persistent-connections"
  18054. >persistent 
  18055.        connections</A
  18056. >.
  18057.       </P
  18058. ></BLOCKQUOTE
  18059. ></DIV
  18060. >
  18061.     </P
  18062. ></DIV
  18063. ></DIV
  18064. ><DIV
  18065. CLASS="sect1"
  18066. ><HR><H2
  18067. CLASS="sect1"
  18068. ><A
  18069. NAME="language.types.null"
  18070. >NULL</A
  18071. ></H2
  18072. ><P
  18073. >     The special <TT
  18074. CLASS="constant"
  18075. ><B
  18076. >NULL</B
  18077. ></TT
  18078. > value represents 
  18079.     that a variable has no value. <TT
  18080. CLASS="constant"
  18081. ><B
  18082. >NULL</B
  18083. ></TT
  18084. > is the only possible value of type
  18085.     <A
  18086. HREF="#language.types.null"
  18087. ><B
  18088. CLASS="type"
  18089. >NULL</B
  18090. ></A
  18091. >.
  18092.    </P
  18093. ><DIV
  18094. CLASS="note"
  18095. ><BLOCKQUOTE
  18096. CLASS="note"
  18097. ><P
  18098. ><B
  18099. >Note: </B
  18100. >
  18101.       The null type was introduced in PHP 4
  18102.      </P
  18103. ></BLOCKQUOTE
  18104. ></DIV
  18105. ><P
  18106. >      A variable is considered to be <TT
  18107. CLASS="constant"
  18108. ><B
  18109. >NULL</B
  18110. ></TT
  18111. > if
  18112.      <P
  18113. ></P
  18114. ><UL
  18115. ><LI
  18116. ><P
  18117. >         it has been assigned the constant <TT
  18118. CLASS="constant"
  18119. ><B
  18120. >NULL</B
  18121. ></TT
  18122. >.
  18123.        </P
  18124. ></LI
  18125. ><LI
  18126. ><P
  18127. >         it has not been set to any value yet.
  18128.        </P
  18129. ></LI
  18130. ><LI
  18131. ><P
  18132. >         it has been <A
  18133. HREF="#function.unset"
  18134. ><B
  18135. CLASS="function"
  18136. >unset()</B
  18137. ></A
  18138. >.
  18139.        </P
  18140. ></LI
  18141. ></UL
  18142. >
  18143.     </P
  18144. ><DIV
  18145. CLASS="sect2"
  18146. ><HR><H3
  18147. CLASS="sect2"
  18148. ><A
  18149. NAME="language.types.null.syntax"
  18150. >Syntax</A
  18151. ></H3
  18152. ><P
  18153. >      There is only one value of type <TT
  18154. CLASS="constant"
  18155. ><B
  18156. >NULL</B
  18157. ></TT
  18158. >, and that is 
  18159.      the case-insensitive keyword <TT
  18160. CLASS="constant"
  18161. ><B
  18162. >NULL</B
  18163. ></TT
  18164. >.
  18165.      <DIV
  18166. CLASS="informalexample"
  18167. ><P
  18168. ></P
  18169. ><A
  18170. NAME="AEN3275"
  18171. ></A
  18172. ><TABLE
  18173. BORDER="0"
  18174. BGCOLOR="#E0E0E0"
  18175. CELLPADDING="5"
  18176. ><TR
  18177. ><TD
  18178. ><PRE
  18179. CLASS="php"
  18180. ><?php
  18181. $var = NULL;       
  18182. ?></PRE
  18183. ></TD
  18184. ></TR
  18185. ></TABLE
  18186. ><P
  18187. ></P
  18188. ></DIV
  18189. >
  18190.     </P
  18191. ><P
  18192. >      See also <A
  18193. HREF="#function.is-null"
  18194. ><B
  18195. CLASS="function"
  18196. >is_null()</B
  18197. ></A
  18198. > and <A
  18199. HREF="#function.unset"
  18200. ><B
  18201. CLASS="function"
  18202. >unset()</B
  18203. ></A
  18204. >.
  18205.     </P
  18206. ></DIV
  18207. ></DIV
  18208. ><DIV
  18209. CLASS="sect1"
  18210. ><HR><H2
  18211. CLASS="sect1"
  18212. ><A
  18213. NAME="language.pseudo-types"
  18214. >Pseudo-types used in this documentation</A
  18215. ></H2
  18216. ><DIV
  18217. CLASS="sect2"
  18218. ><H3
  18219. CLASS="sect2"
  18220. ><A
  18221. NAME="language.types.mixed"
  18222. >mixed</A
  18223. ></H3
  18224. ><P
  18225. >      <VAR
  18226. CLASS="literal"
  18227. >mixed</VAR
  18228. > indicates that a parameter may accept multiple (but not
  18229.      necessarily all) types.
  18230.     </P
  18231. ><P
  18232. >      <A
  18233. HREF="#function.gettype"
  18234. ><B
  18235. CLASS="function"
  18236. >gettype()</B
  18237. ></A
  18238. > for example will accept all PHP types,
  18239.      while <A
  18240. HREF="#function.str-replace"
  18241. ><B
  18242. CLASS="function"
  18243. >str_replace()</B
  18244. ></A
  18245. > will accept strings and arrays.
  18246.     </P
  18247. ></DIV
  18248. ><DIV
  18249. CLASS="sect2"
  18250. ><HR><H3
  18251. CLASS="sect2"
  18252. ><A
  18253. NAME="language.types.number"
  18254. >number</A
  18255. ></H3
  18256. ><P
  18257. >      <VAR
  18258. CLASS="literal"
  18259. >number</VAR
  18260. > indicates that a parameter can be either
  18261.      <A
  18262. HREF="#language.types.integer"
  18263. ><B
  18264. CLASS="type"
  18265. >integer</B
  18266. ></A
  18267. > or <A
  18268. HREF="#language.types.float"
  18269. ><B
  18270. CLASS="type"
  18271. >float</B
  18272. ></A
  18273. >.
  18274.     </P
  18275. ></DIV
  18276. ><DIV
  18277. CLASS="sect2"
  18278. ><HR><H3
  18279. CLASS="sect2"
  18280. ><A
  18281. NAME="language.types.callback"
  18282. >callback</A
  18283. ></H3
  18284. ><P
  18285. >      Some functions like <A
  18286. HREF="#function.call-user-func"
  18287. ><B
  18288. CLASS="function"
  18289. >call_user_func()</B
  18290. ></A
  18291.      or <A
  18292. HREF="#function.usort"
  18293. ><B
  18294. CLASS="function"
  18295. >usort()</B
  18296. ></A
  18297. > accept user defined 
  18298.      callback functions as a parameter. Callback functions can not only
  18299.      be simple functions but also object methods including static class
  18300.      methods. 
  18301.     </P
  18302. ><P
  18303. >      A PHP function is simply passed by its name as a string. You can
  18304.      pass any builtin or user defined function with the exception of 
  18305.      <A
  18306. HREF="#function.array"
  18307. ><B
  18308. CLASS="function"
  18309. >array()</B
  18310. ></A
  18311. >, 
  18312.      <A
  18313. HREF="#function.echo"
  18314. ><B
  18315. CLASS="function"
  18316. >echo()</B
  18317. ></A
  18318. >, 
  18319.      <A
  18320. HREF="#function.empty"
  18321. ><B
  18322. CLASS="function"
  18323. >empty()</B
  18324. ></A
  18325. >, 
  18326.      <A
  18327. HREF="#function.eval"
  18328. ><B
  18329. CLASS="function"
  18330. >eval()</B
  18331. ></A
  18332. >, 
  18333.      <A
  18334. HREF="#function.exit"
  18335. ><B
  18336. CLASS="function"
  18337. >exit()</B
  18338. ></A
  18339. >, 
  18340.      <A
  18341. HREF="#function.isset"
  18342. ><B
  18343. CLASS="function"
  18344. >isset()</B
  18345. ></A
  18346. >, 
  18347.      <A
  18348. HREF="#function.list"
  18349. ><B
  18350. CLASS="function"
  18351. >list()</B
  18352. ></A
  18353. >, 
  18354.      <A
  18355. HREF="#function.print"
  18356. ><B
  18357. CLASS="function"
  18358. >print()</B
  18359. ></A
  18360. > and 
  18361.      <A
  18362. HREF="#function.unset"
  18363. ><B
  18364. CLASS="function"
  18365. >unset()</B
  18366. ></A
  18367. >.
  18368.     </P
  18369. ><P
  18370. >      A method of an instantiated object is passed as an array containing
  18371.      an object as the element with index 0 and a method name as the 
  18372.      element with index 1.
  18373.     </P
  18374. ><P
  18375. >      Static class methods can also be passed without instantiating an
  18376.      object of that class by passing the class name instead of an 
  18377.      object as the element with index 0.
  18378.     </P
  18379. ><P
  18380. >      <TABLE
  18381. WIDTH="100%"
  18382. BORDER="0"
  18383. CELLPADDING="0"
  18384. CELLSPACING="0"
  18385. CLASS="EXAMPLE"
  18386. ><TR
  18387. ><TD
  18388. ><DIV
  18389. CLASS="example"
  18390. ><A
  18391. NAME="AEN3313"
  18392. ></A
  18393. ><P
  18394. ><B
  18395. >Example 11-12. 
  18396.        Callback function examples
  18397.       </B
  18398. ></P
  18399. ><TABLE
  18400. BORDER="0"
  18401. BGCOLOR="#E0E0E0"
  18402. CELLPADDING="5"
  18403. ><TR
  18404. ><TD
  18405. ><PRE
  18406. CLASS="php"
  18407. ><?php 
  18408. // An example callback function
  18409. function my_callback_function() {
  18410.     echo 'hello world!';
  18411. }
  18412.  
  18413. // An example callback method
  18414. class MyClass {
  18415.     function myCallbackMethod() {
  18416.         echo 'Hello World!';
  18417.     }
  18418. }
  18419.  
  18420. // Type 1: Simple callback
  18421. call_user_func('my_callback_function'); 
  18422.  
  18423. // Type 2: Static class method call
  18424. call_user_func(array('MyClass', 'myCallbackMethod')); 
  18425.  
  18426. // Type 3: Object method call
  18427. $obj = new MyClass();
  18428. call_user_func(array(&$obj, 'myCallbackMethod'));
  18429. ?></PRE
  18430. ></TD
  18431. ></TR
  18432. ></TABLE
  18433. ></DIV
  18434. ></TD
  18435. ></TR
  18436. ></TABLE
  18437. >
  18438.     </P
  18439. ></DIV
  18440. ></DIV
  18441. ><DIV
  18442. CLASS="sect1"
  18443. ><HR><H2
  18444. CLASS="sect1"
  18445. ><A
  18446. NAME="language.types.type-juggling"
  18447. >Type Juggling</A
  18448. ></H2
  18449. ><P
  18450. >     PHP does not require (or support) explicit type definition in
  18451.     variable declaration; a variable's type is determined by the
  18452.     context in which that variable is used. That is to say, if you
  18453.     assign a string value to variable <VAR
  18454. CLASS="parameter"
  18455. >$var</VAR
  18456. >,
  18457.     <VAR
  18458. CLASS="parameter"
  18459. >$var</VAR
  18460. > becomes a string. If you then assign an
  18461.     integer value to <VAR
  18462. CLASS="parameter"
  18463. >$var</VAR
  18464. >, it becomes an
  18465.     integer.
  18466.    </P
  18467. ><P
  18468. >     An example of PHP's automatic type conversion is the addition
  18469.     operator '+'. If any of the operands is a float, then all
  18470.     operands are evaluated as floats, and the result will be a
  18471.     float. Otherwise, the operands will be interpreted as integers,
  18472.     and the result will also be an integer. Note that this does NOT
  18473.     change the types of the operands themselves; the only change is in
  18474.     how the operands are evaluated.
  18475.     <DIV
  18476. CLASS="informalexample"
  18477. ><P
  18478. ></P
  18479. ><A
  18480. NAME="AEN3323"
  18481. ></A
  18482. ><TABLE
  18483. BORDER="0"
  18484. BGCOLOR="#E0E0E0"
  18485. CELLPADDING="5"
  18486. ><TR
  18487. ><TD
  18488. ><PRE
  18489. CLASS="php"
  18490. ><?php
  18491. $foo = "0";  // $foo is string (ASCII 48)
  18492. $foo += 2;   // $foo is now an integer (2)
  18493. $foo = $foo + 1.3;  // $foo is now a float (3.3)
  18494. $foo = 5 + "10 Little Piggies"; // $foo is integer (15)
  18495. $foo = 5 + "10 Small Pigs";     // $foo is integer (15)
  18496. ?></PRE
  18497. ></TD
  18498. ></TR
  18499. ></TABLE
  18500. ><P
  18501. ></P
  18502. ></DIV
  18503. >
  18504.    </P
  18505. ><P
  18506. >     If the last two examples above seem odd, see <A
  18507. HREF="#language.types.string.conversion"
  18508. >String
  18509.     conversion to numbers</A
  18510. >.
  18511.    </P
  18512. ><P
  18513. >     If you wish to force a variable to be evaluated as a certain type,
  18514.     see the section on <A
  18515. HREF="#language.types.typecasting"
  18516. >Type
  18517.     casting</A
  18518. >. If you wish to change the type of a variable, see
  18519.     <A
  18520. HREF="#function.settype"
  18521. ><B
  18522. CLASS="function"
  18523. >settype()</B
  18524. ></A
  18525. >.
  18526.    </P
  18527. ><P
  18528. >     If you would like to test any of the examples in this section, you
  18529.     can use the <A
  18530. HREF="#function.var-dump"
  18531. ><B
  18532. CLASS="function"
  18533. >var_dump()</B
  18534. ></A
  18535. > function.
  18536.    </P
  18537. ><DIV
  18538. CLASS="note"
  18539. ><BLOCKQUOTE
  18540. CLASS="note"
  18541. ><P
  18542. ><B
  18543. >Note: </B
  18544. >
  18545.      The behaviour of an automatic conversion to array is currently
  18546.      undefined.
  18547.     </P
  18548. ><P
  18549. >      <DIV
  18550. CLASS="informalexample"
  18551. ><P
  18552. ></P
  18553. ><A
  18554. NAME="AEN3335"
  18555. ></A
  18556. ><TABLE
  18557. BORDER="0"
  18558. BGCOLOR="#E0E0E0"
  18559. CELLPADDING="5"
  18560. ><TR
  18561. ><TD
  18562. ><PRE
  18563. CLASS="php"
  18564. ><?php
  18565. $a = "1";     // $a is a string
  18566. $a[0] = "f";  // What about string offsets? What happens?
  18567. ?></PRE
  18568. ></TD
  18569. ></TR
  18570. ></TABLE
  18571. ><P
  18572. ></P
  18573. ></DIV
  18574. >
  18575.     </P
  18576. ><P
  18577. >      Since PHP (for historical reasons) supports indexing into strings
  18578.      via offsets using the same syntax as array indexing, the example
  18579.      above leads to a problem: should $a become an array with its first
  18580.      element being "f", or should "f" become the first character of the
  18581.      string $a?
  18582.     </P
  18583. ><P
  18584. >      The current versions of PHP interpret the second assignment as
  18585.      a string offset identification, so $a becomes "f", the result
  18586.      of this automatic conversion however should be considered
  18587.      undefined. PHP 4 introduced the new curly bracket syntax to access
  18588.      characters in string, use this syntax instead of the one presented
  18589.      above: 
  18590.      <DIV
  18591. CLASS="informalexample"
  18592. ><P
  18593. ></P
  18594. ><A
  18595. NAME="AEN3339"
  18596. ></A
  18597. ><TABLE
  18598. BORDER="0"
  18599. BGCOLOR="#E0E0E0"
  18600. CELLPADDING="5"
  18601. ><TR
  18602. ><TD
  18603. ><PRE
  18604. CLASS="php"
  18605. ><?php
  18606. $a    = "abc"; // $a is a string
  18607. $a{1} = "f";   // $a is now "afc"
  18608. ?></PRE
  18609. ></TD
  18610. ></TR
  18611. ></TABLE
  18612. ><P
  18613. ></P
  18614. ></DIV
  18615. >
  18616.      See the section titled <A
  18617. HREF="#language.types.string.substr"
  18618. >String
  18619.      access by character</A
  18620. > for more information.
  18621.     </P
  18622. ></BLOCKQUOTE
  18623. ></DIV
  18624. ><DIV
  18625. CLASS="sect2"
  18626. ><HR><H3
  18627. CLASS="sect2"
  18628. ><A
  18629. NAME="language.types.typecasting"
  18630. >Type Casting</A
  18631. ></H3
  18632. ><P
  18633. >      Type casting in PHP works much as it does in C: the name of the
  18634.      desired type is written in parentheses before the variable which
  18635.      is to be cast.
  18636.      <DIV
  18637. CLASS="informalexample"
  18638. ><P
  18639. ></P
  18640. ><A
  18641. NAME="AEN3345"
  18642. ></A
  18643. ><TABLE
  18644. BORDER="0"
  18645. BGCOLOR="#E0E0E0"
  18646. CELLPADDING="5"
  18647. ><TR
  18648. ><TD
  18649. ><PRE
  18650. CLASS="php"
  18651. ><?php
  18652. $foo = 10;   // $foo is an integer
  18653. $bar = (boolean) $foo;   // $bar is a boolean
  18654. ?></PRE
  18655. ></TD
  18656. ></TR
  18657. ></TABLE
  18658. ><P
  18659. ></P
  18660. ></DIV
  18661. >
  18662.     </P
  18663. ><P
  18664. >      The casts allowed are:
  18665.      <P
  18666. ></P
  18667. ><UL
  18668. ><LI
  18669. ><P
  18670. >(int), (integer) - cast to integer</P
  18671. ></LI
  18672. ><LI
  18673. ><P
  18674. >(bool), (boolean) - cast to boolean</P
  18675. ></LI
  18676. ><LI
  18677. ><P
  18678. >(float), (double), (real) - cast to float</P
  18679. ></LI
  18680. ><LI
  18681. ><P
  18682. >(string) - cast to string</P
  18683. ></LI
  18684. ><LI
  18685. ><P
  18686. >(array) - cast to array</P
  18687. ></LI
  18688. ><LI
  18689. ><P
  18690. >(object) - cast to object</P
  18691. ></LI
  18692. ></UL
  18693. >
  18694.     </P
  18695. ><P
  18696. >      Note that tabs and spaces are allowed inside the parentheses, so
  18697.      the following are functionally equivalent:
  18698.      <DIV
  18699. CLASS="informalexample"
  18700. ><P
  18701. ></P
  18702. ><A
  18703. NAME="AEN3362"
  18704. ></A
  18705. ><TABLE
  18706. BORDER="0"
  18707. BGCOLOR="#E0E0E0"
  18708. CELLPADDING="5"
  18709. ><TR
  18710. ><TD
  18711. ><PRE
  18712. CLASS="php"
  18713. ><?php
  18714. $foo = (int) $bar;
  18715. $foo = ( int ) $bar;
  18716. ?></PRE
  18717. ></TD
  18718. ></TR
  18719. ></TABLE
  18720. ><P
  18721. ></P
  18722. ></DIV
  18723. >
  18724.     </P
  18725. ><DIV
  18726. CLASS="note"
  18727. ><BLOCKQUOTE
  18728. CLASS="note"
  18729. ><P
  18730. ><B
  18731. >Note: </B
  18732. >
  18733.       Instead of casting a variable to string, you can also enclose
  18734.       the variable in double quotes.
  18735.      <DIV
  18736. CLASS="informalexample"
  18737. ><P
  18738. ></P
  18739. ><A
  18740. NAME="AEN3366"
  18741. ></A
  18742. ><TABLE
  18743. BORDER="0"
  18744. BGCOLOR="#E0E0E0"
  18745. CELLPADDING="5"
  18746. ><TR
  18747. ><TD
  18748. ><PRE
  18749. CLASS="php"
  18750. ><?php
  18751. $foo = 10;            // $foo is an integer
  18752. $str = "$foo";        // $str is a string
  18753. $fst = (string) $foo; // $fst is also a string
  18754.  
  18755. // This prints out that "they are the same"
  18756. if ($fst === $str) {
  18757.     echo "they are the same";
  18758. }
  18759. ?></PRE
  18760. ></TD
  18761. ></TR
  18762. ></TABLE
  18763. ><P
  18764. ></P
  18765. ></DIV
  18766. >
  18767.      </P
  18768. ></BLOCKQUOTE
  18769. ></DIV
  18770. ><P
  18771. >      It may not be obvious exactly what will happen when casting
  18772.      between certain types. For more info, see these sections:
  18773.     
  18774.      <P
  18775. ></P
  18776. ><UL
  18777. ><LI
  18778. ><P
  18779. ><A
  18780. HREF="#language.types.boolean.casting"
  18781. >Converting to 
  18782.         boolean</A
  18783. ></P
  18784. ></LI
  18785. ><LI
  18786. ><P
  18787. ><A
  18788. HREF="#language.types.integer.casting"
  18789. >Converting to 
  18790.         integer</A
  18791. ></P
  18792. ></LI
  18793. ><LI
  18794. ><P
  18795. ><A
  18796. HREF="#language.types.float.casting"
  18797. >Converting to 
  18798.         float</A
  18799. ></P
  18800. ></LI
  18801. ><LI
  18802. ><P
  18803. ><A
  18804. HREF="#language.types.string.casting"
  18805. >Converting to 
  18806.         string</A
  18807. ></P
  18808. ></LI
  18809. ><LI
  18810. ><P
  18811. ><A
  18812. HREF="#language.types.array.casting"
  18813. >Converting to 
  18814.         array</A
  18815. ></P
  18816. ></LI
  18817. ><LI
  18818. ><P
  18819. ><A
  18820. HREF="#language.types.object.casting"
  18821. >Converting to 
  18822.         object</A
  18823. ></P
  18824. ></LI
  18825. ><LI
  18826. ><P
  18827. ><A
  18828. HREF="#language.types.resource.casting"
  18829. >Converting to 
  18830.         resource</A
  18831. ></P
  18832. ></LI
  18833. ><LI
  18834. ><P
  18835. >         <A
  18836. HREF="#types.comparisons"
  18837. >The type comparison tables</A
  18838. >
  18839.        </P
  18840. ></LI
  18841. ></UL
  18842. >
  18843.     </P
  18844. ></DIV
  18845. ></DIV
  18846. ></DIV
  18847. ><DIV
  18848. CLASS="chapter"
  18849. ><HR><H1
  18850. ><A
  18851. NAME="language.variables"
  18852. >Chapter 12. Variables</A
  18853. ></H1
  18854. ><DIV
  18855. CLASS="sect1"
  18856. ><H2
  18857. CLASS="sect1"
  18858. ><A
  18859. NAME="language.variables.basics"
  18860. >Basics</A
  18861. ></H2
  18862. ><P
  18863. >     Variables in PHP are represented by a dollar sign followed by the
  18864.     name of the variable. The variable name is case-sensitive.
  18865.    </P
  18866. ><P
  18867. >     Variable names follow the same rules as other labels in PHP. A
  18868.     valid variable name starts with a letter or underscore, followed
  18869.     by any number of letters, numbers, or underscores. As a regular
  18870.     expression, it would be expressed thus:
  18871.     '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
  18872.    </P
  18873. ><DIV
  18874. CLASS="note"
  18875. ><BLOCKQUOTE
  18876. CLASS="note"
  18877. ><P
  18878. ><B
  18879. >Note: </B
  18880. >
  18881.      For our purposes here, a letter is a-z, A-Z, and the ASCII
  18882.      characters from 127 through 255 (0x7f-0xff).
  18883.     </P
  18884. ></BLOCKQUOTE
  18885. ></DIV
  18886. ><P
  18887. >     For information on variable related functions, see the
  18888.     <A
  18889. HREF="#ref.var"
  18890. >Variable Functions Reference</A
  18891. >.
  18892.    </P
  18893. ><P
  18894. >     <DIV
  18895. CLASS="informalexample"
  18896. ><P
  18897. ></P
  18898. ><A
  18899. NAME="AEN3405"
  18900. ></A
  18901. ><TABLE
  18902. BORDER="0"
  18903. BGCOLOR="#E0E0E0"
  18904. CELLPADDING="5"
  18905. ><TR
  18906. ><TD
  18907. ><PRE
  18908. CLASS="php"
  18909. ><?php
  18910. $var = "Bob";
  18911. $Var = "Joe";
  18912. echo "$var, $Var";      // outputs "Bob, Joe"
  18913.  
  18914. $4site = 'not yet';     // invalid; starts with a number
  18915. $_4site = 'not yet';    // valid; starts with an underscore
  18916. $t├ñyte = 'mansikka';    // valid; '├ñ' is (Extended) ASCII 228.
  18917. ?></PRE
  18918. ></TD
  18919. ></TR
  18920. ></TABLE
  18921. ><P
  18922. ></P
  18923. ></DIV
  18924. >
  18925.    </P
  18926. ><P
  18927. >     In PHP 3, variables are always assigned by value. That is to say,
  18928.     when you assign an expression to a variable, the entire value of
  18929.     the original expression is copied into the destination
  18930.     variable. This means, for instance, that after assigning one
  18931.     variable's value to another, changing one of those variables will
  18932.     have no effect on the other. For more information on this kind of
  18933.     assignment, see the chapter on <A
  18934. HREF="#language.expressions"
  18935. >Expressions</A
  18936. >.
  18937.    </P
  18938. ><P
  18939. >     As of PHP 4, PHP offers another way to assign values to variables:
  18940.     <A
  18941. HREF="#language.references"
  18942. >assign by reference</A
  18943. >. 
  18944.     This means that the new variable simply references (in other words, 
  18945.     "becomes an alias for" or "points to") the original variable. 
  18946.     Changes to the new variable affect the original, and vice versa. 
  18947.     This also means that no copying is performed; thus, the assignment 
  18948.     happens more quickly. However, any speedup will likely be noticed 
  18949.     only in tight loops or when assigning large 
  18950.     <A
  18951. HREF="#language.types.array"
  18952. >arrays</A
  18953. > or 
  18954.     <A
  18955. HREF="#language.types.object"
  18956. >objects</A
  18957. >.
  18958.    </P
  18959. ><P
  18960. >     To assign by reference, simply prepend an ampersand (&) to the
  18961.     beginning of the variable which is being assigned (the source
  18962.     variable). For instance, the following code snippet outputs 'My
  18963.     name is Bob' twice:
  18964.  
  18965.     <DIV
  18966. CLASS="informalexample"
  18967. ><P
  18968. ></P
  18969. ><A
  18970. NAME="AEN3414"
  18971. ></A
  18972. ><TABLE
  18973. BORDER="0"
  18974. BGCOLOR="#E0E0E0"
  18975. CELLPADDING="5"
  18976. ><TR
  18977. ><TD
  18978. ><PRE
  18979. CLASS="php"
  18980. ><?php
  18981. $foo = 'Bob';              // Assign the value 'Bob' to $foo
  18982. $bar = &$foo;              // Reference $foo via $bar.
  18983. $bar = "My name is $bar";  // Alter $bar...
  18984. echo $bar;
  18985. echo $foo;                 // $foo is altered too.
  18986. ?></PRE
  18987. ></TD
  18988. ></TR
  18989. ></TABLE
  18990. ><P
  18991. ></P
  18992. ></DIV
  18993. >
  18994.    </P
  18995. ><P
  18996. >     One important thing to note is that only named variables may be
  18997.     assigned by reference.
  18998.     <DIV
  18999. CLASS="informalexample"
  19000. ><P
  19001. ></P
  19002. ><A
  19003. NAME="AEN3417"
  19004. ></A
  19005. ><TABLE
  19006. BORDER="0"
  19007. BGCOLOR="#E0E0E0"
  19008. CELLPADDING="5"
  19009. ><TR
  19010. ><TD
  19011. ><PRE
  19012. CLASS="php"
  19013. ><?php
  19014. $foo = 25;
  19015. $bar = &$foo;      // This is a valid assignment.
  19016. $bar = &(24 * 7);  // Invalid; references an unnamed expression.
  19017.  
  19018. function test()
  19019. {
  19020.    return 25;
  19021. }
  19022.  
  19023. $bar = &test();    // Invalid.
  19024. ?></PRE
  19025. ></TD
  19026. ></TR
  19027. ></TABLE
  19028. ><P
  19029. ></P
  19030. ></DIV
  19031. >
  19032.    </P
  19033. ></DIV
  19034. ><DIV
  19035. CLASS="sect1"
  19036. ><HR><H2
  19037. CLASS="sect1"
  19038. ><A
  19039. NAME="language.variables.predefined"
  19040. >Predefined variables</A
  19041. ></H2
  19042. ><P
  19043. >     PHP provides a large number of predefined variables to any script
  19044.     which it runs. Many of these variables, however, cannot be fully
  19045.     documented as they are dependent upon which server is running, the
  19046.     version and setup of the server, and other factors. Some of these
  19047.     variables will not be available when PHP is run on the 
  19048.     <A
  19049. HREF="#features.commandline"
  19050. >command line</A
  19051. >. 
  19052.     For a listing of these variables, please see the section on 
  19053.     <A
  19054. HREF="#reserved.variables"
  19055. >Reserved Predefined Variables</A
  19056. >.
  19057.    </P
  19058. ><DIV
  19059. CLASS="warning"
  19060. ><P
  19061. ></P
  19062. ><TABLE
  19063. CLASS="warning"
  19064. BORDER="1"
  19065. WIDTH="100%"
  19066. ><TR
  19067. ><TD
  19068. ALIGN="CENTER"
  19069. ><B
  19070. >Warning</B
  19071. ></TD
  19072. ></TR
  19073. ><TR
  19074. ><TD
  19075. ALIGN="LEFT"
  19076. ><P
  19077. >      In PHP 4.2.0 and later, the default value for the PHP directive <A
  19078. HREF="#ini.register-globals"
  19079. >register_globals</A
  19080. > is 
  19081.      <SPAN
  19082. CLASS="emphasis"
  19083. ><I
  19084. CLASS="emphasis"
  19085. >off</I
  19086. ></SPAN
  19087. >. This is a major change in PHP.  Having 
  19088.      register_globals <SPAN
  19089. CLASS="emphasis"
  19090. ><I
  19091. CLASS="emphasis"
  19092. >off</I
  19093. ></SPAN
  19094. > affects the set of predefined 
  19095.      variables available in the global scope.  For example, to get 
  19096.      <VAR
  19097. CLASS="varname"
  19098. >DOCUMENT_ROOT</VAR
  19099. > you'll use 
  19100.      <VAR
  19101. CLASS="varname"
  19102. >$_SERVER['DOCUMENT_ROOT']</VAR
  19103. > instead of 
  19104.      <VAR
  19105. CLASS="varname"
  19106. >$DOCUMENT_ROOT</VAR
  19107. >, or <VAR
  19108. CLASS="varname"
  19109. >$_GET['id']</VAR
  19110. > from 
  19111.      the URL <VAR
  19112. CLASS="literal"
  19113. >http://www.example.com/test.php?id=3</VAR
  19114. > instead 
  19115.      of <VAR
  19116. CLASS="varname"
  19117. >$id</VAR
  19118. >, or <VAR
  19119. CLASS="varname"
  19120. >$_ENV['HOME']</VAR
  19121. > instead of 
  19122.      <VAR
  19123. CLASS="varname"
  19124. >$HOME</VAR
  19125. >.
  19126.     </P
  19127. ><P
  19128. >      For related information on this change, read the configuration entry for 
  19129.      <A
  19130. HREF="#ini.register-globals"
  19131. >register_globals</A
  19132. >, the security 
  19133.      chapter on <A
  19134. HREF="#security.globals"
  19135. >Using Register Globals
  19136.      </A
  19137. >, as well as the PHP <A
  19138. HREF="http://www.php.net/release_4_1_0.php"
  19139. TARGET="_top"
  19140. >4.1.0
  19141.      </A
  19142. > and <A
  19143. HREF="http://www.php.net/release_4_2_0.php"
  19144. TARGET="_top"
  19145. >4.2.0</A
  19146. > Release 
  19147.      Announcements.
  19148.     </P
  19149. ><P
  19150. >      Using the available PHP Reserved Predefined Variables, like the 
  19151.      <A
  19152. HREF="#language.variables.superglobals"
  19153. >superglobal arrays</A
  19154. >, 
  19155.      is preferred.
  19156.     </P
  19157. ></TD
  19158. ></TR
  19159. ></TABLE
  19160. ></DIV
  19161. ><P
  19162. >     From version 4.1.0 onward, PHP provides an additional set of predefined arrays
  19163.     containing variables from the web server (if applicable), the
  19164.     environment, and user input. These new arrays are rather special
  19165.     in that they are automatically global--i.e., automatically
  19166.     available in every scope. For this reason, they are often known as
  19167.     'autoglobals' or 'superglobals'. (There is no mechanism in PHP for
  19168.     user-defined superglobals.) The superglobals are listed below;
  19169.     however, for a listing of their contents and further discussion on
  19170.     PHP predefined variables and their natures, please see the section
  19171.     <A
  19172. HREF="#reserved.variables"
  19173. >Reserved Predefined Variables</A
  19174. >.  
  19175.     Also, you'll notice how the older predefined variables 
  19176.     (<VAR
  19177. CLASS="varname"
  19178. >$HTTP_*_VARS</VAR
  19179. >) still exist.
  19180.  
  19181.     As of PHP 5.0.0, the long PHP 
  19182. <A
  19183. HREF="#language.variables.predefined"
  19184. >predefined variable</A
  19185. >
  19186. arrays may be disabled with the 
  19187. <A
  19188. HREF="#ini.register-long-arrays"
  19189. >register_long_arrays</A
  19190. >
  19191. directive.
  19192.    </P
  19193. ><DIV
  19194. CLASS="note"
  19195. ><BLOCKQUOTE
  19196. CLASS="note"
  19197. ><P
  19198. ><B
  19199. >Variable variables: </B
  19200. >
  19201.      Superglobals cannot be used as 
  19202.      <A
  19203. HREF="#language.variables.variable"
  19204. >variable variables</A
  19205. >
  19206.      inside functions or class methods.
  19207.     </P
  19208. ></BLOCKQUOTE
  19209. ></DIV
  19210. ><DIV
  19211. CLASS="note"
  19212. ><BLOCKQUOTE
  19213. CLASS="note"
  19214. ><P
  19215. ><B
  19216. >Note: </B
  19217. >
  19218.      Even though both the superglobal and HTTP_*_VARS can exist at the same
  19219.      time; they are not identical, so modifying one will not change the other.
  19220.     </P
  19221. ></BLOCKQUOTE
  19222. ></DIV
  19223. ><P
  19224. >     If certain variables in <A
  19225. HREF="#ini.variables-order"
  19226. >variables_order</A
  19227. > are not set, their
  19228.     appropriate PHP predefined arrays are also left empty.
  19229.    </P
  19230. ><P
  19231. ></P
  19232. ><DIV
  19233. CLASS="variablelist"
  19234. ><P
  19235. ><B
  19236. >PHP Superglobals</B
  19237. ></P
  19238. ><DL
  19239. ><DT
  19240. ><A
  19241. HREF="#reserved.variables.globals"
  19242. >$GLOBALS</A
  19243. ></DT
  19244. ><DD
  19245. ><P
  19246. >        Contains a reference to every variable which is currently
  19247.        available within the global scope of the script. The keys of
  19248.        this array are the names of the global variables.
  19249.        <VAR
  19250. CLASS="varname"
  19251. >$GLOBALS</VAR
  19252. > has existed since PHP 3.
  19253.       </P
  19254. ></DD
  19255. ><DT
  19256. ><A
  19257. HREF="#reserved.variables.server"
  19258. >$_SERVER</A
  19259. ></DT
  19260. ><DD
  19261. ><P
  19262. >        Variables set by the web server or otherwise directly related
  19263.        to the execution environment of the current script. Analogous
  19264.        to the old <VAR
  19265. CLASS="varname"
  19266. >$HTTP_SERVER_VARS</VAR
  19267. > array (which is
  19268.        still available, but deprecated).
  19269.       </P
  19270. ></DD
  19271. ><DT
  19272. ><A
  19273. HREF="#reserved.variables.get"
  19274. >$_GET</A
  19275. ></DT
  19276. ><DD
  19277. ><P
  19278. >        Variables provided to the script via HTTP GET. Analogous to the
  19279.        old <VAR
  19280. CLASS="varname"
  19281. >$HTTP_GET_VARS</VAR
  19282. > array (which is still
  19283.        available, but deprecated).
  19284.       </P
  19285. ></DD
  19286. ><DT
  19287. ><A
  19288. HREF="#reserved.variables.post"
  19289. >$_POST</A
  19290. ></DT
  19291. ><DD
  19292. ><P
  19293. >        Variables provided to the script via HTTP POST. Analogous to the
  19294.        old <VAR
  19295. CLASS="varname"
  19296. >$HTTP_POST_VARS</VAR
  19297. > array (which is still
  19298.        available, but deprecated).
  19299.       </P
  19300. ></DD
  19301. ><DT
  19302. ><A
  19303. HREF="#reserved.variables.cookies"
  19304. >$_COOKIE</A
  19305. ></DT
  19306. ><DD
  19307. ><P
  19308. >        Variables provided to the script via HTTP cookies. Analogous to
  19309.        the old <VAR
  19310. CLASS="varname"
  19311. >$HTTP_COOKIE_VARS</VAR
  19312. > array (which is
  19313.        still available, but deprecated).
  19314.       </P
  19315. ></DD
  19316. ><DT
  19317. ><A
  19318. HREF="#reserved.variables.files"
  19319. >$_FILES</A
  19320. ></DT
  19321. ><DD
  19322. ><P
  19323. >        Variables provided to the script via HTTP post file
  19324.        uploads. Analogous to the old
  19325.        <VAR
  19326. CLASS="varname"
  19327. >$HTTP_POST_FILES</VAR
  19328. > array (which is still
  19329.        available, but deprecated). See <A
  19330. HREF="#features.file-upload.post-method"
  19331. >POST method
  19332.         uploads</A
  19333. > for more information.
  19334.       </P
  19335. ></DD
  19336. ><DT
  19337. ><A
  19338. HREF="#reserved.variables.environment"
  19339. >$_ENV</A
  19340. ></DT
  19341. ><DD
  19342. ><P
  19343. >        Variables provided to the script via the environment. Analogous
  19344.        to the old <VAR
  19345. CLASS="varname"
  19346. >$HTTP_ENV_VARS</VAR
  19347. > array (which is
  19348.        still available, but deprecated).
  19349.       </P
  19350. ></DD
  19351. ><DT
  19352. ><A
  19353. HREF="#reserved.variables.request"
  19354. >$_REQUEST</A
  19355. ></DT
  19356. ><DD
  19357. ><P
  19358. >        Variables provided to the script via the GET, POST, and COOKIE input
  19359.        mechanisms, and which therefore cannot be trusted. The presence and
  19360.        order of variable inclusion in this array is defined according to the
  19361.        PHP <A
  19362. HREF="#ini.variables-order"
  19363. >variables_order</A
  19364. >
  19365.        configuration directive. This array has no direct analogue in versions
  19366.        of PHP prior to 4.1.0.  See also 
  19367.        <A
  19368. HREF="#function.import-request-variables"
  19369. ><B
  19370. CLASS="function"
  19371. >import_request_variables()</B
  19372. ></A
  19373. >.
  19374.       </P
  19375. ><DIV
  19376. CLASS="caution"
  19377. ><P
  19378. ></P
  19379. ><TABLE
  19380. CLASS="caution"
  19381. BORDER="1"
  19382. WIDTH="100%"
  19383. ><TR
  19384. ><TD
  19385. ALIGN="CENTER"
  19386. ><B
  19387. >Caution</B
  19388. ></TD
  19389. ></TR
  19390. ><TR
  19391. ><TD
  19392. ALIGN="LEFT"
  19393. ><P
  19394. >         Since PHP 4.3.0, FILE information from <VAR
  19395. CLASS="varname"
  19396. >$_FILES</VAR
  19397. > does
  19398.         not exist in <VAR
  19399. CLASS="varname"
  19400. >$_REQUEST</VAR
  19401. >.
  19402.        </P
  19403. ></TD
  19404. ></TR
  19405. ></TABLE
  19406. ></DIV
  19407. ><DIV
  19408. CLASS="note"
  19409. ><BLOCKQUOTE
  19410. CLASS="note"
  19411. ><P
  19412. ><B
  19413. >Note: </B
  19414. >
  19415.         When running on the <A
  19416. HREF="#features.commandline"
  19417. >command line
  19418.         </A
  19419. >, this will <SPAN
  19420. CLASS="emphasis"
  19421. ><I
  19422. CLASS="emphasis"
  19423. >not</I
  19424. ></SPAN
  19425. > include the 
  19426.         <VAR
  19427. CLASS="varname"
  19428. >argv</VAR
  19429. > and <VAR
  19430. CLASS="varname"
  19431. >argc</VAR
  19432. > entries; these are 
  19433.         present in the <VAR
  19434. CLASS="varname"
  19435. >$_SERVER</VAR
  19436. > array.
  19437.        </P
  19438. ></BLOCKQUOTE
  19439. ></DIV
  19440. ></DD
  19441. ><DT
  19442. ><A
  19443. HREF="#reserved.variables.session"
  19444. >$_SESSION</A
  19445. ></DT
  19446. ><DD
  19447. ><P
  19448. >        Variables which are currently registered to a script's
  19449.        session. Analogous to the old
  19450.        <VAR
  19451. CLASS="varname"
  19452. >$HTTP_SESSION_VARS</VAR
  19453. > array (which is still
  19454.        available, but deprecated). See the <A
  19455. HREF="#ref.session"
  19456. >Session handling functions</A
  19457. > section
  19458.        for more information.
  19459.       </P
  19460. ></DD
  19461. ></DL
  19462. ></DIV
  19463. ></DIV
  19464. ><DIV
  19465. CLASS="sect1"
  19466. ><HR><H2
  19467. CLASS="sect1"
  19468. ><A
  19469. NAME="language.variables.scope"
  19470. >Variable scope</A
  19471. ></H2
  19472. ><P
  19473. >     The scope of a variable is the context within which it is defined.
  19474.     For the most part all PHP variables only have a single scope.
  19475.     This single scope spans included and required files as well.  For
  19476.     example:
  19477.    </P
  19478. ><DIV
  19479. CLASS="informalexample"
  19480. ><P
  19481. ></P
  19482. ><A
  19483. NAME="AEN3530"
  19484. ></A
  19485. ><TABLE
  19486. BORDER="0"
  19487. BGCOLOR="#E0E0E0"
  19488. CELLPADDING="5"
  19489. ><TR
  19490. ><TD
  19491. ><PRE
  19492. CLASS="php"
  19493. ><?php
  19494. $a = 1;
  19495. include "b.inc";
  19496. ?></PRE
  19497. ></TD
  19498. ></TR
  19499. ></TABLE
  19500. ><P
  19501. ></P
  19502. ></DIV
  19503. ><P
  19504. >     Here the <VAR
  19505. CLASS="varname"
  19506. >$a</VAR
  19507. > variable will be available within
  19508.     the included <TT
  19509. CLASS="filename"
  19510. >b.inc</TT
  19511. > script.  However, within
  19512.     user-defined functions a local function scope is introduced.  Any
  19513.     variable used inside a function is by default limited to the local
  19514.     function scope.  For example:
  19515.    </P
  19516. ><DIV
  19517. CLASS="informalexample"
  19518. ><P
  19519. ></P
  19520. ><A
  19521. NAME="AEN3535"
  19522. ></A
  19523. ><TABLE
  19524. BORDER="0"
  19525. BGCOLOR="#E0E0E0"
  19526. CELLPADDING="5"
  19527. ><TR
  19528. ><TD
  19529. ><PRE
  19530. CLASS="php"
  19531. ><?php
  19532. $a = 1; /* global scope */ 
  19533.  
  19534. function Test()
  19535.     echo $a; /* reference to local scope variable */ 
  19536.  
  19537. Test();
  19538. ?></PRE
  19539. ></TD
  19540. ></TR
  19541. ></TABLE
  19542. ><P
  19543. ></P
  19544. ></DIV
  19545. ><P
  19546. >     This script will not produce any output because the echo statement
  19547.     refers to a local version of the <VAR
  19548. CLASS="varname"
  19549. >$a</VAR
  19550. > variable,
  19551.     and it has not been assigned a value within this scope.  You may
  19552.     notice that this is a little bit different from the C language in
  19553.     that global variables in C are automatically available to
  19554.     functions unless specifically overridden by a local definition.
  19555.     This can cause some problems in that people may inadvertently
  19556.     change a global variable.  In PHP global variables must be
  19557.     declared global inside a function if they are going to be used in
  19558.     that function.
  19559.    </P
  19560. ><DIV
  19561. CLASS="sect2"
  19562. ><HR><H3
  19563. CLASS="sect2"
  19564. ><A
  19565. NAME="language.variables.scope.global"
  19566. >The global keyword</A
  19567. ></H3
  19568. ><P
  19569. >      First, an example use of <VAR
  19570. CLASS="literal"
  19571. >global</VAR
  19572. >:
  19573.     </P
  19574. ><P
  19575. >      <TABLE
  19576. WIDTH="100%"
  19577. BORDER="0"
  19578. CELLPADDING="0"
  19579. CELLSPACING="0"
  19580. CLASS="EXAMPLE"
  19581. ><TR
  19582. ><TD
  19583. ><DIV
  19584. CLASS="example"
  19585. ><A
  19586. NAME="AEN3544"
  19587. ></A
  19588. ><P
  19589. ><B
  19590. >Example 12-1. Using global</B
  19591. ></P
  19592. ><TABLE
  19593. BORDER="0"
  19594. BGCOLOR="#E0E0E0"
  19595. CELLPADDING="5"
  19596. ><TR
  19597. ><TD
  19598. ><PRE
  19599. CLASS="php"
  19600. ><?php
  19601. $a = 1;
  19602. $b = 2;
  19603.  
  19604. function Sum()
  19605. {
  19606.     global $a, $b;
  19607.  
  19608.     $b = $a + $b;
  19609.  
  19610. Sum();
  19611. echo $b;
  19612. ?></PRE
  19613. ></TD
  19614. ></TR
  19615. ></TABLE
  19616. ></DIV
  19617. ></TD
  19618. ></TR
  19619. ></TABLE
  19620. >
  19621.     </P
  19622. ><P
  19623. >     The above script will output "3".  By declaring
  19624.     <VAR
  19625. CLASS="varname"
  19626. >$a</VAR
  19627. > and <VAR
  19628. CLASS="varname"
  19629. >$b</VAR
  19630. > global within the
  19631.     function, all references to either variable will refer to the
  19632.     global version.  There is no limit to the number of global
  19633.     variables that can be manipulated by a function.
  19634.    </P
  19635. ><P
  19636. >     A second way to access variables from the global scope is to use
  19637.     the special PHP-defined <VAR
  19638. CLASS="varname"
  19639. >$GLOBALS</VAR
  19640. > array.  The
  19641.     previous example can be rewritten as:
  19642.    </P
  19643. ><P
  19644. >     <TABLE
  19645. WIDTH="100%"
  19646. BORDER="0"
  19647. CELLPADDING="0"
  19648. CELLSPACING="0"
  19649. CLASS="EXAMPLE"
  19650. ><TR
  19651. ><TD
  19652. ><DIV
  19653. CLASS="example"
  19654. ><A
  19655. NAME="AEN3553"
  19656. ></A
  19657. ><P
  19658. ><B
  19659. >Example 12-2. Using <VAR
  19660. CLASS="varname"
  19661. >$GLOBALS</VAR
  19662. > instead of global</B
  19663. ></P
  19664. ><TABLE
  19665. BORDER="0"
  19666. BGCOLOR="#E0E0E0"
  19667. CELLPADDING="5"
  19668. ><TR
  19669. ><TD
  19670. ><PRE
  19671. CLASS="php"
  19672. ><?php
  19673. $a = 1;
  19674. $b = 2;
  19675.  
  19676. function Sum()
  19677. {
  19678.     $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
  19679.  
  19680. Sum();
  19681. echo $b;
  19682. ?></PRE
  19683. ></TD
  19684. ></TR
  19685. ></TABLE
  19686. ></DIV
  19687. ></TD
  19688. ></TR
  19689. ></TABLE
  19690. >
  19691.    </P
  19692. ><P
  19693. >     The <VAR
  19694. CLASS="varname"
  19695. >$GLOBALS</VAR
  19696. > array is an associative array with
  19697.     the name of the global variable being the key and the contents of
  19698.     that variable being the value of the array element.
  19699.     Notice how <VAR
  19700. CLASS="varname"
  19701. >$GLOBALS</VAR
  19702. > exists in any scope, this 
  19703.     is because $GLOBALS is a <A
  19704. HREF="#language.variables.superglobals"
  19705. >superglobal</A
  19706. >.
  19707.     Here's an example demonstrating the power of superglobals: 
  19708.    </P
  19709. ><P
  19710. >     <TABLE
  19711. WIDTH="100%"
  19712. BORDER="0"
  19713. CELLPADDING="0"
  19714. CELLSPACING="0"
  19715. CLASS="EXAMPLE"
  19716. ><TR
  19717. ><TD
  19718. ><DIV
  19719. CLASS="example"
  19720. ><A
  19721. NAME="AEN3562"
  19722. ></A
  19723. ><P
  19724. ><B
  19725. >Example 12-3. Example demonstrating superglobals and scope</B
  19726. ></P
  19727. ><TABLE
  19728. BORDER="0"
  19729. BGCOLOR="#E0E0E0"
  19730. CELLPADDING="5"
  19731. ><TR
  19732. ><TD
  19733. ><PRE
  19734. CLASS="php"
  19735. ><?php
  19736. function test_global()
  19737. {
  19738.     // Most predefined variables aren't "super" and require 
  19739.     // 'global' to be available to the functions local scope.
  19740.     global $HTTP_POST_VARS;
  19741.     
  19742.     echo $HTTP_POST_VARS['name'];
  19743.     
  19744.     // Superglobals are available in any scope and do 
  19745.     // not require 'global'. Superglobals are available 
  19746.     // as of PHP 4.1.0
  19747.     echo $_POST['name'];
  19748. }
  19749. ?></PRE
  19750. ></TD
  19751. ></TR
  19752. ></TABLE
  19753. ></DIV
  19754. ></TD
  19755. ></TR
  19756. ></TABLE
  19757. >
  19758.    </P
  19759. ></DIV
  19760. ><DIV
  19761. CLASS="sect2"
  19762. ><HR><H3
  19763. CLASS="sect2"
  19764. ><A
  19765. NAME="language.variables.scope.static"
  19766. >Using static variables</A
  19767. ></H3
  19768. ><P
  19769. >     Another important feature of variable scoping is the
  19770.     <SPAN
  19771. CLASS="emphasis"
  19772. ><I
  19773. CLASS="emphasis"
  19774. >static</I
  19775. ></SPAN
  19776. > variable.  A static variable exists
  19777.     only in a local function scope, but it does not lose its value
  19778.     when program execution leaves this scope.  Consider the following
  19779.     example:
  19780.    </P
  19781. ><P
  19782. >     <TABLE
  19783. WIDTH="100%"
  19784. BORDER="0"
  19785. CELLPADDING="0"
  19786. CELLSPACING="0"
  19787. CLASS="EXAMPLE"
  19788. ><TR
  19789. ><TD
  19790. ><DIV
  19791. CLASS="example"
  19792. ><A
  19793. NAME="AEN3570"
  19794. ></A
  19795. ><P
  19796. ><B
  19797. >Example 12-4. Example demonstrating need for static variables</B
  19798. ></P
  19799. ><TABLE
  19800. BORDER="0"
  19801. BGCOLOR="#E0E0E0"
  19802. CELLPADDING="5"
  19803. ><TR
  19804. ><TD
  19805. ><PRE
  19806. CLASS="php"
  19807. ><?php
  19808. function Test ()
  19809. {
  19810.     $a = 0;
  19811.     echo $a;
  19812.     $a++;
  19813. }
  19814. ?></PRE
  19815. ></TD
  19816. ></TR
  19817. ></TABLE
  19818. ></DIV
  19819. ></TD
  19820. ></TR
  19821. ></TABLE
  19822. >
  19823.    </P
  19824. ><P
  19825. >     This function is quite useless since every time it is called it
  19826.     sets <VAR
  19827. CLASS="varname"
  19828. >$a</VAR
  19829. > to <VAR
  19830. CLASS="literal"
  19831. >0</VAR
  19832. > and prints
  19833.     "0".  The <VAR
  19834. CLASS="varname"
  19835. >$a</VAR
  19836. >++ which increments the
  19837.     variable serves no purpose since as soon as the function exits the
  19838.     <VAR
  19839. CLASS="varname"
  19840. >$a</VAR
  19841. > variable disappears.  To make a useful
  19842.     counting function which will not lose track of the current count,
  19843.     the <VAR
  19844. CLASS="varname"
  19845. >$a</VAR
  19846. > variable is declared static:
  19847.    </P
  19848. ><P
  19849. >     <TABLE
  19850. WIDTH="100%"
  19851. BORDER="0"
  19852. CELLPADDING="0"
  19853. CELLSPACING="0"
  19854. CLASS="EXAMPLE"
  19855. ><TR
  19856. ><TD
  19857. ><DIV
  19858. CLASS="example"
  19859. ><A
  19860. NAME="AEN3580"
  19861. ></A
  19862. ><P
  19863. ><B
  19864. >Example 12-5. Example use of static variables</B
  19865. ></P
  19866. ><TABLE
  19867. BORDER="0"
  19868. BGCOLOR="#E0E0E0"
  19869. CELLPADDING="5"
  19870. ><TR
  19871. ><TD
  19872. ><PRE
  19873. CLASS="php"
  19874. ><?php
  19875. function Test()
  19876. {
  19877.     static $a = 0;
  19878.     echo $a;
  19879.     $a++;
  19880. }
  19881. ?></PRE
  19882. ></TD
  19883. ></TR
  19884. ></TABLE
  19885. ></DIV
  19886. ></TD
  19887. ></TR
  19888. ></TABLE
  19889. >
  19890.    </P
  19891. ><P
  19892. >     Now, every time the Test() function is called it will print the
  19893.     value of <VAR
  19894. CLASS="varname"
  19895. >$a</VAR
  19896. > and increment it.
  19897.    </P
  19898. ><P
  19899. >     Static variables also provide one way to deal with recursive
  19900.     functions. A recursive function is one which calls itself.  Care
  19901.     must be taken when writing a recursive function because it is
  19902.     possible to make it recurse indefinitely.  You must make sure you
  19903.     have an adequate way of terminating the recursion.  The following
  19904.     simple function recursively counts to 10, using the static
  19905.     variable <VAR
  19906. CLASS="varname"
  19907. >$count</VAR
  19908. > to know when to stop:
  19909.    </P
  19910. ><P
  19911. >     <TABLE
  19912. WIDTH="100%"
  19913. BORDER="0"
  19914. CELLPADDING="0"
  19915. CELLSPACING="0"
  19916. CLASS="EXAMPLE"
  19917. ><TR
  19918. ><TD
  19919. ><DIV
  19920. CLASS="example"
  19921. ><A
  19922. NAME="AEN3588"
  19923. ></A
  19924. ><P
  19925. ><B
  19926. >Example 12-6. Static variables with recursive functions</B
  19927. ></P
  19928. ><TABLE
  19929. BORDER="0"
  19930. BGCOLOR="#E0E0E0"
  19931. CELLPADDING="5"
  19932. ><TR
  19933. ><TD
  19934. ><PRE
  19935. CLASS="php"
  19936. ><?php
  19937. function Test()
  19938. {
  19939.     static $count = 0;
  19940.  
  19941.     $count++;
  19942.     echo $count;
  19943.     if ($count < 10) {
  19944.         Test ();
  19945.     }
  19946.     $count--;
  19947. }
  19948. ?></PRE
  19949. ></TD
  19950. ></TR
  19951. ></TABLE
  19952. ></DIV
  19953. ></TD
  19954. ></TR
  19955. ></TABLE
  19956. >
  19957.    </P
  19958. ><DIV
  19959. CLASS="note"
  19960. ><BLOCKQUOTE
  19961. CLASS="note"
  19962. ><P
  19963. ><B
  19964. >Note: </B
  19965. >  
  19966.        Static variables maybe declared as seen in the examples above. 
  19967.        Trying to assign values to these variables which are the 
  19968.        result of expressions will cause a parse error.
  19969.      </P
  19970. ><P
  19971. >  
  19972.       <TABLE
  19973. WIDTH="100%"
  19974. BORDER="0"
  19975. CELLPADDING="0"
  19976. CELLSPACING="0"
  19977. CLASS="EXAMPLE"
  19978. ><TR
  19979. ><TD
  19980. ><DIV
  19981. CLASS="example"
  19982. ><A
  19983. NAME="AEN3594"
  19984. ></A
  19985. ><P
  19986. ><B
  19987. >Example 12-7. Declaring static variables</B
  19988. ></P
  19989. ><TABLE
  19990. BORDER="0"
  19991. BGCOLOR="#E0E0E0"
  19992. CELLPADDING="5"
  19993. ><TR
  19994. ><TD
  19995. ><PRE
  19996. CLASS="php"
  19997. ><?php
  19998. function foo(){
  19999.     static $int = 0;          // correct 
  20000.     static $int = 1+2;        // wrong  (as it is an expression)
  20001.     static $int = sqrt(121);  // wrong  (as it is an expression too)
  20002.  
  20003.     $int++;
  20004.     echo $int;
  20005. }
  20006. ?></PRE
  20007. ></TD
  20008. ></TR
  20009. ></TABLE
  20010. ></DIV
  20011. ></TD
  20012. ></TR
  20013. ></TABLE
  20014. >
  20015.     </P
  20016. ></BLOCKQUOTE
  20017. ></DIV
  20018. ></DIV
  20019. ><DIV
  20020. CLASS="sect2"
  20021. ><HR><H3
  20022. CLASS="sect2"
  20023. ><A
  20024. NAME="language.variables.scope.references"
  20025. >References with global and static variables</A
  20026. ></H3
  20027. ><P
  20028. >     The Zend Engine 1, driving PHP 4, implements the
  20029.     <A
  20030. HREF="#language.variables.scope.static"
  20031. >static</A
  20032. > and 
  20033.     <A
  20034. HREF="#language.variables.scope.global"
  20035. >global</A
  20036. > modifier 
  20037.     for variables in terms of <A
  20038. HREF="#language.references"
  20039. >     references</A
  20040. >. For example, a true global variable
  20041.     imported inside a function scope with the <VAR
  20042. CLASS="literal"
  20043. >global</VAR
  20044. >
  20045.     statement actually creates a reference to the global variable. This can
  20046.     lead to unexpected behaviour which the following example addresses:
  20047.    </P
  20048. ><DIV
  20049. CLASS="informalexample"
  20050. ><P
  20051. ></P
  20052. ><A
  20053. NAME="AEN3604"
  20054. ></A
  20055. ><TABLE
  20056. BORDER="0"
  20057. BGCOLOR="#E0E0E0"
  20058. CELLPADDING="5"
  20059. ><TR
  20060. ><TD
  20061. ><PRE
  20062. CLASS="php"
  20063. ><?php
  20064. function test_global_ref() {
  20065.     global $obj;
  20066.     $obj = &new stdclass;
  20067. }
  20068.  
  20069. function test_global_noref() {
  20070.     global $obj;
  20071.     $obj = new stdclass;
  20072. }
  20073.  
  20074. test_global_ref();
  20075. var_dump($obj);
  20076. test_global_noref();
  20077. var_dump($obj);
  20078. ?></PRE
  20079. ></TD
  20080. ></TR
  20081. ></TABLE
  20082. ><P
  20083. ></P
  20084. ></DIV
  20085. ><P
  20086. >     Executing this example will result in the following output:
  20087.    </P
  20088. ><TABLE
  20089. BORDER="0"
  20090. BGCOLOR="#E0E0E0"
  20091. CELLPADDING="5"
  20092. ><TR
  20093. ><TD
  20094. ><PRE
  20095. CLASS="screen"
  20096. >NULL
  20097. object(stdClass)(0) {
  20098. }</PRE
  20099. ></TD
  20100. ></TR
  20101. ></TABLE
  20102. ><P
  20103. >     A similar behaviour applies to the <VAR
  20104. CLASS="literal"
  20105. >static</VAR
  20106. > statement.
  20107.     References are not stored statically:
  20108.    </P
  20109. ><DIV
  20110. CLASS="informalexample"
  20111. ><P
  20112. ></P
  20113. ><A
  20114. NAME="AEN3610"
  20115. ></A
  20116. ><TABLE
  20117. BORDER="0"
  20118. BGCOLOR="#E0E0E0"
  20119. CELLPADDING="5"
  20120. ><TR
  20121. ><TD
  20122. ><PRE
  20123. CLASS="php"
  20124. ><?php
  20125. function &get_instance_ref() {
  20126.     static $obj;
  20127.  
  20128.     echo "Static object: ";
  20129.     var_dump($obj);
  20130.     if (!isset($obj)) {
  20131.         // Assign a reference to the static variable
  20132.         $obj = &new stdclass;
  20133.     }
  20134.     $obj->property++;
  20135.     return $obj;
  20136. }
  20137.  
  20138. function &get_instance_noref() {
  20139.     static $obj;
  20140.  
  20141.     echo "Static object: ";
  20142.     var_dump($obj);
  20143.     if (!isset($obj)) {
  20144.         // Assign the object to the static variable
  20145.         $obj = new stdclass;
  20146.     }
  20147.     $obj->property++;
  20148.     return $obj;
  20149. }
  20150.  
  20151. $obj1 = get_instance_ref();
  20152. $still_obj1 = get_instance_ref();
  20153. echo "\n";
  20154. $obj2 = get_instance_noref();
  20155. $still_obj2 = get_instance_noref();
  20156. ?></PRE
  20157. ></TD
  20158. ></TR
  20159. ></TABLE
  20160. ><P
  20161. ></P
  20162. ></DIV
  20163. ><P
  20164. >     Executing this example will result in the following output:
  20165.    </P
  20166. ><TABLE
  20167. BORDER="0"
  20168. BGCOLOR="#E0E0E0"
  20169. CELLPADDING="5"
  20170. ><TR
  20171. ><TD
  20172. ><PRE
  20173. CLASS="screen"
  20174. >Static object: NULL
  20175. Static object: NULL
  20176.  
  20177. Static object: NULL
  20178. Static object: object(stdClass)(1) {
  20179.   ["property"]=>
  20180.   int(1)
  20181. }</PRE
  20182. ></TD
  20183. ></TR
  20184. ></TABLE
  20185. ><P
  20186. >     This example demonstrates that when assigning a reference to a static
  20187.     variable, it's not <SPAN
  20188. CLASS="emphasis"
  20189. ><I
  20190. CLASS="emphasis"
  20191. >remembered</I
  20192. ></SPAN
  20193. > when you call the
  20194.     <VAR
  20195. CLASS="literal"
  20196. >&get_instance_ref()</VAR
  20197. > function a second time.
  20198.    </P
  20199. ></DIV
  20200. ></DIV
  20201. ><DIV
  20202. CLASS="sect1"
  20203. ><HR><H2
  20204. CLASS="sect1"
  20205. ><A
  20206. NAME="language.variables.variable"
  20207. >Variable variables</A
  20208. ></H2
  20209. ><P
  20210. >     Sometimes it is convenient to be able to have variable variable
  20211.     names.  That is, a variable name which can be set and used
  20212.     dynamically.  A normal variable is set with a statement such as:
  20213.    </P
  20214. ><DIV
  20215. CLASS="informalexample"
  20216. ><P
  20217. ></P
  20218. ><A
  20219. NAME="AEN3620"
  20220. ></A
  20221. ><TABLE
  20222. BORDER="0"
  20223. BGCOLOR="#E0E0E0"
  20224. CELLPADDING="5"
  20225. ><TR
  20226. ><TD
  20227. ><PRE
  20228. CLASS="php"
  20229. ><?php
  20230. $a = "hello";
  20231. ?></PRE
  20232. ></TD
  20233. ></TR
  20234. ></TABLE
  20235. ><P
  20236. ></P
  20237. ></DIV
  20238. ><P
  20239. >     A variable variable takes the value of a variable and treats that
  20240.     as the name of a variable.  In the above example,
  20241.     <SPAN
  20242. CLASS="emphasis"
  20243. ><I
  20244. CLASS="emphasis"
  20245. >hello</I
  20246. ></SPAN
  20247. >, can be used as the name of a variable
  20248.     by using two dollar signs. i.e.
  20249.    </P
  20250. ><DIV
  20251. CLASS="informalexample"
  20252. ><P
  20253. ></P
  20254. ><A
  20255. NAME="AEN3624"
  20256. ></A
  20257. ><TABLE
  20258. BORDER="0"
  20259. BGCOLOR="#E0E0E0"
  20260. CELLPADDING="5"
  20261. ><TR
  20262. ><TD
  20263. ><PRE
  20264. CLASS="php"
  20265. ><?php
  20266. $$a = "world";
  20267. ?></PRE
  20268. ></TD
  20269. ></TR
  20270. ></TABLE
  20271. ><P
  20272. ></P
  20273. ></DIV
  20274. ><P
  20275. >     At this point two variables have been defined and stored in the
  20276.     PHP symbol tree: <VAR
  20277. CLASS="varname"
  20278. >$a</VAR
  20279. > with contents "hello" and
  20280.     <VAR
  20281. CLASS="varname"
  20282. >$hello</VAR
  20283. > with contents "world".  Therefore, this
  20284.     statement:
  20285.    </P
  20286. ><DIV
  20287. CLASS="informalexample"
  20288. ><P
  20289. ></P
  20290. ><A
  20291. NAME="AEN3629"
  20292. ></A
  20293. ><TABLE
  20294. BORDER="0"
  20295. BGCOLOR="#E0E0E0"
  20296. CELLPADDING="5"
  20297. ><TR
  20298. ><TD
  20299. ><PRE
  20300. CLASS="php"
  20301. ><?php
  20302. echo "$a ${$a}";
  20303. ?></PRE
  20304. ></TD
  20305. ></TR
  20306. ></TABLE
  20307. ><P
  20308. ></P
  20309. ></DIV
  20310. ><P
  20311. >     produces the exact same output as:
  20312.    </P
  20313. ><DIV
  20314. CLASS="informalexample"
  20315. ><P
  20316. ></P
  20317. ><A
  20318. NAME="AEN3632"
  20319. ></A
  20320. ><TABLE
  20321. BORDER="0"
  20322. BGCOLOR="#E0E0E0"
  20323. CELLPADDING="5"
  20324. ><TR
  20325. ><TD
  20326. ><PRE
  20327. CLASS="php"
  20328. ><?php
  20329. echo "$a $hello";
  20330. ?></PRE
  20331. ></TD
  20332. ></TR
  20333. ></TABLE
  20334. ><P
  20335. ></P
  20336. ></DIV
  20337. ><P
  20338. >     i.e. they both produce: <SAMP
  20339. CLASS="computeroutput"
  20340. >hello world</SAMP
  20341. >.
  20342.    </P
  20343. ><P
  20344. >     In order to use variable variables with arrays, you have to
  20345.     resolve an ambiguity problem.  That is, if you write
  20346.     <VAR
  20347. CLASS="varname"
  20348. >$$a[1]</VAR
  20349. > then the parser needs to know if you
  20350.     meant to use <VAR
  20351. CLASS="varname"
  20352. >$a[1]</VAR
  20353. > as a variable, or if you
  20354.     wanted <VAR
  20355. CLASS="varname"
  20356. >$$a</VAR
  20357. > as the variable and then the [1]
  20358.     index from that variable.  The syntax for resolving this ambiguity
  20359.     is: <VAR
  20360. CLASS="varname"
  20361. >${$a[1]}</VAR
  20362. > for the first case and
  20363.     <VAR
  20364. CLASS="varname"
  20365. >${$a}[1]</VAR
  20366. > for the second.
  20367.    </P
  20368. ><DIV
  20369. CLASS="warning"
  20370. ><P
  20371. ></P
  20372. ><TABLE
  20373. CLASS="warning"
  20374. BORDER="1"
  20375. WIDTH="100%"
  20376. ><TR
  20377. ><TD
  20378. ALIGN="CENTER"
  20379. ><B
  20380. >Warning</B
  20381. ></TD
  20382. ></TR
  20383. ><TR
  20384. ><TD
  20385. ALIGN="LEFT"
  20386. ><P
  20387. >      Please note that variable variables cannot be used with PHP's 
  20388.      <A
  20389. HREF="#language.variables.superglobals"
  20390. >Superglobal arrays</A
  20391. >
  20392.      within functions or class methods.
  20393.     </P
  20394. ></TD
  20395. ></TR
  20396. ></TABLE
  20397. ></DIV
  20398. ></DIV
  20399. ><DIV
  20400. CLASS="sect1"
  20401. ><HR><H2
  20402. CLASS="sect1"
  20403. ><A
  20404. NAME="language.variables.external"
  20405. >Variables from outside PHP</A
  20406. ></H2
  20407. ><DIV
  20408. CLASS="sect2"
  20409. ><H3
  20410. CLASS="sect2"
  20411. ><A
  20412. NAME="language.variables.external.form"
  20413. >HTML Forms (GET and POST)</A
  20414. ></H3
  20415. ><P
  20416. >      When a form is submitted to a PHP script, the information from 
  20417.      that form is automatically made available to the script.  There 
  20418.      are many ways to access this information, for example:
  20419.     </P
  20420. ><P
  20421. >      <TABLE
  20422. WIDTH="100%"
  20423. BORDER="0"
  20424. CELLPADDING="0"
  20425. CELLSPACING="0"
  20426. CLASS="EXAMPLE"
  20427. ><TR
  20428. ><TD
  20429. ><DIV
  20430. CLASS="example"
  20431. ><A
  20432. NAME="AEN3651"
  20433. ></A
  20434. ><P
  20435. ><B
  20436. >Example 12-8. A simple HTML form</B
  20437. ></P
  20438. ><TABLE
  20439. BORDER="0"
  20440. BGCOLOR="#E0E0E0"
  20441. CELLPADDING="5"
  20442. ><TR
  20443. ><TD
  20444. ><PRE
  20445. CLASS="html"
  20446. ><form action="foo.php" method="post">
  20447.     Name:  <input type="text" name="username" /><br />
  20448.     Email: <input type="text" name="email" /><br />
  20449.     <input type="submit" name="submit" value="Submit me!" />
  20450. </form></PRE
  20451. ></TD
  20452. ></TR
  20453. ></TABLE
  20454. ></DIV
  20455. ></TD
  20456. ></TR
  20457. ></TABLE
  20458. >
  20459.     </P
  20460. ><P
  20461. >      Depending on your particular setup and personal preferences, there 
  20462.      are many ways to access data from your HTML forms.  Some examples are:
  20463.     </P
  20464. ><P
  20465. >      <TABLE
  20466. WIDTH="100%"
  20467. BORDER="0"
  20468. CELLPADDING="0"
  20469. CELLSPACING="0"
  20470. CLASS="EXAMPLE"
  20471. ><TR
  20472. ><TD
  20473. ><DIV
  20474. CLASS="example"
  20475. ><A
  20476. NAME="AEN3656"
  20477. ></A
  20478. ><P
  20479. ><B
  20480. >Example 12-9. Accessing data from a simple POST HTML form</B
  20481. ></P
  20482. ><TABLE
  20483. BORDER="0"
  20484. BGCOLOR="#E0E0E0"
  20485. CELLPADDING="5"
  20486. ><TR
  20487. ><TD
  20488. ><PRE
  20489. CLASS="html"
  20490. ><?php 
  20491. // Available since PHP 4.1.0
  20492.  
  20493.    echo $_POST['username'];
  20494.    echo $_REQUEST['username'];
  20495.  
  20496.    import_request_variables('p', 'p_');
  20497.    echo $p_username;
  20498.  
  20499. // Available since PHP 3. As of PHP 5.0.0, these long predefined
  20500. // variables can be disabled with the register_long_arrays directive.
  20501.  
  20502.    echo $HTTP_POST_VARS['username'];
  20503.  
  20504. // Available if the PHP directive register_globals = on. As of 
  20505. // PHP 4.2.0 the default value of register_globals = off.
  20506. // Using/relying on this method is not preferred.
  20507.  
  20508.    echo $username;
  20509. ?></PRE
  20510. ></TD
  20511. ></TR
  20512. ></TABLE
  20513. ></DIV
  20514. ></TD
  20515. ></TR
  20516. ></TABLE
  20517. >
  20518.     </P
  20519. ><P
  20520. >      Using a GET form is similar except you'll use the appropriate
  20521.      GET predefined variable instead.  GET also applies to the
  20522.      QUERY_STRING (the information after the '?' in a URL).  So,
  20523.      for example, <VAR
  20524. CLASS="literal"
  20525. >http://www.example.com/test.php?id=3</VAR
  20526. >
  20527.      contains GET data which is accessible with <VAR
  20528. CLASS="varname"
  20529. >$_GET['id']</VAR
  20530. >.
  20531.      See also <A
  20532. HREF="#reserved.variables.request"
  20533. >$_REQUEST</A
  20534. > and 
  20535.      <A
  20536. HREF="#function.import-request-variables"
  20537. ><B
  20538. CLASS="function"
  20539. >import_request_variables()</B
  20540. ></A
  20541. >.
  20542.     </P
  20543. ><DIV
  20544. CLASS="note"
  20545. ><BLOCKQUOTE
  20546. CLASS="note"
  20547. ><P
  20548. ><B
  20549. >Note: </B
  20550. >
  20551.       <A
  20552. HREF="#language.variables.superglobals"
  20553. >Superglobal arrays</A
  20554. >, 
  20555.       like <VAR
  20556. CLASS="varname"
  20557. >$_POST</VAR
  20558. > and <VAR
  20559. CLASS="varname"
  20560. >$_GET</VAR
  20561. >, became 
  20562.       available in PHP 4.1.0
  20563.      </P
  20564. ></BLOCKQUOTE
  20565. ></DIV
  20566. ><P
  20567. >      As shown, before PHP 4.2.0 the default value for <A
  20568. HREF="#ini.register-globals"
  20569. >register_globals</A
  20570. >
  20571.      was <SPAN
  20572. CLASS="emphasis"
  20573. ><I
  20574. CLASS="emphasis"
  20575. >on</I
  20576. ></SPAN
  20577. >.  And, in PHP 3 it was always on.  The PHP 
  20578.      community is encouraging all to not rely on this directive 
  20579.      as it's preferred to assume it's <SPAN
  20580. CLASS="emphasis"
  20581. ><I
  20582. CLASS="emphasis"
  20583. >off</I
  20584. ></SPAN
  20585. > and code 
  20586.      accordingly.
  20587.     </P
  20588. ><DIV
  20589. CLASS="note"
  20590. ><BLOCKQUOTE
  20591. CLASS="note"
  20592. ><P
  20593. ><B
  20594. >Note: </B
  20595. >
  20596.       The <A
  20597. HREF="#ini.magic-quotes-gpc"
  20598. >magic_quotes_gpc</A
  20599.       configuration directive affects Get, Post and Cookie values.  If 
  20600.       turned on, value (It's "PHP!") will automagically become (It\'s \"PHP!\").
  20601.       Escaping is needed for DB insertion.  See also 
  20602.       <A
  20603. HREF="#function.addslashes"
  20604. ><B
  20605. CLASS="function"
  20606. >addslashes()</B
  20607. ></A
  20608. >, <A
  20609. HREF="#function.stripslashes"
  20610. ><B
  20611. CLASS="function"
  20612. >stripslashes()</B
  20613. ></A
  20614. > and 
  20615.       <A
  20616. HREF="#ini.magic-quotes-sybase"
  20617. >magic_quotes_sybase</A
  20618. >.
  20619.      </P
  20620. ></BLOCKQUOTE
  20621. ></DIV
  20622. ><P
  20623. >      PHP also understands arrays in the context of form variables 
  20624.      (see the <A
  20625. HREF="#faq.html"
  20626. >related faq</A
  20627. >).  You may, 
  20628.      for example, group related variables together, or use this 
  20629.      feature to retrieve values from a multiple select input.  For 
  20630.      example, let's post a form to itself and upon submission display 
  20631.      the data:
  20632.     </P
  20633. ><P
  20634. >      <TABLE
  20635. WIDTH="100%"
  20636. BORDER="0"
  20637. CELLPADDING="0"
  20638. CELLSPACING="0"
  20639. CLASS="EXAMPLE"
  20640. ><TR
  20641. ><TD
  20642. ><DIV
  20643. CLASS="example"
  20644. ><A
  20645. NAME="AEN3682"
  20646. ></A
  20647. ><P
  20648. ><B
  20649. >Example 12-10. More complex form variables</B
  20650. ></P
  20651. ><TABLE
  20652. BORDER="0"
  20653. BGCOLOR="#E0E0E0"
  20654. CELLPADDING="5"
  20655. ><TR
  20656. ><TD
  20657. ><PRE
  20658. CLASS="php"
  20659. ><?php
  20660. if (isset($_POST['action']) && $_POST['action'] == 'submitted') {
  20661.     echo '<pre>';
  20662.     print_r($_POST);
  20663.     echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
  20664.  
  20665.     echo '</pre>';
  20666. } else {
  20667. ?>
  20668. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  20669.     Name:  <input type="text" name="personal[name]" /><br />
  20670.     Email: <input type="text" name="personal[email]" /><br />
  20671.     Beer: <br />
  20672.     <select multiple name="beer[]">
  20673.         <option value="warthog">Warthog</option>
  20674.         <option value="guinness">Guinness</option>
  20675.         <option value="stuttgarter">Stuttgarter Schwabenbr├ñu</option>
  20676.     </select><br />
  20677.     <input type="hidden" name="action" value="submitted" />
  20678.     <input type="submit" name="submit" value="submit me!" />
  20679. </form>
  20680. <?php
  20681. }
  20682. ?></PRE
  20683. ></TD
  20684. ></TR
  20685. ></TABLE
  20686. ></DIV
  20687. ></TD
  20688. ></TR
  20689. ></TABLE
  20690. >
  20691.     </P
  20692. ><P
  20693. >      In PHP 3, the array form variable usage is limited to
  20694.      single-dimensional arrays. As of PHP 4, no such restriction applies.
  20695.     </P
  20696. ><DIV
  20697. CLASS="sect3"
  20698. ><HR><H4
  20699. CLASS="sect3"
  20700. ><A
  20701. NAME="language.variables.external.form.submit"
  20702. >IMAGE SUBMIT variable names</A
  20703. ></H4
  20704. ><P
  20705. >       When submitting a form, it is possible to use an image instead
  20706.       of the standard submit button with a tag like:
  20707.      </P
  20708. ><DIV
  20709. CLASS="informalexample"
  20710. ><P
  20711. ></P
  20712. ><A
  20713. NAME="AEN3689"
  20714. ></A
  20715. ><TABLE
  20716. BORDER="0"
  20717. BGCOLOR="#E0E0E0"
  20718. CELLPADDING="5"
  20719. ><TR
  20720. ><TD
  20721. ><PRE
  20722. CLASS="html"
  20723. ><input type="image" src="image.gif" name="sub" /></PRE
  20724. ></TD
  20725. ></TR
  20726. ></TABLE
  20727. ><P
  20728. ></P
  20729. ></DIV
  20730. ><P
  20731. >       When the user clicks somewhere on the image, the accompanying
  20732.       form will be transmitted to the server with two additional
  20733.       variables, sub_x and sub_y. These contain the coordinates of the
  20734.       user click within the image.  The experienced may note that the
  20735.       actual variable names sent by the browser contains a period
  20736.       rather than an underscore, but PHP converts the period to an
  20737.       underscore automatically.
  20738.      </P
  20739. ></DIV
  20740. ></DIV
  20741. ><DIV
  20742. CLASS="sect2"
  20743. ><HR><H3
  20744. CLASS="sect2"
  20745. ><A
  20746. NAME="language.variables.external.cookies"
  20747. >HTTP Cookies</A
  20748. ></H3
  20749. ><P
  20750. >      PHP transparently supports HTTP cookies as defined by <A
  20751. HREF="http://www.netscape.com/newsref/std/cookie_spec.html"
  20752. TARGET="_top"
  20753. >Netscape's Spec</A
  20754. >.  Cookies are a
  20755.      mechanism for storing data in the remote browser and thus
  20756.      tracking or identifying return users.  You can set cookies using
  20757.      the <A
  20758. HREF="#function.setcookie"
  20759. ><B
  20760. CLASS="function"
  20761. >setcookie()</B
  20762. ></A
  20763. > function.  Cookies are part of
  20764.      the HTTP header, so the SetCookie function must be called before
  20765.      any output is sent to the browser.  This is the same restriction
  20766.      as for the <A
  20767. HREF="#function.header"
  20768. ><B
  20769. CLASS="function"
  20770. >header()</B
  20771. ></A
  20772. > function.  Cookie data 
  20773.      is then available in the appropriate cookie data arrays, such 
  20774.      as <VAR
  20775. CLASS="varname"
  20776. >$_COOKIE</VAR
  20777. >, <VAR
  20778. CLASS="varname"
  20779. >$HTTP_COOKIE_VARS</VAR
  20780.      as well as in <VAR
  20781. CLASS="varname"
  20782. >$_REQUEST</VAR
  20783. >.  See the 
  20784.      <A
  20785. HREF="#function.setcookie"
  20786. ><B
  20787. CLASS="function"
  20788. >setcookie()</B
  20789. ></A
  20790. > manual page for more details and 
  20791.      examples.
  20792.     </P
  20793. ><P
  20794. >      If you wish to assign multiple values to a single cookie variable, you 
  20795.      may assign it as an array.  For example:
  20796.     </P
  20797. ><DIV
  20798. CLASS="informalexample"
  20799. ><P
  20800. ></P
  20801. ><A
  20802. NAME="AEN3703"
  20803. ></A
  20804. ><TABLE
  20805. BORDER="0"
  20806. BGCOLOR="#E0E0E0"
  20807. CELLPADDING="5"
  20808. ><TR
  20809. ><TD
  20810. ><PRE
  20811. CLASS="php"
  20812. ><?php
  20813.   setcookie("MyCookie[foo]", "Testing 1", time()+3600);
  20814.   setcookie("MyCookie[bar]", "Testing 2", time()+3600);
  20815. ?></PRE
  20816. ></TD
  20817. ></TR
  20818. ></TABLE
  20819. ><P
  20820. ></P
  20821. ></DIV
  20822. ><P
  20823. >      That will create two separate cookies although MyCookie will now 
  20824.      be a single array in your script.  If you want to set just one cookie 
  20825.      with multiple values, consider using <A
  20826. HREF="#function.serialize"
  20827. ><B
  20828. CLASS="function"
  20829. >serialize()</B
  20830. ></A
  20831. > or 
  20832.      <A
  20833. HREF="#function.explode"
  20834. ><B
  20835. CLASS="function"
  20836. >explode()</B
  20837. ></A
  20838. > on the value first.
  20839.     </P
  20840. ><P
  20841. >      Note that a cookie will replace a previous cookie by the same
  20842.      name in your browser unless the path or domain is different.  So,
  20843.      for a shopping cart application you may want to keep a counter
  20844.      and pass this along.  i.e.
  20845.     </P
  20846. ><TABLE
  20847. WIDTH="100%"
  20848. BORDER="0"
  20849. CELLPADDING="0"
  20850. CELLSPACING="0"
  20851. CLASS="EXAMPLE"
  20852. ><TR
  20853. ><TD
  20854. ><DIV
  20855. CLASS="example"
  20856. ><A
  20857. NAME="AEN3709"
  20858. ></A
  20859. ><P
  20860. ><B
  20861. >Example 12-11. A <A
  20862. HREF="#function.setcookie"
  20863. ><B
  20864. CLASS="function"
  20865. >setcookie()</B
  20866. ></A
  20867. > example</B
  20868. ></P
  20869. ><TABLE
  20870. BORDER="0"
  20871. BGCOLOR="#E0E0E0"
  20872. CELLPADDING="5"
  20873. ><TR
  20874. ><TD
  20875. ><PRE
  20876. CLASS="php"
  20877. ><?php
  20878. if (isset($_COOKIE['count'])) {
  20879.     $count = $_COOKIE['count'] + 1;
  20880. } else {
  20881.     $count = 1;
  20882. }
  20883. setcookie("count", $count, time()+3600);
  20884. setcookie("Cart[$count]", $item, time()+3600);
  20885. ?></PRE
  20886. ></TD
  20887. ></TR
  20888. ></TABLE
  20889. ></DIV
  20890. ></TD
  20891. ></TR
  20892. ></TABLE
  20893. ></DIV
  20894. ><DIV
  20895. CLASS="sect2"
  20896. ><HR><H3
  20897. CLASS="sect2"
  20898. ><A
  20899. NAME="language.variables.external.dot-in-names"
  20900. >Dots in incoming variable names</A
  20901. ></H3
  20902. ><P
  20903. >      Typically, PHP does not alter the names of variables when they
  20904.      are passed into a script. However, it should be noted that the
  20905.      dot (period, full stop) is not a valid character in a PHP
  20906.      variable name. For the reason, look at it:
  20907.      <TABLE
  20908. BORDER="0"
  20909. BGCOLOR="#E0E0E0"
  20910. CELLPADDING="5"
  20911. ><TR
  20912. ><TD
  20913. ><PRE
  20914. CLASS="php"
  20915. ><?php
  20916. $varname.ext;  /* invalid variable name */
  20917. ?></PRE
  20918. ></TD
  20919. ></TR
  20920. ></TABLE
  20921. >
  20922.      Now, what the parser sees is a variable named
  20923.      <VAR
  20924. CLASS="varname"
  20925. >$varname</VAR
  20926. >, followed by the string concatenation
  20927.      operator, followed by the barestring (i.e. unquoted string which
  20928.      doesn't match any known key or reserved words) 'ext'. Obviously,
  20929.      this doesn't have the intended result.
  20930.     </P
  20931. ><P
  20932. >      For this reason, it is important to note that PHP will
  20933.      automatically replace any dots in incoming variable names with
  20934.      underscores.
  20935.     </P
  20936. ></DIV
  20937. ><DIV
  20938. CLASS="sect2"
  20939. ><HR><H3
  20940. CLASS="sect2"
  20941. ><A
  20942. NAME="language.variables.determining-type-of"
  20943. >Determining variable types</A
  20944. ></H3
  20945. ><P
  20946. >      Because PHP determines the types of variables and converts them
  20947.      (generally) as needed, it is not always obvious what type a given
  20948.      variable is at any one time.  PHP includes several functions
  20949.      which find out what type a variable is, such as:
  20950.      <A
  20951. HREF="#function.gettype"
  20952. ><B
  20953. CLASS="function"
  20954. >gettype()</B
  20955. ></A
  20956. >, <A
  20957. HREF="#function.is-array"
  20958. ><B
  20959. CLASS="function"
  20960. >is_array()</B
  20961. ></A
  20962. >,
  20963.      <A
  20964. HREF="#function.is-float"
  20965. ><B
  20966. CLASS="function"
  20967. >is_float()</B
  20968. ></A
  20969. >, <A
  20970. HREF="#function.is-int"
  20971. ><B
  20972. CLASS="function"
  20973. >is_int()</B
  20974. ></A
  20975. >,
  20976.      <A
  20977. HREF="#function.is-object"
  20978. ><B
  20979. CLASS="function"
  20980. >is_object()</B
  20981. ></A
  20982. >, and
  20983.      <A
  20984. HREF="#function.is-string"
  20985. ><B
  20986. CLASS="function"
  20987. >is_string()</B
  20988. ></A
  20989. >.  See also the chapter on 
  20990.      <A
  20991. HREF="#language.types"
  20992. >Types</A
  20993. >.
  20994.     </P
  20995. ></DIV
  20996. ></DIV
  20997. ></DIV
  20998. ><DIV
  20999. CLASS="chapter"
  21000. ><HR><H1
  21001. ><A
  21002. NAME="language.constants"
  21003. >Chapter 13. Constants</A
  21004. ></H1
  21005. ><P
  21006. >    A constant is an identifier (name) for a simple value. As the name
  21007.    suggests, that value cannot change during the execution of the
  21008.    script (except for <A
  21009. HREF="#language.constants.predefined"
  21010. >    magic constants</A
  21011. >, which aren't actually constants).
  21012.    A constant is case-sensitive by default. By convention, constant 
  21013.    identifiers are always uppercase.
  21014.   </P
  21015. ><P
  21016. >    The name of a constant follows the same rules as any label in PHP. A 
  21017.    valid constant name starts with a letter or underscore, followed
  21018.    by any number of letters, numbers, or underscores. As a regular
  21019.    expression, it would be expressed thusly:
  21020.    <VAR
  21021. CLASS="literal"
  21022. >[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</VAR
  21023. >
  21024.   </P
  21025. ><P
  21026. >    <TABLE
  21027. WIDTH="100%"
  21028. BORDER="0"
  21029. CELLPADDING="0"
  21030. CELLSPACING="0"
  21031. CLASS="EXAMPLE"
  21032. ><TR
  21033. ><TD
  21034. ><DIV
  21035. CLASS="example"
  21036. ><A
  21037. NAME="AEN3736"
  21038. ></A
  21039. ><P
  21040. ><B
  21041. >Example 13-1. Valid and invalid constant names</B
  21042. ></P
  21043. ><TABLE
  21044. BORDER="0"
  21045. BGCOLOR="#E0E0E0"
  21046. CELLPADDING="5"
  21047. ><TR
  21048. ><TD
  21049. ><PRE
  21050. CLASS="php"
  21051. ><?php
  21052.  
  21053. // Valid constant names
  21054. define("FOO",     "something");
  21055. define("FOO2",    "something else");
  21056. define("FOO_BAR", "something more")
  21057.  
  21058. // Invalid constant names
  21059. define("2FOO",    "something");
  21060.  
  21061. // This is valid, but should be avoided:
  21062. // PHP may one day provide a magical constant
  21063. // that will break your script
  21064. define("__FOO__", "something"); 
  21065.  
  21066. ?></PRE
  21067. ></TD
  21068. ></TR
  21069. ></TABLE
  21070. ></DIV
  21071. ></TD
  21072. ></TR
  21073. ></TABLE
  21074. >
  21075.   </P
  21076. ><DIV
  21077. CLASS="note"
  21078. ><BLOCKQUOTE
  21079. CLASS="note"
  21080. ><P
  21081. ><B
  21082. >Note: </B
  21083. >
  21084.     For our purposes here, a letter is a-z, A-Z, and the ASCII
  21085.     characters from 127 through 255 (0x7f-0xff).
  21086.    </P
  21087. ></BLOCKQUOTE
  21088. ></DIV
  21089. ><P
  21090. >    Like <A
  21091. HREF="#language.variables.predefined"
  21092. >superglobals</A
  21093. >, the scope of a constant is global.  You 
  21094.    can access constants anywhere in your script without regard to scope.  
  21095.    For more information on scope, read the manual section on
  21096.    <A
  21097. HREF="#language.variables.scope"
  21098. >variable scope</A
  21099. >.
  21100.   </P
  21101. ><DIV
  21102. CLASS="sect1"
  21103. ><HR><H2
  21104. CLASS="sect1"
  21105. ><A
  21106. NAME="language.constants.syntax"
  21107. >Syntax</A
  21108. ></H2
  21109. ><P
  21110. >     You can define a constant by using the 
  21111.     <A
  21112. HREF="#function.define"
  21113. ><B
  21114. CLASS="function"
  21115. >define()</B
  21116. ></A
  21117. >-function. Once a constant is defined,
  21118.     it can never be changed or undefined.
  21119.    </P
  21120. ><P
  21121. >     Only scalar data (<A
  21122. HREF="#language.types.boolean"
  21123. ><B
  21124. CLASS="type"
  21125. >boolean</B
  21126. ></A
  21127. >, <A
  21128. HREF="#language.types.integer"
  21129. ><B
  21130. CLASS="type"
  21131. >integer</B
  21132. ></A
  21133. >, 
  21134.     <A
  21135. HREF="#language.types.float"
  21136. ><B
  21137. CLASS="type"
  21138. >float</B
  21139. ></A
  21140. > and <A
  21141. HREF="#language.types.string"
  21142. ><B
  21143. CLASS="type"
  21144. >string</B
  21145. ></A
  21146. >) can be contained 
  21147.     in constants.
  21148.    </P
  21149. ><P
  21150. >     You can get the value of a constant by simply specifying its name.
  21151.     Unlike with variables, you should <SPAN
  21152. CLASS="emphasis"
  21153. ><I
  21154. CLASS="emphasis"
  21155. >not</I
  21156. ></SPAN
  21157. > prepend
  21158.     a constant with a <VAR
  21159. CLASS="literal"
  21160. >$</VAR
  21161. >.
  21162.     You can also use the function <A
  21163. HREF="#function.constant"
  21164. ><B
  21165. CLASS="function"
  21166. >constant()</B
  21167. ></A
  21168. > to
  21169.     read a constant's value if you wish to obtain the constant's name
  21170.     dynamically. 
  21171.     Use <A
  21172. HREF="#function.get-defined-constants"
  21173. ><B
  21174. CLASS="function"
  21175. >get_defined_constants()</B
  21176. ></A
  21177. > to get a list of 
  21178.     all defined constants.
  21179.    </P
  21180. ><DIV
  21181. CLASS="note"
  21182. ><BLOCKQUOTE
  21183. CLASS="note"
  21184. ><P
  21185. ><B
  21186. >Note: </B
  21187. >
  21188.      Constants and (global) variables are in a different namespace. 
  21189.      This implies that for example <TT
  21190. CLASS="constant"
  21191. ><B
  21192. >TRUE</B
  21193. ></TT
  21194. > and 
  21195.      <VAR
  21196. CLASS="varname"
  21197. >$TRUE</VAR
  21198. > are generally different.
  21199.     </P
  21200. ></BLOCKQUOTE
  21201. ></DIV
  21202. ><P
  21203. >     If you use an undefined constant, PHP assumes that you mean
  21204.     the name of the constant itself, just as if you called it as
  21205.     a <A
  21206. HREF="#language.types.string"
  21207. ><B
  21208. CLASS="type"
  21209. >string</B
  21210. ></A
  21211. > (CONSTANT vs "CONSTANT").  An error of level
  21212.     <A
  21213. HREF="#ref.errorfunc"
  21214. >E_NOTICE</A
  21215. > will be issued
  21216.     when this happens.  See also the manual entry on why 
  21217.     <A
  21218. HREF="#language.types.array.foo-bar"
  21219. >$foo[bar]</A
  21220. > is
  21221.     wrong (unless you first <A
  21222. HREF="#function.define"
  21223. ><B
  21224. CLASS="function"
  21225. >define()</B
  21226. ></A
  21227. >
  21228.     <VAR
  21229. CLASS="literal"
  21230. >bar</VAR
  21231. > as a constant).  If you simply want to check if a
  21232.     constant is set, use the <A
  21233. HREF="#function.defined"
  21234. ><B
  21235. CLASS="function"
  21236. >defined()</B
  21237. ></A
  21238. > function.
  21239.    </P
  21240. ><P
  21241. >     These are the differences between constants and variables:
  21242.     <P
  21243. ></P
  21244. ><UL
  21245. ><LI
  21246. ><P
  21247. >        Constants do not have a dollar sign (<VAR
  21248. CLASS="literal"
  21249. >$</VAR
  21250. >)
  21251.        before them;
  21252.       </P
  21253. ></LI
  21254. ><LI
  21255. ><P
  21256. >        Constants may only be defined using the
  21257.        <A
  21258. HREF="#function.define"
  21259. ><B
  21260. CLASS="function"
  21261. >define()</B
  21262. ></A
  21263. > function, not by simple assignment;
  21264.       </P
  21265. ></LI
  21266. ><LI
  21267. ><P
  21268. >        Constants may be defined and accessed anywhere without regard
  21269.        to variable scoping rules;
  21270.       </P
  21271. ></LI
  21272. ><LI
  21273. ><P
  21274. >        Constants may not be redefined or undefined once they have been
  21275.        set; and
  21276.       </P
  21277. ></LI
  21278. ><LI
  21279. ><P
  21280. >        Constants may only evaluate to scalar values.
  21281.        </P
  21282. ></LI
  21283. ></UL
  21284. >
  21285.    </P
  21286. ><P
  21287. >     <TABLE
  21288. WIDTH="100%"
  21289. BORDER="0"
  21290. CELLPADDING="0"
  21291. CELLSPACING="0"
  21292. CLASS="EXAMPLE"
  21293. ><TR
  21294. ><TD
  21295. ><DIV
  21296. CLASS="example"
  21297. ><A
  21298. NAME="AEN3784"
  21299. ></A
  21300. ><P
  21301. ><B
  21302. >Example 13-2. Defining Constants</B
  21303. ></P
  21304. ><TABLE
  21305. BORDER="0"
  21306. BGCOLOR="#E0E0E0"
  21307. CELLPADDING="5"
  21308. ><TR
  21309. ><TD
  21310. ><PRE
  21311. CLASS="php"
  21312. ><?php
  21313. define("CONSTANT", "Hello world.");
  21314. echo CONSTANT; // outputs "Hello world."
  21315. echo Constant; // outputs "Constant" and issues a notice.
  21316. ?></PRE
  21317. ></TD
  21318. ></TR
  21319. ></TABLE
  21320. ></DIV
  21321. ></TD
  21322. ></TR
  21323. ></TABLE
  21324. >
  21325.  
  21326.    </P
  21327. ></DIV
  21328. ><DIV
  21329. CLASS="sect1"
  21330. ><HR><H2
  21331. CLASS="sect1"
  21332. ><A
  21333. NAME="language.constants.predefined"
  21334. >Magic constants</A
  21335. ></H2
  21336. ><P
  21337. >     PHP provides a large number of <A
  21338. HREF="#reserved.constants"
  21339. >predefined constants</A
  21340. > to any script
  21341.     which it runs. Many of these constants, however, are created by
  21342.     various extensions, and will only be present when those extensions
  21343.     are available, either via dynamic loading or because they have
  21344.     been compiled in.
  21345.    </P
  21346. ><P
  21347. >     There are five magical constants that change depending on
  21348.     where they are used.  For example, the value of
  21349.     <TT
  21350. CLASS="constant"
  21351. ><B
  21352. >__LINE__</B
  21353. ></TT
  21354. > depends on the line that it's
  21355.     used on in your script. These special constants are 
  21356.     case-insensitive and are as follows:
  21357.    </P
  21358. ><P
  21359. >     <DIV
  21360. CLASS="table"
  21361. ><A
  21362. NAME="AEN3794"
  21363. ></A
  21364. ><P
  21365. ><B
  21366. >Table 13-1. A few "magical" PHP constants</B
  21367. ></P
  21368. ><TABLE
  21369. BORDER="1"
  21370. CLASS="CALSTABLE"
  21371. ><COL><COL><THEAD
  21372. ><TR
  21373. ><TH
  21374. >Name</TH
  21375. ><TH
  21376. >Description</TH
  21377. ></TR
  21378. ></THEAD
  21379. ><TBODY
  21380. ><TR
  21381. ><TD
  21382. ><TT
  21383. CLASS="constant"
  21384. ><B
  21385. >__LINE__</B
  21386. ></TT
  21387. ></TD
  21388. ><TD
  21389. >          The current line number of the file.
  21390.         </TD
  21391. ></TR
  21392. ><TR
  21393. ><TD
  21394. ><TT
  21395. CLASS="constant"
  21396. ><B
  21397. >__FILE__</B
  21398. ></TT
  21399. ></TD
  21400. ><TD
  21401. >          The full path and filename of the file.  If used inside an include,
  21402.          the name of the included file is returned.
  21403.         </TD
  21404. ></TR
  21405. ><TR
  21406. ><TD
  21407. ><TT
  21408. CLASS="constant"
  21409. ><B
  21410. >__FUNCTION__</B
  21411. ></TT
  21412. ></TD
  21413. ><TD
  21414. >          The function name. (Added in PHP 4.3.0)  As of PHP 5 this constant 
  21415.          returns the function name as it was declared (case-sensitive).  In
  21416.          PHP 4 its value is always lowercased.
  21417.         </TD
  21418. ></TR
  21419. ><TR
  21420. ><TD
  21421. ><TT
  21422. CLASS="constant"
  21423. ><B
  21424. >__CLASS__</B
  21425. ></TT
  21426. ></TD
  21427. ><TD
  21428. >          The class name. (Added in PHP 4.3.0)  As of PHP 5 this constant 
  21429.          returns the class name as it was declared (case-sensitive).  In PHP
  21430.          4 its value is always lowercased.
  21431.         </TD
  21432. ></TR
  21433. ><TR
  21434. ><TD
  21435. ><TT
  21436. CLASS="constant"
  21437. ><B
  21438. >__METHOD__</B
  21439. ></TT
  21440. ></TD
  21441. ><TD
  21442. >          The class method name. (Added in PHP 5.0.0)  The method name is
  21443.          returned as it was declared (case-sensitive).
  21444.         </TD
  21445. ></TR
  21446. ></TBODY
  21447. ></TABLE
  21448. ></DIV
  21449. >
  21450.    </P
  21451. ><P
  21452. >     See also 
  21453.     <A
  21454. HREF="#function.get-class"
  21455. ><B
  21456. CLASS="function"
  21457. >get_class()</B
  21458. ></A
  21459. >,
  21460.     <A
  21461. HREF="#function.get-object-vars"
  21462. ><B
  21463. CLASS="function"
  21464. >get_object_vars()</B
  21465. ></A
  21466. >,
  21467.     <A
  21468. HREF="#function.file-exists"
  21469. ><B
  21470. CLASS="function"
  21471. >file_exists()</B
  21472. ></A
  21473. > and
  21474.     <A
  21475. HREF="#function.function-exists"
  21476. ><B
  21477. CLASS="function"
  21478. >function_exists()</B
  21479. ></A
  21480. >.
  21481.    </P
  21482. ></DIV
  21483. ></DIV
  21484. ><DIV
  21485. CLASS="chapter"
  21486. ><HR><H1
  21487. ><A
  21488. NAME="language.expressions"
  21489. >Chapter 14. Expressions</A
  21490. ></H1
  21491. ><P
  21492. >     Expressions are the most important building stones of PHP.  In PHP,
  21493.     almost anything you write is an expression.  The simplest yet
  21494.     most accurate way to define an expression is "anything that has a
  21495.     value".
  21496.    </P
  21497. ><P
  21498. >     The most basic forms of expressions are constants and variables.
  21499.     When you type "$a = 5", you're assigning '5' into $a.  '5', obviously,
  21500.     has the value 5, or in other words '5' is an expression with the
  21501.     value of 5 (in this case, '5' is an integer constant).
  21502.    </P
  21503. ><P
  21504. >     After this assignment, you'd expect $a's value to be 5 as
  21505.     well, so if you wrote $b = $a, you'd expect it to behave just as
  21506.     if you wrote $b = 5.  In other words, $a is an expression with the
  21507.     value of 5 as well.  If everything works right, this is exactly
  21508.     what will happen.
  21509.    </P
  21510. ><P
  21511. >     Slightly more complex examples for expressions are functions.  For
  21512.     instance, consider the following function:
  21513.     <DIV
  21514. CLASS="informalexample"
  21515. ><P
  21516. ></P
  21517. ><A
  21518. NAME="AEN3833"
  21519. ></A
  21520. ><TABLE
  21521. BORDER="0"
  21522. BGCOLOR="#E0E0E0"
  21523. CELLPADDING="5"
  21524. ><TR
  21525. ><TD
  21526. ><PRE
  21527. CLASS="php"
  21528. ><?php
  21529. function foo ()
  21530. {
  21531.     return 5;
  21532. }
  21533. ?></PRE
  21534. ></TD
  21535. ></TR
  21536. ></TABLE
  21537. ><P
  21538. ></P
  21539. ></DIV
  21540. >
  21541.    </P
  21542. ><P
  21543. >     Assuming you're familiar with the concept of functions (if you're
  21544.     not, take a look at the chapter about <A
  21545. HREF="#language.functions"
  21546. >functions</A
  21547. >), you'd assume
  21548.     that typing <VAR
  21549. CLASS="literal"
  21550. >$c = foo()</VAR
  21551. > is essentially just like
  21552.     writing <VAR
  21553. CLASS="literal"
  21554. >$c = 5</VAR
  21555. >, and you're right.  Functions
  21556.     are expressions with the value of their return value.  Since foo()
  21557.     returns 5, the value of the expression 'foo()' is 5.  Usually
  21558.     functions don't just return a static value but compute something.
  21559.    </P
  21560. ><P
  21561. >     Of course, values in PHP don't have to be integers, and very often
  21562.     they aren't.  PHP supports four scalar value types: <A
  21563. HREF="#language.types.integer"
  21564. ><B
  21565. CLASS="type"
  21566. >integer</B
  21567. ></A
  21568. >
  21569.     values, floating point values (<A
  21570. HREF="#language.types.float"
  21571. ><B
  21572. CLASS="type"
  21573. >float</B
  21574. ></A
  21575. >), <A
  21576. HREF="#language.types.string"
  21577. ><B
  21578. CLASS="type"
  21579. >string</B
  21580. ></A
  21581. >
  21582.     values and <A
  21583. HREF="#language.types.boolean"
  21584. ><B
  21585. CLASS="type"
  21586. >boolean</B
  21587. ></A
  21588. > values (scalar values are values that you
  21589.     can't 'break' into smaller pieces, unlike arrays, for instance). PHP also 
  21590.     supports two composite (non-scalar) types: arrays and objects. Each of
  21591.     these value types can be assigned into variables or returned from functions.
  21592.    </P
  21593. ><P
  21594. >     PHP takes expressions much further, in the same way many other languages
  21595.     do.  PHP is an expression-oriented language, in the
  21596.     sense that almost everything is an expression.  Consider the
  21597.     example we've already dealt with, '$a = 5'.  It's easy to see that
  21598.     there are two values involved here, the value of the integer
  21599.     constant '5', and the value of $a which is being updated to 5 as
  21600.     well.  But the truth is that there's one additional value involved
  21601.     here, and that's the value of the assignment itself.  The
  21602.     assignment itself evaluates to the assigned value, in this case 5.
  21603.     In practice, it means that '$a = 5', regardless of what it does,
  21604.     is an expression with the value 5.  Thus, writing something like
  21605.     '$b = ($a = 5)' is like writing '$a = 5; $b = 5;' (a semicolon
  21606.     marks the end of a statement).  Since assignments are parsed in a
  21607.     right to left order, you can also write '$b = $a = 5'.
  21608.    </P
  21609. ><P
  21610. >     Another good example of expression orientation is pre- and
  21611.     post-increment and decrement.  Users of PHP and many other
  21612.     languages may be familiar with the notation of variable++ and
  21613.     variable--.  These are <A
  21614. HREF="#language.operators.increment"
  21615. >     increment and decrement operators</A
  21616. >.  In
  21617.     PHP/FI 2, the statement '$a++' has no value (is not an
  21618.     expression), and thus you can't assign it or use it in any way.
  21619.     PHP enhances the increment/decrement capabilities by making
  21620.     these expressions as well, like in C.  In PHP, like in C, there
  21621.     are two types of increment - pre-increment and post-increment.
  21622.     Both pre-increment and post-increment essentially increment the
  21623.     variable, and the effect on the variable is identical.  The
  21624.     difference is with the value of the increment expression.
  21625.     Pre-increment, which is written '++$variable', evaluates to the
  21626.     incremented value (PHP increments the variable before reading its
  21627.     value, thus the name 'pre-increment').  Post-increment, which is
  21628.     written '$variable++' evaluates to the original value of
  21629.     $variable, before it was incremented (PHP increments the variable
  21630.     after reading its value, thus the name 'post-increment').
  21631.    </P
  21632. ><P
  21633. >     A very common type of expressions are <A
  21634. HREF="#language.operators.comparison"
  21635. >comparison</A
  21636. >
  21637.     expressions. These expressions evaluate to either  <TT
  21638. CLASS="constant"
  21639. ><B
  21640. >FALSE</B
  21641. ></TT
  21642. > or <TT
  21643. CLASS="constant"
  21644. ><B
  21645. >TRUE</B
  21646. ></TT
  21647. >. PHP
  21648.     supports > (bigger than), >= (bigger than or equal to), == (equal),
  21649.     != (not equal), < (smaller than) and <= (smaller than or equal to).
  21650.     The language also supports a set of strict equivalence operators: ===
  21651.     (equal to and same type) and !== (not equal to or not same type).
  21652.     These expressions are most commonly used inside conditional execution,
  21653.     such as <VAR
  21654. CLASS="literal"
  21655. >if</VAR
  21656. > statements.
  21657.    </P
  21658. ><P
  21659. >     The last example of expressions we'll deal with here is combined
  21660.     operator-assignment expressions.  You already know that if you
  21661.     want to increment $a by 1, you can simply write '$a++' or '++$a'.
  21662.     But what if you want to add more than one to it, for instance 3?
  21663.     You could write '$a++' multiple times, but this is obviously not a
  21664.     very efficient or comfortable way.  A much more common practice is
  21665.     to write '$a = $a + 3'.  '$a + 3' evaluates to the value of $a
  21666.     plus 3, and is assigned back into $a, which results in
  21667.     incrementing $a by 3.  In PHP, as in several other languages
  21668.     like C, you can write this in a shorter way, which with time would
  21669.     become clearer and quicker to understand as well.  Adding 3 to the
  21670.     current value of $a can be written '$a += 3'.  This means exactly
  21671.     "take the value of $a, add 3 to it, and assign it back into $a".
  21672.     In addition to being shorter and clearer, this also results in
  21673.     faster execution.  The value of '$a += 3', like the value of a
  21674.     regular assignment, is the assigned value.  Notice that it is NOT
  21675.     3, but the combined value of $a plus 3 (this is the value that's
  21676.     assigned into $a).  Any two-place operator can be used in this
  21677.     operator-assignment mode, for example '$a -= 5' (subtract 5 from
  21678.     the value of $a), '$b *= 7' (multiply the value of $b by 7), etc.
  21679.    </P
  21680. ><P
  21681. >     There is one more expression that may seem odd if you haven't seen
  21682.     it in other languages, the ternary conditional operator:
  21683.    </P
  21684. ><P
  21685. >     <DIV
  21686. CLASS="informalexample"
  21687. ><P
  21688. ></P
  21689. ><A
  21690. NAME="AEN3855"
  21691. ></A
  21692. ><TABLE
  21693. BORDER="0"
  21694. BGCOLOR="#E0E0E0"
  21695. CELLPADDING="5"
  21696. ><TR
  21697. ><TD
  21698. ><PRE
  21699. CLASS="php"
  21700. ><?php
  21701. $first ? $second : $third
  21702. ?></PRE
  21703. ></TD
  21704. ></TR
  21705. ></TABLE
  21706. ><P
  21707. ></P
  21708. ></DIV
  21709. >
  21710.    </P
  21711. ><P
  21712. >     If the value of the first subexpression is <TT
  21713. CLASS="constant"
  21714. ><B
  21715. >TRUE</B
  21716. ></TT
  21717. > (non-zero), then
  21718.     the second subexpression is evaluated, and that is the result of
  21719.     the conditional expression. Otherwise, the third subexpression is
  21720.     evaluated, and that is the value.
  21721.    </P
  21722. ><P
  21723. >     The following example should help you understand pre- and
  21724.     post-increment and expressions in general a bit better:
  21725.    </P
  21726. ><P
  21727. >     <DIV
  21728. CLASS="informalexample"
  21729. ><P
  21730. ></P
  21731. ><A
  21732. NAME="AEN3861"
  21733. ></A
  21734. ><TABLE
  21735. BORDER="0"
  21736. BGCOLOR="#E0E0E0"
  21737. CELLPADDING="5"
  21738. ><TR
  21739. ><TD
  21740. ><PRE
  21741. CLASS="php"
  21742. ><?php
  21743. function double($i)
  21744. {
  21745.     return $i*2;
  21746. }
  21747. $b = $a = 5;        /* assign the value five into the variable $a and $b */
  21748. $c = $a++;          /* post-increment, assign original value of $a 
  21749.                        (5) to $c */
  21750. $e = $d = ++$b;     /* pre-increment, assign the incremented value of 
  21751.                        $b (6) to $d and $e */
  21752.  
  21753. /* at this point, both $d and $e are equal to 6 */
  21754.  
  21755. $f = double($d++);  /* assign twice the value of $d before
  21756.                        the increment, 2*6 = 12 to $f */
  21757. $g = double(++$e);  /* assign twice the value of $e after
  21758.                        the increment, 2*7 = 14 to $g */
  21759. $h = $g += 10;      /* first, $g is incremented by 10 and ends with the 
  21760.                        value of 24. the value of the assignment (24) is 
  21761.                        then assigned into $h, and $h ends with the value 
  21762.                        of 24 as well. */
  21763. ?></PRE
  21764. ></TD
  21765. ></TR
  21766. ></TABLE
  21767. ><P
  21768. ></P
  21769. ></DIV
  21770. >
  21771.    </P
  21772. ><P
  21773. >     Some expressions can be considered as statements. In
  21774.     this case, a statement has the form of 'expr' ';' that is, an
  21775.     expression followed by a semicolon.  In '$b=$a=5;', $a=5 is a
  21776.     valid expression, but it's not a statement by itself.  '$b=$a=5;'
  21777.     however is a valid statement.
  21778.    </P
  21779. ><P
  21780. >     One last thing worth mentioning is the truth value of expressions.
  21781.     In many events, mainly in conditional execution and loops, you're
  21782.     not interested in the specific value of the expression, but only
  21783.     care about whether it means <TT
  21784. CLASS="constant"
  21785. ><B
  21786. >TRUE</B
  21787. ></TT
  21788. > or <TT
  21789. CLASS="constant"
  21790. ><B
  21791. >FALSE</B
  21792. ></TT
  21793. >.
  21794.     
  21795.     
  21796.     
  21797.     The constants <TT
  21798. CLASS="constant"
  21799. ><B
  21800. >TRUE</B
  21801. ></TT
  21802. > and <TT
  21803. CLASS="constant"
  21804. ><B
  21805. >FALSE</B
  21806. ></TT
  21807. > (case-insensitive) are the two 
  21808.     possible boolean values. When necessary, an expression is 
  21809.     automatically converted to boolean. See the 
  21810.     <A
  21811. HREF="#language.types.typecasting"
  21812. >section about
  21813.     type-casting</A
  21814. > for details about how.
  21815.    </P
  21816. ><P
  21817. >     PHP provides a full and powerful implementation of expressions, and
  21818.     documenting it entirely goes beyond the scope of this manual. The
  21819.     above examples should give you a good idea about what expressions
  21820.     are and how you can construct useful expressions. Throughout the
  21821.     rest of this manual we'll write <VAR
  21822. CLASS="varname"
  21823. >expr</VAR
  21824. >
  21825.     to indicate any valid PHP expression.
  21826.    </P
  21827. ></DIV
  21828. ><DIV
  21829. CLASS="chapter"
  21830. ><HR><H1
  21831. ><A
  21832. NAME="language.operators"
  21833. >Chapter 15. Operators</A
  21834. ></H1
  21835. ><P
  21836. >    An operator is something that you feed with one or more values (or
  21837.    expressions, in programming jargon) which yields another value (so that the
  21838.    construction itself becomes an expression). So you can think of functions
  21839.    or constructions that return a value (like print) as operators and those
  21840.    that return nothing (like echo) as any other thing.
  21841.   </P
  21842. ><P
  21843. >    There are three types of operators.  Firstly there is the unary operator which
  21844.    operates on only one value, for example ! (the negation operator) or ++
  21845.    (the increment operator). The second group are termed binary operators; this
  21846.    group contains most of the operators that PHP supports, and a list follows
  21847.    below in the section <A
  21848. HREF="#language.operators.precedence"
  21849. >Operator
  21850.    Precedence</A
  21851. >.
  21852.   </P
  21853. ><P
  21854. >    The third group is the ternary operator: ?:.  It should be used to select
  21855.    between two expressions depending on a third one, rather than to select two
  21856.    sentences or paths of execution. Surrounding ternary expressions with
  21857.    parentheses is a very good idea.
  21858.   </P
  21859. ><DIV
  21860. CLASS="sect1"
  21861. ><HR><H2
  21862. CLASS="sect1"
  21863. ><A
  21864. NAME="language.operators.precedence"
  21865. >Operator Precedence</A
  21866. ></H2
  21867. ><P
  21868. >     The precedence of an operator specifies how "tightly" it binds two
  21869.     expressions together. For example, in the expression <VAR
  21870. CLASS="literal"
  21871. >1 +
  21872.     5 * 3</VAR
  21873. >, the answer is <VAR
  21874. CLASS="literal"
  21875. >16</VAR
  21876. > and not
  21877.     <VAR
  21878. CLASS="literal"
  21879. >18</VAR
  21880. > because the multiplication ("*") operator
  21881.     has a higher precedence than the addition ("+") operator.
  21882.     Parentheses may be used to force precedence, if necessary. For
  21883.     instance: <VAR
  21884. CLASS="literal"
  21885. >(1 + 5) * 3</VAR
  21886. > evaluates to
  21887.     <VAR
  21888. CLASS="literal"
  21889. >18</VAR
  21890. >. If operator precedence is equal, left to right 
  21891.     associativity is used.
  21892.    </P
  21893. ><P
  21894. >     The following table lists the precedence of operators with the
  21895.     highest-precedence operators listed at the top of the table. Operators
  21896.     on the same line have equal precedence, in which case their
  21897.     associativity decides which order to evaluate them in.
  21898.     <DIV
  21899. CLASS="table"
  21900. ><A
  21901. NAME="AEN3887"
  21902. ></A
  21903. ><P
  21904. ><B
  21905. >Table 15-1. Operator Precedence</B
  21906. ></P
  21907. ><TABLE
  21908. BORDER="1"
  21909. CLASS="CALSTABLE"
  21910. ><COL><COL><THEAD
  21911. ><TR
  21912. ><TH
  21913. >Associativity</TH
  21914. ><TH
  21915. >Operators</TH
  21916. ></TR
  21917. ></THEAD
  21918. ><TBODY
  21919. ><TR
  21920. ><TD
  21921. >non-associative</TD
  21922. ><TD
  21923. >new</TD
  21924. ></TR
  21925. ><TR
  21926. ><TD
  21927. >right</TD
  21928. ><TD
  21929. >[</TD
  21930. ></TR
  21931. ><TR
  21932. ><TD
  21933. >non-associative</TD
  21934. ><TD
  21935. >++ --</TD
  21936. ></TR
  21937. ><TR
  21938. ><TD
  21939. >non-associative</TD
  21940. ><TD
  21941. >! ~ - (int) (float) (string) (array) (object) @</TD
  21942. ></TR
  21943. ><TR
  21944. ><TD
  21945. >left</TD
  21946. ><TD
  21947. >* / %</TD
  21948. ></TR
  21949. ><TR
  21950. ><TD
  21951. >left</TD
  21952. ><TD
  21953. >+ - .</TD
  21954. ></TR
  21955. ><TR
  21956. ><TD
  21957. >left</TD
  21958. ><TD
  21959. ><< >></TD
  21960. ></TR
  21961. ><TR
  21962. ><TD
  21963. >non-associative</TD
  21964. ><TD
  21965. >< <= > >=</TD
  21966. ></TR
  21967. ><TR
  21968. ><TD
  21969. >non-associative</TD
  21970. ><TD
  21971. >== != === !==</TD
  21972. ></TR
  21973. ><TR
  21974. ><TD
  21975. >left</TD
  21976. ><TD
  21977. >&</TD
  21978. ></TR
  21979. ><TR
  21980. ><TD
  21981. >left</TD
  21982. ><TD
  21983. >^</TD
  21984. ></TR
  21985. ><TR
  21986. ><TD
  21987. >left</TD
  21988. ><TD
  21989. >|</TD
  21990. ></TR
  21991. ><TR
  21992. ><TD
  21993. >left</TD
  21994. ><TD
  21995. >&&</TD
  21996. ></TR
  21997. ><TR
  21998. ><TD
  21999. >left</TD
  22000. ><TD
  22001. >||</TD
  22002. ></TR
  22003. ><TR
  22004. ><TD
  22005. >left</TD
  22006. ><TD
  22007. >? :</TD
  22008. ></TR
  22009. ><TR
  22010. ><TD
  22011. >right</TD
  22012. ><TD
  22013. >          = += -= *= /= .= %= &= |= ^= <<= >>=
  22014.         </TD
  22015. ></TR
  22016. ><TR
  22017. ><TD
  22018. >left</TD
  22019. ><TD
  22020. >and</TD
  22021. ></TR
  22022. ><TR
  22023. ><TD
  22024. >left</TD
  22025. ><TD
  22026. >xor</TD
  22027. ></TR
  22028. ><TR
  22029. ><TD
  22030. >left</TD
  22031. ><TD
  22032. >or</TD
  22033. ></TR
  22034. ><TR
  22035. ><TD
  22036. >left</TD
  22037. ><TD
  22038. >,</TD
  22039. ></TR
  22040. ></TBODY
  22041. ></TABLE
  22042. ></DIV
  22043. >
  22044.    </P
  22045. ><P
  22046. >     Left associativity means that the expression is evaluated from left to right,
  22047.     right associativity means the opposite.
  22048.     <TABLE
  22049. WIDTH="100%"
  22050. BORDER="0"
  22051. CELLPADDING="0"
  22052. CELLSPACING="0"
  22053. CLASS="EXAMPLE"
  22054. ><TR
  22055. ><TD
  22056. ><DIV
  22057. CLASS="example"
  22058. ><A
  22059. NAME="AEN3956"
  22060. ></A
  22061. ><P
  22062. ><B
  22063. >Example 15-1. Associativity</B
  22064. ></P
  22065. ><TABLE
  22066. BORDER="0"
  22067. BGCOLOR="#E0E0E0"
  22068. CELLPADDING="5"
  22069. ><TR
  22070. ><TD
  22071. ><PRE
  22072. CLASS="php"
  22073. ><?php
  22074. $a = 3 * 3 % 5; // (3 * 3) % 5 = 4
  22075. $a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2
  22076.  
  22077. $a = 1;
  22078. $b = 2;
  22079. $a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
  22080. ?></PRE
  22081. ></TD
  22082. ></TR
  22083. ></TABLE
  22084. ></DIV
  22085. ></TD
  22086. ></TR
  22087. ></TABLE
  22088. >
  22089.     Use parentheses to increase readability of the code.
  22090.    </P
  22091. ><DIV
  22092. CLASS="note"
  22093. ><BLOCKQUOTE
  22094. CLASS="note"
  22095. ><P
  22096. ><B
  22097. >Note: </B
  22098. >
  22099.      Although <VAR
  22100. CLASS="literal"
  22101. >!</VAR
  22102. > has a higher precedence than
  22103.      <VAR
  22104. CLASS="literal"
  22105. >=</VAR
  22106. >, PHP will still allow expressions
  22107.      similar to the following: <VAR
  22108. CLASS="literal"
  22109. >if (!$a = foo())</VAR
  22110. >,
  22111.      in which case the output from <VAR
  22112. CLASS="literal"
  22113. >foo()</VAR
  22114. > is
  22115.      put into <VAR
  22116. CLASS="varname"
  22117. >$a</VAR
  22118. >.
  22119.     </P
  22120. ></BLOCKQUOTE
  22121. ></DIV
  22122. ></DIV
  22123. ><DIV
  22124. CLASS="sect1"
  22125. ><HR><H2
  22126. CLASS="sect1"
  22127. ><A
  22128. NAME="language.operators.arithmetic"
  22129. >Arithmetic Operators</A
  22130. ></H2
  22131. ><P
  22132. >     Remember basic arithmetic from school? These work just
  22133.     like those.
  22134.    </P
  22135. ><DIV
  22136. CLASS="table"
  22137. ><A
  22138. NAME="AEN3969"
  22139. ></A
  22140. ><P
  22141. ><B
  22142. >Table 15-2. Arithmetic Operators</B
  22143. ></P
  22144. ><TABLE
  22145. BORDER="1"
  22146. CLASS="CALSTABLE"
  22147. ><COL><COL><COL><THEAD
  22148. ><TR
  22149. ><TH
  22150. >Example</TH
  22151. ><TH
  22152. >Name</TH
  22153. ><TH
  22154. >Result</TH
  22155. ></TR
  22156. ></THEAD
  22157. ><TBODY
  22158. ><TR
  22159. ><TD
  22160. >-$a</TD
  22161. ><TD
  22162. >Negation</TD
  22163. ><TD
  22164. >Opposite of $a.</TD
  22165. ></TR
  22166. ><TR
  22167. ><TD
  22168. >$a + $b</TD
  22169. ><TD
  22170. >Addition</TD
  22171. ><TD
  22172. >Sum of $a and $b.</TD
  22173. ></TR
  22174. ><TR
  22175. ><TD
  22176. >$a - $b</TD
  22177. ><TD
  22178. >Subtraction</TD
  22179. ><TD
  22180. >Difference of $a and $b.</TD
  22181. ></TR
  22182. ><TR
  22183. ><TD
  22184. >$a * $b</TD
  22185. ><TD
  22186. >Multiplication</TD
  22187. ><TD
  22188. >Product of $a and $b.</TD
  22189. ></TR
  22190. ><TR
  22191. ><TD
  22192. >$a / $b</TD
  22193. ><TD
  22194. >Division</TD
  22195. ><TD
  22196. >Quotient of $a and $b.</TD
  22197. ></TR
  22198. ><TR
  22199. ><TD
  22200. >$a % $b</TD
  22201. ><TD
  22202. >Modulus</TD
  22203. ><TD
  22204. >Remainder of $a divided by $b.</TD
  22205. ></TR
  22206. ></TBODY
  22207. ></TABLE
  22208. ></DIV
  22209. ><P
  22210. >     The division operator ("/") returns a float value anytime,
  22211.     even if the two operands are integers (or strings that get
  22212.     converted to integers).
  22213.    </P
  22214. ><DIV
  22215. CLASS="note"
  22216. ><BLOCKQUOTE
  22217. CLASS="note"
  22218. ><P
  22219. ><B
  22220. >Note: </B
  22221. >
  22222.      Remainder <VAR
  22223. CLASS="literal"
  22224. >$a % $b</VAR
  22225. > is negative for negative
  22226.      <VAR
  22227. CLASS="literal"
  22228. >$a</VAR
  22229. >.
  22230.     </P
  22231. ></BLOCKQUOTE
  22232. ></DIV
  22233. ><P
  22234. >     See also the manual page on 
  22235.     <A
  22236. HREF="#ref.math"
  22237. >Math functions</A
  22238. >. 
  22239.    </P
  22240. ></DIV
  22241. ><DIV
  22242. CLASS="sect1"
  22243. ><HR><H2
  22244. CLASS="sect1"
  22245. ><A
  22246. NAME="language.operators.assignment"
  22247. >Assignment Operators</A
  22248. ></H2
  22249. ><P
  22250. >     The basic assignment operator is "=". Your first inclination might
  22251.     be to think of this as "equal to". Don't. It really means that the
  22252.     the left operand gets set to the value of the expression on the
  22253.     rights (that is, "gets set to").
  22254.    </P
  22255. ><P
  22256. >     The value of an assignment expression is the value assigned. That
  22257.     is, the value of "$a = 3" is 3. This allows you to do some tricky
  22258.     things: 
  22259.     <DIV
  22260. CLASS="informalexample"
  22261. ><P
  22262. ></P
  22263. ><A
  22264. NAME="AEN4013"
  22265. ></A
  22266. ><TABLE
  22267. BORDER="0"
  22268. BGCOLOR="#E0E0E0"
  22269. CELLPADDING="5"
  22270. ><TR
  22271. ><TD
  22272. ><PRE
  22273. CLASS="php"
  22274. ><?php
  22275.  
  22276. $a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
  22277.  
  22278. ?></PRE
  22279. ></TD
  22280. ></TR
  22281. ></TABLE
  22282. ><P
  22283. ></P
  22284. ></DIV
  22285. >
  22286.    </P
  22287. ><P
  22288. >     In addition to the basic assignment operator, there are "combined
  22289.     operators" for all of the <A
  22290. HREF="#language.operators"
  22291. >binary
  22292.     arithmetic</A
  22293. > and string operators that allow you to use a value in an
  22294.     expression and then set its value to the result of that expression. For
  22295.     example:
  22296.     <DIV
  22297. CLASS="informalexample"
  22298. ><P
  22299. ></P
  22300. ><A
  22301. NAME="AEN4017"
  22302. ></A
  22303. ><TABLE
  22304. BORDER="0"
  22305. BGCOLOR="#E0E0E0"
  22306. CELLPADDING="5"
  22307. ><TR
  22308. ><TD
  22309. ><PRE
  22310. CLASS="php"
  22311. ><?php
  22312.  
  22313. $a = 3;
  22314. $a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
  22315. $b = "Hello ";
  22316. $b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
  22317.  
  22318. ?></PRE
  22319. ></TD
  22320. ></TR
  22321. ></TABLE
  22322. ><P
  22323. ></P
  22324. ></DIV
  22325. >
  22326.    </P
  22327. ><P
  22328. >     Note that the assignment copies the original variable to the new
  22329.     one (assignment by value), so changes to one will not affect the
  22330.     other. This may also have relevance if you need to copy something
  22331.     like a large array inside a tight loop. Since PHP 4, assignment
  22332.     by reference has been supported, using the <SAMP
  22333. CLASS="computeroutput"
  22334. >$var =
  22335.     &$othervar;</SAMP
  22336. > syntax, but this is not possible
  22337.     in PHP 3. 'Assignment by reference' means that both variables end
  22338.     up pointing at the same data, and nothing is copied anywhere. 
  22339.     To learn more about references, please read <A
  22340. HREF="#language.references"
  22341. >References explained</A
  22342. >.
  22343.    </P
  22344. ></DIV
  22345. ><DIV
  22346. CLASS="sect1"
  22347. ><HR><H2
  22348. CLASS="sect1"
  22349. ><A
  22350. NAME="language.operators.bitwise"
  22351. >Bitwise Operators</A
  22352. ></H2
  22353. ><P
  22354. >     Bitwise operators allow you to turn specific bits within an
  22355.     integer on or off. If both the left- and right-hand parameters are
  22356.     strings, the bitwise operator will operate on the characters' ASCII
  22357.     values.
  22358.    </P
  22359. ><P
  22360. >     <DIV
  22361. CLASS="informalexample"
  22362. ><P
  22363. ></P
  22364. ><A
  22365. NAME="AEN4026"
  22366. ></A
  22367. ><TABLE
  22368. BORDER="0"
  22369. BGCOLOR="#E0E0E0"
  22370. CELLPADDING="5"
  22371. ><TR
  22372. ><TD
  22373. ><PRE
  22374. CLASS="php"
  22375. ><?php
  22376. echo 12 ^ 9; // Outputs '5'
  22377.  
  22378. echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
  22379.                  // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8
  22380.  
  22381. echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
  22382.                         // 'a' ^ 'e' = #4
  22383. ?></PRE
  22384. ></TD
  22385. ></TR
  22386. ></TABLE
  22387. ><P
  22388. ></P
  22389. ></DIV
  22390.    </P
  22391. ><DIV
  22392. CLASS="table"
  22393. ><A
  22394. NAME="AEN4028"
  22395. ></A
  22396. ><P
  22397. ><B
  22398. >Table 15-3. Bitwise Operators</B
  22399. ></P
  22400. ><TABLE
  22401. BORDER="1"
  22402. CLASS="CALSTABLE"
  22403. ><COL><COL><COL><THEAD
  22404. ><TR
  22405. ><TH
  22406. >Example</TH
  22407. ><TH
  22408. >Name</TH
  22409. ><TH
  22410. >Result</TH
  22411. ></TR
  22412. ></THEAD
  22413. ><TBODY
  22414. ><TR
  22415. ><TD
  22416. >$a & $b</TD
  22417. ><TD
  22418. >And</TD
  22419. ><TD
  22420. >Bits that are set in both $a and $b are set.</TD
  22421. ></TR
  22422. ><TR
  22423. ><TD
  22424. >$a | $b</TD
  22425. ><TD
  22426. >Or</TD
  22427. ><TD
  22428. >Bits that are set in either $a or $b are set.</TD
  22429. ></TR
  22430. ><TR
  22431. ><TD
  22432. >$a ^ $b</TD
  22433. ><TD
  22434. >Xor</TD
  22435. ><TD
  22436. >         Bits that are set in $a or $b but not both are set.
  22437.        </TD
  22438. ></TR
  22439. ><TR
  22440. ><TD
  22441. >~ $a</TD
  22442. ><TD
  22443. >Not</TD
  22444. ><TD
  22445. >         Bits that are set in $a are not set, and vice versa.
  22446.        </TD
  22447. ></TR
  22448. ><TR
  22449. ><TD
  22450. >$a << $b</TD
  22451. ><TD
  22452. >Shift left</TD
  22453. ><TD
  22454. >         Shift the bits of $a $b steps to the left (each step means
  22455.         "multiply by two")
  22456.        </TD
  22457. ></TR
  22458. ><TR
  22459. ><TD
  22460. >$a >> $b</TD
  22461. ><TD
  22462. >Shift right</TD
  22463. ><TD
  22464. >         Shift the bits of $a $b steps to the right (each step means
  22465.         "divide by two")
  22466.        </TD
  22467. ></TR
  22468. ></TBODY
  22469. ></TABLE
  22470. ></DIV
  22471. ><DIV
  22472. CLASS="warning"
  22473. ><P
  22474. ></P
  22475. ><TABLE
  22476. CLASS="warning"
  22477. BORDER="1"
  22478. WIDTH="100%"
  22479. ><TR
  22480. ><TD
  22481. ALIGN="CENTER"
  22482. ><B
  22483. >Warning</B
  22484. ></TD
  22485. ></TR
  22486. ><TR
  22487. ><TD
  22488. ALIGN="LEFT"
  22489. ><P
  22490. >      Don't right shift for more than 32 bits on 32 bits systems. Don't left shift
  22491.      in case it results to number longer than 32 bits.
  22492.     </P
  22493. ></TD
  22494. ></TR
  22495. ></TABLE
  22496. ></DIV
  22497. ></DIV
  22498. ><DIV
  22499. CLASS="sect1"
  22500. ><HR><H2
  22501. CLASS="sect1"
  22502. ><A
  22503. NAME="language.operators.comparison"
  22504. >Comparison Operators</A
  22505. ></H2
  22506. ><P
  22507. >     Comparison operators, as their name implies, allow you to compare
  22508.     two values.  You may also be interested in viewing
  22509.     <A
  22510. HREF="#types.comparisons"
  22511. >the type comparison tables</A
  22512. >, 
  22513.     as they show examples of various type related comparisons.
  22514.    </P
  22515. ><DIV
  22516. CLASS="table"
  22517. ><A
  22518. NAME="AEN4067"
  22519. ></A
  22520. ><P
  22521. ><B
  22522. >Table 15-4. Comparison Operators</B
  22523. ></P
  22524. ><TABLE
  22525. BORDER="1"
  22526. CLASS="CALSTABLE"
  22527. ><COL><COL><COL><THEAD
  22528. ><TR
  22529. ><TH
  22530. >Example</TH
  22531. ><TH
  22532. >Name</TH
  22533. ><TH
  22534. >Result</TH
  22535. ></TR
  22536. ></THEAD
  22537. ><TBODY
  22538. ><TR
  22539. ><TD
  22540. >$a == $b</TD
  22541. ><TD
  22542. >Equal</TD
  22543. ><TD
  22544. ><TT
  22545. CLASS="constant"
  22546. ><B
  22547. >TRUE</B
  22548. ></TT
  22549. > if $a is equal to $b.</TD
  22550. ></TR
  22551. ><TR
  22552. ><TD
  22553. >$a === $b</TD
  22554. ><TD
  22555. >Identical</TD
  22556. ><TD
  22557. >         <TT
  22558. CLASS="constant"
  22559. ><B
  22560. >TRUE</B
  22561. ></TT
  22562. > if $a is equal to $b, and they are of the same
  22563.         type. (introduced in PHP 4)
  22564.        </TD
  22565. ></TR
  22566. ><TR
  22567. ><TD
  22568. >$a != $b</TD
  22569. ><TD
  22570. >Not equal</TD
  22571. ><TD
  22572. ><TT
  22573. CLASS="constant"
  22574. ><B
  22575. >TRUE</B
  22576. ></TT
  22577. > if $a is not equal to $b.</TD
  22578. ></TR
  22579. ><TR
  22580. ><TD
  22581. >$a <> $b</TD
  22582. ><TD
  22583. >Not equal</TD
  22584. ><TD
  22585. ><TT
  22586. CLASS="constant"
  22587. ><B
  22588. >TRUE</B
  22589. ></TT
  22590. > if $a is not equal to $b.</TD
  22591. ></TR
  22592. ><TR
  22593. ><TD
  22594. >$a !== $b</TD
  22595. ><TD
  22596. >Not identical</TD
  22597. ><TD
  22598. >         <TT
  22599. CLASS="constant"
  22600. ><B
  22601. >TRUE</B
  22602. ></TT
  22603. > if $a is not equal to $b, or they are not of the same
  22604.         type. (introduced in PHP 4)
  22605.        </TD
  22606. ></TR
  22607. ><TR
  22608. ><TD
  22609. >$a < $b</TD
  22610. ><TD
  22611. >Less than</TD
  22612. ><TD
  22613. ><TT
  22614. CLASS="constant"
  22615. ><B
  22616. >TRUE</B
  22617. ></TT
  22618. > if $a is strictly less than $b.</TD
  22619. ></TR
  22620. ><TR
  22621. ><TD
  22622. >$a > $b</TD
  22623. ><TD
  22624. >Greater than</TD
  22625. ><TD
  22626. ><TT
  22627. CLASS="constant"
  22628. ><B
  22629. >TRUE</B
  22630. ></TT
  22631. > if $a is strictly greater than $b.</TD
  22632. ></TR
  22633. ><TR
  22634. ><TD
  22635. >$a <= $b</TD
  22636. ><TD
  22637. >Less than or equal to </TD
  22638. ><TD
  22639. ><TT
  22640. CLASS="constant"
  22641. ><B
  22642. >TRUE</B
  22643. ></TT
  22644. > if $a is less than or equal to $b.</TD
  22645. ></TR
  22646. ><TR
  22647. ><TD
  22648. >$a >= $b</TD
  22649. ><TD
  22650. >Greater than or equal to </TD
  22651. ><TD
  22652. ><TT
  22653. CLASS="constant"
  22654. ><B
  22655. >TRUE</B
  22656. ></TT
  22657. > if $a is greater than or equal to $b.</TD
  22658. ></TR
  22659. ></TBODY
  22660. ></TABLE
  22661. ></DIV
  22662. ><P
  22663. >     If you compare an integer with a string, the string is
  22664.     <A
  22665. HREF="#language.types.string.conversion"
  22666. >converted to a number</A
  22667. >.
  22668.     If you compare two numerical strings, they are compared as integers. These
  22669.     rules also apply to the
  22670.     <A
  22671. HREF="#control-structures.switch"
  22672. >switch</A
  22673. > statement.
  22674.     <DIV
  22675. CLASS="informalexample"
  22676. ><P
  22677. ></P
  22678. ><A
  22679. NAME="AEN4124"
  22680. ></A
  22681. ><TABLE
  22682. BORDER="0"
  22683. BGCOLOR="#E0E0E0"
  22684. CELLPADDING="5"
  22685. ><TR
  22686. ><TD
  22687. ><PRE
  22688. CLASS="php"
  22689. ><?php
  22690. var_dump(0 == "a"); // 0 == 0 -> true
  22691. var_dump("1" == "01"); // 1 == 1 -> true
  22692.  
  22693. switch ("a") {
  22694. case 0:
  22695.     echo "0";
  22696.     break;
  22697. case "a": // never reached because "a" is already matched with 0
  22698.     echo "a";
  22699.     break;
  22700. }
  22701. ?></PRE
  22702. ></TD
  22703. ></TR
  22704. ></TABLE
  22705. ><P
  22706. ></P
  22707. ></DIV
  22708.    </P
  22709. ><P
  22710. >     Another conditional operator is the "?:" (or ternary) operator.
  22711.     <DIV
  22712. CLASS="informalexample"
  22713. ><P
  22714. ></P
  22715. ><A
  22716. NAME="AEN4127"
  22717. ></A
  22718. ><TABLE
  22719. BORDER="0"
  22720. BGCOLOR="#E0E0E0"
  22721. CELLPADDING="5"
  22722. ><TR
  22723. ><TD
  22724. ><PRE
  22725. CLASS="php"
  22726. ><?php
  22727. // Example usage for: Ternary Operator
  22728. $action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
  22729.  
  22730. // The above is identical to this if/else statement
  22731. if (empty($_POST['action'])) {
  22732.     $action = 'default';
  22733. } else {
  22734.     $action = $_POST['action'];
  22735. }
  22736. ?></PRE
  22737. ></TD
  22738. ></TR
  22739. ></TABLE
  22740. ><P
  22741. ></P
  22742. ></DIV
  22743.     The expression <VAR
  22744. CLASS="literal"
  22745. >(expr1) ? (expr2) : (expr3)</VAR
  22746. >
  22747.     evaluates to <VAR
  22748. CLASS="replaceable"
  22749. >expr2</VAR
  22750. > if
  22751.     <VAR
  22752. CLASS="replaceable"
  22753. >expr1</VAR
  22754. > evaluates to <TT
  22755. CLASS="constant"
  22756. ><B
  22757. >TRUE</B
  22758. ></TT
  22759. >, and
  22760.     <VAR
  22761. CLASS="replaceable"
  22762. >expr3</VAR
  22763. > if
  22764.     <VAR
  22765. CLASS="replaceable"
  22766. >expr1</VAR
  22767. > evaluates to <TT
  22768. CLASS="constant"
  22769. ><B
  22770. >FALSE</B
  22771. ></TT
  22772. >.
  22773.    </P
  22774. ><P
  22775. >     See also <A
  22776. HREF="#function.strcasecmp"
  22777. ><B
  22778. CLASS="function"
  22779. >strcasecmp()</B
  22780. ></A
  22781. >,
  22782.     <A
  22783. HREF="#function.strcmp"
  22784. ><B
  22785. CLASS="function"
  22786. >strcmp()</B
  22787. ></A
  22788. >,
  22789.     <A
  22790. HREF="#language.operators.array"
  22791. >Array operators</A
  22792. >,
  22793.     and the manual section on
  22794.     <A
  22795. HREF="#language.types"
  22796. >Types</A
  22797. >.
  22798.    </P
  22799. ></DIV
  22800. ><DIV
  22801. CLASS="sect1"
  22802. ><HR><H2
  22803. CLASS="sect1"
  22804. ><A
  22805. NAME="language.operators.errorcontrol"
  22806. >Error Control Operators</A
  22807. ></H2
  22808. ><P
  22809. >     PHP supports one error control operator: the at sign (@). When
  22810.     prepended to an expression in PHP, any error messages that might
  22811.     be generated by that expression will be ignored.
  22812.    </P
  22813. ><P
  22814. >     If the <A
  22815. HREF="#ini.track-errors"
  22816. ><VAR
  22817. CLASS="option"
  22818. >track_errors</VAR
  22819. ></A
  22820. >
  22821.     feature is enabled, any error message generated by the expression
  22822.     will be saved in the variable
  22823.     <A
  22824. HREF="#reserved.variables.phperrormsg"
  22825. >$php_errormsg</A
  22826. >.
  22827.     This variable will be overwritten on each error, so check early if you
  22828.     want to use it.
  22829.    </P
  22830. ><P
  22831. >     <DIV
  22832. CLASS="informalexample"
  22833. ><P
  22834. ></P
  22835. ><A
  22836. NAME="AEN4149"
  22837. ></A
  22838. ><TABLE
  22839. BORDER="0"
  22840. BGCOLOR="#E0E0E0"
  22841. CELLPADDING="5"
  22842. ><TR
  22843. ><TD
  22844. ><PRE
  22845. CLASS="php"
  22846. ><?php
  22847. /* Intentional file error */
  22848. $my_file = @file ('non_existent_file') or
  22849.     die ("Failed opening file: error was '$php_errormsg'");
  22850.  
  22851. // this works for any expression, not just functions:
  22852. $value = @$cache[$key]; 
  22853. // will not issue a notice if the index $key doesn't exist.
  22854.  
  22855. ?></PRE
  22856. ></TD
  22857. ></TR
  22858. ></TABLE
  22859. ><P
  22860. ></P
  22861. ></DIV
  22862. >
  22863.    </P
  22864. ><DIV
  22865. CLASS="note"
  22866. ><BLOCKQUOTE
  22867. CLASS="note"
  22868. ><P
  22869. ><B
  22870. >Note: </B
  22871. >
  22872.      The @-operator works only on
  22873.      <A
  22874. HREF="#language.expressions"
  22875. >expressions</A
  22876. >. A simple rule
  22877.      of thumb is: if you can take the value of something, you can prepend
  22878.      the @ operator to it. For instance, you can prepend it to variables,
  22879.      function and <A
  22880. HREF="#function.include"
  22881. ><B
  22882. CLASS="function"
  22883. >include()</B
  22884. ></A
  22885. > calls, constants, and
  22886.      so forth. You cannot prepend it to function or class definitions,
  22887.      or conditional structures such as <VAR
  22888. CLASS="literal"
  22889. >if</VAR
  22890. > and
  22891.      <VAR
  22892. CLASS="literal"
  22893. >foreach</VAR
  22894. >, and so forth.
  22895.     </P
  22896. ></BLOCKQUOTE
  22897. ></DIV
  22898. ><P
  22899. >     See also <A
  22900. HREF="#function.error-reporting"
  22901. ><B
  22902. CLASS="function"
  22903. >error_reporting()</B
  22904. ></A
  22905. > and the manual section for
  22906.     <A
  22907. HREF="#ref.errorfunc"
  22908. >Error Handling and Logging functions</A
  22909. >.
  22910.    </P
  22911. ><DIV
  22912. CLASS="note"
  22913. ><BLOCKQUOTE
  22914. CLASS="note"
  22915. ><P
  22916. ><B
  22917. >Note: </B
  22918. >
  22919.      The "@" error-control operator prefix will not disable messages
  22920.      that are the result of parse errors.
  22921.     </P
  22922. ></BLOCKQUOTE
  22923. ></DIV
  22924. ><DIV
  22925. CLASS="warning"
  22926. ><P
  22927. ></P
  22928. ><TABLE
  22929. CLASS="warning"
  22930. BORDER="1"
  22931. WIDTH="100%"
  22932. ><TR
  22933. ><TD
  22934. ALIGN="CENTER"
  22935. ><B
  22936. >Warning</B
  22937. ></TD
  22938. ></TR
  22939. ><TR
  22940. ><TD
  22941. ALIGN="LEFT"
  22942. ><P
  22943. >      Currently the "@" error-control operator prefix will even disable
  22944.      error reporting for critical errors that will terminate script
  22945.      execution. Among other things, this means that if you use "@" to
  22946.      suppress errors from a certain function and either it isn't
  22947.      available or has been mistyped, the script will die right there
  22948.      with no indication as to why.
  22949.     </P
  22950. ></TD
  22951. ></TR
  22952. ></TABLE
  22953. ></DIV
  22954. ></DIV
  22955. ><DIV
  22956. CLASS="sect1"
  22957. ><HR><H2
  22958. CLASS="sect1"
  22959. ><A
  22960. NAME="language.operators.execution"
  22961. >Execution Operators</A
  22962. ></H2
  22963. ><P
  22964. >     PHP supports one execution operator: backticks (``). Note that
  22965.     these are not single-quotes! PHP will attempt to execute the
  22966.     contents of the backticks as a shell command; the output will be
  22967.     returned (i.e., it won't simply be dumped to output; it can be
  22968.     assigned to a variable).  Use of the backtick operator is identical 
  22969.     to <A
  22970. HREF="#function.shell-exec"
  22971. ><B
  22972. CLASS="function"
  22973. >shell_exec()</B
  22974. ></A
  22975. >. 
  22976.     <DIV
  22977. CLASS="informalexample"
  22978. ><P
  22979. ></P
  22980. ><A
  22981. NAME="AEN4168"
  22982. ></A
  22983. ><TABLE
  22984. BORDER="0"
  22985. BGCOLOR="#E0E0E0"
  22986. CELLPADDING="5"
  22987. ><TR
  22988. ><TD
  22989. ><PRE
  22990. CLASS="php"
  22991. ><?php
  22992. $output = `ls -al`;
  22993. echo "<pre>$output</pre>";
  22994. ?></PRE
  22995. ></TD
  22996. ></TR
  22997. ></TABLE
  22998. ><P
  22999. ></P
  23000. ></DIV
  23001. >
  23002.    </P
  23003. ><DIV
  23004. CLASS="note"
  23005. ><BLOCKQUOTE
  23006. CLASS="note"
  23007. ><P
  23008. ><B
  23009. >Note: </B
  23010. >
  23011.      The backtick operator is disabled when <A
  23012. HREF="#ini.safe-mode"
  23013. >safe mode</A
  23014. > is enabled
  23015.      or <A
  23016. HREF="#function.shell-exec"
  23017. ><B
  23018. CLASS="function"
  23019. >shell_exec()</B
  23020. ></A
  23021. > is disabled.
  23022.     </P
  23023. ></BLOCKQUOTE
  23024. ></DIV
  23025. ><P
  23026. >     See also the manual section on <A
  23027. HREF="#ref.exec"
  23028. >Program
  23029.     Execution functions</A
  23030. >, <A
  23031. HREF="#function.popen"
  23032. ><B
  23033. CLASS="function"
  23034. >popen()</B
  23035. ></A
  23036. >
  23037.     <A
  23038. HREF="#function.proc-open"
  23039. ><B
  23040. CLASS="function"
  23041. >proc_open()</B
  23042. ></A
  23043. >, and
  23044.     <A
  23045. HREF="#features.commandline"
  23046. >Using PHP from the
  23047.     commandline</A
  23048. >.
  23049.    </P
  23050. ></DIV
  23051. ><DIV
  23052. CLASS="sect1"
  23053. ><HR><H2
  23054. CLASS="sect1"
  23055. ><A
  23056. NAME="language.operators.increment"
  23057. >Incrementing/Decrementing Operators</A
  23058. ></H2
  23059. ><P
  23060. >     PHP supports C-style pre- and post-increment and decrement
  23061.     operators.
  23062.    </P
  23063. ><DIV
  23064. CLASS="table"
  23065. ><A
  23066. NAME="AEN4182"
  23067. ></A
  23068. ><P
  23069. ><B
  23070. >Table 15-5. Increment/decrement Operators</B
  23071. ></P
  23072. ><TABLE
  23073. BORDER="1"
  23074. CLASS="CALSTABLE"
  23075. ><COL><COL><COL><THEAD
  23076. ><TR
  23077. ><TH
  23078. >Example</TH
  23079. ><TH
  23080. >Name</TH
  23081. ><TH
  23082. >Effect</TH
  23083. ></TR
  23084. ></THEAD
  23085. ><TBODY
  23086. ><TR
  23087. ><TD
  23088. >++$a</TD
  23089. ><TD
  23090. >Pre-increment</TD
  23091. ><TD
  23092. >Increments $a by one, then returns $a.</TD
  23093. ></TR
  23094. ><TR
  23095. ><TD
  23096. >$a++</TD
  23097. ><TD
  23098. >Post-increment</TD
  23099. ><TD
  23100. >Returns $a, then increments $a by one.</TD
  23101. ></TR
  23102. ><TR
  23103. ><TD
  23104. >--$a</TD
  23105. ><TD
  23106. >Pre-decrement</TD
  23107. ><TD
  23108. >Decrements $a by one, then returns $a.</TD
  23109. ></TR
  23110. ><TR
  23111. ><TD
  23112. >$a--</TD
  23113. ><TD
  23114. >Post-decrement</TD
  23115. ><TD
  23116. >Returns $a, then decrements $a by one.</TD
  23117. ></TR
  23118. ></TBODY
  23119. ></TABLE
  23120. ></DIV
  23121. ><P
  23122. >     Here's a simple example script:
  23123.     <DIV
  23124. CLASS="informalexample"
  23125. ><P
  23126. ></P
  23127. ><A
  23128. NAME="AEN4208"
  23129. ></A
  23130. ><TABLE
  23131. BORDER="0"
  23132. BGCOLOR="#E0E0E0"
  23133. CELLPADDING="5"
  23134. ><TR
  23135. ><TD
  23136. ><PRE
  23137. CLASS="php"
  23138. ><?php
  23139. echo "<h3>Postincrement</h3>";
  23140. $a = 5;
  23141. echo "Should be 5: " . $a++ . "<br />\n";
  23142. echo "Should be 6: " . $a . "<br />\n";
  23143.  
  23144. echo "<h3>Preincrement</h3>";
  23145. $a = 5;
  23146. echo "Should be 6: " . ++$a . "<br />\n";
  23147. echo "Should be 6: " . $a . "<br />\n";
  23148.  
  23149. echo "<h3>Postdecrement</h3>";
  23150. $a = 5;
  23151. echo "Should be 5: " . $a-- . "<br />\n";
  23152. echo "Should be 4: " . $a . "<br />\n";
  23153.  
  23154. echo "<h3>Predecrement</h3>";
  23155. $a = 5;
  23156. echo "Should be 4: " . --$a . "<br />\n";
  23157. echo "Should be 4: " . $a . "<br />\n";
  23158. ?></PRE
  23159. ></TD
  23160. ></TR
  23161. ></TABLE
  23162. ><P
  23163. ></P
  23164. ></DIV
  23165. >
  23166.    </P
  23167. ><P
  23168. >     PHP follows Perl's convention when dealing with arithmetic operations
  23169.     on character variables and not C's.  For example, in Perl 'Z'+1 turns
  23170.     into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ).
  23171.     Note that character variables can be incremented but not decremented.
  23172.     <TABLE
  23173. WIDTH="100%"
  23174. BORDER="0"
  23175. CELLPADDING="0"
  23176. CELLSPACING="0"
  23177. CLASS="EXAMPLE"
  23178. ><TR
  23179. ><TD
  23180. ><DIV
  23181. CLASS="example"
  23182. ><A
  23183. NAME="AEN4211"
  23184. ></A
  23185. ><P
  23186. ><B
  23187. >Example 15-2. Arithmetic Operations on Character Variables</B
  23188. ></P
  23189. ><TABLE
  23190. BORDER="0"
  23191. BGCOLOR="#E0E0E0"
  23192. CELLPADDING="5"
  23193. ><TR
  23194. ><TD
  23195. ><PRE
  23196. CLASS="php"
  23197. ><?php
  23198. $i = 'W';
  23199. for($n=0; $n<6; $n++)
  23200.   echo ++$i . "\n";
  23201.  
  23202. /*
  23203.   Produces the output similar to the following:
  23204.  
  23205. X
  23206. Y
  23207. Z
  23208. AA
  23209. AB
  23210. AC
  23211.  
  23212. */
  23213. ?></PRE
  23214. ></TD
  23215. ></TR
  23216. ></TABLE
  23217. ></DIV
  23218. ></TD
  23219. ></TR
  23220. ></TABLE
  23221. >
  23222.    </P
  23223. ><P
  23224. >     Incrementing or decrementing booleans has no effect.
  23225.    </P
  23226. ></DIV
  23227. ><DIV
  23228. CLASS="sect1"
  23229. ><HR><H2
  23230. CLASS="sect1"
  23231. ><A
  23232. NAME="language.operators.logical"
  23233. >Logical Operators</A
  23234. ></H2
  23235. ><DIV
  23236. CLASS="table"
  23237. ><A
  23238. NAME="AEN4217"
  23239. ></A
  23240. ><P
  23241. ><B
  23242. >Table 15-6. Logical Operators</B
  23243. ></P
  23244. ><TABLE
  23245. BORDER="1"
  23246. CLASS="CALSTABLE"
  23247. ><COL><COL><COL><THEAD
  23248. ><TR
  23249. ><TH
  23250. >Example</TH
  23251. ><TH
  23252. >Name</TH
  23253. ><TH
  23254. >Result</TH
  23255. ></TR
  23256. ></THEAD
  23257. ><TBODY
  23258. ><TR
  23259. ><TD
  23260. >$a and $b</TD
  23261. ><TD
  23262. >And</TD
  23263. ><TD
  23264. ><TT
  23265. CLASS="constant"
  23266. ><B
  23267. >TRUE</B
  23268. ></TT
  23269. > if both $a and $b are <TT
  23270. CLASS="constant"
  23271. ><B
  23272. >TRUE</B
  23273. ></TT
  23274. >.</TD
  23275. ></TR
  23276. ><TR
  23277. ><TD
  23278. >$a or $b</TD
  23279. ><TD
  23280. >Or</TD
  23281. ><TD
  23282. ><TT
  23283. CLASS="constant"
  23284. ><B
  23285. >TRUE</B
  23286. ></TT
  23287. > if either $a or $b is <TT
  23288. CLASS="constant"
  23289. ><B
  23290. >TRUE</B
  23291. ></TT
  23292. >.</TD
  23293. ></TR
  23294. ><TR
  23295. ><TD
  23296. >$a xor $b</TD
  23297. ><TD
  23298. >Xor</TD
  23299. ><TD
  23300. ><TT
  23301. CLASS="constant"
  23302. ><B
  23303. >TRUE</B
  23304. ></TT
  23305. > if either $a or $b is <TT
  23306. CLASS="constant"
  23307. ><B
  23308. >TRUE</B
  23309. ></TT
  23310. >, but not both.</TD
  23311. ></TR
  23312. ><TR
  23313. ><TD
  23314. >! $a</TD
  23315. ><TD
  23316. >Not</TD
  23317. ><TD
  23318. ><TT
  23319. CLASS="constant"
  23320. ><B
  23321. >TRUE</B
  23322. ></TT
  23323. > if $a is not <TT
  23324. CLASS="constant"
  23325. ><B
  23326. >TRUE</B
  23327. ></TT
  23328. >.</TD
  23329. ></TR
  23330. ><TR
  23331. ><TD
  23332. >$a && $b</TD
  23333. ><TD
  23334. >And</TD
  23335. ><TD
  23336. ><TT
  23337. CLASS="constant"
  23338. ><B
  23339. >TRUE</B
  23340. ></TT
  23341. > if both $a and $b are <TT
  23342. CLASS="constant"
  23343. ><B
  23344. >TRUE</B
  23345. ></TT
  23346. >.</TD
  23347. ></TR
  23348. ><TR
  23349. ><TD
  23350. >$a || $b</TD
  23351. ><TD
  23352. >Or</TD
  23353. ><TD
  23354. ><TT
  23355. CLASS="constant"
  23356. ><B
  23357. >TRUE</B
  23358. ></TT
  23359. > if either $a or $b is <TT
  23360. CLASS="constant"
  23361. ><B
  23362. >TRUE</B
  23363. ></TT
  23364. >.</TD
  23365. ></TR
  23366. ></TBODY
  23367. ></TABLE
  23368. ></DIV
  23369. ><P
  23370. >     The reason for the two different variations of "and" and "or"
  23371.     operators is that they operate at different precedences. (See
  23372.     <A
  23373. HREF="#language.operators.precedence"
  23374. >Operator
  23375.     Precedence</A
  23376. >.)
  23377.    </P
  23378. ></DIV
  23379. ><DIV
  23380. CLASS="sect1"
  23381. ><HR><H2
  23382. CLASS="sect1"
  23383. ><A
  23384. NAME="language.operators.string"
  23385. >String Operators</A
  23386. ></H2
  23387. ><P
  23388. >     There are two <A
  23389. HREF="#language.types.string"
  23390. ><B
  23391. CLASS="type"
  23392. >string</B
  23393. ></A
  23394. > operators. The first is the
  23395.     concatenation operator ('.'), which returns the concatenation of its
  23396.     right and left arguments. The second is the concatenating assignment
  23397.     operator ('.='), which appends the argument on the right side to
  23398.     the argument on the left side. Please read <A
  23399. HREF="#language.operators.assignment"
  23400. >Assignment
  23401.     Operators</A
  23402. > for more information.
  23403.    </P
  23404. ><P
  23405. >     <DIV
  23406. CLASS="informalexample"
  23407. ><P
  23408. ></P
  23409. ><A
  23410. NAME="AEN4270"
  23411. ></A
  23412. ><TABLE
  23413. BORDER="0"
  23414. BGCOLOR="#E0E0E0"
  23415. CELLPADDING="5"
  23416. ><TR
  23417. ><TD
  23418. ><PRE
  23419. CLASS="php"
  23420. ><?php
  23421. $a = "Hello ";
  23422. $b = $a . "World!"; // now $b contains "Hello World!"
  23423.  
  23424. $a = "Hello ";
  23425. $a .= "World!";     // now $a contains "Hello World!"
  23426. ?></PRE
  23427. ></TD
  23428. ></TR
  23429. ></TABLE
  23430. ><P
  23431. ></P
  23432. ></DIV
  23433. >
  23434.    </P
  23435. ><P
  23436. >     See also the manual sections on the 
  23437.     <A
  23438. HREF="#language.types.string"
  23439. >String type</A
  23440. > and 
  23441.     <A
  23442. HREF="#ref.strings"
  23443. >String functions</A
  23444. >.
  23445.    </P
  23446. ></DIV
  23447. ><DIV
  23448. CLASS="sect1"
  23449. ><HR><H2
  23450. CLASS="sect1"
  23451. ><A
  23452. NAME="language.operators.array"
  23453. >Array Operators</A
  23454. ></H2
  23455. ><DIV
  23456. CLASS="table"
  23457. ><A
  23458. NAME="AEN4277"
  23459. ></A
  23460. ><P
  23461. ><B
  23462. >Table 15-7. Array Operators</B
  23463. ></P
  23464. ><TABLE
  23465. BORDER="1"
  23466. CLASS="CALSTABLE"
  23467. ><COL><COL><COL><THEAD
  23468. ><TR
  23469. ><TH
  23470. >Example</TH
  23471. ><TH
  23472. >Name</TH
  23473. ><TH
  23474. >Result</TH
  23475. ></TR
  23476. ></THEAD
  23477. ><TBODY
  23478. ><TR
  23479. ><TD
  23480. >$a + $b</TD
  23481. ><TD
  23482. >Union</TD
  23483. ><TD
  23484. >Union of $a and $b.</TD
  23485. ></TR
  23486. ><TR
  23487. ><TD
  23488. >$a == $b</TD
  23489. ><TD
  23490. >Equality</TD
  23491. ><TD
  23492. ><TT
  23493. CLASS="constant"
  23494. ><B
  23495. >TRUE</B
  23496. ></TT
  23497. > if $a and $b have the same key/value pairs.</TD
  23498. ></TR
  23499. ><TR
  23500. ><TD
  23501. >$a === $b</TD
  23502. ><TD
  23503. >Identity</TD
  23504. ><TD
  23505. ><TT
  23506. CLASS="constant"
  23507. ><B
  23508. >TRUE</B
  23509. ></TT
  23510. > if $a and $b have the same key/value pairs in the same
  23511.         order and of the same types.</TD
  23512. ></TR
  23513. ><TR
  23514. ><TD
  23515. >$a != $b</TD
  23516. ><TD
  23517. >Inequality</TD
  23518. ><TD
  23519. ><TT
  23520. CLASS="constant"
  23521. ><B
  23522. >TRUE</B
  23523. ></TT
  23524. > if $a is not equal to $b.</TD
  23525. ></TR
  23526. ><TR
  23527. ><TD
  23528. >$a <> $b</TD
  23529. ><TD
  23530. >Inequality</TD
  23531. ><TD
  23532. ><TT
  23533. CLASS="constant"
  23534. ><B
  23535. >TRUE</B
  23536. ></TT
  23537. > if $a is not equal to $b.</TD
  23538. ></TR
  23539. ><TR
  23540. ><TD
  23541. >$a !== $b</TD
  23542. ><TD
  23543. >Non-identity</TD
  23544. ><TD
  23545. ><TT
  23546. CLASS="constant"
  23547. ><B
  23548. >TRUE</B
  23549. ></TT
  23550. > if $a is not identical to $b.</TD
  23551. ></TR
  23552. ></TBODY
  23553. ></TABLE
  23554. ></DIV
  23555. ><P
  23556. >     The <VAR
  23557. CLASS="literal"
  23558. >+</VAR
  23559. > operator
  23560.     appends the right handed array to the left handed, whereas 
  23561.     duplicated keys are NOT overwritten.
  23562.    </P
  23563. ><P
  23564. >     <DIV
  23565. CLASS="informalexample"
  23566. ><P
  23567. ></P
  23568. ><A
  23569. NAME="AEN4318"
  23570. ></A
  23571. ><TABLE
  23572. BORDER="0"
  23573. BGCOLOR="#E0E0E0"
  23574. CELLPADDING="5"
  23575. ><TR
  23576. ><TD
  23577. ><PRE
  23578. CLASS="php"
  23579. ><?php
  23580. $a = array("a" => "apple", "b" => "banana");
  23581. $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
  23582.  
  23583. $c = $a + $b; // Union of $a and $b
  23584. echo "Union of \$a and \$b: \n";
  23585. var_dump($c);
  23586.  
  23587. $c = $b + $a; // Union of $b and $a
  23588. echo "Union of \$b and \$a: \n";
  23589. var_dump($c);
  23590. ?></PRE
  23591. ></TD
  23592. ></TR
  23593. ></TABLE
  23594. ><P
  23595. ></P
  23596. ></DIV
  23597. >
  23598.     When executed, this script will print the following:
  23599.     <TABLE
  23600. BORDER="0"
  23601. BGCOLOR="#E0E0E0"
  23602. CELLPADDING="5"
  23603. ><TR
  23604. ><TD
  23605. ><PRE
  23606. CLASS="php"
  23607. >Union of $a and $b:
  23608. array(3) {
  23609.   ["a"]=>
  23610.   string(5) "apple"
  23611.   ["b"]=>
  23612.   string(6) "banana"
  23613.   ["c"]=>
  23614.   string(6) "cherry"
  23615. }
  23616. Union of $b and $a:
  23617. array(3) {
  23618.   ["a"]=>
  23619.   string(4) "pear"
  23620.   ["b"]=>
  23621.   string(10) "strawberry"
  23622.   ["c"]=>
  23623.   string(6) "cherry"
  23624. }</PRE
  23625. ></TD
  23626. ></TR
  23627. ></TABLE
  23628. >
  23629.    </P
  23630. ><P
  23631. >     Elements of arrays are equal for the comparison if they have the
  23632.     same key and value.
  23633.    </P
  23634. ><P
  23635. >     <TABLE
  23636. WIDTH="100%"
  23637. BORDER="0"
  23638. CELLPADDING="0"
  23639. CELLSPACING="0"
  23640. CLASS="EXAMPLE"
  23641. ><TR
  23642. ><TD
  23643. ><DIV
  23644. CLASS="example"
  23645. ><A
  23646. NAME="AEN4323"
  23647. ></A
  23648. ><P
  23649. ><B
  23650. >Example 15-3. Comparing arrays</B
  23651. ></P
  23652. ><TABLE
  23653. BORDER="0"
  23654. BGCOLOR="#E0E0E0"
  23655. CELLPADDING="5"
  23656. ><TR
  23657. ><TD
  23658. ><PRE
  23659. CLASS="php"
  23660. ><?php
  23661. $a = array("apple", "banana");
  23662. $b = array(1 => "banana", "0" => "apple");
  23663.  
  23664. var_dump($a == $b); // bool(true)
  23665. var_dump($a === $b); // bool(false)
  23666. ?></PRE
  23667. ></TD
  23668. ></TR
  23669. ></TABLE
  23670. ></DIV
  23671. ></TD
  23672. ></TR
  23673. ></TABLE
  23674. >
  23675.    </P
  23676. ><P
  23677. >     See also the manual sections on the 
  23678.     <A
  23679. HREF="#language.types.array"
  23680. >Array type</A
  23681. > and 
  23682.     <A
  23683. HREF="#ref.array"
  23684. >Array functions</A
  23685. >.
  23686.    </P
  23687. ></DIV
  23688. ><DIV
  23689. CLASS="sect1"
  23690. ><HR><H2
  23691. CLASS="sect1"
  23692. ><A
  23693. NAME="language.operators.type"
  23694. >Type Operators</A
  23695. ></H2
  23696. ><P
  23697. >     PHP has a single type operator: <VAR
  23698. CLASS="literal"
  23699. >instanceof</VAR
  23700. >. 
  23701.     <VAR
  23702. CLASS="literal"
  23703. >instanceof</VAR
  23704. > is used to determine whether a given
  23705.     object is of a specified <A
  23706. HREF="#language.oop"
  23707. >object class</A
  23708. >.
  23709.    </P
  23710. ><P
  23711. >     The <VAR
  23712. CLASS="literal"
  23713. >instanceof</VAR
  23714. > operator was introduced in PHP 5.
  23715.     Before this time <A
  23716. HREF="#function.is-a"
  23717. ><B
  23718. CLASS="function"
  23719. >is_a()</B
  23720. ></A
  23721. > was used but
  23722.     <A
  23723. HREF="#function.is-a"
  23724. ><B
  23725. CLASS="function"
  23726. >is_a()</B
  23727. ></A
  23728. > has since been deprecated in favor of
  23729.     <VAR
  23730. CLASS="literal"
  23731. >instanceof</VAR
  23732. >. 
  23733.    </P
  23734. ><DIV
  23735. CLASS="informalexample"
  23736. ><P
  23737. ></P
  23738. ><A
  23739. NAME="AEN4340"
  23740. ></A
  23741. ><TABLE
  23742. BORDER="0"
  23743. BGCOLOR="#E0E0E0"
  23744. CELLPADDING="5"
  23745. ><TR
  23746. ><TD
  23747. ><PRE
  23748. CLASS="programlisting"
  23749. ><?php
  23750. class A { }
  23751. class B { }
  23752.  
  23753. $thing = new A;
  23754.  
  23755. if ($thing instanceof A) {
  23756.     echo 'A';
  23757. }
  23758. if ($thing instanceof B) {
  23759.     echo 'B';
  23760. }
  23761. ?></PRE
  23762. ></TD
  23763. ></TR
  23764. ></TABLE
  23765. ><P
  23766. >      As <VAR
  23767. CLASS="varname"
  23768. >$thing</VAR
  23769. > is an <A
  23770. HREF="#language.types.object"
  23771. ><B
  23772. CLASS="type"
  23773. >object</B
  23774. ></A
  23775. > of type A, but
  23776.      not B, only the block dependent on the A type will be executed:
  23777.     </P
  23778. ><TABLE
  23779. BORDER="0"
  23780. BGCOLOR="#E0E0E0"
  23781. CELLPADDING="5"
  23782. ><TR
  23783. ><TD
  23784. ><PRE
  23785. CLASS="screen"
  23786. >A</PRE
  23787. ></TD
  23788. ></TR
  23789. ></TABLE
  23790. ><P
  23791. ></P
  23792. ></DIV
  23793. ><P
  23794. >     See also <A
  23795. HREF="#function.get-class"
  23796. ><B
  23797. CLASS="function"
  23798. >get_class()</B
  23799. ></A
  23800. > and 
  23801.     <A
  23802. HREF="#function.is-a"
  23803. ><B
  23804. CLASS="function"
  23805. >is_a()</B
  23806. ></A
  23807. >.
  23808.    </P
  23809. ></DIV
  23810. ></DIV
  23811. ><DIV
  23812. CLASS="chapter"
  23813. ><HR><H1
  23814. ><A
  23815. NAME="language.control-structures"
  23816. >Chapter 16. Control Structures</A
  23817. ></H1
  23818. ><P
  23819. >    Any PHP script is built out of a series of statements. A statement
  23820.    can be an assignment, a function call, a loop, a conditional
  23821.    statement of even a statement that does nothing (an empty
  23822.    statement). Statements usually end with a semicolon. In addition,
  23823.    statements can be grouped into a statement-group by encapsulating a
  23824.    group of statements with curly braces. A statement-group is a
  23825.    statement by itself as well. The various statement types are
  23826.    described in this chapter.
  23827.   </P
  23828. ><DIV
  23829. CLASS="sect1"
  23830. ><HR><H2
  23831. CLASS="sect1"
  23832. ><A
  23833. NAME="control-structures.if"
  23834. ><VAR
  23835. CLASS="literal"
  23836. >if</VAR
  23837. ></A
  23838. ></H2
  23839. ><P
  23840. >     The <VAR
  23841. CLASS="literal"
  23842. >if</VAR
  23843. > construct is one of the most important
  23844.     features of many languages, PHP included.  It allows for
  23845.     conditional execution of code fragments.  PHP features an
  23846.     <VAR
  23847. CLASS="literal"
  23848. >if</VAR
  23849. > structure that is similar to that of C:
  23850.     <DIV
  23851. CLASS="informalexample"
  23852. ><P
  23853. ></P
  23854. ><A
  23855. NAME="AEN4358"
  23856. ></A
  23857. ><TABLE
  23858. BORDER="0"
  23859. BGCOLOR="#E0E0E0"
  23860. CELLPADDING="5"
  23861. ><TR
  23862. ><TD
  23863. ><PRE
  23864. CLASS="programlisting"
  23865. ><?php
  23866. if (expression)
  23867.     statement
  23868. ?></PRE
  23869. ></TD
  23870. ></TR
  23871. ></TABLE
  23872. ><P
  23873. ></P
  23874. ></DIV
  23875. >
  23876.    </P
  23877. ><P
  23878. >     As described in <A
  23879. HREF="#language.expressions"
  23880. >the section about
  23881.     expressions</A
  23882. >, <VAR
  23883. CLASS="replaceable"
  23884. >expr</VAR
  23885. > is evaluated to its
  23886.     Boolean value.  If <VAR
  23887. CLASS="replaceable"
  23888. >expr</VAR
  23889. > evaluates to <TT
  23890. CLASS="constant"
  23891. ><B
  23892. >TRUE</B
  23893. ></TT
  23894. >,
  23895.    PHP will execute <VAR
  23896. CLASS="replaceable"
  23897. >statement</VAR
  23898. >, and if it evaluates
  23899.     to <TT
  23900. CLASS="constant"
  23901. ><B
  23902. >FALSE</B
  23903. ></TT
  23904. > - it'll ignore it. More information about what values evaluate
  23905.     to <TT
  23906. CLASS="constant"
  23907. ><B
  23908. >FALSE</B
  23909. ></TT
  23910. > can be found in the <A
  23911. HREF="#language.types.boolean.casting"
  23912. >'Converting to boolean'</A
  23913. >
  23914.     section.
  23915.    </P
  23916. ><P
  23917. >     The following example would display <SAMP
  23918. CLASS="computeroutput"
  23919. >a is bigger
  23920.     than b</SAMP
  23921. > if <VAR
  23922. CLASS="varname"
  23923. >$a</VAR
  23924. > is bigger
  23925.     than <VAR
  23926. CLASS="varname"
  23927. >$b</VAR
  23928. >:
  23929.     <DIV
  23930. CLASS="informalexample"
  23931. ><P
  23932. ></P
  23933. ><A
  23934. NAME="AEN4373"
  23935. ></A
  23936. ><TABLE
  23937. BORDER="0"
  23938. BGCOLOR="#E0E0E0"
  23939. CELLPADDING="5"
  23940. ><TR
  23941. ><TD
  23942. ><PRE
  23943. CLASS="php"
  23944. ><?php
  23945. if ($a > $b)
  23946.     echo "a is bigger than b";
  23947. ?></PRE
  23948. ></TD
  23949. ></TR
  23950. ></TABLE
  23951. ><P
  23952. ></P
  23953. ></DIV
  23954. >
  23955.    </P
  23956. ><P
  23957. >     Often you'd want to have more than one statement to be executed
  23958.     conditionally.  Of course, there's no need to wrap each statement
  23959.     with an <VAR
  23960. CLASS="literal"
  23961. >if</VAR
  23962. > clause.  Instead, you can group
  23963.     several statements into a statement group.  For example, this code
  23964.     would display <SAMP
  23965. CLASS="computeroutput"
  23966. >a is bigger than b</SAMP
  23967. >
  23968.     if <VAR
  23969. CLASS="varname"
  23970. >$a</VAR
  23971. > is bigger than
  23972.     <VAR
  23973. CLASS="varname"
  23974. >$b</VAR
  23975. >, and would then assign the value of
  23976.     <VAR
  23977. CLASS="varname"
  23978. >$a</VAR
  23979. > into <VAR
  23980. CLASS="varname"
  23981. >$b</VAR
  23982. >:
  23983.     <DIV
  23984. CLASS="informalexample"
  23985. ><P
  23986. ></P
  23987. ><A
  23988. NAME="AEN4382"
  23989. ></A
  23990. ><TABLE
  23991. BORDER="0"
  23992. BGCOLOR="#E0E0E0"
  23993. CELLPADDING="5"
  23994. ><TR
  23995. ><TD
  23996. ><PRE
  23997. CLASS="php"
  23998. ><?php
  23999. if ($a > $b) {
  24000.     echo "a is bigger than b";
  24001.     $b = $a;
  24002. }
  24003. ?></PRE
  24004. ></TD
  24005. ></TR
  24006. ></TABLE
  24007. ><P
  24008. ></P
  24009. ></DIV
  24010. >
  24011.    </P
  24012. ><P
  24013. >     <VAR
  24014. CLASS="literal"
  24015. >If</VAR
  24016. > statements can be nested indefinitely within other
  24017.     <VAR
  24018. CLASS="literal"
  24019. >if</VAR
  24020. > statements, which provides you with complete
  24021.     flexibility for conditional execution of the various parts of your
  24022.     program.
  24023.    </P
  24024. ></DIV
  24025. ><DIV
  24026. CLASS="sect1"
  24027. ><HR><H2
  24028. CLASS="sect1"
  24029. ><A
  24030. NAME="control-structures.else"
  24031. ><VAR
  24032. CLASS="literal"
  24033. >else</VAR
  24034. ></A
  24035. ></H2
  24036. ><P
  24037. >     Often you'd want to execute a statement if a certain condition is
  24038.     met, and a different statement if the condition is not met.  This
  24039.     is what <VAR
  24040. CLASS="literal"
  24041. >else</VAR
  24042. > is for.  <VAR
  24043. CLASS="literal"
  24044. >else</VAR
  24045. >
  24046.     extends an <VAR
  24047. CLASS="literal"
  24048. >if</VAR
  24049. > statement to execute a statement
  24050.     in case the expression in the <VAR
  24051. CLASS="literal"
  24052. >if</VAR
  24053. > statement
  24054.     evaluates to <TT
  24055. CLASS="constant"
  24056. ><B
  24057. >FALSE</B
  24058. ></TT
  24059. >.  For example, the following
  24060.     code would display <SAMP
  24061. CLASS="computeroutput"
  24062. >a is bigger than
  24063.     b</SAMP
  24064. > if <VAR
  24065. CLASS="varname"
  24066. >$a</VAR
  24067. > is bigger than
  24068.     <VAR
  24069. CLASS="varname"
  24070. >$b</VAR
  24071. >, and <SAMP
  24072. CLASS="computeroutput"
  24073. >a is NOT bigger
  24074.     than b</SAMP
  24075. > otherwise:
  24076.     <DIV
  24077. CLASS="informalexample"
  24078. ><P
  24079. ></P
  24080. ><A
  24081. NAME="AEN4400"
  24082. ></A
  24083. ><TABLE
  24084. BORDER="0"
  24085. BGCOLOR="#E0E0E0"
  24086. CELLPADDING="5"
  24087. ><TR
  24088. ><TD
  24089. ><PRE
  24090. CLASS="php"
  24091. ><?php
  24092. if ($a > $b) {
  24093.     echo "a is bigger than b";
  24094. } else {
  24095.     echo "a is NOT bigger than b";
  24096. }
  24097. ?></PRE
  24098. ></TD
  24099. ></TR
  24100. ></TABLE
  24101. ><P
  24102. ></P
  24103. ></DIV
  24104. >
  24105.  
  24106.     The <VAR
  24107. CLASS="literal"
  24108. >else</VAR
  24109. > statement is only executed if the
  24110.     <VAR
  24111. CLASS="literal"
  24112. >if</VAR
  24113. > expression evaluated to
  24114.     <TT
  24115. CLASS="constant"
  24116. ><B
  24117. >FALSE</B
  24118. ></TT
  24119. >, and if there were any
  24120.     <VAR
  24121. CLASS="literal"
  24122. >elseif</VAR
  24123. > expressions - only if they evaluated to
  24124.     <TT
  24125. CLASS="constant"
  24126. ><B
  24127. >FALSE</B
  24128. ></TT
  24129. > as well (see <A
  24130. HREF="#control-structures.elseif"
  24131. >elseif</A
  24132. >).
  24133.  
  24134.    </P
  24135. ></DIV
  24136. ><DIV
  24137. CLASS="sect1"
  24138. ><HR><H2
  24139. CLASS="sect1"
  24140. ><A
  24141. NAME="control-structures.elseif"
  24142. ><VAR
  24143. CLASS="literal"
  24144. >elseif</VAR
  24145. ></A
  24146. ></H2
  24147. ><P
  24148. >     <VAR
  24149. CLASS="literal"
  24150. >elseif</VAR
  24151. >, as its name suggests, is a combination
  24152.     of <VAR
  24153. CLASS="literal"
  24154. >if</VAR
  24155. > and <VAR
  24156. CLASS="literal"
  24157. >else</VAR
  24158. >.  Like
  24159.     <VAR
  24160. CLASS="literal"
  24161. >else</VAR
  24162. >, it extends an <VAR
  24163. CLASS="literal"
  24164. >if</VAR
  24165. >
  24166.     statement to execute a different statement in case the original
  24167.     <VAR
  24168. CLASS="literal"
  24169. >if</VAR
  24170. > expression evaluates to
  24171.     <TT
  24172. CLASS="constant"
  24173. ><B
  24174. >FALSE</B
  24175. ></TT
  24176. >.  However, unlike
  24177.     <VAR
  24178. CLASS="literal"
  24179. >else</VAR
  24180. >, it will execute that alternative
  24181.     expression only if the <VAR
  24182. CLASS="literal"
  24183. >elseif</VAR
  24184. > conditional
  24185.     expression evaluates to <TT
  24186. CLASS="constant"
  24187. ><B
  24188. >TRUE</B
  24189. ></TT
  24190. >.  For example, the
  24191.     following code would display <SAMP
  24192. CLASS="computeroutput"
  24193. >a is bigger than
  24194.     b</SAMP
  24195. >, <SAMP
  24196. CLASS="computeroutput"
  24197. >a equal to b</SAMP
  24198. >
  24199.     or <SAMP
  24200. CLASS="computeroutput"
  24201. >a is smaller than b</SAMP
  24202. >:
  24203.     <DIV
  24204. CLASS="informalexample"
  24205. ><P
  24206. ></P
  24207. ><A
  24208. NAME="AEN4425"
  24209. ></A
  24210. ><TABLE
  24211. BORDER="0"
  24212. BGCOLOR="#E0E0E0"
  24213. CELLPADDING="5"
  24214. ><TR
  24215. ><TD
  24216. ><PRE
  24217. CLASS="php"
  24218. ><?php
  24219. if ($a > $b) {
  24220.     echo "a is bigger than b";
  24221. } elseif ($a == $b) {
  24222.     echo "a is equal to b";
  24223. } else {
  24224.     echo "a is smaller than b";
  24225. }
  24226. ?></PRE
  24227. ></TD
  24228. ></TR
  24229. ></TABLE
  24230. ><P
  24231. ></P
  24232. ></DIV
  24233. >
  24234.    </P
  24235. ><P
  24236. >     There may be several <VAR
  24237. CLASS="literal"
  24238. >elseif</VAR
  24239. >s within the same
  24240.     <VAR
  24241. CLASS="literal"
  24242. >if</VAR
  24243. > statement.  The first
  24244.     <VAR
  24245. CLASS="literal"
  24246. >elseif</VAR
  24247. > expression (if any) that evaluates to
  24248.     <TT
  24249. CLASS="constant"
  24250. ><B
  24251. >TRUE</B
  24252. ></TT
  24253. > would be executed.  In PHP, you can also
  24254.     write 'else if' (in two words) and the behavior would be identical
  24255.     to the one of 'elseif' (in a single word).  The syntactic meaning
  24256.     is slightly different (if you're familiar with C, this is the same
  24257.     behavior) but the bottom line is that both would result in exactly
  24258.     the same behavior.
  24259.    </P
  24260. ><P
  24261. >     The <VAR
  24262. CLASS="literal"
  24263. >elseif</VAR
  24264. > statement is only executed if the
  24265.     preceding <VAR
  24266. CLASS="literal"
  24267. >if</VAR
  24268. > expression and any preceding
  24269.     <VAR
  24270. CLASS="literal"
  24271. >elseif</VAR
  24272. > expressions evaluated to
  24273.     <TT
  24274. CLASS="constant"
  24275. ><B
  24276. >FALSE</B
  24277. ></TT
  24278. >, and the current
  24279.     <VAR
  24280. CLASS="literal"
  24281. >elseif</VAR
  24282. > expression evaluated to
  24283.     <TT
  24284. CLASS="constant"
  24285. ><B
  24286. >TRUE</B
  24287. ></TT
  24288. >.
  24289.    </P
  24290. ></DIV
  24291. ><DIV
  24292. CLASS="sect1"
  24293. ><HR><H2
  24294. CLASS="sect1"
  24295. ><A
  24296. NAME="control-structures.alternative-syntax"
  24297. >Alternative syntax for control structures</A
  24298. ></H2
  24299. ><P
  24300. >     PHP offers an alternative syntax for some of its control
  24301.     structures; namely, <VAR
  24302. CLASS="literal"
  24303. >if</VAR
  24304. >,
  24305.     <VAR
  24306. CLASS="literal"
  24307. >while</VAR
  24308. >, <VAR
  24309. CLASS="literal"
  24310. >for</VAR
  24311. >,
  24312.     <VAR
  24313. CLASS="literal"
  24314. >foreach</VAR
  24315. >, and <VAR
  24316. CLASS="literal"
  24317. >switch</VAR
  24318. >.
  24319.     In each case, the basic form of the alternate syntax is to change
  24320.     the opening brace to a colon (:) and the closing brace to
  24321.     <VAR
  24322. CLASS="literal"
  24323. >endif;</VAR
  24324. >, <VAR
  24325. CLASS="literal"
  24326. >endwhile;</VAR
  24327. >,
  24328.     <VAR
  24329. CLASS="literal"
  24330. >endfor;</VAR
  24331. >, <VAR
  24332. CLASS="literal"
  24333. >endforeach;</VAR
  24334. >, or
  24335.     <VAR
  24336. CLASS="literal"
  24337. >endswitch;</VAR
  24338. >, respectively.
  24339.     <DIV
  24340. CLASS="informalexample"
  24341. ><P
  24342. ></P
  24343. ><A
  24344. NAME="AEN4452"
  24345. ></A
  24346. ><TABLE
  24347. BORDER="0"
  24348. BGCOLOR="#E0E0E0"
  24349. CELLPADDING="5"
  24350. ><TR
  24351. ><TD
  24352. ><PRE
  24353. CLASS="php"
  24354. ><?php if ($a == 5): ?>
  24355. A is equal to 5
  24356. <?php endif; ?></PRE
  24357. ></TD
  24358. ></TR
  24359. ></TABLE
  24360. ><P
  24361. ></P
  24362. ></DIV
  24363. >
  24364.    </P
  24365. ><P
  24366. >     In the above example, the HTML block "A is equal to 5" is nested within an
  24367.     <VAR
  24368. CLASS="literal"
  24369. >if</VAR
  24370. > statement written in the alternative syntax.  The
  24371.     HTML block would be displayed only if <VAR
  24372. CLASS="varname"
  24373. >$a</VAR
  24374. > is equal to 5.
  24375.    </P
  24376. ><P
  24377. >     The alternative syntax applies to <VAR
  24378. CLASS="literal"
  24379. >else</VAR
  24380. > and
  24381.     <VAR
  24382. CLASS="literal"
  24383. >elseif</VAR
  24384. > as well.  The following is an
  24385.     <VAR
  24386. CLASS="literal"
  24387. >if</VAR
  24388. > structure with <VAR
  24389. CLASS="literal"
  24390. >elseif</VAR
  24391. > and
  24392.     <VAR
  24393. CLASS="literal"
  24394. >else</VAR
  24395. > in the alternative format:
  24396.     <DIV
  24397. CLASS="informalexample"
  24398. ><P
  24399. ></P
  24400. ><A
  24401. NAME="AEN4463"
  24402. ></A
  24403. ><TABLE
  24404. BORDER="0"
  24405. BGCOLOR="#E0E0E0"
  24406. CELLPADDING="5"
  24407. ><TR
  24408. ><TD
  24409. ><PRE
  24410. CLASS="php"
  24411. ><?php
  24412. if ($a == 5):
  24413.     echo "a equals 5";
  24414.     echo "...";
  24415. elseif ($a == 6):
  24416.     echo "a equals 6";
  24417.     echo "!!!";
  24418. else:
  24419.     echo "a is neither 5 nor 6";
  24420. endif;
  24421. ?></PRE
  24422. ></TD
  24423. ></TR
  24424. ></TABLE
  24425. ><P
  24426. ></P
  24427. ></DIV
  24428. >
  24429.    </P
  24430. ><P
  24431. >     See also <A
  24432. HREF="#control-structures.while"
  24433. >while</A
  24434. >,
  24435.     <A
  24436. HREF="#control-structures.for"
  24437. >for</A
  24438. >, and <A
  24439. HREF="#control-structures.if"
  24440. >if</A
  24441. > for further examples.
  24442.    </P
  24443. ></DIV
  24444. ><DIV
  24445. CLASS="sect1"
  24446. ><HR><H2
  24447. CLASS="sect1"
  24448. ><A
  24449. NAME="control-structures.while"
  24450. ><VAR
  24451. CLASS="literal"
  24452. >while</VAR
  24453. ></A
  24454. ></H2
  24455. ><P
  24456. >     <VAR
  24457. CLASS="literal"
  24458. >while</VAR
  24459. > loops are the simplest type of loop in
  24460.     PHP.  They behave just like their C counterparts.  The basic form
  24461.     of a <VAR
  24462. CLASS="literal"
  24463. >while</VAR
  24464. > statement is:
  24465.     <DIV
  24466. CLASS="informalexample"
  24467. ><P
  24468. ></P
  24469. ><A
  24470. NAME="AEN4475"
  24471. ></A
  24472. ><TABLE
  24473. BORDER="0"
  24474. BGCOLOR="#E0E0E0"
  24475. CELLPADDING="5"
  24476. ><TR
  24477. ><TD
  24478. ><PRE
  24479. CLASS="programlisting"
  24480. >while (expr)
  24481.     statement</PRE
  24482. ></TD
  24483. ></TR
  24484. ></TABLE
  24485. ><P
  24486. ></P
  24487. ></DIV
  24488. >
  24489.    </P
  24490. ><P
  24491. >     The meaning of a <VAR
  24492. CLASS="literal"
  24493. >while</VAR
  24494. > statement is simple.  It
  24495.     tells PHP to execute the nested statement(s) repeatedly, as long
  24496.     as the <VAR
  24497. CLASS="literal"
  24498. >while</VAR
  24499. > expression evaluates to
  24500.     <TT
  24501. CLASS="constant"
  24502. ><B
  24503. >TRUE</B
  24504. ></TT
  24505. >.  The value of the expression is checked
  24506.     each time at the beginning of the loop, so even if this value
  24507.     changes during the execution of the nested statement(s), execution
  24508.     will not stop until the end of the iteration (each time PHP runs
  24509.     the statements in the loop is one iteration).  Sometimes, if the
  24510.     <VAR
  24511. CLASS="literal"
  24512. >while</VAR
  24513. > expression evaluates to
  24514.     <TT
  24515. CLASS="constant"
  24516. ><B
  24517. >FALSE</B
  24518. ></TT
  24519. > from the very beginning, the nested
  24520.     statement(s) won't even be run once.
  24521.    </P
  24522. ><P
  24523. >     Like with the <VAR
  24524. CLASS="literal"
  24525. >if</VAR
  24526. > statement, you can group
  24527.     multiple statements within the same <VAR
  24528. CLASS="literal"
  24529. >while</VAR
  24530. > loop
  24531.     by surrounding a group of statements with curly braces, or by
  24532.     using the alternate syntax:
  24533.     <DIV
  24534. CLASS="informalexample"
  24535. ><P
  24536. ></P
  24537. ><A
  24538. NAME="AEN4486"
  24539. ></A
  24540. ><TABLE
  24541. BORDER="0"
  24542. BGCOLOR="#E0E0E0"
  24543. CELLPADDING="5"
  24544. ><TR
  24545. ><TD
  24546. ><PRE
  24547. CLASS="programlisting"
  24548. >while (expr):
  24549.     statement
  24550.     ...
  24551. endwhile;</PRE
  24552. ></TD
  24553. ></TR
  24554. ></TABLE
  24555. ><P
  24556. ></P
  24557. ></DIV
  24558. >
  24559.    </P
  24560. ><P
  24561. >     The following examples are identical, and both print numbers from
  24562.     1 to 10:
  24563.     <DIV
  24564. CLASS="informalexample"
  24565. ><P
  24566. ></P
  24567. ><A
  24568. NAME="AEN4489"
  24569. ></A
  24570. ><TABLE
  24571. BORDER="0"
  24572. BGCOLOR="#E0E0E0"
  24573. CELLPADDING="5"
  24574. ><TR
  24575. ><TD
  24576. ><PRE
  24577. CLASS="php"
  24578. ><?php
  24579. /* example 1 */
  24580.  
  24581. $i = 1;
  24582. while ($i <= 10) {
  24583.     echo $i++;  /* the printed value would be
  24584.                     $i before the increment
  24585.                     (post-increment) */
  24586. }
  24587.  
  24588. /* example 2 */
  24589.  
  24590. $i = 1;
  24591. while ($i <= 10):
  24592.     echo $i;
  24593.     $i++;
  24594. endwhile;
  24595. ?></PRE
  24596. ></TD
  24597. ></TR
  24598. ></TABLE
  24599. ><P
  24600. ></P
  24601. ></DIV
  24602. >
  24603.    </P
  24604. ></DIV
  24605. ><DIV
  24606. CLASS="sect1"
  24607. ><HR><H2
  24608. CLASS="sect1"
  24609. ><A
  24610. NAME="control-structures.do.while"
  24611. ><VAR
  24612. CLASS="literal"
  24613. >do-while</VAR
  24614. ></A
  24615. ></H2
  24616. ><P
  24617. >     <VAR
  24618. CLASS="literal"
  24619. >do-while</VAR
  24620. > loops are very similar to
  24621.     <VAR
  24622. CLASS="literal"
  24623. >while</VAR
  24624. > loops, except the truth expression is
  24625.     checked at the end of each iteration instead of in the beginning.
  24626.     The main difference from regular <VAR
  24627. CLASS="literal"
  24628. >while</VAR
  24629. > loops is
  24630.     that the first iteration of a <VAR
  24631. CLASS="literal"
  24632. >do-while</VAR
  24633. > loop is
  24634.     guaranteed to run (the truth expression is only checked at the end
  24635.     of the iteration), whereas it's may not necessarily run with a
  24636.     regular <VAR
  24637. CLASS="literal"
  24638. >while</VAR
  24639. > loop (the truth expression is
  24640.     checked at the beginning of each iteration, if it evaluates to
  24641.     <TT
  24642. CLASS="constant"
  24643. ><B
  24644. >FALSE</B
  24645. ></TT
  24646. > right from the beginning, the loop
  24647.     execution would end immediately).
  24648.    </P
  24649. ><P
  24650. >     There is just one syntax for <VAR
  24651. CLASS="literal"
  24652. >do-while</VAR
  24653. > loops:
  24654.  
  24655.     <DIV
  24656. CLASS="informalexample"
  24657. ><P
  24658. ></P
  24659. ><A
  24660. NAME="AEN4503"
  24661. ></A
  24662. ><TABLE
  24663. BORDER="0"
  24664. BGCOLOR="#E0E0E0"
  24665. CELLPADDING="5"
  24666. ><TR
  24667. ><TD
  24668. ><PRE
  24669. CLASS="php"
  24670. ><?php
  24671. $i = 0;
  24672. do {
  24673.     echo $i;
  24674. } while ($i > 0);
  24675. ?></PRE
  24676. ></TD
  24677. ></TR
  24678. ></TABLE
  24679. ><P
  24680. ></P
  24681. ></DIV
  24682. >
  24683.    </P
  24684. ><P
  24685. >      The above loop would run one time exactly, since after the first
  24686.      iteration, when truth expression is checked, it evaluates to
  24687.      <TT
  24688. CLASS="constant"
  24689. ><B
  24690. >FALSE</B
  24691. ></TT
  24692. > ($i is not bigger than 0) and the loop
  24693.      execution ends.
  24694.    </P
  24695. ><P
  24696. >     Advanced C users may be familiar with a different usage of the
  24697.     <VAR
  24698. CLASS="literal"
  24699. >do-while</VAR
  24700. > loop, to allow stopping execution in
  24701.     the middle of code blocks, by encapsulating them with
  24702.     <VAR
  24703. CLASS="literal"
  24704. >do-while</VAR
  24705. > (0), and using the <A
  24706. HREF="#control-structures.break"
  24707. ><VAR
  24708. CLASS="literal"
  24709. >break</VAR
  24710. ></A
  24711. >
  24712.     statement.  The following code fragment demonstrates this:
  24713.     <DIV
  24714. CLASS="informalexample"
  24715. ><P
  24716. ></P
  24717. ><A
  24718. NAME="AEN4512"
  24719. ></A
  24720. ><TABLE
  24721. BORDER="0"
  24722. BGCOLOR="#E0E0E0"
  24723. CELLPADDING="5"
  24724. ><TR
  24725. ><TD
  24726. ><PRE
  24727. CLASS="php"
  24728. ><?php
  24729. do {
  24730.     if ($i < 5) {
  24731.         echo "i is not big enough";
  24732.         break;
  24733.     }
  24734.     $i *= $factor;
  24735.     if ($i < $minimum_limit) {
  24736.         break;
  24737.     }
  24738.    echo "i is ok";
  24739.  
  24740.     /* process i */
  24741.  
  24742. } while (0);
  24743. ?></PRE
  24744. ></TD
  24745. ></TR
  24746. ></TABLE
  24747. ><P
  24748. ></P
  24749. ></DIV
  24750. >
  24751.    </P
  24752. ><P
  24753. >     Don't worry if you don't understand this right away or at all.
  24754.     You can code scripts and even powerful scripts without using this
  24755.     'feature'.
  24756.    </P
  24757. ></DIV
  24758. ><DIV
  24759. CLASS="sect1"
  24760. ><HR><H2
  24761. CLASS="sect1"
  24762. ><A
  24763. NAME="control-structures.for"
  24764. ><VAR
  24765. CLASS="literal"
  24766. >for</VAR
  24767. ></A
  24768. ></H2
  24769. ><P
  24770. >     <VAR
  24771. CLASS="literal"
  24772. >for</VAR
  24773. > loops are the most complex loops in PHP.
  24774.     They behave like their C counterparts.  The syntax of a
  24775.     <VAR
  24776. CLASS="literal"
  24777. >for</VAR
  24778. > loop is:
  24779.     <DIV
  24780. CLASS="informalexample"
  24781. ><P
  24782. ></P
  24783. ><A
  24784. NAME="AEN4521"
  24785. ></A
  24786. ><TABLE
  24787. BORDER="0"
  24788. BGCOLOR="#E0E0E0"
  24789. CELLPADDING="5"
  24790. ><TR
  24791. ><TD
  24792. ><PRE
  24793. CLASS="programlisting"
  24794. >for (expr1; expr2; expr3)
  24795.     statement</PRE
  24796. ></TD
  24797. ></TR
  24798. ></TABLE
  24799. ><P
  24800. ></P
  24801. ></DIV
  24802. >
  24803.    </P
  24804. ><P
  24805. >     The first expression (<VAR
  24806. CLASS="varname"
  24807. >expr1</VAR
  24808. >) is
  24809.     evaluated (executed) once unconditionally at the beginning of the
  24810.     loop.
  24811.    </P
  24812. ><P
  24813. >     In the beginning of each iteration,
  24814.     <VAR
  24815. CLASS="varname"
  24816. >expr2</VAR
  24817. > is evaluated.  If it evaluates to
  24818.     <TT
  24819. CLASS="constant"
  24820. ><B
  24821. >TRUE</B
  24822. ></TT
  24823. >, the loop continues and the nested
  24824.     statement(s) are executed.  If it evaluates to
  24825.     <TT
  24826. CLASS="constant"
  24827. ><B
  24828. >FALSE</B
  24829. ></TT
  24830. >, the execution of the loop ends.
  24831.    </P
  24832. ><P
  24833. >     At the end of each iteration, <VAR
  24834. CLASS="varname"
  24835. >expr3</VAR
  24836. > is
  24837.     evaluated (executed).
  24838.    </P
  24839. ><P
  24840. >     Each of the expressions can be empty.
  24841.     <VAR
  24842. CLASS="varname"
  24843. >expr2</VAR
  24844. > being empty means the loop should
  24845.     be run indefinitely (PHP implicitly considers it as
  24846.     <TT
  24847. CLASS="constant"
  24848. ><B
  24849. >TRUE</B
  24850. ></TT
  24851. >, like C).  This may not be as useless as
  24852.     you might think, since often you'd want to end the loop using a
  24853.     conditional <A
  24854. HREF="#control-structures.break"
  24855. ><VAR
  24856. CLASS="literal"
  24857. >break</VAR
  24858. ></A
  24859. >
  24860.     statement instead of using the <VAR
  24861. CLASS="literal"
  24862. >for</VAR
  24863. > truth
  24864.     expression.
  24865.    </P
  24866. ><P
  24867. >     Consider the following examples.  All of them display numbers from
  24868.     1 to 10:
  24869.     <DIV
  24870. CLASS="informalexample"
  24871. ><P
  24872. ></P
  24873. ><A
  24874. NAME="AEN4538"
  24875. ></A
  24876. ><TABLE
  24877. BORDER="0"
  24878. BGCOLOR="#E0E0E0"
  24879. CELLPADDING="5"
  24880. ><TR
  24881. ><TD
  24882. ><PRE
  24883. CLASS="php"
  24884. ><?php
  24885. /* example 1 */
  24886.  
  24887. for ($i = 1; $i <= 10; $i++) {
  24888.     echo $i;
  24889. }
  24890.  
  24891. /* example 2 */
  24892.  
  24893. for ($i = 1; ; $i++) {
  24894.     if ($i > 10) {
  24895.         break;
  24896.     }
  24897.     echo $i;
  24898. }
  24899.  
  24900. /* example 3 */
  24901.  
  24902. $i = 1;
  24903. for (; ; ) {
  24904.     if ($i > 10) {
  24905.         break;
  24906.     }
  24907.     echo $i;
  24908.     $i++;
  24909. }
  24910.  
  24911. /* example 4 */
  24912.  
  24913. for ($i = 1; $i <= 10; print $i, $i++);
  24914. ?></PRE
  24915. ></TD
  24916. ></TR
  24917. ></TABLE
  24918. ><P
  24919. ></P
  24920. ></DIV
  24921. >
  24922.    </P
  24923. ><P
  24924. >     Of course, the first example appears to be the nicest one (or
  24925.     perhaps the fourth), but you may find that being able to use empty
  24926.     expressions in <VAR
  24927. CLASS="literal"
  24928. >for</VAR
  24929. > loops comes in handy in many
  24930.     occasions.
  24931.    </P
  24932. ><P
  24933. >     PHP also supports the alternate "colon syntax" for
  24934.     <VAR
  24935. CLASS="literal"
  24936. >for</VAR
  24937. > loops.
  24938.     <DIV
  24939. CLASS="informalexample"
  24940. ><P
  24941. ></P
  24942. ><A
  24943. NAME="AEN4544"
  24944. ></A
  24945. ><TABLE
  24946. BORDER="0"
  24947. BGCOLOR="#E0E0E0"
  24948. CELLPADDING="5"
  24949. ><TR
  24950. ><TD
  24951. ><PRE
  24952. CLASS="programlisting"
  24953. >for (expr1; expr2; expr3):
  24954.     statement
  24955.     ...
  24956. endfor;</PRE
  24957. ></TD
  24958. ></TR
  24959. ></TABLE
  24960. ><P
  24961. ></P
  24962. ></DIV
  24963. >
  24964.    </P
  24965. ></DIV
  24966. ><DIV
  24967. CLASS="sect1"
  24968. ><HR><H2
  24969. CLASS="sect1"
  24970. ><A
  24971. NAME="control-structures.foreach"
  24972. ><VAR
  24973. CLASS="literal"
  24974. >foreach</VAR
  24975. ></A
  24976. ></H2
  24977. ><P
  24978. >     PHP 4 introduced a <VAR
  24979. CLASS="literal"
  24980. >foreach</VAR
  24981. > construct, much
  24982.     like Perl and some other languages. This simply gives an easy way to
  24983.     iterate over arrays. <VAR
  24984. CLASS="literal"
  24985. >foreach</VAR
  24986. > works only on arrays, and
  24987.     will issue an error when you try to use it on a variable with a different
  24988.     data type or an uninitialized variable. There are two syntaxes; the
  24989.     second is a minor but useful extension of the first:
  24990.     <DIV
  24991. CLASS="informalexample"
  24992. ><P
  24993. ></P
  24994. ><A
  24995. NAME="AEN4552"
  24996. ></A
  24997. ><TABLE
  24998. BORDER="0"
  24999. BGCOLOR="#E0E0E0"
  25000. CELLPADDING="5"
  25001. ><TR
  25002. ><TD
  25003. ><PRE
  25004. CLASS="programlisting"
  25005. >foreach (array_expression as $value)
  25006.     statement
  25007. foreach (array_expression as $key => $value)
  25008.     statement</PRE
  25009. ></TD
  25010. ></TR
  25011. ></TABLE
  25012. ><P
  25013. ></P
  25014. ></DIV
  25015. >
  25016.    </P
  25017. ><P
  25018. >     The first form loops over the array given by
  25019.     <VAR
  25020. CLASS="literal"
  25021. >array_expression</VAR
  25022. >. On each loop, the value of
  25023.     the current element is assigned to <VAR
  25024. CLASS="literal"
  25025. >$value</VAR
  25026. > and
  25027.     the internal array pointer is advanced by one (so on the next
  25028.     loop, you'll be looking at the next element).
  25029.    </P
  25030. ><P
  25031. >     The second form does the same thing, except that the current
  25032.     element's key will be assigned to the variable
  25033.     <VAR
  25034. CLASS="literal"
  25035. >$key</VAR
  25036. > on each loop.
  25037.    </P
  25038. ><P
  25039. >     <DIV
  25040. CLASS="note"
  25041. ><BLOCKQUOTE
  25042. CLASS="note"
  25043. ><P
  25044. ><B
  25045. >Note: </B
  25046. >
  25047.       When <VAR
  25048. CLASS="literal"
  25049. >foreach</VAR
  25050. > first starts executing, the
  25051.       internal array pointer is automatically reset to the first element
  25052.       of the array. This means that you do not need to call
  25053.       <A
  25054. HREF="#function.reset"
  25055. ><B
  25056. CLASS="function"
  25057. >reset()</B
  25058. ></A
  25059. > before a <VAR
  25060. CLASS="literal"
  25061. >foreach</VAR
  25062. >
  25063.       loop.
  25064.      </P
  25065. ></BLOCKQUOTE
  25066. ></DIV
  25067. >
  25068.    </P
  25069. ><P
  25070. >     <DIV
  25071. CLASS="note"
  25072. ><BLOCKQUOTE
  25073. CLASS="note"
  25074. ><P
  25075. ><B
  25076. >Note: </B
  25077. >
  25078.       Also note that <VAR
  25079. CLASS="literal"
  25080. >foreach</VAR
  25081. > operates on a copy of
  25082.       the specified array and not the array itself. Therefore, the
  25083.       array pointer is not modified as with the
  25084.       <A
  25085. HREF="#function.each"
  25086. ><B
  25087. CLASS="function"
  25088. >each()</B
  25089. ></A
  25090. > construct, and changes to the array
  25091.       element returned are not reflected in the original array.
  25092.       However, the internal pointer of the original array
  25093.       <SPAN
  25094. CLASS="emphasis"
  25095. ><I
  25096. CLASS="emphasis"
  25097. >is</I
  25098. ></SPAN
  25099. > advanced with the processing of the
  25100.       array.  Assuming the foreach loop runs to completion, the
  25101.       array's internal pointer will be at the end of the array.
  25102.      </P
  25103. ><P
  25104. >       As of PHP 5, you can easily modify array's elements by preceding
  25105.       <VAR
  25106. CLASS="literal"
  25107. >$value</VAR
  25108. > with &. This will assign
  25109.       <A
  25110. HREF="#language.references"
  25111. >reference</A
  25112. > instead of copying
  25113.       the value.
  25114.       <DIV
  25115. CLASS="informalexample"
  25116. ><P
  25117. ></P
  25118. ><A
  25119. NAME="AEN4574"
  25120. ></A
  25121. ><TABLE
  25122. BORDER="0"
  25123. BGCOLOR="#E0E0E0"
  25124. CELLPADDING="5"
  25125. ><TR
  25126. ><TD
  25127. ><PRE
  25128. CLASS="php"
  25129. ><?php
  25130. $arr = array(1, 2, 3, 4);
  25131. foreach ($arr as &$value) {
  25132.     $value = $value * 2;
  25133. }
  25134. // $arr is now array(2, 4, 6, 8)
  25135. ?></PRE
  25136. ></TD
  25137. ></TR
  25138. ></TABLE
  25139. ><P
  25140. ></P
  25141. ></DIV
  25142. >
  25143.       This is possible only if iterated array can be referenced (i.e. is
  25144.       variable).
  25145.      </P
  25146. ></BLOCKQUOTE
  25147. ></DIV
  25148. >
  25149.    </P
  25150. ><P
  25151. >     <DIV
  25152. CLASS="note"
  25153. ><BLOCKQUOTE
  25154. CLASS="note"
  25155. ><P
  25156. ><B
  25157. >Note: </B
  25158. >
  25159.       <VAR
  25160. CLASS="literal"
  25161. >foreach</VAR
  25162. > does not support the ability to
  25163.       suppress error messages using '@'.
  25164.      </P
  25165. ></BLOCKQUOTE
  25166. ></DIV
  25167. >
  25168.    </P
  25169. ><P
  25170. >     You may have noticed that the following are functionally
  25171.     identical:
  25172.     <DIV
  25173. CLASS="informalexample"
  25174. ><P
  25175. ></P
  25176. ><A
  25177. NAME="AEN4581"
  25178. ></A
  25179. ><TABLE
  25180. BORDER="0"
  25181. BGCOLOR="#E0E0E0"
  25182. CELLPADDING="5"
  25183. ><TR
  25184. ><TD
  25185. ><PRE
  25186. CLASS="php"
  25187. ><?php
  25188. $arr = array("one", "two", "three");
  25189. reset($arr);
  25190. while (list(, $value) = each($arr)) {
  25191.     echo "Value: $value<br />\n";
  25192. }
  25193.  
  25194. foreach ($arr as $value) {
  25195.     echo "Value: $value<br />\n";
  25196. }
  25197. ?></PRE
  25198. ></TD
  25199. ></TR
  25200. ></TABLE
  25201. ><P
  25202. ></P
  25203. ></DIV
  25204. >
  25205.     The following are also functionally identical:
  25206.     <DIV
  25207. CLASS="informalexample"
  25208. ><P
  25209. ></P
  25210. ><A
  25211. NAME="AEN4583"
  25212. ></A
  25213. ><TABLE
  25214. BORDER="0"
  25215. BGCOLOR="#E0E0E0"
  25216. CELLPADDING="5"
  25217. ><TR
  25218. ><TD
  25219. ><PRE
  25220. CLASS="php"
  25221. ><?php
  25222. $arr = array("one", "two", "three");
  25223. reset($arr);
  25224. while (list($key, $value) = each($arr)) {
  25225.     echo "Key: $key; Value: $value<br />\n";
  25226. }
  25227.  
  25228. foreach ($arr as $key => $value) {
  25229.     echo "Key: $key; Value: $value<br />\n";
  25230. }
  25231. ?></PRE
  25232. ></TD
  25233. ></TR
  25234. ></TABLE
  25235. ><P
  25236. ></P
  25237. ></DIV
  25238. >
  25239.    </P
  25240. ><P
  25241. >     Some more examples to demonstrate usages:
  25242.     <DIV
  25243. CLASS="informalexample"
  25244. ><P
  25245. ></P
  25246. ><A
  25247. NAME="AEN4586"
  25248. ></A
  25249. ><TABLE
  25250. BORDER="0"
  25251. BGCOLOR="#E0E0E0"
  25252. CELLPADDING="5"
  25253. ><TR
  25254. ><TD
  25255. ><PRE
  25256. CLASS="php"
  25257. ><?php
  25258. /* foreach example 1: value only */
  25259.  
  25260. $a = array(1, 2, 3, 17);
  25261.  
  25262. foreach ($a as $v) {
  25263.    echo "Current value of \$a: $v.\n";
  25264. }
  25265.  
  25266. /* foreach example 2: value (with key printed for illustration) */
  25267.  
  25268. $a = array(1, 2, 3, 17);
  25269.  
  25270. $i = 0; /* for illustrative purposes only */
  25271.  
  25272. foreach ($a as $v) {
  25273.     echo "\$a[$i] => $v.\n";
  25274.     $i++;
  25275. }
  25276.  
  25277. /* foreach example 3: key and value */
  25278.  
  25279. $a = array(
  25280.     "one" => 1,
  25281.     "two" => 2,
  25282.     "three" => 3,
  25283.     "seventeen" => 17
  25284. );
  25285.  
  25286. foreach ($a as $k => $v) {
  25287.     echo "\$a[$k] => $v.\n";
  25288. }
  25289.  
  25290. /* foreach example 4: multi-dimensional arrays */
  25291. $a = array();
  25292. $a[0][0] = "a";
  25293. $a[0][1] = "b";
  25294. $a[1][0] = "y";
  25295. $a[1][1] = "z";
  25296.  
  25297. foreach ($a as $v1) {
  25298.     foreach ($v1 as $v2) {
  25299.         echo "$v2\n";
  25300.     }
  25301. }
  25302.  
  25303. /* foreach example 5: dynamic arrays */
  25304.  
  25305. foreach (array(1, 2, 3, 4, 5) as $v) {
  25306.     echo "$v\n";
  25307. }
  25308. ?></PRE
  25309. ></TD
  25310. ></TR
  25311. ></TABLE
  25312. ><P
  25313. ></P
  25314. ></DIV
  25315. >
  25316.    </P
  25317. ></DIV
  25318. ><DIV
  25319. CLASS="sect1"
  25320. ><HR><H2
  25321. CLASS="sect1"
  25322. ><A
  25323. NAME="control-structures.break"
  25324. ><VAR
  25325. CLASS="literal"
  25326. >break</VAR
  25327. ></A
  25328. ></H2
  25329. ><P
  25330. >     <VAR
  25331. CLASS="literal"
  25332. >break</VAR
  25333. > ends execution of the current
  25334.     <VAR
  25335. CLASS="literal"
  25336. >for</VAR
  25337. >, <VAR
  25338. CLASS="literal"
  25339. >foreach</VAR
  25340. >,
  25341.     <VAR
  25342. CLASS="literal"
  25343. >while</VAR
  25344. >, <VAR
  25345. CLASS="literal"
  25346. >do-while</VAR
  25347. > or
  25348.     <VAR
  25349. CLASS="literal"
  25350. >switch</VAR
  25351. > structure.
  25352.    </P
  25353. ><P
  25354. >     <VAR
  25355. CLASS="literal"
  25356. >break</VAR
  25357. > accepts an optional numeric argument
  25358.     which tells it how many nested enclosing structures are to be
  25359.     broken out of.
  25360.    </P
  25361. ><P
  25362. >     <DIV
  25363. CLASS="informalexample"
  25364. ><P
  25365. ></P
  25366. ><A
  25367. NAME="AEN4601"
  25368. ></A
  25369. ><TABLE
  25370. BORDER="0"
  25371. BGCOLOR="#E0E0E0"
  25372. CELLPADDING="5"
  25373. ><TR
  25374. ><TD
  25375. ><PRE
  25376. CLASS="php"
  25377. ><?php
  25378. $arr = array('one', 'two', 'three', 'four', 'stop', 'five');
  25379. while (list(, $val) = each($arr)) {
  25380.     if ($val == 'stop') {
  25381.         break;    /* You could also write 'break 1;' here. */
  25382.     }
  25383.     echo "$val<br />\n";
  25384. }
  25385.  
  25386. /* Using the optional argument. */
  25387.  
  25388. $i = 0;
  25389. while (++$i) {
  25390.     switch ($i) {
  25391.     case 5:
  25392.         echo "At 5<br />\n";
  25393.         break 1;  /* Exit only the switch. */
  25394.     case 10:
  25395.         echo "At 10; quitting<br />\n";
  25396.         break 2;  /* Exit the switch and the while. */
  25397.     default:
  25398.         break;
  25399.     }
  25400. }
  25401. ?></PRE
  25402. ></TD
  25403. ></TR
  25404. ></TABLE
  25405. ><P
  25406. ></P
  25407. ></DIV
  25408. >
  25409.    </P
  25410. ></DIV
  25411. ><DIV
  25412. CLASS="sect1"
  25413. ><HR><H2
  25414. CLASS="sect1"
  25415. ><A
  25416. NAME="control-structures.continue"
  25417. ><VAR
  25418. CLASS="literal"
  25419. >continue</VAR
  25420. ></A
  25421. ></H2
  25422. ><P
  25423. >     <VAR
  25424. CLASS="literal"
  25425. >continue</VAR
  25426. > is used within looping structures to
  25427.     skip the rest of the current loop iteration and continue execution
  25428.     at the beginning of the next iteration.
  25429.    </P
  25430. ><DIV
  25431. CLASS="note"
  25432. ><BLOCKQUOTE
  25433. CLASS="note"
  25434. ><P
  25435. ><B
  25436. >Note: </B
  25437. >
  25438.      Note that in PHP the
  25439.      <A
  25440. HREF="#control-structures.switch"
  25441. >switch</A
  25442. > statement is
  25443.      considered a looping structure for the purposes of
  25444.      <VAR
  25445. CLASS="literal"
  25446. >continue</VAR
  25447. >.
  25448.     </P
  25449. ></BLOCKQUOTE
  25450. ></DIV
  25451. ><P
  25452. >     <VAR
  25453. CLASS="literal"
  25454. >continue</VAR
  25455. > accepts an optional numeric argument
  25456.     which tells it how many levels of enclosing loops it should skip
  25457.     to the end of.
  25458.    </P
  25459. ><P
  25460. >     <DIV
  25461. CLASS="informalexample"
  25462. ><P
  25463. ></P
  25464. ><A
  25465. NAME="AEN4615"
  25466. ></A
  25467. ><TABLE
  25468. BORDER="0"
  25469. BGCOLOR="#E0E0E0"
  25470. CELLPADDING="5"
  25471. ><TR
  25472. ><TD
  25473. ><PRE
  25474. CLASS="php"
  25475. ><?php
  25476. while (list($key, $value) = each($arr)) {
  25477.     if (!($key % 2)) { // skip odd members
  25478.         continue;
  25479.     }
  25480.     do_something_odd($value);
  25481. }
  25482.  
  25483. $i = 0;
  25484. while ($i++ < 5) {
  25485.     echo "Outer<br />\n";
  25486.     while (1) {
  25487.         echo "&nbsp;&nbsp;Middle<br />\n";
  25488.         while (1) {
  25489.             echo "&nbsp;&nbsp;Inner<br />\n";
  25490.             continue 3;
  25491.         }
  25492.         echo "This never gets output.<br />\n";
  25493.     }
  25494.     echo "Neither does this.<br />\n";
  25495. }
  25496. ?></PRE
  25497. ></TD
  25498. ></TR
  25499. ></TABLE
  25500. ><P
  25501. ></P
  25502. ></DIV
  25503. >
  25504.     </P
  25505. ><P
  25506. >      Omitting the semicolon after <VAR
  25507. CLASS="literal"
  25508. >continue</VAR
  25509. > can lead to
  25510.      confusion. Here's an example of what you shouldn't do. 
  25511.     </P
  25512. ><P
  25513. >      <DIV
  25514. CLASS="informalexample"
  25515. ><P
  25516. ></P
  25517. ><A
  25518. NAME="AEN4620"
  25519. ></A
  25520. ><TABLE
  25521. BORDER="0"
  25522. BGCOLOR="#E0E0E0"
  25523. CELLPADDING="5"
  25524. ><TR
  25525. ><TD
  25526. ><PRE
  25527. CLASS="php"
  25528. ><?php
  25529.   for ($i = 0; $i < 5; ++$i) {
  25530.       if ($i == 2)
  25531.           continue
  25532.       print "$i\n";
  25533.   }
  25534. ?></PRE
  25535. ></TD
  25536. ></TR
  25537. ></TABLE
  25538. ><P
  25539. >        One can expect the result to be :
  25540.       </P
  25541. ><TABLE
  25542. BORDER="0"
  25543. BGCOLOR="#E0E0E0"
  25544. CELLPADDING="5"
  25545. ><TR
  25546. ><TD
  25547. ><PRE
  25548. CLASS="screen"
  25549. >0
  25550. 1
  25551. 3
  25552. 4</PRE
  25553. ></TD
  25554. ></TR
  25555. ></TABLE
  25556. ><P
  25557. >        but this script will output :
  25558.       </P
  25559. ><TABLE
  25560. BORDER="0"
  25561. BGCOLOR="#E0E0E0"
  25562. CELLPADDING="5"
  25563. ><TR
  25564. ><TD
  25565. ><PRE
  25566. CLASS="screen"
  25567. >2</PRE
  25568. ></TD
  25569. ></TR
  25570. ></TABLE
  25571. ><P
  25572. >        because the return value of the <A
  25573. HREF="#function.print"
  25574. ><B
  25575. CLASS="function"
  25576. >print()</B
  25577. ></A
  25578. >
  25579.        call is <VAR
  25580. CLASS="literal"
  25581. >int(1)</VAR
  25582. >, and it will look like the
  25583.        optional numeric argument mentioned above.
  25584.       </P
  25585. ><P
  25586. ></P
  25587. ></DIV
  25588. >
  25589.     </P
  25590. ></DIV
  25591. ><DIV
  25592. CLASS="sect1"
  25593. ><HR><H2
  25594. CLASS="sect1"
  25595. ><A
  25596. NAME="control-structures.switch"
  25597. ><VAR
  25598. CLASS="literal"
  25599. >switch</VAR
  25600. ></A
  25601. ></H2
  25602. ><P
  25603. >     The <VAR
  25604. CLASS="literal"
  25605. >switch</VAR
  25606. > statement is similar to a series of
  25607.     IF statements on the same expression.  In many occasions, you may
  25608.     want to compare the same variable (or expression) with many
  25609.     different values, and execute a different piece of code depending
  25610.     on which value it equals to.  This is exactly what the
  25611.     <VAR
  25612. CLASS="literal"
  25613. >switch</VAR
  25614. > statement is for.
  25615.    </P
  25616. ><DIV
  25617. CLASS="note"
  25618. ><BLOCKQUOTE
  25619. CLASS="note"
  25620. ><P
  25621. ><B
  25622. >Note: </B
  25623. >
  25624.      Note that unlike some other languages, the
  25625.      <A
  25626. HREF="#control-structures.continue"
  25627. >continue</A
  25628. > statement
  25629.      applies to switch and acts similar to <VAR
  25630. CLASS="literal"
  25631. >break</VAR
  25632. >.  If you
  25633.      have a switch inside a loop and wish to continue to the next iteration of
  25634.      the outer loop, use <VAR
  25635. CLASS="literal"
  25636. >continue 2</VAR
  25637. >.
  25638.     </P
  25639. ></BLOCKQUOTE
  25640. ></DIV
  25641. ><P
  25642. >     The following two examples are two different ways to write the
  25643.     same thing, one using a series of <VAR
  25644. CLASS="literal"
  25645. >if</VAR
  25646. > and
  25647.     <VAR
  25648. CLASS="literal"
  25649. >elseif</VAR
  25650. > statements, and the other using the
  25651.     <VAR
  25652. CLASS="literal"
  25653. >switch</VAR
  25654. > statement:
  25655.     <TABLE
  25656. WIDTH="100%"
  25657. BORDER="0"
  25658. CELLPADDING="0"
  25659. CELLSPACING="0"
  25660. CLASS="EXAMPLE"
  25661. ><TR
  25662. ><TD
  25663. ><DIV
  25664. CLASS="example"
  25665. ><A
  25666. NAME="AEN4644"
  25667. ></A
  25668. ><P
  25669. ><B
  25670. >Example 16-1. <VAR
  25671. CLASS="literal"
  25672. >switch</VAR
  25673. > structure</B
  25674. ></P
  25675. ><TABLE
  25676. BORDER="0"
  25677. BGCOLOR="#E0E0E0"
  25678. CELLPADDING="5"
  25679. ><TR
  25680. ><TD
  25681. ><PRE
  25682. CLASS="php"
  25683. ><?php
  25684. if ($i == 0) {
  25685.     echo "i equals 0";
  25686. } elseif ($i == 1) {
  25687.     echo "i equals 1";
  25688. } elseif ($i == 2) {
  25689.     echo "i equals 2";
  25690. }
  25691.  
  25692. switch ($i) {
  25693. case 0:
  25694.     echo "i equals 0";
  25695.     break;
  25696. case 1:
  25697.     echo "i equals 1";
  25698.     break;
  25699. case 2:
  25700.     echo "i equals 2";
  25701.     break;
  25702. }
  25703. ?></PRE
  25704. ></TD
  25705. ></TR
  25706. ></TABLE
  25707. ></DIV
  25708. ></TD
  25709. ></TR
  25710. ></TABLE
  25711. >
  25712.     <TABLE
  25713. WIDTH="100%"
  25714. BORDER="0"
  25715. CELLPADDING="0"
  25716. CELLSPACING="0"
  25717. CLASS="EXAMPLE"
  25718. ><TR
  25719. ><TD
  25720. ><DIV
  25721. CLASS="example"
  25722. ><A
  25723. NAME="AEN4648"
  25724. ></A
  25725. ><P
  25726. ><B
  25727. >Example 16-2. <VAR
  25728. CLASS="literal"
  25729. >switch</VAR
  25730. > structure allows usage of strings</B
  25731. ></P
  25732. ><TABLE
  25733. BORDER="0"
  25734. BGCOLOR="#E0E0E0"
  25735. CELLPADDING="5"
  25736. ><TR
  25737. ><TD
  25738. ><PRE
  25739. CLASS="php"
  25740. ><?php
  25741. switch ($i) {
  25742. case "apple":
  25743.     echo "i is apple";
  25744.     break;
  25745. case "bar":
  25746.     echo "i is bar";
  25747.     break;
  25748. case "cake":
  25749.     echo "i is cake";
  25750.     break;
  25751. }
  25752. ?></PRE
  25753. ></TD
  25754. ></TR
  25755. ></TABLE
  25756. ></DIV
  25757. ></TD
  25758. ></TR
  25759. ></TABLE
  25760. >
  25761.    </P
  25762. ><P
  25763. >     It is important to understand how the <VAR
  25764. CLASS="literal"
  25765. >switch</VAR
  25766. >
  25767.     statement is executed in order to avoid mistakes.  The
  25768.     <VAR
  25769. CLASS="literal"
  25770. >switch</VAR
  25771. > statement executes line by line
  25772.     (actually, statement by statement).  In the beginning, no code is
  25773.     executed.  Only when a <VAR
  25774. CLASS="literal"
  25775. >case</VAR
  25776. > statement is found
  25777.     with a value that matches the value of the
  25778.     <VAR
  25779. CLASS="literal"
  25780. >switch</VAR
  25781. > expression does PHP begin to execute the
  25782.     statements.  PHP continues to execute the statements until the end
  25783.     of the <VAR
  25784. CLASS="literal"
  25785. >switch</VAR
  25786. > block, or the first time it sees
  25787.     a <VAR
  25788. CLASS="literal"
  25789. >break</VAR
  25790. > statement.  If you don't write a
  25791.     <VAR
  25792. CLASS="literal"
  25793. >break</VAR
  25794. > statement at the end of a case's
  25795.     statement list, PHP will go on executing the statements of the
  25796.     following case.  For example:
  25797.     <DIV
  25798. CLASS="informalexample"
  25799. ><P
  25800. ></P
  25801. ><A
  25802. NAME="AEN4660"
  25803. ></A
  25804. ><TABLE
  25805. BORDER="0"
  25806. BGCOLOR="#E0E0E0"
  25807. CELLPADDING="5"
  25808. ><TR
  25809. ><TD
  25810. ><PRE
  25811. CLASS="php"
  25812. ><?php
  25813. switch ($i) {
  25814. case 0:
  25815.     echo "i equals 0";
  25816. case 1:
  25817.     echo "i equals 1";
  25818. case 2:
  25819.     echo "i equals 2";
  25820. }
  25821. ?></PRE
  25822. ></TD
  25823. ></TR
  25824. ></TABLE
  25825. ><P
  25826. ></P
  25827. ></DIV
  25828. >
  25829.    </P
  25830. ><P
  25831. >     Here, if <VAR
  25832. CLASS="varname"
  25833. >$i</VAR
  25834. > is equal to 0, PHP would execute all of the echo
  25835.     statements!  If <VAR
  25836. CLASS="varname"
  25837. >$i</VAR
  25838. > is equal to 1, PHP would execute the last two
  25839.     echo statements. You would get the expected behavior ('i equals 2' 
  25840.     would be displayed) only if <VAR
  25841. CLASS="varname"
  25842. >$i</VAR
  25843. > is equal to 2.  Thus,
  25844.     it is important not to forget <VAR
  25845. CLASS="literal"
  25846. >break</VAR
  25847. > statements
  25848.     (even though you may want to avoid supplying them on purpose under
  25849.     certain circumstances).
  25850.    </P
  25851. ><P
  25852. >     In a <VAR
  25853. CLASS="literal"
  25854. >switch</VAR
  25855. > statement, the condition is
  25856.     evaluated only once and the result is compared to each
  25857.     <VAR
  25858. CLASS="literal"
  25859. >case</VAR
  25860. > statement. In an <VAR
  25861. CLASS="literal"
  25862. >elseif</VAR
  25863. >
  25864.     statement, the condition is evaluated again. If your condition is
  25865.     more complicated than a simple compare and/or is in a tight loop,
  25866.     a <VAR
  25867. CLASS="literal"
  25868. >switch</VAR
  25869. > may be faster.
  25870.    </P
  25871. ><P
  25872. >     The statement list for a case can also be empty, which simply
  25873.     passes control into the statement list for the next case.
  25874.     <DIV
  25875. CLASS="informalexample"
  25876. ><P
  25877. ></P
  25878. ><A
  25879. NAME="AEN4673"
  25880. ></A
  25881. ><TABLE
  25882. BORDER="0"
  25883. BGCOLOR="#E0E0E0"
  25884. CELLPADDING="5"
  25885. ><TR
  25886. ><TD
  25887. ><PRE
  25888. CLASS="php"
  25889. ><?php
  25890. switch ($i) {
  25891. case 0:
  25892. case 1:
  25893. case 2:
  25894.     echo "i is less than 3 but not negative";
  25895.     break;
  25896. case 3:
  25897.     echo "i is 3";
  25898. }
  25899. ?></PRE
  25900. ></TD
  25901. ></TR
  25902. ></TABLE
  25903. ><P
  25904. ></P
  25905. ></DIV
  25906. >
  25907.    </P
  25908. ><P
  25909. >     A special case is the <VAR
  25910. CLASS="literal"
  25911. >default</VAR
  25912. > case.  This case matches
  25913.     anything that wasn't matched by the other cases, and should be the last
  25914.     <VAR
  25915. CLASS="literal"
  25916. >case</VAR
  25917. > statement.  For example:
  25918.     <DIV
  25919. CLASS="informalexample"
  25920. ><P
  25921. ></P
  25922. ><A
  25923. NAME="AEN4678"
  25924. ></A
  25925. ><TABLE
  25926. BORDER="0"
  25927. BGCOLOR="#E0E0E0"
  25928. CELLPADDING="5"
  25929. ><TR
  25930. ><TD
  25931. ><PRE
  25932. CLASS="php"
  25933. ><?php
  25934. switch ($i) {
  25935. case 0:
  25936.     echo "i equals 0";
  25937.     break;
  25938. case 1:
  25939.     echo "i equals 1";
  25940.     break;
  25941. case 2:
  25942.     echo "i equals 2";
  25943.     break;
  25944. default:
  25945.     echo "i is not equal to 0, 1 or 2";
  25946. }
  25947. ?></PRE
  25948. ></TD
  25949. ></TR
  25950. ></TABLE
  25951. ><P
  25952. ></P
  25953. ></DIV
  25954. >
  25955.    </P
  25956. ><P
  25957. >     The <VAR
  25958. CLASS="literal"
  25959. >case</VAR
  25960. > expression may be any expression that
  25961.     evaluates to a simple type, that is, integer or floating-point
  25962.     numbers and strings.  Arrays or objects cannot be used here unless
  25963.     they are dereferenced to a simple type.
  25964.    </P
  25965. ><P
  25966. >     The alternative syntax for control structures is supported with
  25967.     switches. For more information, see <A
  25968. HREF="#control-structures.alternative-syntax"
  25969. >Alternative syntax
  25970.     for control structures</A
  25971. > .
  25972.     <DIV
  25973. CLASS="informalexample"
  25974. ><P
  25975. ></P
  25976. ><A
  25977. NAME="AEN4684"
  25978. ></A
  25979. ><TABLE
  25980. BORDER="0"
  25981. BGCOLOR="#E0E0E0"
  25982. CELLPADDING="5"
  25983. ><TR
  25984. ><TD
  25985. ><PRE
  25986. CLASS="php"
  25987. ><?php
  25988. switch ($i):
  25989. case 0:
  25990.     echo "i equals 0";
  25991.     break;
  25992. case 1:
  25993.     echo "i equals 1";
  25994.     break;
  25995. case 2:
  25996.     echo "i equals 2";
  25997.     break;
  25998. default:
  25999.     echo "i is not equal to 0, 1 or 2";
  26000. endswitch;
  26001. ?></PRE
  26002. ></TD
  26003. ></TR
  26004. ></TABLE
  26005. ><P
  26006. ></P
  26007. ></DIV
  26008. >
  26009.    </P
  26010. ></DIV
  26011. ><DIV
  26012. CLASS="sect1"
  26013. ><HR><H2
  26014. CLASS="sect1"
  26015. ><A
  26016. NAME="control-structures.declare"
  26017. ><VAR
  26018. CLASS="literal"
  26019. >declare</VAR
  26020. ></A
  26021. ></H2
  26022. ><P
  26023. >     The <VAR
  26024. CLASS="literal"
  26025. >declare</VAR
  26026. > construct is used to
  26027.     set execution directives for a block of code.
  26028.     The syntax of <VAR
  26029. CLASS="literal"
  26030. >declare</VAR
  26031. > is similar to
  26032.     the syntax of other flow control constructs:
  26033.     <DIV
  26034. CLASS="informalexample"
  26035. ><P
  26036. ></P
  26037. ><A
  26038. NAME="AEN4692"
  26039. ></A
  26040. ><TABLE
  26041. BORDER="0"
  26042. BGCOLOR="#E0E0E0"
  26043. CELLPADDING="5"
  26044. ><TR
  26045. ><TD
  26046. ><PRE
  26047. CLASS="programlisting"
  26048. >declare (directive)
  26049.     statement</PRE
  26050. ></TD
  26051. ></TR
  26052. ></TABLE
  26053. ><P
  26054. ></P
  26055. ></DIV
  26056. >
  26057.    </P
  26058. ><P
  26059. >     The <VAR
  26060. CLASS="literal"
  26061. >directive</VAR
  26062. > section allows the
  26063.     behavior of the <VAR
  26064. CLASS="literal"
  26065. >declare</VAR
  26066. > block to
  26067.     be set.
  26068.     Currently only one directive is recognized: the
  26069.     <VAR
  26070. CLASS="literal"
  26071. >ticks</VAR
  26072. > directive. (See below for more
  26073.     information on the
  26074.     <A
  26075. HREF="#control-structures.declare.ticks"
  26076. >ticks</A
  26077. >
  26078.     directive)
  26079.    </P
  26080. ><P
  26081. >     The <VAR
  26082. CLASS="literal"
  26083. >statement</VAR
  26084. > part of the
  26085.     <VAR
  26086. CLASS="literal"
  26087. >declare</VAR
  26088. > block will be executed -- how
  26089.     it is executed and what side effects occur during execution
  26090.     may depend on the directive set in the
  26091.     <VAR
  26092. CLASS="literal"
  26093. >directive</VAR
  26094. > block.
  26095.    </P
  26096. ><P
  26097. >     The <VAR
  26098. CLASS="literal"
  26099. >declare</VAR
  26100. > construct can also be used in the global 
  26101.     scope, affecting all code following it.
  26102.     <DIV
  26103. CLASS="informalexample"
  26104. ><P
  26105. ></P
  26106. ><A
  26107. NAME="AEN4705"
  26108. ></A
  26109. ><TABLE
  26110. BORDER="0"
  26111. BGCOLOR="#E0E0E0"
  26112. CELLPADDING="5"
  26113. ><TR
  26114. ><TD
  26115. ><PRE
  26116. CLASS="php"
  26117. ><?php
  26118. // these are the same:
  26119.  
  26120. // you can use this:
  26121. declare(ticks=1) {
  26122.     // entire script here
  26123. }
  26124.  
  26125. // or you can use this:
  26126. declare(ticks=1);
  26127. // entire script here
  26128. ?></PRE
  26129. ></TD
  26130. ></TR
  26131. ></TABLE
  26132. ><P
  26133. ></P
  26134. ></DIV
  26135. >
  26136.    </P
  26137. ><DIV
  26138. CLASS="sect2"
  26139. ><HR><H3
  26140. CLASS="sect2"
  26141. ><A
  26142. NAME="control-structures.declare.ticks"
  26143. >Ticks</A
  26144. ></H3
  26145. ><P
  26146. >A tick is an event that occurs for every
  26147.     <VAR
  26148. CLASS="varname"
  26149. >N</VAR
  26150. > low-level statements executed
  26151.     by the parser within the <VAR
  26152. CLASS="literal"
  26153. >declare</VAR
  26154. > block.
  26155.     The value for <VAR
  26156. CLASS="varname"
  26157. >N</VAR
  26158. > is specified
  26159.     using <VAR
  26160. CLASS="literal"
  26161. >ticks=<VAR
  26162. CLASS="varname"
  26163. >N</VAR
  26164. ></VAR
  26165. >
  26166.     within the <VAR
  26167. CLASS="literal"
  26168. >declare</VAR
  26169. > blocks's
  26170.     <VAR
  26171. CLASS="literal"
  26172. >directive</VAR
  26173. > section.
  26174.    </P
  26175. ><P
  26176. >     The event(s) that occur on each tick are specified using the
  26177.     <A
  26178. HREF="#function.register-tick-function"
  26179. ><B
  26180. CLASS="function"
  26181. >register_tick_function()</B
  26182. ></A
  26183. >. See the example 
  26184.     below for more details. Note that more than one event can occur
  26185.     for each tick.
  26186.    </P
  26187. ><P
  26188. >     <TABLE
  26189. WIDTH="100%"
  26190. BORDER="0"
  26191. CELLPADDING="0"
  26192. CELLSPACING="0"
  26193. CLASS="EXAMPLE"
  26194. ><TR
  26195. ><TD
  26196. ><DIV
  26197. CLASS="example"
  26198. ><A
  26199. NAME="AEN4720"
  26200. ></A
  26201. ><P
  26202. ><B
  26203. >Example 16-3. Profile a section of PHP code</B
  26204. ></P
  26205. ><TABLE
  26206. BORDER="0"
  26207. BGCOLOR="#E0E0E0"
  26208. CELLPADDING="5"
  26209. ><TR
  26210. ><TD
  26211. ><PRE
  26212. CLASS="php"
  26213. ><?php
  26214. // A function that records the time when it is called
  26215. function profile($dump = FALSE)
  26216. {
  26217.     static $profile;
  26218.  
  26219.     // Return the times stored in profile, then erase it
  26220.     if ($dump) {
  26221.         $temp = $profile;
  26222.         unset($profile);
  26223.         return($temp);
  26224.     }
  26225.  
  26226.     $profile[] = microtime();
  26227. }
  26228.  
  26229. // Set up a tick handler
  26230. register_tick_function("profile");
  26231.  
  26232. // Initialize the function before the declare block
  26233. profile();
  26234.  
  26235. // Run a block of code, throw a tick every 2nd statement
  26236. declare(ticks=2) {
  26237.     for ($x = 1; $x < 50; ++$x) {
  26238.         echo similar_text(md5($x), md5($x*$x)), "<br />;";
  26239.     }
  26240. }
  26241.  
  26242. // Display the data stored in the profiler
  26243. print_r(profile(TRUE));
  26244. ?></PRE
  26245. ></TD
  26246. ></TR
  26247. ></TABLE
  26248. ></DIV
  26249. ></TD
  26250. ></TR
  26251. ></TABLE
  26252. >
  26253.     The example profiles the PHP code within the 'declare'
  26254.     block, recording the time at which every second low-level
  26255.     statement in the block was executed. This information can
  26256.     then be used to find the slow areas within particular
  26257.     segments of code. This process can be performed using other
  26258.     methods: using ticks is more convenient and easier to
  26259.     implement.
  26260.    </P
  26261. ><P
  26262. >     Ticks are well suited for debugging, implementing simple
  26263.     multitasking, background I/O and many other tasks.
  26264.    </P
  26265. ><P
  26266. >     See also <A
  26267. HREF="#function.register-tick-function"
  26268. ><B
  26269. CLASS="function"
  26270. >register_tick_function()</B
  26271. ></A
  26272. > and
  26273.     <A
  26274. HREF="#function.unregister-tick-function"
  26275. ><B
  26276. CLASS="function"
  26277. >unregister_tick_function()</B
  26278. ></A
  26279. >.
  26280.    </P
  26281. ></DIV
  26282. ></DIV
  26283. ><DIV
  26284. CLASS="sect1"
  26285. ><HR><H2
  26286. CLASS="sect1"
  26287. ><A
  26288. NAME="function.return"
  26289. >return</A
  26290. ></H2
  26291. ><P
  26292. >     If called from within a function, the <A
  26293. HREF="#function.return"
  26294. ><B
  26295. CLASS="function"
  26296. >return()</B
  26297. ></A
  26298. >
  26299.     statement immediately ends execution of the current function, and
  26300.     returns its argument as the value of the function
  26301.     call. <A
  26302. HREF="#function.return"
  26303. ><B
  26304. CLASS="function"
  26305. >return()</B
  26306. ></A
  26307. > will also end the execution of
  26308.     an <A
  26309. HREF="#function.eval"
  26310. ><B
  26311. CLASS="function"
  26312. >eval()</B
  26313. ></A
  26314. > statement or script file.
  26315.    </P
  26316. ><P
  26317. >     If called from the global scope, then execution of the current
  26318.     script file is ended. If the current script file was
  26319.     <A
  26320. HREF="#function.include"
  26321. ><B
  26322. CLASS="function"
  26323. >include()</B
  26324. ></A
  26325. >ed or <A
  26326. HREF="#function.require"
  26327. ><B
  26328. CLASS="function"
  26329. >require()</B
  26330. ></A
  26331. >ed,
  26332.     then control is passed back to the calling file. Furthermore, if
  26333.     the current script file was <A
  26334. HREF="#function.include"
  26335. ><B
  26336. CLASS="function"
  26337. >include()</B
  26338. ></A
  26339. >ed, then
  26340.     the value given to <A
  26341. HREF="#function.return"
  26342. ><B
  26343. CLASS="function"
  26344. >return()</B
  26345. ></A
  26346. > will be returned as
  26347.     the value of the <A
  26348. HREF="#function.include"
  26349. ><B
  26350. CLASS="function"
  26351. >include()</B
  26352. ></A
  26353. > call. If
  26354.     <A
  26355. HREF="#function.return"
  26356. ><B
  26357. CLASS="function"
  26358. >return()</B
  26359. ></A
  26360. > is called from within the main script
  26361.     file, then script execution ends. If the current script file was
  26362.     named by the <A
  26363. HREF="#ini.auto-prepend-file"
  26364. >auto_prepend_file</A
  26365. > or <A
  26366. HREF="#ini.auto-append-file"
  26367. >auto_append_file</A
  26368. >
  26369.     configuration options in <TT
  26370. CLASS="filename"
  26371. >php.ini</TT
  26372. >, 
  26373.     then that script file's execution is ended.
  26374.    </P
  26375. ><P
  26376. >For more information, see <A
  26377. HREF="#functions.returning-values"
  26378. >Returning values</A
  26379. >.
  26380.    </P
  26381. ><P
  26382. >     <DIV
  26383. CLASS="note"
  26384. ><BLOCKQUOTE
  26385. CLASS="note"
  26386. ><P
  26387. ><B
  26388. >Note: </B
  26389. >
  26390.       Note that since <A
  26391. HREF="#function.return"
  26392. ><B
  26393. CLASS="function"
  26394. >return()</B
  26395. ></A
  26396. > is a language
  26397.       construct and not a function, the parentheses surrounding its
  26398.       arguments are <SPAN
  26399. CLASS="emphasis"
  26400. ><I
  26401. CLASS="emphasis"
  26402. >only</I
  26403. ></SPAN
  26404. > required if the argument
  26405.       contains an expression. It is common to leave them out while returning a variable.
  26406.      </P
  26407. ></BLOCKQUOTE
  26408. ></DIV
  26409. >
  26410.    </P
  26411. ></DIV
  26412. ><DIV
  26413. CLASS="sect1"
  26414. ><HR><H2
  26415. CLASS="sect1"
  26416. ><A
  26417. NAME="function.require"
  26418. ><A
  26419. HREF="#function.require"
  26420. ><B
  26421. CLASS="function"
  26422. >require()</B
  26423. ></A
  26424. ></A
  26425. ></H2
  26426. ><P
  26427. >     The <A
  26428. HREF="#function.require"
  26429. ><B
  26430. CLASS="function"
  26431. >require()</B
  26432. ></A
  26433. > statement includes and evaluates
  26434.     the specific file.
  26435.    </P
  26436. ><P
  26437. >      <A
  26438. HREF="#function.require"
  26439. ><B
  26440. CLASS="function"
  26441. >require()</B
  26442. ></A
  26443. > includes and evaluates a specific file.  
  26444.      Detailed information on how this inclusion works is described in the 
  26445.      documentation for <A
  26446. HREF="#function.include"
  26447. ><B
  26448. CLASS="function"
  26449. >include()</B
  26450. ></A
  26451. >.
  26452.    </P
  26453. ><P
  26454. >      <A
  26455. HREF="#function.require"
  26456. ><B
  26457. CLASS="function"
  26458. >require()</B
  26459. ></A
  26460. > and <A
  26461. HREF="#function.include"
  26462. ><B
  26463. CLASS="function"
  26464. >include()</B
  26465. ></A
  26466.      are identical in every way except how they handle failure.  
  26467.      <A
  26468. HREF="#function.include"
  26469. ><B
  26470. CLASS="function"
  26471. >include()</B
  26472. ></A
  26473. > produces a 
  26474.      <A
  26475. HREF="#internal.e-warning"
  26476. >Warning</A
  26477. > while 
  26478.      <A
  26479. HREF="#function.require"
  26480. ><B
  26481. CLASS="function"
  26482. >require()</B
  26483. ></A
  26484. > results in a <A
  26485. HREF="#internal.e-error"
  26486. >      Fatal Error</A
  26487. >.  In other words, don't hesitate to use 
  26488.      <A
  26489. HREF="#function.require"
  26490. ><B
  26491. CLASS="function"
  26492. >require()</B
  26493. ></A
  26494. > if you want a missing file to halt processing 
  26495.      of the page.  <A
  26496. HREF="#function.include"
  26497. ><B
  26498. CLASS="function"
  26499. >include()</B
  26500. ></A
  26501. > does not behave this way, the
  26502.      script will continue regardless.  Be sure to have an appropriate
  26503.      <A
  26504. HREF="#ini.include-path"
  26505. >include_path</A
  26506. > setting as well.
  26507.    </P
  26508. ><P
  26509. >     <TABLE
  26510. WIDTH="100%"
  26511. BORDER="0"
  26512. CELLPADDING="0"
  26513. CELLSPACING="0"
  26514. CLASS="EXAMPLE"
  26515. ><TR
  26516. ><TD
  26517. ><DIV
  26518. CLASS="example"
  26519. ><A
  26520. NAME="AEN4769"
  26521. ></A
  26522. ><P
  26523. ><B
  26524. >Example 16-4. Basic <A
  26525. HREF="#function.require"
  26526. ><B
  26527. CLASS="function"
  26528. >require()</B
  26529. ></A
  26530. > examples</B
  26531. ></P
  26532. ><TABLE
  26533. BORDER="0"
  26534. BGCOLOR="#E0E0E0"
  26535. CELLPADDING="5"
  26536. ><TR
  26537. ><TD
  26538. ><PRE
  26539. CLASS="php"
  26540. ><?php
  26541.  
  26542. require 'prepend.php';
  26543.  
  26544. require $somefile;
  26545.  
  26546. require ('somefile.txt');
  26547.  
  26548. ?></PRE
  26549. ></TD
  26550. ></TR
  26551. ></TABLE
  26552. ></DIV
  26553. ></TD
  26554. ></TR
  26555. ></TABLE
  26556. >
  26557.    </P
  26558. ><P
  26559. >     See the <A
  26560. HREF="#function.include"
  26561. ><B
  26562. CLASS="function"
  26563. >include()</B
  26564. ></A
  26565. > documentation for more examples.
  26566.    </P
  26567. ><P
  26568. >     <DIV
  26569. CLASS="note"
  26570. ><BLOCKQUOTE
  26571. CLASS="note"
  26572. ><P
  26573. ><B
  26574. >Note: </B
  26575. >
  26576.       Prior to PHP 4.0.2, the following applies: <A
  26577. HREF="#function.require"
  26578. ><B
  26579. CLASS="function"
  26580. >require()</B
  26581. ></A
  26582. >
  26583.       will always attempt to read the target file, even if the line it's on
  26584.       never executes.  The conditional statement won't affect
  26585.       <A
  26586. HREF="#function.require"
  26587. ><B
  26588. CLASS="function"
  26589. >require()</B
  26590. ></A
  26591. >. However, if the line on which the
  26592.       <A
  26593. HREF="#function.require"
  26594. ><B
  26595. CLASS="function"
  26596. >require()</B
  26597. ></A
  26598. > occurs is not executed, neither will any of
  26599.       the code in the target file be executed.  Similarly, looping structures
  26600.       do not affect the behaviour of <A
  26601. HREF="#function.require"
  26602. ><B
  26603. CLASS="function"
  26604. >require()</B
  26605. ></A
  26606. >. Although
  26607.       the code contained in the target file is still subject to the loop, the
  26608.       <A
  26609. HREF="#function.require"
  26610. ><B
  26611. CLASS="function"
  26612. >require()</B
  26613. ></A
  26614. > itself happens only once.
  26615.      </P
  26616. ></BLOCKQUOTE
  26617. ></DIV
  26618. >
  26619.    </P
  26620. ><DIV
  26621. CLASS="note"
  26622. ><BLOCKQUOTE
  26623. CLASS="note"
  26624. ><P
  26625. ><B
  26626. >Note: </B
  26627. >Because this is a 
  26628. language construct and not a function, it cannot be called using 
  26629. <A
  26630. HREF="#functions.variable-functions"
  26631. >variable functions</A
  26632. ></P
  26633. ></BLOCKQUOTE
  26634. ></DIV
  26635. ><DIV
  26636. CLASS="warning"
  26637. ><P
  26638. ></P
  26639. ><TABLE
  26640. CLASS="warning"
  26641. BORDER="1"
  26642. WIDTH="100%"
  26643. ><TR
  26644. ><TD
  26645. ALIGN="CENTER"
  26646. ><B
  26647. >Warning</B
  26648. ></TD
  26649. ></TR
  26650. ><TR
  26651. ><TD
  26652. ALIGN="LEFT"
  26653. ><P
  26654. >Windows versions of PHP
  26655. prior to PHP 4.3.0 do not support accessing remote files via this function, even if
  26656. <A
  26657. HREF="#ini.allow-url-fopen"
  26658. >allow_url_fopen</A
  26659. > is enabled.
  26660. </P
  26661. ></TD
  26662. ></TR
  26663. ></TABLE
  26664. ></DIV
  26665. ><P
  26666. >     See also <A
  26667. HREF="#function.include"
  26668. ><B
  26669. CLASS="function"
  26670. >include()</B
  26671. ></A
  26672. >, <A
  26673. HREF="#function.require-once"
  26674. ><B
  26675. CLASS="function"
  26676. >require_once()</B
  26677. ></A
  26678. >,
  26679.     <A
  26680. HREF="#function.include-once"
  26681. ><B
  26682. CLASS="function"
  26683. >include_once()</B
  26684. ></A
  26685. >, <A
  26686. HREF="#function.eval"
  26687. ><B
  26688. CLASS="function"
  26689. >eval()</B
  26690. ></A
  26691. >, 
  26692.     <A
  26693. HREF="#function.file"
  26694. ><B
  26695. CLASS="function"
  26696. >file()</B
  26697. ></A
  26698. >, <A
  26699. HREF="#function.readfile"
  26700. ><B
  26701. CLASS="function"
  26702. >readfile()</B
  26703. ></A
  26704. >, 
  26705.     <A
  26706. HREF="#function.virtual"
  26707. ><B
  26708. CLASS="function"
  26709. >virtual()</B
  26710. ></A
  26711. > and <A
  26712. HREF="#ini.include-path"
  26713. >include_path</A
  26714. >.
  26715.    </P
  26716. ></DIV
  26717. ><DIV
  26718. CLASS="sect1"
  26719. ><HR><H2
  26720. CLASS="sect1"
  26721. ><A
  26722. NAME="function.include"
  26723. ><A
  26724. HREF="#function.include"
  26725. ><B
  26726. CLASS="function"
  26727. >include()</B
  26728. ></A
  26729. ></A
  26730. ></H2
  26731. ><P
  26732. >     The <A
  26733. HREF="#function.include"
  26734. ><B
  26735. CLASS="function"
  26736. >include()</B
  26737. ></A
  26738. > statement includes and evaluates
  26739.     the specified file.
  26740.    </P
  26741. ><P
  26742. >      The documentation below also applies to <A
  26743. HREF="#function.require"
  26744. ><B
  26745. CLASS="function"
  26746. >require()</B
  26747. ></A
  26748. >.
  26749.      The two constructs are identical in every way except how they handle
  26750.      failure.  <A
  26751. HREF="#function.include"
  26752. ><B
  26753. CLASS="function"
  26754. >include()</B
  26755. ></A
  26756. > produces a 
  26757.      <A
  26758. HREF="#internal.e-warning"
  26759. >Warning</A
  26760. > while <A
  26761. HREF="#function.require"
  26762. ><B
  26763. CLASS="function"
  26764. >require()</B
  26765. ></A
  26766. >
  26767.      results in a <A
  26768. HREF="#internal.e-error"
  26769. >Fatal Error</A
  26770. >.
  26771.      In other words, use <A
  26772. HREF="#function.require"
  26773. ><B
  26774. CLASS="function"
  26775. >require()</B
  26776. ></A
  26777. > if you want 
  26778.      a missing file to halt processing of the page.  <A
  26779. HREF="#function.include"
  26780. ><B
  26781. CLASS="function"
  26782. >include()</B
  26783. ></A
  26784. > does 
  26785.      not behave this way, the script will continue regardless.  Be sure to have an 
  26786.      appropriate <A
  26787. HREF="#ini.include-path"
  26788. >include_path</A
  26789. > setting as well.
  26790.      Be warned that parse error in required file doesn't cause processing halting.
  26791.    </P
  26792. ><P
  26793. >     Files for including are first looked in include_path relative to the current working directory
  26794.     and then in include_path relative to the directory of current script. E.g. if your include_path
  26795.     is <VAR
  26796. CLASS="literal"
  26797. >.</VAR
  26798. >, current working directory is <TT
  26799. CLASS="filename"
  26800. >/www/</TT
  26801. >,
  26802.     you included <TT
  26803. CLASS="filename"
  26804. >include/a.php</TT
  26805. > and there is <VAR
  26806. CLASS="literal"
  26807. >include "b.php"</VAR
  26808. >
  26809.     in that file, <TT
  26810. CLASS="filename"
  26811. >b.php</TT
  26812. > is first looked in <TT
  26813. CLASS="filename"
  26814. >/www/</TT
  26815. >
  26816.     and then in <TT
  26817. CLASS="filename"
  26818. >/www/include/</TT
  26819. >.
  26820.     If filename begins with <VAR
  26821. CLASS="literal"
  26822. >../</VAR
  26823. >, it is looked only in
  26824.     include_path relative to the current working directory.
  26825.    </P
  26826. ><P
  26827. >      When a file is included, the code it contains inherits the
  26828.      <A
  26829. HREF="#language.variables.scope"
  26830. >variable scope</A
  26831. > of the
  26832.      line on which the include occurs.  Any variables available at that line
  26833.      in the calling file will be available within the called file, from that
  26834.      point forward.
  26835.    </P
  26836. ><P
  26837. >      <TABLE
  26838. WIDTH="100%"
  26839. BORDER="0"
  26840. CELLPADDING="0"
  26841. CELLSPACING="0"
  26842. CLASS="EXAMPLE"
  26843. ><TR
  26844. ><TD
  26845. ><DIV
  26846. CLASS="example"
  26847. ><A
  26848. NAME="AEN4824"
  26849. ></A
  26850. ><P
  26851. ><B
  26852. >Example 16-5. Basic <A
  26853. HREF="#function.include"
  26854. ><B
  26855. CLASS="function"
  26856. >include()</B
  26857. ></A
  26858. > example</B
  26859. ></P
  26860. ><TABLE
  26861. BORDER="0"
  26862. BGCOLOR="#E0E0E0"
  26863. CELLPADDING="5"
  26864. ><TR
  26865. ><TD
  26866. ><PRE
  26867. CLASS="php"
  26868. >vars.php
  26869. <?php
  26870.  
  26871. $color = 'green';
  26872. $fruit = 'apple';
  26873.  
  26874. ?>
  26875.  
  26876. test.php
  26877. <?php
  26878.  
  26879. echo "A $color $fruit"; // A
  26880.  
  26881. include 'vars.php';
  26882.  
  26883. echo "A $color $fruit"; // A green apple
  26884.  
  26885. ?></PRE
  26886. ></TD
  26887. ></TR
  26888. ></TABLE
  26889. ></DIV
  26890. ></TD
  26891. ></TR
  26892. ></TABLE
  26893. >
  26894.    </P
  26895. ><P
  26896. >      If the include occurs inside a function within the calling file,
  26897.      then all of the code contained in the called file will behave as
  26898.      though it had been defined inside that function.  So, it will follow
  26899.      the variable scope of that function.
  26900.    </P
  26901. ><P
  26902. >      <TABLE
  26903. WIDTH="100%"
  26904. BORDER="0"
  26905. CELLPADDING="0"
  26906. CELLSPACING="0"
  26907. CLASS="EXAMPLE"
  26908. ><TR
  26909. ><TD
  26910. ><DIV
  26911. CLASS="example"
  26912. ><A
  26913. NAME="AEN4830"
  26914. ></A
  26915. ><P
  26916. ><B
  26917. >Example 16-6. Including within functions</B
  26918. ></P
  26919. ><TABLE
  26920. BORDER="0"
  26921. BGCOLOR="#E0E0E0"
  26922. CELLPADDING="5"
  26923. ><TR
  26924. ><TD
  26925. ><PRE
  26926. CLASS="php"
  26927. ><?php
  26928.  
  26929. function foo()
  26930. {
  26931.     global $color;
  26932.  
  26933.     include 'vars.php';
  26934.  
  26935.     echo "A $color $fruit";
  26936. }
  26937.  
  26938. /* vars.php is in the scope of foo() so     *
  26939.  * $fruit is NOT available outside of this  *
  26940.  * scope.  $color is because we declared it *
  26941.  * as global.                               */
  26942.  
  26943. foo();                    // A green apple
  26944. echo "A $color $fruit";   // A green
  26945.  
  26946. ?></PRE
  26947. ></TD
  26948. ></TR
  26949. ></TABLE
  26950. ></DIV
  26951. ></TD
  26952. ></TR
  26953. ></TABLE
  26954. >
  26955.    </P
  26956. ><P
  26957. >      When a file is included, parsing drops out of PHP mode and
  26958.      into HTML mode at the beginning of the target file, and resumes
  26959.      again at the end.  For this reason, any code inside the target
  26960.      file which should be executed as PHP code must be enclosed within
  26961.      <A
  26962. HREF="#language.basic-syntax.phpmode"
  26963. >valid PHP start
  26964.      and end tags</A
  26965. >.
  26966.    </P
  26967. ><P
  26968. >      If "<A
  26969. HREF="#ini.allow-url-fopen"
  26970. >URL fopen wrappers</A
  26971. >"
  26972.      are enabled in PHP (which they are in the default configuration),
  26973.      you can specify the file to be included using a URL (via HTTP or
  26974.      other supported wrapper - see <A
  26975. HREF="#wrappers"
  26976. >Appendix L</A
  26977. > for a list
  26978.      of protocols) instead of a local pathname.  If the target server interprets
  26979.      the target file as PHP code, variables may be passed to the included
  26980.      file using a URL request string as used with HTTP GET.  This is
  26981.      not strictly speaking the same thing as including the file and having
  26982.      it inherit the parent file's variable scope; the script is actually
  26983.      being run on the remote server and the result is then being
  26984.      included into the local script.
  26985.    </P
  26986. ><DIV
  26987. CLASS="warning"
  26988. ><P
  26989. ></P
  26990. ><TABLE
  26991. CLASS="warning"
  26992. BORDER="1"
  26993. WIDTH="100%"
  26994. ><TR
  26995. ><TD
  26996. ALIGN="CENTER"
  26997. ><B
  26998. >Warning</B
  26999. ></TD
  27000. ></TR
  27001. ><TR
  27002. ><TD
  27003. ALIGN="LEFT"
  27004. ><P
  27005. >Windows versions of PHP
  27006. prior to PHP 4.3.0 do not support accessing remote files via this function, even if
  27007. <A
  27008. HREF="#ini.allow-url-fopen"
  27009. >allow_url_fopen</A
  27010. > is enabled.
  27011. </P
  27012. ></TD
  27013. ></TR
  27014. ></TABLE
  27015. ></DIV
  27016. ><P
  27017. >     <TABLE
  27018. WIDTH="100%"
  27019. BORDER="0"
  27020. CELLPADDING="0"
  27021. CELLSPACING="0"
  27022. CLASS="EXAMPLE"
  27023. ><TR
  27024. ><TD
  27025. ><DIV
  27026. CLASS="example"
  27027. ><A
  27028. NAME="AEN4842"
  27029. ></A
  27030. ><P
  27031. ><B
  27032. >Example 16-7. <A
  27033. HREF="#function.include"
  27034. ><B
  27035. CLASS="function"
  27036. >include()</B
  27037. ></A
  27038. > through HTTP</B
  27039. ></P
  27040. ><TABLE
  27041. BORDER="0"
  27042. BGCOLOR="#E0E0E0"
  27043. CELLPADDING="5"
  27044. ><TR
  27045. ><TD
  27046. ><PRE
  27047. CLASS="php"
  27048. ><?php
  27049.  
  27050. /* This example assumes that www.example.com is configured to parse .php
  27051.  * files and not .txt files. Also, 'Works' here means that the variables
  27052.  * $foo and $bar are available within the included file. */
  27053.  
  27054. // Won't work; file.txt wasn't handled by www.example.com as PHP
  27055. include 'http://www.example.com/file.txt?foo=1&bar=2';
  27056.  
  27057. // Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
  27058. // local filesystem.
  27059. include 'file.php?foo=1&bar=2';
  27060.  
  27061. // Works.
  27062. include 'http://www.example.com/file.php?foo=1&bar=2';
  27063.  
  27064. $foo = 1;
  27065. $bar = 2;
  27066. include 'file.txt';  // Works.
  27067. include 'file.php';  // Works.
  27068.  
  27069. ?></PRE
  27070. ></TD
  27071. ></TR
  27072. ></TABLE
  27073. ></DIV
  27074. ></TD
  27075. ></TR
  27076. ></TABLE
  27077. >
  27078.     See also <A
  27079. HREF="#features.remote-files"
  27080. >Remote files</A
  27081. >,
  27082.     <A
  27083. HREF="#function.fopen"
  27084. ><B
  27085. CLASS="function"
  27086. >fopen()</B
  27087. ></A
  27088. > and <A
  27089. HREF="#function.file"
  27090. ><B
  27091. CLASS="function"
  27092. >file()</B
  27093. ></A
  27094. > for related 
  27095.     information.
  27096.    </P
  27097. ><P
  27098. >      Because <A
  27099. HREF="#function.include"
  27100. ><B
  27101. CLASS="function"
  27102. >include()</B
  27103. ></A
  27104. > and <A
  27105. HREF="#function.require"
  27106. ><B
  27107. CLASS="function"
  27108. >require()</B
  27109. ></A
  27110. >
  27111.      are special language constructs, you must enclose them within a statement
  27112.      block if it's inside a conditional block.
  27113.    </P
  27114. ><P
  27115. >     <TABLE
  27116. WIDTH="100%"
  27117. BORDER="0"
  27118. CELLPADDING="0"
  27119. CELLSPACING="0"
  27120. CLASS="EXAMPLE"
  27121. ><TR
  27122. ><TD
  27123. ><DIV
  27124. CLASS="example"
  27125. ><A
  27126. NAME="AEN4853"
  27127. ></A
  27128. ><P
  27129. ><B
  27130. >Example 16-8. include() and conditional blocks</B
  27131. ></P
  27132. ><TABLE
  27133. BORDER="0"
  27134. BGCOLOR="#E0E0E0"
  27135. CELLPADDING="5"
  27136. ><TR
  27137. ><TD
  27138. ><PRE
  27139. CLASS="php"
  27140. ><?php
  27141.  
  27142. // This is WRONG and will not work as desired.
  27143. if ($condition)
  27144.     include $file;
  27145. else
  27146.     include $other;
  27147.  
  27148.  
  27149. // This is CORRECT.
  27150. if ($condition) {
  27151.     include $file;
  27152. } else {
  27153.     include $other;
  27154. }
  27155.  
  27156. ?></PRE
  27157. ></TD
  27158. ></TR
  27159. ></TABLE
  27160. ></DIV
  27161. ></TD
  27162. ></TR
  27163. ></TABLE
  27164. >
  27165.    </P
  27166. ><P
  27167. >     Handling Returns: It is possible to execute a <A
  27168. HREF="#function.return"
  27169. ><B
  27170. CLASS="function"
  27171. >return()</B
  27172. ></A
  27173. >
  27174.     statement inside an included file in order to terminate processing in that
  27175.     file and return to the script which called it.  Also, it's possible to return
  27176.     values from included files.  You can take the value of the include call as
  27177.     you would a normal function.  This is not, however, possible when including
  27178.     remote files unless the output of the remote file has
  27179.     <A
  27180. HREF="#language.basic-syntax.phpmode"
  27181. >valid PHP start
  27182.     and end tags</A
  27183. > (as with any local file).  You can declare the needed
  27184.     variables within those tags and they will be introduced at whichever point
  27185.     the file was included.
  27186.    </P
  27187. ><P
  27188. >     Because <A
  27189. HREF="#function.include"
  27190. ><B
  27191. CLASS="function"
  27192. >include()</B
  27193. ></A
  27194. > is a special language construct,
  27195.     parentheses are not needed around its argument. Take care when comparing
  27196.     return value.
  27197.     <TABLE
  27198. WIDTH="100%"
  27199. BORDER="0"
  27200. CELLPADDING="0"
  27201. CELLSPACING="0"
  27202. CLASS="EXAMPLE"
  27203. ><TR
  27204. ><TD
  27205. ><DIV
  27206. CLASS="example"
  27207. ><A
  27208. NAME="AEN4861"
  27209. ></A
  27210. ><P
  27211. ><B
  27212. >Example 16-9. Comparing return value of include</B
  27213. ></P
  27214. ><TABLE
  27215. BORDER="0"
  27216. BGCOLOR="#E0E0E0"
  27217. CELLPADDING="5"
  27218. ><TR
  27219. ><TD
  27220. ><PRE
  27221. CLASS="php"
  27222. ><?php
  27223. // won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
  27224. if (include('vars.php') == 'OK') {
  27225.     echo 'OK';
  27226. }
  27227.  
  27228. // works
  27229. if ((include 'vars.php') == 'OK') {
  27230.     echo 'OK';
  27231. }
  27232. ?></PRE
  27233. ></TD
  27234. ></TR
  27235. ></TABLE
  27236. ></DIV
  27237. ></TD
  27238. ></TR
  27239. ></TABLE
  27240. >
  27241.    </P
  27242. ><P
  27243. >     <DIV
  27244. CLASS="note"
  27245. ><BLOCKQUOTE
  27246. CLASS="note"
  27247. ><P
  27248. ><B
  27249. >Note: </B
  27250. >
  27251.       In PHP 3, the return may not appear inside a block unless it's
  27252.       a function block, in which case the <A
  27253. HREF="#function.return"
  27254. ><B
  27255. CLASS="function"
  27256. >return()</B
  27257. ></A
  27258. > applies 
  27259.       to that function and not the whole file.
  27260.      </P
  27261. ></BLOCKQUOTE
  27262. ></DIV
  27263. >
  27264.    </P
  27265. ><P
  27266. >     <TABLE
  27267. WIDTH="100%"
  27268. BORDER="0"
  27269. CELLPADDING="0"
  27270. CELLSPACING="0"
  27271. CLASS="EXAMPLE"
  27272. ><TR
  27273. ><TD
  27274. ><DIV
  27275. CLASS="example"
  27276. ><A
  27277. NAME="AEN4869"
  27278. ></A
  27279. ><P
  27280. ><B
  27281. >Example 16-10. <A
  27282. HREF="#function.include"
  27283. ><B
  27284. CLASS="function"
  27285. >include()</B
  27286. ></A
  27287. > and the <A
  27288. HREF="#function.return"
  27289. ><B
  27290. CLASS="function"
  27291. >return()</B
  27292. ></A
  27293. > statement</B
  27294. ></P
  27295. ><TABLE
  27296. BORDER="0"
  27297. BGCOLOR="#E0E0E0"
  27298. CELLPADDING="5"
  27299. ><TR
  27300. ><TD
  27301. ><PRE
  27302. CLASS="php"
  27303. >return.php
  27304. <?php
  27305.  
  27306. $var = 'PHP';
  27307.  
  27308. return $var;
  27309.  
  27310. ?>
  27311.  
  27312. noreturn.php
  27313. <?php
  27314.  
  27315. $var = 'PHP';
  27316.  
  27317. ?>
  27318.  
  27319. testreturns.php
  27320. <?php
  27321.  
  27322. $foo = include 'return.php';
  27323.  
  27324. echo $foo; // prints 'PHP'
  27325.  
  27326. $bar = include 'noreturn.php';
  27327.  
  27328. echo $bar; // prints 1
  27329.  
  27330. ?></PRE
  27331. ></TD
  27332. ></TR
  27333. ></TABLE
  27334. ></DIV
  27335. ></TD
  27336. ></TR
  27337. ></TABLE
  27338. >
  27339.    </P
  27340. ><P
  27341. >     <VAR
  27342. CLASS="literal"
  27343. >$bar</VAR
  27344. > is the value <VAR
  27345. CLASS="literal"
  27346. >1</VAR
  27347. > because the include 
  27348.     was successful.  Notice the difference between the above examples.  The first uses 
  27349.     <A
  27350. HREF="#function.return"
  27351. ><B
  27352. CLASS="function"
  27353. >return()</B
  27354. ></A
  27355. > within the included file while the other does not.  
  27356.     If the file can't be included, <TT
  27357. CLASS="constant"
  27358. ><B
  27359. >FALSE</B
  27360. ></TT
  27361. > is returned and
  27362.     <VAR
  27363. CLASS="literal"
  27364. >E_WARNING</VAR
  27365. > is issued.
  27366.    </P
  27367. ><P
  27368. >     If there are functions defined in the included file, they can be used in the
  27369.     main file independent if they are before <A
  27370. HREF="#function.return"
  27371. ><B
  27372. CLASS="function"
  27373. >return()</B
  27374. ></A
  27375. > or after.
  27376.     If the file is included twice, PHP 5 issues fatal error because functions
  27377.     were already declared, while PHP 4 doesn't complain about functions
  27378.     defined after <A
  27379. HREF="#function.return"
  27380. ><B
  27381. CLASS="function"
  27382. >return()</B
  27383. ></A
  27384. >.
  27385.     It is recommended to use <A
  27386. HREF="#function.include-once"
  27387. ><B
  27388. CLASS="function"
  27389. >include_once()</B
  27390. ></A
  27391. > instead of
  27392.     checking if the file was already included and conditionally return inside
  27393.     the included file.
  27394.    </P
  27395. ><P
  27396. >     A few other ways to "include" files into variables are with 
  27397.     <A
  27398. HREF="#function.fopen"
  27399. ><B
  27400. CLASS="function"
  27401. >fopen()</B
  27402. ></A
  27403. >, <A
  27404. HREF="#function.file"
  27405. ><B
  27406. CLASS="function"
  27407. >file()</B
  27408. ></A
  27409. > or by using 
  27410.     <A
  27411. HREF="#function.include"
  27412. ><B
  27413. CLASS="function"
  27414. >include()</B
  27415. ></A
  27416. > along with 
  27417.     <A
  27418. HREF="#ref.outcontrol"
  27419. >Output Control Functions</A
  27420. >.
  27421.    </P
  27422. ><DIV
  27423. CLASS="note"
  27424. ><BLOCKQUOTE
  27425. CLASS="note"
  27426. ><P
  27427. ><B
  27428. >Note: </B
  27429. >Because this is a 
  27430. language construct and not a function, it cannot be called using 
  27431. <A
  27432. HREF="#functions.variable-functions"
  27433. >variable functions</A
  27434. ></P
  27435. ></BLOCKQUOTE
  27436. ></DIV
  27437. ><P
  27438. >     See also <A
  27439. HREF="#function.require"
  27440. ><B
  27441. CLASS="function"
  27442. >require()</B
  27443. ></A
  27444. >, <A
  27445. HREF="#function.require-once"
  27446. ><B
  27447. CLASS="function"
  27448. >require_once()</B
  27449. ></A
  27450. >,
  27451.     <A
  27452. HREF="#function.include-once"
  27453. ><B
  27454. CLASS="function"
  27455. >include_once()</B
  27456. ></A
  27457. >, <A
  27458. HREF="#function.readfile"
  27459. ><B
  27460. CLASS="function"
  27461. >readfile()</B
  27462. ></A
  27463. >,
  27464.     <A
  27465. HREF="#function.virtual"
  27466. ><B
  27467. CLASS="function"
  27468. >virtual()</B
  27469. ></A
  27470. >, and
  27471.     <A
  27472. HREF="#ini.include-path"
  27473. >include_path</A
  27474. >.
  27475.    </P
  27476. ></DIV
  27477. ><DIV
  27478. CLASS="sect1"
  27479. ><HR><H2
  27480. CLASS="sect1"
  27481. ><A
  27482. NAME="function.require-once"
  27483. ><A
  27484. HREF="#function.require-once"
  27485. ><B
  27486. CLASS="function"
  27487. >require_once()</B
  27488. ></A
  27489. ></A
  27490. ></H2
  27491. ><P
  27492. >     The <A
  27493. HREF="#function.require-once"
  27494. ><B
  27495. CLASS="function"
  27496. >require_once()</B
  27497. ></A
  27498. > statement includes and evaluates
  27499.     the specified file during the execution of the script.
  27500.     This is a behavior similar to the <A
  27501. HREF="#function.require"
  27502. ><B
  27503. CLASS="function"
  27504. >require()</B
  27505. ></A
  27506. > statement,
  27507.     with the only difference being that if the code from a file has already
  27508.     been included, it will not be included again.  See the documentation for
  27509.     <A
  27510. HREF="#function.require"
  27511. ><B
  27512. CLASS="function"
  27513. >require()</B
  27514. ></A
  27515. > for more information on how this statement 
  27516.     works.
  27517.    </P
  27518. ><P
  27519. >     <A
  27520. HREF="#function.require-once"
  27521. ><B
  27522. CLASS="function"
  27523. >require_once()</B
  27524. ></A
  27525. > should be used in cases where
  27526.     the same file might be included and evaluated more than once during a
  27527.     particular execution of a script, and you want to be sure that it is
  27528.     included exactly once to avoid problems with function redefinitions,
  27529.     variable value reassignments, etc.
  27530.    </P
  27531. ><P
  27532. >      For examples on using <A
  27533. HREF="#function.require-once"
  27534. ><B
  27535. CLASS="function"
  27536. >require_once()</B
  27537. ></A
  27538. > and
  27539.      <A
  27540. HREF="#function.include-once"
  27541. ><B
  27542. CLASS="function"
  27543. >include_once()</B
  27544. ></A
  27545. >, look at the 
  27546.      <A
  27547. HREF="http://pear.php.net/"
  27548. TARGET="_top"
  27549. >PEAR</A
  27550. > code included in the 
  27551.      latest PHP source code distributions.
  27552.    </P
  27553. ><P
  27554. >     Return values are the same as with <A
  27555. HREF="#function.include"
  27556. ><B
  27557. CLASS="function"
  27558. >include()</B
  27559. ></A
  27560. >. If the file
  27561.     was already included, this function returns <TT
  27562. CLASS="constant"
  27563. ><B
  27564. >TRUE</B
  27565. ></TT
  27566. >
  27567.    </P
  27568. ><P
  27569. >     <DIV
  27570. CLASS="note"
  27571. ><BLOCKQUOTE
  27572. CLASS="note"
  27573. ><P
  27574. ><B
  27575. >Note: </B
  27576. >
  27577.       <A
  27578. HREF="#function.require-once"
  27579. ><B
  27580. CLASS="function"
  27581. >require_once()</B
  27582. ></A
  27583. > was added in PHP 4.0.1pl2
  27584.      </P
  27585. ></BLOCKQUOTE
  27586. ></DIV
  27587. >
  27588.    </P
  27589. ><P
  27590. >     <DIV
  27591. CLASS="note"
  27592. ><BLOCKQUOTE
  27593. CLASS="note"
  27594. ><P
  27595. ><B
  27596. >Note: </B
  27597. >
  27598.       Be aware, that the behaviour of <A
  27599. HREF="#function.require-once"
  27600. ><B
  27601. CLASS="function"
  27602. >require_once()</B
  27603. ></A
  27604. >
  27605.       and <A
  27606. HREF="#function.include-once"
  27607. ><B
  27608. CLASS="function"
  27609. >include_once()</B
  27610. ></A
  27611. > may not be what you expect
  27612.       on a non case sensitive operating system (such as Windows).
  27613.       <TABLE
  27614. WIDTH="100%"
  27615. BORDER="0"
  27616. CELLPADDING="0"
  27617. CELLSPACING="0"
  27618. CLASS="EXAMPLE"
  27619. ><TR
  27620. ><TD
  27621. ><DIV
  27622. CLASS="example"
  27623. ><A
  27624. NAME="AEN4924"
  27625. ></A
  27626. ><P
  27627. ><B
  27628. >Example 16-11. <A
  27629. HREF="#function.require-once"
  27630. ><B
  27631. CLASS="function"
  27632. >require_once()</B
  27633. ></A
  27634. > is case insensitive on Windows</B
  27635. ></P
  27636. ><TABLE
  27637. BORDER="0"
  27638. BGCOLOR="#E0E0E0"
  27639. CELLPADDING="5"
  27640. ><TR
  27641. ><TD
  27642. ><PRE
  27643. CLASS="php"
  27644. ><?php
  27645. require_once("a.php"); // this will include a.php
  27646. require_once("A.php"); // this will include a.php again on Windows! (PHP 4 only)
  27647. ?></PRE
  27648. ></TD
  27649. ></TR
  27650. ></TABLE
  27651. ></DIV
  27652. ></TD
  27653. ></TR
  27654. ></TABLE
  27655. >
  27656.       This behaviour changed in PHP 5 - the path is normalized first so that
  27657.       <TT
  27658. CLASS="filename"
  27659. >C:\PROGRA~1\A.php</TT
  27660. > is realized the same as
  27661.       <TT
  27662. CLASS="filename"
  27663. >C:\Program Files\a.php</TT
  27664. > and the file is required just once.
  27665.      </P
  27666. ></BLOCKQUOTE
  27667. ></DIV
  27668. >
  27669.    </P
  27670. ><DIV
  27671. CLASS="warning"
  27672. ><P
  27673. ></P
  27674. ><TABLE
  27675. CLASS="warning"
  27676. BORDER="1"
  27677. WIDTH="100%"
  27678. ><TR
  27679. ><TD
  27680. ALIGN="CENTER"
  27681. ><B
  27682. >Warning</B
  27683. ></TD
  27684. ></TR
  27685. ><TR
  27686. ><TD
  27687. ALIGN="LEFT"
  27688. ><P
  27689. >Windows versions of PHP
  27690. prior to PHP 4.3.0 do not support accessing remote files via this function, even if
  27691. <A
  27692. HREF="#ini.allow-url-fopen"
  27693. >allow_url_fopen</A
  27694. > is enabled.
  27695. </P
  27696. ></TD
  27697. ></TR
  27698. ></TABLE
  27699. ></DIV
  27700. ><P
  27701. >     See also <A
  27702. HREF="#function.require"
  27703. ><B
  27704. CLASS="function"
  27705. >require()</B
  27706. ></A
  27707. >,
  27708.     <A
  27709. HREF="#function.include"
  27710. ><B
  27711. CLASS="function"
  27712. >include()</B
  27713. ></A
  27714. >, <A
  27715. HREF="#function.include-once"
  27716. ><B
  27717. CLASS="function"
  27718. >include_once()</B
  27719. ></A
  27720. >,
  27721.     <A
  27722. HREF="#function.get-required-files"
  27723. ><B
  27724. CLASS="function"
  27725. >get_required_files()</B
  27726. ></A
  27727. >,
  27728.     <A
  27729. HREF="#function.get-included-files"
  27730. ><B
  27731. CLASS="function"
  27732. >get_included_files()</B
  27733. ></A
  27734. >, <A
  27735. HREF="#function.readfile"
  27736. ><B
  27737. CLASS="function"
  27738. >readfile()</B
  27739. ></A
  27740. >, and 
  27741.     <A
  27742. HREF="#function.virtual"
  27743. ><B
  27744. CLASS="function"
  27745. >virtual()</B
  27746. ></A
  27747. >.
  27748.    </P
  27749. ></DIV
  27750. ><DIV
  27751. CLASS="sect1"
  27752. ><HR><H2
  27753. CLASS="sect1"
  27754. ><A
  27755. NAME="function.include-once"
  27756. ><A
  27757. HREF="#function.include-once"
  27758. ><B
  27759. CLASS="function"
  27760. >include_once()</B
  27761. ></A
  27762. ></A
  27763. ></H2
  27764. ><P
  27765. >     The <A
  27766. HREF="#function.include-once"
  27767. ><B
  27768. CLASS="function"
  27769. >include_once()</B
  27770. ></A
  27771. > statement includes and evaluates
  27772.     the specified file during the execution of the script.
  27773.     This is a behavior similar to the <A
  27774. HREF="#function.include"
  27775. ><B
  27776. CLASS="function"
  27777. >include()</B
  27778. ></A
  27779. > statement,
  27780.     with the only difference being that if the code from a file has already
  27781.     been included, it will not be included again.  As the name suggests, 
  27782.     it will be included just once.
  27783.    </P
  27784. ><P
  27785. >     <A
  27786. HREF="#function.include-once"
  27787. ><B
  27788. CLASS="function"
  27789. >include_once()</B
  27790. ></A
  27791. > should be used in cases where 
  27792.     the same file might be included and evaluated more than once during a
  27793.     particular execution of a script, and you want to be sure that it is
  27794.     included exactly once to avoid problems with function redefinitions,
  27795.     variable value reassignments, etc.
  27796.    </P
  27797. ><P
  27798. >     For more examples on using <A
  27799. HREF="#function.require-once"
  27800. ><B
  27801. CLASS="function"
  27802. >require_once()</B
  27803. ></A
  27804. > and
  27805.     <A
  27806. HREF="#function.include-once"
  27807. ><B
  27808. CLASS="function"
  27809. >include_once()</B
  27810. ></A
  27811. >, look at the
  27812.     <A
  27813. HREF="http://pear.php.net/"
  27814. TARGET="_top"
  27815. >PEAR</A
  27816. > code included in the latest
  27817.     PHP source code distributions.
  27818.    </P
  27819. ><P
  27820. >     Return values are the same as with <A
  27821. HREF="#function.include"
  27822. ><B
  27823. CLASS="function"
  27824. >include()</B
  27825. ></A
  27826. >. If the file
  27827.     was already included, this function returns <TT
  27828. CLASS="constant"
  27829. ><B
  27830. >TRUE</B
  27831. ></TT
  27832. >
  27833.    </P
  27834. ><P
  27835. >     <DIV
  27836. CLASS="note"
  27837. ><BLOCKQUOTE
  27838. CLASS="note"
  27839. ><P
  27840. ><B
  27841. >Note: </B
  27842. >
  27843.       <A
  27844. HREF="#function.include-once"
  27845. ><B
  27846. CLASS="function"
  27847. >include_once()</B
  27848. ></A
  27849. > was added in PHP 4.0.1pl2
  27850.      </P
  27851. ></BLOCKQUOTE
  27852. ></DIV
  27853. >
  27854.    </P
  27855. ><P
  27856. >    <DIV
  27857. CLASS="note"
  27858. ><BLOCKQUOTE
  27859. CLASS="note"
  27860. ><P
  27861. ><B
  27862. >Note: </B
  27863. >
  27864.       Be aware, that the behaviour of <A
  27865. HREF="#function.include-once"
  27866. ><B
  27867. CLASS="function"
  27868. >include_once()</B
  27869. ></A
  27870. >
  27871.       and <A
  27872. HREF="#function.require-once"
  27873. ><B
  27874. CLASS="function"
  27875. >require_once()</B
  27876. ></A
  27877. > may not be what you expect
  27878.       on a non case sensitive operating system (such as Windows).
  27879.       <TABLE
  27880. WIDTH="100%"
  27881. BORDER="0"
  27882. CELLPADDING="0"
  27883. CELLSPACING="0"
  27884. CLASS="EXAMPLE"
  27885. ><TR
  27886. ><TD
  27887. ><DIV
  27888. CLASS="example"
  27889. ><A
  27890. NAME="AEN4965"
  27891. ></A
  27892. ><P
  27893. ><B
  27894. >Example 16-12. <A
  27895. HREF="#function.include-once"
  27896. ><B
  27897. CLASS="function"
  27898. >include_once()</B
  27899. ></A
  27900. > is case insensitive on Windows</B
  27901. ></P
  27902. ><TABLE
  27903. BORDER="0"
  27904. BGCOLOR="#E0E0E0"
  27905. CELLPADDING="5"
  27906. ><TR
  27907. ><TD
  27908. ><PRE
  27909. CLASS="php"
  27910. ><?php
  27911. include_once("a.php"); // this will include a.php
  27912. include_once("A.php"); // this will include a.php again on Windows! (PHP 4 only)
  27913. ?></PRE
  27914. ></TD
  27915. ></TR
  27916. ></TABLE
  27917. ></DIV
  27918. ></TD
  27919. ></TR
  27920. ></TABLE
  27921. >
  27922.       This behaviour changed in PHP 5 - the path is normalized first so that
  27923.       <TT
  27924. CLASS="filename"
  27925. >C:\PROGRA~1\A.php</TT
  27926. > is realized the same as
  27927.       <TT
  27928. CLASS="filename"
  27929. >C:\Program Files\a.php</TT
  27930. > and the file is included just once.
  27931.      </P
  27932. ></BLOCKQUOTE
  27933. ></DIV
  27934. >
  27935.    </P
  27936. ><DIV
  27937. CLASS="warning"
  27938. ><P
  27939. ></P
  27940. ><TABLE
  27941. CLASS="warning"
  27942. BORDER="1"
  27943. WIDTH="100%"
  27944. ><TR
  27945. ><TD
  27946. ALIGN="CENTER"
  27947. ><B
  27948. >Warning</B
  27949. ></TD
  27950. ></TR
  27951. ><TR
  27952. ><TD
  27953. ALIGN="LEFT"
  27954. ><P
  27955. >Windows versions of PHP
  27956. prior to PHP 4.3.0 do not support accessing remote files via this function, even if
  27957. <A
  27958. HREF="#ini.allow-url-fopen"
  27959. >allow_url_fopen</A
  27960. > is enabled.
  27961. </P
  27962. ></TD
  27963. ></TR
  27964. ></TABLE
  27965. ></DIV
  27966. ><P
  27967. >     See also <A
  27968. HREF="#function.include"
  27969. ><B
  27970. CLASS="function"
  27971. >include()</B
  27972. ></A
  27973. >,
  27974.     <A
  27975. HREF="#function.require"
  27976. ><B
  27977. CLASS="function"
  27978. >require()</B
  27979. ></A
  27980. >, <A
  27981. HREF="#function.require-once"
  27982. ><B
  27983. CLASS="function"
  27984. >require_once()</B
  27985. ></A
  27986. >,
  27987.     <A
  27988. HREF="#function.get-required-files"
  27989. ><B
  27990. CLASS="function"
  27991. >get_required_files()</B
  27992. ></A
  27993. >,
  27994.     <A
  27995. HREF="#function.get-included-files"
  27996. ><B
  27997. CLASS="function"
  27998. >get_included_files()</B
  27999. ></A
  28000. >, <A
  28001. HREF="#function.readfile"
  28002. ><B
  28003. CLASS="function"
  28004. >readfile()</B
  28005. ></A
  28006. >,
  28007.     and <A
  28008. HREF="#function.virtual"
  28009. ><B
  28010. CLASS="function"
  28011. >virtual()</B
  28012. ></A
  28013. >.
  28014.    </P
  28015. ></DIV
  28016. ></DIV
  28017. ><DIV
  28018. CLASS="chapter"
  28019. ><HR><H1
  28020. ><A
  28021. NAME="language.functions"
  28022. >Chapter 17. Functions</A
  28023. ></H1
  28024. ><DIV
  28025. CLASS="sect1"
  28026. ><H2
  28027. CLASS="sect1"
  28028. ><A
  28029. NAME="functions.user-defined"
  28030. >User-defined functions</A
  28031. ></H2
  28032. ><P
  28033. >     A function may be defined using syntax such as the following:
  28034.    </P
  28035. ><P
  28036. >     <TABLE
  28037. WIDTH="100%"
  28038. BORDER="0"
  28039. CELLPADDING="0"
  28040. CELLSPACING="0"
  28041. CLASS="EXAMPLE"
  28042. ><TR
  28043. ><TD
  28044. ><DIV
  28045. CLASS="example"
  28046. ><A
  28047. NAME="AEN4988"
  28048. ></A
  28049. ><P
  28050. ><B
  28051. >Example 17-1. Pseudo code to demonstrate function uses</B
  28052. ></P
  28053. ><TABLE
  28054. BORDER="0"
  28055. BGCOLOR="#E0E0E0"
  28056. CELLPADDING="5"
  28057. ><TR
  28058. ><TD
  28059. ><PRE
  28060. CLASS="php"
  28061. ><?php
  28062. function foo($arg_1, $arg_2, /* ..., */ $arg_n)
  28063. {
  28064.     echo "Example function.\n";
  28065.     return $retval;
  28066. }
  28067. ?></PRE
  28068. ></TD
  28069. ></TR
  28070. ></TABLE
  28071. ></DIV
  28072. ></TD
  28073. ></TR
  28074. ></TABLE
  28075. >
  28076.    </P
  28077. ><P
  28078. >     Any valid PHP code may appear inside a function, even other
  28079.     functions and <A
  28080. HREF="#keyword.class"
  28081. >class</A
  28082. >
  28083.     definitions.
  28084.    </P
  28085. ><P
  28086. >     In PHP 3, functions must be defined before they are referenced. No
  28087.     such requirement exists since PHP 4. <SPAN
  28088. CLASS="emphasis"
  28089. ><I
  28090. CLASS="emphasis"
  28091. >Except</I
  28092. ></SPAN
  28093. > when
  28094.     a function is conditionally defined such as shown in the two examples
  28095.     below.
  28096.    </P
  28097. ><P
  28098. >     When a function is defined in a conditional manner such as the two
  28099.     examples shown.  Its definition must be processed <SPAN
  28100. CLASS="emphasis"
  28101. ><I
  28102. CLASS="emphasis"
  28103. >prior</I
  28104. ></SPAN
  28105. >
  28106.     to being called.
  28107.    </P
  28108. ><P
  28109. >     <TABLE
  28110. WIDTH="100%"
  28111. BORDER="0"
  28112. CELLPADDING="0"
  28113. CELLSPACING="0"
  28114. CLASS="EXAMPLE"
  28115. ><TR
  28116. ><TD
  28117. ><DIV
  28118. CLASS="example"
  28119. ><A
  28120. NAME="AEN4998"
  28121. ></A
  28122. ><P
  28123. ><B
  28124. >Example 17-2. Conditional functions</B
  28125. ></P
  28126. ><TABLE
  28127. BORDER="0"
  28128. BGCOLOR="#E0E0E0"
  28129. CELLPADDING="5"
  28130. ><TR
  28131. ><TD
  28132. ><PRE
  28133. CLASS="php"
  28134. ><?php
  28135.  
  28136. $makefoo = true;
  28137.  
  28138. /* We can't call foo() from here 
  28139.    since it doesn't exist yet,
  28140.    but we can call bar() */
  28141.  
  28142. bar();
  28143.  
  28144. if ($makefoo) {
  28145.   function foo()
  28146.   {
  28147.     echo "I don't exist until program execution reaches me.\n";
  28148.   }
  28149. }
  28150.  
  28151. /* Now we can safely call foo()
  28152.    since $makefoo evaluated to true */
  28153.  
  28154. if ($makefoo) foo();
  28155.  
  28156. function bar() 
  28157. {
  28158.   echo "I exist immediately upon program start.\n";
  28159. }
  28160.  
  28161. ?></PRE
  28162. ></TD
  28163. ></TR
  28164. ></TABLE
  28165. ></DIV
  28166. ></TD
  28167. ></TR
  28168. ></TABLE
  28169. >
  28170.    </P
  28171. ><P
  28172. >     <TABLE
  28173. WIDTH="100%"
  28174. BORDER="0"
  28175. CELLPADDING="0"
  28176. CELLSPACING="0"
  28177. CLASS="EXAMPLE"
  28178. ><TR
  28179. ><TD
  28180. ><DIV
  28181. CLASS="example"
  28182. ><A
  28183. NAME="AEN5002"
  28184. ></A
  28185. ><P
  28186. ><B
  28187. >Example 17-3. Functions within functions</B
  28188. ></P
  28189. ><TABLE
  28190. BORDER="0"
  28191. BGCOLOR="#E0E0E0"
  28192. CELLPADDING="5"
  28193. ><TR
  28194. ><TD
  28195. ><PRE
  28196. CLASS="php"
  28197. ><?php
  28198. function foo() 
  28199. {
  28200.   function bar() 
  28201.   {
  28202.     echo "I don't exist until foo() is called.\n";
  28203.   }
  28204. }
  28205.  
  28206. /* We can't call bar() yet
  28207.    since it doesn't exist. */
  28208.  
  28209. foo();
  28210.  
  28211. /* Now we can call bar(),
  28212.    foo()'s processesing has
  28213.    made it accessible. */
  28214.  
  28215. bar();
  28216.  
  28217. ?></PRE
  28218. ></TD
  28219. ></TR
  28220. ></TABLE
  28221. ></DIV
  28222. ></TD
  28223. ></TR
  28224. ></TABLE
  28225. >
  28226.    </P
  28227. ><P
  28228. >     PHP does not support function overloading, nor is it possible to
  28229.     undefine or redefine previously-declared functions.
  28230.    </P
  28231. ><DIV
  28232. CLASS="note"
  28233. ><BLOCKQUOTE
  28234. CLASS="note"
  28235. ><P
  28236. ><B
  28237. >Note: </B
  28238. >
  28239.      Function names are case-insensitive, though it is usually good form
  28240.      to call functions as they appear in their declaration.
  28241.     </P
  28242. ></BLOCKQUOTE
  28243. ></DIV
  28244. ><P
  28245. >     PHP 3 does not support variable numbers of arguments to functions,
  28246.     although default arguments are supported (see <A
  28247. HREF="#functions.arguments.default"
  28248. >Default argument
  28249.     values</A
  28250. > for more information). Both are supported, as of PHP 4: see <A
  28251. HREF="#functions.variable-arg-list"
  28252. >Variable-length argument
  28253.     lists</A
  28254. > and the function references for
  28255.     <A
  28256. HREF="#function.func-num-args"
  28257. ><B
  28258. CLASS="function"
  28259. >func_num_args()</B
  28260. ></A
  28261. >,
  28262.     <A
  28263. HREF="#function.func-get-arg"
  28264. ><B
  28265. CLASS="function"
  28266. >func_get_arg()</B
  28267. ></A
  28268. >, and
  28269.     <A
  28270. HREF="#function.func-get-args"
  28271. ><B
  28272. CLASS="function"
  28273. >func_get_args()</B
  28274. ></A
  28275. > for more information.
  28276.    </P
  28277. ></DIV
  28278. ><DIV
  28279. CLASS="sect1"
  28280. ><HR><H2
  28281. CLASS="sect1"
  28282. ><A
  28283. NAME="functions.arguments"
  28284. >Function arguments</A
  28285. ></H2
  28286. ><P
  28287. >     Information may be passed to functions via the argument list,
  28288.     which is a comma-delimited list of expressions.
  28289.    </P
  28290. ><P
  28291. >     PHP supports passing arguments by value (the default), <A
  28292. HREF="#functions.arguments.by-reference"
  28293. >passing by
  28294.     reference</A
  28295. >, and <A
  28296. HREF="#functions.arguments.default"
  28297. >default argument
  28298.     values</A
  28299. >. Variable-length argument lists are supported only
  28300.     in PHP 4 and later; see <A
  28301. HREF="#functions.variable-arg-list"
  28302. >Variable-length argument
  28303.     lists</A
  28304. > and the function references for
  28305.     <A
  28306. HREF="#function.func-num-args"
  28307. ><B
  28308. CLASS="function"
  28309. >func_num_args()</B
  28310. ></A
  28311. >,
  28312.     <A
  28313. HREF="#function.func-get-arg"
  28314. ><B
  28315. CLASS="function"
  28316. >func_get_arg()</B
  28317. ></A
  28318. >, and
  28319.     <A
  28320. HREF="#function.func-get-args"
  28321. ><B
  28322. CLASS="function"
  28323. >func_get_args()</B
  28324. ></A
  28325. > for more information. A
  28326.     similar effect can be achieved in PHP 3 by passing an array of
  28327.     arguments to a function:
  28328.    </P
  28329. ><P
  28330. >     <TABLE
  28331. WIDTH="100%"
  28332. BORDER="0"
  28333. CELLPADDING="0"
  28334. CELLSPACING="0"
  28335. CLASS="EXAMPLE"
  28336. ><TR
  28337. ><TD
  28338. ><DIV
  28339. CLASS="example"
  28340. ><A
  28341. NAME="AEN5025"
  28342. ></A
  28343. ><P
  28344. ><B
  28345. >Example 17-4. Passing arrays to functions</B
  28346. ></P
  28347. ><TABLE
  28348. BORDER="0"
  28349. BGCOLOR="#E0E0E0"
  28350. CELLPADDING="5"
  28351. ><TR
  28352. ><TD
  28353. ><PRE
  28354. CLASS="php"
  28355. ><?php
  28356. function takes_array($input)
  28357. {
  28358.     echo "$input[0] + $input[1] = ", $input[0]+$input[1];
  28359. }
  28360. ?></PRE
  28361. ></TD
  28362. ></TR
  28363. ></TABLE
  28364. ></DIV
  28365. ></TD
  28366. ></TR
  28367. ></TABLE
  28368. >
  28369.    </P
  28370. ><DIV
  28371. CLASS="sect2"
  28372. ><HR><H3
  28373. CLASS="sect2"
  28374. ><A
  28375. NAME="functions.arguments.by-reference"
  28376. >Making arguments be passed by reference</A
  28377. ></H3
  28378. ><P
  28379. >      By default, function arguments are passed by value (so that if
  28380.      you change the value of the argument within the function, it does
  28381.      not get changed outside of the function). If you wish to allow a
  28382.      function to modify its arguments, you must pass them by
  28383.      reference.
  28384.     </P
  28385. ><P
  28386. >      If you want an argument to a function to always be passed by
  28387.      reference, you can prepend an ampersand (&) to the argument
  28388.      name in the function definition:
  28389.     </P
  28390. ><P
  28391. >      <TABLE
  28392. WIDTH="100%"
  28393. BORDER="0"
  28394. CELLPADDING="0"
  28395. CELLSPACING="0"
  28396. CLASS="EXAMPLE"
  28397. ><TR
  28398. ><TD
  28399. ><DIV
  28400. CLASS="example"
  28401. ><A
  28402. NAME="AEN5033"
  28403. ></A
  28404. ><P
  28405. ><B
  28406. >Example 17-5. Passing function parameters by reference</B
  28407. ></P
  28408. ><TABLE
  28409. BORDER="0"
  28410. BGCOLOR="#E0E0E0"
  28411. CELLPADDING="5"
  28412. ><TR
  28413. ><TD
  28414. ><PRE
  28415. CLASS="php"
  28416. ><?php
  28417. function add_some_extra(&$string)
  28418. {
  28419.     $string .= 'and something extra.';
  28420. }
  28421. $str = 'This is a string, ';
  28422. add_some_extra($str);
  28423. echo $str;    // outputs 'This is a string, and something extra.'
  28424. ?></PRE
  28425. ></TD
  28426. ></TR
  28427. ></TABLE
  28428. ></DIV
  28429. ></TD
  28430. ></TR
  28431. ></TABLE
  28432. >
  28433.     </P
  28434. ></DIV
  28435. ><DIV
  28436. CLASS="sect2"
  28437. ><HR><H3
  28438. CLASS="sect2"
  28439. ><A
  28440. NAME="functions.arguments.default"
  28441. >Default argument values</A
  28442. ></H3
  28443. ><P
  28444. >      A function may define C++-style default values for scalar
  28445.      arguments as follows:
  28446.     </P
  28447. ><P
  28448. >      <TABLE
  28449. WIDTH="100%"
  28450. BORDER="0"
  28451. CELLPADDING="0"
  28452. CELLSPACING="0"
  28453. CLASS="EXAMPLE"
  28454. ><TR
  28455. ><TD
  28456. ><DIV
  28457. CLASS="example"
  28458. ><A
  28459. NAME="AEN5040"
  28460. ></A
  28461. ><P
  28462. ><B
  28463. >Example 17-6. Use of default parameters in functions</B
  28464. ></P
  28465. ><TABLE
  28466. BORDER="0"
  28467. BGCOLOR="#E0E0E0"
  28468. CELLPADDING="5"
  28469. ><TR
  28470. ><TD
  28471. ><PRE
  28472. CLASS="php"
  28473. ><?php
  28474. function makecoffee($type = "cappuccino")
  28475. {
  28476.     return "Making a cup of $type.\n";
  28477. }
  28478. echo makecoffee();
  28479. echo makecoffee("espresso");
  28480. ?></PRE
  28481. ></TD
  28482. ></TR
  28483. ></TABLE
  28484. ></DIV
  28485. ></TD
  28486. ></TR
  28487. ></TABLE
  28488. >
  28489.     </P
  28490. ><P
  28491. >      The output from the above snippet is:
  28492.     </P
  28493. ><P
  28494. >      <TABLE
  28495. BORDER="0"
  28496. BGCOLOR="#E0E0E0"
  28497. CELLPADDING="5"
  28498. ><TR
  28499. ><TD
  28500. ><PRE
  28501. CLASS="screen"
  28502. >Making a cup of cappuccino.
  28503. Making a cup of espresso.</PRE
  28504. ></TD
  28505. ></TR
  28506. ></TABLE
  28507. >
  28508.     </P
  28509. ><P
  28510. >      Also PHP allows you to use arrays and special type NULL as  
  28511.      default values, for example:
  28512.     </P
  28513. ><P
  28514. >      <TABLE
  28515. WIDTH="100%"
  28516. BORDER="0"
  28517. CELLPADDING="0"
  28518. CELLSPACING="0"
  28519. CLASS="EXAMPLE"
  28520. ><TR
  28521. ><TD
  28522. ><DIV
  28523. CLASS="example"
  28524. ><A
  28525. NAME="AEN5048"
  28526. ></A
  28527. ><P
  28528. ><B
  28529. >Example 17-7. Using non-scalar types as default values</B
  28530. ></P
  28531. ><TABLE
  28532. BORDER="0"
  28533. BGCOLOR="#E0E0E0"
  28534. CELLPADDING="5"
  28535. ><TR
  28536. ><TD
  28537. ><PRE
  28538. CLASS="php"
  28539. ><?php
  28540. function makecoffee($types = array("cappuccino"), $coffeeMaker = NULL)
  28541. {
  28542.     $device = is_null($coffeeMaker) ? "hands" : $coffeeMaker;
  28543.     return "Making a cup of ".join(", ", $types)." with $device.\n";
  28544. }
  28545. echo makecoffee();
  28546. echo makecoffee(array("cappuccino", "lavazza"), "teapot");
  28547. ?></PRE
  28548. ></TD
  28549. ></TR
  28550. ></TABLE
  28551. ></DIV
  28552. ></TD
  28553. ></TR
  28554. ></TABLE
  28555. >
  28556.     
  28557.     </P
  28558. ><P
  28559. >      The default value must be a constant expression, not (for
  28560.      example) a variable, a class member or a function call.
  28561.     </P
  28562. ><P
  28563. >      Note that when using default arguments, any defaults should be on
  28564.      the right side of any non-default arguments; otherwise, things
  28565.      will not work as expected. Consider the following code snippet:
  28566.     </P
  28567. ><P
  28568. >      <TABLE
  28569. WIDTH="100%"
  28570. BORDER="0"
  28571. CELLPADDING="0"
  28572. CELLSPACING="0"
  28573. CLASS="EXAMPLE"
  28574. ><TR
  28575. ><TD
  28576. ><DIV
  28577. CLASS="example"
  28578. ><A
  28579. NAME="AEN5054"
  28580. ></A
  28581. ><P
  28582. ><B
  28583. >Example 17-8. Incorrect usage of default function arguments</B
  28584. ></P
  28585. ><TABLE
  28586. BORDER="0"
  28587. BGCOLOR="#E0E0E0"
  28588. CELLPADDING="5"
  28589. ><TR
  28590. ><TD
  28591. ><PRE
  28592. CLASS="php"
  28593. ><?php
  28594. function makeyogurt($type = "acidophilus", $flavour)
  28595. {
  28596.     return "Making a bowl of $type $flavour.\n";
  28597. }
  28598.  
  28599. echo makeyogurt("raspberry");   // won't work as expected
  28600. ?></PRE
  28601. ></TD
  28602. ></TR
  28603. ></TABLE
  28604. ></DIV
  28605. ></TD
  28606. ></TR
  28607. ></TABLE
  28608. >
  28609.     </P
  28610. ><P
  28611. >      The output of the above example is:
  28612.     </P
  28613. ><P
  28614. >      <TABLE
  28615. BORDER="0"
  28616. BGCOLOR="#E0E0E0"
  28617. CELLPADDING="5"
  28618. ><TR
  28619. ><TD
  28620. ><PRE
  28621. CLASS="screen"
  28622. >Warning: Missing argument 2 in call to makeyogurt() in 
  28623. /usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
  28624. Making a bowl of raspberry .</PRE
  28625. ></TD
  28626. ></TR
  28627. ></TABLE
  28628. >
  28629.     </P
  28630. ><P
  28631. >      Now, compare the above with this:
  28632.     </P
  28633. ><P
  28634. >      <TABLE
  28635. WIDTH="100%"
  28636. BORDER="0"
  28637. CELLPADDING="0"
  28638. CELLSPACING="0"
  28639. CLASS="EXAMPLE"
  28640. ><TR
  28641. ><TD
  28642. ><DIV
  28643. CLASS="example"
  28644. ><A
  28645. NAME="AEN5062"
  28646. ></A
  28647. ><P
  28648. ><B
  28649. >Example 17-9. Correct usage of default function arguments</B
  28650. ></P
  28651. ><TABLE
  28652. BORDER="0"
  28653. BGCOLOR="#E0E0E0"
  28654. CELLPADDING="5"
  28655. ><TR
  28656. ><TD
  28657. ><PRE
  28658. CLASS="php"
  28659. ><?php
  28660. function makeyogurt($flavour, $type = "acidophilus")
  28661. {
  28662.     return "Making a bowl of $type $flavour.\n";
  28663. }
  28664.  
  28665. echo makeyogurt("raspberry");   // works as expected
  28666. ?></PRE
  28667. ></TD
  28668. ></TR
  28669. ></TABLE
  28670. ></DIV
  28671. ></TD
  28672. ></TR
  28673. ></TABLE
  28674. >
  28675.     </P
  28676. ><P
  28677. >      The output of this example is:
  28678.     </P
  28679. ><P
  28680. >      <TABLE
  28681. BORDER="0"
  28682. BGCOLOR="#E0E0E0"
  28683. CELLPADDING="5"
  28684. ><TR
  28685. ><TD
  28686. ><PRE
  28687. CLASS="screen"
  28688. >Making a bowl of acidophilus raspberry.</PRE
  28689. ></TD
  28690. ></TR
  28691. ></TABLE
  28692. >
  28693.     </P
  28694. ><DIV
  28695. CLASS="note"
  28696. ><BLOCKQUOTE
  28697. CLASS="note"
  28698. ><P
  28699. ><B
  28700. >Note: </B
  28701. >
  28702.       As of PHP 5, default values may be passed by reference.      
  28703.      </P
  28704. ></BLOCKQUOTE
  28705. ></DIV
  28706. ></DIV
  28707. ><DIV
  28708. CLASS="sect2"
  28709. ><HR><H3
  28710. CLASS="sect2"
  28711. ><A
  28712. NAME="functions.variable-arg-list"
  28713. >Variable-length argument lists</A
  28714. ></H3
  28715. ><P
  28716. >      PHP 4 and above has support for variable-length argument lists in
  28717.      user-defined functions. This is really quite easy, using the
  28718.      <A
  28719. HREF="#function.func-num-args"
  28720. ><B
  28721. CLASS="function"
  28722. >func_num_args()</B
  28723. ></A
  28724. >,
  28725.      <A
  28726. HREF="#function.func-get-arg"
  28727. ><B
  28728. CLASS="function"
  28729. >func_get_arg()</B
  28730. ></A
  28731. >, and
  28732.      <A
  28733. HREF="#function.func-get-args"
  28734. ><B
  28735. CLASS="function"
  28736. >func_get_args()</B
  28737. ></A
  28738. > functions.
  28739.     </P
  28740. ><P
  28741. >      No special syntax is required, and argument lists may still be
  28742.      explicitly provided with function definitions and will behave as
  28743.      normal.
  28744.     </P
  28745. ></DIV
  28746. ></DIV
  28747. ><DIV
  28748. CLASS="sect1"
  28749. ><HR><H2
  28750. CLASS="sect1"
  28751. ><A
  28752. NAME="functions.returning-values"
  28753. >Returning values</A
  28754. ></H2
  28755. ><P
  28756. >     Values are returned by using the optional return statement. Any
  28757.     type may be returned, including lists and objects. This causes the
  28758.     function to end its execution immediately and pass control back to
  28759.     the line from which it was called. See <A
  28760. HREF="#function.return"
  28761. ><B
  28762. CLASS="function"
  28763. >return()</B
  28764. ></A
  28765. >
  28766.     for more information.
  28767.    </P
  28768. ><P
  28769. >     <TABLE
  28770. WIDTH="100%"
  28771. BORDER="0"
  28772. CELLPADDING="0"
  28773. CELLSPACING="0"
  28774. CLASS="EXAMPLE"
  28775. ><TR
  28776. ><TD
  28777. ><DIV
  28778. CLASS="example"
  28779. ><A
  28780. NAME="AEN5082"
  28781. ></A
  28782. ><P
  28783. ><B
  28784. >Example 17-10. Use of <A
  28785. HREF="#function.return"
  28786. ><B
  28787. CLASS="function"
  28788. >return()</B
  28789. ></A
  28790. ></B
  28791. ></P
  28792. ><TABLE
  28793. BORDER="0"
  28794. BGCOLOR="#E0E0E0"
  28795. CELLPADDING="5"
  28796. ><TR
  28797. ><TD
  28798. ><PRE
  28799. CLASS="php"
  28800. ><?php
  28801. function square($num)
  28802. {
  28803.     return $num * $num;
  28804. }
  28805. echo square(4);   // outputs '16'.
  28806. ?></PRE
  28807. ></TD
  28808. ></TR
  28809. ></TABLE
  28810. ></DIV
  28811. ></TD
  28812. ></TR
  28813. ></TABLE
  28814. >
  28815.    </P
  28816. ><P
  28817. >     You can't return multiple values from a function, but similar
  28818.     results can be obtained by returning a list.
  28819.    </P
  28820. ><P
  28821. >     <TABLE
  28822. WIDTH="100%"
  28823. BORDER="0"
  28824. CELLPADDING="0"
  28825. CELLSPACING="0"
  28826. CLASS="EXAMPLE"
  28827. ><TR
  28828. ><TD
  28829. ><DIV
  28830. CLASS="example"
  28831. ><A
  28832. NAME="AEN5088"
  28833. ></A
  28834. ><P
  28835. ><B
  28836. >Example 17-11. Returning an array to get multiple values</B
  28837. ></P
  28838. ><TABLE
  28839. BORDER="0"
  28840. BGCOLOR="#E0E0E0"
  28841. CELLPADDING="5"
  28842. ><TR
  28843. ><TD
  28844. ><PRE
  28845. CLASS="php"
  28846. ><?php
  28847. function small_numbers()
  28848. {
  28849.     return array (0, 1, 2);
  28850. }
  28851. list ($zero, $one, $two) = small_numbers();
  28852. ?></PRE
  28853. ></TD
  28854. ></TR
  28855. ></TABLE
  28856. ></DIV
  28857. ></TD
  28858. ></TR
  28859. ></TABLE
  28860. >
  28861.    </P
  28862. ><P
  28863. >     To return a reference from a function, you have to use
  28864.     the reference operator & in both the function declaration and
  28865.     when assigning the returned value to a variable:
  28866.    </P
  28867. ><P
  28868. >     <TABLE
  28869. WIDTH="100%"
  28870. BORDER="0"
  28871. CELLPADDING="0"
  28872. CELLSPACING="0"
  28873. CLASS="EXAMPLE"
  28874. ><TR
  28875. ><TD
  28876. ><DIV
  28877. CLASS="example"
  28878. ><A
  28879. NAME="AEN5093"
  28880. ></A
  28881. ><P
  28882. ><B
  28883. >Example 17-12. Returning a reference from a function</B
  28884. ></P
  28885. ><TABLE
  28886. BORDER="0"
  28887. BGCOLOR="#E0E0E0"
  28888. CELLPADDING="5"
  28889. ><TR
  28890. ><TD
  28891. ><PRE
  28892. CLASS="php"
  28893. ><?php
  28894. function &returns_reference()
  28895. {
  28896.     return $someref;
  28897. }
  28898.  
  28899. $newref =& returns_reference();
  28900. ?></PRE
  28901. ></TD
  28902. ></TR
  28903. ></TABLE
  28904. ></DIV
  28905. ></TD
  28906. ></TR
  28907. ></TABLE
  28908. >
  28909.    </P
  28910. ><P
  28911. >     For more information on references, please check out <A
  28912. HREF="#language.references"
  28913. >References Explained</A
  28914. >.
  28915.    </P
  28916. ></DIV
  28917. ><DIV
  28918. CLASS="sect1"
  28919. ><HR><H2
  28920. CLASS="sect1"
  28921. ><A
  28922. NAME="functions.variable-functions"
  28923. >Variable functions</A
  28924. ></H2
  28925. ><P
  28926. >     PHP supports the concept of variable functions. This means that if
  28927.     a variable name has parentheses appended to it, PHP will look for
  28928.     a function with the same name as whatever the variable evaluates
  28929.     to, and will attempt to execute it. Among other things, this can
  28930.     be used to implement callbacks, function tables, and so forth.
  28931.    </P
  28932. ><P
  28933. >     Variable functions won't work with language constructs such 
  28934.     as <A
  28935. HREF="#function.echo"
  28936. ><B
  28937. CLASS="function"
  28938. >echo()</B
  28939. ></A
  28940. >, <A
  28941. HREF="#function.print"
  28942. ><B
  28943. CLASS="function"
  28944. >print()</B
  28945. ></A
  28946. >,
  28947.     <A
  28948. HREF="#function.unset"
  28949. ><B
  28950. CLASS="function"
  28951. >unset()</B
  28952. ></A
  28953. >, <A
  28954. HREF="#function.isset"
  28955. ><B
  28956. CLASS="function"
  28957. >isset()</B
  28958. ></A
  28959. >,
  28960.     <A
  28961. HREF="#function.empty"
  28962. ><B
  28963. CLASS="function"
  28964. >empty()</B
  28965. ></A
  28966. >, <A
  28967. HREF="#function.include"
  28968. ><B
  28969. CLASS="function"
  28970. >include()</B
  28971. ></A
  28972. >,
  28973.     <A
  28974. HREF="#function.require"
  28975. ><B
  28976. CLASS="function"
  28977. >require()</B
  28978. ></A
  28979. > and the like. You need to use
  28980.     your own wrapper function to utilize any of these constructs
  28981.     as variable functions.
  28982.    </P
  28983. ><P
  28984. >     <TABLE
  28985. WIDTH="100%"
  28986. BORDER="0"
  28987. CELLPADDING="0"
  28988. CELLSPACING="0"
  28989. CLASS="EXAMPLE"
  28990. ><TR
  28991. ><TD
  28992. ><DIV
  28993. CLASS="example"
  28994. ><A
  28995. NAME="AEN5110"
  28996. ></A
  28997. ><P
  28998. ><B
  28999. >Example 17-13. Variable function example</B
  29000. ></P
  29001. ><TABLE
  29002. BORDER="0"
  29003. BGCOLOR="#E0E0E0"
  29004. CELLPADDING="5"
  29005. ><TR
  29006. ><TD
  29007. ><PRE
  29008. CLASS="php"
  29009. ><?php
  29010. function foo() {
  29011.     echo "In foo()<br />\n";
  29012. }
  29013.  
  29014. function bar($arg = '')
  29015. {
  29016.     echo "In bar(); argument was '$arg'.<br />\n";
  29017. }
  29018.  
  29019. // This is a wrapper function around echo
  29020. function echoit($string)
  29021. {
  29022.     echo $string;
  29023. }
  29024.  
  29025. $func = 'foo';
  29026. $func();        // This calls foo()
  29027.  
  29028. $func = 'bar';
  29029. $func('test');  // This calls bar()
  29030.  
  29031. $func = 'echoit';
  29032. $func('test');  // This calls echoit()
  29033. ?></PRE
  29034. ></TD
  29035. ></TR
  29036. ></TABLE
  29037. ></DIV
  29038. ></TD
  29039. ></TR
  29040. ></TABLE
  29041. >
  29042.    </P
  29043. ><P
  29044. >     You can also call an object's method by using the variable functions
  29045.     feature.
  29046.     <TABLE
  29047. WIDTH="100%"
  29048. BORDER="0"
  29049. CELLPADDING="0"
  29050. CELLSPACING="0"
  29051. CLASS="EXAMPLE"
  29052. ><TR
  29053. ><TD
  29054. ><DIV
  29055. CLASS="example"
  29056. ><A
  29057. NAME="AEN5114"
  29058. ></A
  29059. ><P
  29060. ><B
  29061. >Example 17-14. Variable method example</B
  29062. ></P
  29063. ><TABLE
  29064. BORDER="0"
  29065. BGCOLOR="#E0E0E0"
  29066. CELLPADDING="5"
  29067. ><TR
  29068. ><TD
  29069. ><PRE
  29070. CLASS="php"
  29071. ><?php
  29072. class Foo
  29073. {
  29074.     function Variable()
  29075.     {
  29076.         $name = 'Bar';
  29077.         $this->$name(); // This calls the Bar() method
  29078.     }
  29079.     
  29080.     function Bar()
  29081.     {
  29082.         echo "This is Bar";
  29083.     }
  29084. }
  29085.  
  29086. $foo = new Foo();
  29087. $funcname = "Variable";
  29088. $foo->$funcname();  // This calls $foo->Variable()
  29089.  
  29090. ?></PRE
  29091. ></TD
  29092. ></TR
  29093. ></TABLE
  29094. ></DIV
  29095. ></TD
  29096. ></TR
  29097. ></TABLE
  29098. >
  29099.    </P
  29100. ><P
  29101. >     See also <A
  29102. HREF="#function.call-user-func"
  29103. ><B
  29104. CLASS="function"
  29105. >call_user_func()</B
  29106. ></A
  29107. >,
  29108.     <A
  29109. HREF="#language.variables.variable"
  29110. >     variable variables</A
  29111. > and <A
  29112. HREF="#function.function-exists"
  29113. ><B
  29114. CLASS="function"
  29115. >function_exists()</B
  29116. ></A
  29117. >.
  29118.    </P
  29119. ></DIV
  29120. ><DIV
  29121. CLASS="sect1"
  29122. ><HR><H2
  29123. CLASS="sect1"
  29124. ><A
  29125. NAME="functions.internal"
  29126. >Internal (built-in) functions</A
  29127. ></H2
  29128. ><P
  29129. >     PHP comes standard with many functions and constructs.  There are also
  29130.     functions that require specific PHP extensions compiled in otherwise 
  29131.     you'll get fatal "undefined function" errors.  For example, to use 
  29132.     <A
  29133. HREF="#ref.image"
  29134. >image</A
  29135. > functions such as 
  29136.     <A
  29137. HREF="#function.imagecreatetruecolor"
  29138. ><B
  29139. CLASS="function"
  29140. >imagecreatetruecolor()</B
  29141. ></A
  29142. >, you'll need your PHP compiled
  29143.     with <SPAN
  29144. CLASS="productname"
  29145. >GD</SPAN
  29146. > support.  Or, to use <A
  29147. HREF="#function.mysql-connect"
  29148. ><B
  29149. CLASS="function"
  29150. >mysql_connect()</B
  29151. ></A
  29152. > you'll
  29153.     need your PHP compiled in with <A
  29154. HREF="#ref.mysql"
  29155. >MySQL</A
  29156. >
  29157.     support.  There are many core functions that are included in every
  29158.     version of PHP like the <A
  29159. HREF="#ref.strings"
  29160. >string</A
  29161. > and 
  29162.     <A
  29163. HREF="#ref.var"
  29164. >variable</A
  29165. > functions.  A call 
  29166.     to <A
  29167. HREF="#function.phpinfo"
  29168. ><B
  29169. CLASS="function"
  29170. >phpinfo()</B
  29171. ></A
  29172. > or
  29173.     <A
  29174. HREF="#function.get-loaded-extensions"
  29175. ><B
  29176. CLASS="function"
  29177. >get_loaded_extensions()</B
  29178. ></A
  29179. > will show you which
  29180.     extensions are loaded into your PHP.  Also note that many extensions are
  29181.     enabled by default and that the PHP manual is split up by extension.
  29182.     See the <A
  29183. HREF="#configuration"
  29184. >configuration</A
  29185. >,  
  29186.     <A
  29187. HREF="#install"
  29188. >installation</A
  29189. >, and individual
  29190.     extension chapters, for information on how to setup your PHP.
  29191.    </P
  29192. ><P
  29193. >     Reading and understanding a function's prototype is explained within the
  29194.     manual section titled 
  29195.     <A
  29196. HREF="#about.prototypes"
  29197. >how to read a function definition</A
  29198. >.
  29199.     It's important to realize what a function returns or if a function works
  29200.     directly on a passed in value.  For example,
  29201.     <A
  29202. HREF="#function.str-replace"
  29203. ><B
  29204. CLASS="function"
  29205. >str_replace()</B
  29206. ></A
  29207. > will return the modified string while 
  29208.     <A
  29209. HREF="#function.usort"
  29210. ><B
  29211. CLASS="function"
  29212. >usort()</B
  29213. ></A
  29214. > works on the actual passed in variable
  29215.     itself.  Each manual page also has specific information for each
  29216.     function like information on function parameters, behavior changes,
  29217.     return values for both success and failure, and availability information.
  29218.     Knowing these important (yet often subtle) differences is crucial for
  29219.     writing correct PHP code.
  29220.    </P
  29221. ><P
  29222. >     See also <A
  29223. HREF="#function.function-exists"
  29224. ><B
  29225. CLASS="function"
  29226. >function_exists()</B
  29227. ></A
  29228. >, 
  29229.     <A
  29230. HREF="#funcref"
  29231. >the function reference</A
  29232. >,
  29233.     <A
  29234. HREF="#function.get-extension-funcs"
  29235. ><B
  29236. CLASS="function"
  29237. >get_extension_funcs()</B
  29238. ></A
  29239. >, and 
  29240.     <A
  29241. HREF="#function.dl"
  29242. ><B
  29243. CLASS="function"
  29244. >dl()</B
  29245. ></A
  29246. >.
  29247.    </P
  29248. ></DIV
  29249. ></DIV
  29250. ><DIV
  29251. CLASS="chapter"
  29252. ><HR><H1
  29253. ><A
  29254. NAME="language.oop"
  29255. >Chapter 18. Classes and Objects (PHP 4)</A
  29256. ></H1
  29257. ><DIV
  29258. CLASS="sect1"
  29259. ><H2
  29260. CLASS="sect1"
  29261. ><A
  29262. NAME="keyword.class"
  29263. ><VAR
  29264. CLASS="literal"
  29265. >class</VAR
  29266. ></A
  29267. ></H2
  29268. ><P
  29269. >     A class is a collection of variables and functions working with
  29270.     these variables.  A class is defined using the following syntax:
  29271.    </P
  29272. ><P
  29273. >     <DIV
  29274. CLASS="informalexample"
  29275. ><P
  29276. ></P
  29277. ><A
  29278. NAME="AEN5151"
  29279. ></A
  29280. ><TABLE
  29281. BORDER="0"
  29282. BGCOLOR="#E0E0E0"
  29283. CELLPADDING="5"
  29284. ><TR
  29285. ><TD
  29286. ><PRE
  29287. CLASS="php"
  29288. ><?php
  29289. class Cart {
  29290.     var $items;  // Items in our shopping cart
  29291.  
  29292.     // Add $num articles of $artnr to the cart
  29293.  
  29294.     function add_item($artnr, $num) {
  29295.         $this->items[$artnr] += $num;
  29296.     }
  29297.  
  29298.     // Take $num articles of $artnr out of the cart
  29299.  
  29300.     function remove_item($artnr, $num) {
  29301.         if ($this->items[$artnr] > $num) {
  29302.             $this->items[$artnr] -= $num;
  29303.             return true;
  29304.         } elseif ($this->items[$artnr] == $num) {
  29305.             unset($this->items[$artnr]);
  29306.             return true;
  29307.         } else {
  29308.             return false;
  29309.         }
  29310.     }
  29311. }
  29312. ?></PRE
  29313. ></TD
  29314. ></TR
  29315. ></TABLE
  29316. ><P
  29317. ></P
  29318. ></DIV
  29319. >
  29320.    </P
  29321. ><P
  29322. >     This defines a class named Cart that consists of an associative
  29323.     array of articles in the cart and two functions to add and remove
  29324.     items from this cart.
  29325.    </P
  29326. ><DIV
  29327. CLASS="warning"
  29328. ><P
  29329. ></P
  29330. ><TABLE
  29331. CLASS="warning"
  29332. BORDER="1"
  29333. WIDTH="100%"
  29334. ><TR
  29335. ><TD
  29336. ALIGN="CENTER"
  29337. ><B
  29338. >Warning</B
  29339. ></TD
  29340. ></TR
  29341. ><TR
  29342. ><TD
  29343. ALIGN="LEFT"
  29344. ><P
  29345. >      You can <SPAN
  29346. CLASS="emphasis"
  29347. ><I
  29348. CLASS="emphasis"
  29349. >NOT</I
  29350. ></SPAN
  29351. > break up a class definition into
  29352.      multiple files. You also can <SPAN
  29353. CLASS="emphasis"
  29354. ><I
  29355. CLASS="emphasis"
  29356. >NOT</I
  29357. ></SPAN
  29358. > break a class
  29359.      definition into multiple PHP blocks, unless the break is within a method
  29360.      declaration. The following will not work:
  29361.     </P
  29362. ><P
  29363. >      <DIV
  29364. CLASS="informalexample"
  29365. ><P
  29366. ></P
  29367. ><A
  29368. NAME="AEN5159"
  29369. ></A
  29370. ><TABLE
  29371. BORDER="0"
  29372. BGCOLOR="#E0E0E0"
  29373. CELLPADDING="5"
  29374. ><TR
  29375. ><TD
  29376. ><PRE
  29377. CLASS="php"
  29378. ><?php
  29379. class test {
  29380. ?>
  29381. <?php
  29382.     function test() {
  29383.         print 'OK';
  29384.     }
  29385. }
  29386. ?></PRE
  29387. ></TD
  29388. ></TR
  29389. ></TABLE
  29390. ><P
  29391. ></P
  29392. ></DIV
  29393. >
  29394.     </P
  29395. ><P
  29396. >      However, the following is allowed:
  29397.     </P
  29398. ><P
  29399. >      <DIV
  29400. CLASS="informalexample"
  29401. ><P
  29402. ></P
  29403. ><A
  29404. NAME="AEN5163"
  29405. ></A
  29406. ><TABLE
  29407. BORDER="0"
  29408. BGCOLOR="#E0E0E0"
  29409. CELLPADDING="5"
  29410. ><TR
  29411. ><TD
  29412. ><PRE
  29413. CLASS="php"
  29414. ><?php
  29415. class test {
  29416.     function test() {
  29417.         ?>
  29418.         <?php
  29419.         print 'OK';
  29420.     }
  29421. }
  29422. ?></PRE
  29423. ></TD
  29424. ></TR
  29425. ></TABLE
  29426. ><P
  29427. ></P
  29428. ></DIV
  29429. >
  29430.     </P
  29431. ></TD
  29432. ></TR
  29433. ></TABLE
  29434. ></DIV
  29435. ><P
  29436. >     The following cautionary notes are valid for PHP 4.
  29437.    </P
  29438. ><DIV
  29439. CLASS="caution"
  29440. ><P
  29441. ></P
  29442. ><TABLE
  29443. CLASS="caution"
  29444. BORDER="1"
  29445. WIDTH="100%"
  29446. ><TR
  29447. ><TD
  29448. ALIGN="CENTER"
  29449. ><B
  29450. >Caution</B
  29451. ></TD
  29452. ></TR
  29453. ><TR
  29454. ><TD
  29455. ALIGN="LEFT"
  29456. ><P
  29457. >      The name <VAR
  29458. CLASS="literal"
  29459. >stdClass</VAR
  29460. > is used interally by
  29461.      Zend and is reserved. You cannot have a class named
  29462.      <VAR
  29463. CLASS="literal"
  29464. >stdClass</VAR
  29465. > in PHP.
  29466.     </P
  29467. ></TD
  29468. ></TR
  29469. ></TABLE
  29470. ></DIV
  29471. ><DIV
  29472. CLASS="caution"
  29473. ><P
  29474. ></P
  29475. ><TABLE
  29476. CLASS="caution"
  29477. BORDER="1"
  29478. WIDTH="100%"
  29479. ><TR
  29480. ><TD
  29481. ALIGN="CENTER"
  29482. ><B
  29483. >Caution</B
  29484. ></TD
  29485. ></TR
  29486. ><TR
  29487. ><TD
  29488. ALIGN="LEFT"
  29489. ><P
  29490. >       The function names <VAR
  29491. CLASS="literal"
  29492. >__sleep</VAR
  29493. > and
  29494.       <VAR
  29495. CLASS="literal"
  29496. >__wakeup</VAR
  29497. > are magical in PHP classes. You
  29498.       cannot have functions with these names in any of your
  29499.       classes unless you want the magic functionality associated
  29500.       with them. See below for more information.
  29501.     </P
  29502. ></TD
  29503. ></TR
  29504. ></TABLE
  29505. ></DIV
  29506. ><DIV
  29507. CLASS="caution"
  29508. ><P
  29509. ></P
  29510. ><TABLE
  29511. CLASS="caution"
  29512. BORDER="1"
  29513. WIDTH="100%"
  29514. ><TR
  29515. ><TD
  29516. ALIGN="CENTER"
  29517. ><B
  29518. >Caution</B
  29519. ></TD
  29520. ></TR
  29521. ><TR
  29522. ><TD
  29523. ALIGN="LEFT"
  29524. ><P
  29525. >       PHP reserves all function names starting with __ as magical.
  29526.       It is recommended that you do not use function names with
  29527.       __ in PHP unless you want some documented magic functionality.
  29528.     </P
  29529. ></TD
  29530. ></TR
  29531. ></TABLE
  29532. ></DIV
  29533. ><P
  29534. >     In PHP 4, only constant initializers for <VAR
  29535. CLASS="literal"
  29536. >var</VAR
  29537. >
  29538.     variables are allowed. To initialize variables with non-constant
  29539.     values, you need an initialization function which is called
  29540.     automatically when an object is being constructed from the
  29541.     class. Such a function is called a constructor (see below).
  29542.    </P
  29543. ><DIV
  29544. CLASS="informalexample"
  29545. ><P
  29546. ></P
  29547. ><A
  29548. NAME="AEN5178"
  29549. ></A
  29550. ><TABLE
  29551. BORDER="0"
  29552. BGCOLOR="#E0E0E0"
  29553. CELLPADDING="5"
  29554. ><TR
  29555. ><TD
  29556. ><PRE
  29557. CLASS="php"
  29558. ><?php
  29559. class Cart {
  29560.     /* None of these will work in PHP 4. */
  29561.     var $todays_date = date("Y-m-d");
  29562.     var $name = $firstname;
  29563.     var $owner = 'Fred ' . 'Jones';
  29564.     /* Arrays containing constant values will, though. */
  29565.     var $items = array("VCR", "TV");
  29566. }
  29567.  
  29568. /* This is how it should be done. */
  29569. class Cart {
  29570.     var $todays_date;
  29571.     var $name;
  29572.     var $owner;
  29573.     var $items = array("VCR", "TV");
  29574.  
  29575.     function Cart() {
  29576.         $this->todays_date = date("Y-m-d");
  29577.         $this->name = $GLOBALS['firstname'];
  29578.         /* etc. . . */
  29579.     }
  29580. }
  29581. ?></PRE
  29582. ></TD
  29583. ></TR
  29584. ></TABLE
  29585. ><P
  29586. ></P
  29587. ></DIV
  29588. ><P
  29589. >     Classes are types, that is, they are blueprints for actual
  29590.     variables. You have to create a variable of the desired type with
  29591.     the <VAR
  29592. CLASS="literal"
  29593. >new</VAR
  29594. > operator.
  29595.    </P
  29596. ><DIV
  29597. CLASS="informalexample"
  29598. ><P
  29599. ></P
  29600. ><A
  29601. NAME="AEN5182"
  29602. ></A
  29603. ><TABLE
  29604. BORDER="0"
  29605. BGCOLOR="#E0E0E0"
  29606. CELLPADDING="5"
  29607. ><TR
  29608. ><TD
  29609. ><PRE
  29610. CLASS="php"
  29611. ><?php
  29612. $cart = new Cart;
  29613. $cart->add_item("10", 1);
  29614.  
  29615. $another_cart = new Cart;
  29616. $another_cart->add_item("0815", 3);
  29617. ?></PRE
  29618. ></TD
  29619. ></TR
  29620. ></TABLE
  29621. ><P
  29622. ></P
  29623. ></DIV
  29624. ><P
  29625. >     This creates the objects <VAR
  29626. CLASS="varname"
  29627. >$cart</VAR
  29628. > and
  29629.     <VAR
  29630. CLASS="varname"
  29631. >$another_cart</VAR
  29632. >, both of the class Cart. The function
  29633.     add_item() of the <VAR
  29634. CLASS="varname"
  29635. >$cart</VAR
  29636. > object is being called to add 1
  29637.     item of article number 10 to the <VAR
  29638. CLASS="varname"
  29639. >$cart</VAR
  29640. >. 3 items of
  29641.     article number 0815 are being added to <VAR
  29642. CLASS="varname"
  29643. >$another_cart</VAR
  29644. >.
  29645.    </P
  29646. ><P
  29647. >     Both, <VAR
  29648. CLASS="varname"
  29649. >$cart</VAR
  29650. > and <VAR
  29651. CLASS="varname"
  29652. >$another_cart</VAR
  29653. >, have
  29654.     functions add_item(), remove_item() and a variable items. These are
  29655.     distinct functions and variables. You can think of the objects as
  29656.     something similar to directories in a filesystem. In a filesystem you can
  29657.     have two different files <TT
  29658. CLASS="filename"
  29659. >README.TXT</TT
  29660. >, as long as they are in different
  29661.     directories.  Just like with directories where you'll have to type the
  29662.     full pathname in order to reach each file from the toplevel directory, you
  29663.     have to specify the complete name of the function you want to call: In PHP
  29664.     terms, the toplevel directory would be the global namespace, and the
  29665.     pathname separator would be <VAR
  29666. CLASS="literal"
  29667. >-></VAR
  29668. >.  Thus, the names
  29669.     <VAR
  29670. CLASS="varname"
  29671. >$cart->items</VAR
  29672. > and
  29673.     <VAR
  29674. CLASS="varname"
  29675. >$another_cart->items</VAR
  29676. > name two different variables.
  29677.     Note that the variable is named <VAR
  29678. CLASS="varname"
  29679. >$cart->items</VAR
  29680. >, not
  29681.     <VAR
  29682. CLASS="varname"
  29683. >$cart->$items</VAR
  29684. >, that is, a variable name in PHP has
  29685.     only a single dollar sign.
  29686.    </P
  29687. ><DIV
  29688. CLASS="informalexample"
  29689. ><P
  29690. ></P
  29691. ><A
  29692. NAME="AEN5199"
  29693. ></A
  29694. ><TABLE
  29695. BORDER="0"
  29696. BGCOLOR="#E0E0E0"
  29697. CELLPADDING="5"
  29698. ><TR
  29699. ><TD
  29700. ><PRE
  29701. CLASS="php"
  29702. ><?php
  29703. // correct, single $
  29704. $cart->items = array("10" => 1); 
  29705.  
  29706. // invalid, because $cart->$items becomes $cart->""
  29707. $cart->$items = array("10" => 1);
  29708.  
  29709. // correct, but may or may not be what was intended:
  29710. // $cart->$myvar becomes $cart->items
  29711. $myvar = 'items';
  29712. $cart->$myvar = array("10" => 1);  
  29713. ?></PRE
  29714. ></TD
  29715. ></TR
  29716. ></TABLE
  29717. ><P
  29718. ></P
  29719. ></DIV
  29720. ><P
  29721. >     Within a class definition, you do not know under which name the object
  29722.     will be accessible in your program: at the time the Cart class was
  29723.     written, it was unknown that the object will be named
  29724.     <VAR
  29725. CLASS="varname"
  29726. >$cart</VAR
  29727. > or <VAR
  29728. CLASS="varname"
  29729. >$another_cart</VAR
  29730. > later. Thus,
  29731.     you cannot write <VAR
  29732. CLASS="varname"
  29733. >$cart->items</VAR
  29734. > within the Cart class
  29735.     itself. Instead, in order to be able to access it's own functions and
  29736.     variables from within a class, one can use the pseudo-variable
  29737.     <VAR
  29738. CLASS="varname"
  29739. >$this</VAR
  29740. > which can be read as 'my own' or 'current
  29741.     object'. Thus, '<VAR
  29742. CLASS="varname"
  29743. >$this->items[$artnr]</VAR
  29744. > +=
  29745.     <VAR
  29746. CLASS="varname"
  29747. >$num</VAR
  29748. >' can be read as 'add <VAR
  29749. CLASS="varname"
  29750. >$num</VAR
  29751. > to
  29752.     the <VAR
  29753. CLASS="varname"
  29754. >$artnr</VAR
  29755. > counter of my own items array' or 'add
  29756.     <VAR
  29757. CLASS="varname"
  29758. >$num</VAR
  29759. > to the <VAR
  29760. CLASS="varname"
  29761. >$artnr</VAR
  29762. > counter of the
  29763.     items array within the current object'.
  29764.    </P
  29765. ><DIV
  29766. CLASS="note"
  29767. ><BLOCKQUOTE
  29768. CLASS="note"
  29769. ><P
  29770. ><B
  29771. >Note: </B
  29772. >
  29773.     There are some nice functions to handle classes and objects. You might want
  29774.     to take a look at the <A
  29775. HREF="#ref.classobj"
  29776. >Class/Object
  29777.     Functions</A
  29778. >.
  29779.     </P
  29780. ></BLOCKQUOTE
  29781. ></DIV
  29782. ></DIV
  29783. ><DIV
  29784. CLASS="sect1"
  29785. ><HR><H2
  29786. CLASS="sect1"
  29787. ><A
  29788. NAME="keyword.extends"
  29789. ><VAR
  29790. CLASS="literal"
  29791. >extends</VAR
  29792. ></A
  29793. ></H2
  29794. ><P
  29795. >     Often you need classes with similar variables and functions
  29796.     to another existing class. In fact, it is good practice to
  29797.     define a generic class which can be used in all your
  29798.     projects and adapt this class for the needs of each of your
  29799.     specific projects. To facilitate this, classes can be
  29800.     extensions of other classes.  The extended or derived class
  29801.     has all variables and functions of the base class (this is
  29802.     called 'inheritance' despite the fact that nobody died) and what
  29803.     you add in the extended definition. It is not possible to
  29804.     subtract from a class, that is, to undefine any existing 
  29805.     functions or variables. An extended class is always dependent
  29806.     on a single base class, that is, multiple inheritance is
  29807.     not supported. Classes are extended using the keyword 'extends'.
  29808.    </P
  29809. ><DIV
  29810. CLASS="informalexample"
  29811. ><P
  29812. ></P
  29813. ><A
  29814. NAME="AEN5219"
  29815. ></A
  29816. ><TABLE
  29817. BORDER="0"
  29818. BGCOLOR="#E0E0E0"
  29819. CELLPADDING="5"
  29820. ><TR
  29821. ><TD
  29822. ><PRE
  29823. CLASS="php"
  29824. ><?php
  29825. class Named_Cart extends Cart {
  29826.     var $owner;
  29827.   
  29828.     function set_owner ($name) {
  29829.         $this->owner = $name;
  29830.     }
  29831. }
  29832. ?></PRE
  29833. ></TD
  29834. ></TR
  29835. ></TABLE
  29836. ><P
  29837. ></P
  29838. ></DIV
  29839. ><P
  29840. >     This defines a class Named_Cart that has all variables and functions of
  29841.     Cart plus an additional variable <VAR
  29842. CLASS="varname"
  29843. >$owner</VAR
  29844. > and an
  29845.     additional function set_owner(). You create a named cart the usual way and
  29846.     can now set and get the carts owner. You can still use normal cart
  29847.     functions on named carts:
  29848.    </P
  29849. ><DIV
  29850. CLASS="informalexample"
  29851. ><P
  29852. ></P
  29853. ><A
  29854. NAME="AEN5223"
  29855. ></A
  29856. ><TABLE
  29857. BORDER="0"
  29858. BGCOLOR="#E0E0E0"
  29859. CELLPADDING="5"
  29860. ><TR
  29861. ><TD
  29862. ><PRE
  29863. CLASS="php"
  29864. ><?php
  29865. $ncart = new Named_Cart;    // Create a named cart
  29866. $ncart->set_owner("kris");  // Name that cart
  29867. print $ncart->owner;        // print the cart owners name
  29868. $ncart->add_item("10", 1);  // (inherited functionality from cart)
  29869. ?></PRE
  29870. ></TD
  29871. ></TR
  29872. ></TABLE
  29873. ><P
  29874. ></P
  29875. ></DIV
  29876. ><P
  29877. >     This is also called a "parent-child" relationship. You create a class,
  29878.     parent, and use <VAR
  29879. CLASS="literal"
  29880. >extends</VAR
  29881. > to create a new class
  29882.     <SPAN
  29883. CLASS="emphasis"
  29884. ><I
  29885. CLASS="emphasis"
  29886. >based</I
  29887. ></SPAN
  29888. > on the parent class: the child class. You can
  29889.     even use this new child class and create another class based on this child
  29890.     class.
  29891.    </P
  29892. ><DIV
  29893. CLASS="note"
  29894. ><BLOCKQUOTE
  29895. CLASS="note"
  29896. ><P
  29897. ><B
  29898. >Note: </B
  29899. >
  29900.      Classes must be defined before they are used! If you want the class
  29901.      <VAR
  29902. CLASS="literal"
  29903. >Named_Cart</VAR
  29904. > to extend the class
  29905.      <VAR
  29906. CLASS="literal"
  29907. >Cart</VAR
  29908. >, you will have to define the class
  29909.      <VAR
  29910. CLASS="literal"
  29911. >Cart</VAR
  29912. > first. If you want to create another class called
  29913.      <VAR
  29914. CLASS="literal"
  29915. >Yellow_named_cart</VAR
  29916. > based on the class
  29917.      <VAR
  29918. CLASS="literal"
  29919. >Named_Cart</VAR
  29920. > you have to define
  29921.      <VAR
  29922. CLASS="literal"
  29923. >Named_Cart</VAR
  29924. > first. To make it short: the order in which
  29925.      the classes are defined is important.
  29926.     </P
  29927. ></BLOCKQUOTE
  29928. ></DIV
  29929. ></DIV
  29930. ><DIV
  29931. CLASS="sect1"
  29932. ><HR><H2
  29933. CLASS="sect1"
  29934. ><A
  29935. NAME="language.oop.constructor"
  29936. ><VAR
  29937. CLASS="literal"
  29938. >Constructors</VAR
  29939. ></A
  29940. ></H2
  29941. ><DIV
  29942. CLASS="caution"
  29943. ><P
  29944. ></P
  29945. ><TABLE
  29946. CLASS="caution"
  29947. BORDER="1"
  29948. WIDTH="100%"
  29949. ><TR
  29950. ><TD
  29951. ALIGN="CENTER"
  29952. ><B
  29953. >Caution</B
  29954. ></TD
  29955. ></TR
  29956. ><TR
  29957. ><TD
  29958. ALIGN="LEFT"
  29959. ><P
  29960. >      In PHP 3 and PHP 4 constructors behave differently. The PHP 4
  29961.      semantics are strongly preferred.
  29962.     </P
  29963. ></TD
  29964. ></TR
  29965. ></TABLE
  29966. ></DIV
  29967. ><P
  29968. >     Constructors are functions in a class that are automatically
  29969.     called when you create a new instance of a class with
  29970.     <VAR
  29971. CLASS="literal"
  29972. >new</VAR
  29973. >. In PHP 3, a
  29974.     function becomes a constructor when it has the same name as 
  29975.     the class. In PHP 4, a function becomes a constructor, when
  29976.     it has the same name as the class it is defined in - the
  29977.     difference is subtle, but crucial (see below).
  29978.    </P
  29979. ><DIV
  29980. CLASS="informalexample"
  29981. ><P
  29982. ></P
  29983. ><A
  29984. NAME="AEN5243"
  29985. ></A
  29986. ><TABLE
  29987. BORDER="0"
  29988. BGCOLOR="#E0E0E0"
  29989. CELLPADDING="5"
  29990. ><TR
  29991. ><TD
  29992. ><PRE
  29993. CLASS="php"
  29994. ><?php
  29995. // Works in PHP 3 and PHP 4.
  29996. class Auto_Cart extends Cart {
  29997.     function Auto_Cart() {
  29998.         $this->add_item("10", 1);
  29999.     }
  30000. }
  30001. ?></PRE
  30002. ></TD
  30003. ></TR
  30004. ></TABLE
  30005. ><P
  30006. ></P
  30007. ></DIV
  30008. ><P
  30009. >     This defines a class Auto_Cart that is a Cart plus a constructor
  30010.     which initializes the cart with one item of article number "10"
  30011.     each time a new Auto_Cart is being made with "new". Constructors
  30012.     can take arguments and these arguments can be optional, which
  30013.     makes them much more useful. To be able to still use the class
  30014.     without parameters, all parameters to constructors should be
  30015.     made optional by providing default values.
  30016.    </P
  30017. ><DIV
  30018. CLASS="informalexample"
  30019. ><P
  30020. ></P
  30021. ><A
  30022. NAME="AEN5246"
  30023. ></A
  30024. ><TABLE
  30025. BORDER="0"
  30026. BGCOLOR="#E0E0E0"
  30027. CELLPADDING="5"
  30028. ><TR
  30029. ><TD
  30030. ><PRE
  30031. CLASS="php"
  30032. ><?php
  30033. // Works in PHP 3 and PHP 4.
  30034. class Constructor_Cart extends Cart {
  30035.     function Constructor_Cart($item = "10", $num = 1) {
  30036.         $this->add_item ($item, $num);
  30037.     }
  30038. }
  30039.  
  30040. // Shop the same old boring stuff.
  30041.  
  30042. $default_cart = new Constructor_Cart;
  30043.  
  30044. // Shop for real...
  30045.  
  30046. $different_cart = new Constructor_Cart("20", 17);
  30047. ?></PRE
  30048. ></TD
  30049. ></TR
  30050. ></TABLE
  30051. ><P
  30052. ></P
  30053. ></DIV
  30054. ><P
  30055. >     You also can use the <VAR
  30056. CLASS="literal"
  30057. >@</VAR
  30058. > operator to
  30059.     <SPAN
  30060. CLASS="emphasis"
  30061. ><I
  30062. CLASS="emphasis"
  30063. >mute</I
  30064. ></SPAN
  30065. > errors occurring in the constructor, e.g.
  30066.     <VAR
  30067. CLASS="literal"
  30068. >@new</VAR
  30069. >.
  30070.    </P
  30071. ><DIV
  30072. CLASS="caution"
  30073. ><P
  30074. ></P
  30075. ><TABLE
  30076. CLASS="caution"
  30077. BORDER="1"
  30078. WIDTH="100%"
  30079. ><TR
  30080. ><TD
  30081. ALIGN="CENTER"
  30082. ><B
  30083. >Caution</B
  30084. ></TD
  30085. ></TR
  30086. ><TR
  30087. ><TD
  30088. ALIGN="LEFT"
  30089. ><P
  30090. >      In PHP 3, derived classes and constructors have a number of
  30091.      limitations. The following examples should be read carefully
  30092.      to understand these limitations.
  30093.     </P
  30094. ></TD
  30095. ></TR
  30096. ></TABLE
  30097. ></DIV
  30098. ><DIV
  30099. CLASS="informalexample"
  30100. ><P
  30101. ></P
  30102. ><A
  30103. NAME="AEN5254"
  30104. ></A
  30105. ><TABLE
  30106. BORDER="0"
  30107. BGCOLOR="#E0E0E0"
  30108. CELLPADDING="5"
  30109. ><TR
  30110. ><TD
  30111. ><PRE
  30112. CLASS="php"
  30113. ><?php
  30114. class A {
  30115.     function A() {
  30116.       echo "I am the constructor of A.<br />\n";
  30117.     }
  30118. }
  30119.  
  30120. class B extends A {
  30121.     function C() {
  30122.         echo "I am a regular function.<br />\n";
  30123.     }
  30124. }
  30125.  
  30126. // no constructor is being called in PHP 3.
  30127. $b = new B;
  30128. ?></PRE
  30129. ></TD
  30130. ></TR
  30131. ></TABLE
  30132. ><P
  30133. ></P
  30134. ></DIV
  30135. ><P
  30136. >     In PHP 3, no constructor is being called in the above example.
  30137.     The rule in PHP 3 is: 'A constructor is a function of the same
  30138.     name as the class.'. The name of the class is B, and there is
  30139.     no function called B() in class B. Nothing happens.
  30140.    </P
  30141. ><P
  30142. >     This is fixed in PHP 4 by introducing another rule: If a class
  30143.     has no constructor, the constructor of the base class is being
  30144.     called, if it exists. The above example would have printed
  30145.     'I am the constructor of A.<br />' in PHP 4.
  30146.    </P
  30147. ><DIV
  30148. CLASS="informalexample"
  30149. ><P
  30150. ></P
  30151. ><A
  30152. NAME="AEN5258"
  30153. ></A
  30154. ><TABLE
  30155. BORDER="0"
  30156. BGCOLOR="#E0E0E0"
  30157. CELLPADDING="5"
  30158. ><TR
  30159. ><TD
  30160. ><PRE
  30161. CLASS="php"
  30162. ><?php
  30163. class A
  30164. {
  30165.     function A()
  30166.     {
  30167.         echo "I am the constructor of A.<br />\n";
  30168.     }
  30169.  
  30170.     function B()
  30171.     {
  30172.         echo "I am a regular function named B in class A.<br />\n";
  30173.         echo "I am not a constructor in A.<br />\n";
  30174.     }
  30175. }
  30176.  
  30177. class B extends A
  30178. {
  30179.     function C()
  30180.     {
  30181.         echo "I am a regular function.<br />\n";
  30182.     }
  30183. }
  30184.  
  30185. // This will call B() as a constructor.
  30186. $b = new B;
  30187. ?></PRE
  30188. ></TD
  30189. ></TR
  30190. ></TABLE
  30191. ><P
  30192. ></P
  30193. ></DIV
  30194. ><P
  30195. >     In PHP 3, the function B() in class A will suddenly become a
  30196.     constructor in class B, although it was never intended to be.
  30197.     The rule in PHP 3 is: 'A constructor is a function of the same
  30198.     name as the class.'. PHP 3 does not care if the function is
  30199.     being defined in class B, or if it has been inherited.
  30200.    </P
  30201. ><P
  30202. >     This is fixed in PHP 4 by modifying the rule to: 'A constructor
  30203.     is a function of the same name as the class it is being defined
  30204.     in.'. Thus in PHP 4, the class B would have no constructor function
  30205.     of its own and the constructor of the base class would have been
  30206.     called, printing 'I am the constructor of A.<br />'.
  30207.    </P
  30208. ><DIV
  30209. CLASS="caution"
  30210. ><P
  30211. ></P
  30212. ><TABLE
  30213. CLASS="caution"
  30214. BORDER="1"
  30215. WIDTH="100%"
  30216. ><TR
  30217. ><TD
  30218. ALIGN="CENTER"
  30219. ><B
  30220. >Caution</B
  30221. ></TD
  30222. ></TR
  30223. ><TR
  30224. ><TD
  30225. ALIGN="LEFT"
  30226. ><P
  30227. >      Neither PHP 3 nor PHP 4 call constructors of the base class 
  30228.      automatically from a constructor of a derived class. It is
  30229.      your responsibility to propagate the call to constructors
  30230.      upstream where appropriate.
  30231.     </P
  30232. ></TD
  30233. ></TR
  30234. ></TABLE
  30235. ></DIV
  30236. ><DIV
  30237. CLASS="note"
  30238. ><BLOCKQUOTE
  30239. CLASS="note"
  30240. ><P
  30241. ><B
  30242. >Note: </B
  30243. >
  30244.      There are no destructors in PHP 3 or PHP 4. You may use
  30245.      <A
  30246. HREF="#function.register-shutdown-function"
  30247. ><B
  30248. CLASS="function"
  30249. >register_shutdown_function()</B
  30250. ></A
  30251. > instead
  30252.      to simulate most effects of destructors.
  30253.     </P
  30254. ></BLOCKQUOTE
  30255. ></DIV
  30256. ><P
  30257. >     Destructors are functions that are called automatically
  30258.     when an object is destroyed, either with <A
  30259. HREF="#function.unset"
  30260. ><B
  30261. CLASS="function"
  30262. >unset()</B
  30263. ></A
  30264. >
  30265.     or by simply going out of scope. There are no destructors
  30266.     in PHP.
  30267.    </P
  30268. ></DIV
  30269. ><DIV
  30270. CLASS="sect1"
  30271. ><HR><H2
  30272. CLASS="sect1"
  30273. ><A
  30274. NAME="keyword.paamayim-nekudotayim"
  30275. >Scope Resolution Operator (<VAR
  30276. CLASS="literal"
  30277. >::</VAR
  30278. >)</A
  30279. ></H2
  30280. ><DIV
  30281. CLASS="caution"
  30282. ><P
  30283. ></P
  30284. ><TABLE
  30285. CLASS="caution"
  30286. BORDER="1"
  30287. WIDTH="100%"
  30288. ><TR
  30289. ><TD
  30290. ALIGN="CENTER"
  30291. ><B
  30292. >Caution</B
  30293. ></TD
  30294. ></TR
  30295. ><TR
  30296. ><TD
  30297. ALIGN="LEFT"
  30298. ><P
  30299. >      The following is valid for PHP 4 and later only.
  30300.     </P
  30301. ></TD
  30302. ></TR
  30303. ></TABLE
  30304. ></DIV
  30305. ><P
  30306. >     Sometimes it is useful to refer to functions and variables
  30307.     in base classes or to refer to functions in classes that
  30308.     have not yet any instances. The :: operator is being used
  30309.     for this.
  30310.    </P
  30311. ><DIV
  30312. CLASS="informalexample"
  30313. ><P
  30314. ></P
  30315. ><A
  30316. NAME="AEN5275"
  30317. ></A
  30318. ><TABLE
  30319. BORDER="0"
  30320. BGCOLOR="#E0E0E0"
  30321. CELLPADDING="5"
  30322. ><TR
  30323. ><TD
  30324. ><PRE
  30325. CLASS="php"
  30326. ><?php
  30327. class A {
  30328.     function example() {
  30329.         echo "I am the original function A::example().<br />\n";
  30330.     }
  30331. }
  30332.  
  30333. class B extends A {
  30334.     function example() {
  30335.         echo "I am the redefined function B::example().<br />\n";
  30336.         A::example();
  30337.     }
  30338. }
  30339.  
  30340. // there is no object of class A.
  30341. // this will print
  30342. //   I am the original function A::example().<br />
  30343. A::example();
  30344.  
  30345. // create an object of class B.
  30346. $b = new B;
  30347.  
  30348. // this will print 
  30349. //   I am the redefined function B::example().<br />
  30350. //   I am the original function A::example().<br />
  30351. $b->example();
  30352. ?></PRE
  30353. ></TD
  30354. ></TR
  30355. ></TABLE
  30356. ><P
  30357. ></P
  30358. ></DIV
  30359. ><P
  30360. >     The above example calls the function example() in
  30361.     class A, but there is no object of class A, so that
  30362.     we cannot write $a->example() or similar. Instead we
  30363.     call example() as a 'class function', that is, as a
  30364.     function of the class itself, not any object of that
  30365.     class.
  30366.    </P
  30367. ><P
  30368. >     There are class functions, but there are no class variables.
  30369.     In fact, there is no object at all at the time of the call.
  30370.     Thus, a class function may not use any object variables (but
  30371.     it can use local and global variables), and it may no use
  30372.     <VAR
  30373. CLASS="varname"
  30374. >$this</VAR
  30375. > at all.
  30376.    </P
  30377. ><P
  30378. >     In the above example, class B redefines the function example(). 
  30379.     The original definition in class A is shadowed
  30380.     and no longer available, unless you are referring specifically
  30381.     to the implementation of example() in class A using the 
  30382.     ::-operator. Write A::example() to do this (in fact, you
  30383.     should be writing parent::example(), as shown in the next
  30384.     section).
  30385.    </P
  30386. ><P
  30387. >     In this context, there is a current object and it may have object
  30388.     variables. Thus, when used from WITHIN an object function, you may use
  30389.     <VAR
  30390. CLASS="varname"
  30391. >$this</VAR
  30392. > and object variables.
  30393.    </P
  30394. ></DIV
  30395. ><DIV
  30396. CLASS="sect1"
  30397. ><HR><H2
  30398. CLASS="sect1"
  30399. ><A
  30400. NAME="keyword.parent"
  30401. ><VAR
  30402. CLASS="literal"
  30403. >parent</VAR
  30404. ></A
  30405. ></H2
  30406. ><P
  30407. >    You may find yourself writing code that refers to
  30408.    variables and functions in base classes. This is
  30409.    particularly true if your derived class is a refinement
  30410.    or specialisation of code in your base class. 
  30411.   </P
  30412. ><P
  30413. >    Instead of using the literal name of the base class in your
  30414.    code, you should be using the special name
  30415.    <VAR
  30416. CLASS="literal"
  30417. >parent</VAR
  30418. >, which refers to the name of your
  30419.    base class as given in the <VAR
  30420. CLASS="literal"
  30421. >extends</VAR
  30422. >
  30423.    declaration of your class. By doing this, you avoid using the
  30424.    name of your base class in more than one place. Should
  30425.    your inheritance tree change during implementation, the
  30426.    change is easily made by simply changing the 
  30427.    <VAR
  30428. CLASS="literal"
  30429. >extends</VAR
  30430. > declaration of your class.
  30431.   </P
  30432. ><DIV
  30433. CLASS="informalexample"
  30434. ><P
  30435. ></P
  30436. ><A
  30437. NAME="AEN5291"
  30438. ></A
  30439. ><TABLE
  30440. BORDER="0"
  30441. BGCOLOR="#E0E0E0"
  30442. CELLPADDING="5"
  30443. ><TR
  30444. ><TD
  30445. ><PRE
  30446. CLASS="php"
  30447. ><?php
  30448. class A {
  30449.     function example() {
  30450.         echo "I am A::example() and provide basic functionality.<br />\n";
  30451.     }
  30452. }
  30453.  
  30454. class B extends A {
  30455.     function example() {
  30456.         echo "I am B::example() and provide additional functionality.<br />\n";
  30457.         parent::example();
  30458.     }
  30459. }
  30460.  
  30461. $b = new B;
  30462.  
  30463. // This will call B::example(), which will in turn call A::example().
  30464. $b->example();
  30465. ?></PRE
  30466. ></TD
  30467. ></TR
  30468. ></TABLE
  30469. ><P
  30470. ></P
  30471. ></DIV
  30472. ></DIV
  30473. ><DIV
  30474. CLASS="sect1"
  30475. ><HR><H2
  30476. CLASS="sect1"
  30477. ><A
  30478. NAME="language.oop.serialization"
  30479. >Serializing objects - objects in sessions</A
  30480. ></H2
  30481. ><DIV
  30482. CLASS="note"
  30483. ><BLOCKQUOTE
  30484. CLASS="note"
  30485. ><P
  30486. ><B
  30487. >Note: </B
  30488. >
  30489.     In PHP 3, objects will lose their class association
  30490.     throughout the process of serialization and unserialization. 
  30491.     The resulting variable is of type object, but has no class
  30492.     and no methods, thus it is pretty useless (it has become
  30493.     just like an array with a funny syntax).
  30494.    </P
  30495. ></BLOCKQUOTE
  30496. ></DIV
  30497. ><DIV
  30498. CLASS="caution"
  30499. ><P
  30500. ></P
  30501. ><TABLE
  30502. CLASS="caution"
  30503. BORDER="1"
  30504. WIDTH="100%"
  30505. ><TR
  30506. ><TD
  30507. ALIGN="CENTER"
  30508. ><B
  30509. >Caution</B
  30510. ></TD
  30511. ></TR
  30512. ><TR
  30513. ><TD
  30514. ALIGN="LEFT"
  30515. ><P
  30516. >     The following information is valid for PHP 4 only. 
  30517.    </P
  30518. ></TD
  30519. ></TR
  30520. ></TABLE
  30521. ></DIV
  30522. ><P
  30523. >    <A
  30524. HREF="#function.serialize"
  30525. ><B
  30526. CLASS="function"
  30527. >serialize()</B
  30528. ></A
  30529. > returns a string containing a
  30530.    byte-stream representation of any value that can be stored in
  30531.    PHP. <A
  30532. HREF="#function.unserialize"
  30533. ><B
  30534. CLASS="function"
  30535. >unserialize()</B
  30536. ></A
  30537. > can use this string to
  30538.    recreate the original variable values. Using serialize to
  30539.    save an object will save all variables in an object.  The
  30540.    functions in an object will not be saved, only the name of
  30541.    the class.
  30542.   </P
  30543. ><P
  30544. >    In order to be able to <A
  30545. HREF="#function.unserialize"
  30546. ><B
  30547. CLASS="function"
  30548. >unserialize()</B
  30549. ></A
  30550. > an object, the
  30551.    class of that object needs to be defined. That is, if you have an object
  30552.    <VAR
  30553. CLASS="varname"
  30554. >$a</VAR
  30555. > of class A on page1.php and serialize this, you'll
  30556.    get a string that refers to class A and contains all values of variabled
  30557.    contained in <VAR
  30558. CLASS="varname"
  30559. >$a</VAR
  30560. >. If you want to be able to unserialize
  30561.    this on page2.php, recreating <VAR
  30562. CLASS="varname"
  30563. >$a</VAR
  30564. > of class A, the
  30565.    definition of class A must be present in page2.php. This can be done for
  30566.    example by storing the class definition of class A in an include file and
  30567.    including this file in both page1.php and page2.php.
  30568.   </P
  30569. ><DIV
  30570. CLASS="informalexample"
  30571. ><P
  30572. ></P
  30573. ><A
  30574. NAME="AEN5307"
  30575. ></A
  30576. ><TABLE
  30577. BORDER="0"
  30578. BGCOLOR="#E0E0E0"
  30579. CELLPADDING="5"
  30580. ><TR
  30581. ><TD
  30582. ><PRE
  30583. CLASS="php"
  30584. ><?php
  30585. // classa.inc:
  30586.   
  30587.   class A {
  30588.       var $one = 1;
  30589.     
  30590.       function show_one() {
  30591.           echo $this->one;
  30592.       }
  30593.   }
  30594.   
  30595. // page1.php:
  30596.  
  30597.   include("classa.inc");
  30598.   
  30599.   $a = new A;
  30600.   $s = serialize($a);
  30601.   // store $s somewhere where page2.php can find it.
  30602.   $fp = fopen("store", "w");
  30603.   fwrite($fp, $s);
  30604.   fclose($fp);
  30605.  
  30606. // page2.php:
  30607.   
  30608.   // this is needed for the unserialize to work properly.
  30609.   include("classa.inc");
  30610.  
  30611.   $s = implode("", @file("store"));
  30612.   $a = unserialize($s);
  30613.  
  30614.   // now use the function show_one() of the $a object.  
  30615.   $a->show_one();
  30616. ?></PRE
  30617. ></TD
  30618. ></TR
  30619. ></TABLE
  30620. ><P
  30621. ></P
  30622. ></DIV
  30623. ><P
  30624. >    If you are using sessions and use <A
  30625. HREF="#function.session-register"
  30626. ><B
  30627. CLASS="function"
  30628. >session_register()</B
  30629. ></A
  30630. >
  30631.    to register objects, these objects are serialized automatically
  30632.    at the end of each PHP page, and are unserialized automatically on
  30633.    each of the following pages. This basically means that these objects
  30634.    can show up on any of your pages once they become part of your
  30635.    session.
  30636.   </P
  30637. ><P
  30638. >    It is strongly recommended that you include the class
  30639.    definitions of all such registered objects on all of your
  30640.    pages, even if you do not actually use these classes on all
  30641.    of your pages. If you don't and an object is being
  30642.    unserialized without its class definition being present, it
  30643.    will lose its class association and become an object of class
  30644.    <VAR
  30645. CLASS="literal"
  30646. >stdClass</VAR
  30647. > without any functions available
  30648.    at all, that is, it will become quite useless.
  30649.   </P
  30650. ><P
  30651. >    So if in the example above <VAR
  30652. CLASS="varname"
  30653. >$a</VAR
  30654. > became part of a session
  30655.    by running <VAR
  30656. CLASS="literal"
  30657. >session_register("a")</VAR
  30658. >, you should include the
  30659.    file <VAR
  30660. CLASS="literal"
  30661. >classa.inc</VAR
  30662. > on all of your pages, not only page1.php
  30663.    and page2.php.
  30664.   </P
  30665. ></DIV
  30666. ><DIV
  30667. CLASS="sect1"
  30668. ><HR><H2
  30669. CLASS="sect1"
  30670. ><A
  30671. NAME="language.oop.magic-functions"
  30672. >The magic functions <VAR
  30673. CLASS="literal"
  30674. >__sleep</VAR
  30675. > and <VAR
  30676. CLASS="literal"
  30677. >__wakeup</VAR
  30678. ></A
  30679. ></H2
  30680. ><P
  30681. >    <A
  30682. HREF="#function.serialize"
  30683. ><B
  30684. CLASS="function"
  30685. >serialize()</B
  30686. ></A
  30687. > checks if your class has a function with
  30688.    the magic name <VAR
  30689. CLASS="literal"
  30690. >__sleep</VAR
  30691. >. If so, that function is
  30692.    being run prior to any serialization. It can clean up the object
  30693.    and is supposed to return an array with the names of all variables
  30694.    of that object that should be serialized.
  30695.   </P
  30696. ><P
  30697. >    The intended use of <VAR
  30698. CLASS="literal"
  30699. >__sleep</VAR
  30700. > is to close any
  30701.    database connections that object may have, committing pending
  30702.    data or perform similar cleanup tasks. Also, the function is
  30703.    useful if you have very large objects which need not be
  30704.    saved completely.
  30705.   </P
  30706. ><P
  30707. >    Conversely, <A
  30708. HREF="#function.unserialize"
  30709. ><B
  30710. CLASS="function"
  30711. >unserialize()</B
  30712. ></A
  30713. > checks for the
  30714.    presence of a function with the magic name 
  30715.    <VAR
  30716. CLASS="literal"
  30717. >__wakeup</VAR
  30718. >. If present, this function can
  30719.    reconstruct any resources that object may have.
  30720.   </P
  30721. ><P
  30722. >     The intended use of <VAR
  30723. CLASS="literal"
  30724. >__wakeup</VAR
  30725. > is to
  30726.     reestablish any database connections that may have been lost
  30727.     during serialization and perform other reinitialization
  30728.     tasks.
  30729.   </P
  30730. ></DIV
  30731. ><DIV
  30732. CLASS="sect1"
  30733. ><HR><H2
  30734. CLASS="sect1"
  30735. ><A
  30736. NAME="language.oop.newref"
  30737. >References inside the constructor</A
  30738. ></H2
  30739. ><P
  30740. >     Creating references within the constructor can lead to confusing
  30741.     results. This tutorial-like section helps you to avoid problems.
  30742.  
  30743.     <DIV
  30744. CLASS="informalexample"
  30745. ><P
  30746. ></P
  30747. ><A
  30748. NAME="AEN5334"
  30749. ></A
  30750. ><TABLE
  30751. BORDER="0"
  30752. BGCOLOR="#E0E0E0"
  30753. CELLPADDING="5"
  30754. ><TR
  30755. ><TD
  30756. ><PRE
  30757. CLASS="php"
  30758. ><?php
  30759. class Foo {
  30760.     function Foo($name) {
  30761.         // create a reference inside the global array $globalref
  30762.         global $globalref;
  30763.         $globalref[] = &$this;
  30764.         // set name to passed value
  30765.         $this->setName($name);
  30766.         // and put it out
  30767.         $this->echoName();
  30768.     }
  30769.  
  30770.     function echoName() {
  30771.         echo "<br />", $this->name;
  30772.     }
  30773.  
  30774.     function setName($name) {
  30775.         $this->name = $name;
  30776.     }
  30777. }
  30778. ?></PRE
  30779. ></TD
  30780. ></TR
  30781. ></TABLE
  30782. ><P
  30783. ></P
  30784. ></DIV
  30785. >
  30786.   </P
  30787. ><P
  30788. >     Let us check out if there is a difference between
  30789.     <VAR
  30790. CLASS="varname"
  30791. >$bar1</VAR
  30792. > which has been created using
  30793.     the copy <VAR
  30794. CLASS="literal"
  30795. >=</VAR
  30796. > operator and
  30797.     <VAR
  30798. CLASS="varname"
  30799. >$bar2</VAR
  30800. > which has been created using
  30801.     the reference <VAR
  30802. CLASS="literal"
  30803. >=&</VAR
  30804. > operator...
  30805.  
  30806.     <DIV
  30807. CLASS="informalexample"
  30808. ><P
  30809. ></P
  30810. ><A
  30811. NAME="AEN5341"
  30812. ></A
  30813. ><TABLE
  30814. BORDER="0"
  30815. BGCOLOR="#E0E0E0"
  30816. CELLPADDING="5"
  30817. ><TR
  30818. ><TD
  30819. ><PRE
  30820. CLASS="php"
  30821. ><?php
  30822. $bar1 = new Foo('set in constructor');
  30823. $bar1->echoName();
  30824. $globalref[0]->echoName();
  30825.  
  30826. /* output:
  30827. set in constructor
  30828. set in constructor
  30829. set in constructor */
  30830.  
  30831. $bar2 =& new Foo('set in constructor');
  30832. $bar2->echoName();
  30833. $globalref[1]->echoName();
  30834.  
  30835. /* output:
  30836. set in constructor
  30837. set in constructor
  30838. set in constructor */
  30839. ?></PRE
  30840. ></TD
  30841. ></TR
  30842. ></TABLE
  30843. ><P
  30844. ></P
  30845. ></DIV
  30846. >
  30847.    </P
  30848. ><P
  30849. >     Apparently there is no difference, but in fact there is a
  30850.     very significant one: <VAR
  30851. CLASS="varname"
  30852. >$bar1</VAR
  30853. > and
  30854.     <VAR
  30855. CLASS="varname"
  30856. >$globalref[0]</VAR
  30857. > are _NOT_ referenced, they
  30858.     are NOT the same variable. This is because "new" does not
  30859.     return a reference by default, instead it returns a copy.
  30860.     <DIV
  30861. CLASS="note"
  30862. ><BLOCKQUOTE
  30863. CLASS="note"
  30864. ><P
  30865. ><B
  30866. >Note: </B
  30867. >
  30868.       There is no performance loss (since PHP 4 and up use reference
  30869.       counting) returning copies instead of references. On the
  30870.       contrary it is most often better to simply work with copies
  30871.       instead of references, because creating references takes some
  30872.       time where creating copies virtually takes no time (unless none
  30873.       of them is a large array or object and one of them gets changed
  30874.       and the other(s) one(s) subsequently, then it would be wise to
  30875.       use references to change them all concurrently).
  30876.      </P
  30877. ></BLOCKQUOTE
  30878. ></DIV
  30879. >
  30880.     To prove what is written above let us watch the code below.
  30881.  
  30882.     <DIV
  30883. CLASS="informalexample"
  30884. ><P
  30885. ></P
  30886. ><A
  30887. NAME="AEN5348"
  30888. ></A
  30889. ><TABLE
  30890. BORDER="0"
  30891. BGCOLOR="#E0E0E0"
  30892. CELLPADDING="5"
  30893. ><TR
  30894. ><TD
  30895. ><PRE
  30896. CLASS="php"
  30897. ><?php
  30898. // now we will change the name. what do you expect?
  30899. // you could expect that both $bar1 and $globalref[0] change their names...
  30900. $bar1->setName('set from outside');
  30901.  
  30902. // as mentioned before this is not the case.
  30903. $bar1->echoName();
  30904. $globalref[0]->echoName();
  30905.  
  30906. /* output:
  30907. set from outside
  30908. set in constructor */
  30909.  
  30910. // let us see what is different with $bar2 and $globalref[1]
  30911. $bar2->setName('set from outside');
  30912.  
  30913. // luckily they are not only equal, they are the same variable
  30914. // thus $bar2->name and $globalref[1]->name are the same too
  30915. $bar2->echoName();
  30916. $globalref[1]->echoName();
  30917.  
  30918. /* output:
  30919. set from outside
  30920. set from outside */
  30921. ?></PRE
  30922. ></TD
  30923. ></TR
  30924. ></TABLE
  30925. ><P
  30926. ></P
  30927. ></DIV
  30928. >   
  30929.    </P
  30930. ><P
  30931. >    Another final example, try to understand it.
  30932.    
  30933.     <DIV
  30934. CLASS="informalexample"
  30935. ><P
  30936. ></P
  30937. ><A
  30938. NAME="AEN5351"
  30939. ></A
  30940. ><TABLE
  30941. BORDER="0"
  30942. BGCOLOR="#E0E0E0"
  30943. CELLPADDING="5"
  30944. ><TR
  30945. ><TD
  30946. ><PRE
  30947. CLASS="php"
  30948. ><?php
  30949. class A {
  30950.     function A($i) {
  30951.         $this->value = $i;
  30952.         // try to figure out why we do not need a reference here
  30953.         $this->b = new B($this);
  30954.     }
  30955.  
  30956.     function createRef() {
  30957.         $this->c = new B($this);
  30958.     }
  30959.  
  30960.     function echoValue() {
  30961.         echo "<br />","class ",get_class($this),': ',$this->value;
  30962.     }
  30963. }
  30964.  
  30965.  
  30966. class B {
  30967.     function B(&$a) {
  30968.         $this->a = &$a;
  30969.     }
  30970.  
  30971.     function echoValue() {
  30972.         echo "<br />","class ",get_class($this),': ',$this->a->value;
  30973.     }
  30974. }
  30975.  
  30976. // try to understand why using a simple copy here would yield
  30977. // in an undesired result in the *-marked line
  30978. $a =& new A(10);
  30979. $a->createRef();
  30980.  
  30981. $a->echoValue();
  30982. $a->b->echoValue();
  30983. $a->c->echoValue();
  30984.  
  30985. $a->value = 11;
  30986.  
  30987. $a->echoValue();
  30988. $a->b->echoValue(); // *
  30989. $a->c->echoValue();
  30990.  
  30991. ?></PRE
  30992. ></TD
  30993. ></TR
  30994. ></TABLE
  30995. ><P
  30996. >       This example will output:
  30997.      </P
  30998. ><TABLE
  30999. BORDER="0"
  31000. BGCOLOR="#E0E0E0"
  31001. CELLPADDING="5"
  31002. ><TR
  31003. ><TD
  31004. ><PRE
  31005. CLASS="screen"
  31006. >class A: 10
  31007. class B: 10
  31008. class B: 10
  31009. class A: 11
  31010. class B: 11
  31011. class B: 11</PRE
  31012. ></TD
  31013. ></TR
  31014. ></TABLE
  31015. ><P
  31016. ></P
  31017. ></DIV
  31018. >
  31019.    </P
  31020. ></DIV
  31021. ><DIV
  31022. CLASS="sect1"
  31023. ><HR><H2
  31024. CLASS="sect1"
  31025. ><A
  31026. NAME="language.oop.object-comparison"
  31027. >Comparing objects</A
  31028. ></H2
  31029. ><P
  31030. >    In PHP 4, objects are compared in a very simple manner, namely: Two object
  31031.    instances are equal if they have the same attributes and values, and are
  31032.    instances of the same class. Similar rules are applied when comparing two
  31033.    objects using the identity operator (<VAR
  31034. CLASS="literal"
  31035. >===</VAR
  31036. >).
  31037.   </P
  31038. ><P
  31039. >    If we were to execute the code in the example below:
  31040.    <TABLE
  31041. WIDTH="100%"
  31042. BORDER="0"
  31043. CELLPADDING="0"
  31044. CELLSPACING="0"
  31045. CLASS="EXAMPLE"
  31046. ><TR
  31047. ><TD
  31048. ><DIV
  31049. CLASS="example"
  31050. ><A
  31051. NAME="AEN5360"
  31052. ></A
  31053. ><P
  31054. ><B
  31055. >Example 18-1. Example of object comparison in PHP 4</B
  31056. ></P
  31057. ><TABLE
  31058. BORDER="0"
  31059. BGCOLOR="#E0E0E0"
  31060. CELLPADDING="5"
  31061. ><TR
  31062. ><TD
  31063. ><PRE
  31064. CLASS="php"
  31065. ><?php
  31066. function bool2str($bool) {
  31067.     if ($bool === false) {
  31068.             return 'FALSE';
  31069.     } else {
  31070.             return 'TRUE';
  31071.     }
  31072. }
  31073.  
  31074. function compareObjects(&$o1, &$o2) {
  31075.     echo 'o1 == o2 : '.bool2str($o1 == $o2)."\n";
  31076.     echo 'o1 != o2 : '.bool2str($o1 != $o2)."\n";
  31077.     echo 'o1 === o2 : '.bool2str($o1 === $o2)."\n";
  31078.     echo 'o1 !== o2 : '.bool2str($o1 !== $o2)."\n";
  31079. }
  31080.  
  31081. class Flag {
  31082.     var $flag;
  31083.  
  31084.     function Flag($flag=true) {
  31085.             $this->flag = $flag;
  31086.     }
  31087. }
  31088.  
  31089. class SwitchableFlag extends Flag {
  31090.  
  31091.     function turnOn() {
  31092.         $this->flag = true;
  31093.     }
  31094.  
  31095.     function turnOff() {
  31096.         $this->flag = false;
  31097.     }
  31098. }
  31099.  
  31100. $o = new Flag();
  31101. $p = new Flag(false);
  31102. $q = new Flag();
  31103.  
  31104. $r = new SwitchableFlag();
  31105.  
  31106. echo "Compare instances created with the same parameters\n";
  31107. compareObjects($o, $q);
  31108.  
  31109. echo "\nCompare instances created with different parameters\n";
  31110. compareObjects($o, $p);
  31111.  
  31112. echo "\nCompare an instance of a parent class with one from a subclass\n";
  31113. compareObjects($o, $r);
  31114. ?></PRE
  31115. ></TD
  31116. ></TR
  31117. ></TABLE
  31118. ></DIV
  31119. ></TD
  31120. ></TR
  31121. ></TABLE
  31122. >
  31123.    We will see:
  31124.    <TABLE
  31125. BORDER="0"
  31126. BGCOLOR="#E0E0E0"
  31127. CELLPADDING="5"
  31128. ><TR
  31129. ><TD
  31130. ><PRE
  31131. CLASS="screen"
  31132. >Compare instances created with the same parameters
  31133. o1 == o2 : TRUE
  31134. o1 != o2 : FALSE
  31135. o1 === o2 : TRUE
  31136. o1 !== o2 : FALSE
  31137.  
  31138. Compare instances created with different parameters
  31139. o1 == o2 : FALSE
  31140. o1 != o2 : TRUE
  31141. o1 === o2 : FALSE
  31142. o1 !== o2 : TRUE
  31143.  
  31144. Compare an instance of a parent class with one from a subclass
  31145. o1 == o2 : FALSE
  31146. o1 != o2 : TRUE
  31147. o1 === o2 : FALSE
  31148. o1 !== o2 : TRUE</PRE
  31149. ></TD
  31150. ></TR
  31151. ></TABLE
  31152. >
  31153.    Which is the output we will expect to obtain given the comparison rules
  31154.    above. Only instances with the same values for their attributes and from the same
  31155.    class are considered equal and identical.
  31156.   </P
  31157. ><P
  31158. >    Even in the cases where we have object composition, the same comparison
  31159.    rules apply. In the example below we create a container class that stores
  31160.    an associative array of <B
  31161. CLASS="classname"
  31162. >Flag</B
  31163. > objects.
  31164.    <TABLE
  31165. WIDTH="100%"
  31166. BORDER="0"
  31167. CELLPADDING="0"
  31168. CELLSPACING="0"
  31169. CLASS="EXAMPLE"
  31170. ><TR
  31171. ><TD
  31172. ><DIV
  31173. CLASS="example"
  31174. ><A
  31175. NAME="AEN5366"
  31176. ></A
  31177. ><P
  31178. ><B
  31179. >Example 18-2. Compound object comparisons in PHP 4</B
  31180. ></P
  31181. ><TABLE
  31182. BORDER="0"
  31183. BGCOLOR="#E0E0E0"
  31184. CELLPADDING="5"
  31185. ><TR
  31186. ><TD
  31187. ><PRE
  31188. CLASS="php"
  31189. ><?php
  31190. class FlagSet {
  31191.     var $set;
  31192.  
  31193.     function FlagSet($flagArr = array()) {
  31194.         $this->set = $flagArr;
  31195.     }
  31196.  
  31197.     function addFlag($name, $flag) {
  31198.         $this->set[$name] = $flag;
  31199.     }
  31200.  
  31201.     function removeFlag($name) {
  31202.         if (array_key_exists($name, $this->set)) {
  31203.             unset($this->set[$name]);
  31204.         }
  31205.     }
  31206. }
  31207.  
  31208.  
  31209. $u = new FlagSet();
  31210. $u->addFlag('flag1', $o);
  31211. $u->addFlag('flag2', $p);
  31212. $v = new FlagSet(array('flag1'=>$q, 'flag2'=>$p));
  31213. $w = new FlagSet(array('flag1'=>$q));
  31214.  
  31215. echo "\nComposite objects u(o,p) and v(q,p)\n";
  31216. compareObjects($u, $v);
  31217.  
  31218. echo "\nu(o,p) and w(q)\n";
  31219. compareObjects($u, $w);
  31220. ?></PRE
  31221. ></TD
  31222. ></TR
  31223. ></TABLE
  31224. ></DIV
  31225. ></TD
  31226. ></TR
  31227. ></TABLE
  31228. >
  31229.    Which gives the expected output:
  31230.    <TABLE
  31231. BORDER="0"
  31232. BGCOLOR="#E0E0E0"
  31233. CELLPADDING="5"
  31234. ><TR
  31235. ><TD
  31236. ><PRE
  31237. CLASS="screen"
  31238. >Composite objects u(o,p) and v(q,p)
  31239. o1 == o2 : TRUE
  31240. o1 != o2 : FALSE
  31241. o1 === o2 : TRUE
  31242. o1 !== o2 : FALSE
  31243.  
  31244. u(o,p) and w(q)
  31245. o1 == o2 : FALSE
  31246. o1 != o2 : TRUE
  31247. o1 === o2 : FALSE
  31248. o1 !== o2 : TRUE</PRE
  31249. ></TD
  31250. ></TR
  31251. ></TABLE
  31252. >
  31253.   </P
  31254. ></DIV
  31255. ></DIV
  31256. ><DIV
  31257. CLASS="chapter"
  31258. ><HR><H1
  31259. ><A
  31260. NAME="language.oop5"
  31261. >Chapter 19. Classes and Objects (PHP 5)</A
  31262. ></H1
  31263. ><DIV
  31264. CLASS="sect1"
  31265. ><H2
  31266. CLASS="sect1"
  31267. ><A
  31268. NAME="op5.intro"
  31269. >Introduction</A
  31270. ></H2
  31271. ><P
  31272. >     In PHP 5 there is a new Object Model. PHP's handling of objects has been
  31273.     completely rewritten, allowing for better performance and more features.
  31274.    </P
  31275. ></DIV
  31276. ><DIV
  31277. CLASS="sect1"
  31278. ><HR><H2
  31279. CLASS="sect1"
  31280. ><A
  31281. NAME="language.oop5.basic"
  31282. >The Basics</A
  31283. ></H2
  31284. ><DIV
  31285. CLASS="sect2"
  31286. ><H3
  31287. CLASS="sect2"
  31288. ><A
  31289. NAME="language.oop5.basic.class"
  31290. >class</A
  31291. ></H3
  31292. ><P
  31293. >     Every class definition begins with the keyword class, followed by a class
  31294.     name, which can be any name that isn't a <A
  31295. HREF="#reserved"
  31296. >reserved</A
  31297. >
  31298.     word in PHP. Followed  by a pair of curly braces, of
  31299.     which contains the definition of the classes members and methods. Within
  31300.     each method, except for <A
  31301. HREF="#language.oop5.static"
  31302. >static</A
  31303. >
  31304.     methods, a pseudo variable <VAR
  31305. CLASS="varname"
  31306. >$this</VAR
  31307. > is available.
  31308.     <VAR
  31309. CLASS="varname"
  31310. >$this</VAR
  31311. > is a reference to the same instance that
  31312.     called the method.
  31313.    </P
  31314. ><TABLE
  31315. WIDTH="100%"
  31316. BORDER="0"
  31317. CELLPADDING="0"
  31318. CELLSPACING="0"
  31319. CLASS="EXAMPLE"
  31320. ><TR
  31321. ><TD
  31322. ><DIV
  31323. CLASS="example"
  31324. ><A
  31325. NAME="AEN5384"
  31326. ></A
  31327. ><P
  31328. ><B
  31329. >Example 19-1. Simple Class definition</B
  31330. ></P
  31331. ><TABLE
  31332. BORDER="0"
  31333. BGCOLOR="#E0E0E0"
  31334. CELLPADDING="5"
  31335. ><TR
  31336. ><TD
  31337. ><PRE
  31338. CLASS="php"
  31339. ><?php
  31340. class SimpleClass
  31341. {
  31342.     // member declaration
  31343.     public $var = 'a default value';
  31344.  
  31345.     // method declaration
  31346.     public function displayVar() {
  31347.         echo $this->var;
  31348.     }
  31349. }
  31350. ?></PRE
  31351. ></TD
  31352. ></TR
  31353. ></TABLE
  31354. ></DIV
  31355. ></TD
  31356. ></TR
  31357. ></TABLE
  31358. ></DIV
  31359. ><DIV
  31360. CLASS="sect2"
  31361. ><HR><H3
  31362. CLASS="sect2"
  31363. ><A
  31364. NAME="language.oop5.basic.new"
  31365. >new</A
  31366. ></H3
  31367. ><P
  31368. >     To create an instance of an object, a new object must be created and
  31369.     assigned to a variable.  An object will always be assigned when
  31370.     creating a new object unless the object has a
  31371.     <A
  31372. HREF="#language.oop5.decon"
  31373. >constructor</A
  31374. > defined that throws an
  31375.     <A
  31376. HREF="#language.exceptions"
  31377. >exception</A
  31378. > on error.
  31379.    </P
  31380. ><TABLE
  31381. WIDTH="100%"
  31382. BORDER="0"
  31383. CELLPADDING="0"
  31384. CELLSPACING="0"
  31385. CLASS="EXAMPLE"
  31386. ><TR
  31387. ><TD
  31388. ><DIV
  31389. CLASS="example"
  31390. ><A
  31391. NAME="AEN5392"
  31392. ></A
  31393. ><P
  31394. ><B
  31395. >Example 19-2. Creating an instance</B
  31396. ></P
  31397. ><TABLE
  31398. BORDER="0"
  31399. BGCOLOR="#E0E0E0"
  31400. CELLPADDING="5"
  31401. ><TR
  31402. ><TD
  31403. ><PRE
  31404. CLASS="php"
  31405. ><?php
  31406. $instance = new SimpleClass()
  31407. ?></PRE
  31408. ></TD
  31409. ></TR
  31410. ></TABLE
  31411. ></DIV
  31412. ></TD
  31413. ></TR
  31414. ></TABLE
  31415. ><P
  31416. >     When assigning an already created instance of an object to a new variable, the new variable
  31417.     will access the same instance as the object that was assigned. This
  31418.     behaviour is the same when passing instances to a function. A new instance
  31419.     of an already created object can be made by
  31420.     <A
  31421. HREF="#language.oop5.cloning"
  31422. >cloning</A
  31423. > it.
  31424.    </P
  31425. ><TABLE
  31426. WIDTH="100%"
  31427. BORDER="0"
  31428. CELLPADDING="0"
  31429. CELLSPACING="0"
  31430. CLASS="EXAMPLE"
  31431. ><TR
  31432. ><TD
  31433. ><DIV
  31434. CLASS="example"
  31435. ><A
  31436. NAME="AEN5397"
  31437. ></A
  31438. ><P
  31439. ><B
  31440. >Example 19-3. Object Assignment</B
  31441. ></P
  31442. ><TABLE
  31443. BORDER="0"
  31444. BGCOLOR="#E0E0E0"
  31445. CELLPADDING="5"
  31446. ><TR
  31447. ><TD
  31448. ><PRE
  31449. CLASS="php"
  31450. ><?php
  31451. $assigned   =  $instance;
  31452. $reference  =& $instance;
  31453.  
  31454. $instance->var = '$assigned will have this value';
  31455.  
  31456. $instance = null; // $instance and $reference become null
  31457.  
  31458. var_dump($instance);
  31459. var_dump($reference);
  31460. var_dump($assigned);
  31461. ?></PRE
  31462. ></TD
  31463. ></TR
  31464. ></TABLE
  31465. ><P
  31466. >The above example will output:</P
  31467. ><TABLE
  31468. BORDER="0"
  31469. BGCOLOR="#E0E0E0"
  31470. CELLPADDING="5"
  31471. ><TR
  31472. ><TD
  31473. ><PRE
  31474. CLASS="screen"
  31475. >NULL
  31476. NULL
  31477. object(SimpleClass)#1 (1) {
  31478.    ["var"]=>
  31479.      string(30) "$assigned will have this value"
  31480. }</PRE
  31481. ></TD
  31482. ></TR
  31483. ></TABLE
  31484. ></DIV
  31485. ></TD
  31486. ></TR
  31487. ></TABLE
  31488. ></DIV
  31489. ><DIV
  31490. CLASS="sect2"
  31491. ><HR><H3
  31492. CLASS="sect2"
  31493. ><A
  31494. NAME="language.oop5.basic.extends"
  31495. >extends</A
  31496. ></H3
  31497. ><P
  31498. >     A class can inherit methods and members of another class by using the
  31499.     extends keyword in the declaration. It is not possible to extend multiple
  31500.     classes, a class can only inherit one base class.
  31501.    </P
  31502. ><P
  31503. >     The inherited methods and members can be overrided, unless the parent
  31504.     class has defined a method as <A
  31505. HREF="#language.oop5.final"
  31506. >final</A
  31507. >,
  31508.     by redeclaring them within the same name defined in the parent class.
  31509.     It is possible to access the overrided method or members by
  31510.     referencing them with <A
  31511. HREF="#language.oop5.paamayim-nekudotayim"
  31512. >parent::</A
  31513. >
  31514.    </P
  31515. ><TABLE
  31516. WIDTH="100%"
  31517. BORDER="0"
  31518. CELLPADDING="0"
  31519. CELLSPACING="0"
  31520. CLASS="EXAMPLE"
  31521. ><TR
  31522. ><TD
  31523. ><DIV
  31524. CLASS="example"
  31525. ><A
  31526. NAME="AEN5408"
  31527. ></A
  31528. ><P
  31529. ><B
  31530. >Example 19-4. Simple Class Inherintance</B
  31531. ></P
  31532. ><TABLE
  31533. BORDER="0"
  31534. BGCOLOR="#E0E0E0"
  31535. CELLPADDING="5"
  31536. ><TR
  31537. ><TD
  31538. ><PRE
  31539. CLASS="php"
  31540. ><?php
  31541. class ExtendClass extends SimpleClass
  31542. {
  31543.     // Redefine the parent method
  31544.     function displayVar()
  31545.     {
  31546.         echo "Extending class\n";
  31547.         parent::displayVar();
  31548.     }
  31549. }
  31550.  
  31551. $extended = new ExtendClass();
  31552. $extended->displayVar();
  31553. ?></PRE
  31554. ></TD
  31555. ></TR
  31556. ></TABLE
  31557. ><P
  31558. >The above example will output:</P
  31559. ><TABLE
  31560. BORDER="0"
  31561. BGCOLOR="#E0E0E0"
  31562. CELLPADDING="5"
  31563. ><TR
  31564. ><TD
  31565. ><PRE
  31566. CLASS="screen"
  31567. >Extending class
  31568. a default value</PRE
  31569. ></TD
  31570. ></TR
  31571. ></TABLE
  31572. ></DIV
  31573. ></TD
  31574. ></TR
  31575. ></TABLE
  31576. ></DIV
  31577. ></DIV
  31578. ><DIV
  31579. CLASS="sect1"
  31580. ><HR><H2
  31581. CLASS="sect1"
  31582. ><A
  31583. NAME="language.oop5.autoload"
  31584. >Autoloading Objects</A
  31585. ></H2
  31586. ><P
  31587. >    Many developers writing object-oriented applications create
  31588.    one PHP source file per-class definition. One of the biggest
  31589.    annoyances is having to write a long list of needed includes
  31590.    at the beginning of each script (one for each class).
  31591.   </P
  31592. ><P
  31593. >    In PHP 5, this is no longer necessary. You may define an
  31594.    __autoload function which is automatically
  31595.    called in case you are trying to use a class which hasn't been
  31596.    defined yet. By calling this function the scripting engine is given
  31597.    a last chance to load the class before PHP fails with an error.
  31598.   </P
  31599. ><DIV
  31600. CLASS="note"
  31601. ><BLOCKQUOTE
  31602. CLASS="note"
  31603. ><P
  31604. ><B
  31605. >Note: </B
  31606. >
  31607.     Exceptions thrown in __autoload function cannot be catched in the
  31608.     <A
  31609. HREF="#language.exceptions"
  31610. >catch</A
  31611. > block and result in fatal
  31612.     error.
  31613.    </P
  31614. ></BLOCKQUOTE
  31615. ></DIV
  31616. ><P
  31617. >    <TABLE
  31618. WIDTH="100%"
  31619. BORDER="0"
  31620. CELLPADDING="0"
  31621. CELLSPACING="0"
  31622. CLASS="EXAMPLE"
  31623. ><TR
  31624. ><TD
  31625. ><DIV
  31626. CLASS="example"
  31627. ><A
  31628. NAME="AEN5421"
  31629. ></A
  31630. ><P
  31631. ><B
  31632. >Example 19-5. Autoload example</B
  31633. ></P
  31634. ><P
  31635. >      This example attempts to load the classes <VAR
  31636. CLASS="literal"
  31637. >MyClass1</VAR
  31638. >
  31639.      and <VAR
  31640. CLASS="literal"
  31641. >MyClass2</VAR
  31642. > from the files <TT
  31643. CLASS="filename"
  31644. >MyClass1.php</TT
  31645. >
  31646.      and <TT
  31647. CLASS="filename"
  31648. >MyClass2.php</TT
  31649. > respectively.
  31650.     </P
  31651. ><TABLE
  31652. BORDER="0"
  31653. BGCOLOR="#E0E0E0"
  31654. CELLPADDING="5"
  31655. ><TR
  31656. ><TD
  31657. ><PRE
  31658. CLASS="php"
  31659. ><?php
  31660. function __autoload($class_name) {
  31661.     require_once $class_name . '.php';
  31662. }
  31663.  
  31664. $obj  = new MyClass1();
  31665. $obj2 = new MyClass2(); 
  31666. ?></PRE
  31667. ></TD
  31668. ></TR
  31669. ></TABLE
  31670. ></DIV
  31671. ></TD
  31672. ></TR
  31673. ></TABLE
  31674. >
  31675.   </P
  31676. ></DIV
  31677. ><DIV
  31678. CLASS="sect1"
  31679. ><HR><H2
  31680. CLASS="sect1"
  31681. ><A
  31682. NAME="language.oop5.decon"
  31683. >Constructors and Destructors</A
  31684. ></H2
  31685. ><DIV
  31686. CLASS="sect2"
  31687. ><H3
  31688. CLASS="sect2"
  31689. ><A
  31690. NAME="language.oop5.decon.constructor"
  31691. >Constructor</A
  31692. ></H3
  31693. >void <B
  31694. CLASS="methodname"
  31695. >__construct</B
  31696. > ( [mixed args [, ...]])<BR
  31697. ></BR
  31698. ><P
  31699. >     PHP 5 allows developers to declare constructor methods for classes.
  31700.     Classes which have a constructor method call this method on each
  31701.     newly-created object, so it is suitable for any initialization that the
  31702.     object may need before it is used.
  31703.    </P
  31704. ><DIV
  31705. CLASS="note"
  31706. ><BLOCKQUOTE
  31707. CLASS="note"
  31708. ><P
  31709. ><B
  31710. >Note: </B
  31711. >
  31712.      Parent constructors are not called implicitly if the child class defines
  31713.      a constructor.  In order to run a parent constructor, a call to
  31714.      <B
  31715. CLASS="function"
  31716. >parent::__construct()</B
  31717. > within the child constructor is
  31718.      required.
  31719.     </P
  31720. ></BLOCKQUOTE
  31721. ></DIV
  31722. ><TABLE
  31723. WIDTH="100%"
  31724. BORDER="0"
  31725. CELLPADDING="0"
  31726. CELLSPACING="0"
  31727. CLASS="EXAMPLE"
  31728. ><TR
  31729. ><TD
  31730. ><DIV
  31731. CLASS="example"
  31732. ><A
  31733. NAME="AEN5445"
  31734. ></A
  31735. ><P
  31736. ><B
  31737. >Example 19-6. using new unified constructors</B
  31738. ></P
  31739. ><TABLE
  31740. BORDER="0"
  31741. BGCOLOR="#E0E0E0"
  31742. CELLPADDING="5"
  31743. ><TR
  31744. ><TD
  31745. ><PRE
  31746. CLASS="php"
  31747. ><?php
  31748. class BaseClass {
  31749.    function __construct() {
  31750.        print "In BaseClass constructor\n";
  31751.    }
  31752. }
  31753.  
  31754. class SubClass extends BaseClass {
  31755.    function __construct() {
  31756.        parent::__construct();
  31757.        print "In SubClass constructor\n";
  31758.    }
  31759. }
  31760.  
  31761. $obj = new BaseClass();
  31762. $obj = new SubClass();
  31763. ?></PRE
  31764. ></TD
  31765. ></TR
  31766. ></TABLE
  31767. ></DIV
  31768. ></TD
  31769. ></TR
  31770. ></TABLE
  31771. ><P
  31772. >     For backwards compatibility, if PHP 5 cannot find a 
  31773.     <B
  31774. CLASS="function"
  31775. >__construct()</B
  31776. > function for a given class, it will
  31777.     search for the old-style constructor function, by the name of the class.
  31778.     Effectively, it means that the only case that would have compatibility
  31779.     issues is if the class had a method named 
  31780.     <B
  31781. CLASS="function"
  31782. >__construct()</B
  31783. > which was used for different semantics.
  31784.    </P
  31785. ></DIV
  31786. ><DIV
  31787. CLASS="sect2"
  31788. ><HR><H3
  31789. CLASS="sect2"
  31790. ><A
  31791. NAME="language.oop5.decon.destructor"
  31792. >Destructor</A
  31793. ></H3
  31794. >void <B
  31795. CLASS="methodname"
  31796. >__destruct</B
  31797. > ( void )<BR
  31798. ></BR
  31799. ><P
  31800. >     PHP 5 introduces a destructor concept similar to that of other
  31801.     object-oriented languages, such as C++. The destructor method will be
  31802.     called as soon as all references to a particular object are removed or when
  31803.     the object is explicitly destroyed.
  31804.    </P
  31805. ><TABLE
  31806. WIDTH="100%"
  31807. BORDER="0"
  31808. CELLPADDING="0"
  31809. CELLSPACING="0"
  31810. CLASS="EXAMPLE"
  31811. ><TR
  31812. ><TD
  31813. ><DIV
  31814. CLASS="example"
  31815. ><A
  31816. NAME="AEN5458"
  31817. ></A
  31818. ><P
  31819. ><B
  31820. >Example 19-7. Destructor Example</B
  31821. ></P
  31822. ><TABLE
  31823. BORDER="0"
  31824. BGCOLOR="#E0E0E0"
  31825. CELLPADDING="5"
  31826. ><TR
  31827. ><TD
  31828. ><PRE
  31829. CLASS="php"
  31830. ><?php
  31831. class MyDestructableClass {
  31832.    function __construct() {
  31833.        print "In constructor\n";
  31834.        $this->name = "MyDestructableClass";
  31835.    }
  31836.  
  31837.    function __destruct() {
  31838.        print "Destroying " . $this->name . "\n";
  31839.    }
  31840. }
  31841.  
  31842. $obj = new MyDestructableClass();
  31843. ?></PRE
  31844. ></TD
  31845. ></TR
  31846. ></TABLE
  31847. ></DIV
  31848. ></TD
  31849. ></TR
  31850. ></TABLE
  31851. ><P
  31852. >     Like constructors, parent destructors will not be called implicitly by
  31853.     the engine. In order to run a parent destructor, one would have to
  31854.     explicitly call <B
  31855. CLASS="function"
  31856. >parent::__destruct()</B
  31857. > in the destructor
  31858.     body.
  31859.    </P
  31860. ></DIV
  31861. ></DIV
  31862. ><DIV
  31863. CLASS="sect1"
  31864. ><HR><H2
  31865. CLASS="sect1"
  31866. ><A
  31867. NAME="language.oop5.visibility"
  31868. >Visibility</A
  31869. ></H2
  31870. ><P
  31871. >    The visibility of a property or method can be defined by prefixing the
  31872.    declaration with the keywords: public, protected or private. Public
  31873.    declared items can be accessed everywhere. Protected limits access to
  31874.    inherited classes (and to the class that defines the item). Private limits
  31875.    visibility only to the class that defines the item.
  31876.   </P
  31877. ><DIV
  31878. CLASS="sect2"
  31879. ><HR><H3
  31880. CLASS="sect2"
  31881. ><A
  31882. NAME="language.oop5.visiblity-members"
  31883. >Members Visibility</A
  31884. ></H3
  31885. ><P
  31886. >    Class members must be defined with public, private, or protected.
  31887.    </P
  31888. ><P
  31889. >     <TABLE
  31890. WIDTH="100%"
  31891. BORDER="0"
  31892. CELLPADDING="0"
  31893. CELLSPACING="0"
  31894. CLASS="EXAMPLE"
  31895. ><TR
  31896. ><TD
  31897. ><DIV
  31898. CLASS="example"
  31899. ><A
  31900. NAME="AEN5470"
  31901. ></A
  31902. ><P
  31903. ><B
  31904. >Example 19-8. Member declaration</B
  31905. ></P
  31906. ><TABLE
  31907. BORDER="0"
  31908. BGCOLOR="#E0E0E0"
  31909. CELLPADDING="5"
  31910. ><TR
  31911. ><TD
  31912. ><PRE
  31913. CLASS="php"
  31914. ><?php
  31915. /**
  31916.  * Define MyClass
  31917.  */
  31918. class MyClass
  31919. {
  31920.     public $public = 'Public';
  31921.     protected $protected = 'Protected';
  31922.     private $private = 'Private';
  31923.  
  31924.     function printHello()
  31925.     {
  31926.         echo $this->public;
  31927.         echo $this->protected;
  31928.         echo $this->private;
  31929.     }
  31930. }
  31931.  
  31932. $obj = new MyClass();
  31933. echo $obj->public; // Works
  31934. echo $obj->protected; // Fatal Error
  31935. echo $obj->private; // Fatal Error
  31936. $obj->printHello(); // Shows Public, Protected and Private
  31937.  
  31938.  
  31939. /**
  31940.  * Define MyClass2
  31941.  */
  31942. class MyClass2 extends MyClass
  31943. {
  31944.     // We can redeclare the public and protected method, but not private
  31945.     protected $protected = 'Protected2';
  31946.  
  31947.     function printHello()
  31948.     {
  31949.         echo $this->public;
  31950.         echo $this->protected;
  31951.         echo $this->private;
  31952.     }
  31953. }
  31954.  
  31955. $obj2 = new MyClass2();
  31956. echo $obj->public; // Works
  31957. echo $obj2->private; // Undefined
  31958. echo $obj2->protected; // Fatal Error
  31959. $obj2->printHello(); // Shows Public, Protected2, not Private
  31960.  
  31961. ?></PRE
  31962. ></TD
  31963. ></TR
  31964. ></TABLE
  31965. ></DIV
  31966. ></TD
  31967. ></TR
  31968. ></TABLE
  31969. >
  31970.    </P
  31971. ><DIV
  31972. CLASS="note"
  31973. ><BLOCKQUOTE
  31974. CLASS="note"
  31975. ><P
  31976. ><B
  31977. >Note: </B
  31978. >
  31979.      The PHP 4 method of declaring a variable with the
  31980.      <SPAN
  31981. CLASS="emphasis"
  31982. ><I
  31983. CLASS="emphasis"
  31984. >var</I
  31985. ></SPAN
  31986. > keyword is no longer valid
  31987.      for PHP 5 objects. For compatibility a variable declared
  31988.      in php will be assumed with public visibility, and a
  31989.      <TT
  31990. CLASS="constant"
  31991. ><B
  31992. >E_STRICT</B
  31993. ></TT
  31994. > warning will be issued.
  31995.     </P
  31996. ></BLOCKQUOTE
  31997. ></DIV
  31998. ></DIV
  31999. ><DIV
  32000. CLASS="sect2"
  32001. ><HR><H3
  32002. CLASS="sect2"
  32003. ><A
  32004. NAME="language.oop5.visiblity-methods"
  32005. >Method Visibility</A
  32006. ></H3
  32007. ><P
  32008. >     Class methods must be defined with public, private, or protected. Methods
  32009.     without any declaration are defined as public.
  32010.    </P
  32011. ><P
  32012. >     <TABLE
  32013. WIDTH="100%"
  32014. BORDER="0"
  32015. CELLPADDING="0"
  32016. CELLSPACING="0"
  32017. CLASS="EXAMPLE"
  32018. ><TR
  32019. ><TD
  32020. ><DIV
  32021. CLASS="example"
  32022. ><A
  32023. NAME="AEN5481"
  32024. ></A
  32025. ><P
  32026. ><B
  32027. >Example 19-9. Method Declaration</B
  32028. ></P
  32029. ><TABLE
  32030. BORDER="0"
  32031. BGCOLOR="#E0E0E0"
  32032. CELLPADDING="5"
  32033. ><TR
  32034. ><TD
  32035. ><PRE
  32036. CLASS="php"
  32037. ><?php
  32038. /**
  32039.  * Define MyClass
  32040.  */
  32041. class MyClass
  32042. {
  32043.     // Contructors must be public
  32044.     public function __construct() { }
  32045.  
  32046.     // Declare a public method
  32047.     public function MyPublic() { }
  32048.  
  32049.     // Declare a protected method
  32050.     protected function MyProtected() { }
  32051.  
  32052.     // Declare a private method
  32053.     private function MyPrivate() { }
  32054.  
  32055.     // This is public
  32056.     function Foo()
  32057.     {
  32058.         $this->MyPublic();
  32059.         $this->MyProtected();
  32060.         $this->MyPrivate();
  32061.     }
  32062. }
  32063.  
  32064. $myclass = new MyClass;
  32065. $myclass->MyPublic(); // Works
  32066. $myclass->MyProtected(); // Fatal Error
  32067. $myclass->MyPrivate(); // Fatal Error
  32068. $myclass->Foo(); // Public, Protected and Private work
  32069.  
  32070.  
  32071. /**
  32072.  * Define MyClass2
  32073.  */
  32074. class MyClass2 extends MyClass
  32075. {
  32076.     // This is public
  32077.     function Foo2()
  32078.     {
  32079.         $this->MyPublic();
  32080.         $this->MyProtected();
  32081.         $this->MyPrivate(); // Fatal Error
  32082.     }
  32083. }
  32084.  
  32085. $myclass2 = new MyClass2;
  32086. $myclass2->MyPublic(); // Works
  32087. $myclass2->Foo2(); // Public and Protected work, not Private
  32088. ?></PRE
  32089. ></TD
  32090. ></TR
  32091. ></TABLE
  32092. ></DIV
  32093. ></TD
  32094. ></TR
  32095. ></TABLE
  32096. >
  32097.    </P
  32098. ></DIV
  32099. ></DIV
  32100. ><DIV
  32101. CLASS="sect1"
  32102. ><HR><H2
  32103. CLASS="sect1"
  32104. ><A
  32105. NAME="language.oop5.paamayim-nekudotayim"
  32106. >Scope Resolution Operator (::)</A
  32107. ></H2
  32108. ><P
  32109. >    The Scope Resolution Operator (also called Paamayim Nekudotayim) or in
  32110.    simpler terms, the double colon, is a token that allows access to 
  32111.    <A
  32112. HREF="#language.oop5.static"
  32113. >static</A
  32114. >,
  32115.    <A
  32116. HREF="#language.oop5.constants"
  32117. >constant</A
  32118. >, and overridden 
  32119.    members or methods of a class. 
  32120.   </P
  32121. ><P
  32122. >    When referencing these items from outside the class definition, use
  32123.    the name of the class.
  32124.   </P
  32125. ><P
  32126. >    Paamayim Nekudotayim would, at first, seem like a strange choice for
  32127.    naming a double-colon. However, while writing the Zend Engine 0.5
  32128.    (which powers PHP 3), that's what the Zend team decided to call it.
  32129.    It actually does mean double-colon - in Hebrew!
  32130.   </P
  32131. ><TABLE
  32132. WIDTH="100%"
  32133. BORDER="0"
  32134. CELLPADDING="0"
  32135. CELLSPACING="0"
  32136. CLASS="EXAMPLE"
  32137. ><TR
  32138. ><TD
  32139. ><DIV
  32140. CLASS="example"
  32141. ><A
  32142. NAME="AEN5491"
  32143. ></A
  32144. ><P
  32145. ><B
  32146. >Example 19-10. :: from outside the class definition</B
  32147. ></P
  32148. ><TABLE
  32149. BORDER="0"
  32150. BGCOLOR="#E0E0E0"
  32151. CELLPADDING="5"
  32152. ><TR
  32153. ><TD
  32154. ><PRE
  32155. CLASS="php"
  32156. ><?php
  32157. class MyClass {
  32158.     const CONST_VALUE = 'A constant value';
  32159. }
  32160.  
  32161. echo MyClass::CONST_VALUE;
  32162. ?></PRE
  32163. ></TD
  32164. ></TR
  32165. ></TABLE
  32166. ></DIV
  32167. ></TD
  32168. ></TR
  32169. ></TABLE
  32170. ><P
  32171. >    Two special keywords <VAR
  32172. CLASS="varname"
  32173. >self</VAR
  32174. > and <VAR
  32175. CLASS="varname"
  32176. >parent</VAR
  32177. >
  32178.    are used to access members or methods from inside the class definition.
  32179.   </P
  32180. ><TABLE
  32181. WIDTH="100%"
  32182. BORDER="0"
  32183. CELLPADDING="0"
  32184. CELLSPACING="0"
  32185. CLASS="EXAMPLE"
  32186. ><TR
  32187. ><TD
  32188. ><DIV
  32189. CLASS="example"
  32190. ><A
  32191. NAME="AEN5497"
  32192. ></A
  32193. ><P
  32194. ><B
  32195. >Example 19-11. :: from inside the class definition</B
  32196. ></P
  32197. ><TABLE
  32198. BORDER="0"
  32199. BGCOLOR="#E0E0E0"
  32200. CELLPADDING="5"
  32201. ><TR
  32202. ><TD
  32203. ><PRE
  32204. CLASS="php"
  32205. ><?php
  32206. class OtherClass extends MyClass
  32207. {
  32208.     public static $my_static = 'static var';
  32209.  
  32210.     public static function doubleColon() {
  32211.         echo parent::CONST_VALUE . "\n";
  32212.         echo self::$my_static . "\n";
  32213.     }
  32214. }
  32215.  
  32216. OtherClass::doubleColon();
  32217. ?></PRE
  32218. ></TD
  32219. ></TR
  32220. ></TABLE
  32221. ></DIV
  32222. ></TD
  32223. ></TR
  32224. ></TABLE
  32225. ><P
  32226. >    When an extending class overrides the parents definition of a method,
  32227.    PHP will not call the parent's method. It's up to the extended class
  32228.    on whether or not the parent's method is called. This also applies to <A
  32229. HREF="#language.oop5.decon"
  32230. >Constructors and Destructors</A
  32231. >, <A
  32232. HREF="#language.oop5.overloading"
  32233. >Overloading</A
  32234. >, and <A
  32235. HREF="#language.oop5.magic"
  32236. >Magic</A
  32237. > method definitions.
  32238.   </P
  32239. ><TABLE
  32240. WIDTH="100%"
  32241. BORDER="0"
  32242. CELLPADDING="0"
  32243. CELLSPACING="0"
  32244. CLASS="EXAMPLE"
  32245. ><TR
  32246. ><TD
  32247. ><DIV
  32248. CLASS="example"
  32249. ><A
  32250. NAME="AEN5504"
  32251. ></A
  32252. ><P
  32253. ><B
  32254. >Example 19-12. Calling a parent's method</B
  32255. ></P
  32256. ><TABLE
  32257. BORDER="0"
  32258. BGCOLOR="#E0E0E0"
  32259. CELLPADDING="5"
  32260. ><TR
  32261. ><TD
  32262. ><PRE
  32263. CLASS="php"
  32264. ><?php
  32265. class MyClass
  32266. {
  32267.     protected function myFunc() {
  32268.         echo "MyClass::myFunc()\n";
  32269.     }
  32270. }
  32271.  
  32272. class OtherClass extends MyClass
  32273. {
  32274.     // Override parent's definition
  32275.     public function myFunc()
  32276.     {
  32277.         // But still call the parent function
  32278.         parent::myFunc();
  32279.         echo "OtherClass::myFunc()\n";
  32280.     }
  32281. }
  32282.  
  32283. $class = new OtherClass();
  32284. $class->myFunc();
  32285. ?></PRE
  32286. ></TD
  32287. ></TR
  32288. ></TABLE
  32289. ></DIV
  32290. ></TD
  32291. ></TR
  32292. ></TABLE
  32293. ></DIV
  32294. ><DIV
  32295. CLASS="sect1"
  32296. ><HR><H2
  32297. CLASS="sect1"
  32298. ><A
  32299. NAME="language.oop5.static"
  32300. >Static Keyword</A
  32301. ></H2
  32302. ><P
  32303. >    Declaring class members or methods as static, makes them callable
  32304.    from outside the object context. A member or method declared
  32305.    with static can not be accessed with a variable that is an instance
  32306.    of the object and cannot be re-defined in an extending class.
  32307.   </P
  32308. ><P
  32309. >    The static declaration must be after the visibility declaration. For 
  32310.    compatibility with PHP 4, if no <A
  32311. HREF="#language.oop5.visibility"
  32312. >visibility</A
  32313. >
  32314.    declaration is used, then the member or method will be treated
  32315.    as if it was declared as <VAR
  32316. CLASS="literal"
  32317. >public static</VAR
  32318. >.
  32319.   </P
  32320. ><P
  32321. >    Because static methods are callable without an instance of
  32322.    the object created, the pseudo variable <VAR
  32323. CLASS="varname"
  32324. >$this</VAR
  32325. > is
  32326.    not available inside the method declared as static.
  32327.   </P
  32328. ><P
  32329. >    In fact <VAR
  32330. CLASS="literal"
  32331. >static</VAR
  32332. > method calls are resolved at compile 
  32333.    time. When using an explicit class name the method is already identified
  32334.    completley and no inheritance rules apply. If the call is done by 
  32335.    <VAR
  32336. CLASS="literal"
  32337. >self</VAR
  32338. > then <VAR
  32339. CLASS="literal"
  32340. >self</VAR
  32341. > is translated to 
  32342.    the current class, that is the class the code belongs to. Here also no 
  32343.    inheritance rules apply.
  32344.   </P
  32345. ><P
  32346. >    Static properties cannot be accessed through the object using the arrow
  32347.    operator ->.
  32348.   </P
  32349. ><TABLE
  32350. WIDTH="100%"
  32351. BORDER="0"
  32352. CELLPADDING="0"
  32353. CELLSPACING="0"
  32354. CLASS="EXAMPLE"
  32355. ><TR
  32356. ><TD
  32357. ><DIV
  32358. CLASS="example"
  32359. ><A
  32360. NAME="AEN5520"
  32361. ></A
  32362. ><P
  32363. ><B
  32364. >Example 19-13. Static member example</B
  32365. ></P
  32366. ><TABLE
  32367. BORDER="0"
  32368. BGCOLOR="#E0E0E0"
  32369. CELLPADDING="5"
  32370. ><TR
  32371. ><TD
  32372. ><PRE
  32373. CLASS="php"
  32374. ><?php
  32375. class Foo
  32376. {
  32377.     public static $my_static = 'foo';
  32378.  
  32379.     public function staticValue() {
  32380.         return self::$my_static;
  32381.     }
  32382. }
  32383.  
  32384. class Bar extends Foo
  32385. {
  32386.     public function fooStatic() {
  32387.         return parent::$my_static;
  32388.     }
  32389. }
  32390.  
  32391.  
  32392. print Foo::$my_static . "\n";
  32393.  
  32394. $foo = new Foo();
  32395. print $foo->staticValue() . "\n";
  32396. print $foo->my_static . "\n";      // Undefined "Property" my_static 
  32397.  
  32398. // $foo::my_static is not possible
  32399.  
  32400. print Bar::$my_static . "\n";
  32401. $bar = new Bar();
  32402. print $bar->fooStatic() . "\n";
  32403. ?></PRE
  32404. ></TD
  32405. ></TR
  32406. ></TABLE
  32407. ></DIV
  32408. ></TD
  32409. ></TR
  32410. ></TABLE
  32411. ><TABLE
  32412. WIDTH="100%"
  32413. BORDER="0"
  32414. CELLPADDING="0"
  32415. CELLSPACING="0"
  32416. CLASS="EXAMPLE"
  32417. ><TR
  32418. ><TD
  32419. ><DIV
  32420. CLASS="example"
  32421. ><A
  32422. NAME="AEN5523"
  32423. ></A
  32424. ><P
  32425. ><B
  32426. >Example 19-14. Static method example</B
  32427. ></P
  32428. ><TABLE
  32429. BORDER="0"
  32430. BGCOLOR="#E0E0E0"
  32431. CELLPADDING="5"
  32432. ><TR
  32433. ><TD
  32434. ><PRE
  32435. CLASS="php"
  32436. ><?php
  32437. class Foo {
  32438.     public static function aStaticMethod() {
  32439.         // ...
  32440.     }
  32441. }
  32442.  
  32443. Foo::aStaticMethod();
  32444. ?></PRE
  32445. ></TD
  32446. ></TR
  32447. ></TABLE
  32448. ></DIV
  32449. ></TD
  32450. ></TR
  32451. ></TABLE
  32452. ></DIV
  32453. ><DIV
  32454. CLASS="sect1"
  32455. ><HR><H2
  32456. CLASS="sect1"
  32457. ><A
  32458. NAME="language.oop5.constants"
  32459. >Object Constants</A
  32460. ></H2
  32461. ><P
  32462. >    It is possible to define constant values on a per-class basis remaining the
  32463.    same and unchangeable. Constants differ from normal variables in that you
  32464.    don't use the <VAR
  32465. CLASS="varname"
  32466. >$</VAR
  32467. > symbol to declare or use them.  Like
  32468.    <A
  32469. HREF="#language.oop5.static"
  32470. >static</A
  32471. > members, constant values
  32472.    can not be accessed from an instance of the object.
  32473.   </P
  32474. ><TABLE
  32475. WIDTH="100%"
  32476. BORDER="0"
  32477. CELLPADDING="0"
  32478. CELLSPACING="0"
  32479. CLASS="EXAMPLE"
  32480. ><TR
  32481. ><TD
  32482. ><DIV
  32483. CLASS="example"
  32484. ><A
  32485. NAME="AEN5531"
  32486. ></A
  32487. ><P
  32488. ><B
  32489. >Example 19-15. Defining and using a constant</B
  32490. ></P
  32491. ><TABLE
  32492. BORDER="0"
  32493. BGCOLOR="#E0E0E0"
  32494. CELLPADDING="5"
  32495. ><TR
  32496. ><TD
  32497. ><PRE
  32498. CLASS="php"
  32499. ><?php
  32500. class MyClass
  32501. {
  32502.     const constant = 'constant value';
  32503.  
  32504.     function showConstant() {
  32505.         echo  self::constant . "\n";
  32506.     }
  32507. }
  32508.  
  32509. echo MyClass::constant . "\n";
  32510.  
  32511. $class = new MyClass();
  32512. $class->showConstant();
  32513. // echo $class::constant;  is not allowed
  32514. ?></PRE
  32515. ></TD
  32516. ></TR
  32517. ></TABLE
  32518. ></DIV
  32519. ></TD
  32520. ></TR
  32521. ></TABLE
  32522. ></DIV
  32523. ><DIV
  32524. CLASS="sect1"
  32525. ><HR><H2
  32526. CLASS="sect1"
  32527. ><A
  32528. NAME="language.oop5.abstract"
  32529. >Object Abstraction</A
  32530. ></H2
  32531. ><P
  32532. >    PHP 5 introduces abstract classes and methods. It is not allowed to create
  32533.    an instance of a class that has been defined as abstract.  Any class that
  32534.    contains at least one abstract method must also be abstract.  Methods
  32535.    defined as abstract simply declare the method's signature they cannot
  32536.    define the implementation.
  32537.   </P
  32538. ><P
  32539. >    The class that implements the abstract method must define with the same
  32540.    <A
  32541. HREF="#language.oop5.visibility"
  32542. >visibillity</A
  32543. > or weaker. If the
  32544.    abstract method is defined as protected, the function implementation must be
  32545.    defined as either protected or public.
  32546.   </P
  32547. ><TABLE
  32548. WIDTH="100%"
  32549. BORDER="0"
  32550. CELLPADDING="0"
  32551. CELLSPACING="0"
  32552. CLASS="EXAMPLE"
  32553. ><TR
  32554. ><TD
  32555. ><DIV
  32556. CLASS="example"
  32557. ><A
  32558. NAME="AEN5539"
  32559. ></A
  32560. ><P
  32561. ><B
  32562. >Example 19-16. Abstract class example</B
  32563. ></P
  32564. ><TABLE
  32565. BORDER="0"
  32566. BGCOLOR="#E0E0E0"
  32567. CELLPADDING="5"
  32568. ><TR
  32569. ><TD
  32570. ><PRE
  32571. CLASS="php"
  32572. ><?php
  32573. abstract class AbstractClass
  32574. {
  32575.     // Force Extending class to define this method
  32576.     abstract protected function getValue();
  32577.  
  32578.     // Common method
  32579.     public function printOut() {
  32580.         print $this->getValue();
  32581.     }
  32582. }
  32583.  
  32584. class ConcreteClass1 extends AbstractClass
  32585. {
  32586.     protected function getValue() {
  32587.         return "ConcreteClass1";
  32588.     }
  32589. }
  32590.  
  32591. class ConcreteClass2 extends AbstractClass
  32592. {
  32593.     protected function getValue() {
  32594.         return "ConcreteClass2";
  32595.     }
  32596.  
  32597. }
  32598.  
  32599. $class1 = new ConcreteClass1;
  32600. $class1->printOut();
  32601.  
  32602. $class2 = new ConcreteClass2;
  32603. $class2->printOut();
  32604. ?></PRE
  32605. ></TD
  32606. ></TR
  32607. ></TABLE
  32608. ></DIV
  32609. ></TD
  32610. ></TR
  32611. ></TABLE
  32612. ><P
  32613. >     Old code that has no user-defined classes or functions named
  32614.     'abstract' should run without modifications.
  32615.    </P
  32616. ></DIV
  32617. ><DIV
  32618. CLASS="sect1"
  32619. ><HR><H2
  32620. CLASS="sect1"
  32621. ><A
  32622. NAME="language.oop5.interfaces"
  32623. >Object Interfaces</A
  32624. ></H2
  32625. ><P
  32626. >    Object interfaces allow you to create code which specifies which methods a
  32627.    class must implement, without having to define how these methods are
  32628.    handled.
  32629.   </P
  32630. ><P
  32631. >    Interfaces are defined using the interface keyword, in the same way as a
  32632.    standard class, but without any of the methods having their contents
  32633.    defined. Classes which implement an interface should do so using the
  32634.    implements keyword, and must have definitions for all the methods listed
  32635.    in the interface. Classes may implement more than one interface if desired
  32636.    by listing each interface split by a comma.
  32637.   </P
  32638. ><P
  32639. >    All methods declared in an interface must be public, this is the nature of an
  32640.    interface.
  32641.   </P
  32642. ><P
  32643. >    Stating that a class implements an interface, and then not implementing all
  32644.    the methods in the interface will result in a fatal error telling you which
  32645.    methods have not been implemented.
  32646.   </P
  32647. ><TABLE
  32648. WIDTH="100%"
  32649. BORDER="0"
  32650. CELLPADDING="0"
  32651. CELLSPACING="0"
  32652. CLASS="EXAMPLE"
  32653. ><TR
  32654. ><TD
  32655. ><DIV
  32656. CLASS="example"
  32657. ><A
  32658. NAME="AEN5549"
  32659. ></A
  32660. ><P
  32661. ><B
  32662. >Example 19-17. Interface example</B
  32663. ></P
  32664. ><TABLE
  32665. BORDER="0"
  32666. BGCOLOR="#E0E0E0"
  32667. CELLPADDING="5"
  32668. ><TR
  32669. ><TD
  32670. ><PRE
  32671. CLASS="php"
  32672. ><?php
  32673. // Declare the interface 'iTemplate'
  32674. interface iTemplate
  32675. {
  32676.     public function setVariable($name, $var);
  32677.     public function getHtml($template);
  32678. }
  32679.  
  32680. // Implement the interface
  32681. // This will work
  32682. class Template implements iTemplate
  32683. {
  32684.     private $vars = array();
  32685.   
  32686.     public function setVariable($name, $var)
  32687.     {
  32688.         $this->vars[$name] = $var;
  32689.     }
  32690.   
  32691.     public function getHtml($template)
  32692.     {
  32693.         foreach($this->vars as $name => $value) {
  32694.             $template = str_replace('{' . $name . '}', $value, $template);
  32695.         }
  32696.  
  32697.         return $template;
  32698.     }
  32699. }
  32700.  
  32701. // This will not work
  32702. // Fatal error: Class BadTemplate contains 1 abstract methods
  32703. // and must therefore be declared abstract (iTemplate::getHtml)
  32704. class BadTemplate implements iTemplate
  32705. {
  32706.     private $vars = array();
  32707.   
  32708.     public function setVariable($name, $var)
  32709.     {
  32710.         $this->vars[$name] = $var;
  32711.     }
  32712. }
  32713.  
  32714. ?></PRE
  32715. ></TD
  32716. ></TR
  32717. ></TABLE
  32718. ></DIV
  32719. ></TD
  32720. ></TR
  32721. ></TABLE
  32722. ></DIV
  32723. ><DIV
  32724. CLASS="sect1"
  32725. ><HR><H2
  32726. CLASS="sect1"
  32727. ><A
  32728. NAME="language.oop5.overloading"
  32729. >Overloading</A
  32730. ></H2
  32731. ><P
  32732. >    Both method calls and member accesses can be overloaded via the
  32733.    __call, __get and __set methods. These methods will only be
  32734.    triggered when your object or inherited object doesn't contain the 
  32735.    member or method you're trying to access.
  32736.   </P
  32737. ><DIV
  32738. CLASS="sect2"
  32739. ><HR><H3
  32740. CLASS="sect2"
  32741. ><A
  32742. NAME="language.oop5.overloading.members"
  32743. >Member overloading</A
  32744. ></H3
  32745. >void <B
  32746. CLASS="methodname"
  32747. >__set</B
  32748. > ( string name, mixed value)<BR
  32749. ></BR
  32750. >mixed <B
  32751. CLASS="methodname"
  32752. >__get</B
  32753. > ( mixed name)<BR
  32754. ></BR
  32755. ><P
  32756. >     Class members can be overloaded to run custom code defined in your class
  32757.     by defining these specially named methods. The <VAR
  32758. CLASS="varname"
  32759. >$name</VAR
  32760.     parameter used is the name of the variable that should be set or retrieved.
  32761.     The __set() method's <VAR
  32762. CLASS="varname"
  32763. >$value</VAR
  32764. > parameter specifies the 
  32765.     value that the object should set set the <VAR
  32766. CLASS="varname"
  32767. >$name</VAR
  32768. >.
  32769.    </P
  32770. ><TABLE
  32771. WIDTH="100%"
  32772. BORDER="0"
  32773. CELLPADDING="0"
  32774. CELLSPACING="0"
  32775. CLASS="EXAMPLE"
  32776. ><TR
  32777. ><TD
  32778. ><DIV
  32779. CLASS="example"
  32780. ><A
  32781. NAME="AEN5576"
  32782. ></A
  32783. ><P
  32784. ><B
  32785. >Example 19-18. overloading with __get and __set example</B
  32786. ></P
  32787. ><TABLE
  32788. BORDER="0"
  32789. BGCOLOR="#E0E0E0"
  32790. CELLPADDING="5"
  32791. ><TR
  32792. ><TD
  32793. ><PRE
  32794. CLASS="php"
  32795. ><?php
  32796. class Setter
  32797. {
  32798.     public $n;
  32799.     private $x = array("a" => 1, "b" => 2, "c" => 3);
  32800.  
  32801.     function __get($nm)
  32802.     {
  32803.         print "Getting [$nm]\n";
  32804.  
  32805.         if (isset($this->x[$nm])) {
  32806.             $r = $this->x[$nm];
  32807.             print "Returning: $r\n";
  32808.             return $r;
  32809.         } else {
  32810.             echo "Nothing!\n";
  32811.         }
  32812.     }
  32813.  
  32814.     function __set($nm, $val)
  32815.     {
  32816.         print "Setting [$nm] to $val\n";
  32817.  
  32818.         if (isset($this->x[$nm])) {
  32819.             $this->x[$nm] = $val;
  32820.             echo "OK!\n";
  32821.         } else {
  32822.             echo "Not OK!\n";
  32823.         }
  32824.     }
  32825. }
  32826.  
  32827. $foo = new Setter();
  32828. $foo->n = 1;
  32829. $foo->a = 100;
  32830. $foo->a++;
  32831. $foo->z++;
  32832. var_dump($foo);
  32833. ?></PRE
  32834. ></TD
  32835. ></TR
  32836. ></TABLE
  32837. ><P
  32838. >      Will output:
  32839.     </P
  32840. ><TABLE
  32841. BORDER="0"
  32842. BGCOLOR="#E0E0E0"
  32843. CELLPADDING="5"
  32844. ><TR
  32845. ><TD
  32846. ><PRE
  32847. CLASS="php"
  32848. >Setting [a] to 100
  32849. OK!
  32850. Getting [a]
  32851. Returning: 100
  32852. Setting [a] to 101
  32853. OK!
  32854. Getting [z]
  32855. Nothing!
  32856. Setting [z] to 1
  32857. Not OK!
  32858. object(Setter)#1 (2) {
  32859.     ["n"]=>
  32860.     int(1)
  32861.     ["x:private"]=>
  32862.     array(3) {
  32863.         ["a"]=>
  32864.         int(101)
  32865.         ["b"]=>
  32866.         int(2)
  32867.         ["c"]=>
  32868.         int(3)
  32869.     }
  32870. }</PRE
  32871. ></TD
  32872. ></TR
  32873. ></TABLE
  32874. ></DIV
  32875. ></TD
  32876. ></TR
  32877. ></TABLE
  32878. ></DIV
  32879. ><DIV
  32880. CLASS="sect2"
  32881. ><HR><H3
  32882. CLASS="sect2"
  32883. ><A
  32884. NAME="language.oop5.overloading.methods"
  32885. >Method overloading</A
  32886. ></H3
  32887. >mixed <B
  32888. CLASS="methodname"
  32889. >__call</B
  32890. > ( string name, array arguments)<BR
  32891. ></BR
  32892. ><P
  32893. >     Class methods can be overloaded to run custom code defined in your class
  32894.     by defining this specially named method. The <VAR
  32895. CLASS="varname"
  32896. >$name</VAR
  32897.     parameter used is the name as the function name that was requested 
  32898.     to be used. The arguments that were passed in the function will be 
  32899.     defined as an array in the <VAR
  32900. CLASS="varname"
  32901. >$arguments</VAR
  32902. > parameter.
  32903.     The value returned from the __call() method will be returned to the
  32904.     caller of the method.
  32905.    </P
  32906. ><TABLE
  32907. WIDTH="100%"
  32908. BORDER="0"
  32909. CELLPADDING="0"
  32910. CELLSPACING="0"
  32911. CLASS="EXAMPLE"
  32912. ><TR
  32913. ><TD
  32914. ><DIV
  32915. CLASS="example"
  32916. ><A
  32917. NAME="AEN5595"
  32918. ></A
  32919. ><P
  32920. ><B
  32921. >Example 19-19. overloading with __call example</B
  32922. ></P
  32923. ><TABLE
  32924. BORDER="0"
  32925. BGCOLOR="#E0E0E0"
  32926. CELLPADDING="5"
  32927. ><TR
  32928. ><TD
  32929. ><PRE
  32930. CLASS="php"
  32931. ><?php
  32932. class Caller
  32933. {
  32934.     private $x = array(1, 2, 3);
  32935.  
  32936.     function __call($m, $a)
  32937.     {
  32938.         print "Method $m called:\n";
  32939.         var_dump($a);
  32940.         return $this->x;
  32941.     }
  32942. }
  32943.  
  32944. $foo = new Caller();
  32945. $a = $foo->test(1, "2", 3.4, true);
  32946. var_dump($a);
  32947. ?></PRE
  32948. ></TD
  32949. ></TR
  32950. ></TABLE
  32951. ><P
  32952. >      Will Output:
  32953.     </P
  32954. ><TABLE
  32955. BORDER="0"
  32956. BGCOLOR="#E0E0E0"
  32957. CELLPADDING="5"
  32958. ><TR
  32959. ><TD
  32960. ><PRE
  32961. CLASS="php"
  32962. >Method test called:
  32963. array(4) {
  32964.     [0]=>
  32965.     int(1)
  32966.     [1]=>
  32967.     string(1) "2"
  32968.     [2]=>
  32969.     float(3.4)
  32970.     [3]=>
  32971.     bool(true)
  32972. }
  32973. array(3) {
  32974.     [0]=>
  32975.     int(1)
  32976.     [1]=>
  32977.     int(2)
  32978.     [2]=>
  32979.     int(3)
  32980. }</PRE
  32981. ></TD
  32982. ></TR
  32983. ></TABLE
  32984. ></DIV
  32985. ></TD
  32986. ></TR
  32987. ></TABLE
  32988. ></DIV
  32989. ></DIV
  32990. ><DIV
  32991. CLASS="sect1"
  32992. ><HR><H2
  32993. CLASS="sect1"
  32994. ><A
  32995. NAME="language.oop5.iterations"
  32996. >Object Iteration</A
  32997. ></H2
  32998. ><P
  32999. >
  33000.    PHP 5 provides a way for objects to be defined so it is possible to iterate
  33001.    through a list of items, with, for example a <A
  33002. HREF="#control-structures.foreach"
  33003. >foreach</A
  33004. > statement. By default,
  33005.    all <A
  33006. HREF="#language.oop5.visibility"
  33007. >visible</A
  33008. > properties will be used
  33009.    for the iteration.
  33010.  
  33011.   </P
  33012. ><TABLE
  33013. WIDTH="100%"
  33014. BORDER="0"
  33015. CELLPADDING="0"
  33016. CELLSPACING="0"
  33017. CLASS="EXAMPLE"
  33018. ><TR
  33019. ><TD
  33020. ><DIV
  33021. CLASS="example"
  33022. ><A
  33023. NAME="AEN5605"
  33024. ></A
  33025. ><P
  33026. ><B
  33027. >Example 19-20. Simple Object Iteration</B
  33028. ></P
  33029. ><TABLE
  33030. BORDER="0"
  33031. BGCOLOR="#E0E0E0"
  33032. CELLPADDING="5"
  33033. ><TR
  33034. ><TD
  33035. ><PRE
  33036. CLASS="php"
  33037. ><?php
  33038. class MyClass
  33039. {
  33040.     public $var1 = 'value 1';
  33041.     public $var2 = 'value 2';
  33042.     public $var3 = 'value 3';
  33043.  
  33044.     protected $protected = 'protected var';
  33045.     private   $private   = 'private var';
  33046.  
  33047.     function iterateVisible() {
  33048.        echo "MyClass::iterateVisible:\n";
  33049.        foreach($this as $key => $value) {
  33050.            print "$key => $value\n";
  33051.        }
  33052.     }
  33053. }
  33054.  
  33055. $class = new MyClass();
  33056.  
  33057. foreach($class as $key => $value) {
  33058.     print "$key => $value\n";
  33059. }
  33060. echo "\n";
  33061.  
  33062.  
  33063. $class->iterateVisible();
  33064.  
  33065. ?></PRE
  33066. ></TD
  33067. ></TR
  33068. ></TABLE
  33069. ><P
  33070. >The above example will output:</P
  33071. ><TABLE
  33072. BORDER="0"
  33073. BGCOLOR="#E0E0E0"
  33074. CELLPADDING="5"
  33075. ><TR
  33076. ><TD
  33077. ><PRE
  33078. CLASS="php"
  33079. >var1 => value 1
  33080. var2 => value 2
  33081. var3 => value 3
  33082.  
  33083. MyClass::iterateVisible:
  33084. var1 => value 1
  33085. var2 => value 2
  33086. var3 => value 3
  33087. protected => protected var
  33088. private => private var</PRE
  33089. ></TD
  33090. ></TR
  33091. ></TABLE
  33092. ></DIV
  33093. ></TD
  33094. ></TR
  33095. ></TABLE
  33096. ><P
  33097. >   As the output shows, the <A
  33098. HREF="#control-structures.foreach"
  33099. >foreach</A
  33100. > iterated through all
  33101.   <A
  33102. HREF="#language.oop5.visibility"
  33103. >visible</A
  33104. > variables that can be
  33105.   accessed. To take it a step further you can <VAR
  33106. CLASS="varname"
  33107. >implement</VAR
  33108. > one
  33109.   of PHP 5's internal <A
  33110. HREF="#language.oop5.interfaces"
  33111. >interface</A
  33112. > named
  33113.   <VAR
  33114. CLASS="varname"
  33115. >Iterator</VAR
  33116. >. This allows the object to decide what and how
  33117.   the object will be iterated.
  33118.  </P
  33119. ><TABLE
  33120. WIDTH="100%"
  33121. BORDER="0"
  33122. CELLPADDING="0"
  33123. CELLSPACING="0"
  33124. CLASS="EXAMPLE"
  33125. ><TR
  33126. ><TD
  33127. ><DIV
  33128. CLASS="example"
  33129. ><A
  33130. NAME="AEN5616"
  33131. ></A
  33132. ><P
  33133. ><B
  33134. >Example 19-21. Object Iteration implementing Iterator</B
  33135. ></P
  33136. ><TABLE
  33137. BORDER="0"
  33138. BGCOLOR="#E0E0E0"
  33139. CELLPADDING="5"
  33140. ><TR
  33141. ><TD
  33142. ><PRE
  33143. CLASS="php"
  33144. ><?php
  33145. class MyIterator implements Iterator
  33146. {
  33147.     private $var = array();
  33148.  
  33149.     public function __construct($array)
  33150.     {
  33151.         if (is_array($array)) {
  33152.             $this->var = $array;
  33153.         }
  33154.     }
  33155.  
  33156.     public function rewind() {
  33157.         echo "rewinding\n";
  33158.         reset($this->var);
  33159.     }
  33160.  
  33161.     public function current() {
  33162.         $var = current($this->var);
  33163.         echo "current: $var\n";
  33164.         return $var;
  33165.     }
  33166.  
  33167.     public function key() {
  33168.         $var = key($this->var);
  33169.         echo "key: $var\n";
  33170.         return $var;
  33171.     }
  33172.  
  33173.     public function next() {
  33174.         $var = next($this->var);
  33175.         echo "next: $var\n";
  33176.         return $var;
  33177.     }
  33178.  
  33179.     public function valid() {
  33180.         $var = $this->current() !== false;
  33181.         echo "valid: {$var}\n";
  33182.         return $var;
  33183.     }
  33184. }
  33185.  
  33186. $values = array(1,2,3);
  33187. $it = new MyIterator($values);
  33188.  
  33189. foreach ($it as $a => $b) {
  33190.     print "$a: $b\n";
  33191. }
  33192. ?></PRE
  33193. ></TD
  33194. ></TR
  33195. ></TABLE
  33196. ><P
  33197. >      Will output:
  33198.    </P
  33199. ><TABLE
  33200. BORDER="0"
  33201. BGCOLOR="#E0E0E0"
  33202. CELLPADDING="5"
  33203. ><TR
  33204. ><TD
  33205. ><PRE
  33206. CLASS="php"
  33207. >rewinding
  33208. current: 1
  33209. valid: 1
  33210. current: 1
  33211. key: 0
  33212. 0: 1
  33213. next: 2
  33214. current: 2
  33215. valid: 1
  33216. current: 2
  33217. key: 1
  33218. 1: 2
  33219. next: 3
  33220. current: 3
  33221. valid: 1
  33222. current: 3
  33223. key: 2
  33224. 2: 3
  33225. next:
  33226. current:
  33227. valid:</PRE
  33228. ></TD
  33229. ></TR
  33230. ></TABLE
  33231. ></DIV
  33232. ></TD
  33233. ></TR
  33234. ></TABLE
  33235. ><P
  33236. >    You can also define your class so that it doesn't have to define
  33237.    all the <VAR
  33238. CLASS="varname"
  33239. >Iterator</VAR
  33240. > functions by simply implementing
  33241.    the PHP 5 <VAR
  33242. CLASS="varname"
  33243. >IteratorAggregate</VAR
  33244. > interface.
  33245.   </P
  33246. ><TABLE
  33247. WIDTH="100%"
  33248. BORDER="0"
  33249. CELLPADDING="0"
  33250. CELLSPACING="0"
  33251. CLASS="EXAMPLE"
  33252. ><TR
  33253. ><TD
  33254. ><DIV
  33255. CLASS="example"
  33256. ><A
  33257. NAME="AEN5624"
  33258. ></A
  33259. ><P
  33260. ><B
  33261. >Example 19-22. Object Iteration implementing IteratorAggregate</B
  33262. ></P
  33263. ><TABLE
  33264. BORDER="0"
  33265. BGCOLOR="#E0E0E0"
  33266. CELLPADDING="5"
  33267. ><TR
  33268. ><TD
  33269. ><PRE
  33270. CLASS="php"
  33271. ><?php
  33272. class MyCollection implements IteratorAggregate
  33273. {
  33274.     private $items = array();
  33275.     private $count = 0;
  33276.  
  33277.     // Required definition of interface IteratorAggregate
  33278.     public function getIterator() {
  33279.         return new MyIterator($this->items);
  33280.     }
  33281.  
  33282.     public function add($value) {
  33283.         $this->items[$this->count++] = $value;
  33284.     }
  33285. }
  33286.  
  33287. $coll = new MyCollection();
  33288. $coll->add('value 1');
  33289. $coll->add('value 2');
  33290. $coll->add('value 3');
  33291.  
  33292. foreach ($coll as $key => $val) {
  33293.     echo "key/value: [$key -> $val]\n\n";
  33294. }
  33295. ?></PRE
  33296. ></TD
  33297. ></TR
  33298. ></TABLE
  33299. ><P
  33300. >      Will output:
  33301.    </P
  33302. ><TABLE
  33303. BORDER="0"
  33304. BGCOLOR="#E0E0E0"
  33305. CELLPADDING="5"
  33306. ><TR
  33307. ><TD
  33308. ><PRE
  33309. CLASS="php"
  33310. >rewinding
  33311. current: value 1
  33312. valid: 1
  33313. current: value 1
  33314. key: 0
  33315. key/value: [0 -> value 1]
  33316.  
  33317. next: value 2
  33318. current: value 2
  33319. valid: 1
  33320. current: value 2
  33321. key: 1
  33322. key/value: [1 -> value 2]
  33323.  
  33324. next: value 3
  33325. current: value 3
  33326. valid: 1
  33327. current: value 3
  33328. key: 2
  33329. key/value: [2 -> value 3]
  33330.  
  33331. next:
  33332. current:
  33333. valid:</PRE
  33334. ></TD
  33335. ></TR
  33336. ></TABLE
  33337. ></DIV
  33338. ></TD
  33339. ></TR
  33340. ></TABLE
  33341. ><DIV
  33342. CLASS="note"
  33343. ><BLOCKQUOTE
  33344. CLASS="note"
  33345. ><P
  33346. ><B
  33347. >Note: </B
  33348. >
  33349.     For more examples of iterators, see the
  33350.     <A
  33351. HREF="#ref.spl"
  33352. >SPL Extension</A
  33353. >.
  33354.    </P
  33355. ></BLOCKQUOTE
  33356. ></DIV
  33357. ></DIV
  33358. ><DIV
  33359. CLASS="sect1"
  33360. ><HR><H2
  33361. CLASS="sect1"
  33362. ><A
  33363. NAME="language.oop5.patterns"
  33364. >Patterns</A
  33365. ></H2
  33366. ><P
  33367. >   Patterns are ways to describe best practices and good designs.
  33368.   They show a flexible solution to common programming problems.
  33369.  </P
  33370. ><DIV
  33371. CLASS="sect2"
  33372. ><HR><H3
  33373. CLASS="sect2"
  33374. ><A
  33375. NAME="language.oop5.patterns.factory"
  33376. >Factory</A
  33377. ></H3
  33378. ><P
  33379. >    The Factory pattern allows for the instantation of objects
  33380.    at runtime. It is called a Factory Pattern since it is
  33381.    responsible for "manufacturing" an object.
  33382.   </P
  33383. ><TABLE
  33384. WIDTH="100%"
  33385. BORDER="0"
  33386. CELLPADDING="0"
  33387. CELLSPACING="0"
  33388. CLASS="EXAMPLE"
  33389. ><TR
  33390. ><TD
  33391. ><DIV
  33392. CLASS="example"
  33393. ><A
  33394. NAME="AEN5638"
  33395. ></A
  33396. ><P
  33397. ><B
  33398. >Example 19-23. Factory Method</B
  33399. ></P
  33400. ><TABLE
  33401. BORDER="0"
  33402. BGCOLOR="#E0E0E0"
  33403. CELLPADDING="5"
  33404. ><TR
  33405. ><TD
  33406. ><PRE
  33407. CLASS="php"
  33408. ><?php
  33409. class Example
  33410. {
  33411.     // The factory method
  33412.     function &factory($type)
  33413.     {
  33414.         if (include_once 'Drivers/' . $type . '.php') {
  33415.             $classname = 'Driver_' . $type;
  33416.             return new $classname;
  33417.         } else {
  33418.             throw new Exception ('Driver not found');
  33419.         }
  33420.     }
  33421. }
  33422. ?></PRE
  33423. ></TD
  33424. ></TR
  33425. ></TABLE
  33426. ><P
  33427. >     Defining this method in a class allows drivers to be loaded on the
  33428.     fly. If the <VAR
  33429. CLASS="literal"
  33430. >Example</VAR
  33431. > class was a database
  33432.     abstraction class, loading a <VAR
  33433. CLASS="literal"
  33434. >MySQL</VAR
  33435. > and
  33436.     <VAR
  33437. CLASS="literal"
  33438. >SQLite</VAR
  33439. > driver could be done as follows:
  33440.    </P
  33441. ><TABLE
  33442. BORDER="0"
  33443. BGCOLOR="#E0E0E0"
  33444. CELLPADDING="5"
  33445. ><TR
  33446. ><TD
  33447. ><PRE
  33448. CLASS="php"
  33449. ><?php
  33450. // Load a MySQL Driver
  33451. $mysql = Example::factory('MySQL');
  33452.  
  33453. // Load a SQLite Driver
  33454. $sqlite = Example::factory('SQLite');
  33455. ?></PRE
  33456. ></TD
  33457. ></TR
  33458. ></TABLE
  33459. ></DIV
  33460. ></TD
  33461. ></TR
  33462. ></TABLE
  33463. ></DIV
  33464. ><DIV
  33465. CLASS="sect2"
  33466. ><HR><H3
  33467. CLASS="sect2"
  33468. ><A
  33469. NAME="language.oop5.patterns.singleton"
  33470. >Singleton</A
  33471. ></H3
  33472. ><P
  33473. >    The Singleton pattern applies to situations in which
  33474.    there needs to be a single instance of a class.
  33475.    The most common example of this is a database connection.
  33476.    Implementing this pattern allows a programmer to make this
  33477.    single instance easily accessible by many other objects.
  33478.   </P
  33479. ><TABLE
  33480. WIDTH="100%"
  33481. BORDER="0"
  33482. CELLPADDING="0"
  33483. CELLSPACING="0"
  33484. CLASS="EXAMPLE"
  33485. ><TR
  33486. ><TD
  33487. ><DIV
  33488. CLASS="example"
  33489. ><A
  33490. NAME="AEN5649"
  33491. ></A
  33492. ><P
  33493. ><B
  33494. >Example 19-24. Singleton Function</B
  33495. ></P
  33496. ><TABLE
  33497. BORDER="0"
  33498. BGCOLOR="#E0E0E0"
  33499. CELLPADDING="5"
  33500. ><TR
  33501. ><TD
  33502. ><PRE
  33503. CLASS="php"
  33504. ><?php
  33505. class Example
  33506. {
  33507.     // Hold an instance of the class
  33508.     static private $instance;
  33509.     
  33510.     // A private constructor
  33511.     private function __construct() 
  33512.     {
  33513.         echo 'I am constructed';
  33514.     }
  33515.  
  33516.     // The singleton method
  33517.     static public function singleton() 
  33518.     {
  33519.         if (!isset(self::$instance)) {
  33520.             $c = __CLASS__;
  33521.             self::$instance = new $c;
  33522.         }
  33523.  
  33524.         return self::$instance;
  33525.     }
  33526.     
  33527.     // Example method
  33528.     public function bark()
  33529.     {
  33530.         echo 'Woof!';
  33531.     }
  33532. }
  33533.  
  33534. ?></PRE
  33535. ></TD
  33536. ></TR
  33537. ></TABLE
  33538. ><P
  33539. >     This allows a single instance of the <VAR
  33540. CLASS="literal"
  33541. >Example</VAR
  33542. >
  33543.     class to be retrieved.
  33544.    </P
  33545. ><TABLE
  33546. BORDER="0"
  33547. BGCOLOR="#E0E0E0"
  33548. CELLPADDING="5"
  33549. ><TR
  33550. ><TD
  33551. ><PRE
  33552. CLASS="php"
  33553. ><?php
  33554. // This would fail because the constructor is private
  33555. $test = new Example;
  33556.  
  33557. // This will always retrieve a single instance of the class
  33558. $test = Example::singleton();
  33559. $test->bark();
  33560.  
  33561. ?></PRE
  33562. ></TD
  33563. ></TR
  33564. ></TABLE
  33565. ></DIV
  33566. ></TD
  33567. ></TR
  33568. ></TABLE
  33569. ></DIV
  33570. ></DIV
  33571. ><DIV
  33572. CLASS="sect1"
  33573. ><HR><H2
  33574. CLASS="sect1"
  33575. ><A
  33576. NAME="language.oop5.magic"
  33577. >Magic Methods</A
  33578. ></H2
  33579. ><P
  33580. >    The function names
  33581.    <VAR
  33582. CLASS="literal"
  33583. >__construct</VAR
  33584. >,
  33585.    <VAR
  33586. CLASS="literal"
  33587. >__destruct</VAR
  33588. >
  33589.    (see <A
  33590. HREF="#language.oop5.decon"
  33591. >Constructors and Destructors</A
  33592. >),
  33593.    <VAR
  33594. CLASS="literal"
  33595. >__call</VAR
  33596. >,
  33597.    <VAR
  33598. CLASS="literal"
  33599. >__get</VAR
  33600. >,
  33601.    <VAR
  33602. CLASS="literal"
  33603. >__set</VAR
  33604. >
  33605.    (see <A
  33606. HREF="#language.oop5.overloading"
  33607. >Overloading</A
  33608. >),
  33609.    <VAR
  33610. CLASS="literal"
  33611. >__sleep</VAR
  33612. >,
  33613.    <VAR
  33614. CLASS="literal"
  33615. >__wakeup</VAR
  33616. >, and
  33617.    <VAR
  33618. CLASS="literal"
  33619. >__toString</VAR
  33620.    are magical in PHP classes. You
  33621.    cannot have functions with these names in any of your
  33622.    classes unless you want the magic functionality associated
  33623.    with them.
  33624.   </P
  33625. ><DIV
  33626. CLASS="caution"
  33627. ><P
  33628. ></P
  33629. ><TABLE
  33630. CLASS="caution"
  33631. BORDER="1"
  33632. WIDTH="100%"
  33633. ><TR
  33634. ><TD
  33635. ALIGN="CENTER"
  33636. ><B
  33637. >Caution</B
  33638. ></TD
  33639. ></TR
  33640. ><TR
  33641. ><TD
  33642. ALIGN="LEFT"
  33643. ><P
  33644. >     PHP reserves all function names starting with __ as magical.
  33645.     It is recommended that you do not use function names with
  33646.     __ in PHP unless you want some documented magic functionality.
  33647.    </P
  33648. ></TD
  33649. ></TR
  33650. ></TABLE
  33651. ></DIV
  33652. ><DIV
  33653. CLASS="sect2"
  33654. ><HR><H3
  33655. CLASS="sect2"
  33656. ><A
  33657. NAME="language.oop5.magic.sleep"
  33658. ><VAR
  33659. CLASS="literal"
  33660. >__sleep</VAR
  33661. > and <VAR
  33662. CLASS="literal"
  33663. >__wakeup</VAR
  33664. ></A
  33665. ></H3
  33666. ><P
  33667. >     <A
  33668. HREF="#function.serialize"
  33669. ><B
  33670. CLASS="function"
  33671. >serialize()</B
  33672. ></A
  33673. > checks if your class has a function with
  33674.     the magic name <VAR
  33675. CLASS="literal"
  33676. >__sleep</VAR
  33677. >. If so, that function is
  33678.     being run prior to any serialization. It can clean up the object
  33679.     and is supposed to return an array with the names of all variables
  33680.     of that object that should be serialized.
  33681.    </P
  33682. ><P
  33683. >     The intended use of <VAR
  33684. CLASS="literal"
  33685. >__sleep</VAR
  33686. > is to close any
  33687.     database connections that object may have, committing pending
  33688.     data or perform similar cleanup tasks. Also, the function is
  33689.     useful if you have very large objects which need not be
  33690.     saved completely.
  33691.    </P
  33692. ><P
  33693. >     Conversely, <A
  33694. HREF="#function.unserialize"
  33695. ><B
  33696. CLASS="function"
  33697. >unserialize()</B
  33698. ></A
  33699. > checks for the
  33700.     presence of a function with the magic name 
  33701.     <VAR
  33702. CLASS="literal"
  33703. >__wakeup</VAR
  33704. >. If present, this function can
  33705.     reconstruct any resources that object may have.
  33706.    </P
  33707. ><P
  33708. >     The intended use of <VAR
  33709. CLASS="literal"
  33710. >__wakeup</VAR
  33711. > is to
  33712.     reestablish any database connections that may have been lost
  33713.     during serialization and perform other reinitialization
  33714.     tasks.
  33715.    </P
  33716. ></DIV
  33717. ><DIV
  33718. CLASS="sect2"
  33719. ><HR><H3
  33720. CLASS="sect2"
  33721. ><A
  33722. NAME="language.oop5.magic.tostring"
  33723. ><VAR
  33724. CLASS="literal"
  33725. >__toString</VAR
  33726. ></A
  33727. ></H3
  33728. ><P
  33729. >     The <VAR
  33730. CLASS="literal"
  33731. >__toString</VAR
  33732. > method allows a class to decide
  33733.     how it will react when it is converted to a string.
  33734.    </P
  33735. ><TABLE
  33736. WIDTH="100%"
  33737. BORDER="0"
  33738. CELLPADDING="0"
  33739. CELLSPACING="0"
  33740. CLASS="EXAMPLE"
  33741. ><TR
  33742. ><TD
  33743. ><DIV
  33744. CLASS="example"
  33745. ><A
  33746. NAME="AEN5689"
  33747. ></A
  33748. ><P
  33749. ><B
  33750. >Example 19-25. Simple example</B
  33751. ></P
  33752. ><TABLE
  33753. BORDER="0"
  33754. BGCOLOR="#E0E0E0"
  33755. CELLPADDING="5"
  33756. ><TR
  33757. ><TD
  33758. ><PRE
  33759. CLASS="php"
  33760. ><?php
  33761. // Declare a simple class
  33762. class TestClass
  33763. {
  33764.     public $foo;
  33765.  
  33766.     public function __construct($foo) {
  33767.         $this->foo = $foo;
  33768.     }
  33769.  
  33770.     public function __toString() {
  33771.         return $this->foo;
  33772.     }
  33773. }
  33774.  
  33775. $class = new TestClass('Hello');
  33776. echo $class;
  33777. ?></PRE
  33778. ></TD
  33779. ></TR
  33780. ></TABLE
  33781. ><P
  33782. >The above example will output:</P
  33783. ><TABLE
  33784. BORDER="0"
  33785. BGCOLOR="#E0E0E0"
  33786. CELLPADDING="5"
  33787. ><TR
  33788. ><TD
  33789. ><PRE
  33790. CLASS="screen"
  33791. >Hello</PRE
  33792. ></TD
  33793. ></TR
  33794. ></TABLE
  33795. ></DIV
  33796. ></TD
  33797. ></TR
  33798. ></TABLE
  33799. ><P
  33800. >     It is worth noting that the <VAR
  33801. CLASS="literal"
  33802. >__toString</VAR
  33803. > method
  33804.     will only be called when it is directly combined with
  33805.     <A
  33806. HREF="#function.echo"
  33807. ><B
  33808. CLASS="function"
  33809. >echo()</B
  33810. ></A
  33811. > or <A
  33812. HREF="#function.print"
  33813. ><B
  33814. CLASS="function"
  33815. >print()</B
  33816. ></A
  33817. >.
  33818.    </P
  33819. ><TABLE
  33820. WIDTH="100%"
  33821. BORDER="0"
  33822. CELLPADDING="0"
  33823. CELLSPACING="0"
  33824. CLASS="EXAMPLE"
  33825. ><TR
  33826. ><TD
  33827. ><DIV
  33828. CLASS="example"
  33829. ><A
  33830. NAME="AEN5698"
  33831. ></A
  33832. ><P
  33833. ><B
  33834. >Example 19-26. Cases where <VAR
  33835. CLASS="literal"
  33836. >__toString</VAR
  33837. > is called</B
  33838. ></P
  33839. ><TABLE
  33840. BORDER="0"
  33841. BGCOLOR="#E0E0E0"
  33842. CELLPADDING="5"
  33843. ><TR
  33844. ><TD
  33845. ><PRE
  33846. CLASS="php"
  33847. ><?php
  33848. // __toString called
  33849. echo $class;
  33850.  
  33851. // __toString called (still a normal parameter for echo)
  33852. echo 'text', $class;
  33853.  
  33854. // __toString not called (concatenation operator used first)
  33855. echo 'text' . $class;
  33856.  
  33857. // __toString not called (cast to string first)
  33858. echo (string) $class;
  33859.  
  33860. // __toString not called (cast to string first)
  33861. echo "text $class";
  33862. ?></PRE
  33863. ></TD
  33864. ></TR
  33865. ></TABLE
  33866. ></DIV
  33867. ></TD
  33868. ></TR
  33869. ></TABLE
  33870. ></DIV
  33871. ></DIV
  33872. ><DIV
  33873. CLASS="sect1"
  33874. ><HR><H2
  33875. CLASS="sect1"
  33876. ><A
  33877. NAME="language.oop5.final"
  33878. >Final Keyword</A
  33879. ></H2
  33880. ><P
  33881. >    PHP 5 introduces the final keyword, which prevents child classes from
  33882.    overriding a method by prefixing the definition with final. If the class
  33883.    itself is being defined final then it cannot be extended.
  33884.   </P
  33885. ><TABLE
  33886. WIDTH="100%"
  33887. BORDER="0"
  33888. CELLPADDING="0"
  33889. CELLSPACING="0"
  33890. CLASS="EXAMPLE"
  33891. ><TR
  33892. ><TD
  33893. ><DIV
  33894. CLASS="example"
  33895. ><A
  33896. NAME="AEN5705"
  33897. ></A
  33898. ><P
  33899. ><B
  33900. >Example 19-27. Final methods example</B
  33901. ></P
  33902. ><TABLE
  33903. BORDER="0"
  33904. BGCOLOR="#E0E0E0"
  33905. CELLPADDING="5"
  33906. ><TR
  33907. ><TD
  33908. ><PRE
  33909. CLASS="php"
  33910. ><?php
  33911. class BaseClass {
  33912.    public function test() {
  33913.        echo "BaseClass::test() called\n";
  33914.    }
  33915.    
  33916.    final public function moreTesting() {
  33917.        echo "BaseClass::moreTesting() called\n";
  33918.    }
  33919. }
  33920.  
  33921. class ChildClass extends BaseClass {
  33922.    public function moreTesting() {
  33923.        echo "ChildClass::moreTesting() called\n";
  33924.    }
  33925. }
  33926. // Results in Fatal error: Cannot override final method BaseClass::moreTesting()
  33927. ?></PRE
  33928. ></TD
  33929. ></TR
  33930. ></TABLE
  33931. ></DIV
  33932. ></TD
  33933. ></TR
  33934. ></TABLE
  33935. ><TABLE
  33936. WIDTH="100%"
  33937. BORDER="0"
  33938. CELLPADDING="0"
  33939. CELLSPACING="0"
  33940. CLASS="EXAMPLE"
  33941. ><TR
  33942. ><TD
  33943. ><DIV
  33944. CLASS="example"
  33945. ><A
  33946. NAME="AEN5708"
  33947. ></A
  33948. ><P
  33949. ><B
  33950. >Example 19-28. Final class example</B
  33951. ></P
  33952. ><TABLE
  33953. BORDER="0"
  33954. BGCOLOR="#E0E0E0"
  33955. CELLPADDING="5"
  33956. ><TR
  33957. ><TD
  33958. ><PRE
  33959. CLASS="php"
  33960. ><?php
  33961. final class BaseClass {
  33962.    public function test() {
  33963.        echo "BaseClass::test() called\n";
  33964.    }
  33965.  
  33966.    // Here it doesn't matter if you specify the function as final or not
  33967.    final public function moreTesting() {
  33968.        echo "BaseClass::moreTesting() called\n";
  33969.    }
  33970. }
  33971.  
  33972. class ChildClass extends BaseClass {
  33973. }
  33974. // Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
  33975. ?></PRE
  33976. ></TD
  33977. ></TR
  33978. ></TABLE
  33979. ></DIV
  33980. ></TD
  33981. ></TR
  33982. ></TABLE
  33983. ></DIV
  33984. ><DIV
  33985. CLASS="sect1"
  33986. ><HR><H2
  33987. CLASS="sect1"
  33988. ><A
  33989. NAME="language.oop5.cloning"
  33990. >Object cloning</A
  33991. ></H2
  33992. ><P
  33993. >    Creating a copy of an object with fully replicated properties is not
  33994.    always the wanted behavior. A good example of the need for copy
  33995.    constructors, is if you have an object which represents a GTK window and the
  33996.    object holds the resource of this GTK window, when you create a duplicate
  33997.    you might want to create a new window with the same properties and have the
  33998.    new object hold the resource of the new window. Another example is if your
  33999.    object holds a reference to another object which it uses and when you
  34000.    replicate the parent object you want to create a new instance of this other
  34001.    object so that the replica has its own separate copy.
  34002.   </P
  34003. ><P
  34004. >    An object copy is created by using the clone keyword (which calls the
  34005.    object's __clone() method if possible). An object's __clone()  method
  34006.    cannot be called directly.
  34007.   </P
  34008. ><DIV
  34009. CLASS="informalexample"
  34010. ><P
  34011. ></P
  34012. ><A
  34013. NAME="AEN5715"
  34014. ></A
  34015. ><TABLE
  34016. BORDER="0"
  34017. BGCOLOR="#E0E0E0"
  34018. CELLPADDING="5"
  34019. ><TR
  34020. ><TD
  34021. ><PRE
  34022. CLASS="programlisting"
  34023. >$copy_of_object = clone $object;</PRE
  34024. ></TD
  34025. ></TR
  34026. ></TABLE
  34027. ><P
  34028. ></P
  34029. ></DIV
  34030. ><P
  34031. >    When an object is cloned, PHP 5 will perform a shallow copy of all of the
  34032.    object's properties. Any properties that are references to other variables,
  34033.    will remain references. If a __clone() method is defined, then the newly
  34034.    created  object's __clone() method will be called, to allow any necessary
  34035.    properties that need to be changed.
  34036.   </P
  34037. ><TABLE
  34038. WIDTH="100%"
  34039. BORDER="0"
  34040. CELLPADDING="0"
  34041. CELLSPACING="0"
  34042. CLASS="EXAMPLE"
  34043. ><TR
  34044. ><TD
  34045. ><DIV
  34046. CLASS="example"
  34047. ><A
  34048. NAME="AEN5718"
  34049. ></A
  34050. ><P
  34051. ><B
  34052. >Example 19-29. Cloning an object</B
  34053. ></P
  34054. ><TABLE
  34055. BORDER="0"
  34056. BGCOLOR="#E0E0E0"
  34057. CELLPADDING="5"
  34058. ><TR
  34059. ><TD
  34060. ><PRE
  34061. CLASS="php"
  34062. ><?php
  34063. class SubObject
  34064. {
  34065.     static $instances = 0;
  34066.     public $instance;
  34067.  
  34068.     public function __construct() {
  34069.         $this->instance = ++self::$instances;
  34070.     }
  34071.  
  34072.     public function __clone() {
  34073.         $this->instance = ++self::$instances;
  34074.     }
  34075. }
  34076.  
  34077. class MyCloneable
  34078. {
  34079.     public $object1;
  34080.     public $object2;
  34081.  
  34082.     function __clone()
  34083.     {
  34084.         // Force a copy of this->object, otherwise
  34085.         // it will point to same object.
  34086.         $this->object1 = clone($this->object1);
  34087.     }
  34088. }
  34089.  
  34090. $obj = new MyCloneable();
  34091.  
  34092. $obj->object1 = new SubObject();
  34093. $obj->object2 = new SubObject();
  34094.  
  34095. $obj2 = clone $obj;
  34096.  
  34097.  
  34098. print("Original Object:\n");
  34099. print_r($obj);
  34100.  
  34101. print("Cloned Object:\n");
  34102. print_r($obj2);
  34103.  
  34104. ?></PRE
  34105. ></TD
  34106. ></TR
  34107. ></TABLE
  34108. ><P
  34109. >The above example will output:</P
  34110. ><TABLE
  34111. BORDER="0"
  34112. BGCOLOR="#E0E0E0"
  34113. CELLPADDING="5"
  34114. ><TR
  34115. ><TD
  34116. ><PRE
  34117. CLASS="php"
  34118. >Original Object:
  34119. MyCloneable Object
  34120. (
  34121.     [object1] => SubObject Object
  34122.         (
  34123.             [instance] => 1
  34124.         )
  34125.  
  34126.     [object2] => SubObject Object
  34127.         (
  34128.             [instance] => 2
  34129.         )
  34130.  
  34131. )
  34132. Cloned Object:
  34133. MyCloneable Object
  34134. (
  34135.     [object1] => SubObject Object
  34136.         (
  34137.             [instance] => 3
  34138.         )
  34139.  
  34140.     [object2] => SubObject Object
  34141.         (
  34142.             [instance] => 2
  34143.         )
  34144.  
  34145. )</PRE
  34146. ></TD
  34147. ></TR
  34148. ></TABLE
  34149. ></DIV
  34150. ></TD
  34151. ></TR
  34152. ></TABLE
  34153. ></DIV
  34154. ><DIV
  34155. CLASS="sect1"
  34156. ><HR><H2
  34157. CLASS="sect1"
  34158. ><A
  34159. NAME="language.oop5.object-comparison"
  34160. >Comparing objects</A
  34161. ></H2
  34162. ><P
  34163. >     In PHP 5, object comparison is more complicated than in PHP 4 and more
  34164.     in accordance to what one will expect from an Object Oriented Language
  34165.     (not that PHP 5 is such a language).
  34166.    </P
  34167. ><P
  34168. >     When using the comparison operator (<VAR
  34169. CLASS="literal"
  34170. >==</VAR
  34171. >), 
  34172.     object variables are compared in a simple manner, namely: Two object
  34173.     instances are equal if they have the same attributes and values, and are
  34174.     instances of the same class.
  34175.    </P
  34176. ><P
  34177. >     On the other hand, when using the identity operator (<VAR
  34178. CLASS="literal"
  34179. >===</VAR
  34180. >),
  34181.     object variables are identical if and only if they refer to the same
  34182.     instance of the same class.
  34183.    </P
  34184. ><P
  34185. >     An example will clarify these rules.
  34186.     <TABLE
  34187. WIDTH="100%"
  34188. BORDER="0"
  34189. CELLPADDING="0"
  34190. CELLSPACING="0"
  34191. CLASS="EXAMPLE"
  34192. ><TR
  34193. ><TD
  34194. ><DIV
  34195. CLASS="example"
  34196. ><A
  34197. NAME="AEN5731"
  34198. ></A
  34199. ><P
  34200. ><B
  34201. >Example 19-30. Example of object comparison in PHP 5</B
  34202. ></P
  34203. ><TABLE
  34204. BORDER="0"
  34205. BGCOLOR="#E0E0E0"
  34206. CELLPADDING="5"
  34207. ><TR
  34208. ><TD
  34209. ><PRE
  34210. CLASS="php"
  34211. ><?php
  34212. function bool2str($bool)
  34213. {
  34214.     if ($bool === false) {
  34215.         return 'FALSE';
  34216.     } else {
  34217.         return 'TRUE';
  34218.     }
  34219. }
  34220.  
  34221. function compareObjects(&$o1, &$o2)
  34222. {
  34223.     echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n";
  34224.     echo 'o1 != o2 : ' . bool2str($o1 != $o2) . "\n";
  34225.     echo 'o1 === o2 : ' . bool2str($o1 === $o2) . "\n";
  34226.     echo 'o1 !== o2 : ' . bool2str($o1 !== $o2) . "\n";
  34227. }
  34228.  
  34229. class Flag
  34230. {
  34231.     public $flag;
  34232.  
  34233.     function Flag($flag = true) {
  34234.         $this->flag = $flag;
  34235.     }
  34236. }
  34237.  
  34238. class OtherFlag
  34239. {
  34240.     public $flag;
  34241.  
  34242.     function OtherFlag($flag = true) {
  34243.         $this->flag = $flag;
  34244.     }
  34245. }
  34246.  
  34247. $o = new Flag();
  34248. $p = new Flag();
  34249. $q = $o;
  34250. $r = new OtherFlag();
  34251.  
  34252. echo "Two instances of the same class\n";
  34253. compareObjects($o, $p);
  34254.  
  34255. echo "\nTwo references to the same instance\n";
  34256. compareObjects($o, $q);
  34257.  
  34258. echo "\nInstances of two different classes\n";
  34259. compareObjects($o, $r);
  34260. ?></PRE
  34261. ></TD
  34262. ></TR
  34263. ></TABLE
  34264. ></DIV
  34265. ></TD
  34266. ></TR
  34267. ></TABLE
  34268. >
  34269.     This example will output:
  34270.     <TABLE
  34271. BORDER="0"
  34272. BGCOLOR="#E0E0E0"
  34273. CELLPADDING="5"
  34274. ><TR
  34275. ><TD
  34276. ><PRE
  34277. CLASS="screen"
  34278. >Two instances of the same class
  34279. o1 == o2 : TRUE
  34280. o1 != o2 : FALSE
  34281. o1 === o2 : FALSE
  34282. o1 !== o2 : TRUE
  34283.  
  34284. Two references to the same instance
  34285. o1 == o2 : TRUE
  34286. o1 != o2 : FALSE
  34287. o1 === o2 : TRUE
  34288. o1 !== o2 : FALSE
  34289.  
  34290. Instances of two different classes
  34291. o1 == o2 : FALSE
  34292. o1 != o2 : TRUE
  34293. o1 === o2 : FALSE
  34294. o1 !== o2 : TRUE</PRE
  34295. ></TD
  34296. ></TR
  34297. ></TABLE
  34298. >
  34299.    </P
  34300. ></DIV
  34301. ><DIV
  34302. CLASS="sect1"
  34303. ><HR><H2
  34304. CLASS="sect1"
  34305. ><A
  34306. NAME="language.oop5.reflection"
  34307. >Reflection</A
  34308. ></H2
  34309. ><DIV
  34310. CLASS="sect2"
  34311. ><H3
  34312. CLASS="sect2"
  34313. ><A
  34314. NAME="language.oop5.reflection.introduction"
  34315. >Introduction</A
  34316. ></H3
  34317. ><P
  34318. >     PHP 5 comes with a complete reflection API that adds the ability to
  34319.     reverse-engineer classes, interfaces, functions and methods as well
  34320.     as extensions. Additionally, the reflection API also offers ways of 
  34321.     retrieving doc comments for functions, classes and methods.
  34322.    </P
  34323. ><P
  34324. >     The reflection API is an object-oriented extension to the Zend Engine,
  34325.     consisting of the following classes:
  34326.    </P
  34327. ><DIV
  34328. CLASS="informalexample"
  34329. ><P
  34330. ></P
  34331. ><A
  34332. NAME="AEN5741"
  34333. ></A
  34334. ><TABLE
  34335. BORDER="0"
  34336. BGCOLOR="#E0E0E0"
  34337. CELLPADDING="5"
  34338. ><TR
  34339. ><TD
  34340. ><PRE
  34341. CLASS="php"
  34342. ><?php
  34343. class Reflection { }
  34344. interface Reflector { }
  34345. class ReflectionException extends Exception { }
  34346. class ReflectionFunction implements Reflector { }
  34347. class ReflectionParameter implements Reflector { }
  34348. class ReflectionMethod extends ReflectionFunction { }
  34349. class ReflectionClass implements Reflector { }
  34350. class ReflectionObject extends ReflectionClass { }
  34351. class ReflectionProperty implements Reflector { }
  34352. class ReflectionExtension implements Reflector { }
  34353. ?></PRE
  34354. ></TD
  34355. ></TR
  34356. ></TABLE
  34357. ><P
  34358. ></P
  34359. ></DIV
  34360. ><DIV
  34361. CLASS="note"
  34362. ><BLOCKQUOTE
  34363. CLASS="note"
  34364. ><P
  34365. ><B
  34366. >Note: </B
  34367. >
  34368.      For details on these classes, have a look at the next chapters.
  34369.     </P
  34370. ></BLOCKQUOTE
  34371. ></DIV
  34372. ><P
  34373. >     If we were to execute the code in the example below:
  34374.     <TABLE
  34375. WIDTH="100%"
  34376. BORDER="0"
  34377. CELLPADDING="0"
  34378. CELLSPACING="0"
  34379. CLASS="EXAMPLE"
  34380. ><TR
  34381. ><TD
  34382. ><DIV
  34383. CLASS="example"
  34384. ><A
  34385. NAME="AEN5746"
  34386. ></A
  34387. ><P
  34388. ><B
  34389. >Example 19-31. Basic usage of the reflection API</B
  34390. ></P
  34391. ><TABLE
  34392. BORDER="0"
  34393. BGCOLOR="#E0E0E0"
  34394. CELLPADDING="5"
  34395. ><TR
  34396. ><TD
  34397. ><PRE
  34398. CLASS="php"
  34399. ><?php
  34400. Reflection::export(new ReflectionClass('Exception'));
  34401. ?></PRE
  34402. ></TD
  34403. ></TR
  34404. ></TABLE
  34405. ><P
  34406. >The above example will output:</P
  34407. ><TABLE
  34408. BORDER="0"
  34409. BGCOLOR="#E0E0E0"
  34410. CELLPADDING="5"
  34411. ><TR
  34412. ><TD
  34413. ><PRE
  34414. CLASS="screen"
  34415. >Class [ <internal> class Exception ] {
  34416.  
  34417.   - Constants [0] {
  34418.   }
  34419.  
  34420.   - Static properties [0] {
  34421.   }
  34422.  
  34423.   - Static methods [0] {
  34424.   }
  34425.  
  34426.   - Properties [6] {
  34427.     Property [ <default> protected $message ]
  34428.     Property [ <default> private $string ]
  34429.     Property [ <default> protected $code ]
  34430.     Property [ <default> protected $file ]
  34431.     Property [ <default> protected $line ]
  34432.     Property [ <default> private $trace ]
  34433.   }
  34434.  
  34435.   - Methods [9] {
  34436.     Method [ <internal> final private method __clone ] {
  34437.     }
  34438.  
  34439.     Method [ <internal> <ctor> public method __construct ] {
  34440.  
  34441.       - Parameters [2] {
  34442.         Parameter #0 [ <required> $message ]
  34443.         Parameter #1 [ <required> $code ]
  34444.       }
  34445.     }
  34446.  
  34447.     Method [ <internal> final public method getMessage ] {
  34448.     }
  34449.  
  34450.     Method [ <internal> final public method getCode ] {
  34451.     }
  34452.  
  34453.     Method [ <internal> final public method getFile ] {
  34454.     }
  34455.  
  34456.     Method [ <internal> final public method getLine ] {
  34457.     }
  34458.  
  34459.     Method [ <internal> final public method getTrace ] {
  34460.     }
  34461.  
  34462.     Method [ <internal> final public method getTraceAsString ] {
  34463.     }
  34464.  
  34465.     Method [ <internal> public method __toString ] {
  34466.     }
  34467.   }
  34468. }</PRE
  34469. ></TD
  34470. ></TR
  34471. ></TABLE
  34472. ></DIV
  34473. ></TD
  34474. ></TR
  34475. ></TABLE
  34476. >
  34477.    </P
  34478. ></DIV
  34479. ><DIV
  34480. CLASS="sect2"
  34481. ><HR><H3
  34482. CLASS="sect2"
  34483. ><A
  34484. NAME="language.oop5.reflection.reflectionfunction"
  34485. ><B
  34486. CLASS="classname"
  34487. >ReflectionFunction</B
  34488. ></A
  34489. ></H3
  34490. ><P
  34491. >     The <B
  34492. CLASS="classname"
  34493. >ReflectionFunction</B
  34494. > class lets you
  34495.     reverse-engineer functions.
  34496.    </P
  34497. ><DIV
  34498. CLASS="informalexample"
  34499. ><P
  34500. ></P
  34501. ><A
  34502. NAME="AEN5756"
  34503. ></A
  34504. ><TABLE
  34505. BORDER="0"
  34506. BGCOLOR="#E0E0E0"
  34507. CELLPADDING="5"
  34508. ><TR
  34509. ><TD
  34510. ><PRE
  34511. CLASS="php"
  34512. ><?php
  34513. class ReflectionFunction implements Reflector
  34514. {
  34515.     final private __clone()
  34516.     public object __construct(string name)
  34517.     public string __toString()
  34518.     public static string export()
  34519.     public string getName()
  34520.     public bool isInternal()
  34521.     public bool isUserDefined()
  34522.     public string getFileName()
  34523.     public int getStartLine()
  34524.     public int getEndLine()
  34525.     public string getDocComment()
  34526.     public array getStaticVariables()
  34527.     public mixed invoke(mixed* args)
  34528.     public mixed invokeArgs(array args)
  34529.     public bool returnsReference()
  34530.     public ReflectionParameter[] getParameters()
  34531.     public int getNumberOfParameters()
  34532.     public int getNumberOfRequiredParameters()
  34533. }
  34534. ?></PRE
  34535. ></TD
  34536. ></TR
  34537. ></TABLE
  34538. ><P
  34539. ></P
  34540. ></DIV
  34541. ><DIV
  34542. CLASS="note"
  34543. ><BLOCKQUOTE
  34544. CLASS="note"
  34545. ><P
  34546. ><B
  34547. >Note: </B
  34548. >
  34549.      <B
  34550. CLASS="function"
  34551. >invokeArgs()</B
  34552. > was added in PHP 5.1.0.
  34553.     </P
  34554. ></BLOCKQUOTE
  34555. ></DIV
  34556. ><P
  34557. >     To introspect a function, you will first have to create an instance
  34558.     of the <B
  34559. CLASS="classname"
  34560. >ReflectionFunction</B
  34561. > class. You can then call
  34562.     any of the above methods on this instance.
  34563.    </P
  34564. ><TABLE
  34565. WIDTH="100%"
  34566. BORDER="0"
  34567. CELLPADDING="0"
  34568. CELLSPACING="0"
  34569. CLASS="EXAMPLE"
  34570. ><TR
  34571. ><TD
  34572. ><DIV
  34573. CLASS="example"
  34574. ><A
  34575. NAME="AEN5763"
  34576. ></A
  34577. ><P
  34578. ><B
  34579. >Example 19-32. Using the <B
  34580. CLASS="classname"
  34581. >ReflectionFunction</B
  34582. > class</B
  34583. ></P
  34584. ><TABLE
  34585. BORDER="0"
  34586. BGCOLOR="#E0E0E0"
  34587. CELLPADDING="5"
  34588. ><TR
  34589. ><TD
  34590. ><PRE
  34591. CLASS="php"
  34592. ><?php
  34593. /**
  34594.  * A simple counter
  34595.  *
  34596.  * @return    int
  34597.  */
  34598. function counter() 
  34599. {
  34600.     static $c = 0;
  34601.     return $c++;
  34602. }
  34603.  
  34604. // Create an instance of the Reflection_Function class
  34605. $func = new ReflectionFunction('counter');
  34606.  
  34607. // Print out basic information
  34608. printf(
  34609.     "===> The %s function '%s'\n".
  34610.     "     declared in %s\n".
  34611.     "     lines %d to %d\n",
  34612.     $func->isInternal() ? 'internal' : 'user-defined',
  34613.     $func->getName(),
  34614.     $func->getFileName(),
  34615.     $func->getStartLine(),
  34616.     $func->getEndline()
  34617. );
  34618.  
  34619. // Print documentation comment
  34620. printf("---> Documentation:\n %s\n", var_export($func->getDocComment(), 1));
  34621.  
  34622. // Print static variables if existant
  34623. if ($statics = $func->getStaticVariables())
  34624. {
  34625.     printf("---> Static variables: %s\n", var_export($statics, 1));
  34626. }
  34627.  
  34628. // Invoke the function
  34629. printf("---> Invokation results in: ");
  34630. var_dump($func->invoke());
  34631.  
  34632.  
  34633. // you may prefer to use the export() method
  34634. echo "\nReflectionFunction::export() results:\n";
  34635. echo ReflectionFunction::export('counter');
  34636. ?></PRE
  34637. ></TD
  34638. ></TR
  34639. ></TABLE
  34640. ></DIV
  34641. ></TD
  34642. ></TR
  34643. ></TABLE
  34644. ><DIV
  34645. CLASS="note"
  34646. ><BLOCKQUOTE
  34647. CLASS="note"
  34648. ><P
  34649. ><B
  34650. >Note: </B
  34651. >
  34652.      The method <B
  34653. CLASS="function"
  34654. >invoke()</B
  34655. > accepts a variable number of
  34656.      arguments which are passed to the function just as in
  34657.      <A
  34658. HREF="#function.call-user-func"
  34659. ><B
  34660. CLASS="function"
  34661. >call_user_func()</B
  34662. ></A
  34663. >.
  34664.     </P
  34665. ></BLOCKQUOTE
  34666. ></DIV
  34667. ></DIV
  34668. ><DIV
  34669. CLASS="sect2"
  34670. ><HR><H3
  34671. CLASS="sect2"
  34672. ><A
  34673. NAME="language.oop5.reflection.reflectionparameter"
  34674. ><B
  34675. CLASS="classname"
  34676. >ReflectionParameter</B
  34677. ></A
  34678. ></H3
  34679. ><P
  34680. >     The <B
  34681. CLASS="classname"
  34682. >ReflectionParameter</B
  34683. > class retrieves
  34684.     information about a function's or method's parameters.
  34685.    </P
  34686. ><DIV
  34687. CLASS="informalexample"
  34688. ><P
  34689. ></P
  34690. ><A
  34691. NAME="AEN5776"
  34692. ></A
  34693. ><TABLE
  34694. BORDER="0"
  34695. BGCOLOR="#E0E0E0"
  34696. CELLPADDING="5"
  34697. ><TR
  34698. ><TD
  34699. ><PRE
  34700. CLASS="php"
  34701. ><?php
  34702. class ReflectionParameter implements Reflector
  34703. {
  34704.     final private __clone()
  34705.     public object __construct(string name)
  34706.     public string __toString()
  34707.     public static string export()
  34708.     public string getName()
  34709.     public bool isPassedByReference()
  34710.     public ReflectionClass getClass()
  34711.     public bool allowsNull()
  34712.     public bool isOptional()
  34713.     public bool isDefaultValueAvailable()
  34714.     public mixed getDefaultValue()
  34715. }
  34716. ?></PRE
  34717. ></TD
  34718. ></TR
  34719. ></TABLE
  34720. ><P
  34721. ></P
  34722. ></DIV
  34723. ><DIV
  34724. CLASS="note"
  34725. ><BLOCKQUOTE
  34726. CLASS="note"
  34727. ><P
  34728. ><B
  34729. >Note: </B
  34730. >
  34731.      <B
  34732. CLASS="function"
  34733. >getDefaultValue()</B
  34734. >,
  34735.      <B
  34736. CLASS="function"
  34737. >isDefaultValueAvailable()</B
  34738. >,
  34739.      <B
  34740. CLASS="function"
  34741. >isOptional()</B
  34742. > were added in PHP 5.1.0.
  34743.     </P
  34744. ></BLOCKQUOTE
  34745. ></DIV
  34746. ><P
  34747. >     To introspect function parameters, you will first have to create an instance
  34748.     of the <B
  34749. CLASS="classname"
  34750. >ReflectionFunction</B
  34751. > or 
  34752.     <B
  34753. CLASS="classname"
  34754. >ReflectionMethod</B
  34755. > classes and then use their 
  34756.     <B
  34757. CLASS="function"
  34758. >getParameters()</B
  34759. > method to retrieve an array of parameters.
  34760.    </P
  34761. ><TABLE
  34762. WIDTH="100%"
  34763. BORDER="0"
  34764. CELLPADDING="0"
  34765. CELLSPACING="0"
  34766. CLASS="EXAMPLE"
  34767. ><TR
  34768. ><TD
  34769. ><DIV
  34770. CLASS="example"
  34771. ><A
  34772. NAME="AEN5787"
  34773. ></A
  34774. ><P
  34775. ><B
  34776. >Example 19-33. Using the <B
  34777. CLASS="classname"
  34778. >ReflectionParameter</B
  34779. > class</B
  34780. ></P
  34781. ><TABLE
  34782. BORDER="0"
  34783. BGCOLOR="#E0E0E0"
  34784. CELLPADDING="5"
  34785. ><TR
  34786. ><TD
  34787. ><PRE
  34788. CLASS="php"
  34789. ><?php
  34790. function foo($a, $b, $c) { }
  34791. function bar(Exception $a, &$b, $c) { }
  34792. function baz(ReflectionFunction $a, $b = 1, $c = null) { }
  34793. function abc() { }
  34794.  
  34795. // Create an instance of Reflection_Function with the
  34796. // parameter given from the command line.    
  34797. $reflect = new ReflectionFunction($argv[1]);
  34798.  
  34799. echo $reflect;
  34800.  
  34801. foreach ($reflect->getParameters() as $i => $param) {
  34802.     printf(
  34803.         "-- Parameter #%d: %s {\n".
  34804.         "   Class: %s\n".
  34805.         "   Allows NULL: %s\n".
  34806.         "   Passed to by reference: %s\n".
  34807.         "   Is optional?: %s\n".
  34808.         "}\n",
  34809.         $i, 
  34810.         $param->getName(),
  34811.         var_export($param->getClass(), 1),
  34812.         var_export($param->allowsNull(), 1),
  34813.         var_export($param->isPassedByReference(), 1),
  34814.         $param->isOptional() ? 'yes' : 'no'
  34815.     );
  34816. }
  34817. ?></PRE
  34818. ></TD
  34819. ></TR
  34820. ></TABLE
  34821. ></DIV
  34822. ></TD
  34823. ></TR
  34824. ></TABLE
  34825. ></DIV
  34826. ><DIV
  34827. CLASS="sect2"
  34828. ><HR><H3
  34829. CLASS="sect2"
  34830. ><A
  34831. NAME="language.oop5.reflection.reflectionclass"
  34832. ><B
  34833. CLASS="classname"
  34834. >ReflectionClass</B
  34835. ></A
  34836. ></H3
  34837. ><P
  34838. >     The <B
  34839. CLASS="classname"
  34840. >ReflectionClass</B
  34841. > class lets
  34842.     you reverse-engineer classes.
  34843.    </P
  34844. ><DIV
  34845. CLASS="informalexample"
  34846. ><P
  34847. ></P
  34848. ><A
  34849. NAME="AEN5796"
  34850. ></A
  34851. ><TABLE
  34852. BORDER="0"
  34853. BGCOLOR="#E0E0E0"
  34854. CELLPADDING="5"
  34855. ><TR
  34856. ><TD
  34857. ><PRE
  34858. CLASS="php"
  34859. ><?php
  34860. class ReflectionClass implements Reflector
  34861. {
  34862.     final private __clone()
  34863.     public object __construct(string name)
  34864.     public string __toString()
  34865.     public static string export()
  34866.     public string getName()
  34867.     public bool isInternal()
  34868.     public bool isUserDefined()
  34869.     public bool isInstantiable()
  34870.     public bool hasMethod(string name)
  34871.     public string getFileName()
  34872.     public int getStartLine()
  34873.     public int getEndLine()
  34874.     public string getDocComment()
  34875.     public ReflectionMethod getConstructor()
  34876.     public ReflectionMethod getMethod(string name)
  34877.     public ReflectionMethod[] getMethods()
  34878.     public ReflectionProperty getProperty(string name)
  34879.     public ReflectionProperty[] getProperties()
  34880.     public array getConstants()
  34881.     public mixed getConstant(string name)
  34882.     public ReflectionClass[] getInterfaces()
  34883.     public bool isInterface()
  34884.     public bool isAbstract()
  34885.     public bool isFinal()
  34886.     public int getModifiers()
  34887.     public bool isInstance(stdclass object)
  34888.     public stdclass newInstance(mixed* args)
  34889.     public ReflectionClass getParentClass()
  34890.     public bool isSubclassOf(ReflectionClass class)
  34891.     public array getStaticProperties()
  34892.     public array getDefaultProperties()
  34893.     public bool isIterateable()
  34894.     public bool implementsInterface(string name)
  34895.     public ReflectionExtension getExtension()
  34896.     public string getExtensionName()
  34897. }
  34898. ?></PRE
  34899. ></TD
  34900. ></TR
  34901. ></TABLE
  34902. ><P
  34903. ></P
  34904. ></DIV
  34905. ><DIV
  34906. CLASS="note"
  34907. ><BLOCKQUOTE
  34908. CLASS="note"
  34909. ><P
  34910. ><B
  34911. >Note: </B
  34912. >
  34913.      <B
  34914. CLASS="function"
  34915. >hasMethod()</B
  34916. > was added in PHP 5.1.0.
  34917.     </P
  34918. ></BLOCKQUOTE
  34919. ></DIV
  34920. ><P
  34921. >     To introspect a class, you will first have to create an instance
  34922.     of the <B
  34923. CLASS="classname"
  34924. >ReflectionClass</B
  34925. > class. You can then
  34926.     call any of the above methods on this instance.
  34927.    </P
  34928. ><TABLE
  34929. WIDTH="100%"
  34930. BORDER="0"
  34931. CELLPADDING="0"
  34932. CELLSPACING="0"
  34933. CLASS="EXAMPLE"
  34934. ><TR
  34935. ><TD
  34936. ><DIV
  34937. CLASS="example"
  34938. ><A
  34939. NAME="AEN5803"
  34940. ></A
  34941. ><P
  34942. ><B
  34943. >Example 19-34. Using the <B
  34944. CLASS="classname"
  34945. >ReflectionClass</B
  34946. > class</B
  34947. ></P
  34948. ><TABLE
  34949. BORDER="0"
  34950. BGCOLOR="#E0E0E0"
  34951. CELLPADDING="5"
  34952. ><TR
  34953. ><TD
  34954. ><PRE
  34955. CLASS="php"
  34956. ><?php
  34957. interface Serializable
  34958. {
  34959.     // ...
  34960. }
  34961.  
  34962. class Object
  34963. {
  34964.     // ...
  34965. }
  34966.  
  34967. /**
  34968.  * A counter class
  34969.  */
  34970. class Counter extends Object implements Serializable 
  34971. {
  34972.     const START = 0;
  34973.     private static $c = Counter::START;
  34974.  
  34975.     /**
  34976.      * Invoke counter
  34977.      *
  34978.      * @access  public
  34979.      * @return  int
  34980.      */
  34981.     public function count() {
  34982.         return self::$c++;
  34983.     }
  34984. }
  34985.  
  34986. // Create an instance of the ReflectionClass class
  34987. $class = new ReflectionClass('Counter');
  34988.  
  34989. // Print out basic information
  34990. printf(
  34991.     "===> The %s%s%s %s '%s' [extends %s]\n" .
  34992.     "     declared in %s\n" .
  34993.     "     lines %d to %d\n" .
  34994.     "     having the modifiers %d [%s]\n",
  34995.         $class->isInternal() ? 'internal' : 'user-defined',
  34996.         $class->isAbstract() ? ' abstract' : '',
  34997.         $class->isFinal() ? ' final' : '',
  34998.         $class->isInterface() ? 'interface' : 'class',
  34999.         $class->getName(),
  35000.         var_export($class->getParentClass(), 1),
  35001.         $class->getFileName(),
  35002.         $class->getStartLine(),
  35003.         $class->getEndline(),
  35004.         $class->getModifiers(),
  35005.         implode(' ', Reflection::getModifierNames($class->getModifiers()))
  35006. );
  35007.  
  35008. // Print documentation comment
  35009. printf("---> Documentation:\n %s\n", var_export($class->getDocComment(), 1));
  35010.  
  35011. // Print which interfaces are implemented by this class
  35012. printf("---> Implements:\n %s\n", var_export($class->getInterfaces(), 1));
  35013.  
  35014. // Print class constants
  35015. printf("---> Constants: %s\n", var_export($class->getConstants(), 1));
  35016.  
  35017. // Print class properties
  35018. printf("---> Properties: %s\n", var_export($class->getProperties(), 1));
  35019.  
  35020. // Print class methods
  35021. printf("---> Methods: %s\n", var_export($class->getMethods(), 1));
  35022.  
  35023. // If this class is instantiable, create an instance
  35024. if ($class->isInstantiable()) {
  35025.     $counter = $class->newInstance();
  35026.  
  35027.     echo '---> $counter is instance? '; 
  35028.     echo $class->isInstance($counter) ? 'yes' : 'no';
  35029.  
  35030.     echo "\n---> new Object() is instance? ";
  35031.     echo $class->isInstance(new Object()) ? 'yes' : 'no';
  35032. }
  35033. ?></PRE
  35034. ></TD
  35035. ></TR
  35036. ></TABLE
  35037. ></DIV
  35038. ></TD
  35039. ></TR
  35040. ></TABLE
  35041. ><DIV
  35042. CLASS="note"
  35043. ><BLOCKQUOTE
  35044. CLASS="note"
  35045. ><P
  35046. ><B
  35047. >Note: </B
  35048. >
  35049.      The method <B
  35050. CLASS="function"
  35051. >newInstance()</B
  35052. > accepts a variable number of
  35053.      arguments which are passed to the function just as in
  35054.      <A
  35055. HREF="#function.call-user-func"
  35056. ><B
  35057. CLASS="function"
  35058. >call_user_func()</B
  35059. ></A
  35060. >.
  35061.     </P
  35062. ></BLOCKQUOTE
  35063. ></DIV
  35064. ><DIV
  35065. CLASS="note"
  35066. ><BLOCKQUOTE
  35067. CLASS="note"
  35068. ><P
  35069. ><B
  35070. >Note: </B
  35071. >
  35072.      <VAR
  35073. CLASS="literal"
  35074. >$class = new ReflectionClass('Foo'); $class->isInstance($arg)</VAR
  35075. >
  35076.      is equivalent to <VAR
  35077. CLASS="literal"
  35078. >$arg instanceof Foo</VAR
  35079. > or 
  35080.      <VAR
  35081. CLASS="literal"
  35082. >is_a($arg, 'Foo')</VAR
  35083. >.
  35084.     </P
  35085. ></BLOCKQUOTE
  35086. ></DIV
  35087. ></DIV
  35088. ><DIV
  35089. CLASS="sect2"
  35090. ><HR><H3
  35091. CLASS="sect2"
  35092. ><A
  35093. NAME="language.oop5.reflection.reflectionmethod"
  35094. ><B
  35095. CLASS="classname"
  35096. >ReflectionMethod</B
  35097. ></A
  35098. ></H3
  35099. ><P
  35100. >     The <B
  35101. CLASS="classname"
  35102. >ReflectionMethod</B
  35103. > class lets you
  35104.     reverse-engineer class methods.
  35105.    </P
  35106. ><DIV
  35107. CLASS="informalexample"
  35108. ><P
  35109. ></P
  35110. ><A
  35111. NAME="AEN5821"
  35112. ></A
  35113. ><TABLE
  35114. BORDER="0"
  35115. BGCOLOR="#E0E0E0"
  35116. CELLPADDING="5"
  35117. ><TR
  35118. ><TD
  35119. ><PRE
  35120. CLASS="php"
  35121. ><?php
  35122. class ReflectionMethod extends ReflectionFunction
  35123. {
  35124.     public __construct(mixed class, string name)
  35125.     public string __toString()
  35126.     public static string export()
  35127.     public mixed invoke(stdclass object, mixed* args)
  35128.     public moxed invokeArgs(stdclass object, array args)
  35129.     public bool isFinal()
  35130.     public bool isAbstract()
  35131.     public bool isPublic()
  35132.     public bool isPrivate()
  35133.     public bool isProtected()
  35134.     public bool isStatic()
  35135.     public bool isConstructor()
  35136.     public bool isDestructor()
  35137.     public int getModifiers()
  35138.     public ReflectionClass getDeclaringClass()
  35139.  
  35140.     // Inherited from ReflectionFunction
  35141.     final private __clone()
  35142.     public string getName()
  35143.     public bool isInternal()
  35144.     public bool isUserDefined()
  35145.     public string getFileName()
  35146.     public int getStartLine()
  35147.     public int getEndLine()
  35148.     public string getDocComment()
  35149.     public array getStaticVariables()
  35150.     public bool returnsReference()
  35151.     public ReflectionParameter[] getParameters()
  35152.     public int getNumberOfParameters()
  35153.     public int getNumberOfRequiredParameters()
  35154. }
  35155. ?></PRE
  35156. ></TD
  35157. ></TR
  35158. ></TABLE
  35159. ><P
  35160. ></P
  35161. ></DIV
  35162. ><P
  35163. >     To introspect a method, you will first have to create an instance
  35164.     of the <B
  35165. CLASS="classname"
  35166. >ReflectionMethod</B
  35167. > class. You can then call
  35168.     any of the above methods on this instance.
  35169.    </P
  35170. ><TABLE
  35171. WIDTH="100%"
  35172. BORDER="0"
  35173. CELLPADDING="0"
  35174. CELLSPACING="0"
  35175. CLASS="EXAMPLE"
  35176. ><TR
  35177. ><TD
  35178. ><DIV
  35179. CLASS="example"
  35180. ><A
  35181. NAME="AEN5825"
  35182. ></A
  35183. ><P
  35184. ><B
  35185. >Example 19-35. Using the <B
  35186. CLASS="classname"
  35187. >ReflectionMethod</B
  35188. > class</B
  35189. ></P
  35190. ><TABLE
  35191. BORDER="0"
  35192. BGCOLOR="#E0E0E0"
  35193. CELLPADDING="5"
  35194. ><TR
  35195. ><TD
  35196. ><PRE
  35197. CLASS="php"
  35198. ><?php
  35199. class Counter
  35200. {
  35201.     private static $c = 0;
  35202.  
  35203.     /**
  35204.      * Increment counter
  35205.      *
  35206.      * @final
  35207.      * @static
  35208.      * @access  public
  35209.      * @return  int
  35210.      */
  35211.     final public static function increment()
  35212.     {
  35213.         return ++self::$c;
  35214.     }
  35215. }
  35216.  
  35217. // Create an instance of the Reflection_Method class
  35218. $method = new ReflectionMethod('Counter', 'increment');
  35219.  
  35220. // Print out basic information
  35221. printf(
  35222.     "===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n" .
  35223.     "     declared in %s\n" .
  35224.     "     lines %d to %d\n" .
  35225.     "     having the modifiers %d[%s]\n",
  35226.         $method->isInternal() ? 'internal' : 'user-defined',
  35227.         $method->isAbstract() ? ' abstract' : '',
  35228.         $method->isFinal() ? ' final' : '',
  35229.         $method->isPublic() ? ' public' : '',
  35230.         $method->isPrivate() ? ' private' : '',
  35231.         $method->isProtected() ? ' protected' : '',
  35232.         $method->isStatic() ? ' static' : '',
  35233.         $method->getName(),
  35234.         $method->isConstructor() ? 'the constructor' : 'a regular method',
  35235.         $method->getFileName(),
  35236.         $method->getStartLine(),
  35237.         $method->getEndline(),
  35238.         $method->getModifiers(),
  35239.         implode(' ', Reflection::getModifierNames($method->getModifiers()))
  35240. );
  35241.  
  35242. // Print documentation comment
  35243. printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1));
  35244.  
  35245. // Print static variables if existant
  35246. if ($statics= $method->getStaticVariables()) {
  35247.     printf("---> Static variables: %s\n", var_export($statics, 1));
  35248. }
  35249.  
  35250. // Invoke the method
  35251. printf("---> Invokation results in: ");
  35252. var_dump($method->invoke(NULL));
  35253. ?></PRE
  35254. ></TD
  35255. ></TR
  35256. ></TABLE
  35257. ></DIV
  35258. ></TD
  35259. ></TR
  35260. ></TABLE
  35261. ><DIV
  35262. CLASS="note"
  35263. ><BLOCKQUOTE
  35264. CLASS="note"
  35265. ><P
  35266. ><B
  35267. >Note: </B
  35268. >
  35269.      Trying to invoke private, protected or abstract methods will result
  35270.      in an exception being thrown from the <B
  35271. CLASS="function"
  35272. >invoke()</B
  35273. >
  35274.      method.
  35275.     </P
  35276. ></BLOCKQUOTE
  35277. ></DIV
  35278. ><DIV
  35279. CLASS="note"
  35280. ><BLOCKQUOTE
  35281. CLASS="note"
  35282. ><P
  35283. ><B
  35284. >Note: </B
  35285. >
  35286.      For static methods as seen above, you should pass NULL as the first
  35287.      argument to <B
  35288. CLASS="function"
  35289. >invoke()</B
  35290. >. For non-static methods, pass 
  35291.      an instance of the class.
  35292.     </P
  35293. ></BLOCKQUOTE
  35294. ></DIV
  35295. ></DIV
  35296. ><DIV
  35297. CLASS="sect2"
  35298. ><HR><H3
  35299. CLASS="sect2"
  35300. ><A
  35301. NAME="language.oop5.reflection.reflectionproperty"
  35302. ><B
  35303. CLASS="classname"
  35304. >ReflectionProperty</B
  35305. ></A
  35306. ></H3
  35307. ><P
  35308. >     The <B
  35309. CLASS="classname"
  35310. >ReflectionProperty</B
  35311. > class lets you
  35312.     reverse-engineer class properties.
  35313.    </P
  35314. ><DIV
  35315. CLASS="informalexample"
  35316. ><P
  35317. ></P
  35318. ><A
  35319. NAME="AEN5840"
  35320. ></A
  35321. ><TABLE
  35322. BORDER="0"
  35323. BGCOLOR="#E0E0E0"
  35324. CELLPADDING="5"
  35325. ><TR
  35326. ><TD
  35327. ><PRE
  35328. CLASS="php"
  35329. ><?php
  35330. class ReflectionProperty implements Reflector
  35331. {
  35332.     final private __clone()
  35333.     public __construct(mixed class, string name)
  35334.     public string __toString()
  35335.     public static string export()
  35336.     public string getName()
  35337.     public bool isPublic()
  35338.     public bool isPrivate()
  35339.     public bool isProtected()
  35340.     public bool isStatic()
  35341.     public bool isDefault()
  35342.     public int getModifiers()
  35343.     public mixed getValue(stdclass object)
  35344.     public void setValue(stdclass object, mixed value)
  35345.     public ReflectionClass getDeclaringClass()
  35346. }
  35347. ?></PRE
  35348. ></TD
  35349. ></TR
  35350. ></TABLE
  35351. ><P
  35352. ></P
  35353. ></DIV
  35354. ><P
  35355. >     To introspect a property, you will first have to create an instance
  35356.     of the <B
  35357. CLASS="classname"
  35358. >ReflectionProperty</B
  35359. > class. You can then
  35360.     call any of the above methods on this instance.
  35361.    </P
  35362. ><TABLE
  35363. WIDTH="100%"
  35364. BORDER="0"
  35365. CELLPADDING="0"
  35366. CELLSPACING="0"
  35367. CLASS="EXAMPLE"
  35368. ><TR
  35369. ><TD
  35370. ><DIV
  35371. CLASS="example"
  35372. ><A
  35373. NAME="AEN5844"
  35374. ></A
  35375. ><P
  35376. ><B
  35377. >Example 19-36. Using the <B
  35378. CLASS="classname"
  35379. >ReflectionProperty</B
  35380. > class</B
  35381. ></P
  35382. ><TABLE
  35383. BORDER="0"
  35384. BGCOLOR="#E0E0E0"
  35385. CELLPADDING="5"
  35386. ><TR
  35387. ><TD
  35388. ><PRE
  35389. CLASS="php"
  35390. ><?php
  35391. class String
  35392. {
  35393.     public $length  = 5;
  35394. }
  35395.  
  35396. // Create an instance of the ReflectionProperty class
  35397. $prop = new ReflectionProperty('String', 'length');
  35398.  
  35399. // Print out basic information
  35400. printf(
  35401.     "===> The%s%s%s%s property '%s' (which was %s)\n" .
  35402.     "     having the modifiers %s\n",
  35403.         $prop->isPublic() ? ' public' : '',
  35404.         $prop->isPrivate() ? ' private' : '',
  35405.         $prop->isProtected() ? ' protected' : '',
  35406.         $prop->isStatic() ? ' static' : '',
  35407.         $prop->getName(),
  35408.         $prop->isDefault() ? 'declared at compile-time' : 'created at run-time',
  35409.         var_export(Reflection::getModifierNames($prop->getModifiers()), 1)
  35410. );
  35411.  
  35412. // Create an instance of String
  35413. $obj= new String();
  35414.  
  35415. // Get current value
  35416. printf("---> Value is: ");
  35417. var_dump($prop->getValue($obj));
  35418.  
  35419. // Change value
  35420. $prop->setValue($obj, 10);
  35421. printf("---> Setting value to 10, new value is: ");
  35422. var_dump($prop->getValue($obj));
  35423.  
  35424. // Dump object
  35425. var_dump($obj);
  35426. ?></PRE
  35427. ></TD
  35428. ></TR
  35429. ></TABLE
  35430. ></DIV
  35431. ></TD
  35432. ></TR
  35433. ></TABLE
  35434. ><DIV
  35435. CLASS="note"
  35436. ><BLOCKQUOTE
  35437. CLASS="note"
  35438. ><P
  35439. ><B
  35440. >Note: </B
  35441. >
  35442.      Trying to get or set private or protected class property's values
  35443.      will result in an exception being thrown.
  35444.     </P
  35445. ></BLOCKQUOTE
  35446. ></DIV
  35447. ></DIV
  35448. ><DIV
  35449. CLASS="sect2"
  35450. ><HR><H3
  35451. CLASS="sect2"
  35452. ><A
  35453. NAME="language.oop5.reflection.reflectionextension"
  35454. ><B
  35455. CLASS="classname"
  35456. >ReflectionExtension</B
  35457. ></A
  35458. ></H3
  35459. ><P
  35460. >     The <B
  35461. CLASS="classname"
  35462. >ReflectionExtension</B
  35463. > class lets you
  35464.     reverse-engineer extensions. You can retrieve all loaded extensions
  35465.     at runtime using the <A
  35466. HREF="#function.get-loaded-extensions"
  35467. ><B
  35468. CLASS="function"
  35469. >get_loaded_extensions()</B
  35470. ></A
  35471. >.
  35472.    </P
  35473. ><DIV
  35474. CLASS="informalexample"
  35475. ><P
  35476. ></P
  35477. ><A
  35478. NAME="AEN5856"
  35479. ></A
  35480. ><TABLE
  35481. BORDER="0"
  35482. BGCOLOR="#E0E0E0"
  35483. CELLPADDING="5"
  35484. ><TR
  35485. ><TD
  35486. ><PRE
  35487. CLASS="php"
  35488. ><?php
  35489. class ReflectionExtension implements Reflector {
  35490.     final private __clone()
  35491.     public __construct(string name)
  35492.     public string __toString()
  35493.     public static string export()
  35494.     public string getName()
  35495.     public string getVersion()
  35496.     public ReflectionFunction[] getFunctions()
  35497.     public array getConstants()
  35498.     public array getINIEntries()
  35499.     public ReflectionClass[] getClasses()
  35500.     public array getClassNames()
  35501. }
  35502. ?></PRE
  35503. ></TD
  35504. ></TR
  35505. ></TABLE
  35506. ><P
  35507. ></P
  35508. ></DIV
  35509. ><P
  35510. >     To introspect an extension, you will first have to create an instance
  35511.     of the <B
  35512. CLASS="classname"
  35513. >ReflectionExtension</B
  35514. > class. You can then call
  35515.     any of the above methods on this instance.
  35516.    </P
  35517. ><TABLE
  35518. WIDTH="100%"
  35519. BORDER="0"
  35520. CELLPADDING="0"
  35521. CELLSPACING="0"
  35522. CLASS="EXAMPLE"
  35523. ><TR
  35524. ><TD
  35525. ><DIV
  35526. CLASS="example"
  35527. ><A
  35528. NAME="AEN5860"
  35529. ></A
  35530. ><P
  35531. ><B
  35532. >Example 19-37. Using the <B
  35533. CLASS="classname"
  35534. >ReflectionExtension</B
  35535. > class</B
  35536. ></P
  35537. ><TABLE
  35538. BORDER="0"
  35539. BGCOLOR="#E0E0E0"
  35540. CELLPADDING="5"
  35541. ><TR
  35542. ><TD
  35543. ><PRE
  35544. CLASS="php"
  35545. ><?php
  35546. // Create an instance of the ReflectionProperty class
  35547. $ext = new ReflectionExtension('standard');
  35548.  
  35549. // Print out basic information
  35550. printf(
  35551.     "Name        : %s\n" .
  35552.     "Version     : %s\n" .
  35553.     "Functions   : [%d] %s\n" .
  35554.     "Constants   : [%d] %s\n" .
  35555.     "INI entries : [%d] %s\n" .
  35556.     "Classes     : [%d] %s\n",
  35557.         $ext->getName(),
  35558.         $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION',
  35559.         sizeof($ext->getFunctions()),
  35560.         var_export($ext->getFunctions(), 1),
  35561.  
  35562.         sizeof($ext->getConstants()),
  35563.         var_export($ext->getConstants(), 1),
  35564.  
  35565.         sizeof($ext->getINIEntries()),
  35566.         var_export($ext->getINIEntries(), 1),
  35567.  
  35568.         sizeof($ext->getClassNames()),
  35569.         var_export($ext->getClassNames(), 1)
  35570. );
  35571. ?></PRE
  35572. ></TD
  35573. ></TR
  35574. ></TABLE
  35575. ></DIV
  35576. ></TD
  35577. ></TR
  35578. ></TABLE
  35579. ></DIV
  35580. ><DIV
  35581. CLASS="sect2"
  35582. ><HR><H3
  35583. CLASS="sect2"
  35584. ><A
  35585. NAME="language.oop5.reflection.extending"
  35586. >Extending the reflection classes</A
  35587. ></H3
  35588. ><P
  35589. >     In case you want to create specialized versions of the built-in
  35590.     classes (say, for creating colorized HTML when being exported,
  35591.     having easy-access member variables instead of methods or
  35592.     having utility methods), you may go ahead and extend them.
  35593.    </P
  35594. ><TABLE
  35595. WIDTH="100%"
  35596. BORDER="0"
  35597. CELLPADDING="0"
  35598. CELLSPACING="0"
  35599. CLASS="EXAMPLE"
  35600. ><TR
  35601. ><TD
  35602. ><DIV
  35603. CLASS="example"
  35604. ><A
  35605. NAME="AEN5867"
  35606. ></A
  35607. ><P
  35608. ><B
  35609. >Example 19-38. Extending the built-in classes</B
  35610. ></P
  35611. ><TABLE
  35612. BORDER="0"
  35613. BGCOLOR="#E0E0E0"
  35614. CELLPADDING="5"
  35615. ><TR
  35616. ><TD
  35617. ><PRE
  35618. CLASS="php"
  35619. ><?php
  35620. /**
  35621.  * My Reflection_Method class
  35622.  */
  35623. class My_Reflection_Method extends ReflectionMethod
  35624. {
  35625.     public $visibility = '';
  35626.  
  35627.     public function __construct($o, $m)
  35628.     {
  35629.         parent::__construct($o, $m);
  35630.         $this->visibility= Reflection::getModifierNames($this->getModifiers());
  35631.     }
  35632. }
  35633.  
  35634. /**
  35635.  * Demo class #1
  35636.  *
  35637.  */
  35638. class T {
  35639.     protected function x() {}
  35640. }
  35641.  
  35642. /**
  35643.  * Demo class #2
  35644.  *
  35645.  */
  35646. class U extends T {
  35647.     function x() {}
  35648. }
  35649.  
  35650. // Print out information
  35651. var_dump(new My_Reflection_Method('U', 'x'));
  35652. ?></PRE
  35653. ></TD
  35654. ></TR
  35655. ></TABLE
  35656. ></DIV
  35657. ></TD
  35658. ></TR
  35659. ></TABLE
  35660. ><DIV
  35661. CLASS="note"
  35662. ><BLOCKQUOTE
  35663. CLASS="note"
  35664. ><P
  35665. ><B
  35666. >Note: </B
  35667. >
  35668.      Caution: If you're overwriting the constructor, remember to call
  35669.      the parent's constructor _before_ any code you insert. Failing to
  35670.      do so will result in the following:
  35671.      <VAR
  35672. CLASS="literal"
  35673. >       Fatal error: Internal error: Failed to retrieve the reflection object
  35674.      </VAR
  35675. >
  35676.     </P
  35677. ></BLOCKQUOTE
  35678. ></DIV
  35679. ></DIV
  35680. ></DIV
  35681. ><DIV
  35682. CLASS="sect1"
  35683. ><HR><H2
  35684. CLASS="sect1"
  35685. ><A
  35686. NAME="language.oop5.typehinting"
  35687. >Type Hinting</A
  35688. ></H2
  35689. ><P
  35690. >    PHP 5 introduces Type Hinting. Functions are now able to force parameters
  35691.    to be objects by specifying the name of the class in the function prototype.
  35692.   </P
  35693. ><TABLE
  35694. WIDTH="100%"
  35695. BORDER="0"
  35696. CELLPADDING="0"
  35697. CELLSPACING="0"
  35698. CLASS="EXAMPLE"
  35699. ><TR
  35700. ><TD
  35701. ><DIV
  35702. CLASS="example"
  35703. ><A
  35704. NAME="AEN5876"
  35705. ></A
  35706. ><P
  35707. ><B
  35708. >Example 19-39. Type Hinting example</B
  35709. ></P
  35710. ><TABLE
  35711. BORDER="0"
  35712. BGCOLOR="#E0E0E0"
  35713. CELLPADDING="5"
  35714. ><TR
  35715. ><TD
  35716. ><PRE
  35717. CLASS="php"
  35718. ><?php
  35719. // An example class
  35720. class MyClass
  35721. {
  35722.     /**
  35723.      * A test function
  35724.      *
  35725.      * First parameter must be an object of type OtherClass
  35726.      */
  35727.     public function test(OtherClass $otherclass) {
  35728.         echo $otherclass->var;
  35729.     }
  35730. }
  35731.  
  35732. // Another example class
  35733. class OtherClass {
  35734.     public $var = 'Hello World';
  35735. }
  35736. ?></PRE
  35737. ></TD
  35738. ></TR
  35739. ></TABLE
  35740. ><P
  35741. >     Failing to satisfy the type hint results in a fatal error.
  35742.    </P
  35743. ><TABLE
  35744. BORDER="0"
  35745. BGCOLOR="#E0E0E0"
  35746. CELLPADDING="5"
  35747. ><TR
  35748. ><TD
  35749. ><PRE
  35750. CLASS="php"
  35751. ><?php
  35752. // An instance of each class
  35753. $myclass = new MyClass;
  35754. $otherclass = new OtherClass;
  35755.  
  35756. // Fatal Error: Argument 1 must be an object of class OtherClass
  35757. $myclass->test('hello');
  35758.  
  35759. // Fatal Error: Argument 1 must be an instance of OtherClass
  35760. $foo = new stdClass;
  35761. $myclass->test($foo);
  35762.  
  35763. // Fatal Error: Argument 1 must not be null
  35764. $myclass->test(null);
  35765.  
  35766. // Works: Prints Hello World
  35767. $myclass->test($otherclass);
  35768. ?></PRE
  35769. ></TD
  35770. ></TR
  35771. ></TABLE
  35772. ><P
  35773. >     Type hinting also works with functions:
  35774.    </P
  35775. ><TABLE
  35776. BORDER="0"
  35777. BGCOLOR="#E0E0E0"
  35778. CELLPADDING="5"
  35779. ><TR
  35780. ><TD
  35781. ><PRE
  35782. CLASS="php"
  35783. ><?php
  35784. // An example class
  35785. class MyClass {
  35786.     public $var = 'Hello World';
  35787. }
  35788.  
  35789. /**
  35790.  * A test function
  35791.  *
  35792.  * First parameter must be an object of type MyClass
  35793.  */
  35794. function MyFunction (MyClass $foo) {
  35795.     echo $foo->var;
  35796. }
  35797.  
  35798. // Works
  35799. $myclass = new MyClass;
  35800. MyFunction($myclass);
  35801. ?></PRE
  35802. ></TD
  35803. ></TR
  35804. ></TABLE
  35805. ></DIV
  35806. ></TD
  35807. ></TR
  35808. ></TABLE
  35809. ><P
  35810. >    Type Hints can only be of the <A
  35811. HREF="#language.types.object"
  35812. ><B
  35813. CLASS="type"
  35814. >object</B
  35815. ></A
  35816. > type. Traditional
  35817.    type hinting with <A
  35818. HREF="#language.types.integer"
  35819. ><B
  35820. CLASS="type"
  35821. >int</B
  35822. ></A
  35823. > and <A
  35824. HREF="#language.types.string"
  35825. ><B
  35826. CLASS="type"
  35827. >string</B
  35828. ></A
  35829. > are not
  35830.    supported.
  35831.   </P
  35832. ></DIV
  35833. ></DIV
  35834. ><DIV
  35835. CLASS="chapter"
  35836. ><HR><H1
  35837. ><A
  35838. NAME="language.exceptions"
  35839. >Chapter 20. Exceptions</A
  35840. ></H1
  35841. ><P
  35842. >    PHP 5 has an exception model similar to that of other programming languages.
  35843.    An exception can be thrown, try and caught within PHP. A Try block must
  35844.    include at least one catch block. Multiple catch blocks can be used to
  35845.    catch different classtypes; execution will continue after that last catch
  35846.    block defined in sequence. Exceptions can be thrown within catch blocks.
  35847.   </P
  35848. ><P
  35849. >    When an exception is thrown, code following the statement will not be
  35850.    executed and PHP will attempt to find the first matching catch block. If an
  35851.    exception is not caught a PHP Fatal Error will be issued with an Uncaught
  35852.    Exception message, unless there has been a handler defined with
  35853.    <A
  35854. HREF="#function.set-exception-handler"
  35855. ><B
  35856. CLASS="function"
  35857. >set_exception_handler()</B
  35858. ></A
  35859. >.
  35860.   </P
  35861. ><TABLE
  35862. WIDTH="100%"
  35863. BORDER="0"
  35864. CELLPADDING="0"
  35865. CELLSPACING="0"
  35866. CLASS="EXAMPLE"
  35867. ><TR
  35868. ><TD
  35869. ><DIV
  35870. CLASS="example"
  35871. ><A
  35872. NAME="AEN5892"
  35873. ></A
  35874. ><P
  35875. ><B
  35876. >Example 20-1. Throwing an Exception</B
  35877. ></P
  35878. ><TABLE
  35879. BORDER="0"
  35880. BGCOLOR="#E0E0E0"
  35881. CELLPADDING="5"
  35882. ><TR
  35883. ><TD
  35884. ><PRE
  35885. CLASS="php"
  35886. ><?php
  35887. try {
  35888.     $error = 'Always throw this error';
  35889.     throw new Exception($error);
  35890.  
  35891.     // Code following an exception is not executed.
  35892.     echo 'Never executed';
  35893.  
  35894. } catch (Exception $e) {
  35895.     echo 'Caught exception: ',  $e->getMessage(), "\n";
  35896. }
  35897.  
  35898. // Continue execution
  35899. echo 'Hello World';
  35900. ?></PRE
  35901. ></TD
  35902. ></TR
  35903. ></TABLE
  35904. ></DIV
  35905. ></TD
  35906. ></TR
  35907. ></TABLE
  35908. ><DIV
  35909. CLASS="sect1"
  35910. ><HR><H2
  35911. CLASS="sect1"
  35912. ><A
  35913. NAME="language.exceptions.extending"
  35914. >Extending Exceptions</A
  35915. ></H2
  35916. ><P
  35917. >     A User defined Exception class can be defined by extending the built-in
  35918.     Exception class. The members and properties below, show what is accessible
  35919.     within the child class that derives from the built-in Exception class.
  35920.    </P
  35921. ><TABLE
  35922. WIDTH="100%"
  35923. BORDER="0"
  35924. CELLPADDING="0"
  35925. CELLSPACING="0"
  35926. CLASS="EXAMPLE"
  35927. ><TR
  35928. ><TD
  35929. ><DIV
  35930. CLASS="example"
  35931. ><A
  35932. NAME="AEN5898"
  35933. ></A
  35934. ><P
  35935. ><B
  35936. >Example 20-2. The Built in Exception class</B
  35937. ></P
  35938. ><TABLE
  35939. BORDER="0"
  35940. BGCOLOR="#E0E0E0"
  35941. CELLPADDING="5"
  35942. ><TR
  35943. ><TD
  35944. ><PRE
  35945. CLASS="php"
  35946. ><?php
  35947. class Exception
  35948. {
  35949.     protected $message = 'Unknown exception';   // exception message
  35950.     protected $code = 0;                        // user defined exception code
  35951.     protected $file;                            // source filename of exception
  35952.     protected $line;                            // source line of exception
  35953.  
  35954.     function __construct($message = null, $code = 0);
  35955.  
  35956.     final function getMessage();                // message of exception 
  35957.     final function getCode();                   // code of exception
  35958.     final function getFile();                   // source filename
  35959.     final function getLine();                   // source line
  35960.     final function getTrace();                  // an array of the backtrace()
  35961.     final function getTraceAsString();          // formated string of trace
  35962.  
  35963.     /* Overrideable */
  35964.     function __toString();                       // formated string for display
  35965. }
  35966. ?></PRE
  35967. ></TD
  35968. ></TR
  35969. ></TABLE
  35970. ></DIV
  35971. ></TD
  35972. ></TR
  35973. ></TABLE
  35974. ><P
  35975. >     If a class extends the built-in Exception class and re-defines the <A
  35976. HREF="#language.oop5.decon"
  35977. >constructor</A
  35978. >, it is highly recomended
  35979.     that it also call <A
  35980. HREF="#language.oop5.paamayim-nekudotayim"
  35981. >parent::__construct()</A
  35982.     to ensure all available data has been properly assigned. The <A
  35983. HREF="#language.oop5.magic"
  35984. >__toString()</A
  35985. > method can be overriden
  35986.     to provide a custom output when the object is presented as a string.
  35987.    </P
  35988. ><TABLE
  35989. WIDTH="100%"
  35990. BORDER="0"
  35991. CELLPADDING="0"
  35992. CELLSPACING="0"
  35993. CLASS="EXAMPLE"
  35994. ><TR
  35995. ><TD
  35996. ><DIV
  35997. CLASS="example"
  35998. ><A
  35999. NAME="AEN5905"
  36000. ></A
  36001. ><P
  36002. ><B
  36003. >Example 20-3. Extending the Exception class</B
  36004. ></P
  36005. ><TABLE
  36006. BORDER="0"
  36007. BGCOLOR="#E0E0E0"
  36008. CELLPADDING="5"
  36009. ><TR
  36010. ><TD
  36011. ><PRE
  36012. CLASS="php"
  36013. ><?php
  36014. /**
  36015.  * Define a custom exception class
  36016.  */
  36017. class MyException extends Exception
  36018. {
  36019.     // Redefine the exception so message isn't optional
  36020.     public function __construct($message, $code = 0) {
  36021.         // some code
  36022.     
  36023.         // make sure everything is assigned properly
  36024.         parent::__construct($message, $code);
  36025.     }
  36026.  
  36027.     // custom string representation of object */
  36028.     public function __toString() {
  36029.         return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
  36030.     }
  36031.  
  36032.     public function customFunction() {
  36033.         echo "A Custom function for this type of exception\n";
  36034.     }
  36035. }
  36036.  
  36037.  
  36038. /**
  36039.  * Create a class to test the exception
  36040.  */
  36041. class TestException
  36042. {
  36043.     public $var;
  36044.  
  36045.     const THROW_NONE    = 0;
  36046.     const THROW_CUSTOM  = 1;
  36047.     const THROW_DEFAULT = 2;
  36048.  
  36049.     function __construct($avalue = self::THROW_NONE) {
  36050.  
  36051.         switch ($avalue) {
  36052.             case self::THROW_CUSTOM:
  36053.                 // throw custom exception
  36054.                 throw new MyException('1 is an invalid parameter', 5);
  36055.                 break;
  36056.  
  36057.             case self::THROW_DEFAULT:
  36058.                 // throw default one.
  36059.                 throw new Exception('2 isnt allowed as a parameter', 6);
  36060.                 break;
  36061.  
  36062.             default: 
  36063.                 // No exception, object will be created.
  36064.                 $this->var = $avalue;
  36065.                 break;
  36066.         }
  36067.     }
  36068. }
  36069.  
  36070.  
  36071. // Example 1
  36072. try {
  36073.     $o = new TestException(TestException::THROW_CUSTOM);
  36074. } catch (MyException $e) {      // Will be caught
  36075.     echo "Caught my exception\n", $e;
  36076.     $e->customFunction();
  36077. } catch (Exception $e) {        // Skipped
  36078.     echo "Caught Default Exception\n", $e;
  36079. }
  36080.  
  36081. // Continue execution
  36082. var_dump($o);
  36083. echo "\n\n";
  36084.  
  36085.  
  36086. // Example 2
  36087. try {
  36088.     $o = new TestException(TestException::THROW_DEFAULT);
  36089. } catch (MyException $e) {      // Doesn't match this type
  36090.     echo "Caught my exception\n", $e;
  36091.     $e->customFunction();
  36092. } catch (Exception $e) {        // Will be caught
  36093.     echo "Caught Default Exception\n", $e;
  36094. }
  36095.  
  36096. // Continue execution
  36097. var_dump($o);
  36098. echo "\n\n";
  36099.  
  36100.  
  36101. // Example 3
  36102. try {
  36103.     $o = new TestException(TestException::THROW_CUSTOM);
  36104. } catch (Exception $e) {        // Will be caught
  36105.     echo "Default Exception caught\n", $e;
  36106. }
  36107.  
  36108. // Continue execution
  36109. var_dump($o);
  36110. echo "\n\n";
  36111.  
  36112.  
  36113. // Example 4
  36114. try {
  36115.     $o = new TestException();
  36116. } catch (Exception $e) {        // Skipped, no exception
  36117.     echo "Default Exception caught\n", $e;
  36118. }
  36119.  
  36120. // Continue execution
  36121. var_dump($o);
  36122. echo "\n\n";
  36123. ?></PRE
  36124. ></TD
  36125. ></TR
  36126. ></TABLE
  36127. ></DIV
  36128. ></TD
  36129. ></TR
  36130. ></TABLE
  36131. ></DIV
  36132. ></DIV
  36133. ><DIV
  36134. CLASS="chapter"
  36135. ><HR><H1
  36136. ><A
  36137. NAME="language.references"
  36138. >Chapter 21. References Explained</A
  36139. ></H1
  36140. ><DIV
  36141. CLASS="sect1"
  36142. ><H2
  36143. CLASS="sect1"
  36144. ><A
  36145. NAME="language.references.whatare"
  36146. >What References Are</A
  36147. ></H2
  36148. ><P
  36149. >  
  36150.     References in PHP are a means to access the same variable content 
  36151.     by different names. They are not like C pointers, they are symbol
  36152.     table aliases. Note that in PHP, variable name and variable content 
  36153.     are different, so the same content can have different names.
  36154.     The most close analogy is with Unix filenames and files -
  36155.     variable names are directory entries, while variable contents is
  36156.     the file itself. References can be thought of as hardlinking in
  36157.     Unix filesystem.
  36158.    </P
  36159. ></DIV
  36160. ><DIV
  36161. CLASS="sect1"
  36162. ><HR><H2
  36163. CLASS="sect1"
  36164. ><A
  36165. NAME="language.references.whatdo"
  36166. >What References Do</A
  36167. ></H2
  36168. ><P
  36169. >     PHP references allow you to make two variables to refer to the
  36170.     same content. Meaning, when you do:
  36171.     <DIV
  36172. CLASS="informalexample"
  36173. ><P
  36174. ></P
  36175. ><A
  36176. NAME="AEN5916"
  36177. ></A
  36178. ><TABLE
  36179. BORDER="0"
  36180. BGCOLOR="#E0E0E0"
  36181. CELLPADDING="5"
  36182. ><TR
  36183. ><TD
  36184. ><PRE
  36185. CLASS="php"
  36186. ><?php
  36187. $a =& $b;
  36188. ?></PRE
  36189. ></TD
  36190. ></TR
  36191. ></TABLE
  36192. ><P
  36193. ></P
  36194. ></DIV
  36195. >
  36196.     it means that <VAR
  36197. CLASS="varname"
  36198. >$a</VAR
  36199. > and <VAR
  36200. CLASS="varname"
  36201. >$b</VAR
  36202. >
  36203.     point to the same variable.
  36204.     <DIV
  36205. CLASS="note"
  36206. ><BLOCKQUOTE
  36207. CLASS="note"
  36208. ><P
  36209. ><B
  36210. >Note: </B
  36211. >
  36212.       <VAR
  36213. CLASS="varname"
  36214. >$a</VAR
  36215. > and <VAR
  36216. CLASS="varname"
  36217. >$b</VAR
  36218. > are completely
  36219.       equal here, that's not <VAR
  36220. CLASS="varname"
  36221. >$a</VAR
  36222. > is pointing to
  36223.       <VAR
  36224. CLASS="varname"
  36225. >$b</VAR
  36226. > or vice versa, that's
  36227.       <VAR
  36228. CLASS="varname"
  36229. >$a</VAR
  36230. > and <VAR
  36231. CLASS="varname"
  36232. >$b</VAR
  36233. > pointing to the
  36234.       same place.
  36235.      </P
  36236. ></BLOCKQUOTE
  36237. ></DIV
  36238. >
  36239.    </P
  36240. ><DIV
  36241. CLASS="note"
  36242. ><BLOCKQUOTE
  36243. CLASS="note"
  36244. ><P
  36245. ><B
  36246. >Note: </B
  36247. >
  36248.      If array with references is copied, its values are not dereferenced.
  36249.      This is valid also for arrays passed by value to functions.
  36250.     </P
  36251. ></BLOCKQUOTE
  36252. ></DIV
  36253. ><P
  36254. >    The same syntax can be used with functions, that return references,
  36255.    and with <VAR
  36256. CLASS="literal"
  36257. >new</VAR
  36258. > operator (in PHP 4.0.4 and later):
  36259.     <DIV
  36260. CLASS="informalexample"
  36261. ><P
  36262. ></P
  36263. ><A
  36264. NAME="AEN5932"
  36265. ></A
  36266. ><TABLE
  36267. BORDER="0"
  36268. BGCOLOR="#E0E0E0"
  36269. CELLPADDING="5"
  36270. ><TR
  36271. ><TD
  36272. ><PRE
  36273. CLASS="php"
  36274. ><?php
  36275. $bar =& new fooclass();
  36276. $foo =& find_var($bar);
  36277. ?></PRE
  36278. ></TD
  36279. ></TR
  36280. ></TABLE
  36281. ><P
  36282. ></P
  36283. ></DIV
  36284. >
  36285.     </P
  36286. ><DIV
  36287. CLASS="note"
  36288. ><BLOCKQUOTE
  36289. CLASS="note"
  36290. ><P
  36291. ><B
  36292. >Note: </B
  36293. >
  36294.      Not using the <VAR
  36295. CLASS="literal"
  36296. >&</VAR
  36297. > operator causes a copy of the
  36298.      object to be made. If you use <VAR
  36299. CLASS="literal"
  36300. >$this</VAR
  36301. > in the class it
  36302.      will operate on the current instance of the class. The assignment without
  36303.      <VAR
  36304. CLASS="literal"
  36305. >&</VAR
  36306. > will copy the instance (i.e. the object) and
  36307.      <VAR
  36308. CLASS="literal"
  36309. >$this</VAR
  36310. > will operate on the copy, which is not always
  36311.      what is desired. Usually you want to have a single instance to work with,
  36312.      due to performance and memory consumption issues.
  36313.      </P
  36314. ><P
  36315. >       While you can use the <VAR
  36316. CLASS="literal"
  36317. >@</VAR
  36318. > operator to
  36319.       <SPAN
  36320. CLASS="emphasis"
  36321. ><I
  36322. CLASS="emphasis"
  36323. >mute</I
  36324. ></SPAN
  36325. > any errors in the constructor when using it as
  36326.       <VAR
  36327. CLASS="literal"
  36328. >@new</VAR
  36329. >, this does not work when using the
  36330.       <VAR
  36331. CLASS="literal"
  36332. >&new</VAR
  36333. > statement. This is a limitation of the Zend
  36334.       Engine and will therefore result in a parser error.
  36335.      </P
  36336. ></BLOCKQUOTE
  36337. ></DIV
  36338. ><DIV
  36339. CLASS="warning"
  36340. ><P
  36341. ></P
  36342. ><TABLE
  36343. CLASS="warning"
  36344. BORDER="1"
  36345. WIDTH="100%"
  36346. ><TR
  36347. ><TD
  36348. ALIGN="CENTER"
  36349. ><B
  36350. >Warning</B
  36351. ></TD
  36352. ></TR
  36353. ><TR
  36354. ><TD
  36355. ALIGN="LEFT"
  36356. ><P
  36357. >      If you assign a reference to a variable declared <VAR
  36358. CLASS="literal"
  36359. >global</VAR
  36360. >
  36361.      inside a function, the reference will be visible only inside the function.
  36362.      You can avoid this by using the <VAR
  36363. CLASS="varname"
  36364. >$GLOBALS</VAR
  36365. > array.
  36366.      <TABLE
  36367. WIDTH="100%"
  36368. BORDER="0"
  36369. CELLPADDING="0"
  36370. CELLSPACING="0"
  36371. CLASS="EXAMPLE"
  36372. ><TR
  36373. ><TD
  36374. ><DIV
  36375. CLASS="example"
  36376. ><A
  36377. NAME="AEN5949"
  36378. ></A
  36379. ><P
  36380. ><B
  36381. >Example 21-1. Referencing global variables inside function</B
  36382. ></P
  36383. ><TABLE
  36384. BORDER="0"
  36385. BGCOLOR="#E0E0E0"
  36386. CELLPADDING="5"
  36387. ><TR
  36388. ><TD
  36389. ><PRE
  36390. CLASS="php"
  36391. ><?php
  36392. $var1 = "Example variable";
  36393. $var2 = "";
  36394.  
  36395. function global_references($use_globals)
  36396. {
  36397.     global $var1, $var2;
  36398.     if (!$use_globals) {
  36399.         $var2 =& $var1; // visible only inside the function
  36400.     } else {
  36401.         $GLOBALS["var2"] =& $var1; // visible also in global context
  36402.     }
  36403. }
  36404.  
  36405. global_references(false);
  36406. echo "var2 is set to '$var2'\n"; // var2 is set to ''
  36407. global_references(true);
  36408. echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
  36409. ?></PRE
  36410. ></TD
  36411. ></TR
  36412. ></TABLE
  36413. ></DIV
  36414. ></TD
  36415. ></TR
  36416. ></TABLE
  36417. >
  36418.      Think about <VAR
  36419. CLASS="literal"
  36420. >global $var;</VAR
  36421. > as a shortcut to <VAR
  36422. CLASS="literal"
  36423. >$var
  36424.      =& $GLOBALS['var'];</VAR
  36425. >. Thus assigning other reference
  36426.      to <VAR
  36427. CLASS="literal"
  36428. >$var</VAR
  36429. > only changes the local variable's reference.
  36430.     </P
  36431. ></TD
  36432. ></TR
  36433. ></TABLE
  36434. ></DIV
  36435. ><DIV
  36436. CLASS="note"
  36437. ><BLOCKQUOTE
  36438. CLASS="note"
  36439. ><P
  36440. ><B
  36441. >Note: </B
  36442. >
  36443.      If you assign a value to a variable with references in a <A
  36444. HREF="#control-structures.foreach"
  36445. >foreach</A
  36446. > statement,
  36447.      the references are modified too.
  36448.      <TABLE
  36449. WIDTH="100%"
  36450. BORDER="0"
  36451. CELLPADDING="0"
  36452. CELLSPACING="0"
  36453. CLASS="EXAMPLE"
  36454. ><TR
  36455. ><TD
  36456. ><DIV
  36457. CLASS="example"
  36458. ><A
  36459. NAME="AEN5958"
  36460. ></A
  36461. ><P
  36462. ><B
  36463. >Example 21-2. References and foreach statement</B
  36464. ></P
  36465. ><TABLE
  36466. BORDER="0"
  36467. BGCOLOR="#E0E0E0"
  36468. CELLPADDING="5"
  36469. ><TR
  36470. ><TD
  36471. ><PRE
  36472. CLASS="php"
  36473. ><?php
  36474. $ref = 0;
  36475. $row =& $ref;
  36476. foreach (array(1, 2, 3) as $row) {
  36477.     // do something
  36478. }
  36479. echo $ref; // 3 - last element of the iterated array
  36480. ?></PRE
  36481. ></TD
  36482. ></TR
  36483. ></TABLE
  36484. ></DIV
  36485. ></TD
  36486. ></TR
  36487. ></TABLE
  36488. >
  36489.     </P
  36490. ></BLOCKQUOTE
  36491. ></DIV
  36492. ><DIV
  36493. CLASS="warning"
  36494. ><P
  36495. ></P
  36496. ><TABLE
  36497. CLASS="warning"
  36498. BORDER="1"
  36499. WIDTH="100%"
  36500. ><TR
  36501. ><TD
  36502. ALIGN="CENTER"
  36503. ><B
  36504. >Warning</B
  36505. ></TD
  36506. ></TR
  36507. ><TR
  36508. ><TD
  36509. ALIGN="LEFT"
  36510. ><P
  36511. >      Complex arrays are sometimes rather copied than referenced. Thus following
  36512.      example will not work as expected.
  36513.      <TABLE
  36514. WIDTH="100%"
  36515. BORDER="0"
  36516. CELLPADDING="0"
  36517. CELLSPACING="0"
  36518. CLASS="EXAMPLE"
  36519. ><TR
  36520. ><TD
  36521. ><DIV
  36522. CLASS="example"
  36523. ><A
  36524. NAME="AEN5963"
  36525. ></A
  36526. ><P
  36527. ><B
  36528. >Example 21-3. References with complex arrays</B
  36529. ></P
  36530. ><TABLE
  36531. BORDER="0"
  36532. BGCOLOR="#E0E0E0"
  36533. CELLPADDING="5"
  36534. ><TR
  36535. ><TD
  36536. ><PRE
  36537. CLASS="php"
  36538. ><?php
  36539. $top = array(
  36540.     'A' => array(),
  36541.     'B' => array(
  36542.         'B_b' => array(),
  36543.     ),
  36544. );
  36545.  
  36546. $top['A']['parent'] = &$top;
  36547. $top['B']['parent'] = &$top;
  36548. $top['B']['B_b']['data'] = 'test';
  36549. print_r($top['A']['parent']['B']['B_b']); // array()
  36550. ?></PRE
  36551. ></TD
  36552. ></TR
  36553. ></TABLE
  36554. ></DIV
  36555. ></TD
  36556. ></TR
  36557. ></TABLE
  36558. >
  36559.     </P
  36560. ></TD
  36561. ></TR
  36562. ></TABLE
  36563. ></DIV
  36564. ><P
  36565. >     The second thing references do is to pass variables
  36566.     by-reference. This is done by making a local variable in a function and
  36567.     a variable in the calling scope reference to the same content. Example:
  36568.     <DIV
  36569. CLASS="informalexample"
  36570. ><P
  36571. ></P
  36572. ><A
  36573. NAME="AEN5967"
  36574. ></A
  36575. ><TABLE
  36576. BORDER="0"
  36577. BGCOLOR="#E0E0E0"
  36578. CELLPADDING="5"
  36579. ><TR
  36580. ><TD
  36581. ><PRE
  36582. CLASS="php"
  36583. ><?php
  36584. function foo(&$var)
  36585. {
  36586.     $var++;
  36587. }
  36588.  
  36589. $a=5;
  36590. foo($a);
  36591. ?></PRE
  36592. ></TD
  36593. ></TR
  36594. ></TABLE
  36595. ><P
  36596. ></P
  36597. ></DIV
  36598. >
  36599.     will make <VAR
  36600. CLASS="varname"
  36601. >$a</VAR
  36602. > to be 6. This happens because in
  36603.     the function <VAR
  36604. CLASS="varname"
  36605. >foo</VAR
  36606. > the variable
  36607.     <VAR
  36608. CLASS="varname"
  36609. >$var</VAR
  36610. > refers to the same content as
  36611.     <VAR
  36612. CLASS="varname"
  36613. >$a</VAR
  36614. >. See also more detailed explanations about <A
  36615. HREF="#language.references.pass"
  36616. >passing by reference</A
  36617. >.
  36618.    </P
  36619. ><P
  36620. >     The third thing reference can do is <A
  36621. HREF="#language.references.return"
  36622. >return by reference</A
  36623. >.
  36624.    </P
  36625. ></DIV
  36626. ><DIV
  36627. CLASS="sect1"
  36628. ><HR><H2
  36629. CLASS="sect1"
  36630. ><A
  36631. NAME="language.references.arent"
  36632. >What References Are Not</A
  36633. ></H2
  36634. ><P
  36635. >     As said before, references aren't pointers. That means, the
  36636.     following construct won't do what you expect:
  36637.     <DIV
  36638. CLASS="informalexample"
  36639. ><P
  36640. ></P
  36641. ><A
  36642. NAME="AEN5979"
  36643. ></A
  36644. ><TABLE
  36645. BORDER="0"
  36646. BGCOLOR="#E0E0E0"
  36647. CELLPADDING="5"
  36648. ><TR
  36649. ><TD
  36650. ><PRE
  36651. CLASS="php"
  36652. ><?php
  36653. function foo(&$var)
  36654. {
  36655.     $var =& $GLOBALS["baz"];
  36656. }
  36657. foo($bar); 
  36658. ?></PRE
  36659. ></TD
  36660. ></TR
  36661. ></TABLE
  36662. ><P
  36663. ></P
  36664. ></DIV
  36665. >
  36666.    </P
  36667. ><P
  36668. >     What happens is that <VAR
  36669. CLASS="varname"
  36670. >$var</VAR
  36671. > in
  36672.     <VAR
  36673. CLASS="varname"
  36674. >foo</VAR
  36675. > will be bound with
  36676.     <VAR
  36677. CLASS="varname"
  36678. >$bar</VAR
  36679. > in caller, but then it will be
  36680.     re-bound with <VAR
  36681. CLASS="varname"
  36682. >$GLOBALS["baz"]</VAR
  36683. >. There's no way
  36684.     to bind <VAR
  36685. CLASS="varname"
  36686. >$bar</VAR
  36687. > in the calling scope to something else
  36688.     using the reference mechanism, since <VAR
  36689. CLASS="varname"
  36690. >$bar</VAR
  36691. > is not
  36692.     available in the function <VAR
  36693. CLASS="varname"
  36694. >foo</VAR
  36695. > (it is represented by
  36696.     <VAR
  36697. CLASS="varname"
  36698. >$var</VAR
  36699. >, but <VAR
  36700. CLASS="varname"
  36701. >$var</VAR
  36702. > has only
  36703.     variable contents and not name-to-value binding in the calling
  36704.     symbol table).
  36705.     You can use <A
  36706. HREF="#language.references.return"
  36707. >returning
  36708.     references</A
  36709. > to reference variables selected by the function.
  36710.    </P
  36711. ></DIV
  36712. ><DIV
  36713. CLASS="sect1"
  36714. ><HR><H2
  36715. CLASS="sect1"
  36716. ><A
  36717. NAME="language.references.pass"
  36718. >Passing by Reference</A
  36719. ></H2
  36720. ><P
  36721. >    You can pass variable to function by reference, so that function could modify
  36722.    its arguments. The syntax is as follows:
  36723.     <DIV
  36724. CLASS="informalexample"
  36725. ><P
  36726. ></P
  36727. ><A
  36728. NAME="AEN5995"
  36729. ></A
  36730. ><TABLE
  36731. BORDER="0"
  36732. BGCOLOR="#E0E0E0"
  36733. CELLPADDING="5"
  36734. ><TR
  36735. ><TD
  36736. ><PRE
  36737. CLASS="php"
  36738. ><?php
  36739. function foo(&$var)
  36740. {
  36741.     $var++;
  36742. }
  36743.  
  36744. $a=5;
  36745. foo($a);
  36746. // $a is 6 here
  36747. ?></PRE
  36748. ></TD
  36749. ></TR
  36750. ></TABLE
  36751. ><P
  36752. ></P
  36753. ></DIV
  36754. >
  36755.   Note that there's no reference sign on function call - only on
  36756.   function definition. Function definition alone is enough to
  36757.   correctly pass the argument by reference. 
  36758.   </P
  36759. ><P
  36760. >   Following things can be passed by reference:
  36761.    <P
  36762. ></P
  36763. ><UL
  36764. ><LI
  36765. ><P
  36766. >       Variable, i.e. <VAR
  36767. CLASS="literal"
  36768. >foo($a)</VAR
  36769. >
  36770.      </P
  36771. ></LI
  36772. ><LI
  36773. ><P
  36774. >       New statement, i.e. <VAR
  36775. CLASS="literal"
  36776. >foo(new foobar())</VAR
  36777. >
  36778.      </P
  36779. ></LI
  36780. ><LI
  36781. ><P
  36782. >       Reference, returned from a function, i.e.:
  36783.     <DIV
  36784. CLASS="informalexample"
  36785. ><P
  36786. ></P
  36787. ><A
  36788. NAME="AEN6007"
  36789. ></A
  36790. ><TABLE
  36791. BORDER="0"
  36792. BGCOLOR="#E0E0E0"
  36793. CELLPADDING="5"
  36794. ><TR
  36795. ><TD
  36796. ><PRE
  36797. CLASS="php"
  36798. ><?php
  36799. function &bar()
  36800. {
  36801.     $a = 5;
  36802.     return $a;
  36803. }
  36804. foo(bar());
  36805. ?></PRE
  36806. ></TD
  36807. ></TR
  36808. ></TABLE
  36809. ><P
  36810. ></P
  36811. ></DIV
  36812. >
  36813.     See also explanations about <A
  36814. HREF="#language.references.return"
  36815. >returning by reference</A
  36816. >. 
  36817.      </P
  36818. ></LI
  36819. ></UL
  36820. >
  36821.   </P
  36822. ><P
  36823. >   Any other expression should not be passed by reference, as the
  36824.   result is undefined. For example, the following examples of passing
  36825.   by reference are invalid:
  36826.     <DIV
  36827. CLASS="informalexample"
  36828. ><P
  36829. ></P
  36830. ><A
  36831. NAME="AEN6011"
  36832. ></A
  36833. ><TABLE
  36834. BORDER="0"
  36835. BGCOLOR="#E0E0E0"
  36836. CELLPADDING="5"
  36837. ><TR
  36838. ><TD
  36839. ><PRE
  36840. CLASS="php"
  36841. ><?php
  36842. function bar() // Note the missing &
  36843. {
  36844.     $a = 5;
  36845.     return $a;
  36846. }
  36847. foo(bar());
  36848.  
  36849. foo($a = 5); // Expression, not variable
  36850. foo(5); // Constant, not variable
  36851. ?></PRE
  36852. ></TD
  36853. ></TR
  36854. ></TABLE
  36855. ><P
  36856. ></P
  36857. ></DIV
  36858. >
  36859.     These requirements are for PHP 4.0.4 and later.
  36860.   </P
  36861. ></DIV
  36862. ><DIV
  36863. CLASS="sect1"
  36864. ><HR><H2
  36865. CLASS="sect1"
  36866. ><A
  36867. NAME="language.references.return"
  36868. >Returning References</A
  36869. ></H2
  36870. ><P
  36871. >     Returning by-reference is useful when you want to use a function
  36872.     to find which variable a reference should be bound to. When
  36873.     returning references, use this syntax:
  36874.     <DIV
  36875. CLASS="informalexample"
  36876. ><P
  36877. ></P
  36878. ><A
  36879. NAME="AEN6016"
  36880. ></A
  36881. ><TABLE
  36882. BORDER="0"
  36883. BGCOLOR="#E0E0E0"
  36884. CELLPADDING="5"
  36885. ><TR
  36886. ><TD
  36887. ><PRE
  36888. CLASS="php"
  36889. ><?php
  36890. function &find_var($param)
  36891. {
  36892.     /* ...code... */
  36893.     return $found_var;
  36894. }
  36895.  
  36896. $foo =& find_var($bar);
  36897. $foo->x = 2;
  36898. ?></PRE
  36899. ></TD
  36900. ></TR
  36901. ></TABLE
  36902. ><P
  36903. ></P
  36904. ></DIV
  36905. >
  36906.     In this example, the property of the object returned by the
  36907.     <VAR
  36908. CLASS="varname"
  36909. >find_var</VAR
  36910. > function would be set, not the
  36911.     copy, as it would be without using reference syntax.
  36912.    </P
  36913. ><DIV
  36914. CLASS="note"
  36915. ><BLOCKQUOTE
  36916. CLASS="note"
  36917. ><P
  36918. ><B
  36919. >Note: </B
  36920. >
  36921.      Unlike parameter passing, here you have to use
  36922.      <VAR
  36923. CLASS="literal"
  36924. >&</VAR
  36925. > in both places - to indicate that you
  36926.      return by-reference, not a copy as usual, and to indicate that
  36927.      reference binding, rather than usual assignment, should be done
  36928.      for <VAR
  36929. CLASS="varname"
  36930. >$foo</VAR
  36931. >.
  36932.     </P
  36933. ></BLOCKQUOTE
  36934. ></DIV
  36935. ></DIV
  36936. ><DIV
  36937. CLASS="sect1"
  36938. ><HR><H2
  36939. CLASS="sect1"
  36940. ><A
  36941. NAME="language.references.unset"
  36942. >Unsetting References</A
  36943. ></H2
  36944. ><P
  36945. >     When you unset the reference, you just break the binding between
  36946.     variable name and variable content. This does not mean that
  36947.     variable content will be destroyed. For example:
  36948.     <DIV
  36949. CLASS="informalexample"
  36950. ><P
  36951. ></P
  36952. ><A
  36953. NAME="AEN6026"
  36954. ></A
  36955. ><TABLE
  36956. BORDER="0"
  36957. BGCOLOR="#E0E0E0"
  36958. CELLPADDING="5"
  36959. ><TR
  36960. ><TD
  36961. ><PRE
  36962. CLASS="php"
  36963. ><?php
  36964. $a = 1;
  36965. $b =& $a;
  36966. unset($a); 
  36967. ?></PRE
  36968. ></TD
  36969. ></TR
  36970. ></TABLE
  36971. ><P
  36972. ></P
  36973. ></DIV
  36974. >
  36975.     won't unset <VAR
  36976. CLASS="varname"
  36977. >$b</VAR
  36978. >, just <VAR
  36979. CLASS="varname"
  36980. >$a</VAR
  36981. >. 
  36982.    </P
  36983. ><P
  36984. >     Again, it might be useful to think about this as analogous to Unix
  36985.     <B
  36986. CLASS="command"
  36987. >unlink</B
  36988. > call.
  36989.    </P
  36990. ></DIV
  36991. ><DIV
  36992. CLASS="sect1"
  36993. ><HR><H2
  36994. CLASS="sect1"
  36995. ><A
  36996. NAME="language.references.spot"
  36997. >Spotting References</A
  36998. ></H2
  36999. ><P
  37000. >     Many syntax constructs in PHP are implemented via referencing
  37001.     mechanisms, so everything told above about reference binding also
  37002.     apply to these constructs. Some constructs, like passing and
  37003.     returning by-reference, are mentioned above. Other constructs that
  37004.     use references are:
  37005.    </P
  37006. ><DIV
  37007. CLASS="sect2"
  37008. ><HR><H3
  37009. CLASS="sect2"
  37010. ><A
  37011. NAME="references.global"
  37012. ><VAR
  37013. CLASS="literal"
  37014. >global</VAR
  37015. > References</A
  37016. ></H3
  37017. ><P
  37018. >      When you declare variable as <B
  37019. CLASS="command"
  37020. >global $var</B
  37021. > you
  37022.      are in fact creating reference to a global variable. That means,
  37023.      this is the same as:
  37024.      <DIV
  37025. CLASS="informalexample"
  37026. ><P
  37027. ></P
  37028. ><A
  37029. NAME="AEN6040"
  37030. ></A
  37031. ><TABLE
  37032. BORDER="0"
  37033. BGCOLOR="#E0E0E0"
  37034. CELLPADDING="5"
  37035. ><TR
  37036. ><TD
  37037. ><PRE
  37038. CLASS="php"
  37039. ><?php
  37040. $var =& $GLOBALS["var"];
  37041. ?></PRE
  37042. ></TD
  37043. ></TR
  37044. ></TABLE
  37045. ><P
  37046. ></P
  37047. ></DIV
  37048. >
  37049.     </P
  37050. ><P
  37051. >      That means, for example, that unsetting <VAR
  37052. CLASS="varname"
  37053. >$var</VAR
  37054. >
  37055.      won't unset global variable.
  37056.     </P
  37057. ></DIV
  37058. ><DIV
  37059. CLASS="sect2"
  37060. ><HR><H3
  37061. CLASS="sect2"
  37062. ><A
  37063. NAME="references.this"
  37064. ><VAR
  37065. CLASS="literal"
  37066. >$this</VAR
  37067. ></A
  37068. ></H3
  37069. ><P
  37070. >      In an object method, <VAR
  37071. CLASS="varname"
  37072. >$this</VAR
  37073. > is always reference
  37074.      to the caller object.
  37075.     </P
  37076. ></DIV
  37077. ></DIV
  37078. ></DIV
  37079. ></DIV
  37080. ><DIV
  37081. CLASS="PART"
  37082. ><A
  37083. NAME="security"
  37084. ></A
  37085. ><DIV
  37086. CLASS="TITLEPAGE"
  37087. ><H1
  37088. CLASS="title"
  37089. >IV. Security</H1
  37090. ><DIV
  37091. CLASS="TOC"
  37092. ><DL
  37093. ><DT
  37094. ><B
  37095. >Table of Contents</B
  37096. ></DT
  37097. ><DT
  37098. >22. <A
  37099. HREF="#security.intro"
  37100. >Introduction</A
  37101. ></DT
  37102. ><DT
  37103. >23. <A
  37104. HREF="#security.general"
  37105. >General considerations</A
  37106. ></DT
  37107. ><DT
  37108. >24. <A
  37109. HREF="#security.cgi-bin"
  37110. >Installed as CGI binary</A
  37111. ></DT
  37112. ><DT
  37113. >25. <A
  37114. HREF="#security.apache"
  37115. >Installed as an Apache module</A
  37116. ></DT
  37117. ><DT
  37118. >26. <A
  37119. HREF="#security.filesystem"
  37120. >Filesystem Security</A
  37121. ></DT
  37122. ><DT
  37123. >27. <A
  37124. HREF="#security.database"
  37125. >Database Security</A
  37126. ></DT
  37127. ><DT
  37128. >28. <A
  37129. HREF="#security.errors"
  37130. >Error Reporting</A
  37131. ></DT
  37132. ><DT
  37133. >29. <A
  37134. HREF="#security.globals"
  37135. >Using Register Globals</A
  37136. ></DT
  37137. ><DT
  37138. >30. <A
  37139. HREF="#security.variables"
  37140. >User Submitted Data</A
  37141. ></DT
  37142. ><DT
  37143. >31. <A
  37144. HREF="#security.magicquotes"
  37145. >Magic Quotes</A
  37146. ></DT
  37147. ><DT
  37148. >32. <A
  37149. HREF="#security.hiding"
  37150. >Hiding PHP</A
  37151. ></DT
  37152. ><DT
  37153. >33. <A
  37154. HREF="#security.current"
  37155. >Keeping Current</A
  37156. ></DT
  37157. ></DL
  37158. ></DIV
  37159. ></DIV
  37160. ><DIV
  37161. CLASS="chapter"
  37162. ><HR><H1
  37163. ><A
  37164. NAME="security.intro"
  37165. >Chapter 22. Introduction</A
  37166. ></H1
  37167. ><P
  37168. >    PHP is a powerful language and the interpreter, whether included
  37169.    in a web server as a module or executed as a separate
  37170.    <ACRONYM
  37171. CLASS="acronym"
  37172. >CGI</ACRONYM
  37173. > binary, is able to access files, execute
  37174.    commands and open network connections on the server.  These
  37175.    properties make anything run on a web server insecure by default.
  37176.    PHP is designed specifically to be a more secure language for
  37177.    writing CGI programs than Perl or C, and with correct selection of
  37178.    compile-time and runtime configuration options, and proper coding
  37179.    practices, it can give you exactly the combination of freedom and
  37180.    security you need.
  37181.   </P
  37182. ><P
  37183. >    As there are many different ways of utilizing PHP, there are many
  37184.    configuration options controlling its behaviour.  A large
  37185.    selection of options guarantees you can use PHP for a lot of
  37186.    purposes, but it also means there are combinations of these
  37187.    options and server configurations that result in an insecure
  37188.    setup.
  37189.   </P
  37190. ><P
  37191. >    The configuration flexibility of PHP is equally rivalled by the
  37192.    code flexibility. PHP can be used to build complete server
  37193.    applications, with all the power of a shell user, or it can be used
  37194.    for simple server-side includes with little risk in a tightly
  37195.    controlled environment. How you build that environment, and how
  37196.    secure it is, is largely up to the PHP developer.
  37197.   </P
  37198. ><P
  37199. >    This chapter starts with some general security advice, explains
  37200.    the different configuration option combinations and the situations
  37201.    they can be safely used, and describes different considerations in
  37202.    coding for different levels of security.
  37203.   </P
  37204. ></DIV
  37205. ><DIV
  37206. CLASS="chapter"
  37207. ><HR><H1
  37208. ><A
  37209. NAME="security.general"
  37210. >Chapter 23. General considerations</A
  37211. ></H1
  37212. ><P
  37213. >     A completely secure system is a virtual impossibility, so an
  37214.     approach often used in the security profession is one of balancing
  37215.     risk and usability. If every variable submitted by a user required
  37216.     two forms of biometric validation (such as a retinal scan and a
  37217.     fingerprint), you would have an extremely high level of
  37218.     accountability. It would also take half an hour to fill out a fairly
  37219.     complex form, which would tend to encourage users to find ways of
  37220.     bypassing the security.
  37221.    </P
  37222. ><P
  37223. >     The best security is often unobtrusive enough to suit the
  37224.     requirements without the user being prevented from accomplishing
  37225.     their work, or over-burdening the code author with excessive
  37226.     complexity. Indeed, some security attacks are merely exploits of
  37227.     this kind of overly built security, which tends to erode over time.
  37228.    </P
  37229. ><P
  37230. >     A phrase worth remembering: A system is only as good as the weakest
  37231.     link in a chain. If all transactions are heavily logged based on
  37232.     time, location, transaction type, etc. but the user is only
  37233.     verified based on a single cookie, the validity of tying the users
  37234.     to the transaction log is severely weakened.
  37235.    </P
  37236. ><P
  37237. >     When testing, keep in mind that you will not be able to test all
  37238.     possibilities for even the simplest of pages. The input you
  37239.     may expect will be completely unrelated to the input given by
  37240.     a disgruntled employee, a cracker with months of time on their
  37241.     hands, or a housecat walking across the keyboard. This is why it's
  37242.     best to look at the code from a logical perspective, to discern
  37243.     where unexpected data can be introduced, and then follow how it is
  37244.     modified, reduced, or amplified.
  37245.    </P
  37246. ><P
  37247. >     The Internet is filled with people trying to make a name for
  37248.     themselves by breaking your code, crashing your site, posting
  37249.     inappropriate content, and otherwise making your day interesting.
  37250.     It doesn't matter if you have a small or large site, you are
  37251.     a target by simply being online, by having a server that can be
  37252.     connected to. Many cracking programs do not discern by size, they
  37253.     simply trawl massive IP blocks looking for victims. Try not to
  37254.     become one.
  37255.    </P
  37256. ></DIV
  37257. ><DIV
  37258. CLASS="chapter"
  37259. ><HR><H1
  37260. ><A
  37261. NAME="security.cgi-bin"
  37262. >Chapter 24. Installed as CGI binary</A
  37263. ></H1
  37264. ><DIV
  37265. CLASS="sect1"
  37266. ><H2
  37267. CLASS="sect1"
  37268. ><A
  37269. NAME="security.cgi-bin.attacks"
  37270. >Possible attacks</A
  37271. ></H2
  37272. ><P
  37273. >      Using PHP as a <ACRONYM
  37274. CLASS="acronym"
  37275. >CGI</ACRONYM
  37276. > binary is an option for
  37277.      setups that for some reason do not wish to integrate PHP as a
  37278.      module into server software (like Apache), or will use PHP with
  37279.      different kinds of CGI wrappers to create safe chroot and setuid
  37280.      environments for scripts.  This setup usually involves installing
  37281.      executable PHP binary to the web server cgi-bin directory.  CERT
  37282.      advisory <A
  37283. HREF="http://www.cert.org/advisories/CA-1996-11.html"
  37284. TARGET="_top"
  37285. >CA-96.11</A
  37286. > recommends
  37287.      against placing any interpreters into cgi-bin.  Even if the PHP
  37288.      binary can be used as a standalone interpreter, PHP is designed
  37289.      to prevent the attacks this setup makes possible:
  37290.     </P
  37291. ><P
  37292. ></P
  37293. ><UL
  37294. ><LI
  37295. ><P
  37296. >        Accessing system files: <TT
  37297. CLASS="filename"
  37298. >http://my.host/cgi-bin/php?/etc/passwd</TT
  37299. >
  37300.       </P
  37301. ><P
  37302. >        The query information in a URL after the question mark (?)  is
  37303.        passed as command line arguments to the interpreter by the CGI
  37304.        interface.  Usually interpreters open and execute the file
  37305.        specified as the first argument on the command line.
  37306.       </P
  37307. ><P
  37308. >        When invoked as a CGI binary, PHP refuses to interpret the
  37309.        command line arguments.
  37310.       </P
  37311. ></LI
  37312. ><LI
  37313. ><P
  37314. >        Accessing any web document on server: <TT
  37315. CLASS="filename"
  37316. >http://my.host/cgi-bin/php/secret/doc.html</TT
  37317. >
  37318.       </P
  37319. ><P
  37320. >        The path information part of the URL after the PHP binary name,
  37321.        <TT
  37322. CLASS="filename"
  37323. >/secret/doc.html</TT
  37324. > is
  37325.        conventionally used to specify the name of the file to be
  37326.        opened and interpreted by the <ACRONYM
  37327. CLASS="acronym"
  37328. >CGI</ACRONYM
  37329. > program.
  37330.        Usually some web server configuration directives (Apache:
  37331.        Action) are used to redirect requests to documents like
  37332.        <TT
  37333. CLASS="filename"
  37334. >http://my.host/secret/script.php</TT
  37335. > to the
  37336.        PHP interpreter.  With this setup, the web server first checks
  37337.        the access permissions to the directory <TT
  37338. CLASS="filename"
  37339. >/secret</TT
  37340. >, and after that creates the
  37341.        redirected request <TT
  37342. CLASS="filename"
  37343. >http://my.host/cgi-bin/php/secret/script.php</TT
  37344. >.
  37345.        Unfortunately, if the request is originally given in this form,
  37346.        no access checks are made by web server for file <TT
  37347. CLASS="filename"
  37348. >/secret/script.php</TT
  37349. >, but only for the
  37350.        <TT
  37351. CLASS="filename"
  37352. >/cgi-bin/php</TT
  37353. > file.  This way
  37354.        any user able to access <TT
  37355. CLASS="filename"
  37356. >/cgi-bin/php</TT
  37357. > is able to access any
  37358.        protected document on the web server.
  37359.       </P
  37360. ><P
  37361. >        In PHP, compile-time configuration option <A
  37362. HREF="#configure.enable-force-cgi-redirect"
  37363. >--enable-force-cgi-redirect</A
  37364. >
  37365.        and runtime configuration directives <A
  37366. HREF="#ini.doc-root"
  37367. >doc_root</A
  37368. > and <A
  37369. HREF="#ini.user-dir"
  37370. >user_dir</A
  37371. > can be used to prevent
  37372.        this attack, if the server document tree has any directories
  37373.        with access restrictions.  See below for full the explanation
  37374.        of the different combinations.
  37375.       </P
  37376. ></LI
  37377. ></UL
  37378. ></DIV
  37379. ><DIV
  37380. CLASS="sect1"
  37381. ><HR><H2
  37382. CLASS="sect1"
  37383. ><A
  37384. NAME="security.cgi-bin.default"
  37385. >Case 1: only public files served</A
  37386. ></H2
  37387. ><P
  37388. >      If your server does not have any content that is not restricted
  37389.      by password or ip based access control, there is no need for
  37390.      these configuration options.  If your web server does not allow
  37391.      you to do redirects, or the server does not have a way to
  37392.      communicate to the PHP binary that the request is a safely
  37393.      redirected request, you can specify the option <A
  37394. HREF="#configure.enable-force-cgi-redirect"
  37395. >--enable-force-cgi-redirect</A
  37396. >
  37397.      to the configure script.  You still have to make sure your PHP
  37398.      scripts do not rely on one or another way of calling the script,
  37399.      neither by directly <TT
  37400. CLASS="filename"
  37401. >http://my.host/cgi-bin/php/dir/script.php</TT
  37402. >
  37403.      nor by redirection <TT
  37404. CLASS="filename"
  37405. >http://my.host/dir/script.php</TT
  37406. >.
  37407.     </P
  37408. ><P
  37409. >      Redirection can be configured in Apache by using AddHandler and
  37410.      Action directives (see below).
  37411.     </P
  37412. ></DIV
  37413. ><DIV
  37414. CLASS="sect1"
  37415. ><HR><H2
  37416. CLASS="sect1"
  37417. ><A
  37418. NAME="security.cgi-bin.force-redirect"
  37419. >Case 2: using --enable-force-cgi-redirect</A
  37420. ></H2
  37421. ><P
  37422. >      This compile-time option prevents anyone from calling PHP
  37423.      directly with a URL like <TT
  37424. CLASS="filename"
  37425. >http://my.host/cgi-bin/php/secretdir/script.php</TT
  37426. >.
  37427.      Instead, PHP will only parse in this mode if it has gone through
  37428.      a web server redirect rule.
  37429.     </P
  37430. ><P
  37431. >      Usually the redirection in the Apache configuration is done with
  37432.      the following directives:
  37433.     </P
  37434. ><TABLE
  37435. BORDER="0"
  37436. BGCOLOR="#E0E0E0"
  37437. CELLPADDING="5"
  37438. ><TR
  37439. ><TD
  37440. ><PRE
  37441. CLASS="apache-conf"
  37442. >Action php-script /cgi-bin/php
  37443. AddHandler php-script .php</PRE
  37444. ></TD
  37445. ></TR
  37446. ></TABLE
  37447. ><P
  37448. >      This option has only been tested with the Apache web server, and
  37449.      relies on Apache to set the non-standard CGI environment variable
  37450.      <VAR
  37451. CLASS="envar"
  37452. >REDIRECT_STATUS</VAR
  37453. > on redirected requests.  If your
  37454.      web server does not support any way of telling if the request is
  37455.      direct or redirected, you cannot use this option and you must use
  37456.      one of the other ways of running the CGI version documented
  37457.      here.
  37458.     </P
  37459. ></DIV
  37460. ><DIV
  37461. CLASS="sect1"
  37462. ><HR><H2
  37463. CLASS="sect1"
  37464. ><A
  37465. NAME="security.cgi-bin.doc-root"
  37466. >Case 3: setting doc_root or user_dir</A
  37467. ></H2
  37468. ><P
  37469. >      To include active content, like scripts and executables, in the
  37470.      web server document directories is sometimes considered an insecure
  37471.      practice.  If, because of some configuration mistake, the scripts
  37472.      are not executed but displayed as regular HTML documents, this
  37473.      may result in leakage of intellectual property or security
  37474.      information like passwords.  Therefore many sysadmins will prefer
  37475.      setting up another directory structure for scripts that are
  37476.      accessible only through the PHP CGI, and therefore always
  37477.      interpreted and not displayed as such.
  37478.     </P
  37479. ><P
  37480. >      Also if the method for making sure the requests are not
  37481.      redirected, as described in the previous section, is not
  37482.      available, it is necessary to set up a script doc_root that is
  37483.      different from web document root.
  37484.     </P
  37485. ><P
  37486. >      You can set the PHP script document root by the configuration
  37487.      directive <A
  37488. HREF="#ini.doc-root"
  37489. >doc_root</A
  37490. > in the
  37491.      <A
  37492. HREF="#configuration.file"
  37493. >configuration file</A
  37494. >, or
  37495.      you can set the environment variable
  37496.      <VAR
  37497. CLASS="envar"
  37498. >PHP_DOCUMENT_ROOT</VAR
  37499. >.  If it is set, the CGI version
  37500.      of PHP will always construct the file name to open with this
  37501.      <VAR
  37502. CLASS="parameter"
  37503. >doc_root</VAR
  37504. > and the path information in the
  37505.      request, so you can be sure no script is executed outside this
  37506.      directory (except for <VAR
  37507. CLASS="parameter"
  37508. >user_dir</VAR
  37509. >
  37510.      below).
  37511.     </P
  37512. ><P
  37513. >      Another option usable here is <A
  37514. HREF="#ini.user-dir"
  37515. >user_dir</A
  37516. >.  When user_dir is unset,
  37517.      only thing controlling the opened file name is
  37518.      <VAR
  37519. CLASS="parameter"
  37520. >doc_root</VAR
  37521. >.  Opening a URL like <TT
  37522. CLASS="filename"
  37523. >http://my.host/~user/doc.php</TT
  37524. > does not
  37525.      result in opening a file under users home directory, but a file
  37526.      called <TT
  37527. CLASS="filename"
  37528. >~user/doc.php</TT
  37529. > under
  37530.      doc_root (yes, a directory name starting with a tilde
  37531.      [<VAR
  37532. CLASS="literal"
  37533. >~</VAR
  37534. >]).
  37535.     </P
  37536. ><P
  37537. >      If user_dir is set to for example <TT
  37538. CLASS="filename"
  37539. >public_php</TT
  37540. >, a request like <TT
  37541. CLASS="filename"
  37542. >http://my.host/~user/doc.php</TT
  37543. > will open a
  37544.      file called <TT
  37545. CLASS="filename"
  37546. >doc.php</TT
  37547. > under the directory
  37548.      named <TT
  37549. CLASS="filename"
  37550. >public_php</TT
  37551. > under the home
  37552.      directory of the user.  If the home of the user is <TT
  37553. CLASS="filename"
  37554. >/home/user</TT
  37555. >, the file executed is
  37556.      <TT
  37557. CLASS="filename"
  37558. >/home/user/public_php/doc.php</TT
  37559. >.
  37560.     </P
  37561. ><P
  37562. >      <VAR
  37563. CLASS="parameter"
  37564. >user_dir</VAR
  37565. > expansion happens regardless of
  37566.      the <VAR
  37567. CLASS="parameter"
  37568. >doc_root</VAR
  37569. > setting, so you can control
  37570.      the document root and user directory access
  37571.      separately.
  37572.     </P
  37573. ></DIV
  37574. ><DIV
  37575. CLASS="sect1"
  37576. ><HR><H2
  37577. CLASS="sect1"
  37578. ><A
  37579. NAME="security.cgi-bin.shell"
  37580. >Case 4: PHP parser outside of web tree</A
  37581. ></H2
  37582. ><P
  37583. >      A very secure option is to put the PHP parser binary somewhere
  37584.      outside of the web tree of files.  In <TT
  37585. CLASS="filename"
  37586. >/usr/local/bin</TT
  37587. >, for example.  The only real
  37588.      downside to this option is that you will now have to put a line
  37589.      similar to:
  37590.      <DIV
  37591. CLASS="informalexample"
  37592. ><P
  37593. ></P
  37594. ><A
  37595. NAME="AEN6139"
  37596. ></A
  37597. ><TABLE
  37598. BORDER="0"
  37599. BGCOLOR="#E0E0E0"
  37600. CELLPADDING="5"
  37601. ><TR
  37602. ><TD
  37603. ><PRE
  37604. CLASS="programlisting"
  37605. >#!/usr/local/bin/php</PRE
  37606. ></TD
  37607. ></TR
  37608. ></TABLE
  37609. ><P
  37610. ></P
  37611. ></DIV
  37612. >
  37613.      as the first line of any file containing PHP tags.  You will also
  37614.      need to make the file executable.  That is, treat it exactly as
  37615.      you would treat any other CGI script written in Perl or sh or any
  37616.      other common scripting language which uses the
  37617.      <VAR
  37618. CLASS="literal"
  37619. >#!</VAR
  37620. > shell-escape mechanism for launching
  37621.      itself.
  37622.     </P
  37623. ><P
  37624. >      To get PHP to handle <VAR
  37625. CLASS="envar"
  37626. >PATH_INFO</VAR
  37627. > and
  37628.      <VAR
  37629. CLASS="envar"
  37630. >PATH_TRANSLATED</VAR
  37631. > information correctly with this
  37632.      setup, the PHP parser should be compiled with the <A
  37633. HREF="#configure.enable-discard-path"
  37634. >--enable-discard-path</A
  37635. >
  37636.      configure option.
  37637.     </P
  37638. ></DIV
  37639. ></DIV
  37640. ><DIV
  37641. CLASS="chapter"
  37642. ><HR><H1
  37643. ><A
  37644. NAME="security.apache"
  37645. >Chapter 25. Installed as an Apache module</A
  37646. ></H1
  37647. ><P
  37648. >     When PHP is used as an Apache module it inherits Apache's user
  37649.     permissions (typically those of the "nobody" user). This has several
  37650.     impacts on security and authorization. For example, if you are using
  37651.     PHP to access a database, unless that database has built-in access
  37652.     control, you will have to make the database accessible to the
  37653.     "nobody" user. This means a malicious script could access and modify
  37654.     the database, even without a username and password. It's entirely
  37655.     possible that a web spider could stumble across a database
  37656.     administrator's web page, and drop all of your databases. You can
  37657.     protect against this with Apache authorization, or you can design
  37658.     your own access model using LDAP, <TT
  37659. CLASS="filename"
  37660. >.htaccess</TT
  37661. > files, etc. and include
  37662.     that code as part of your PHP scripts.
  37663.    </P
  37664. ><P
  37665. >     Often, once security is established to the point where the PHP user
  37666.     (in this case, the apache user) has very little risk attached to it,
  37667.     it is discovered that PHP is now prevented from writing any files
  37668.     to user directories. Or perhaps it has been prevented from accessing
  37669.     or changing databases. It has equally been secured from writing
  37670.     good and bad files, or entering good and bad database transactions.
  37671.    </P
  37672. ><P
  37673. >     A frequent security mistake made at this point is to allow apache
  37674.     root permissions, or to escalate apache's abilities in some other
  37675.     way.
  37676.    </P
  37677. ><P
  37678. >     Escalating the Apache user's permissions to root is extremely
  37679.     dangerous and may compromise the entire system, so sudo'ing,
  37680.     chroot'ing, or otherwise running as root should not be considered by
  37681.     those who are not security professionals.
  37682.    </P
  37683. ><P
  37684. >     There are some simpler solutions. By using
  37685.     <A
  37686. HREF="#ini.open-basedir"
  37687. >open_basedir</A
  37688. > you can control and restrict what
  37689.     directories are allowed to be used for PHP. You can also set up
  37690.     apache-only areas, to restrict all web based activity to non-user,
  37691.     or non-system, files.
  37692.    </P
  37693. ></DIV
  37694. ><DIV
  37695. CLASS="chapter"
  37696. ><HR><H1
  37697. ><A
  37698. NAME="security.filesystem"
  37699. >Chapter 26. Filesystem Security</A
  37700. ></H1
  37701. ><P
  37702. >     PHP is subject to the security built into most server systems with
  37703.     respect to permissions on a file and directory basis. This allows
  37704.     you to control which files in the filesystem may be read. Care
  37705.     should be taken with any files which are world readable to ensure
  37706.     that they are safe for reading by all users who have access to that
  37707.     filesystem.
  37708.    </P
  37709. ><P
  37710. >     Since PHP was designed to allow user level access to the filesystem,
  37711.     it's entirely possible to write a PHP script that will allow you
  37712.     to read system files such as /etc/passwd, modify your ethernet
  37713.     connections, send massive printer jobs out, etc. This has some
  37714.     obvious implications, in that you need to ensure that the files
  37715.     that you read from and write to are the appropriate ones.
  37716.    </P
  37717. ><P
  37718. >     Consider the following script, where a user indicates that they'd
  37719.     like to delete a file in their home directory. This assumes a
  37720.     situation where a PHP web interface is regularly used for file
  37721.     management, so the Apache user is allowed to delete files in
  37722.     the user home directories.
  37723.    </P
  37724. ><P
  37725. >     <TABLE
  37726. WIDTH="100%"
  37727. BORDER="0"
  37728. CELLPADDING="0"
  37729. CELLSPACING="0"
  37730. CLASS="EXAMPLE"
  37731. ><TR
  37732. ><TD
  37733. ><DIV
  37734. CLASS="example"
  37735. ><A
  37736. NAME="AEN6161"
  37737. ></A
  37738. ><P
  37739. ><B
  37740. >Example 26-1. Poor variable checking leads to....</B
  37741. ></P
  37742. ><TABLE
  37743. BORDER="0"
  37744. BGCOLOR="#E0E0E0"
  37745. CELLPADDING="5"
  37746. ><TR
  37747. ><TD
  37748. ><PRE
  37749. CLASS="php"
  37750. ><?php
  37751. // remove a file from the user's home directory
  37752. $username = $_POST['user_submitted_name'];
  37753. $homedir = "/home/$username";
  37754. $file_to_delete = "$userfile";
  37755. unlink ("$homedir/$userfile");
  37756. echo "$file_to_delete has been deleted!";
  37757. ?></PRE
  37758. ></TD
  37759. ></TR
  37760. ></TABLE
  37761. ></DIV
  37762. ></TD
  37763. ></TR
  37764. ></TABLE
  37765. >
  37766.    Since the username is postable from a user form, they can submit
  37767.    a username and file belonging to someone else, and delete files.
  37768.    In this case, you'd want to use some other form of authentication.
  37769.    Consider what could happen if the variables submitted were
  37770.    "../etc/" and "passwd". The code would then effectively read:
  37771.     <TABLE
  37772. WIDTH="100%"
  37773. BORDER="0"
  37774. CELLPADDING="0"
  37775. CELLSPACING="0"
  37776. CLASS="EXAMPLE"
  37777. ><TR
  37778. ><TD
  37779. ><DIV
  37780. CLASS="example"
  37781. ><A
  37782. NAME="AEN6164"
  37783. ></A
  37784. ><P
  37785. ><B
  37786. >Example 26-2. ... A filesystem attack</B
  37787. ></P
  37788. ><TABLE
  37789. BORDER="0"
  37790. BGCOLOR="#E0E0E0"
  37791. CELLPADDING="5"
  37792. ><TR
  37793. ><TD
  37794. ><PRE
  37795. CLASS="php"
  37796. ><?php
  37797. // removes a file from anywhere on the hard drive that
  37798. // the PHP user has access to. If PHP has root access:
  37799. $username = "../etc/";
  37800. $homedir = "/home/../etc/";
  37801. $file_to_delete = "passwd";
  37802. unlink ("/home/../etc/passwd");
  37803. echo "/home/../etc/passwd has been deleted!";
  37804. ?></PRE
  37805. ></TD
  37806. ></TR
  37807. ></TABLE
  37808. ></DIV
  37809. ></TD
  37810. ></TR
  37811. ></TABLE
  37812. >
  37813.     There are two important measures you should take to prevent these
  37814.     issues.
  37815.     <P
  37816. ></P
  37817. ><UL
  37818. ><LI
  37819. ><P
  37820. >        Only allow limited permissions to the PHP web user binary.
  37821.       </P
  37822. ></LI
  37823. ><LI
  37824. ><P
  37825. >        Check all variables which are submitted.
  37826.       </P
  37827. ></LI
  37828. ></UL
  37829. >
  37830.     Here is an improved script:
  37831.     <TABLE
  37832. WIDTH="100%"
  37833. BORDER="0"
  37834. CELLPADDING="0"
  37835. CELLSPACING="0"
  37836. CLASS="EXAMPLE"
  37837. ><TR
  37838. ><TD
  37839. ><DIV
  37840. CLASS="example"
  37841. ><A
  37842. NAME="AEN6172"
  37843. ></A
  37844. ><P
  37845. ><B
  37846. >Example 26-3. More secure file name checking</B
  37847. ></P
  37848. ><TABLE
  37849. BORDER="0"
  37850. BGCOLOR="#E0E0E0"
  37851. CELLPADDING="5"
  37852. ><TR
  37853. ><TD
  37854. ><PRE
  37855. CLASS="php"
  37856. ><?php
  37857. // removes a file from the hard drive that
  37858. // the PHP user has access to.
  37859. $username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim
  37860.  
  37861. $homedir = "/home/$username";
  37862.  
  37863. $file_to_delete = basename("$userfile"); // strip paths
  37864. unlink ($homedir/$file_to_delete);
  37865.  
  37866. $fp = fopen("/home/logging/filedelete.log","+a"); //log the deletion
  37867. $logstring = "$username $homedir $file_to_delete";
  37868. fwrite ($fp, $logstring);
  37869. fclose($fp);
  37870.  
  37871. echo "$file_to_delete has been deleted!";
  37872. ?></PRE
  37873. ></TD
  37874. ></TR
  37875. ></TABLE
  37876. ></DIV
  37877. ></TD
  37878. ></TR
  37879. ></TABLE
  37880. >
  37881.     However, even this is not without it's flaws. If your authentication
  37882.     system allowed users to create their own user logins, and a user
  37883.     chose the login "../etc/", the system is once again exposed. For
  37884.     this reason, you may prefer to write a more customized check:
  37885.     <TABLE
  37886. WIDTH="100%"
  37887. BORDER="0"
  37888. CELLPADDING="0"
  37889. CELLSPACING="0"
  37890. CLASS="EXAMPLE"
  37891. ><TR
  37892. ><TD
  37893. ><DIV
  37894. CLASS="example"
  37895. ><A
  37896. NAME="AEN6175"
  37897. ></A
  37898. ><P
  37899. ><B
  37900. >Example 26-4. More secure file name checking</B
  37901. ></P
  37902. ><TABLE
  37903. BORDER="0"
  37904. BGCOLOR="#E0E0E0"
  37905. CELLPADDING="5"
  37906. ><TR
  37907. ><TD
  37908. ><PRE
  37909. CLASS="php"
  37910. ><?php
  37911. $username = $_SERVER['REMOTE_USER']; // using an authentication mechanisim
  37912. $homedir = "/home/$username";
  37913.  
  37914. if (!ereg('^[^./][^/]*$', $userfile))
  37915.      die('bad filename'); //die, do not process
  37916.  
  37917. if (!ereg('^[^./][^/]*$', $username))
  37918.      die('bad username'); //die, do not process
  37919. //etc...
  37920. ?></PRE
  37921. ></TD
  37922. ></TR
  37923. ></TABLE
  37924. ></DIV
  37925. ></TD
  37926. ></TR
  37927. ></TABLE
  37928. >
  37929.    </P
  37930. ><P
  37931. >     Depending on your operating system, there are a wide variety of files
  37932.     which you should be concerned about, including device entries (/dev/
  37933.     or COM1), configuration files (/etc/ files and the .ini files),
  37934.     well known file storage areas (/home/, My Documents), etc. For this
  37935.     reason, it's usually easier to create a policy where you forbid
  37936.     everything except for what you explicitly allow.
  37937.    </P
  37938. ></DIV
  37939. ><DIV
  37940. CLASS="chapter"
  37941. ><HR><H1
  37942. ><A
  37943. NAME="security.database"
  37944. >Chapter 27. Database Security</A
  37945. ></H1
  37946. ><P
  37947. >     Nowadays, databases are cardinal components of any web based application by
  37948.     enabling websites to provide varying dynamic content. Since very sensitive
  37949.     or secret information can be stored in a database, you should strongly
  37950.     consider protecting your databases.
  37951.    </P
  37952. ><P
  37953. >     To retrieve or to store any information you need to connect to the database,
  37954.     send a legitimate query, fetch the result, and close the connection.
  37955.     Nowadays, the commonly used query language in this interaction is the
  37956.     Structured Query Language (SQL). See how an attacker can <A
  37957. HREF="#security.database.sql-injection"
  37958. >tamper with an SQL query</A
  37959. >.
  37960.    </P
  37961. ><P
  37962. >     As you can surmise, PHP cannot protect your database by itself. The
  37963.     following sections aim to be an introduction into the very basics of how to
  37964.     access and manipulate databases within PHP scripts.
  37965.    </P
  37966. ><P
  37967. >     Keep in mind this simple rule: defense in depth. The more places you
  37968.     take action to increase the protection of your database, the less
  37969.     probability of an attacker succeeding in exposing or abusing any stored
  37970.     information. Good design of the database schema and the application
  37971.     deals with your greatest fears.
  37972.    </P
  37973. ><DIV
  37974. CLASS="sect1"
  37975. ><HR><H2
  37976. CLASS="sect1"
  37977. ><A
  37978. NAME="security.database.design"
  37979. >Designing Databases</A
  37980. ></H2
  37981. ><P
  37982. >       The first step is always to create the database, unless you want to use
  37983.       one from a third party. When a database is created, it is
  37984.       assigned to an owner, who executed the creation statement. Usually, only
  37985.       the owner (or a superuser) can do anything with the objects in that
  37986.       database, and in order to allow other users to use it, privileges must be
  37987.       granted.
  37988.      </P
  37989. ><P
  37990. >       Applications should never connect to the database as its owner or a
  37991.       superuser, because these users can execute any query at will, for
  37992.       example, modifying the schema (e.g. dropping tables) or deleting its
  37993.       entire content.
  37994.      </P
  37995. ><P
  37996. >       You may create different database users for every aspect of your
  37997.       application with very limited rights to database objects. The most
  37998.       required privileges should be granted only, and avoid that the same user
  37999.       can interact with the database in different use cases. This means that if
  38000.       intruders gain access to your database using your applications credentials,
  38001.       they can only effect as many changes as your application can.
  38002.      </P
  38003. ><P
  38004. >       You are encouraged not to implement all the business logic in the web
  38005.       application (i.e. your script), instead do it in the database schema
  38006.       using views, triggers or rules. If the system evolves, new ports will be
  38007.       intended to open to the database, and you have to re-implement the logic
  38008.       in each separate database client. Over and above, triggers can be used
  38009.       to transparently and automatically handle fields, which often provides
  38010.       insight when debugging problems with your application or tracing back
  38011.       transactions.
  38012.      </P
  38013. ></DIV
  38014. ><DIV
  38015. CLASS="sect1"
  38016. ><HR><H2
  38017. CLASS="sect1"
  38018. ><A
  38019. NAME="security.database.connection"
  38020. >Connecting to Database</A
  38021. ></H2
  38022. ><P
  38023. >      You may want to establish the connections over SSL to encrypt
  38024.      client/server communications for increased security, or you can use ssh
  38025.      to encrypt the network connection between clients and the database server.
  38026.      If either of these is used, then monitoring your traffic and gaining
  38027.      information about your database will be difficult for a would-be attacker.
  38028.     </P
  38029. ></DIV
  38030. ><DIV
  38031. CLASS="sect1"
  38032. ><HR><H2
  38033. CLASS="sect1"
  38034. ><A
  38035. NAME="security.database.storage"
  38036. >Encrypted Storage Model</A
  38037. ></H2
  38038. ><P
  38039. >      SSL/SSH protects data travelling from the client to the server, SSL/SSH
  38040.      does not protect the persistent data stored in a database. SSL is an
  38041.      on-the-wire protocol.
  38042.     </P
  38043. ><P
  38044. >      Once an attacker gains access to your database directly (bypassing the
  38045.      webserver), the stored sensitive data may be exposed or misused, unless
  38046.      the information is protected by the database itself. Encrypting the data
  38047.      is a good way to mitigate this threat, but very few databases offer this
  38048.      type of data encryption.
  38049.     </P
  38050. ><P
  38051. >      The easiest way to work around this problem is to first create your own
  38052.      encryption package, and then use it from within your PHP scripts. PHP
  38053.      can assist you in this with several extensions, such as <A
  38054. HREF="#ref.mcrypt"
  38055. >Mcrypt</A
  38056. > and <A
  38057. HREF="#ref.mhash"
  38058. >Mhash</A
  38059. >, covering a wide variety of encryption
  38060.      algorithms. The script encrypts the data before inserting it into the database, and decrypts
  38061.      it when retrieving. See the references for further examples of how
  38062.      encryption works.
  38063.     </P
  38064. ><P
  38065. >      In case of truly hidden data, if its raw representation is not needed
  38066.      (i.e. not be displayed), hashing may also be taken into consideration.
  38067.      The well-known example for the hashing is storing the MD5 hash of a
  38068.      password in a database, instead of the password itself. See also
  38069.      <A
  38070. HREF="#function.crypt"
  38071. ><B
  38072. CLASS="function"
  38073. >crypt()</B
  38074. ></A
  38075. > and <A
  38076. HREF="#function.md5"
  38077. ><B
  38078. CLASS="function"
  38079. >md5()</B
  38080. ></A
  38081. >.
  38082.     </P
  38083. ><TABLE
  38084. WIDTH="100%"
  38085. BORDER="0"
  38086. CELLPADDING="0"
  38087. CELLSPACING="0"
  38088. CLASS="EXAMPLE"
  38089. ><TR
  38090. ><TD
  38091. ><DIV
  38092. CLASS="example"
  38093. ><A
  38094. NAME="AEN6205"
  38095. ></A
  38096. ><P
  38097. ><B
  38098. >Example 27-1. Using hashed password field</B
  38099. ></P
  38100. ><TABLE
  38101. BORDER="0"
  38102. BGCOLOR="#E0E0E0"
  38103. CELLPADDING="5"
  38104. ><TR
  38105. ><TD
  38106. ><PRE
  38107. CLASS="php"
  38108. ><?php
  38109.  
  38110. // storing password hash
  38111. $query  = sprintf("INSERT INTO users(name,pwd) VALUES('%s','%s');",
  38112.             addslashes($username), md5($password));
  38113. $result = pg_query($connection, $query);
  38114.  
  38115. // querying if user submitted the right password
  38116. $query = sprintf("SELECT 1 FROM users WHERE name='%s' AND pwd='%s';",
  38117.             addslashes($username), md5($password));
  38118. $result = pg_query($connection, $query);
  38119.  
  38120. if (pg_num_rows($result) > 0) {
  38121.     echo 'Welcome, $username!';
  38122. } else {
  38123.     echo 'Authentication failed for $username.';
  38124. }
  38125.  
  38126. ?></PRE
  38127. ></TD
  38128. ></TR
  38129. ></TABLE
  38130. ></DIV
  38131. ></TD
  38132. ></TR
  38133. ></TABLE
  38134. ></DIV
  38135. ><DIV
  38136. CLASS="sect1"
  38137. ><HR><H2
  38138. CLASS="sect1"
  38139. ><A
  38140. NAME="security.database.sql-injection"
  38141. >SQL Injection</A
  38142. ></H2
  38143. ><P
  38144. >      Many web developers are unaware of how SQL queries can be tampered with,
  38145.      and assume that an SQL query is a trusted command. It means that SQL
  38146.      queries are able to circumvent access controls, thereby bypassing standard
  38147.      authentication and authorization checks, and sometimes SQL queries even
  38148.      may allow access to host operating system level commands.
  38149.     </P
  38150. ><P
  38151. >      Direct SQL Command Injection is a technique where an attacker creates or
  38152.      alters existing SQL commands to expose hidden data, or to override valuable
  38153.      ones, or even to execute dangerous system level commands on the database
  38154.      host. This is accomplished by the application taking user input and
  38155.      combining it with static parameters to build a SQL query. The following
  38156.      examples are based on true stories, unfortunately.
  38157.     </P
  38158. ><P
  38159. >      Owing to the lack of input validation and connecting to the database on
  38160.      behalf of a superuser or the one who can create users, the attacker
  38161.      may create a superuser in your database.
  38162.      <TABLE
  38163. WIDTH="100%"
  38164. BORDER="0"
  38165. CELLPADDING="0"
  38166. CELLSPACING="0"
  38167. CLASS="EXAMPLE"
  38168. ><TR
  38169. ><TD
  38170. ><DIV
  38171. CLASS="example"
  38172. ><A
  38173. NAME="AEN6213"
  38174. ></A
  38175. ><P
  38176. ><B
  38177. >Example 27-2. 
  38178.        Splitting the result set into pages ... and making superusers
  38179.        (PostgreSQL and MySQL)
  38180.       </B
  38181. ></P
  38182. ><TABLE
  38183. BORDER="0"
  38184. BGCOLOR="#E0E0E0"
  38185. CELLPADDING="5"
  38186. ><TR
  38187. ><TD
  38188. ><PRE
  38189. CLASS="php"
  38190. ><?php
  38191.  
  38192. $offset = $argv[0]; // beware, no input validation!
  38193. $query  = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;";
  38194. // with PostgreSQL 
  38195. $result = pg_query($conn, $query);
  38196. // with MySQL
  38197. $result = mysql_query($query);
  38198.  
  38199. ?></PRE
  38200. ></TD
  38201. ></TR
  38202. ></TABLE
  38203. ></DIV
  38204. ></TD
  38205. ></TR
  38206. ></TABLE
  38207. >
  38208.       Normal users click on the 'next', 'prev' links where the <VAR
  38209. CLASS="varname"
  38210. >$offset</VAR
  38211. >
  38212.       is encoded into the URL. The script expects that the incoming
  38213.       <VAR
  38214. CLASS="varname"
  38215. >$offset</VAR
  38216. > is a decimal number. However, what if someone tries to
  38217.       break in by appending a <A
  38218. HREF="#function.urlencode"
  38219. ><B
  38220. CLASS="function"
  38221. >urlencode()</B
  38222. ></A
  38223. >'d form of the
  38224.       following to the URL
  38225.       <DIV
  38226. CLASS="informalexample"
  38227. ><P
  38228. ></P
  38229. ><A
  38230. NAME="AEN6219"
  38231. ></A
  38232. ><TABLE
  38233. BORDER="0"
  38234. BGCOLOR="#E0E0E0"
  38235. CELLPADDING="5"
  38236. ><TR
  38237. ><TD
  38238. ><PRE
  38239. CLASS="sql"
  38240. >// in case of PostgreSQL
  38241. 0;
  38242. insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd)
  38243.     select 'crack', usesysid, 't','t','crack'
  38244.     from pg_shadow where usename='postgres';
  38245. --
  38246.  
  38247. // in case of MySQL
  38248. 0;
  38249. UPDATE user SET Password=PASSWORD('crack') WHERE user='root';
  38250. FLUSH PRIVILEGES;</PRE
  38251. ></TD
  38252. ></TR
  38253. ></TABLE
  38254. ><P
  38255. ></P
  38256. ></DIV
  38257. >
  38258.       If it happened, then the script would present a superuser access to him.
  38259.       Note that <VAR
  38260. CLASS="literal"
  38261. >0;</VAR
  38262. > is to supply a valid offset to the
  38263.       original query and to terminate it.
  38264.     </P
  38265. ><DIV
  38266. CLASS="note"
  38267. ><BLOCKQUOTE
  38268. CLASS="note"
  38269. ><P
  38270. ><B
  38271. >Note: </B
  38272. >
  38273.       It is common technique to force the SQL parser to ignore the rest of the
  38274.       query written by the developer with <VAR
  38275. CLASS="literal"
  38276. >--</VAR
  38277. > which is the
  38278.       comment sign in SQL.
  38279.      </P
  38280. ></BLOCKQUOTE
  38281. ></DIV
  38282. ><P
  38283. >      A feasible way to gain passwords is to circumvent your search result pages.
  38284.      The only thing the attacker needs to do is to see if there are any submitted variables
  38285.      used in SQL statements which are not handled properly. These filters can be set
  38286.      commonly in a preceding form to customize <VAR
  38287. CLASS="literal"
  38288. >WHERE, ORDER BY,
  38289.      LIMIT</VAR
  38290. > and <VAR
  38291. CLASS="literal"
  38292. >OFFSET</VAR
  38293. > clauses in <VAR
  38294. CLASS="literal"
  38295. >SELECT</VAR
  38296. >
  38297.      statements. If your database supports the <VAR
  38298. CLASS="literal"
  38299. >UNION</VAR
  38300. > construct,
  38301.      the attacker may try to append an entire query to the original one to list
  38302.      passwords from an arbitrary table. Using encrypted password fields is 
  38303.      strongly encouraged.
  38304.      <TABLE
  38305. WIDTH="100%"
  38306. BORDER="0"
  38307. CELLPADDING="0"
  38308. CELLSPACING="0"
  38309. CLASS="EXAMPLE"
  38310. ><TR
  38311. ><TD
  38312. ><DIV
  38313. CLASS="example"
  38314. ><A
  38315. NAME="AEN6230"
  38316. ></A
  38317. ><P
  38318. ><B
  38319. >Example 27-3. 
  38320.        Listing out articles ... and some passwords (any database server)
  38321.       </B
  38322. ></P
  38323. ><TABLE
  38324. BORDER="0"
  38325. BGCOLOR="#E0E0E0"
  38326. CELLPADDING="5"
  38327. ><TR
  38328. ><TD
  38329. ><PRE
  38330. CLASS="php"
  38331. ><?php
  38332.  
  38333. $query  = "SELECT id, name, inserted, size FROM products
  38334.                   WHERE size = '$size'
  38335.                   ORDER BY $order LIMIT $limit, $offset;";
  38336. $result = odbc_exec($conn, $query);
  38337.  
  38338. ?></PRE
  38339. ></TD
  38340. ></TR
  38341. ></TABLE
  38342. ></DIV
  38343. ></TD
  38344. ></TR
  38345. ></TABLE
  38346. >
  38347.      The static part of the query can be combined with another
  38348.      <VAR
  38349. CLASS="literal"
  38350. >SELECT</VAR
  38351. > statement which reveals all passwords:
  38352.      <DIV
  38353. CLASS="informalexample"
  38354. ><P
  38355. ></P
  38356. ><A
  38357. NAME="AEN6234"
  38358. ></A
  38359. ><TABLE
  38360. BORDER="0"
  38361. BGCOLOR="#E0E0E0"
  38362. CELLPADDING="5"
  38363. ><TR
  38364. ><TD
  38365. ><PRE
  38366. CLASS="sql"
  38367. >'
  38368. union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable;
  38369. --</PRE
  38370. ></TD
  38371. ></TR
  38372. ></TABLE
  38373. ><P
  38374. ></P
  38375. ></DIV
  38376. >
  38377.      If this query (playing with the <VAR
  38378. CLASS="literal"
  38379. >'</VAR
  38380. > and
  38381.      <VAR
  38382. CLASS="literal"
  38383. >--</VAR
  38384. >) were assigned to one of the variables used in
  38385.      <VAR
  38386. CLASS="varname"
  38387. >$query</VAR
  38388. >, the query beast awakened.
  38389.     </P
  38390. ><P
  38391. >      SQL UPDATE's are also susceptible to attack. These queries are
  38392.      also threatened by chopping and appending an entirely new query to it. But
  38393.      the attacker might fiddle with the <VAR
  38394. CLASS="literal"
  38395. >SET</VAR
  38396. > clause. In this
  38397.      case some schema information must be possessed to manipulate the query
  38398.      successfully. This can be acquired by examining the form variable names, or
  38399.      just simply brute forcing. There are not so many naming conventions for
  38400.      fields storing passwords or usernames.
  38401.      <TABLE
  38402. WIDTH="100%"
  38403. BORDER="0"
  38404. CELLPADDING="0"
  38405. CELLSPACING="0"
  38406. CLASS="EXAMPLE"
  38407. ><TR
  38408. ><TD
  38409. ><DIV
  38410. CLASS="example"
  38411. ><A
  38412. NAME="AEN6241"
  38413. ></A
  38414. ><P
  38415. ><B
  38416. >Example 27-4. 
  38417.       From resetting a password ... to gaining more privileges (any database server)
  38418.      </B
  38419. ></P
  38420. ><TABLE
  38421. BORDER="0"
  38422. BGCOLOR="#E0E0E0"
  38423. CELLPADDING="5"
  38424. ><TR
  38425. ><TD
  38426. ><PRE
  38427. CLASS="php"
  38428. ><?php
  38429. $query = "UPDATE usertable SET pwd='$pwd' WHERE uid='$uid';";
  38430. ?></PRE
  38431. ></TD
  38432. ></TR
  38433. ></TABLE
  38434. ></DIV
  38435. ></TD
  38436. ></TR
  38437. ></TABLE
  38438. >
  38439.      But a malicious user sumbits the value
  38440.      <VAR
  38441. CLASS="literal"
  38442. >' or uid like'%admin%'; --</VAR
  38443. > to <VAR
  38444. CLASS="varname"
  38445. >$uid</VAR
  38446. > to
  38447.      change the admin's password, or simply sets <VAR
  38448. CLASS="varname"
  38449. >$pwd</VAR
  38450. > to
  38451.      <VAR
  38452. CLASS="literal"
  38453. >"hehehe', admin='yes', trusted=100 "</VAR
  38454. > (with a trailing
  38455.      space) to gain more privileges. Then, the query will be twisted:
  38456.      <DIV
  38457. CLASS="informalexample"
  38458. ><P
  38459. ></P
  38460. ><A
  38461. NAME="AEN6248"
  38462. ></A
  38463. ><TABLE
  38464. BORDER="0"
  38465. BGCOLOR="#E0E0E0"
  38466. CELLPADDING="5"
  38467. ><TR
  38468. ><TD
  38469. ><PRE
  38470. CLASS="php"
  38471. ><?php
  38472.  
  38473. // $uid == ' or uid like'%admin%'; --
  38474. $query = "UPDATE usertable SET pwd='...' WHERE uid='' or uid like '%admin%'; --";
  38475.  
  38476. // $pwd == "hehehe', admin='yes', trusted=100 "
  38477. $query = "UPDATE usertable SET pwd='hehehe', admin='yes', trusted=100 WHERE
  38478. ...;";
  38479.  
  38480. ?></PRE
  38481. ></TD
  38482. ></TR
  38483. ></TABLE
  38484. ><P
  38485. ></P
  38486. ></DIV
  38487. >
  38488.     </P
  38489. ><P
  38490. >      A frightening example how operating system level commands can be accessed
  38491.      on some database hosts.
  38492.      <TABLE
  38493. WIDTH="100%"
  38494. BORDER="0"
  38495. CELLPADDING="0"
  38496. CELLSPACING="0"
  38497. CLASS="EXAMPLE"
  38498. ><TR
  38499. ><TD
  38500. ><DIV
  38501. CLASS="example"
  38502. ><A
  38503. NAME="AEN6251"
  38504. ></A
  38505. ><P
  38506. ><B
  38507. >Example 27-5. Attacking the database hosts operating system (MSSQL Server)</B
  38508. ></P
  38509. ><TABLE
  38510. BORDER="0"
  38511. BGCOLOR="#E0E0E0"
  38512. CELLPADDING="5"
  38513. ><TR
  38514. ><TD
  38515. ><PRE
  38516. CLASS="php"
  38517. ><?php
  38518.  
  38519. $query  = "SELECT * FROM products WHERE id LIKE '%$prod%'";
  38520. $result = mssql_query($query);
  38521.  
  38522. ?></PRE
  38523. ></TD
  38524. ></TR
  38525. ></TABLE
  38526. ></DIV
  38527. ></TD
  38528. ></TR
  38529. ></TABLE
  38530. >
  38531.      If attacker submits the value
  38532.      <VAR
  38533. CLASS="literal"
  38534. >a%' exec master..xp_cmdshell 'net user test testpass /ADD' --</VAR
  38535. >
  38536.      to <VAR
  38537. CLASS="varname"
  38538. >$prod</VAR
  38539. >, then the <VAR
  38540. CLASS="varname"
  38541. >$query</VAR
  38542. > will be:
  38543.      <DIV
  38544. CLASS="informalexample"
  38545. ><P
  38546. ></P
  38547. ><A
  38548. NAME="AEN6257"
  38549. ></A
  38550. ><TABLE
  38551. BORDER="0"
  38552. BGCOLOR="#E0E0E0"
  38553. CELLPADDING="5"
  38554. ><TR
  38555. ><TD
  38556. ><PRE
  38557. CLASS="php"
  38558. ><?php
  38559.  
  38560. $query  = "SELECT * FROM products
  38561.                     WHERE id LIKE '%a%'
  38562.                     exec master..xp_cmdshell 'net user test testpass /ADD'--";
  38563. $result = mssql_query($query);
  38564.  
  38565. ?></PRE
  38566. ></TD
  38567. ></TR
  38568. ></TABLE
  38569. ><P
  38570. ></P
  38571. ></DIV
  38572. >
  38573.      MSSQL Server executes the SQL statements in the batch including a command
  38574.      to add a new user to the local accounts database. If this application
  38575.      were running as <VAR
  38576. CLASS="literal"
  38577. >sa</VAR
  38578. > and the MSSQLSERVER service is
  38579.      running with sufficient privileges, the attacker would now have an
  38580.      account with which to access this machine.
  38581.     </P
  38582. ><DIV
  38583. CLASS="note"
  38584. ><BLOCKQUOTE
  38585. CLASS="note"
  38586. ><P
  38587. ><B
  38588. >Note: </B
  38589. >
  38590.       Some of the examples above is tied to a specific database server. This
  38591.       does not mean that a similar attack is impossible against other products.
  38592.       Your database server may be similarly vulnerable in another manner.
  38593.      </P
  38594. ></BLOCKQUOTE
  38595. ></DIV
  38596. ><DIV
  38597. CLASS="sect2"
  38598. ><HR><H3
  38599. CLASS="sect2"
  38600. ><A
  38601. NAME="security.database.avoiding"
  38602. >Avoiding techniques</A
  38603. ></H3
  38604. ><P
  38605. >       You may plead that the attacker must possess a piece of information
  38606.       about the database schema in most examples. You are right, but you
  38607.       never know when and how it can be taken out, and if it happens,
  38608.       your database may be exposed. If you are using an open source, or
  38609.       publicly available database handling package, which may belong to a
  38610.       content management system or forum, the intruders easily produce
  38611.       a copy of a piece of your code. It may be also a security risk if it
  38612.       is a poorly designed one.
  38613.      </P
  38614. ><P
  38615. >       These attacks are mainly based on exploiting the code not being written
  38616.       with security in mind. Never trust any kind of input, especially that
  38617.       which comes from the client side, even though it comes from a select box,
  38618.       a hidden input field or a cookie. The first example shows that such a
  38619.       blameless query can cause disasters.
  38620.      </P
  38621. ><P
  38622. ></P
  38623. ><UL
  38624. ><LI
  38625. ><P
  38626. >         Never connect to the database as a superuser or as the database owner.
  38627.         Use always customized users with very limited privileges.
  38628.        </P
  38629. ></LI
  38630. ><LI
  38631. ><P
  38632. >         Check if the given input has the expected data type. PHP has
  38633.         a wide range of input validating functions, from the simplest ones
  38634.         found in <A
  38635. HREF="#ref.var"
  38636. >Variable Functions</A
  38637. > and
  38638.         in <A
  38639. HREF="#ref.ctype"
  38640. >Character Type Functions</A
  38641. >
  38642.         (e.g. <A
  38643. HREF="#function.is-numeric"
  38644. ><B
  38645. CLASS="function"
  38646. >is_numeric()</B
  38647. ></A
  38648. >, <A
  38649. HREF="#function.ctype-digit"
  38650. ><B
  38651. CLASS="function"
  38652. >ctype_digit()</B
  38653. ></A
  38654. >
  38655.         respectively) and onwards to the
  38656.         <A
  38657. HREF="#ref.pcre"
  38658. >Perl compatible Regular Expressions</A
  38659. >
  38660.         support.
  38661.        </P
  38662. ></LI
  38663. ><LI
  38664. ><P
  38665. >         If the application waits for numerical input, consider verifying data
  38666.         with <A
  38667. HREF="#function.is-numeric"
  38668. ><B
  38669. CLASS="function"
  38670. >is_numeric()</B
  38671. ></A
  38672. >, or silently change its type
  38673.         using <A
  38674. HREF="#function.settype"
  38675. ><B
  38676. CLASS="function"
  38677. >settype()</B
  38678. ></A
  38679. >, or use its numeric representation
  38680.         by <A
  38681. HREF="#function.sprintf"
  38682. ><B
  38683. CLASS="function"
  38684. >sprintf()</B
  38685. ></A
  38686. >.
  38687.         <TABLE
  38688. WIDTH="100%"
  38689. BORDER="0"
  38690. CELLPADDING="0"
  38691. CELLSPACING="0"
  38692. CLASS="EXAMPLE"
  38693. ><TR
  38694. ><TD
  38695. ><DIV
  38696. CLASS="example"
  38697. ><A
  38698. NAME="AEN6281"
  38699. ></A
  38700. ><P
  38701. ><B
  38702. >Example 27-6. A more secure way to compose a query for paging</B
  38703. ></P
  38704. ><TABLE
  38705. BORDER="0"
  38706. BGCOLOR="#E0E0E0"
  38707. CELLPADDING="5"
  38708. ><TR
  38709. ><TD
  38710. ><PRE
  38711. CLASS="php"
  38712. ><?php
  38713.  
  38714. settype($offset, 'integer');
  38715. $query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;";
  38716.  
  38717. // please note %d in the format string, using %s would be meaningless
  38718. $query = sprintf("SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;",
  38719.                  $offset);
  38720.  
  38721. ?></PRE
  38722. ></TD
  38723. ></TR
  38724. ></TABLE
  38725. ></DIV
  38726. ></TD
  38727. ></TR
  38728. ></TABLE
  38729. >
  38730.        </P
  38731. ></LI
  38732. ><LI
  38733. ><P
  38734. >         Quote each non numeric user supplied value that is passed to the
  38735.         database with the database-specific string escape function (e.g.
  38736.         <A
  38737. HREF="#function.mysql-escape-string"
  38738. ><B
  38739. CLASS="function"
  38740. >mysql_escape_string()</B
  38741. ></A
  38742. >,
  38743.         <B
  38744. CLASS="function"
  38745. >sql_escape_string()</B
  38746. >, etc.). If a database-specific
  38747.         string escape mechanism is not available, the
  38748.         <A
  38749. HREF="#function.addslashes"
  38750. ><B
  38751. CLASS="function"
  38752. >addslashes()</B
  38753. ></A
  38754. > and <A
  38755. HREF="#function.str-replace"
  38756. ><B
  38757. CLASS="function"
  38758. >str_replace()</B
  38759. ></A
  38760. >
  38761.         functions may be useful (depending on database type).
  38762.         See <A
  38763. HREF="#security.database.storage"
  38764. >the first example</A
  38765. >.
  38766.         As the example shows, adding quotes to the static part of the query
  38767.         is not enough, making this query easily crackable.
  38768.        </P
  38769. ></LI
  38770. ><LI
  38771. ><P
  38772. >         Do not print out any database specific information, especially
  38773.         about the schema, by fair means or foul. See also <A
  38774. HREF="#security.errors"
  38775. >Error Reporting</A
  38776. > and <A
  38777. HREF="#ref.errorfunc"
  38778. >Error Handling and Logging Functions</A
  38779. >.
  38780.        </P
  38781. ></LI
  38782. ><LI
  38783. ><P
  38784. >         You may use stored procedures and previously defined cursors to abstract
  38785.         data access so that users do not directly access tables or views, but
  38786.         this solution has another impacts.
  38787.        </P
  38788. ></LI
  38789. ></UL
  38790. ><P
  38791. >       Besides these, you benefit from logging queries either within your script
  38792.       or by the database itself, if it supports logging. Obviously, the logging is unable
  38793.       to prevent any harmful attempt, but it can be helpful to trace back which
  38794.       application has been circumvented. The log is not useful by itself, but
  38795.       through the information it contains. More detail is generally better than less.
  38796.      </P
  38797. ></DIV
  38798. ></DIV
  38799. ></DIV
  38800. ><DIV
  38801. CLASS="chapter"
  38802. ><HR><H1
  38803. ><A
  38804. NAME="security.errors"
  38805. >Chapter 28. Error Reporting</A
  38806. ></H1
  38807. ><P
  38808. >     With PHP security, there are two sides to error reporting. One is
  38809.     beneficial to increasing security, the other is detrimental.
  38810.    </P
  38811. ><P
  38812. >     A standard attack tactic involves profiling a system by feeding
  38813.     it improper data, and checking for the kinds, and contexts, of the
  38814.     errors which are returned. This allows the system cracker to probe
  38815.     for information about the server, to determine possible weaknesses.
  38816.     For example, if an attacker had gleaned information about a page
  38817.     based on a prior form submission, they may attempt to override
  38818.     variables, or modify them:
  38819.     <TABLE
  38820. WIDTH="100%"
  38821. BORDER="0"
  38822. CELLPADDING="0"
  38823. CELLSPACING="0"
  38824. CLASS="EXAMPLE"
  38825. ><TR
  38826. ><TD
  38827. ><DIV
  38828. CLASS="example"
  38829. ><A
  38830. NAME="AEN6302"
  38831. ></A
  38832. ><P
  38833. ><B
  38834. >Example 28-1. Attacking Variables with a custom HTML page</B
  38835. ></P
  38836. ><TABLE
  38837. BORDER="0"
  38838. BGCOLOR="#E0E0E0"
  38839. CELLPADDING="5"
  38840. ><TR
  38841. ><TD
  38842. ><PRE
  38843. CLASS="html"
  38844. ><form method="post" action="attacktarget?username=badfoo&amp;password=badfoo">
  38845. <input type="hidden" name="username" value="badfoo" />
  38846. <input type="hidden" name="password" value="badfoo" />
  38847. </form></PRE
  38848. ></TD
  38849. ></TR
  38850. ></TABLE
  38851. ></DIV
  38852. ></TD
  38853. ></TR
  38854. ></TABLE
  38855. >
  38856.    </P
  38857. ><P
  38858. >     The PHP errors which are normally returned can be quite helpful to a
  38859.     developer who is trying to debug a script, indicating such things
  38860.     as the function or file that failed, the PHP file it failed in,
  38861.     and the line number which the failure occurred in. This is all
  38862.     information that can be exploited.  It is not uncommon for a php
  38863.     developer to use <A
  38864. HREF="#function.show-source"
  38865. ><B
  38866. CLASS="function"
  38867. >show_source()</B
  38868. ></A
  38869. >,
  38870.     <A
  38871. HREF="#function.highlight-string"
  38872. ><B
  38873. CLASS="function"
  38874. >highlight_string()</B
  38875. ></A
  38876. >, or
  38877.     <A
  38878. HREF="#function.highlight-file"
  38879. ><B
  38880. CLASS="function"
  38881. >highlight_file()</B
  38882. ></A
  38883. > as a debugging measure, but in
  38884.     a live site, this can expose hidden variables, unchecked syntax,
  38885.     and other dangerous information. Especially dangerous is running
  38886.     code from known sources with built-in debugging handlers, or using
  38887.     common debugging techniques. If the attacker can determine what
  38888.     general technique you are using, they may try to brute-force a page,
  38889.     by sending various common debugging strings:
  38890.     <TABLE
  38891. WIDTH="100%"
  38892. BORDER="0"
  38893. CELLPADDING="0"
  38894. CELLSPACING="0"
  38895. CLASS="EXAMPLE"
  38896. ><TR
  38897. ><TD
  38898. ><DIV
  38899. CLASS="example"
  38900. ><A
  38901. NAME="AEN6309"
  38902. ></A
  38903. ><P
  38904. ><B
  38905. >Example 28-2. Exploiting common debugging variables</B
  38906. ></P
  38907. ><TABLE
  38908. BORDER="0"
  38909. BGCOLOR="#E0E0E0"
  38910. CELLPADDING="5"
  38911. ><TR
  38912. ><TD
  38913. ><PRE
  38914. CLASS="html"
  38915. ><form method="post" action="attacktarget?errors=Y&amp;showerrors=1&amp;debug=1">
  38916. <input type="hidden" name="errors" value="Y" />
  38917. <input type="hidden" name="showerrors" value="1" />
  38918. <input type="hidden" name="debug" value="1" />
  38919. </form></PRE
  38920. ></TD
  38921. ></TR
  38922. ></TABLE
  38923. ></DIV
  38924. ></TD
  38925. ></TR
  38926. ></TABLE
  38927. >
  38928.    </P
  38929. ><P
  38930. >     Regardless of the method of error handling, the ability to probe a
  38931.     system for errors leads to providing an attacker with more
  38932.     information.
  38933.    </P
  38934. ><P
  38935. >     For example, the very style of a generic PHP error indicates a system
  38936.     is running PHP. If the attacker was looking at an .html page, and
  38937.     wanted to probe for the back-end (to look for known weaknesses in
  38938.     the system), by feeding it the wrong data they may be able to
  38939.     determine that a system was built with PHP.
  38940.    </P
  38941. ><P
  38942. >     A function error can indicate whether a system may be running a
  38943.     specific database engine, or give clues as to how a web page or
  38944.     programmed or designed. This allows for deeper investigation into
  38945.     open database ports, or to look for specific bugs or weaknesses
  38946.     in a web page. By feeding different pieces of bad data, for example,
  38947.     an attacker can determine the order of authentication in a script,
  38948.     (from the line number errors) as well as probe for exploits that
  38949.     may be exploited in different locations in the script.
  38950.    </P
  38951. ><P
  38952. >     A filesystem or general PHP error can indicate what permissions
  38953.     the webserver has, as well as the structure and organization of
  38954.     files on the web server. Developer written error code can aggravate
  38955.     this problem, leading to easy exploitation of formerly "hidden"
  38956.     information.
  38957.    </P
  38958. ><P
  38959. >     There are three major solutions to this issue. The first is to
  38960.     scrutinize all functions, and attempt to compensate for the bulk
  38961.     of the errors. The second is to disable error reporting entirely
  38962.     on the running code. The third is to use PHP's custom error
  38963.     handling functions to create your own error handler. Depending
  38964.     on your security policy, you may find all three to be applicable
  38965.     to your situation.
  38966.    </P
  38967. ><P
  38968. >     One way of catching this issue ahead of time is to make use of
  38969.     PHP's own <A
  38970. HREF="#function.error-reporting"
  38971. ><B
  38972. CLASS="function"
  38973. >error_reporting()</B
  38974. ></A
  38975. >, to help you
  38976.     secure your code and find variable usage that may be dangerous.
  38977.     By testing your code, prior to deployment, with E_ALL, you can
  38978.     quickly find areas where your variables may be open to poisoning
  38979.     or modification in other ways. Once you are ready for deployment,
  38980.     by using E_NONE, you insulate your code from probing.
  38981.     <TABLE
  38982. WIDTH="100%"
  38983. BORDER="0"
  38984. CELLPADDING="0"
  38985. CELLSPACING="0"
  38986. CLASS="EXAMPLE"
  38987. ><TR
  38988. ><TD
  38989. ><DIV
  38990. CLASS="example"
  38991. ><A
  38992. NAME="AEN6319"
  38993. ></A
  38994. ><P
  38995. ><B
  38996. >Example 28-3. Finding dangerous variables with E_ALL</B
  38997. ></P
  38998. ><TABLE
  38999. BORDER="0"
  39000. BGCOLOR="#E0E0E0"
  39001. CELLPADDING="5"
  39002. ><TR
  39003. ><TD
  39004. ><PRE
  39005. CLASS="php"
  39006. ><?php
  39007. if ($username) {  // Not initialized or checked before usage
  39008.     $good_login = 1;
  39009. }
  39010. if ($good_login == 1) { // If above test fails, not initialized or checked before usage
  39011.     readfile ("/highly/sensitive/data/index.html");
  39012. }
  39013. ?></PRE
  39014. ></TD
  39015. ></TR
  39016. ></TABLE
  39017. ></DIV
  39018. ></TD
  39019. ></TR
  39020. ></TABLE
  39021. >
  39022.    </P
  39023. ></DIV
  39024. ><DIV
  39025. CLASS="chapter"
  39026. ><HR><H1
  39027. ><A
  39028. NAME="security.globals"
  39029. >Chapter 29. Using Register Globals</A
  39030. ></H1
  39031. ><P
  39032. >     Perhaps the most controversial change in PHP is when the default value
  39033.     for the PHP directive <A
  39034. HREF="#ini.register-globals"
  39035. >     register_globals</A
  39036. > went from ON to OFF in PHP 
  39037.     <A
  39038. HREF="http://www.php.net/release_4_2_0.php"
  39039. TARGET="_top"
  39040. >4.2.0</A
  39041. >.  Reliance on this
  39042.     directive was quite common and many people didn't even know it existed
  39043.     and assumed it's just how PHP works.  This page will explain how one can
  39044.     write insecure code with this directive but keep in mind that the
  39045.     directive itself isn't insecure but rather it's the misuse of it.
  39046.    </P
  39047. ><P
  39048. >     When on, register_globals will inject (poison) your scripts will all
  39049.     sorts of variables, like request variables from HTML forms.  This
  39050.     coupled with the fact that PHP doesn't require variable initialization
  39051.     means writing insecure code is that much easier.  It was a difficult
  39052.     decision, but the PHP community decided to disable this directive by 
  39053.     default.  When on, people use variables yet really don't know for sure
  39054.     where they come from and can only assume.  Internal variables that are
  39055.     defined in the script itself get mixed up with request data sent by
  39056.     users and disabling register_globals changes this.  Let's demonstrate
  39057.     with an example misuse of register_globals: 
  39058.    </P
  39059. ><P
  39060. >     <TABLE
  39061. WIDTH="100%"
  39062. BORDER="0"
  39063. CELLPADDING="0"
  39064. CELLSPACING="0"
  39065. CLASS="EXAMPLE"
  39066. ><TR
  39067. ><TD
  39068. ><DIV
  39069. CLASS="example"
  39070. ><A
  39071. NAME="AEN6329"
  39072. ></A
  39073. ><P
  39074. ><B
  39075. >Example 29-1. Example misuse with register_globals = on</B
  39076. ></P
  39077. ><TABLE
  39078. BORDER="0"
  39079. BGCOLOR="#E0E0E0"
  39080. CELLPADDING="5"
  39081. ><TR
  39082. ><TD
  39083. ><PRE
  39084. CLASS="php"
  39085. ><?php
  39086. // define $authorized = true only if user is authenticated
  39087. if (authenticated_user()) {
  39088.     $authorized = true;
  39089. }
  39090.  
  39091. // Because we didn't first initialize $authorized as false, this might be
  39092. // defined through register_globals, like from GET auth.php?authorized=1
  39093. // So, anyone can be seen as authenticated!
  39094. if ($authorized) {
  39095.     include "/highly/sensitive/data.php";
  39096. }
  39097. ?></PRE
  39098. ></TD
  39099. ></TR
  39100. ></TABLE
  39101. ></DIV
  39102. ></TD
  39103. ></TR
  39104. ></TABLE
  39105. >
  39106.    </P
  39107. ><P
  39108. >     When register_globals = on, our logic above may be compromised.  When
  39109.     off, <VAR
  39110. CLASS="varname"
  39111. >$authorized</VAR
  39112. > can't be set via request so it'll
  39113.     be fine, although it really is generally a good programming practice to
  39114.     initialize variables first.  For example, in our example above we might
  39115.     have first done <VAR
  39116. CLASS="literal"
  39117. >$authorized = false</VAR
  39118. >.  Doing this
  39119.     first means our above code would work with register_globals on or off as
  39120.     users by default would be unauthorized.
  39121.    </P
  39122. ><P
  39123. >     Another example is that of <A
  39124. HREF="#ref.session"
  39125. >sessions</A
  39126. >.
  39127.     When register_globals = on, we could also use
  39128.     <VAR
  39129. CLASS="varname"
  39130. >$username</VAR
  39131. > in our example below but again you must
  39132.     realize that <VAR
  39133. CLASS="varname"
  39134. >$username</VAR
  39135. > could also come from other
  39136.     means, such as GET (through the URL).
  39137.    </P
  39138. ><P
  39139. >     <TABLE
  39140. WIDTH="100%"
  39141. BORDER="0"
  39142. CELLPADDING="0"
  39143. CELLSPACING="0"
  39144. CLASS="EXAMPLE"
  39145. ><TR
  39146. ><TD
  39147. ><DIV
  39148. CLASS="example"
  39149. ><A
  39150. NAME="AEN6340"
  39151. ></A
  39152. ><P
  39153. ><B
  39154. >Example 29-2. Example use of sessions with register_globals on or off</B
  39155. ></P
  39156. ><TABLE
  39157. BORDER="0"
  39158. BGCOLOR="#E0E0E0"
  39159. CELLPADDING="5"
  39160. ><TR
  39161. ><TD
  39162. ><PRE
  39163. CLASS="php"
  39164. ><?php
  39165. // We wouldn't know where $username came from but do know $_SESSION is
  39166. // for session data
  39167. if (isset($_SESSION['username'])) {
  39168.  
  39169.     echo "Hello <b>{$_SESSION['username']}</b>";
  39170.  
  39171. } else {
  39172.  
  39173.     echo "Hello <b>Guest</b><br />";
  39174.     echo "Would you like to login?";
  39175.  
  39176. }
  39177. ?></PRE
  39178. ></TD
  39179. ></TR
  39180. ></TABLE
  39181. ></DIV
  39182. ></TD
  39183. ></TR
  39184. ></TABLE
  39185. >
  39186.    </P
  39187. ><P
  39188. >     It's even possible to take preventative measures to warn when forging is
  39189.     being attempted. If you know ahead of time exactly where a variable
  39190.     should be coming from, you can check to see if the submitted data is
  39191.     coming from an inappropriate kind of submission.  While it doesn't
  39192.     guarantee that data has not been forged, it does require an attacker to
  39193.     guess the right kind of forging.  If you don't care where the request
  39194.     data comes from, you can use <VAR
  39195. CLASS="varname"
  39196. >$_REQUEST</VAR
  39197. > as it contains
  39198.     a mix of GET, POST and COOKIE data.  See also the manual section on
  39199.     using <A
  39200. HREF="#language.variables.external"
  39201. >variables from outside
  39202.     of PHP</A
  39203. >.
  39204.    </P
  39205. ><P
  39206. >     <TABLE
  39207. WIDTH="100%"
  39208. BORDER="0"
  39209. CELLPADDING="0"
  39210. CELLSPACING="0"
  39211. CLASS="EXAMPLE"
  39212. ><TR
  39213. ><TD
  39214. ><DIV
  39215. CLASS="example"
  39216. ><A
  39217. NAME="AEN6347"
  39218. ></A
  39219. ><P
  39220. ><B
  39221. >Example 29-3. Detecting simple variable poisoning</B
  39222. ></P
  39223. ><TABLE
  39224. BORDER="0"
  39225. BGCOLOR="#E0E0E0"
  39226. CELLPADDING="5"
  39227. ><TR
  39228. ><TD
  39229. ><PRE
  39230. CLASS="php"
  39231. ><?php
  39232. if (isset($_COOKIE['MAGIC_COOKIE'])) {
  39233.  
  39234.     // MAGIC_COOKIE comes from a cookie.
  39235.     // Be sure to validate the cookie data!
  39236.  
  39237. } elseif (isset($_GET['MAGIC_COOKIE']) || isset($_POST['MAGIC_COOKIE'])) {
  39238.  
  39239.    mail("admin@example.com", "Possible breakin attempt", $_SERVER['REMOTE_ADDR']);
  39240.    echo "Security violation, admin has been alerted.";
  39241.    exit;
  39242.  
  39243. } else {
  39244.  
  39245.    // MAGIC_COOKIE isn't set through this REQUEST
  39246.  
  39247. }
  39248. ?></PRE
  39249. ></TD
  39250. ></TR
  39251. ></TABLE
  39252. ></DIV
  39253. ></TD
  39254. ></TR
  39255. ></TABLE
  39256. >
  39257.    </P
  39258. ><P
  39259. >     Of course, simply turning off register_globals does not mean your code
  39260.     is secure.  For every piece of data that is submitted, it should also be
  39261.     checked in other ways.  Always validate your user data and initialize
  39262.     your variables!  To check for uninitialized variables you may turn up
  39263.     <A
  39264. HREF="#function.error-reporting"
  39265. ><B
  39266. CLASS="function"
  39267. >error_reporting()</B
  39268. ></A
  39269. > to show
  39270.     <TT
  39271. CLASS="constant"
  39272. ><B
  39273. >E_NOTICE</B
  39274. ></TT
  39275. > level errors.
  39276.    </P
  39277. ><P
  39278. >     For information about emulating register_globals being On or Off, see this <A
  39279. HREF="#faq.misc.registerglobals"
  39280. >FAQ</A
  39281. >.
  39282.    </P
  39283. ><DIV
  39284. CLASS="note"
  39285. ><BLOCKQUOTE
  39286. CLASS="note"
  39287. ><P
  39288. ><B
  39289. >Superglobals: availability note: </B
  39290. >Since PHP 4.1.0, superglobal arrays such as <VAR
  39291. CLASS="varname"
  39292. >$_GET
  39293. </VAR
  39294. >, <VAR
  39295. CLASS="varname"
  39296. >$_POST</VAR
  39297. >, and <VAR
  39298. CLASS="varname"
  39299. >$_SERVER</VAR
  39300. >, 
  39301. etc. have  been available.  For more information, read the manual section 
  39302. on <A
  39303. HREF="#language.variables.predefined"
  39304. >superglobals</A
  39305. ></P
  39306. ></BLOCKQUOTE
  39307. ></DIV
  39308. ></DIV
  39309. ><DIV
  39310. CLASS="chapter"
  39311. ><HR><H1
  39312. ><A
  39313. NAME="security.variables"
  39314. >Chapter 30. User Submitted Data</A
  39315. ></H1
  39316. ><P
  39317. >     The greatest weakness in many PHP programs is not inherent in the
  39318.     language itself, but merely an issue of code not being written with
  39319.     security in mind. For this reason, you should always take the time
  39320.     to consider the implications of a given piece of code, to ascertain
  39321.     the possible damage if an unexpected variable is submitted to it.
  39322.     <TABLE
  39323. WIDTH="100%"
  39324. BORDER="0"
  39325. CELLPADDING="0"
  39326. CELLSPACING="0"
  39327. CLASS="EXAMPLE"
  39328. ><TR
  39329. ><TD
  39330. ><DIV
  39331. CLASS="example"
  39332. ><A
  39333. NAME="AEN6365"
  39334. ></A
  39335. ><P
  39336. ><B
  39337. >Example 30-1. Dangerous Variable Usage</B
  39338. ></P
  39339. ><TABLE
  39340. BORDER="0"
  39341. BGCOLOR="#E0E0E0"
  39342. CELLPADDING="5"
  39343. ><TR
  39344. ><TD
  39345. ><PRE
  39346. CLASS="php"
  39347. ><?php
  39348. // remove a file from the user's home directory... or maybe
  39349. // somebody else's?
  39350. unlink ($evil_var);
  39351.  
  39352. // Write logging of their access... or maybe an /etc/passwd entry?
  39353. fwrite ($fp, $evil_var);
  39354.  
  39355. // Execute something trivial.. or rm -rf *?
  39356. system ($evil_var);
  39357. exec ($evil_var);
  39358.  
  39359. ?></PRE
  39360. ></TD
  39361. ></TR
  39362. ></TABLE
  39363. ></DIV
  39364. ></TD
  39365. ></TR
  39366. ></TABLE
  39367. >
  39368.     You should always carefully examine your code to make sure that any
  39369.     variables being submitted from a web browser are being properly
  39370.     checked, and ask yourself the following questions:
  39371.     <P
  39372. ></P
  39373. ><UL
  39374. ><LI
  39375. ><P
  39376. >        Will this script only affect the intended files?
  39377.       </P
  39378. ></LI
  39379. ><LI
  39380. ><P
  39381. >        Can unusual or undesirable data be acted upon?
  39382.       </P
  39383. ></LI
  39384. ><LI
  39385. ><P
  39386. >        Can this script be used in unintended ways?
  39387.       </P
  39388. ></LI
  39389. ><LI
  39390. ><P
  39391. >        Can this be used in conjunction with other scripts in a negative
  39392.        manner?
  39393.       </P
  39394. ></LI
  39395. ><LI
  39396. ><P
  39397. >        Will any transactions be adequately logged?
  39398.       </P
  39399. ></LI
  39400. ></UL
  39401. >
  39402.     By adequately asking these questions while writing the script,
  39403.     rather than later, you prevent an unfortunate re-write when you
  39404.     need to increase your security. By starting out with this mindset,
  39405.     you won't guarantee the security of your system, but you can help
  39406.     improve it.
  39407.    </P
  39408. ><P
  39409. >     You may also want to consider turning off register_globals,
  39410.     magic_quotes, or other convenience settings which may confuse
  39411.     you as to the validity, source, or value of a given variable.
  39412.     Working with PHP in error_reporting(E_ALL) mode can also help warn
  39413.     you about variables being used before they are checked or
  39414.     initialized (so you can prevent unusual data from being
  39415.     operated upon).
  39416.    </P
  39417. ></DIV
  39418. ><DIV
  39419. CLASS="chapter"
  39420. ><HR><H1
  39421. ><A
  39422. NAME="security.magicquotes"
  39423. >Chapter 31. Magic Quotes</A
  39424. ></H1
  39425. ><P
  39426. >     Magic Quotes is a process that automagically escapes incoming data to the 
  39427.     PHP script. It's preferred to code with magic quotes off and to instead
  39428.     escape the data at runtime, as needed.
  39429.    </P
  39430. ><DIV
  39431. CLASS="sect1"
  39432. ><HR><H2
  39433. CLASS="sect1"
  39434. ><A
  39435. NAME="security.magicquotes.what"
  39436. >What are Magic Quotes</A
  39437. ></H2
  39438. ><P
  39439. >      When on, all <VAR
  39440. CLASS="literal"
  39441. >'</VAR
  39442. > (single-quote), <VAR
  39443. CLASS="literal"
  39444. >"</VAR
  39445.      (double quote), <VAR
  39446. CLASS="literal"
  39447. >\</VAR
  39448. > (backslash) and <VAR
  39449. CLASS="literal"
  39450. >NULL</VAR
  39451.      characters are escaped with a backslash automatically.  This is identical
  39452.      to what <A
  39453. HREF="#function.addslashes"
  39454. ><B
  39455. CLASS="function"
  39456. >addslashes()</B
  39457. ></A
  39458. > does.
  39459.     </P
  39460. ><P
  39461. >      There are three magic quote directives:
  39462.     </P
  39463. ><P
  39464. ></P
  39465. ><UL
  39466. ><LI
  39467. ><P
  39468. >        <A
  39469. HREF="#ini.magic-quotes-gpc"
  39470. >magic_quotes_gpc</A
  39471. >
  39472.       </P
  39473. ><P
  39474. >        Affects HTTP Request data (GET, POST, and COOKIE). Cannot be set at 
  39475.        runtime, and defaults to <SPAN
  39476. CLASS="emphasis"
  39477. ><I
  39478. CLASS="emphasis"
  39479. >on</I
  39480. ></SPAN
  39481. > in PHP.
  39482.       </P
  39483. ><P
  39484. >        See also <A
  39485. HREF="#function.get-magic-quotes-gpc"
  39486. ><B
  39487. CLASS="function"
  39488. >get_magic_quotes_gpc()</B
  39489. ></A
  39490. >.
  39491.       </P
  39492. ></LI
  39493. ><LI
  39494. ><P
  39495. >        <A
  39496. HREF="#ini.magic-quotes-runtime"
  39497. >magic_quotes_runtime</A
  39498. >
  39499.       </P
  39500. ><P
  39501. >        If enabled, most functions that return data from an external source, 
  39502.        including databases and text files, will have quotes escaped with a 
  39503.        backslash. Can be set at runtime, and defaults to <SPAN
  39504. CLASS="emphasis"
  39505. ><I
  39506. CLASS="emphasis"
  39507. >on</I
  39508. ></SPAN
  39509. >
  39510.        in PHP.
  39511.       </P
  39512. ><P
  39513. >        See also <A
  39514. HREF="#function.set-magic-quotes-runtime"
  39515. ><B
  39516. CLASS="function"
  39517. >set_magic_quotes_runtime()</B
  39518. ></A
  39519. > and
  39520.        <A
  39521. HREF="#function.get-magic-quotes-runtime"
  39522. ><B
  39523. CLASS="function"
  39524. >get_magic_quotes_runtime()</B
  39525. ></A
  39526. >.
  39527.       </P
  39528. ></LI
  39529. ><LI
  39530. ><P
  39531. >        <A
  39532. HREF="#ini.magic-quotes-sybase"
  39533. >magic_quotes_sybase</A
  39534. >
  39535.       </P
  39536. ><P
  39537. >        If enabled, a single-quote is escaped with a single-quote instead of a 
  39538.        backslash.  If on, it completely overrides 
  39539.        <A
  39540. HREF="#ini.magic-quotes-gpc"
  39541. >magic_quotes_gpc</A
  39542. >. Having 
  39543.        both directives enabled means only single quotes are escaped as 
  39544.        <VAR
  39545. CLASS="literal"
  39546. >''</VAR
  39547. >. Double quotes, backslashes and NULL's will 
  39548.        remain untouched and unescaped.
  39549.       </P
  39550. ><P
  39551. >        See also <A
  39552. HREF="#function.ini-get"
  39553. ><B
  39554. CLASS="function"
  39555. >ini_get()</B
  39556. ></A
  39557. > for retrieving its value.
  39558.       </P
  39559. ></LI
  39560. ></UL
  39561. ></DIV
  39562. ><DIV
  39563. CLASS="sect1"
  39564. ><HR><H2
  39565. CLASS="sect1"
  39566. ><A
  39567. NAME="security.magicquotes.why"
  39568. >Why use Magic Quotes</A
  39569. ></H2
  39570. ><P
  39571. ></P
  39572. ><UL
  39573. ><LI
  39574. ><P
  39575. >        Useful for beginners
  39576.       </P
  39577. ><P
  39578. >        Magic quotes are implemented in PHP to help code written by beginners 
  39579.        from being dangerous. Although 
  39580.        <A
  39581. HREF="#security.database.sql-injection"
  39582. >SQL Injection</A
  39583.        is still possible with magic quotes on, the risk is reduced.
  39584.       </P
  39585. ></LI
  39586. ><LI
  39587. ><P
  39588. >        Convenience
  39589.       </P
  39590. ><P
  39591. >        For inserting data into a database, magic quotes essentially runs 
  39592.        <A
  39593. HREF="#function.addslashes"
  39594. ><B
  39595. CLASS="function"
  39596. >addslashes()</B
  39597. ></A
  39598. > on all Get, Post, and Cookie data,
  39599.        and does so automagically.
  39600.       </P
  39601. ></LI
  39602. ></UL
  39603. ></DIV
  39604. ><DIV
  39605. CLASS="sect1"
  39606. ><HR><H2
  39607. CLASS="sect1"
  39608. ><A
  39609. NAME="security.magicquotes.whynot"
  39610. >Why not to use Magic Quotes</A
  39611. ></H2
  39612. ><P
  39613. ></P
  39614. ><UL
  39615. ><LI
  39616. ><P
  39617. >        Portability
  39618.       </P
  39619. ><P
  39620. >        Assuming it to be on, or off, affects portability. Use
  39621.        <A
  39622. HREF="#function.get-magic-quotes-gpc"
  39623. ><B
  39624. CLASS="function"
  39625. >get_magic_quotes_gpc()</B
  39626. ></A
  39627. > to check for this, and code
  39628.        accordingly.
  39629.       </P
  39630. ></LI
  39631. ><LI
  39632. ><P
  39633. >        Performance
  39634.       </P
  39635. ><P
  39636. >        Because not every piece of escaped data is inserted into a 
  39637.        database, there is a performance loss for escaping all this data. 
  39638.        Simply calling on the escaping functions (like 
  39639.        <A
  39640. HREF="#function.addslashes"
  39641. ><B
  39642. CLASS="function"
  39643. >addslashes()</B
  39644. ></A
  39645. >) at runtime is more efficient.
  39646.       </P
  39647. ><P
  39648. >        Although <TT
  39649. CLASS="filename"
  39650. >php.ini-dist</TT
  39651. > enables these directives  
  39652.        by default, <TT
  39653. CLASS="filename"
  39654. >php.ini-recommended</TT
  39655. > disables it.
  39656.        This recommendation is mainly due to performance reasons.
  39657.       </P
  39658. ></LI
  39659. ><LI
  39660. ><P
  39661. >        Inconvenience
  39662.       </P
  39663. ><P
  39664. >        Because not all data needs escaping, it's often annoying to see escaped
  39665.        data where it shouldn't be. For example, emailing from a form, and
  39666.        seeing a bunch of \' within the email. To fix, this may require 
  39667.        excessive use of <A
  39668. HREF="#function.stripslashes"
  39669. ><B
  39670. CLASS="function"
  39671. >stripslashes()</B
  39672. ></A
  39673. >.
  39674.       </P
  39675. ></LI
  39676. ></UL
  39677. ></DIV
  39678. ><DIV
  39679. CLASS="sect1"
  39680. ><HR><H2
  39681. CLASS="sect1"
  39682. ><A
  39683. NAME="security.magicquotes.disabling"
  39684. >Disabling Magic Quotes</A
  39685. ></H2
  39686. ><P
  39687. >      The <A
  39688. HREF="#ini.magic-quotes-gpc"
  39689. >magic_quotes_gpc</A
  39690. >
  39691.      directive may only be disabled at the system level, and not at
  39692.      runtime. In otherwords, use of <A
  39693. HREF="#function.ini-set"
  39694. ><B
  39695. CLASS="function"
  39696. >ini_set()</B
  39697. ></A
  39698. > is not
  39699.      an option.
  39700.     </P
  39701. ><P
  39702. >      <TABLE
  39703. WIDTH="100%"
  39704. BORDER="0"
  39705. CELLPADDING="0"
  39706. CELLSPACING="0"
  39707. CLASS="EXAMPLE"
  39708. ><TR
  39709. ><TD
  39710. ><DIV
  39711. CLASS="example"
  39712. ><A
  39713. NAME="AEN6451"
  39714. ></A
  39715. ><P
  39716. ><B
  39717. >Example 31-1. Disabling magic quotes server side</B
  39718. ></P
  39719. ><P
  39720. >        An example that sets the value of these directives to 
  39721.        <VAR
  39722. CLASS="literal"
  39723. >Off</VAR
  39724. > in <TT
  39725. CLASS="filename"
  39726. >php.ini</TT
  39727. >.  For additional details, read the 
  39728.        manual section titled <A
  39729. HREF="#configuration.changes"
  39730. >How to 
  39731.        change configuration settings</A
  39732. >.
  39733.       </P
  39734. ><TABLE
  39735. BORDER="0"
  39736. BGCOLOR="#E0E0E0"
  39737. CELLPADDING="5"
  39738. ><TR
  39739. ><TD
  39740. ><PRE
  39741. CLASS="screen"
  39742. >; Magic quotes
  39743. ;
  39744.  
  39745. ; Magic quotes for incoming GET/POST/Cookie data.
  39746. magic_quotes_gpc = Off
  39747.  
  39748. ; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.
  39749. magic_quotes_runtime = Off
  39750.  
  39751. ; Use Sybase-style magic quotes (escape ' with '' instead of \').
  39752. magic_quotes_sybase = Off</PRE
  39753. ></TD
  39754. ></TR
  39755. ></TABLE
  39756. ><P
  39757. >        If access to the server configuration is unavilable, use of
  39758.        <TT
  39759. CLASS="filename"
  39760. >.htaccess</TT
  39761. > is also an option.  For example:
  39762.       </P
  39763. ><TABLE
  39764. BORDER="0"
  39765. BGCOLOR="#E0E0E0"
  39766. CELLPADDING="5"
  39767. ><TR
  39768. ><TD
  39769. ><PRE
  39770. CLASS="screen"
  39771. >php_flag magic_quotes_gpc Off</PRE
  39772. ></TD
  39773. ></TR
  39774. ></TABLE
  39775. ></DIV
  39776. ></TD
  39777. ></TR
  39778. ></TABLE
  39779. >
  39780.     </P
  39781. ><P
  39782. >      In the interest of writing portable code (code that works in any 
  39783.      environment), like if setting at the server level is not possible,
  39784.      here's an example to disable <A
  39785. HREF="#ini.magic-quotes-gpc"
  39786. >      magic_quotes_gpc</A
  39787. > at runtime. This method is inefficient so 
  39788.      it's preferred to instead set the appropriate directives elsewhere.
  39789.     </P
  39790. ><P
  39791. >      <TABLE
  39792. WIDTH="100%"
  39793. BORDER="0"
  39794. CELLPADDING="0"
  39795. CELLSPACING="0"
  39796. CLASS="EXAMPLE"
  39797. ><TR
  39798. ><TD
  39799. ><DIV
  39800. CLASS="example"
  39801. ><A
  39802. NAME="AEN6464"
  39803. ></A
  39804. ><P
  39805. ><B
  39806. >Example 31-2. Disabling magic quotes at runtime</B
  39807. ></P
  39808. ><TABLE
  39809. BORDER="0"
  39810. BGCOLOR="#E0E0E0"
  39811. CELLPADDING="5"
  39812. ><TR
  39813. ><TD
  39814. ><PRE
  39815. CLASS="php"
  39816. ><?php
  39817. if (get_magic_quotes_gpc()) {
  39818.     function stripslashes_deep($value)
  39819.     {
  39820.         $value = is_array($value) ?
  39821.                     array_map('stripslashes_deep', $value) :
  39822.                     stripslashes($value);
  39823.  
  39824.         return $value;
  39825.     }
  39826.  
  39827.     $_POST = array_map('stripslashes_deep', $_POST);
  39828.     $_GET = array_map('stripslashes_deep', $_GET);
  39829.     $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
  39830. }
  39831. ?></PRE
  39832. ></TD
  39833. ></TR
  39834. ></TABLE
  39835. ></DIV
  39836. ></TD
  39837. ></TR
  39838. ></TABLE
  39839. >
  39840.     </P
  39841. ></DIV
  39842. ></DIV
  39843. ><DIV
  39844. CLASS="chapter"
  39845. ><HR><H1
  39846. ><A
  39847. NAME="security.hiding"
  39848. >Chapter 32. Hiding PHP</A
  39849. ></H1
  39850. ><P
  39851. >     In general, security by obscurity is one of the weakest forms of security.
  39852.     But in some cases, every little bit of extra security is desirable.
  39853.    </P
  39854. ><P
  39855. >     A few simple techniques can help to hide PHP, possibly slowing
  39856.     down an attacker who is attempting to discover weaknesses in your
  39857.     system. By setting expose_php = off in your <TT
  39858. CLASS="filename"
  39859. >php.ini</TT
  39860. > file, you
  39861.     reduce the amount of information available to them.
  39862.    </P
  39863. ><P
  39864. >     Another tactic is to configure web servers such as apache to
  39865.     parse different filetypes through PHP, either with an <TT
  39866. CLASS="filename"
  39867. >.htaccess</TT
  39868. >
  39869.     directive, or in the apache configuration file itself. You can
  39870.     then use misleading file extensions:
  39871.     <TABLE
  39872. WIDTH="100%"
  39873. BORDER="0"
  39874. CELLPADDING="0"
  39875. CELLSPACING="0"
  39876. CLASS="EXAMPLE"
  39877. ><TR
  39878. ><TD
  39879. ><DIV
  39880. CLASS="example"
  39881. ><A
  39882. NAME="AEN6474"
  39883. ></A
  39884. ><P
  39885. ><B
  39886. >Example 32-1. Hiding PHP as another language</B
  39887. ></P
  39888. ><TABLE
  39889. BORDER="0"
  39890. BGCOLOR="#E0E0E0"
  39891. CELLPADDING="5"
  39892. ><TR
  39893. ><TD
  39894. ><PRE
  39895. CLASS="apache-conf"
  39896. ># Make PHP code look like other code types
  39897. AddType application/x-httpd-php .asp .py .pl</PRE
  39898. ></TD
  39899. ></TR
  39900. ></TABLE
  39901. ></DIV
  39902. ></TD
  39903. ></TR
  39904. ></TABLE
  39905. >
  39906.     Or obscure it completely:
  39907.     <TABLE
  39908. WIDTH="100%"
  39909. BORDER="0"
  39910. CELLPADDING="0"
  39911. CELLSPACING="0"
  39912. CLASS="EXAMPLE"
  39913. ><TR
  39914. ><TD
  39915. ><DIV
  39916. CLASS="example"
  39917. ><A
  39918. NAME="AEN6477"
  39919. ></A
  39920. ><P
  39921. ><B
  39922. >Example 32-2. Using unknown types for PHP extensions</B
  39923. ></P
  39924. ><TABLE
  39925. BORDER="0"
  39926. BGCOLOR="#E0E0E0"
  39927. CELLPADDING="5"
  39928. ><TR
  39929. ><TD
  39930. ><PRE
  39931. CLASS="apache-conf"
  39932. ># Make PHP code look like unknown types
  39933. AddType application/x-httpd-php .bop .foo .133t</PRE
  39934. ></TD
  39935. ></TR
  39936. ></TABLE
  39937. ></DIV
  39938. ></TD
  39939. ></TR
  39940. ></TABLE
  39941. >
  39942.     Or hide it as HTML code, which has a slight performance hit because
  39943.     all HTML will be parsed through the PHP engine:
  39944.     <TABLE
  39945. WIDTH="100%"
  39946. BORDER="0"
  39947. CELLPADDING="0"
  39948. CELLSPACING="0"
  39949. CLASS="EXAMPLE"
  39950. ><TR
  39951. ><TD
  39952. ><DIV
  39953. CLASS="example"
  39954. ><A
  39955. NAME="AEN6480"
  39956. ></A
  39957. ><P
  39958. ><B
  39959. >Example 32-3. Using HTML types for PHP extensions</B
  39960. ></P
  39961. ><TABLE
  39962. BORDER="0"
  39963. BGCOLOR="#E0E0E0"
  39964. CELLPADDING="5"
  39965. ><TR
  39966. ><TD
  39967. ><PRE
  39968. CLASS="apache-conf"
  39969. ># Make all PHP code look like HTML
  39970. AddType application/x-httpd-php .htm .html</PRE
  39971. ></TD
  39972. ></TR
  39973. ></TABLE
  39974. ></DIV
  39975. ></TD
  39976. ></TR
  39977. ></TABLE
  39978. >
  39979.     For this to work effectively, you must rename your PHP files with
  39980.     the above extensions. While it is a form of security through
  39981.     obscurity, it's a minor preventative measure with few drawbacks.
  39982.    </P
  39983. ></DIV
  39984. ><DIV
  39985. CLASS="chapter"
  39986. ><HR><H1
  39987. ><A
  39988. NAME="security.current"
  39989. >Chapter 33. Keeping Current</A
  39990. ></H1
  39991. ><P
  39992. >     PHP, like any other large system, is under constant scrutiny and
  39993.     improvement. Each new version will often include both major and
  39994.     minor changes to enhance and repair security flaws, configuration
  39995.     mishaps, and other issues that will affect the overall security
  39996.     and stability of your system.
  39997.    </P
  39998. ><P
  39999. >     Like other system-level scripting languages and programs, the best
  40000.     approach is to update often, and maintain awareness of the latest
  40001.     versions and their changes.
  40002.    </P
  40003. ></DIV
  40004. ></DIV
  40005. ><DIV
  40006. CLASS="PART"
  40007. ><A
  40008. NAME="features"
  40009. ></A
  40010. ><DIV
  40011. CLASS="TITLEPAGE"
  40012. ><H1
  40013. CLASS="title"
  40014. >V. Features</H1
  40015. ><DIV
  40016. CLASS="TOC"
  40017. ><DL
  40018. ><DT
  40019. ><B
  40020. >Table of Contents</B
  40021. ></DT
  40022. ><DT
  40023. >34. <A
  40024. HREF="#features.http-auth"
  40025. >HTTP authentication with PHP</A
  40026. ></DT
  40027. ><DT
  40028. >35. <A
  40029. HREF="#features.cookies"
  40030. >Cookies</A
  40031. ></DT
  40032. ><DT
  40033. >36. <A
  40034. HREF="#features.sessions"
  40035. >Sessions</A
  40036. ></DT
  40037. ><DT
  40038. >37. <A
  40039. HREF="#features.xforms"
  40040. >Dealing with XForms</A
  40041. ></DT
  40042. ><DT
  40043. >38. <A
  40044. HREF="#features.file-upload"
  40045. >Handling file uploads</A
  40046. ></DT
  40047. ><DT
  40048. >39. <A
  40049. HREF="#features.remote-files"
  40050. >Using remote files</A
  40051. ></DT
  40052. ><DT
  40053. >40. <A
  40054. HREF="#features.connection-handling"
  40055. >Connection handling</A
  40056. ></DT
  40057. ><DT
  40058. >41. <A
  40059. HREF="#features.persistent-connections"
  40060. >Persistent Database Connections</A
  40061. ></DT
  40062. ><DT
  40063. >42. <A
  40064. HREF="#features.safe-mode"
  40065. >Safe Mode</A
  40066. ></DT
  40067. ><DT
  40068. >43. <A
  40069. HREF="#features.commandline"
  40070. >Using PHP from the command line</A
  40071. ></DT
  40072. ></DL
  40073. ></DIV
  40074. ></DIV
  40075. ><DIV
  40076. CLASS="chapter"
  40077. ><HR><H1
  40078. ><A
  40079. NAME="features.http-auth"
  40080. >Chapter 34. HTTP authentication with PHP</A
  40081. ></H1
  40082. ><P
  40083. >    The <ACRONYM
  40084. CLASS="acronym"
  40085. >HTTP</ACRONYM
  40086. > Authentication hooks in PHP are only available when it is
  40087.    running as an Apache module and is hence not available in the CGI version.
  40088.    In an Apache module PHP script, it is possible to use the 
  40089.    <A
  40090. HREF="#function.header"
  40091. ><B
  40092. CLASS="function"
  40093. >header()</B
  40094. ></A
  40095. > function to send an "Authentication Required" 
  40096.    message to the client browser causing it to pop up a Username/Password 
  40097.    input window.  Once the user has filled in a username and a password, 
  40098.    the URL containing the PHP script will be called again with the 
  40099.    <A
  40100. HREF="#reserved.variables"
  40101. >predefined variables</A
  40102.    <VAR
  40103. CLASS="varname"
  40104. >PHP_AUTH_USER</VAR
  40105. >, <VAR
  40106. CLASS="varname"
  40107. >PHP_AUTH_PW</VAR
  40108. >, 
  40109.    and <VAR
  40110. CLASS="varname"
  40111. >AUTH_TYPE</VAR
  40112. > set to the user name, password and 
  40113.    authentication type respectively.  These predefined variables are found 
  40114.    in the <A
  40115. HREF="#reserved.variables.server"
  40116. >$_SERVER</A
  40117. > and 
  40118.    <VAR
  40119. CLASS="varname"
  40120. >$HTTP_SERVER_VARS</VAR
  40121. > arrays.  Only "Basic" authentication 
  40122.    is supported. See the <A
  40123. HREF="#function.header"
  40124. ><B
  40125. CLASS="function"
  40126. >header()</B
  40127. ></A
  40128. > function for more 
  40129.    information.
  40130.   </P
  40131. ><DIV
  40132. CLASS="note"
  40133. ><BLOCKQUOTE
  40134. CLASS="note"
  40135. ><P
  40136. ><B
  40137. >PHP Version Note: </B
  40138. >
  40139.     <A
  40140. HREF="#language.variables.superglobals"
  40141. >Autoglobals</A
  40142. >, 
  40143.     such as <A
  40144. HREF="#reserved.variables.server"
  40145. >$_SERVER</A
  40146. >, became 
  40147.     available in PHP <A
  40148. HREF="http://www.php.net/release_4_1_0.php"
  40149. TARGET="_top"
  40150. >4.1.0</A
  40151. >. 
  40152.     <VAR
  40153. CLASS="varname"
  40154. >$HTTP_SERVER_VARS</VAR
  40155. > has been available since PHP 3.
  40156.    </P
  40157. ></BLOCKQUOTE
  40158. ></DIV
  40159. ><P
  40160. >    An example script fragment which would force client authentication
  40161.    on a page is as follows:
  40162.   </P
  40163. ><P
  40164. >    <TABLE
  40165. WIDTH="100%"
  40166. BORDER="0"
  40167. CELLPADDING="0"
  40168. CELLSPACING="0"
  40169. CLASS="EXAMPLE"
  40170. ><TR
  40171. ><TD
  40172. ><DIV
  40173. CLASS="example"
  40174. ><A
  40175. NAME="AEN6510"
  40176. ></A
  40177. ><P
  40178. ><B
  40179. >Example 34-1. HTTP Authentication example</B
  40180. ></P
  40181. ><TABLE
  40182. BORDER="0"
  40183. BGCOLOR="#E0E0E0"
  40184. CELLPADDING="5"
  40185. ><TR
  40186. ><TD
  40187. ><PRE
  40188. CLASS="php"
  40189. ><?php
  40190.   if (!isset($_SERVER['PHP_AUTH_USER'])) {
  40191.     header('WWW-Authenticate: Basic realm="My Realm"');
  40192.     header('HTTP/1.0 401 Unauthorized');
  40193.     echo 'Text to send if user hits Cancel button';
  40194.     exit;
  40195.   } else {
  40196.     echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
  40197.     echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
  40198.   }
  40199. ?></PRE
  40200. ></TD
  40201. ></TR
  40202. ></TABLE
  40203. ></DIV
  40204. ></TD
  40205. ></TR
  40206. ></TABLE
  40207. >
  40208.   </P
  40209. ><DIV
  40210. CLASS="note"
  40211. ><BLOCKQUOTE
  40212. CLASS="note"
  40213. ><P
  40214. ><B
  40215. >Compatibility Note: </B
  40216. >
  40217.     Please be careful when coding the HTTP header lines. In order to guarantee maximum
  40218.     compatibility with all clients, the keyword "Basic" should be written with an
  40219.     uppercase "B", the realm string must be enclosed in double (not single) quotes,
  40220.     and exactly one space should precede the <SPAN
  40221. CLASS="emphasis"
  40222. ><I
  40223. CLASS="emphasis"
  40224. >401</I
  40225. ></SPAN
  40226. > code in the 
  40227.     <SPAN
  40228. CLASS="emphasis"
  40229. ><I
  40230. CLASS="emphasis"
  40231. >HTTP/1.0 401</I
  40232. ></SPAN
  40233. > header line.
  40234.    </P
  40235. ></BLOCKQUOTE
  40236. ></DIV
  40237. ><P
  40238. >    Instead of simply printing out <VAR
  40239. CLASS="varname"
  40240. >PHP_AUTH_USER</VAR
  40241.    and <VAR
  40242. CLASS="varname"
  40243. >PHP_AUTH_PW</VAR
  40244. >, as done in the above example, 
  40245.    you may want to check the username and password for validity.  
  40246.    Perhaps by sending a query to a database, or by looking up the 
  40247.    user in a dbm file.
  40248.   </P
  40249. ><P
  40250. >    Watch out for buggy Internet Explorer browsers out there.  They
  40251.    seem very picky about the order of the headers.  Sending the
  40252.    <SPAN
  40253. CLASS="emphasis"
  40254. ><I
  40255. CLASS="emphasis"
  40256. >WWW-Authenticate</I
  40257. ></SPAN
  40258. > header before the
  40259.    <VAR
  40260. CLASS="literal"
  40261. >HTTP/1.0 401</VAR
  40262. > header seems to do the trick
  40263.    for now.
  40264.   </P
  40265. ><P
  40266. >    As of PHP 4.3.0, in order to prevent someone from writing a script which
  40267.    reveals the password for a page that was authenticated through a
  40268.    traditional external mechanism, the PHP_AUTH variables will not be 
  40269.    set if external authentication is enabled for that particular
  40270.    page and <A
  40271. HREF="#ini.safe-mode"
  40272. >safe mode</A
  40273. > is enabled.  Regardless, 
  40274.    <VAR
  40275. CLASS="varname"
  40276. >REMOTE_USER</VAR
  40277. > can be used 
  40278.    to identify the externally-authenticated user.  So, you can use  
  40279.    <VAR
  40280. CLASS="varname"
  40281. >$_SERVER['REMOTE_USER']</VAR
  40282. >.
  40283.   </P
  40284. ><DIV
  40285. CLASS="note"
  40286. ><BLOCKQUOTE
  40287. CLASS="note"
  40288. ><P
  40289. ><B
  40290. >Configuration Note: </B
  40291. >
  40292.     PHP uses the presence of an <VAR
  40293. CLASS="literal"
  40294. >AuthType</VAR
  40295. > directive
  40296.     to determine whether external authentication is in effect.
  40297.    </P
  40298. ></BLOCKQUOTE
  40299. ></DIV
  40300. ><P
  40301. >    Note, however, that the above does not prevent someone who
  40302.    controls a non-authenticated URL from stealing passwords from
  40303.    authenticated URLs on the same server.
  40304.   </P
  40305. ><P
  40306. >    Both Netscape Navigator and Internet Explorer will clear the local browser
  40307.    window's authentication cache for the realm upon receiving a
  40308.    server response of 401. This can effectively "log out" a user,
  40309.    forcing them to re-enter their username and password. Some people
  40310.    use this to "time out" logins, or provide a "log-out" button.
  40311.   </P
  40312. ><P
  40313. >    <TABLE
  40314. WIDTH="100%"
  40315. BORDER="0"
  40316. CELLPADDING="0"
  40317. CELLSPACING="0"
  40318. CLASS="EXAMPLE"
  40319. ><TR
  40320. ><TD
  40321. ><DIV
  40322. CLASS="example"
  40323. ><A
  40324. NAME="AEN6535"
  40325. ></A
  40326. ><P
  40327. ><B
  40328. >Example 34-2. HTTP Authentication example forcing a new name/password</B
  40329. ></P
  40330. ><TABLE
  40331. BORDER="0"
  40332. BGCOLOR="#E0E0E0"
  40333. CELLPADDING="5"
  40334. ><TR
  40335. ><TD
  40336. ><PRE
  40337. CLASS="php"
  40338. ><?php
  40339.   function authenticate() {
  40340.     header('WWW-Authenticate: Basic realm="Test Authentication System"');
  40341.     header('HTTP/1.0 401 Unauthorized');
  40342.     echo "You must enter a valid login ID and password to access this resource\n";
  40343.     exit;
  40344.   }
  40345.  
  40346.   if (!isset($_SERVER['PHP_AUTH_USER']) ||
  40347.       ($_POST['SeenBefore'] == 1 && $_POST['OldAuth'] == $_SERVER['PHP_AUTH_USER'])) {
  40348.    authenticate();
  40349.   } 
  40350.   else {
  40351.    echo "<p>Welcome: {$_SERVER['PHP_AUTH_USER']}<br />";
  40352.    echo "Old: {$_REQUEST['OldAuth']}";
  40353.    echo "<form action='{$_SERVER['PHP_SELF']}' METHOD='post'>\n";
  40354.    echo "<input type='hidden' name='SeenBefore' value='1' />\n";
  40355.    echo "<input type='hidden' name='OldAuth' value='{$_SERVER['PHP_AUTH_USER']}' />\n";
  40356.    echo "<input type='submit' value='Re Authenticate' />\n";
  40357.    echo "</form></p>\n";
  40358.   }
  40359. ?></PRE
  40360. ></TD
  40361. ></TR
  40362. ></TABLE
  40363. ></DIV
  40364. ></TD
  40365. ></TR
  40366. ></TABLE
  40367. >
  40368.   </P
  40369. ><P
  40370. >    This behavior is not required by the HTTP Basic authentication
  40371.    standard, so you should never depend on this. Testing with Lynx
  40372.    has shown that Lynx does not clear the authentication credentials
  40373.    with a 401 server response, so pressing back and then forward
  40374.    again will open the resource as long as the credential
  40375.    requirements haven't changed. The user can press the
  40376.    '_' key to clear their authentication information, however.
  40377.   </P
  40378. ><P
  40379. >    Also note that until PHP 4.3.3, HTTP Authentication did not work
  40380.    using Microsoft's IIS server with the CGI version of PHP due to a
  40381.    limitation of IIS.  In order to get it to work in PHP 4.3.3+, 
  40382.    you must edit your IIS configuration "Directory Security".  Click
  40383.    on "Edit" and only check "Anonymous Access", all other fields
  40384.    should be left unchecked.
  40385.   </P
  40386. ><P
  40387. >    Another limitation is if you're using the IIS module (ISAPI) and PHP 4, you
  40388.    may not use the <VAR
  40389. CLASS="literal"
  40390. >PHP_AUTH_*</VAR
  40391. > variables but instead, the
  40392.    variable <VAR
  40393. CLASS="literal"
  40394. >HTTP_AUTHORIZATION</VAR
  40395. > is available.  For example,
  40396.    consider the following code: <VAR
  40397. CLASS="literal"
  40398. >list($user, $pw) = explode(':',
  40399.     base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));</VAR
  40400. >
  40401.   </P
  40402. ><DIV
  40403. CLASS="note"
  40404. ><BLOCKQUOTE
  40405. CLASS="note"
  40406. ><P
  40407. ><B
  40408. >IIS Note:: </B
  40409. >
  40410.     For HTTP Authentication to work with IIS, the PHP directive
  40411.     <A
  40412. HREF="#ini.cgi.rfc2616-headers"
  40413. >cgi.rfc2616_headers</A
  40414. > must
  40415.     be set to <VAR
  40416. CLASS="literal"
  40417. >0</VAR
  40418. > (the default value).
  40419.    </P
  40420. ></BLOCKQUOTE
  40421. ></DIV
  40422. ><DIV
  40423. CLASS="note"
  40424. ><BLOCKQUOTE
  40425. CLASS="note"
  40426. ><P
  40427. ><B
  40428. >Note: </B
  40429. >
  40430.     If <A
  40431. HREF="#ini.safe-mode"
  40432. >safe mode</A
  40433. > is enabled, the
  40434.     uid of the script is added to the <VAR
  40435. CLASS="literal"
  40436. >realm</VAR
  40437. > part of
  40438.     the <VAR
  40439. CLASS="literal"
  40440. >WWW-Authenticate</VAR
  40441. > header.
  40442.    </P
  40443. ></BLOCKQUOTE
  40444. ></DIV
  40445. ></DIV
  40446. ><DIV
  40447. CLASS="chapter"
  40448. ><HR><H1
  40449. ><A
  40450. NAME="features.cookies"
  40451. >Chapter 35. Cookies</A
  40452. ></H1
  40453. ><P
  40454. >    PHP transparently supports <ACRONYM
  40455. CLASS="acronym"
  40456. >HTTP</ACRONYM
  40457. > cookies.  Cookies are a mechanism for
  40458.    storing data in the remote browser and thus tracking or identifying return
  40459.    users.  You can set cookies using the <A
  40460. HREF="#function.setcookie"
  40461. ><B
  40462. CLASS="function"
  40463. >setcookie()</B
  40464. ></A
  40465. > or
  40466.    <A
  40467. HREF="#function.setrawcookie"
  40468. ><B
  40469. CLASS="function"
  40470. >setrawcookie()</B
  40471. ></A
  40472. >
  40473.    function.  Cookies are part of the <ACRONYM
  40474. CLASS="acronym"
  40475. >HTTP</ACRONYM
  40476. > header, so
  40477.    <A
  40478. HREF="#function.setcookie"
  40479. ><B
  40480. CLASS="function"
  40481. >setcookie()</B
  40482. ></A
  40483. > must be called before any output is sent to
  40484.    the browser.  This is the same limitation that <A
  40485. HREF="#function.header"
  40486. ><B
  40487. CLASS="function"
  40488. >header()</B
  40489. ></A
  40490. >
  40491.    has. You can use the <A
  40492. HREF="#ref.outcontrol"
  40493. >output buffering
  40494.    functions</A
  40495. > to delay the script output until you have decided whether
  40496.    or not to set any cookies or send any headers.
  40497.   </P
  40498. ><P
  40499. >    Any cookies sent to you from the client will automatically be turned into a
  40500.    PHP variable just like <VAR
  40501. CLASS="literal"
  40502. >GET</VAR
  40503. > and <VAR
  40504. CLASS="literal"
  40505. >POST</VAR
  40506.    method data, depending on the  <A
  40507. HREF="#ini.register-globals"
  40508. >register_globals</A
  40509. >
  40510.    and <A
  40511. HREF="#ini.variables-order"
  40512. >variables_order</A
  40513. >
  40514.    configuration variables.  If you wish to assign multiple values to a single
  40515.    cookie, just add <VAR
  40516. CLASS="literal"
  40517. >[]</VAR
  40518. > to the cookie name.
  40519.   </P
  40520. ><P
  40521. >    In PHP 4.1.0 and later, the <VAR
  40522. CLASS="varname"
  40523. >$_COOKIE</VAR
  40524. > auto-global array
  40525.    will always be set with any cookies sent from the client.
  40526.    <VAR
  40527. CLASS="varname"
  40528. >$HTTP_COOKIE_VARS</VAR
  40529. > is also set in earlier versions of PHP
  40530.    when the <A
  40531. HREF="#ini.track-vars"
  40532. >track_vars</A
  40533. > configuration 
  40534.    variable is set. (This setting is always on since PHP 4.0.3.)
  40535.   </P
  40536. ><P
  40537. >    For more details, including notes on browser bugs, see the
  40538.    <A
  40539. HREF="#function.setcookie"
  40540. ><B
  40541. CLASS="function"
  40542. >setcookie()</B
  40543. ></A
  40544. > and <A
  40545. HREF="#function.setrawcookie"
  40546. ><B
  40547. CLASS="function"
  40548. >setrawcookie()</B
  40549. ></A
  40550. >
  40551.    function.
  40552.   </P
  40553. ></DIV
  40554. ><DIV
  40555. CLASS="chapter"
  40556. ><HR><H1
  40557. ><A
  40558. NAME="features.sessions"
  40559. >Chapter 36. Sessions</A
  40560. ></H1
  40561. ><P
  40562. >    Session support in PHP consists of a way to preserve certain data across
  40563.    subsequent accesses. This enables you to build more customized applications
  40564.    and increase the appeal of your web site. All informations are in
  40565.    <A
  40566. HREF="#ref.session"
  40567. >Session reference</A
  40568. > sections.
  40569.   </P
  40570. ></DIV
  40571. ><DIV
  40572. CLASS="chapter"
  40573. ><HR><H1
  40574. ><A
  40575. NAME="features.xforms"
  40576. >Chapter 37. Dealing with XForms</A
  40577. ></H1
  40578. ><P
  40579. >   <A
  40580. HREF="http://www.w3.org/MarkUp/Forms/"
  40581. TARGET="_top"
  40582. >XForms</A
  40583. > defines a variation on traditional
  40584.   webforms which allows them to be used on a wider variety of platforms and 
  40585.   browsers or even non-traditional media such as PDF documents.
  40586.  </P
  40587. ><P
  40588. >   The first key difference in XForms is how the form is sent to the client.
  40589.   <A
  40590. HREF="http://www.w3.org/MarkUp/Forms/2003/xforms-for-html-authors.html"
  40591. TARGET="_top"
  40592. >XForms for HTML Authors</A
  40593. >
  40594.   contains a detailed description of how to create XForms, for the purpose
  40595.   of this tutorial we'll only be looking at a simple example.
  40596.  </P
  40597. ><TABLE
  40598. WIDTH="100%"
  40599. BORDER="0"
  40600. CELLPADDING="0"
  40601. CELLSPACING="0"
  40602. CLASS="EXAMPLE"
  40603. ><TR
  40604. ><TD
  40605. ><DIV
  40606. CLASS="example"
  40607. ><A
  40608. NAME="AEN6587"
  40609. ></A
  40610. ><P
  40611. ><B
  40612. >Example 37-1. A simple XForms search form</B
  40613. ></P
  40614. ><TABLE
  40615. BORDER="0"
  40616. BGCOLOR="#E0E0E0"
  40617. CELLPADDING="5"
  40618. ><TR
  40619. ><TD
  40620. ><PRE
  40621. CLASS="html"
  40622. ><h:html xmlns:h="http://www.w3.org/1999/xhtml"
  40623.         xmlns="http://www.w3.org/2002/xforms">
  40624. <h:head>
  40625.  <h:title>Search</h:title>
  40626.  <model>
  40627.   <submission action="http://example.com/search"
  40628.               method="post" id="s"/>
  40629.  </model>
  40630. </h:head>
  40631. <h:body>
  40632.  <h:p>
  40633.   <input ref="q"><label>Find</label></input>
  40634.   <submit submission="s"><label>Go</label></submit>
  40635.  </h:p>
  40636. </h:body>
  40637. </h:html></PRE
  40638. ></TD
  40639. ></TR
  40640. ></TABLE
  40641. ></DIV
  40642. ></TD
  40643. ></TR
  40644. ></TABLE
  40645. ><P
  40646. >   The above form displays a text input box (named <VAR
  40647. CLASS="parameter"
  40648. >q</VAR
  40649. >),
  40650.   and a submit button.  When the submit button is clicked, the form will be
  40651.   sent to the page referred to by <VAR
  40652. CLASS="literal"
  40653. >action</VAR
  40654. >.
  40655.  </P
  40656. ><P
  40657. >   Here's where it starts to look different from your web application's point
  40658.   of view.  In a normal HTML form, the data would be sent as 
  40659.   <VAR
  40660. CLASS="literal"
  40661. >application/x-www-form-urlencoded</VAR
  40662. >, in the XForms world
  40663.   however, this information is sent as XML formatted data.
  40664.  </P
  40665. ><P
  40666. >   If you're choosing to work with XForms then you probably want that data as
  40667.   XML, in that case, look in <VAR
  40668. CLASS="varname"
  40669. >$HTTP_RAW_POST_DATA</VAR
  40670. > where
  40671.   you'll find the XML document generated by the browser which you can pass
  40672.   into your favorite XSLT engine or document parser.
  40673.  </P
  40674. ><P
  40675. >   If you're not interested in formatting and just want your data to be loaded
  40676.   into the traditional <VAR
  40677. CLASS="varname"
  40678. >$_POST</VAR
  40679. > variable, you can instruct
  40680.   the client browser to send it as <VAR
  40681. CLASS="literal"
  40682. >application/x-www-form-urlencoded</VAR
  40683. >
  40684.   by changing the <VAR
  40685. CLASS="parameter"
  40686. >method</VAR
  40687. > attribute to
  40688.   <SPAN
  40689. CLASS="emphasis"
  40690. ><I
  40691. CLASS="emphasis"
  40692. >urlencoded-post</I
  40693. ></SPAN
  40694. >.
  40695.  </P
  40696. ><TABLE
  40697. WIDTH="100%"
  40698. BORDER="0"
  40699. CELLPADDING="0"
  40700. CELLSPACING="0"
  40701. CLASS="EXAMPLE"
  40702. ><TR
  40703. ><TD
  40704. ><DIV
  40705. CLASS="example"
  40706. ><A
  40707. NAME="AEN6602"
  40708. ></A
  40709. ><P
  40710. ><B
  40711. >Example 37-2. Using an XForm to populate <VAR
  40712. CLASS="varname"
  40713. >$_POST</VAR
  40714. ></B
  40715. ></P
  40716. ><TABLE
  40717. BORDER="0"
  40718. BGCOLOR="#E0E0E0"
  40719. CELLPADDING="5"
  40720. ><TR
  40721. ><TD
  40722. ><PRE
  40723. CLASS="html"
  40724. ><h:html xmlns:h="http://www.w3.org/1999/xhtml"
  40725.         xmlns="http://www.w3.org/2002/xforms">
  40726. <h:head>
  40727.  <h:title>Search</h:title>
  40728.  <model>
  40729.   <submission action="http://example.com/search"
  40730.               method="urlencoded-post" id="s"/>
  40731.  </model>
  40732. </h:head>
  40733. <h:body>
  40734.  <h:p>
  40735.   <input ref="q"><label>Find</label></input>
  40736.   <submit submission="s"><label>Go</label></submit>
  40737.  </h:p>
  40738. </h:body>
  40739. </h:html></PRE
  40740. ></TD
  40741. ></TR
  40742. ></TABLE
  40743. ></DIV
  40744. ></TD
  40745. ></TR
  40746. ></TABLE
  40747. ><DIV
  40748. CLASS="note"
  40749. ><BLOCKQUOTE
  40750. CLASS="note"
  40751. ><P
  40752. ><B
  40753. >Note: </B
  40754. >
  40755.    As of this writing, many browsers do not support XForms.
  40756.    Check your browser version if the above examples fails.
  40757.   </P
  40758. ></BLOCKQUOTE
  40759. ></DIV
  40760. ></DIV
  40761. ><DIV
  40762. CLASS="chapter"
  40763. ><HR><H1
  40764. ><A
  40765. NAME="features.file-upload"
  40766. >Chapter 38. Handling file uploads</A
  40767. ></H1
  40768. ><DIV
  40769. CLASS="sect1"
  40770. ><H2
  40771. CLASS="sect1"
  40772. ><A
  40773. NAME="features.file-upload.post-method"
  40774. >POST method uploads</A
  40775. ></H2
  40776. ><P
  40777. >     This feature lets people upload both text and binary files. 
  40778.     With PHP's authentication and file manipulation functions,
  40779.     you have full control over who is allowed to upload and
  40780.     what is to be done with the file once it has been uploaded.
  40781.    </P
  40782. ><P
  40783. >     PHP is capable of receiving file uploads from any RFC-1867
  40784.     compliant browser (which includes <SPAN
  40785. CLASS="productname"
  40786. >Netscape Navigator 3</SPAN
  40787.     or later, <SPAN
  40788. CLASS="productname"
  40789. >Microsoft Internet Explorer 3</SPAN
  40790.     with a patch from Microsoft, or later without a patch).
  40791.    </P
  40792. ><DIV
  40793. CLASS="note"
  40794. ><BLOCKQUOTE
  40795. CLASS="note"
  40796. ><P
  40797. ><B
  40798. >Related Configurations Note: </B
  40799. >
  40800.      See also the <A
  40801. HREF="#ini.file-uploads"
  40802. >file_uploads</A
  40803. >, 
  40804.      <A
  40805. HREF="#ini.upload-max-filesize"
  40806. >upload_max_filesize</A
  40807. >,
  40808.      <A
  40809. HREF="#ini.upload-tmp-dir"
  40810. >upload_tmp_dir</A
  40811. >,
  40812.      <A
  40813. HREF="#ini.post-max-size"
  40814. >post_max_size</A
  40815. > and
  40816.      <A
  40817. HREF="#ini.max-input-time"
  40818. >max_input_time</A
  40819. > directives 
  40820.      in <TT
  40821. CLASS="filename"
  40822. >php.ini</TT
  40823. >
  40824.     </P
  40825. ></BLOCKQUOTE
  40826. ></DIV
  40827. ><P
  40828. >     PHP also supports PUT-method file uploads as used by
  40829.     <SPAN
  40830. CLASS="productname"
  40831. >Netscape Composer</SPAN
  40832. > and W3C's 
  40833.     <SPAN
  40834. CLASS="productname"
  40835. >Amaya</SPAN
  40836. > clients.  See the <A
  40837. HREF="#features.file-upload.put-method"
  40838. >PUT Method
  40839.     Support</A
  40840. > for more details.
  40841.    </P
  40842. ><P
  40843. >     <TABLE
  40844. WIDTH="100%"
  40845. BORDER="0"
  40846. CELLPADDING="0"
  40847. CELLSPACING="0"
  40848. CLASS="EXAMPLE"
  40849. ><TR
  40850. ><TD
  40851. ><DIV
  40852. CLASS="example"
  40853. ><A
  40854. NAME="AEN6630"
  40855. ></A
  40856. ><P
  40857. ><B
  40858. >Example 38-1. File Upload Form</B
  40859. ></P
  40860. ><P
  40861. >       A file upload screen can be built by creating a special form which
  40862.       looks something like this:
  40863.      </P
  40864. ><TABLE
  40865. BORDER="0"
  40866. BGCOLOR="#E0E0E0"
  40867. CELLPADDING="5"
  40868. ><TR
  40869. ><TD
  40870. ><PRE
  40871. CLASS="html"
  40872. ><!-- The data encoding type, enctype, MUST be specified as below -->
  40873. <form enctype="multipart/form-data" action="__URL__" method="POST">
  40874.     <!-- MAX_FILE_SIZE must precede the file input field -->
  40875.     <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
  40876.     <!-- Name of input element determines name in $_FILES array -->
  40877.     Send this file: <input name="userfile" type="file" />
  40878.     <input type="submit" value="Send File" />
  40879. </form></PRE
  40880. ></TD
  40881. ></TR
  40882. ></TABLE
  40883. ><P
  40884. >       The <VAR
  40885. CLASS="literal"
  40886. >__URL__</VAR
  40887. > in the above example should be replaced,
  40888.       and point to a PHP file.
  40889.      </P
  40890. ><P
  40891. >       The <VAR
  40892. CLASS="literal"
  40893. >MAX_FILE_SIZE</VAR
  40894. > hidden field (measured in bytes) must
  40895.       precede the file input field, and its value is the maximum filesize accepted.
  40896.       This is an advisory to the browser, PHP also checks it.
  40897.       Fooling this setting on the browser side is quite easy, so never rely
  40898.       on files with a greater size being blocked by this feature.
  40899.       The PHP settings for maximum-size, however, cannot be fooled.
  40900.       This form element should always be used as it
  40901.       saves users the trouble of waiting for a big file being transferred only
  40902.       to find that it was too big and the transfer failed.
  40903.      </P
  40904. ></DIV
  40905. ></TD
  40906. ></TR
  40907. ></TABLE
  40908. >
  40909.    </P
  40910. ><DIV
  40911. CLASS="note"
  40912. ><BLOCKQUOTE
  40913. CLASS="note"
  40914. ><P
  40915. ><B
  40916. >Note: </B
  40917. >
  40918.      Be sure your file upload form has attribute <VAR
  40919. CLASS="literal"
  40920. >enctype="multipart/form-data"</VAR
  40921. >
  40922.      otherwise the file upload will not work.
  40923.     </P
  40924. ></BLOCKQUOTE
  40925. ></DIV
  40926. ><P
  40927. >     The global <A
  40928. HREF="#reserved.variables.files"
  40929. >$_FILES</A
  40930. >
  40931.     exists as of PHP 4.1.0 (Use <VAR
  40932. CLASS="varname"
  40933. >$HTTP_POST_FILES</VAR
  40934. >
  40935.     instead if using an earlier version).
  40936.     These arrays will contain all the uploaded file information.
  40937.    </P
  40938. ><P
  40939. >     The contents of <A
  40940. HREF="#reserved.variables.files"
  40941. >$_FILES</A
  40942. >
  40943.     from the example form is as follows. Note that this assumes the use of 
  40944.     the file upload name <SPAN
  40945. CLASS="emphasis"
  40946. ><I
  40947. CLASS="emphasis"
  40948. >userfile</I
  40949. ></SPAN
  40950. >, as used in the example 
  40951.     script above. This can be any name.
  40952.     <P
  40953. ></P
  40954. ><DIV
  40955. CLASS="variablelist"
  40956. ><DL
  40957. ><DT
  40958. ><VAR
  40959. CLASS="varname"
  40960. >$_FILES['userfile']['name']</VAR
  40961. ></DT
  40962. ><DD
  40963. ><P
  40964. >         The original name of the file on the client machine.
  40965.        </P
  40966. ></DD
  40967. ><DT
  40968. ><VAR
  40969. CLASS="varname"
  40970. >$_FILES['userfile']['type']</VAR
  40971. ></DT
  40972. ><DD
  40973. ><P
  40974. >         The mime type of the file, if the browser provided this
  40975.         information.  An example would be
  40976.         <VAR
  40977. CLASS="literal"
  40978. >"image/gif"</VAR
  40979. >.
  40980.        </P
  40981. ></DD
  40982. ><DT
  40983. ><VAR
  40984. CLASS="varname"
  40985. >$_FILES['userfile']['size']</VAR
  40986. ></DT
  40987. ><DD
  40988. ><P
  40989. >         The size, in bytes, of the uploaded file.
  40990.        </P
  40991. ></DD
  40992. ><DT
  40993. ><VAR
  40994. CLASS="varname"
  40995. >$_FILES['userfile']['tmp_name']</VAR
  40996. ></DT
  40997. ><DD
  40998. ><P
  40999. >         The temporary filename of the file in which the uploaded file
  41000.         was stored on the server.
  41001.        </P
  41002. ></DD
  41003. ><DT
  41004. ><VAR
  41005. CLASS="varname"
  41006. >$_FILES['userfile']['error']</VAR
  41007. ></DT
  41008. ><DD
  41009. ><P
  41010. >         The <A
  41011. HREF="#features.file-upload.errors"
  41012. >error code</A
  41013. >
  41014.         associated with this file upload. This element was added in PHP 4.2.0
  41015.        </P
  41016. ></DD
  41017. ></DL
  41018. ></DIV
  41019. >
  41020.    </P
  41021. ><P
  41022. >     Files will, by default be stored in the server's default temporary
  41023.     directory, unless another location has been given with the <A
  41024. HREF="#ini.upload-tmp-dir"
  41025. >upload_tmp_dir</A
  41026. > directive in
  41027.     <TT
  41028. CLASS="filename"
  41029. >php.ini</TT
  41030. >. The server's default directory can
  41031.     be changed by setting the environment variable
  41032.     <VAR
  41033. CLASS="envar"
  41034. >TMPDIR</VAR
  41035. > in the environment in which PHP runs.
  41036.     Setting it using <A
  41037. HREF="#function.putenv"
  41038. ><B
  41039. CLASS="function"
  41040. >putenv()</B
  41041. ></A
  41042. > from within a PHP
  41043.     script will not work. This environment variable can also be used
  41044.     to make sure that other operations are working on uploaded files,
  41045.     as well.
  41046.     <TABLE
  41047. WIDTH="100%"
  41048. BORDER="0"
  41049. CELLPADDING="0"
  41050. CELLSPACING="0"
  41051. CLASS="EXAMPLE"
  41052. ><TR
  41053. ><TD
  41054. ><DIV
  41055. CLASS="example"
  41056. ><A
  41057. NAME="AEN6680"
  41058. ></A
  41059. ><P
  41060. ><B
  41061. >Example 38-2. Validating file uploads</B
  41062. ></P
  41063. ><P
  41064. >       See also the function entries for <A
  41065. HREF="#function.is-uploaded-file"
  41066. ><B
  41067. CLASS="function"
  41068. >is_uploaded_file()</B
  41069. ></A
  41070.       and <A
  41071. HREF="#function.move-uploaded-file"
  41072. ><B
  41073. CLASS="function"
  41074. >move_uploaded_file()</B
  41075. ></A
  41076. > for further information. The
  41077.       following example will process the file upload that came from a form.
  41078.      </P
  41079. ><TABLE
  41080. BORDER="0"
  41081. BGCOLOR="#E0E0E0"
  41082. CELLPADDING="5"
  41083. ><TR
  41084. ><TD
  41085. ><PRE
  41086. CLASS="php"
  41087. ><?php
  41088. // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
  41089. // of $_FILES.
  41090.  
  41091. $uploaddir = '/var/www/uploads/';
  41092. $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
  41093.  
  41094. echo '<pre>';
  41095. if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  41096.     echo "File is valid, and was successfully uploaded.\n";
  41097. } else {
  41098.     echo "Possible file upload attack!\n";
  41099. }
  41100.  
  41101. echo 'Here is some more debugging info:';
  41102. print_r($_FILES);
  41103.  
  41104. print "</pre>";
  41105.  
  41106. ?></PRE
  41107. ></TD
  41108. ></TR
  41109. ></TABLE
  41110. ></DIV
  41111. ></TD
  41112. ></TR
  41113. ></TABLE
  41114. >
  41115.    </P
  41116. ><P
  41117. >     The PHP script which receives the uploaded file should implement
  41118.     whatever logic is necessary for determining what should be done
  41119.     with the uploaded file. You can, for example, use the
  41120.     <VAR
  41121. CLASS="varname"
  41122. >$_FILES['userfile']['size']</VAR
  41123. > variable
  41124.     to throw away any files that are either too small or too big.  You
  41125.     could use the
  41126.     <VAR
  41127. CLASS="varname"
  41128. >$_FILES['userfile']['type']</VAR
  41129. > variable
  41130.     to throw away any files that didn't match a certain type criteria.
  41131.     As of PHP 4.2.0, you could use <VAR
  41132. CLASS="varname"
  41133. >$_FILES['userfile']['error']</VAR
  41134.     and plan your logic according to the <A
  41135. HREF="#features.file-upload.errors"
  41136. >error codes</A
  41137. >.
  41138.     Whatever the logic, you should either delete the file from the
  41139.     temporary directory or move it elsewhere.
  41140.    </P
  41141. ><P
  41142. >     If no file is selected for upload in your form, PHP will return
  41143.     <VAR
  41144. CLASS="varname"
  41145. >$_FILES['userfile']['size']</VAR
  41146. > as 0, and
  41147.     <VAR
  41148. CLASS="varname"
  41149. >$_FILES['userfile']['tmp_name']</VAR
  41150. > as none.
  41151.    </P
  41152. ><P
  41153. >     The file will be deleted from the temporary directory at the end
  41154.     of the request if it has not been moved away or renamed.
  41155.    </P
  41156. ></DIV
  41157. ><DIV
  41158. CLASS="sect1"
  41159. ><HR><H2
  41160. CLASS="sect1"
  41161. ><A
  41162. NAME="features.file-upload.errors"
  41163. >Error Messages Explained</A
  41164. ></H2
  41165. ><P
  41166. >     Since PHP 4.2.0, PHP returns an appropriate error code along with the 
  41167.     file array.  The error code can be found in the 
  41168.     <VAR
  41169. CLASS="literal"
  41170. >error</VAR
  41171. > segment of the file array that is created 
  41172.     during the file upload by PHP. In other words, the error might be 
  41173.     found in <VAR
  41174. CLASS="varname"
  41175. >$_FILES['userfile']['error']</VAR
  41176. >.
  41177.    </P
  41178. ><P
  41179. >     <P
  41180. ></P
  41181. ><DIV
  41182. CLASS="variablelist"
  41183. ><DL
  41184. ><DT
  41185. ><TT
  41186. CLASS="constant"
  41187. ><B
  41188. >UPLOAD_ERR_OK</B
  41189. ></TT
  41190. ></DT
  41191. ><DD
  41192. ><P
  41193. >         Value: 0; There is no error, the file uploaded with success.
  41194.        </P
  41195. ></DD
  41196. ><DT
  41197. ><TT
  41198. CLASS="constant"
  41199. ><B
  41200. >UPLOAD_ERR_INI_SIZE</B
  41201. ></TT
  41202. ></DT
  41203. ><DD
  41204. ><P
  41205. >         Value: 1; The uploaded file exceeds the
  41206.         <A
  41207. HREF="#ini.upload-max-filesize"
  41208. >upload_max_filesize</A
  41209.         directive in <TT
  41210. CLASS="filename"
  41211. >php.ini</TT
  41212. >.
  41213.        </P
  41214. ></DD
  41215. ><DT
  41216. ><TT
  41217. CLASS="constant"
  41218. ><B
  41219. >UPLOAD_ERR_FORM_SIZE</B
  41220. ></TT
  41221. ></DT
  41222. ><DD
  41223. ><P
  41224. >         Value: 2; The uploaded file exceeds the <SPAN
  41225. CLASS="emphasis"
  41226. ><I
  41227. CLASS="emphasis"
  41228. >MAX_FILE_SIZE</I
  41229. ></SPAN
  41230.         directive that was specified in the HTML form.
  41231.        </P
  41232. ></DD
  41233. ><DT
  41234. ><TT
  41235. CLASS="constant"
  41236. ><B
  41237. >UPLOAD_ERR_PARTIAL</B
  41238. ></TT
  41239. ></DT
  41240. ><DD
  41241. ><P
  41242. >         Value: 3; The uploaded file was only partially uploaded.
  41243.        </P
  41244. ></DD
  41245. ><DT
  41246. ><TT
  41247. CLASS="constant"
  41248. ><B
  41249. >UPLOAD_ERR_NO_FILE</B
  41250. ></TT
  41251. ></DT
  41252. ><DD
  41253. ><P
  41254. >         Value: 4; No file was uploaded.
  41255.        </P
  41256. ></DD
  41257. ><DT
  41258. ><TT
  41259. CLASS="constant"
  41260. ><B
  41261. >UPLOAD_ERR_NO_TMP_DIR</B
  41262. ></TT
  41263. ></DT
  41264. ><DD
  41265. ><P
  41266. >         Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP
  41267.         5.0.3.
  41268.        </P
  41269. ></DD
  41270. ></DL
  41271. ></DIV
  41272. >
  41273.    </P
  41274. ><DIV
  41275. CLASS="note"
  41276. ><BLOCKQUOTE
  41277. CLASS="note"
  41278. ><P
  41279. ><B
  41280. >Note: </B
  41281. >
  41282.      These became PHP constants in PHP 4.3.0.
  41283.     </P
  41284. ></BLOCKQUOTE
  41285. ></DIV
  41286. ></DIV
  41287. ><DIV
  41288. CLASS="sect1"
  41289. ><HR><H2
  41290. CLASS="sect1"
  41291. ><A
  41292. NAME="features.file-upload.common-pitfalls"
  41293. >Common Pitfalls</A
  41294. ></H2
  41295. ><P
  41296. >     The <VAR
  41297. CLASS="literal"
  41298. >MAX_FILE_SIZE</VAR
  41299. > item cannot specify a file size 
  41300.     greater than the file size that has been set in the <A
  41301. HREF="#ini.upload-max-filesize"
  41302. >upload_max_filesize</A
  41303. > ini-setting.
  41304.     The default is 2 Megabytes.
  41305.    </P
  41306. ><P
  41307. >     If a memory limit is enabled, a larger <A
  41308. HREF="#ini.memory-limit"
  41309. >memory_limit</A
  41310. > may be needed. Make
  41311.     sure you set <A
  41312. HREF="#ini.memory-limit"
  41313. >memory_limit</A
  41314. >
  41315.     large enough.
  41316.    </P
  41317. ><P
  41318. >     If <A
  41319. HREF="#ini.max-execution-time"
  41320. >max_execution_time</A
  41321. >
  41322.     is set too small, script execution may be exceeded by the value. Make
  41323.     sure you set <VAR
  41324. CLASS="literal"
  41325. >max_execution_time</VAR
  41326. > large enough.
  41327.    </P
  41328. ><DIV
  41329. CLASS="note"
  41330. ><BLOCKQUOTE
  41331. CLASS="note"
  41332. ><P
  41333. ><B
  41334. >Note: </B
  41335. >
  41336.      <A
  41337. HREF="#ini.max-execution-time"
  41338. >max_execution_time</A
  41339. > only
  41340.      affects the execution time of the script itself.  Any time spent
  41341.      on activity that happens outside the execution of the script
  41342.      such as system calls using <A
  41343. HREF="#function.system"
  41344. ><B
  41345. CLASS="function"
  41346. >system()</B
  41347. ></A
  41348. >, the
  41349.      <A
  41350. HREF="#function.sleep"
  41351. ><B
  41352. CLASS="function"
  41353. >sleep()</B
  41354. ></A
  41355. > function, database queries, time taken by
  41356.      the file upload process, etc. is not included when determining the maximum
  41357.      time that the script has been running.
  41358.     </P
  41359. ></BLOCKQUOTE
  41360. ></DIV
  41361. ><DIV
  41362. CLASS="warning"
  41363. ><P
  41364. ></P
  41365. ><TABLE
  41366. CLASS="warning"
  41367. BORDER="1"
  41368. WIDTH="100%"
  41369. ><TR
  41370. ><TD
  41371. ALIGN="CENTER"
  41372. ><B
  41373. >Warning</B
  41374. ></TD
  41375. ></TR
  41376. ><TR
  41377. ><TD
  41378. ALIGN="LEFT"
  41379. ><P
  41380. >      <A
  41381. HREF="#ini.max-input-time"
  41382. >max_input_time</A
  41383. > sets the maximum
  41384.      time, in seconds, the script is allowed to receive input; this includes
  41385.      file uploads.  For large or multiple files, or users on slower connections,
  41386.      the default of <VAR
  41387. CLASS="literal"
  41388. >60 seconds</VAR
  41389. > may be exceeded.
  41390.     </P
  41391. ></TD
  41392. ></TR
  41393. ></TABLE
  41394. ></DIV
  41395. ><P
  41396. >     If <A
  41397. HREF="#ini.post-max-size"
  41398. >post_max_size</A
  41399. > is set too
  41400.     small, large files cannot be uploaded.  Make sure you set
  41401.     <VAR
  41402. CLASS="literal"
  41403. >post_max_size</VAR
  41404. > large enough.
  41405.    </P
  41406. ><P
  41407. >     Not validating which file you operate on may mean that users can access
  41408.     sensitive information in other directories.
  41409.    </P
  41410. ><P
  41411. >     Please note that the <SPAN
  41412. CLASS="productname"
  41413. >CERN httpd</SPAN
  41414. > seems to strip off everything
  41415.     starting at the first whitespace in the content-type mime header
  41416.     it gets from the client.  As long as this is the case, <SPAN
  41417. CLASS="productname"
  41418. >CERN httpd</SPAN
  41419. >
  41420.     will not support the file upload feature.
  41421.    </P
  41422. ><P
  41423. >     Due to the large amount of directory listing styles we cannot guarantee
  41424.     that files with exotic names (like containing spaces) are handled properly.
  41425.    </P
  41426. ><P
  41427. >     A developer may not mix normal input fields and file upload fields in the same
  41428.     form variable (by using an input name like <VAR
  41429. CLASS="literal"
  41430. >foo[]</VAR
  41431. >).
  41432.    </P
  41433. ></DIV
  41434. ><DIV
  41435. CLASS="sect1"
  41436. ><HR><H2
  41437. CLASS="sect1"
  41438. ><A
  41439. NAME="features.file-upload.multiple"
  41440. >Uploading multiple files</A
  41441. ></H2
  41442. ><P
  41443. >     Multiple files can be uploaded using different
  41444.     <VAR
  41445. CLASS="literal"
  41446. >name</VAR
  41447. > for <VAR
  41448. CLASS="literal"
  41449. >input</VAR
  41450. >.
  41451.    </P
  41452. ><P
  41453. >     It is also possible to upload multiple files simultaneously and
  41454.     have the information organized automatically in arrays for you. To
  41455.     do so, you need to use the same array submission syntax in the
  41456.     HTML form as you do with multiple selects and checkboxes:
  41457.    </P
  41458. ><DIV
  41459. CLASS="note"
  41460. ><BLOCKQUOTE
  41461. CLASS="note"
  41462. ><P
  41463. ><B
  41464. >Note: </B
  41465. >
  41466.      Support for multiple file uploads was added in PHP 3.0.10.
  41467.     </P
  41468. ></BLOCKQUOTE
  41469. ></DIV
  41470. ><P
  41471. >     <TABLE
  41472. WIDTH="100%"
  41473. BORDER="0"
  41474. CELLPADDING="0"
  41475. CELLSPACING="0"
  41476. CLASS="EXAMPLE"
  41477. ><TR
  41478. ><TD
  41479. ><DIV
  41480. CLASS="example"
  41481. ><A
  41482. NAME="AEN6776"
  41483. ></A
  41484. ><P
  41485. ><B
  41486. >Example 38-3. Uploading multiple files</B
  41487. ></P
  41488. ><TABLE
  41489. BORDER="0"
  41490. BGCOLOR="#E0E0E0"
  41491. CELLPADDING="5"
  41492. ><TR
  41493. ><TD
  41494. ><PRE
  41495. CLASS="html"
  41496. ><form action="file-upload.php" method="post" enctype="multipart/form-data">
  41497.   Send these files:<br />
  41498.   <input name="userfile[]" type="file" /><br />
  41499.   <input name="userfile[]" type="file" /><br />
  41500.   <input type="submit" value="Send files" />
  41501. </form></PRE
  41502. ></TD
  41503. ></TR
  41504. ></TABLE
  41505. ></DIV
  41506. ></TD
  41507. ></TR
  41508. ></TABLE
  41509. >
  41510.    </P
  41511. ><P
  41512. >     When the above form is submitted, the arrays
  41513.     <VAR
  41514. CLASS="varname"
  41515. >$_FILES['userfile']</VAR
  41516. >,
  41517.     <VAR
  41518. CLASS="varname"
  41519. >$_FILES['userfile']['name']</VAR
  41520. >, and
  41521.     <VAR
  41522. CLASS="varname"
  41523. >$_FILES['userfile']['size']</VAR
  41524. > will be
  41525.     initialized (as well as in <VAR
  41526. CLASS="varname"
  41527. >$HTTP_POST_FILES</VAR
  41528. > for PHP versions prior
  41529.     to 4.1.0).
  41530.     When
  41531.     <A
  41532. HREF="#ini.register-globals"
  41533. >register_globals</A
  41534. > is on, globals for uploaded
  41535.     files are also initialized. Each of these will be a numerically
  41536.     indexed array of the appropriate values for the submitted files.
  41537.    </P
  41538. ><P
  41539. >     For instance, assume that the filenames
  41540.     <TT
  41541. CLASS="filename"
  41542. >/home/test/review.html</TT
  41543. > and
  41544.     <TT
  41545. CLASS="filename"
  41546. >/home/test/xwp.out</TT
  41547. > are submitted.  In this
  41548.     case, <VAR
  41549. CLASS="varname"
  41550. >$_FILES['userfile']['name'][0]</VAR
  41551. >
  41552.     would contain the value <TT
  41553. CLASS="filename"
  41554. >review.html</TT
  41555. >, and
  41556.     <VAR
  41557. CLASS="varname"
  41558. >$_FILES['userfile']['name'][1]</VAR
  41559. > would
  41560.     contain the value <TT
  41561. CLASS="filename"
  41562. >xwp.out</TT
  41563. >. Similarly,
  41564.     <VAR
  41565. CLASS="varname"
  41566. >$_FILES['userfile']['size'][0]</VAR
  41567. > would
  41568.     contain <TT
  41569. CLASS="filename"
  41570. >review.html</TT
  41571. >'s file size, and so forth.
  41572.    </P
  41573. ><P
  41574. >     <VAR
  41575. CLASS="varname"
  41576. >$_FILES['userfile']['name'][0]</VAR
  41577. >,
  41578.     <VAR
  41579. CLASS="varname"
  41580. >$_FILES['userfile']['tmp_name'][0]</VAR
  41581. >,
  41582.     <VAR
  41583. CLASS="varname"
  41584. >$_FILES['userfile']['size'][0]</VAR
  41585. >, and
  41586.     <VAR
  41587. CLASS="varname"
  41588. >$_FILES['userfile']['type'][0]</VAR
  41589. > are
  41590.     also set.
  41591.    </P
  41592. ></DIV
  41593. ><DIV
  41594. CLASS="sect1"
  41595. ><HR><H2
  41596. CLASS="sect1"
  41597. ><A
  41598. NAME="features.file-upload.put-method"
  41599. >PUT method support</A
  41600. ></H2
  41601. ><P
  41602. >     PUT method support has changed between PHP 3 and PHP 4.
  41603.     In PHP 4, one should use the standard input stream to read
  41604.     the contents of an HTTP PUT.
  41605.    </P
  41606. ><P
  41607. >     <TABLE
  41608. WIDTH="100%"
  41609. BORDER="0"
  41610. CELLPADDING="0"
  41611. CELLSPACING="0"
  41612. CLASS="EXAMPLE"
  41613. ><TR
  41614. ><TD
  41615. ><DIV
  41616. CLASS="example"
  41617. ><A
  41618. NAME="AEN6803"
  41619. ></A
  41620. ><P
  41621. ><B
  41622. >Example 38-4. Saving HTTP PUT files with PHP 4</B
  41623. ></P
  41624. ><TABLE
  41625. BORDER="0"
  41626. BGCOLOR="#E0E0E0"
  41627. CELLPADDING="5"
  41628. ><TR
  41629. ><TD
  41630. ><PRE
  41631. CLASS="php"
  41632. ><?php
  41633. /* PUT data comes in on the stdin stream */
  41634. $putdata = fopen("php://stdin", "r");
  41635.  
  41636. /* Open a file for writing */
  41637. $fp = fopen("myputfile.ext", "w");
  41638.  
  41639. /* Read the data 1 KB at a time
  41640.    and write to the file */
  41641. while ($data = fread($putdata, 1024))
  41642.   fwrite($fp, $data);
  41643.  
  41644. /* Close the streams */
  41645. fclose($fp);
  41646. fclose($putdata);
  41647. ?></PRE
  41648. ></TD
  41649. ></TR
  41650. ></TABLE
  41651. ></DIV
  41652. ></TD
  41653. ></TR
  41654. ></TABLE
  41655. >
  41656.    </P
  41657. ><DIV
  41658. CLASS="note"
  41659. ><BLOCKQUOTE
  41660. CLASS="note"
  41661. ><P
  41662. ><B
  41663. >Note: </B
  41664. >
  41665.      All documentation below applies to PHP 3 only.
  41666.     </P
  41667. ></BLOCKQUOTE
  41668. ></DIV
  41669. ><P
  41670. >     PHP provides support for the HTTP PUT method used by clients such
  41671.     as <SPAN
  41672. CLASS="productname"
  41673. >Netscape Composer</SPAN
  41674. > and W3C <SPAN
  41675. CLASS="productname"
  41676. >Amaya</SPAN
  41677. >.  
  41678.     PUT requests are much simpler
  41679.     than a file upload and they look something like this:
  41680.     <DIV
  41681. CLASS="informalexample"
  41682. ><P
  41683. ></P
  41684. ><A
  41685. NAME="AEN6811"
  41686. ></A
  41687. ><TABLE
  41688. BORDER="0"
  41689. BGCOLOR="#E0E0E0"
  41690. CELLPADDING="5"
  41691. ><TR
  41692. ><TD
  41693. ><PRE
  41694. CLASS="programlisting"
  41695. >PUT /path/filename.html HTTP/1.1</PRE
  41696. ></TD
  41697. ></TR
  41698. ></TABLE
  41699. ><P
  41700. ></P
  41701. ></DIV
  41702. >
  41703.    </P
  41704. ><P
  41705. >     This would normally mean that the remote client would like to save
  41706.     the content that follows as: <TT
  41707. CLASS="filename"
  41708. >/path/filename.html</TT
  41709. > in your web tree.
  41710.     It is obviously not a good idea for Apache or PHP to automatically
  41711.     let everybody overwrite any files in your web tree.  So, to handle
  41712.     such a request you have to first tell your web server that you
  41713.     want a certain PHP script to handle the request.  In Apache you do
  41714.     this with the <SPAN
  41715. CLASS="emphasis"
  41716. ><I
  41717. CLASS="emphasis"
  41718. >Script</I
  41719. ></SPAN
  41720. > directive.  It can be
  41721.     placed almost anywhere in your Apache configuration file.  A
  41722.     common place is inside a <Directory> block or perhaps inside
  41723.     a <Virtualhost> block.  A line like this would do the trick:
  41724.     <DIV
  41725. CLASS="informalexample"
  41726. ><P
  41727. ></P
  41728. ><A
  41729. NAME="AEN6816"
  41730. ></A
  41731. ><TABLE
  41732. BORDER="0"
  41733. BGCOLOR="#E0E0E0"
  41734. CELLPADDING="5"
  41735. ><TR
  41736. ><TD
  41737. ><PRE
  41738. CLASS="programlisting"
  41739. >Script PUT /put.php</PRE
  41740. ></TD
  41741. ></TR
  41742. ></TABLE
  41743. ><P
  41744. ></P
  41745. ></DIV
  41746. >
  41747.    </P
  41748. ><P
  41749. >     This tells Apache to send all PUT requests for URIs that match the
  41750.     context in which you put this line to the put.php script.  This
  41751.     assumes, of course, that you have PHP enabled for the .php
  41752.     extension and PHP is active.
  41753.    </P
  41754. ><P
  41755. >     Inside your put.php file you would then do something like this:
  41756.    </P
  41757. ><P
  41758. >     <DIV
  41759. CLASS="informalexample"
  41760. ><P
  41761. ></P
  41762. ><A
  41763. NAME="AEN6821"
  41764. ></A
  41765. ><TABLE
  41766. BORDER="0"
  41767. BGCOLOR="#E0E0E0"
  41768. CELLPADDING="5"
  41769. ><TR
  41770. ><TD
  41771. ><PRE
  41772. CLASS="php"
  41773. ><?php copy($PHP_UPLOADED_FILE_NAME, $DOCUMENT_ROOT . $REQUEST_URI); ?></PRE
  41774. ></TD
  41775. ></TR
  41776. ></TABLE
  41777. ><P
  41778. ></P
  41779. ></DIV
  41780. >
  41781.    </P
  41782. ><P
  41783. >     This would copy the file to the location requested by the remote
  41784.     client.  You would probably want to perform some checks and/or
  41785.     authenticate the user before performing this file copy.  The only
  41786.     trick here is that when PHP sees a PUT-method request it stores
  41787.     the uploaded file in a temporary file just like those handled by
  41788.     the <A
  41789. HREF="#features.file-upload.post-method"
  41790. >POST-method</A
  41791. >.
  41792.     When the request ends, this temporary file is deleted.  So, your
  41793.     PUT handling PHP script has to copy that file somewhere.  The
  41794.     filename of this temporary file is in the <VAR
  41795. CLASS="varname"
  41796. >$PHP_PUT_FILENAME</VAR
  41797. >
  41798.     variable, and you can see the suggested destination filename in
  41799.     the <VAR
  41800. CLASS="varname"
  41801. >$REQUEST_URI</VAR
  41802. > (may vary on non-Apache web servers).  This
  41803.     destination filename is the one that the remote client specified.
  41804.     You do not have to listen to this client.  You could, for example,
  41805.     copy all uploaded files to a special uploads directory.
  41806.    </P
  41807. ></DIV
  41808. ></DIV
  41809. ><DIV
  41810. CLASS="chapter"
  41811. ><HR><H1
  41812. ><A
  41813. NAME="features.remote-files"
  41814. >Chapter 39. Using remote files</A
  41815. ></H1
  41816. ><P
  41817. >    As long as <VAR
  41818. CLASS="option"
  41819. >allow_url_fopen</VAR
  41820. > is enabled in
  41821.    <TT
  41822. CLASS="filename"
  41823. >php.ini</TT
  41824. >, you can use <ACRONYM
  41825. CLASS="acronym"
  41826. >HTTP</ACRONYM
  41827. > and <ACRONYM
  41828. CLASS="acronym"
  41829. >FTP</ACRONYM
  41830.    URLs with most of the functions
  41831.    that take a filename as a parameter.  In addition, URLs can be
  41832.    used with the <A
  41833. HREF="#function.include"
  41834. ><B
  41835. CLASS="function"
  41836. >include()</B
  41837. ></A
  41838. >,
  41839.    <A
  41840. HREF="#function.include-once"
  41841. ><B
  41842. CLASS="function"
  41843. >include_once()</B
  41844. ></A
  41845. >, <A
  41846. HREF="#function.require"
  41847. ><B
  41848. CLASS="function"
  41849. >require()</B
  41850. ></A
  41851. > and
  41852.    <A
  41853. HREF="#function.require-once"
  41854. ><B
  41855. CLASS="function"
  41856. >require_once()</B
  41857. ></A
  41858. > statements.
  41859.    See <A
  41860. HREF="#wrappers"
  41861. >Appendix L</A
  41862. > for more information about the protocols
  41863.    supported by PHP.
  41864.   </P
  41865. ><DIV
  41866. CLASS="note"
  41867. ><BLOCKQUOTE
  41868. CLASS="note"
  41869. ><P
  41870. ><B
  41871. >Note: </B
  41872. >
  41873.    In PHP 4.0.3 and older, in order to use URL wrappers, you were required
  41874.    to configure PHP using the configure option
  41875.    <VAR
  41876. CLASS="option"
  41877. >--enable-url-fopen-wrapper</VAR
  41878. >.
  41879.    </P
  41880. ></BLOCKQUOTE
  41881. ></DIV
  41882. ><P
  41883. >    <DIV
  41884. CLASS="note"
  41885. ><BLOCKQUOTE
  41886. CLASS="note"
  41887. ><P
  41888. ><B
  41889. >Note: </B
  41890. >
  41891.      The Windows versions of PHP earlier than PHP 4.3
  41892.      did not support remote file accessing for the following functions:
  41893.      <A
  41894. HREF="#function.include"
  41895. ><B
  41896. CLASS="function"
  41897. >include()</B
  41898. ></A
  41899. >, <A
  41900. HREF="#function.include-once"
  41901. ><B
  41902. CLASS="function"
  41903. >include_once()</B
  41904. ></A
  41905. >,
  41906.      <A
  41907. HREF="#function.require"
  41908. ><B
  41909. CLASS="function"
  41910. >require()</B
  41911. ></A
  41912. >, <A
  41913. HREF="#function.require-once"
  41914. ><B
  41915. CLASS="function"
  41916. >require_once()</B
  41917. ></A
  41918. >,
  41919.      and the imagecreatefromXXX functions in the <A
  41920. HREF="#ref.image"
  41921. >Reference XLVI, <I
  41922. >Image Functions</I
  41923. ></A
  41924. >
  41925.      extension.
  41926.     </P
  41927. ></BLOCKQUOTE
  41928. ></DIV
  41929. >
  41930.   </P
  41931. ><P
  41932. >    For example, you can use this to open a file on a remote web server,
  41933.    parse the output for the data you want, and then use that data in a
  41934.    database query, or simply to output it in a style matching the rest
  41935.    of your website.
  41936.   </P
  41937. ><P
  41938. >    <TABLE
  41939. WIDTH="100%"
  41940. BORDER="0"
  41941. CELLPADDING="0"
  41942. CELLSPACING="0"
  41943. CLASS="EXAMPLE"
  41944. ><TR
  41945. ><TD
  41946. ><DIV
  41947. CLASS="example"
  41948. ><A
  41949. NAME="AEN6852"
  41950. ></A
  41951. ><P
  41952. ><B
  41953. >Example 39-1. Getting the title of a remote page</B
  41954. ></P
  41955. ><TABLE
  41956. BORDER="0"
  41957. BGCOLOR="#E0E0E0"
  41958. CELLPADDING="5"
  41959. ><TR
  41960. ><TD
  41961. ><PRE
  41962. CLASS="php"
  41963. ><?php
  41964. $file = fopen ("http://www.example.com/", "r");
  41965. if (!$file) {
  41966.     echo "<p>Unable to open remote file.\n";
  41967.     exit;
  41968. }
  41969. while (!feof ($file)) {
  41970.     $line = fgets ($file, 1024);
  41971.     /* This only works if the title and its tags are on one line */
  41972.     if (eregi ("<title>(.*)</title>", $line, $out)) {
  41973.         $title = $out[1];
  41974.         break;
  41975.     }
  41976. }
  41977. fclose($file);
  41978. ?></PRE
  41979. ></TD
  41980. ></TR
  41981. ></TABLE
  41982. ></DIV
  41983. ></TD
  41984. ></TR
  41985. ></TABLE
  41986. >
  41987.   </P
  41988. ><P
  41989. >    You can also write to files on an FTP server (provided that you
  41990.    have connected as a user with the correct access rights). You
  41991.    can only create new files using this method; if you try to overwrite
  41992.    a file that already exists, the <A
  41993. HREF="#function.fopen"
  41994. ><B
  41995. CLASS="function"
  41996. >fopen()</B
  41997. ></A
  41998. > call will
  41999.    fail.
  42000.   </P
  42001. ><P
  42002. >    To connect as a user other than 'anonymous', you need to specify
  42003.    the username (and possibly password) within the URL, such as
  42004.    'ftp://user:password@ftp.example.com/path/to/file'. (You can use the
  42005.    same sort of syntax to access files via HTTP when they require Basic
  42006.    authentication.)
  42007.   </P
  42008. ><P
  42009. >    <TABLE
  42010. WIDTH="100%"
  42011. BORDER="0"
  42012. CELLPADDING="0"
  42013. CELLSPACING="0"
  42014. CLASS="EXAMPLE"
  42015. ><TR
  42016. ><TD
  42017. ><DIV
  42018. CLASS="example"
  42019. ><A
  42020. NAME="AEN6859"
  42021. ></A
  42022. ><P
  42023. ><B
  42024. >Example 39-2. Storing data on a remote server</B
  42025. ></P
  42026. ><TABLE
  42027. BORDER="0"
  42028. BGCOLOR="#E0E0E0"
  42029. CELLPADDING="5"
  42030. ><TR
  42031. ><TD
  42032. ><PRE
  42033. CLASS="php"
  42034. ><?php
  42035. $file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w");
  42036. if (!$file) {
  42037.     echo "<p>Unable to open remote file for writing.\n";
  42038.     exit;
  42039. }
  42040. /* Write the data here. */
  42041. fwrite ($file, $_SERVER['HTTP_USER_AGENT'] . "\n");
  42042. fclose ($file);
  42043. ?></PRE
  42044. ></TD
  42045. ></TR
  42046. ></TABLE
  42047. ></DIV
  42048. ></TD
  42049. ></TR
  42050. ></TABLE
  42051. >
  42052.   </P
  42053. ><P
  42054. >    <DIV
  42055. CLASS="note"
  42056. ><BLOCKQUOTE
  42057. CLASS="note"
  42058. ><P
  42059. ><B
  42060. >Note: </B
  42061. >
  42062.      You might get the idea from the example above that you can use
  42063.      this technique to write to a remote log file. Unfortunately
  42064.      that would not work because the <A
  42065. HREF="#function.fopen"
  42066. ><B
  42067. CLASS="function"
  42068. >fopen()</B
  42069. ></A
  42070. > call will
  42071.      fail if the remote file already exists. To do distributed logging
  42072.      like that, you should take a look at <A
  42073. HREF="#function.syslog"
  42074. ><B
  42075. CLASS="function"
  42076. >syslog()</B
  42077. ></A
  42078. >.
  42079.     </P
  42080. ></BLOCKQUOTE
  42081. ></DIV
  42082. >
  42083.   </P
  42084. ></DIV
  42085. ><DIV
  42086. CLASS="chapter"
  42087. ><HR><H1
  42088. ><A
  42089. NAME="features.connection-handling"
  42090. >Chapter 40. Connection handling</A
  42091. ></H1
  42092. ><DIV
  42093. CLASS="note"
  42094. ><BLOCKQUOTE
  42095. CLASS="note"
  42096. ><P
  42097. ><B
  42098. >Note: </B
  42099. >The following applies to 3.0.7 and later.</P
  42100. ></BLOCKQUOTE
  42101. ></DIV
  42102. ><P
  42103. >    Internally in PHP a connection status is maintained.  There are 3
  42104.    possible states:
  42105.    <P
  42106. ></P
  42107. ><UL
  42108. ><LI
  42109. ><P
  42110. >0 - NORMAL</P
  42111. ></LI
  42112. ><LI
  42113. ><P
  42114. >1 - ABORTED</P
  42115. ></LI
  42116. ><LI
  42117. ><P
  42118. >2 - TIMEOUT</P
  42119. ></LI
  42120. ></UL
  42121. >
  42122.   </P
  42123. ><P
  42124. >    When a PHP script is running normally the NORMAL state, is active.
  42125.    If the remote client disconnects the ABORTED state flag is
  42126.    turned on.  A remote client disconnect is usually caused by the
  42127.    user hitting his STOP button.  If the PHP-imposed time limit (see
  42128.    <A
  42129. HREF="#function.set-time-limit"
  42130. ><B
  42131. CLASS="function"
  42132. >set_time_limit()</B
  42133. ></A
  42134. >) is hit, the TIMEOUT state flag
  42135.    is turned on.</P
  42136. ><P
  42137. >    You can decide whether or not you want a client disconnect to cause
  42138.    your script to be aborted.  Sometimes it is handy to always have your
  42139.    scripts run to completion even if there is no remote browser receiving
  42140.    the output.  The default behaviour is however for your script to be
  42141.    aborted when the remote client disconnects.  This behaviour can be
  42142.    set via the ignore_user_abort <TT
  42143. CLASS="filename"
  42144. >php.ini</TT
  42145. > directive as well as through
  42146.    the corresponding "php_value ignore_user_abort" Apache .conf directive or
  42147.    with the <A
  42148. HREF="#function.ignore-user-abort"
  42149. ><B
  42150. CLASS="function"
  42151. >ignore_user_abort()</B
  42152. ></A
  42153. > function.  If you do
  42154.    not tell PHP to ignore a user abort and the user aborts, your script
  42155.    will terminate.  The one exception is if you have registered a shutdown
  42156.    function using <A
  42157. HREF="#function.register-shutdown-function"
  42158. ><B
  42159. CLASS="function"
  42160. >register_shutdown_function()</B
  42161. ></A
  42162. >.  With a
  42163.    shutdown function, when the remote user hits his STOP button, the
  42164.    next time your script tries to output something PHP will detect that
  42165.    the connection has been aborted and the shutdown function is called.
  42166.    This shutdown function will also get called at the end of your script
  42167.    terminating normally, so to do something different in case of a client
  42168.    disconnect you can use the <A
  42169. HREF="#function.connection-aborted"
  42170. ><B
  42171. CLASS="function"
  42172. >connection_aborted()</B
  42173. ></A
  42174. >
  42175.    function.  This function will return <TT
  42176. CLASS="constant"
  42177. ><B
  42178. >TRUE</B
  42179. ></TT
  42180. > if the connection was
  42181.    aborted.</P
  42182. ><P
  42183. >    Your script can also be terminated by the built-in script timer.
  42184.    The default timeout is 30 seconds.  It can be changed using
  42185.    the <VAR
  42186. CLASS="option"
  42187. >max_execution_time</VAR
  42188. > <TT
  42189. CLASS="filename"
  42190. >php.ini</TT
  42191. > directive or the corresponding
  42192.    <VAR
  42193. CLASS="literal"
  42194. >php_value max_execution_time</VAR
  42195. > Apache .conf directive as well as with
  42196.    the <A
  42197. HREF="#function.set-time-limit"
  42198. ><B
  42199. CLASS="function"
  42200. >set_time_limit()</B
  42201. ></A
  42202. > function.  When the timer
  42203.    expires the script will be aborted and as with the above client
  42204.    disconnect case, if a shutdown function has been registered it will
  42205.    be called.  Within this shutdown function you can check to see if
  42206.    a timeout caused the shutdown function to be called by calling the
  42207.    <A
  42208. HREF="#function.connection-status"
  42209. ><B
  42210. CLASS="function"
  42211. >connection_status()</B
  42212. ></A
  42213. > function.  This function will
  42214.    return 2 if a timeout caused the shutdown function to be called.
  42215.   </P
  42216. ><P
  42217. >    One thing to note is that both the ABORTED and the TIMEOUT states
  42218.    can be active at the same time.  This is possible if you tell
  42219.    PHP to ignore user aborts.  PHP will still note the fact that
  42220.    a user may have broken the connection, but the script will keep
  42221.    running.  If it then hits the time limit it will be aborted and
  42222.    your shutdown function, if any, will be called.  At this point
  42223.    you will find that <A
  42224. HREF="#function.connection-status"
  42225. ><B
  42226. CLASS="function"
  42227. >connection_status()</B
  42228. ></A
  42229. >
  42230.    returns 3.
  42231.   </P
  42232. ></DIV
  42233. ><DIV
  42234. CLASS="chapter"
  42235. ><HR><H1
  42236. ><A
  42237. NAME="features.persistent-connections"
  42238. >Chapter 41. Persistent Database Connections</A
  42239. ></H1
  42240. ><P
  42241. >    Persistent connections are links that do not close when the
  42242.    execution of your script ends. When a persistent connection is
  42243.    requested, PHP checks if there's already an identical persistent
  42244.    connection (that remained open from earlier) - and if it exists, it
  42245.    uses it. If it does not exist, it creates the link. An 'identical'
  42246.    connection is a connection that was opened to the same host, with
  42247.    the same username and the same password (where applicable).
  42248.   </P
  42249. ><P
  42250. >    People who aren't thoroughly familiar with the way web servers work
  42251.    and distribute the load may mistake persistent connects for what
  42252.    they're not. In particular, they do <SPAN
  42253. CLASS="emphasis"
  42254. ><I
  42255. CLASS="emphasis"
  42256. >not</I
  42257. ></SPAN
  42258. > give
  42259.    you an ability to open 'user sessions' on the same link, they
  42260.    do <SPAN
  42261. CLASS="emphasis"
  42262. ><I
  42263. CLASS="emphasis"
  42264. >not</I
  42265. ></SPAN
  42266. > give you an ability to build up a
  42267.    transaction efficiently, and they don't do a whole lot of other
  42268.    things. In fact, to be extremely clear about the subject,
  42269.    persistent connections don't give you <SPAN
  42270. CLASS="emphasis"
  42271. ><I
  42272. CLASS="emphasis"
  42273. >any</I
  42274. ></SPAN
  42275. >
  42276.    functionality that wasn't possible with their non-persistent
  42277.    brothers.
  42278.   </P
  42279. ><P
  42280. >    Why?
  42281.   </P
  42282. ><P
  42283. >    This has to do with the way web servers work. There are three ways
  42284.    in which your web server can utilize PHP to generate web pages.
  42285.   </P
  42286. ><P
  42287. >    The first method is to use PHP as a CGI "wrapper". When run this
  42288.    way, an instance of the PHP interpreter is created and destroyed
  42289.    for every page request (for a PHP page) to your web server.
  42290.    Because it is destroyed after every request, any resources that it
  42291.    acquires (such as a link to an SQL database server) are closed when
  42292.    it is destroyed. In this case, you do not gain anything from trying
  42293.    to use persistent connections -- they simply don't persist.
  42294.   </P
  42295. ><P
  42296. >    The second, and most popular, method is to run PHP as a module in a
  42297.    multiprocess web server, which currently only includes Apache. A
  42298.    multiprocess server typically has one process (the parent) which
  42299.    coordinates a set of processes (its children) who actually do the
  42300.    work of serving up web pages. When a request comes in from a
  42301.    client, it is handed off to one of the children that is not already
  42302.    serving another client. This means that when the same client makes
  42303.    a second request to the server, it may be served by a different
  42304.    child process than the first time. When opening a persistent connection, 
  42305.    every following page requesting SQL services can reuse the same 
  42306.    established connection to the SQL server.
  42307.   </P
  42308. ><P
  42309. >    The last method is to use PHP as a plug-in for a multithreaded web
  42310.    server. Currently PHP 4 has support for ISAPI, WSAPI, and NSAPI (on
  42311.    Windows), which all allow PHP to be used as a plug-in on multithreaded
  42312.    servers like Netscape FastTrack (iPlanet), Microsoft's Internet Information
  42313.    Server (IIS), and O'Reilly's WebSite Pro. The behavior is essentially
  42314.    the same as for the multiprocess model described before. Note that
  42315.    SAPI support is not available in PHP 3.
  42316.   </P
  42317. ><P
  42318. >    If persistent connections don't have any added functionality, what
  42319.    are they good for?
  42320.   </P
  42321. ><P
  42322. >    The answer here is extremely simple -- efficiency. Persistent
  42323.    connections are good if the overhead to create a link to your SQL
  42324.    server is high. Whether or not this overhead is really high depends
  42325.    on many factors. Like, what kind of database it is, whether or not
  42326.    it sits on the same computer on which your web server sits, how
  42327.    loaded the machine the SQL server sits on is and so forth. The
  42328.    bottom line is that if that connection overhead is high, persistent
  42329.    connections help you considerably. They cause the child process to
  42330.    simply connect only once for its entire lifespan, instead of every
  42331.    time it processes a page that requires connecting to the SQL
  42332.    server. This means that for every child that opened a persistent
  42333.    connection will have its own open persistent connection to the
  42334.    server. For example, if you had 20 different child processes that
  42335.    ran a script that made a persistent connection to your SQL server,
  42336.    you'd have 20 different connections to the SQL server, one from
  42337.    each child.
  42338.   </P
  42339. ><P
  42340. >    Note, however, that this can have some drawbacks if you are using a
  42341.    database with connection limits that are exceeded by persistent
  42342.    child connections. If your database has a limit of 16 simultaneous
  42343.    connections, and in the course of a busy server session, 17 child
  42344.    threads attempt to connect, one will not be able to. If there are
  42345.    bugs in your scripts which do not allow the connections to shut
  42346.    down (such as infinite loops), the database with only 16 connections
  42347.    may be rapidly swamped. Check your database documentation for
  42348.    information on handling abandoned or idle connections.
  42349.   </P
  42350. ><DIV
  42351. CLASS="warning"
  42352. ><P
  42353. ></P
  42354. ><TABLE
  42355. CLASS="warning"
  42356. BORDER="1"
  42357. WIDTH="100%"
  42358. ><TR
  42359. ><TD
  42360. ALIGN="CENTER"
  42361. ><B
  42362. >Warning</B
  42363. ></TD
  42364. ></TR
  42365. ><TR
  42366. ><TD
  42367. ALIGN="LEFT"
  42368. ><P
  42369. >     There are a couple of additional caveats to keep in mind when
  42370.     using persistent connections. One is that when using table
  42371.     locking on a persistent connection, if the script for whatever
  42372.     reason cannot release the lock, then subsequent scripts using the
  42373.     same connection will block indefinitely and may require that you
  42374.     either restart the httpd server or the database server. Another is
  42375.     that when using transactions, a transaction block will also carry
  42376.     over to the next script which uses that connection if script execution
  42377.     ends before the transaction block does. In either case, you can
  42378.     use <A
  42379. HREF="#function.register-shutdown-function"
  42380. ><B
  42381. CLASS="function"
  42382. >register_shutdown_function()</B
  42383. ></A
  42384. > to register a
  42385.     simple cleanup function to unlock your tables or roll back your
  42386.     transactions. Better yet, avoid the problem entirely by not using
  42387.     persistent connections in scripts which use table locks or
  42388.     transactions (you can still use them elsewhere).
  42389.    </P
  42390. ></TD
  42391. ></TR
  42392. ></TABLE
  42393. ></DIV
  42394. ><P
  42395. >    An important summary. Persistent connections were designed to have
  42396.    one-to-one mapping to regular connections. That means that you
  42397.    should <SPAN
  42398. CLASS="emphasis"
  42399. ><I
  42400. CLASS="emphasis"
  42401. >always</I
  42402. ></SPAN
  42403. > be able to replace persistent
  42404.    connections with non-persistent connections, and it won't change
  42405.    the way your script behaves. It <SPAN
  42406. CLASS="emphasis"
  42407. ><I
  42408. CLASS="emphasis"
  42409. >may</I
  42410. ></SPAN
  42411. > (and
  42412.    probably will) change the efficiency of the script, but not its
  42413.    behavior!
  42414.   </P
  42415. ><P
  42416. >    See also <A
  42417. HREF="#function.fbsql-pconnect"
  42418. ><B
  42419. CLASS="function"
  42420. >fbsql_pconnect()</B
  42421. ></A
  42422. >,
  42423.    <A
  42424. HREF="#function.ibase-pconnect"
  42425. ><B
  42426. CLASS="function"
  42427. >ibase_pconnect()</B
  42428. ></A
  42429. >, <A
  42430. HREF="#function.ifx-pconnect"
  42431. ><B
  42432. CLASS="function"
  42433. >ifx_pconnect()</B
  42434. ></A
  42435. >,
  42436.    <A
  42437. HREF="#function.ingres-pconnect"
  42438. ><B
  42439. CLASS="function"
  42440. >ingres_pconnect()</B
  42441. ></A
  42442. >,
  42443.    <A
  42444. HREF="#function.msql-pconnect"
  42445. ><B
  42446. CLASS="function"
  42447. >msql_pconnect()</B
  42448. ></A
  42449. >, <A
  42450. HREF="#function.mssql-pconnect"
  42451. ><B
  42452. CLASS="function"
  42453. >mssql_pconnect()</B
  42454. ></A
  42455. >,
  42456.    <A
  42457. HREF="#function.mysql-pconnect"
  42458. ><B
  42459. CLASS="function"
  42460. >mysql_pconnect()</B
  42461. ></A
  42462. >, <A
  42463. HREF="#function.ociplogon"
  42464. ><B
  42465. CLASS="function"
  42466. >ociplogon()</B
  42467. ></A
  42468. >,
  42469.    <A
  42470. HREF="#function.odbc-pconnect"
  42471. ><B
  42472. CLASS="function"
  42473. >odbc_pconnect()</B
  42474. ></A
  42475. >, <A
  42476. HREF="#function.ora-plogon"
  42477. ><B
  42478. CLASS="function"
  42479. >ora_plogon()</B
  42480. ></A
  42481. >,
  42482.    <A
  42483. HREF="#function.pfsockopen"
  42484. ><B
  42485. CLASS="function"
  42486. >pfsockopen()</B
  42487. ></A
  42488. >, <A
  42489. HREF="#function.pg-pconnect"
  42490. ><B
  42491. CLASS="function"
  42492. >pg_pconnect()</B
  42493. ></A
  42494. >, and
  42495.    <A
  42496. HREF="#function.sybase-pconnect"
  42497. ><B
  42498. CLASS="function"
  42499. >sybase_pconnect()</B
  42500. ></A
  42501. >.
  42502.   </P
  42503. ></DIV
  42504. ><DIV
  42505. CLASS="chapter"
  42506. ><HR><H1
  42507. ><A
  42508. NAME="features.safe-mode"
  42509. >Chapter 42. Safe Mode</A
  42510. ></H1
  42511. ><P
  42512. >    The PHP safe mode is an attempt to solve the shared-server security
  42513.    problem. It is architecturally incorrect to try to solve this
  42514.    problem at the PHP level, but since the alternatives at the web
  42515.    server and OS levels aren't very realistic, many people,
  42516.    especially ISP's, use safe mode for now. 
  42517.   </P
  42518. ><DIV
  42519. CLASS="sect1"
  42520. ><HR><H2
  42521. CLASS="sect1"
  42522. ><A
  42523. NAME="ini.sect.safe-mode"
  42524. >Security and Safe Mode</A
  42525. ></H2
  42526. ><P
  42527. >     <DIV
  42528. CLASS="table"
  42529. ><A
  42530. NAME="AEN6936"
  42531. ></A
  42532. ><P
  42533. ><B
  42534. >Table 42-1. Security and Safe Mode Configuration Directives</B
  42535. ></P
  42536. ><TABLE
  42537. BORDER="1"
  42538. CLASS="CALSTABLE"
  42539. ><COL><COL><COL><THEAD
  42540. ><TR
  42541. ><TH
  42542. >Name</TH
  42543. ><TH
  42544. >Default</TH
  42545. ><TH
  42546. >Changeable</TH
  42547. ></TR
  42548. ></THEAD
  42549. ><TBODY
  42550. ><TR
  42551. ><TD
  42552. >safe_mode</TD
  42553. ><TD
  42554. >"0"</TD
  42555. ><TD
  42556. >PHP_INI_SYSTEM</TD
  42557. ></TR
  42558. ><TR
  42559. ><TD
  42560. >safe_mode_gid</TD
  42561. ><TD
  42562. >"0"</TD
  42563. ><TD
  42564. >PHP_INI_SYSTEM</TD
  42565. ></TR
  42566. ><TR
  42567. ><TD
  42568. >safe_mode_include_dir</TD
  42569. ><TD
  42570. >NULL</TD
  42571. ><TD
  42572. >PHP_INI_SYSTEM</TD
  42573. ></TR
  42574. ><TR
  42575. ><TD
  42576. >safe_mode_exec_dir</TD
  42577. ><TD
  42578. >""</TD
  42579. ><TD
  42580. >PHP_INI_SYSTEM</TD
  42581. ></TR
  42582. ><TR
  42583. ><TD
  42584. >safe_mode_allowed_env_vars</TD
  42585. ><TD
  42586. >PHP_</TD
  42587. ><TD
  42588. >PHP_INI_SYSTEM</TD
  42589. ></TR
  42590. ><TR
  42591. ><TD
  42592. >safe_mode_protected_env_vars</TD
  42593. ><TD
  42594. >LD_LIBRARY_PATH</TD
  42595. ><TD
  42596. >PHP_INI_SYSTEM</TD
  42597. ></TR
  42598. ><TR
  42599. ><TD
  42600. >open_basedir</TD
  42601. ><TD
  42602. >NULL</TD
  42603. ><TD
  42604. >PHP_INI_SYSTEM</TD
  42605. ></TR
  42606. ><TR
  42607. ><TD
  42608. >disable_functions</TD
  42609. ><TD
  42610. >""</TD
  42611. ><TD
  42612. >PHP_INI_SYSTEM</TD
  42613. ></TR
  42614. ><TR
  42615. ><TD
  42616. >disable_classes</TD
  42617. ><TD
  42618. >""</TD
  42619. ><TD
  42620. >PHP_INI_SYSTEM</TD
  42621. ></TR
  42622. ></TBODY
  42623. ></TABLE
  42624. ></DIV
  42625. >
  42626.     For further details and definition of the PHP_INI_* constants see
  42627.     <A
  42628. HREF="#function.ini-set"
  42629. ><B
  42630. CLASS="function"
  42631. >ini_set()</B
  42632. ></A
  42633. >.
  42634.    </P
  42635. ><P
  42636. >Here's a short explanation of
  42637. the configuration directives.</P
  42638. ><P
  42639. >     <P
  42640. ></P
  42641. ><DIV
  42642. CLASS="variablelist"
  42643. ><DL
  42644. ><DT
  42645. ><A
  42646. NAME="ini.safe-mode"
  42647. ></A
  42648. ><VAR
  42649. CLASS="parameter"
  42650. >safe_mode</VAR
  42651. >
  42652.        <A
  42653. HREF="#language.types.boolean"
  42654. ><B
  42655. CLASS="type"
  42656. >boolean</B
  42657. ></A
  42658. ></DT
  42659. ><DD
  42660. ><P
  42661. >         Whether to enable PHP's safe mode. Read the
  42662.         <A
  42663. HREF="#security"
  42664. >Security</A
  42665. > chapter for more
  42666.         information.
  42667.        </P
  42668. ></DD
  42669. ><DT
  42670. ><A
  42671. NAME="ini.safe-mode-gid"
  42672. ></A
  42673. ><VAR
  42674. CLASS="parameter"
  42675. >safe_mode_gid</VAR
  42676. >
  42677.        <A
  42678. HREF="#language.types.boolean"
  42679. ><B
  42680. CLASS="type"
  42681. >boolean</B
  42682. ></A
  42683. ></DT
  42684. ><DD
  42685. ><P
  42686. >         By default, Safe Mode does a UID compare check when
  42687.         opening files. If you want to relax this to a GID compare,
  42688.         then turn on safe_mode_gid.
  42689.         Whether to use <VAR
  42690. CLASS="literal"
  42691. >UID</VAR
  42692. > (<TT
  42693. CLASS="constant"
  42694. ><B
  42695. >FALSE</B
  42696. ></TT
  42697. >) or
  42698.         <VAR
  42699. CLASS="literal"
  42700. >GID</VAR
  42701. > (<TT
  42702. CLASS="constant"
  42703. ><B
  42704. >TRUE</B
  42705. ></TT
  42706. >) checking upon file
  42707.         access.
  42708.        </P
  42709. ></DD
  42710. ><DT
  42711. ><A
  42712. NAME="ini.safe-mode-include-dir"
  42713. ></A
  42714. ><VAR
  42715. CLASS="parameter"
  42716. >safe_mode_include_dir</VAR
  42717. >
  42718.        <A
  42719. HREF="#language.types.string"
  42720. ><B
  42721. CLASS="type"
  42722. >string</B
  42723. ></A
  42724. ></DT
  42725. ><DD
  42726. ><P
  42727. >         <VAR
  42728. CLASS="literal"
  42729. >UID</VAR
  42730. >/<VAR
  42731. CLASS="literal"
  42732. >GID</VAR
  42733. > checks are bypassed when
  42734.         including files from this directory and its subdirectories (directory
  42735.         must also be in <A
  42736. HREF="#ini.include-path"
  42737. >include_path</A
  42738. >
  42739.         or full path must including).
  42740.        </P
  42741. ><P
  42742. >         As of PHP 4.2.0, this directive can take a colon (semi-colon on
  42743.         Windows) separated path in a fashion similar to the
  42744.         <A
  42745. HREF="#ini.include-path"
  42746. >include_path</A
  42747. > directive,
  42748.         rather than just a single directory.
  42749.        </P
  42750. ><P
  42751. >         The restriction specified is actually a prefix, not a directory name. 
  42752.         This means that "safe_mode_include_dir = /dir/incl" also allows
  42753.         access to "/dir/include" and "/dir/incls" if they exist.  When you 
  42754.         want to restrict access to only the specified directory, end with a 
  42755.         slash. For example: "safe_mode_include_dir = /dir/incl/"
  42756.        </P
  42757. ></DD
  42758. ><DT
  42759. ><A
  42760. NAME="ini.safe-mode-exec-dir"
  42761. ></A
  42762. ><VAR
  42763. CLASS="parameter"
  42764. >safe_mode_exec_dir</VAR
  42765. >
  42766.        <A
  42767. HREF="#language.types.string"
  42768. ><B
  42769. CLASS="type"
  42770. >string</B
  42771. ></A
  42772. ></DT
  42773. ><DD
  42774. ><P
  42775. >         If PHP is used in safe mode, <A
  42776. HREF="#function.system"
  42777. ><B
  42778. CLASS="function"
  42779. >system()</B
  42780. ></A
  42781. > and the other
  42782.         <A
  42783. HREF="#ref.exec"
  42784. >functions executing system programs</A
  42785. >
  42786.         refuse to start programs that are not in this directory.
  42787.         You have to use <VAR
  42788. CLASS="literal"
  42789. >/</VAR
  42790. > as directory separator on all
  42791.         environments including Windows.
  42792.        </P
  42793. ></DD
  42794. ><DT
  42795. ><A
  42796. NAME="ini.safe-mode-allowed-env-vars"
  42797. ></A
  42798. ><VAR
  42799. CLASS="parameter"
  42800. >safe_mode_allowed_env_vars</VAR
  42801. >
  42802.        <A
  42803. HREF="#language.types.string"
  42804. ><B
  42805. CLASS="type"
  42806. >string</B
  42807. ></A
  42808. ></DT
  42809. ><DD
  42810. ><P
  42811. >         Setting certain environment variables may be a potential security breach.
  42812.         This directive contains a comma-delimited list of prefixes. In Safe Mode,
  42813.         the user may only alter environment variables whose names begin with the
  42814.         prefixes supplied here. By default, users will only be able to set
  42815.         environment variables that begin with PHP_ (e.g. PHP_FOO=BAR).
  42816.        </P
  42817. ><DIV
  42818. CLASS="note"
  42819. ><BLOCKQUOTE
  42820. CLASS="note"
  42821. ><P
  42822. ><B
  42823. >Note: </B
  42824. >
  42825.          If this directive is empty, PHP will let the user modify ANY
  42826.          environment variable!
  42827.         </P
  42828. ></BLOCKQUOTE
  42829. ></DIV
  42830. ></DD
  42831. ><DT
  42832. ><A
  42833. NAME="ini.safe-mode-protected-env-vars"
  42834. ></A
  42835. ><VAR
  42836. CLASS="parameter"
  42837. >safe_mode_protected_env_vars</VAR
  42838. >
  42839.        <A
  42840. HREF="#language.types.string"
  42841. ><B
  42842. CLASS="type"
  42843. >string</B
  42844. ></A
  42845. ></DT
  42846. ><DD
  42847. ><P
  42848. >         This directive contains a comma-delimited list of environment
  42849.         variables that the end user won't be able to change using
  42850.         <A
  42851. HREF="#function.putenv"
  42852. ><B
  42853. CLASS="function"
  42854. >putenv()</B
  42855. ></A
  42856. >. These variables will be protected
  42857.         even if safe_mode_allowed_env_vars is set to allow to change them.
  42858.        </P
  42859. ></DD
  42860. ><DT
  42861. ><A
  42862. NAME="ini.open-basedir"
  42863. ></A
  42864. ><VAR
  42865. CLASS="parameter"
  42866. >open_basedir</VAR
  42867. >
  42868.        <A
  42869. HREF="#language.types.string"
  42870. ><B
  42871. CLASS="type"
  42872. >string</B
  42873. ></A
  42874. ></DT
  42875. ><DD
  42876. ><P
  42877. >         Limit the files that can be opened by PHP to the specified
  42878.         directory-tree, including the file itself.  This directive 
  42879.         is <SPAN
  42880. CLASS="emphasis"
  42881. ><I
  42882. CLASS="emphasis"
  42883. >NOT</I
  42884. ></SPAN
  42885. > affected by whether Safe Mode is 
  42886.         turned On or Off.
  42887.        </P
  42888. ><P
  42889. >         When a script tries to open a file with, for example,
  42890.         <A
  42891. HREF="#function.fopen"
  42892. ><B
  42893. CLASS="function"
  42894. >fopen()</B
  42895. ></A
  42896. > or <A
  42897. HREF="#function.gzopen"
  42898. ><B
  42899. CLASS="function"
  42900. >gzopen()</B
  42901. ></A
  42902. >,
  42903.         the location of the file is checked. When the file is outside the
  42904.         specified directory-tree, PHP will refuse to open it. All symbolic
  42905.         links are resolved, so it's not possible to avoid this restriction
  42906.         with a symlink.
  42907.        </P
  42908. ><P
  42909. >         The special value <SPAN
  42910. CLASS="systemitem"
  42911. >.</SPAN
  42912. >
  42913.         indicates that the working directory of the script will be used as the
  42914.         base-directory. This is, however, a little dangerous as the working directory
  42915.         of the script can easily be changed with <A
  42916. HREF="#function.chdir"
  42917. ><B
  42918. CLASS="function"
  42919. >chdir()</B
  42920. ></A
  42921. >.
  42922.        </P
  42923. ><P
  42924. >         In <TT
  42925. CLASS="filename"
  42926. >httpd.conf</TT
  42927. >, open_basedir can be turned off
  42928.         (e.g. for some virtual hosts)
  42929.         <A
  42930. HREF="#configuration.changes.apache"
  42931. >the same way</A
  42932. > as
  42933.         any other configuration directive with "php_admin_value open_basedir
  42934.         none".
  42935.        </P
  42936. ><P
  42937. >         Under Windows, separate the directories with a semicolon. On all
  42938.         other systems, separate the directories with a colon. As an Apache
  42939.         module, open_basedir paths from parent directories are now
  42940.         automatically inherited.
  42941.        </P
  42942. ><P
  42943. >          The restriction specified with open_basedir is actually a
  42944.          prefix, not a directory name.  This means that "open_basedir =
  42945.          /dir/incl" also allows access to "/dir/include" and
  42946.          "/dir/incls" if they exist. When you want to restrict access
  42947.          to only the specified directory, end with a slash. For example:
  42948.          "open_basedir = /dir/incl/"
  42949.        </P
  42950. ><DIV
  42951. CLASS="note"
  42952. ><BLOCKQUOTE
  42953. CLASS="note"
  42954. ><P
  42955. ><B
  42956. >Note: </B
  42957. >
  42958.          Support for multiple directories was added in 3.0.7.
  42959.         </P
  42960. ></BLOCKQUOTE
  42961. ></DIV
  42962. ><P
  42963. >         The default is to allow all files to be opened.
  42964.        </P
  42965. ></DD
  42966. ><DT
  42967. ><A
  42968. NAME="ini.disable-functions"
  42969. ></A
  42970. ><VAR
  42971. CLASS="parameter"
  42972. >disable_functions</VAR
  42973. >
  42974.        <A
  42975. HREF="#language.types.string"
  42976. ><B
  42977. CLASS="type"
  42978. >string</B
  42979. ></A
  42980. ></DT
  42981. ><DD
  42982. ><P
  42983. >         This directive allows you to disable certain functions for 
  42984.         <A
  42985. HREF="#security"
  42986. >security</A
  42987. > reasons. It takes 
  42988.         on a comma-delimited list of function names. disable_functions
  42989.         is not affected by <A
  42990. HREF="#ini.safe-mode"
  42991. >Safe Mode</A
  42992. >.
  42993.        </P
  42994. ><P
  42995. >         This directive must be set in <TT
  42996. CLASS="filename"
  42997. >php.ini</TT
  42998. > For example, you 
  42999.         cannot set this in <TT
  43000. CLASS="filename"
  43001. >httpd.conf</TT
  43002. >.
  43003.        </P
  43004. ></DD
  43005. ><DT
  43006. ><A
  43007. NAME="ini.disable-classes"
  43008. ></A
  43009. ><VAR
  43010. CLASS="parameter"
  43011. >disable_classes</VAR
  43012. >
  43013.        <A
  43014. HREF="#language.types.string"
  43015. ><B
  43016. CLASS="type"
  43017. >string</B
  43018. ></A
  43019. ></DT
  43020. ><DD
  43021. ><P
  43022. >         This directive allows you to disable certain classes for
  43023.         <A
  43024. HREF="#security"
  43025. >security</A
  43026. > reasons.  It takes
  43027.         on a comma-delimited list of class names.  disable_classes
  43028.         is not affected by <A
  43029. HREF="#ini.safe-mode"
  43030. >Safe Mode</A
  43031. >.
  43032.        </P
  43033. ><P
  43034. >         This directive must be set in <TT
  43035. CLASS="filename"
  43036. >php.ini</TT
  43037. >  For example, you
  43038.         cannot set this in <TT
  43039. CLASS="filename"
  43040. >httpd.conf</TT
  43041. >.
  43042.        </P
  43043. ><DIV
  43044. CLASS="note"
  43045. ><BLOCKQUOTE
  43046. CLASS="note"
  43047. ><P
  43048. ><B
  43049. >Availability note: </B
  43050. >
  43051.          This directive became available in PHP 4.3.2
  43052.         </P
  43053. ></BLOCKQUOTE
  43054. ></DIV
  43055. ></DD
  43056. ></DL
  43057. ></DIV
  43058. >
  43059.    </P
  43060. ><P
  43061. >     See also: <A
  43062. HREF="#ini.register-globals"
  43063. >register_globals</A
  43064. >,
  43065.     <A
  43066. HREF="#ini.display-errors"
  43067. >display_errors</A
  43068. >, and
  43069.     <A
  43070. HREF="#ini.log-errors"
  43071. >log_errors</A
  43072. >
  43073.    </P
  43074. ><P
  43075. >    When <A
  43076. HREF="#ini.safe-mode"
  43077. >safe_mode</A
  43078. > is on, PHP checks to see 
  43079.    if the owner of the current script matches the owner of the file to be 
  43080.    operated on by a file function or its directory. For example: 
  43081.    <TABLE
  43082. BORDER="0"
  43083. BGCOLOR="#E0E0E0"
  43084. CELLPADDING="5"
  43085. ><TR
  43086. ><TD
  43087. ><PRE
  43088. CLASS="ls"
  43089. >-rw-rw-r--    1 rasmus   rasmus       33 Jul  1 19:20 script.php 
  43090. -rw-r--r--    1 root     root       1116 May 26 18:01 /etc/passwd</PRE
  43091. ></TD
  43092. ></TR
  43093. ></TABLE
  43094. >
  43095.    Running this script.php 
  43096.    <TABLE
  43097. BORDER="0"
  43098. BGCOLOR="#E0E0E0"
  43099. CELLPADDING="5"
  43100. ><TR
  43101. ><TD
  43102. ><PRE
  43103. CLASS="php"
  43104. ><?php
  43105.  readfile('/etc/passwd'); 
  43106. ?></PRE
  43107. ></TD
  43108. ></TR
  43109. ></TABLE
  43110. >
  43111.    results in this error when safe mode is enabled: 
  43112.    <TABLE
  43113. BORDER="0"
  43114. BGCOLOR="#E0E0E0"
  43115. CELLPADDING="5"
  43116. ><TR
  43117. ><TD
  43118. ><PRE
  43119. CLASS="screen"
  43120. >Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not 
  43121. allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2</PRE
  43122. ></TD
  43123. ></TR
  43124. ></TABLE
  43125. >
  43126.   </P
  43127. ><P
  43128. >    However, there may be environments where a strict <VAR
  43129. CLASS="literal"
  43130. >UID</VAR
  43131. >
  43132.    check is not appropriate and a relaxed <VAR
  43133. CLASS="literal"
  43134. >GID</VAR
  43135. > check is
  43136.    sufficient.  This is supported by means of the <A
  43137. HREF="#ini.safe-mode-gid"
  43138. >safe_mode_gid</A
  43139. > switch. Setting it to
  43140.    <VAR
  43141. CLASS="literal"
  43142. >On</VAR
  43143. > performs the relaxed <VAR
  43144. CLASS="literal"
  43145. >GID</VAR
  43146. > checking,
  43147.    setting it to <VAR
  43148. CLASS="literal"
  43149. >Off</VAR
  43150. > (the default) performs
  43151.    <VAR
  43152. CLASS="literal"
  43153. >UID</VAR
  43154. > checking.
  43155.   </P
  43156. ><P
  43157. >    If instead of <A
  43158. HREF="#ini.safe-mode"
  43159. >safe_mode</A
  43160. >, you set an 
  43161.    <A
  43162. HREF="#ini.open-basedir"
  43163. >open_basedir</A
  43164. > directory then all
  43165.    file operations will be limited to files under the specified directory
  43166.    For example (Apache <TT
  43167. CLASS="filename"
  43168. >httpd.conf</TT
  43169. > example): 
  43170.    <TABLE
  43171. BORDER="0"
  43172. BGCOLOR="#E0E0E0"
  43173. CELLPADDING="5"
  43174. ><TR
  43175. ><TD
  43176. ><PRE
  43177. CLASS="ini"
  43178. ><Directory /docroot>
  43179.   php_admin_value open_basedir /docroot 
  43180. </Directory></PRE
  43181. ></TD
  43182. ></TR
  43183. ></TABLE
  43184. >
  43185.    If you run the same script.php with this
  43186.    <A
  43187. HREF="#ini.open-basedir"
  43188. >open_basedir</A
  43189. > setting
  43190.    then this is the result: 
  43191.    <TABLE
  43192. BORDER="0"
  43193. BGCOLOR="#E0E0E0"
  43194. CELLPADDING="5"
  43195. ><TR
  43196. ><TD
  43197. ><PRE
  43198. CLASS="screen"
  43199. >Warning: open_basedir restriction in effect. File is in wrong directory in 
  43200. /docroot/script.php on line 2</PRE
  43201. ></TD
  43202. ></TR
  43203. ></TABLE
  43204. >
  43205.   </P
  43206. ><P
  43207. >    You can also disable individual functions. Note that the
  43208.    <A
  43209. HREF="#ini.disable-functions"
  43210. >disable_functions</A
  43211. >
  43212.    directive can not be used outside of the <TT
  43213. CLASS="filename"
  43214. >php.ini</TT
  43215. > file which means that
  43216.    you cannot disable functions on a per-virtualhost or per-directory basis
  43217.    in your <TT
  43218. CLASS="filename"
  43219. >httpd.conf</TT
  43220. > file.
  43221.    If we add this to our <TT
  43222. CLASS="filename"
  43223. >php.ini</TT
  43224. > file: 
  43225.    <TABLE
  43226. BORDER="0"
  43227. BGCOLOR="#E0E0E0"
  43228. CELLPADDING="5"
  43229. ><TR
  43230. ><TD
  43231. ><PRE
  43232. CLASS="ini"
  43233. >disable_functions readfile,system</PRE
  43234. ></TD
  43235. ></TR
  43236. ></TABLE
  43237. >
  43238.    Then we get this output: 
  43239.    <TABLE
  43240. BORDER="0"
  43241. BGCOLOR="#E0E0E0"
  43242. CELLPADDING="5"
  43243. ><TR
  43244. ><TD
  43245. ><PRE
  43246. CLASS="screen"
  43247. >Warning: readfile() has been disabled for security reasons in 
  43248. /docroot/script.php on line 2</PRE
  43249. ></TD
  43250. ></TR
  43251. ></TABLE
  43252. >
  43253.   </P
  43254. ></DIV
  43255. ><DIV
  43256. CLASS="sect1"
  43257. ><HR><H2
  43258. CLASS="sect1"
  43259. ><A
  43260. NAME="features.safe-mode.functions"
  43261. >Functions restricted/disabled by safe mode</A
  43262. ></H2
  43263. ><P
  43264. >     This is a still probably incomplete and possibly incorrect listing
  43265.     of the functions limited by 
  43266.     <A
  43267. HREF="#features.safe-mode"
  43268. >safe mode</A
  43269. >.
  43270.     
  43271.     <DIV
  43272. CLASS="table"
  43273. ><A
  43274. NAME="AEN7119"
  43275. ></A
  43276. ><P
  43277. ><B
  43278. >Table 42-2. Safe mode limited functions</B
  43279. ></P
  43280. ><TABLE
  43281. BORDER="1"
  43282. CLASS="CALSTABLE"
  43283. ><COL><COL><THEAD
  43284. ><TR
  43285. ><TH
  43286. >Function</TH
  43287. ><TH
  43288. >Limitations</TH
  43289. ></TR
  43290. ></THEAD
  43291. ><TBODY
  43292. ><TR
  43293. ><TD
  43294. ><A
  43295. HREF="#function.dbmopen"
  43296. ><B
  43297. CLASS="function"
  43298. >dbmopen()</B
  43299. ></A
  43300. ></TD
  43301. ><TD
  43302. >Checks whether the files or directories you are 
  43303. about to operate on have the same UID (owner) as the script that is being
  43304. executed.</TD
  43305. ></TR
  43306. ><TR
  43307. ><TD
  43308. ><A
  43309. HREF="#function.dbase-open"
  43310. ><B
  43311. CLASS="function"
  43312. >dbase_open()</B
  43313. ></A
  43314. ></TD
  43315. ><TD
  43316. >Checks whether the files or directories you are 
  43317. about to operate on have the same UID (owner) as the script that is being
  43318. executed.</TD
  43319. ></TR
  43320. ><TR
  43321. ><TD
  43322. ><A
  43323. HREF="#function.filepro"
  43324. ><B
  43325. CLASS="function"
  43326. >filepro()</B
  43327. ></A
  43328. ></TD
  43329. ><TD
  43330. >Checks whether the files or directories you are 
  43331. about to operate on have the same UID (owner) as the script that is being
  43332. executed.</TD
  43333. ></TR
  43334. ><TR
  43335. ><TD
  43336. ><A
  43337. HREF="#function.filepro-rowcount"
  43338. ><B
  43339. CLASS="function"
  43340. >filepro_rowcount()</B
  43341. ></A
  43342. ></TD
  43343. ><TD
  43344. >Checks whether the files or directories you are 
  43345. about to operate on have the same UID (owner) as the script that is being
  43346. executed.</TD
  43347. ></TR
  43348. ><TR
  43349. ><TD
  43350. ><A
  43351. HREF="#function.filepro-retrieve"
  43352. ><B
  43353. CLASS="function"
  43354. >filepro_retrieve()</B
  43355. ></A
  43356. ></TD
  43357. ><TD
  43358. >Checks whether the files or directories you are 
  43359. about to operate on have the same UID (owner) as the script that is being
  43360. executed.</TD
  43361. ></TR
  43362. ><TR
  43363. ><TD
  43364. >ifx_*</TD
  43365. ><TD
  43366. >sql_safe_mode restrictions, (!= safe mode)</TD
  43367. ></TR
  43368. ><TR
  43369. ><TD
  43370. >ingres_*</TD
  43371. ><TD
  43372. >sql_safe_mode restrictions, (!= safe mode)</TD
  43373. ></TR
  43374. ><TR
  43375. ><TD
  43376. >mysql_*</TD
  43377. ><TD
  43378. >sql_safe_mode restrictions, (!= safe mode)</TD
  43379. ></TR
  43380. ><TR
  43381. ><TD
  43382. ><A
  43383. HREF="#function.pg-lo-import"
  43384. ><B
  43385. CLASS="function"
  43386. >pg_lo_import()</B
  43387. ></A
  43388. ></TD
  43389. ><TD
  43390. >Checks whether the files or directories you are 
  43391. about to operate on have the same UID (owner) as the script that is being
  43392. executed.</TD
  43393. ></TR
  43394. ><TR
  43395. ><TD
  43396. ><A
  43397. HREF="#function.posix-mkfifo"
  43398. ><B
  43399. CLASS="function"
  43400. >posix_mkfifo()</B
  43401. ></A
  43402. ></TD
  43403. ><TD
  43404. >Checks whether the directory in which
  43405. you are about to operate has the same UID (owner) as the script that is being
  43406. executed.</TD
  43407. ></TR
  43408. ><TR
  43409. ><TD
  43410. ><A
  43411. HREF="#function.putenv"
  43412. ><B
  43413. CLASS="function"
  43414. >putenv()</B
  43415. ></A
  43416. ></TD
  43417. ><TD
  43418. >Obeys the safe_mode_protected_env_vars and 
  43419.         safe_mode_allowed_env_vars ini-directives. See also the documentation
  43420.         on <A
  43421. HREF="#function.putenv"
  43422. ><B
  43423. CLASS="function"
  43424. >putenv()</B
  43425. ></A
  43426. ></TD
  43427. ></TR
  43428. ><TR
  43429. ><TD
  43430. ><A
  43431. HREF="#function.move-uploaded-file"
  43432. ><B
  43433. CLASS="function"
  43434. >move_uploaded_file()</B
  43435. ></A
  43436. ></TD
  43437. ><TD
  43438. >Checks whether the files or directories you are 
  43439. about to operate on have the same UID (owner) as the script that is being
  43440. executed. </TD
  43441. ></TR
  43442. ><TR
  43443. ><TD
  43444. ><A
  43445. HREF="#function.chdir"
  43446. ><B
  43447. CLASS="function"
  43448. >chdir()</B
  43449. ></A
  43450. ></TD
  43451. ><TD
  43452. >Checks whether the directory in which
  43453. you are about to operate has the same UID (owner) as the script that is being
  43454. executed.</TD
  43455. ></TR
  43456. ><TR
  43457. ><TD
  43458. ><A
  43459. HREF="#function.dl"
  43460. ><B
  43461. CLASS="function"
  43462. >dl()</B
  43463. ></A
  43464. ></TD
  43465. ><TD
  43466. >This function is disabled in <A
  43467. HREF="#features.safe-mode"
  43468. >safe mode</A
  43469. >.</TD
  43470. ></TR
  43471. ><TR
  43472. ><TD
  43473. ><A
  43474. HREF="#language.operators.execution"
  43475. >backtick operator</A
  43476. ></TD
  43477. ><TD
  43478. >This function is disabled in <A
  43479. HREF="#features.safe-mode"
  43480. >safe mode</A
  43481. >.</TD
  43482. ></TR
  43483. ><TR
  43484. ><TD
  43485. ><A
  43486. HREF="#function.shell-exec"
  43487. ><B
  43488. CLASS="function"
  43489. >shell_exec()</B
  43490. ></A
  43491. > (functional equivalent 
  43492.         of backticks)</TD
  43493. ><TD
  43494. >This function is disabled in <A
  43495. HREF="#features.safe-mode"
  43496. >safe mode</A
  43497. >.</TD
  43498. ></TR
  43499. ><TR
  43500. ><TD
  43501. ><A
  43502. HREF="#function.exec"
  43503. ><B
  43504. CLASS="function"
  43505. >exec()</B
  43506. ></A
  43507. ></TD
  43508. ><TD
  43509. >You can only execute executables within the <A
  43510. HREF="#ini.safe-mode-exec-dir"
  43511. >safe_mode_exec_dir</A
  43512. >.
  43513.         For practical reasons it's currently not allowed to have
  43514.         <VAR
  43515. CLASS="literal"
  43516. >..</VAR
  43517. > components in the path to the executable.
  43518.         <A
  43519. HREF="#function.escapeshellcmd"
  43520. ><B
  43521. CLASS="function"
  43522. >escapeshellcmd()</B
  43523. ></A
  43524. > is executed on the argument of this
  43525.         function.</TD
  43526. ></TR
  43527. ><TR
  43528. ><TD
  43529. ><A
  43530. HREF="#function.system"
  43531. ><B
  43532. CLASS="function"
  43533. >system()</B
  43534. ></A
  43535. ></TD
  43536. ><TD
  43537. >You can only execute executables within the <A
  43538. HREF="#ini.safe-mode-exec-dir"
  43539. >safe_mode_exec_dir</A
  43540. >.
  43541.         For practical reasons it's currently not allowed to have
  43542.         <VAR
  43543. CLASS="literal"
  43544. >..</VAR
  43545. > components in the path to the executable.
  43546.         <A
  43547. HREF="#function.escapeshellcmd"
  43548. ><B
  43549. CLASS="function"
  43550. >escapeshellcmd()</B
  43551. ></A
  43552. > is executed on the argument of this
  43553.         function.</TD
  43554. ></TR
  43555. ><TR
  43556. ><TD
  43557. ><A
  43558. HREF="#function.passthru"
  43559. ><B
  43560. CLASS="function"
  43561. >passthru()</B
  43562. ></A
  43563. ></TD
  43564. ><TD
  43565. >You can only execute executables within the <A
  43566. HREF="#ini.safe-mode-exec-dir"
  43567. >safe_mode_exec_dir</A
  43568. >.
  43569.         For practical reasons it's currently not allowed to have
  43570.         <VAR
  43571. CLASS="literal"
  43572. >..</VAR
  43573. > components in the path to the executable.
  43574.         <A
  43575. HREF="#function.escapeshellcmd"
  43576. ><B
  43577. CLASS="function"
  43578. >escapeshellcmd()</B
  43579. ></A
  43580. > is executed on the argument of this
  43581.         function.</TD
  43582. ></TR
  43583. ><TR
  43584. ><TD
  43585. ><A
  43586. HREF="#function.popen"
  43587. ><B
  43588. CLASS="function"
  43589. >popen()</B
  43590. ></A
  43591. ></TD
  43592. ><TD
  43593. >You can only execute executables within the <A
  43594. HREF="#ini.safe-mode-exec-dir"
  43595. >safe_mode_exec_dir</A
  43596. >.
  43597.         For practical reasons it's currently not allowed to have
  43598.         <VAR
  43599. CLASS="literal"
  43600. >..</VAR
  43601. > components in the path to the executable.
  43602.         <A
  43603. HREF="#function.escapeshellcmd"
  43604. ><B
  43605. CLASS="function"
  43606. >escapeshellcmd()</B
  43607. ></A
  43608. > is executed on the argument of this
  43609.         function.</TD
  43610. ></TR
  43611. ><TR
  43612. ><TD
  43613. ><A
  43614. HREF="#function.fopen"
  43615. ><B
  43616. CLASS="function"
  43617. >fopen()</B
  43618. ></A
  43619. ></TD
  43620. ><TD
  43621. >Checks whether the directory in which
  43622. you are about to operate has the same UID (owner) as the script that is being
  43623. executed.</TD
  43624. ></TR
  43625. ><TR
  43626. ><TD
  43627. ><A
  43628. HREF="#function.mkdir"
  43629. ><B
  43630. CLASS="function"
  43631. >mkdir()</B
  43632. ></A
  43633. ></TD
  43634. ><TD
  43635. >Checks whether the directory in which
  43636. you are about to operate has the same UID (owner) as the script that is being
  43637. executed.</TD
  43638. ></TR
  43639. ><TR
  43640. ><TD
  43641. ><A
  43642. HREF="#function.rmdir"
  43643. ><B
  43644. CLASS="function"
  43645. >rmdir()</B
  43646. ></A
  43647. ></TD
  43648. ><TD
  43649. >Checks whether the directory in which
  43650. you are about to operate has the same UID (owner) as the script that is being
  43651. executed.</TD
  43652. ></TR
  43653. ><TR
  43654. ><TD
  43655. ><A
  43656. HREF="#function.rename"
  43657. ><B
  43658. CLASS="function"
  43659. >rename()</B
  43660. ></A
  43661. ></TD
  43662. ><TD
  43663. >Checks whether the files or directories you are 
  43664. about to operate on have the same UID (owner) as the script that is being
  43665. executed. Checks whether the directory in which
  43666. you are about to operate has the same UID (owner) as the script that is being
  43667. executed.</TD
  43668. ></TR
  43669. ><TR
  43670. ><TD
  43671. ><A
  43672. HREF="#function.unlink"
  43673. ><B
  43674. CLASS="function"
  43675. >unlink()</B
  43676. ></A
  43677. ></TD
  43678. ><TD
  43679. >Checks whether the files or directories you are 
  43680. about to operate on have the same UID (owner) as the script that is being
  43681. executed. Checks whether the directory in which
  43682. you are about to operate has the same UID (owner) as the script that is being
  43683. executed.</TD
  43684. ></TR
  43685. ><TR
  43686. ><TD
  43687. ><A
  43688. HREF="#function.copy"
  43689. ><B
  43690. CLASS="function"
  43691. >copy()</B
  43692. ></A
  43693. ></TD
  43694. ><TD
  43695. >Checks whether the files or directories you are 
  43696. about to operate on have the same UID (owner) as the script that is being
  43697. executed. Checks whether the directory in which
  43698. you are about to operate has the same UID (owner) as the script that is being
  43699. executed. (on 
  43700.         <VAR
  43701. CLASS="parameter"
  43702. >source</VAR
  43703. > and 
  43704.         <VAR
  43705. CLASS="parameter"
  43706. >target</VAR
  43707. >) </TD
  43708. ></TR
  43709. ><TR
  43710. ><TD
  43711. ><A
  43712. HREF="#function.chgrp"
  43713. ><B
  43714. CLASS="function"
  43715. >chgrp()</B
  43716. ></A
  43717. ></TD
  43718. ><TD
  43719. >Checks whether the files or directories you are 
  43720. about to operate on have the same UID (owner) as the script that is being
  43721. executed.</TD
  43722. ></TR
  43723. ><TR
  43724. ><TD
  43725. ><A
  43726. HREF="#function.chown"
  43727. ><B
  43728. CLASS="function"
  43729. >chown()</B
  43730. ></A
  43731. ></TD
  43732. ><TD
  43733. >Checks whether the files or directories you are 
  43734. about to operate on have the same UID (owner) as the script that is being
  43735. executed.</TD
  43736. ></TR
  43737. ><TR
  43738. ><TD
  43739. ><A
  43740. HREF="#function.chmod"
  43741. ><B
  43742. CLASS="function"
  43743. >chmod()</B
  43744. ></A
  43745. ></TD
  43746. ><TD
  43747. >Checks whether the files or directories you are 
  43748. about to operate on have the same UID (owner) as the script that is being
  43749. executed. In addition, you cannot  
  43750.         set the SUID, SGID and sticky bits</TD
  43751. ></TR
  43752. ><TR
  43753. ><TD
  43754. ><A
  43755. HREF="#function.touch"
  43756. ><B
  43757. CLASS="function"
  43758. >touch()</B
  43759. ></A
  43760. ></TD
  43761. ><TD
  43762. >Checks whether the files or directories you are 
  43763. about to operate on have the same UID (owner) as the script that is being
  43764. executed. Checks whether the directory in which
  43765. you are about to operate has the same UID (owner) as the script that is being
  43766. executed.</TD
  43767. ></TR
  43768. ><TR
  43769. ><TD
  43770. ><A
  43771. HREF="#function.symlink"
  43772. ><B
  43773. CLASS="function"
  43774. >symlink()</B
  43775. ></A
  43776. ></TD
  43777. ><TD
  43778. >Checks whether the files or directories you are 
  43779. about to operate on have the same UID (owner) as the script that is being
  43780. executed. Checks whether the directory in which
  43781. you are about to operate has the same UID (owner) as the script that is being
  43782. executed. (note: only the target is 
  43783.         checked)</TD
  43784. ></TR
  43785. ><TR
  43786. ><TD
  43787. ><A
  43788. HREF="#function.link"
  43789. ><B
  43790. CLASS="function"
  43791. >link()</B
  43792. ></A
  43793. ></TD
  43794. ><TD
  43795. >Checks whether the files or directories you are 
  43796. about to operate on have the same UID (owner) as the script that is being
  43797. executed. Checks whether the directory in which
  43798. you are about to operate has the same UID (owner) as the script that is being
  43799. executed. (note: only the target is 
  43800.         checked)</TD
  43801. ></TR
  43802. ><TR
  43803. ><TD
  43804. ><A
  43805. HREF="#function.apache-request-headers"
  43806. ><B
  43807. CLASS="function"
  43808. >apache_request_headers()</B
  43809. ></A
  43810. ></TD
  43811. ><TD
  43812. >In safe mode, headers beginning with 'authorization' 
  43813.         (case-insensitive) will not be returned.</TD
  43814. ></TR
  43815. ><TR
  43816. ><TD
  43817. ><A
  43818. HREF="#function.header"
  43819. ><B
  43820. CLASS="function"
  43821. >header()</B
  43822. ></A
  43823. ></TD
  43824. ><TD
  43825. >In safe mode, the uid of the script is added to the
  43826.         <VAR
  43827. CLASS="literal"
  43828. >realm</VAR
  43829. > part of the
  43830.         <VAR
  43831. CLASS="literal"
  43832. >WWW-Authenticate</VAR
  43833. > header if you set this
  43834.         header (used for HTTP Authentication).</TD
  43835. ></TR
  43836. ><TR
  43837. ><TD
  43838. ><A
  43839. HREF="#features.http-auth"
  43840. >PHP_AUTH variables</A
  43841. ></TD
  43842. ><TD
  43843. >          In safe mode, the variables <VAR
  43844. CLASS="varname"
  43845. >PHP_AUTH_USER</VAR
  43846. >,
  43847.          <VAR
  43848. CLASS="varname"
  43849. >PHP_AUTH_PW</VAR
  43850. >, and <VAR
  43851. CLASS="varname"
  43852. >AUTH_TYPE</VAR
  43853. >
  43854.          are not available in <VAR
  43855. CLASS="varname"
  43856. >$_SERVER</VAR
  43857. >. Regardless, you
  43858.          can still use <VAR
  43859. CLASS="varname"
  43860. >REMOTE_USER</VAR
  43861. > for the USER.
  43862.          (note: only affected since PHP 4.3.0)
  43863.         </TD
  43864. ></TR
  43865. ><TR
  43866. ><TD
  43867. >          <A
  43868. HREF="#function.highlight-file"
  43869. ><B
  43870. CLASS="function"
  43871. >highlight_file()</B
  43872. ></A
  43873. >,
  43874.          <A
  43875. HREF="#function.show-source"
  43876. ><B
  43877. CLASS="function"
  43878. >show_source()</B
  43879. ></A
  43880. >
  43881.         </TD
  43882. ><TD
  43883. >          Checks whether the files or directories you are 
  43884. about to operate on have the same UID (owner) as the script that is being
  43885. executed. Checks whether the directory in which
  43886. you are about to operate has the same UID (owner) as the script that is being
  43887. executed. (note: only affected since PHP 4.2.1)
  43888.         </TD
  43889. ></TR
  43890. ><TR
  43891. ><TD
  43892. >          <A
  43893. HREF="#function.parse-ini-file"
  43894. ><B
  43895. CLASS="function"
  43896. >parse_ini_file()</B
  43897. ></A
  43898. >
  43899.         </TD
  43900. ><TD
  43901. >          Checks whether the files or directories you are 
  43902. about to operate on have the same UID (owner) as the script that is being
  43903. executed. Checks whether the directory in which
  43904. you are about to operate has the same UID (owner) as the script that is being
  43905. executed. (note: only affected since PHP 4.2.1)
  43906.         </TD
  43907. ></TR
  43908. ><TR
  43909. ><TD
  43910. >          <A
  43911. HREF="#function.set-time-limit"
  43912. ><B
  43913. CLASS="function"
  43914. >set_time_limit()</B
  43915. ></A
  43916. >
  43917.         </TD
  43918. ><TD
  43919. >          Has no affect when PHP is running in <A
  43920. HREF="#ini.safe-mode"
  43921. >safe mode</A
  43922. >.
  43923.         </TD
  43924. ></TR
  43925. ><TR
  43926. ><TD
  43927. >          <A
  43928. HREF="#ini.max-execution-time"
  43929. >max_execution_time</A
  43930. >
  43931.         </TD
  43932. ><TD
  43933. >          Has no affect when PHP is running in <A
  43934. HREF="#ini.safe-mode"
  43935. >safe mode</A
  43936. >.
  43937.         </TD
  43938. ></TR
  43939. ><TR
  43940. ><TD
  43941. >          <A
  43942. HREF="#function.mail"
  43943. ><B
  43944. CLASS="function"
  43945. >mail()</B
  43946. ></A
  43947. >
  43948.         </TD
  43949. ><TD
  43950. >          In safe mode, the fifth parameter is disabled. (note: only affected since PHP 4.2.3)
  43951.         </TD
  43952. ></TR
  43953. ><TR
  43954. ><TD
  43955. >Any function that uses
  43956.          <TT
  43957. CLASS="filename"
  43958. >php4/main/fopen_wrappers.c</TT
  43959. >
  43960.         </TD
  43961. ><TD
  43962. >??</TD
  43963. ></TR
  43964. ></TBODY
  43965. ></TABLE
  43966. ></DIV
  43967. >
  43968.    </P
  43969. ></DIV
  43970. ></DIV
  43971. ><DIV
  43972. CLASS="chapter"
  43973. ><HR><H1
  43974. ><A
  43975. NAME="features.commandline"
  43976. >Chapter 43. Using PHP from the command line</A
  43977. ></H1
  43978. ><P
  43979. >   As of version 4.3.0, PHP supports a new
  43980.   <VAR
  43981. CLASS="literal"
  43982. >SAPI</VAR
  43983. > type (Server Application Programming Interface)
  43984.   named <VAR
  43985. CLASS="literal"
  43986. >CLI</VAR
  43987. > which means <SPAN
  43988. CLASS="emphasis"
  43989. ><I
  43990. CLASS="emphasis"
  43991. >Command Line
  43992.   Interface</I
  43993. ></SPAN
  43994. >. As the name implies, this <VAR
  43995. CLASS="literal"
  43996. >SAPI</VAR
  43997. > type
  43998.   main focus is on developing shell (or desktop as well) applications with
  43999.   PHP.  There are quite a few differences between the
  44000.   <VAR
  44001. CLASS="literal"
  44002. >CLI SAPI</VAR
  44003. > and other <VAR
  44004. CLASS="literal"
  44005. >SAPI</VAR
  44006. >s which are
  44007.   explained in this chapter.  It's worth mentioning
  44008.   that <VAR
  44009. CLASS="literal"
  44010. >CLI</VAR
  44011. > and <VAR
  44012. CLASS="literal"
  44013. >CGI</VAR
  44014. > are different
  44015.   SAPI's although they do share many of the same behaviors.
  44016.  </P
  44017. ><P
  44018. >   The <VAR
  44019. CLASS="literal"
  44020. >CLI SAPI</VAR
  44021. > was released for the first time with
  44022.   PHP 4.2.0, but was still experimental and had
  44023.   to be explicitly enabled with <VAR
  44024. CLASS="option"
  44025. >--enable-cli</VAR
  44026. > when running
  44027.   <B
  44028. CLASS="command"
  44029. >./configure</B
  44030. >.  Since PHP 4.3.0 the
  44031.   <VAR
  44032. CLASS="literal"
  44033. >CLI SAPI</VAR
  44034. > is no longer experimental and the option
  44035.   <VAR
  44036. CLASS="option"
  44037. >--enable-cli</VAR
  44038. > is on by default.  You may use
  44039.   <VAR
  44040. CLASS="option"
  44041. >--disable-cli</VAR
  44042. > to disable it.
  44043.  </P
  44044. ><P
  44045. >   As of PHP 4.3.0, the name, location and existence of the CLI/CGI binaries
  44046.   will differ depending on how PHP is installed on your system.  By default
  44047.   when executing <B
  44048. CLASS="command"
  44049. >make</B
  44050. >, both the CGI and CLI are built and
  44051.   placed as <TT
  44052. CLASS="filename"
  44053. >sapi/cgi/php</TT
  44054. > and <TT
  44055. CLASS="filename"
  44056. >sapi/cli/php</TT
  44057.   respectively, in your PHP source directory.  You will note that both are
  44058.   named php. What happens during <B
  44059. CLASS="command"
  44060. >make install</B
  44061. > depends on
  44062.   your configure line.  If a module SAPI is chosen during configure, such as apxs, or the
  44063.   <VAR
  44064. CLASS="option"
  44065. >--disable-cgi</VAR
  44066. > option is used, the CLI is copied to
  44067.   <TT
  44068. CLASS="filename"
  44069. >{PREFIX}/bin/php</TT
  44070. > during <B
  44071. CLASS="command"
  44072. >make install</B
  44073. >
  44074.   otherwise the CGI is placed there.  So, for example, if <VAR
  44075. CLASS="option"
  44076. >--with--apxs
  44077.   </VAR
  44078. > is in your configure line then the CLI is copied to
  44079.   <TT
  44080. CLASS="filename"
  44081. >{PREFIX}/bin/php</TT
  44082. > during <B
  44083. CLASS="command"
  44084. >make
  44085.   install</B
  44086. >. If you want to override the installation of the CGI
  44087.   binary, use <B
  44088. CLASS="command"
  44089. >make install-cli</B
  44090. > after <B
  44091. CLASS="command"
  44092. >make
  44093.   install</B
  44094. >.  Alternatively you can specify <VAR
  44095. CLASS="option"
  44096. >   --disable-cgi</VAR
  44097. > in your configure line.
  44098.  </P
  44099. ><P
  44100. >   <DIV
  44101. CLASS="note"
  44102. ><BLOCKQUOTE
  44103. CLASS="note"
  44104. ><P
  44105. ><B
  44106. >Note: </B
  44107. >
  44108.     Because both <VAR
  44109. CLASS="option"
  44110. >--enable-cli</VAR
  44111. > and
  44112.     <VAR
  44113. CLASS="option"
  44114. >--enable-cgi</VAR
  44115. > are enabled by default,
  44116.     simply having <VAR
  44117. CLASS="option"
  44118. >--enable-cli</VAR
  44119. > in your
  44120.     configure line does not necessarily mean the CLI will be copied as
  44121.     <TT
  44122. CLASS="filename"
  44123. >{PREFIX}/bin/php</TT
  44124. > during <B
  44125. CLASS="command"
  44126. >make install</B
  44127. >.
  44128.    </P
  44129. ></BLOCKQUOTE
  44130. ></DIV
  44131. >
  44132.  </P
  44133. ><P
  44134. >   The windows packages between PHP 4.2.0 and PHP 4.2.3 distributed the CLI as
  44135.   <TT
  44136. CLASS="filename"
  44137. >php-cli.exe</TT
  44138. >, living in the same folder as the CGI
  44139.   <TT
  44140. CLASS="filename"
  44141. >php.exe</TT
  44142. >. Starting with PHP 4.3.0 the windows package
  44143.   distributes the CLI as <TT
  44144. CLASS="filename"
  44145. >php.exe</TT
  44146. > in a separate folder
  44147.   named <TT
  44148. CLASS="filename"
  44149. >cli</TT
  44150. >, so <TT
  44151. CLASS="filename"
  44152. >cli/php.exe
  44153.   </TT
  44154. >. Starting with PHP 5, the CLI is distributed in the main folder,
  44155.   named  <TT
  44156. CLASS="filename"
  44157. >php.exe</TT
  44158. >. The CGI version is distributed as
  44159.   <TT
  44160. CLASS="filename"
  44161. >php-cgi.exe</TT
  44162. >.
  44163.  </P
  44164. ><P
  44165. >   As of PHP 5, a new <TT
  44166. CLASS="filename"
  44167. >php-win.exe</TT
  44168. > file is distributed.
  44169.   This is equal to the CLI version, except that php-win doesn't output
  44170.   anything and thus provides no console (no "dos box" appears on the screen).
  44171.   This  behavior is similar to php-gtk. You should configure with
  44172.   <VAR
  44173. CLASS="option"
  44174. >--enable-cli-win32</VAR
  44175. >.
  44176.  </P
  44177. ><P
  44178. >   <DIV
  44179. CLASS="note"
  44180. ><BLOCKQUOTE
  44181. CLASS="note"
  44182. ><P
  44183. ><B
  44184. >What SAPI do I have?: </B
  44185. >
  44186.     From a shell, typing <B
  44187. CLASS="command"
  44188. >php -v</B
  44189. > will tell you
  44190.     whether <TT
  44191. CLASS="filename"
  44192. >php</TT
  44193. > is CGI or CLI.  See also the function
  44194.     <A
  44195. HREF="#function.php-sapi-name"
  44196. ><B
  44197. CLASS="function"
  44198. >php_sapi_name()</B
  44199. ></A
  44200. > and the constant <TT
  44201. CLASS="constant"
  44202. ><B
  44203. >     PHP_SAPI</B
  44204. ></TT
  44205. >.
  44206.    </P
  44207. ></BLOCKQUOTE
  44208. ></DIV
  44209. >
  44210.  </P
  44211. ><P
  44212. >   <DIV
  44213. CLASS="note"
  44214. ><BLOCKQUOTE
  44215. CLASS="note"
  44216. ><P
  44217. ><B
  44218. >Note: </B
  44219. >
  44220.     A Unix <VAR
  44221. CLASS="literal"
  44222. >man</VAR
  44223. >ual page was added in PHP 4.3.2.  You may 
  44224.     view this by typing <B
  44225. CLASS="command"
  44226. >man php</B
  44227. > in your shell environment.
  44228.    </P
  44229. ></BLOCKQUOTE
  44230. ></DIV
  44231. >
  44232.  </P
  44233. ><P
  44234. >   Remarkable differences of the <VAR
  44235. CLASS="literal"
  44236. >CLI SAPI</VAR
  44237. > compared to other
  44238.   <VAR
  44239. CLASS="literal"
  44240. >SAPI</VAR
  44241. >s:
  44242.   <P
  44243. ></P
  44244. ><UL
  44245. ><LI
  44246. ><P
  44247. >      Unlike the <VAR
  44248. CLASS="literal"
  44249. >CGI SAPI</VAR
  44250. >, no headers are written to the
  44251.      output.
  44252.     </P
  44253. ><P
  44254. >      Though the <VAR
  44255. CLASS="literal"
  44256. >CGI SAPI</VAR
  44257. > provides a way to suppress HTTP
  44258.      headers, there's no equivalent switch to enable them in the <VAR
  44259. CLASS="literal"
  44260. >CLI
  44261.      SAPI</VAR
  44262. >.
  44263.     </P
  44264. ><P
  44265. >      CLI is started up in quiet mode by default, though the <VAR
  44266. CLASS="option"
  44267. >-q</VAR
  44268. >
  44269.      and <VAR
  44270. CLASS="option"
  44271. >--no-header</VAR
  44272. > switches are kept for compatibility so
  44273.      that you can use older CGI scripts.
  44274.     </P
  44275. ><P
  44276. >      It does not change the working directory to that of the script.
  44277.      (<VAR
  44278. CLASS="option"
  44279. >-C</VAR
  44280. > and <VAR
  44281. CLASS="option"
  44282. >--no-chdir</VAR
  44283. > switches kept for
  44284.      compatibility)
  44285.     </P
  44286. ><P
  44287. >      Plain text error messages (no HTML formatting).
  44288.     </P
  44289. ></LI
  44290. ><LI
  44291. ><P
  44292. >      There are certain <TT
  44293. CLASS="filename"
  44294. >php.ini</TT
  44295. > directives which are overridden by the <VAR
  44296. CLASS="literal"
  44297. >CLI
  44298.      SAPI</VAR
  44299. > because they do not make sense in shell environments:
  44300.     </P
  44301. ><P
  44302. >      <DIV
  44303. CLASS="table"
  44304. ><A
  44305. NAME="AEN7402"
  44306. ></A
  44307. ><P
  44308. ><B
  44309. >Table 43-1. Overridden <TT
  44310. CLASS="filename"
  44311. >php.ini</TT
  44312. > directives</B
  44313. ></P
  44314. ><TABLE
  44315. BORDER="1"
  44316. CLASS="CALSTABLE"
  44317. ><COL><COL><COL><THEAD
  44318. ><TR
  44319. ><TH
  44320. >Directive</TH
  44321. ><TH
  44322. ><VAR
  44323. CLASS="literal"
  44324. >CLI SAPI</VAR
  44325. > default value</TH
  44326. ><TH
  44327. >Comment</TH
  44328. ></TR
  44329. ></THEAD
  44330. ><TBODY
  44331. ><TR
  44332. ><TD
  44333. ><A
  44334. HREF="#ini.html-errors"
  44335. >html_errors</A
  44336. ></TD
  44337. ><TD
  44338. ><TT
  44339. CLASS="constant"
  44340. ><B
  44341. >FALSE</B
  44342. ></TT
  44343. ></TD
  44344. ><TD
  44345. >           It can be quite hard to read the error message in your shell when
  44346.           it's cluttered with all those meaningless <VAR
  44347. CLASS="literal"
  44348. >HTML</VAR
  44349. >
  44350.           tags, therefore this directive defaults to <TT
  44351. CLASS="constant"
  44352. ><B
  44353. >FALSE</B
  44354. ></TT
  44355. >.
  44356.          </TD
  44357. ></TR
  44358. ><TR
  44359. ><TD
  44360. ><A
  44361. HREF="#ini.implicit-flush"
  44362. >implicit_flush</A
  44363. ></TD
  44364. ><TD
  44365. ><TT
  44366. CLASS="constant"
  44367. ><B
  44368. >TRUE</B
  44369. ></TT
  44370. ></TD
  44371. ><TD
  44372. >           It is desired that any output coming from
  44373.           <A
  44374. HREF="#function.print"
  44375. ><B
  44376. CLASS="function"
  44377. >print()</B
  44378. ></A
  44379. >, <A
  44380. HREF="#function.echo"
  44381. ><B
  44382. CLASS="function"
  44383. >echo()</B
  44384. ></A
  44385. > and friends is
  44386.           immediately written to the output and not cached in any buffer. You
  44387.           still can use <A
  44388. HREF="#ref.outcontrol"
  44389. >output buffering</A
  44390. >
  44391.           if you want to defer or manipulate standard output.
  44392.          </TD
  44393. ></TR
  44394. ><TR
  44395. ><TD
  44396. ><A
  44397. HREF="#ini.max-execution-time"
  44398. >max_execution_time</A
  44399. ></TD
  44400. ><TD
  44401. >0 (unlimited)</TD
  44402. ><TD
  44403. >           Due to endless possibilities of using PHP in
  44404.           shell environments, the maximum execution time has been set to
  44405.           unlimited. Whereas applications written for the web are often
  44406.           executed very quickly, shell application tend to have a much
  44407.           longer execution time.
  44408.          </TD
  44409. ></TR
  44410. ><TR
  44411. ><TD
  44412. ><A
  44413. HREF="#ini.register-argc-argv"
  44414. >register_argc_argv</A
  44415. ></TD
  44416. ><TD
  44417. ><TT
  44418. CLASS="constant"
  44419. ><B
  44420. >TRUE</B
  44421. ></TT
  44422. ></TD
  44423. ><TD
  44424. >          <P
  44425. >           Because this setting is <TT
  44426. CLASS="constant"
  44427. ><B
  44428. >TRUE</B
  44429. ></TT
  44430. > you will always have access to
  44431.           <SPAN
  44432. CLASS="emphasis"
  44433. ><I
  44434. CLASS="emphasis"
  44435. >argc</I
  44436. ></SPAN
  44437. > (number of arguments passed to the 
  44438.           application) and <SPAN
  44439. CLASS="emphasis"
  44440. ><I
  44441. CLASS="emphasis"
  44442. >argv</I
  44443. ></SPAN
  44444. > (array of the actual
  44445.           arguments) in the <VAR
  44446. CLASS="literal"
  44447. >CLI SAPI</VAR
  44448. >.
  44449.          </P
  44450. >
  44451.          <P
  44452. >           As of PHP 4.3.0, the PHP variables <VAR
  44453. CLASS="varname"
  44454. >$argc</VAR
  44455. >
  44456.           and <VAR
  44457. CLASS="varname"
  44458. >$argv</VAR
  44459. > are registered and filled in with the appropriate 
  44460.           values when using the <VAR
  44461. CLASS="literal"
  44462. >CLI SAPI</VAR
  44463. >. Prior to this version,
  44464.           the creation of these variables behaved as they do in
  44465.           <VAR
  44466. CLASS="literal"
  44467. >CGI</VAR
  44468. > and <VAR
  44469. CLASS="literal"
  44470. >MODULE</VAR
  44471. > versions
  44472.           which requires the PHP directive
  44473.           <A
  44474. HREF="#ini.register-globals"
  44475. >register_globals</A
  44476. > to
  44477.           be <SPAN
  44478. CLASS="emphasis"
  44479. ><I
  44480. CLASS="emphasis"
  44481. >on</I
  44482. ></SPAN
  44483. >.  Regardless of version or register_globals
  44484.           setting, you can always go through either 
  44485.           <A
  44486. HREF="#reserved.variables.server"
  44487. >$_SERVER</A
  44488. > or 
  44489.           <VAR
  44490. CLASS="varname"
  44491. >$HTTP_SERVER_VARS</VAR
  44492. >.  Example:
  44493.           <VAR
  44494. CLASS="varname"
  44495. >$_SERVER['argv']</VAR
  44496. >
  44497.          </P
  44498. >
  44499.          </TD
  44500. ></TR
  44501. ></TBODY
  44502. ></TABLE
  44503. ></DIV
  44504. >
  44505.     </P
  44506. ><DIV
  44507. CLASS="note"
  44508. ><BLOCKQUOTE
  44509. CLASS="note"
  44510. ><P
  44511. ><B
  44512. >Note: </B
  44513. >
  44514.       These directives cannot be initialized with another value from the
  44515.       configuration file <TT
  44516. CLASS="filename"
  44517. >php.ini</TT
  44518. > or a custom one (if specified). This is a
  44519.       limitation because those default values are applied after all
  44520.       configuration files have been parsed. However, their value can be changed
  44521.       during runtime (which does not make sense for all of those directives,
  44522.       e.g. <A
  44523. HREF="#ini.register-argc-argv"
  44524. >register_argc_argv</A
  44525. >).
  44526.      </P
  44527. ></BLOCKQUOTE
  44528. ></DIV
  44529. ></LI
  44530. ><LI
  44531. ><P
  44532. >      To ease working in the shell environment, the following constants
  44533.      are defined:
  44534.      <DIV
  44535. CLASS="table"
  44536. ><A
  44537. NAME="AEN7463"
  44538. ></A
  44539. ><P
  44540. ><B
  44541. >Table 43-2. CLI specific Constants</B
  44542. ></P
  44543. ><TABLE
  44544. BORDER="1"
  44545. CLASS="CALSTABLE"
  44546. ><COL><COL><THEAD
  44547. ><TR
  44548. ><TH
  44549. >Constant</TH
  44550. ><TH
  44551. >Description</TH
  44552. ></TR
  44553. ></THEAD
  44554. ><TBODY
  44555. ><TR
  44556. ><TD
  44557. ><TT
  44558. CLASS="constant"
  44559. ><B
  44560. >STDIN</B
  44561. ></TT
  44562. ></TD
  44563. ><TD
  44564. >          An already opened stream to <VAR
  44565. CLASS="literal"
  44566. >stdin</VAR
  44567. >. This saves
  44568.          opening it with
  44569.          <TABLE
  44570. BORDER="0"
  44571. BGCOLOR="#E0E0E0"
  44572. CELLPADDING="5"
  44573. ><TR
  44574. ><TD
  44575. ><PRE
  44576. CLASS="php"
  44577. ><?php
  44578.  
  44579. $stdin = fopen('php://stdin', 'r');
  44580.  
  44581. ?></PRE
  44582. ></TD
  44583. ></TR
  44584. ></TABLE
  44585. >
  44586.          If you want to read single line from <VAR
  44587. CLASS="literal"
  44588. >stdin</VAR
  44589. >, you can
  44590.          use
  44591.          <TABLE
  44592. BORDER="0"
  44593. BGCOLOR="#E0E0E0"
  44594. CELLPADDING="5"
  44595. ><TR
  44596. ><TD
  44597. ><PRE
  44598. CLASS="php"
  44599. ><?php
  44600. $line = trim(fgets(STDIN)); // reads one line from STDIN
  44601. fscanf(STDIN, "%d\n", $number); // reads number from STDIN
  44602. ?></PRE
  44603. ></TD
  44604. ></TR
  44605. ></TABLE
  44606. >
  44607.          </TD
  44608. ></TR
  44609. ><TR
  44610. ><TD
  44611. ><TT
  44612. CLASS="constant"
  44613. ><B
  44614. >STDOUT</B
  44615. ></TT
  44616. ></TD
  44617. ><TD
  44618. >          An already opened stream to <VAR
  44619. CLASS="literal"
  44620. >stdout</VAR
  44621. >. This saves
  44622.          opening it with
  44623.          <TABLE
  44624. BORDER="0"
  44625. BGCOLOR="#E0E0E0"
  44626. CELLPADDING="5"
  44627. ><TR
  44628. ><TD
  44629. ><PRE
  44630. CLASS="php"
  44631. ><?php
  44632.  
  44633. $stdout = fopen('php://stdout', 'w');
  44634.  
  44635. ?></PRE
  44636. ></TD
  44637. ></TR
  44638. ></TABLE
  44639. >
  44640.          </TD
  44641. ></TR
  44642. ><TR
  44643. ><TD
  44644. ><TT
  44645. CLASS="constant"
  44646. ><B
  44647. >STDERR</B
  44648. ></TT
  44649. ></TD
  44650. ><TD
  44651. >          An already opened stream to <VAR
  44652. CLASS="literal"
  44653. >stderr</VAR
  44654. >. This saves
  44655.          opening it with
  44656.          <TABLE
  44657. BORDER="0"
  44658. BGCOLOR="#E0E0E0"
  44659. CELLPADDING="5"
  44660. ><TR
  44661. ><TD
  44662. ><PRE
  44663. CLASS="php"
  44664. ><?php
  44665.  
  44666. $stderr = fopen('php://stderr', 'w');
  44667.  
  44668. ?></PRE
  44669. ></TD
  44670. ></TR
  44671. ></TABLE
  44672. >
  44673.          </TD
  44674. ></TR
  44675. ></TBODY
  44676. ></TABLE
  44677. ></DIV
  44678. >
  44679.     </P
  44680. ><P
  44681. >      Given the above, you don't need to open e.g. a stream for
  44682.      <VAR
  44683. CLASS="literal"
  44684. >stderr</VAR
  44685. > yourself but simply use the constant instead of
  44686.      the stream resource:
  44687.      <TABLE
  44688. BORDER="0"
  44689. BGCOLOR="#E0E0E0"
  44690. CELLPADDING="5"
  44691. ><TR
  44692. ><TD
  44693. ><PRE
  44694. CLASS="shell"
  44695. >php -r 'fwrite(STDERR, "stderr\n");'</PRE
  44696. ></TD
  44697. ></TR
  44698. ></TABLE
  44699. >
  44700.      You do not need to explicitly close these streams, as they are closed
  44701.      automatically by PHP when your script ends.
  44702.     </P
  44703. ></LI
  44704. ><LI
  44705. ><P
  44706. >      The <VAR
  44707. CLASS="literal"
  44708. >CLI SAPI</VAR
  44709. > does <SPAN
  44710. CLASS="strong"
  44711. ><B
  44712. CLASS="emphasis"
  44713. >not</B
  44714. ></SPAN
  44715. > change the current directory to the directory
  44716.      of the executed script!
  44717.     </P
  44718. ><P
  44719. >      Example showing the difference to the <VAR
  44720. CLASS="literal"
  44721. >CGI SAPI</VAR
  44722. >:
  44723.      <TABLE
  44724. BORDER="0"
  44725. BGCOLOR="#E0E0E0"
  44726. CELLPADDING="5"
  44727. ><TR
  44728. ><TD
  44729. ><PRE
  44730. CLASS="php"
  44731. ><?php
  44732. // Our simple test application named test.php
  44733. echo getcwd(), "\n";
  44734. ?></PRE
  44735. ></TD
  44736. ></TR
  44737. ></TABLE
  44738. >
  44739.     </P
  44740. ><P
  44741. >      When using the <VAR
  44742. CLASS="literal"
  44743. >CGI</VAR
  44744. > version, the output is:
  44745.      <TABLE
  44746. BORDER="0"
  44747. BGCOLOR="#E0E0E0"
  44748. CELLPADDING="5"
  44749. ><TR
  44750. ><TD
  44751. ><PRE
  44752. CLASS="screen"
  44753. >$ pwd
  44754. /tmp
  44755.  
  44756. $ php -q another_directory/test.php
  44757. /tmp/another_directory</PRE
  44758. ></TD
  44759. ></TR
  44760. ></TABLE
  44761. >
  44762.      This clearly shows that PHP changes its current
  44763.      directory to the one of the executed script.
  44764.     </P
  44765. ><P
  44766. >      Using the <VAR
  44767. CLASS="literal"
  44768. >CLI SAPI</VAR
  44769. > yields:
  44770.      <TABLE
  44771. BORDER="0"
  44772. BGCOLOR="#E0E0E0"
  44773. CELLPADDING="5"
  44774. ><TR
  44775. ><TD
  44776. ><PRE
  44777. CLASS="screen"
  44778. >$ pwd
  44779. /tmp
  44780.  
  44781. $ php -f another_directory/test.php
  44782. /tmp</PRE
  44783. ></TD
  44784. ></TR
  44785. ></TABLE
  44786. >
  44787.      This allows greater flexibility when writing shell tools in
  44788.      PHP.
  44789.     </P
  44790. ><DIV
  44791. CLASS="note"
  44792. ><BLOCKQUOTE
  44793. CLASS="note"
  44794. ><P
  44795. ><B
  44796. >Note: </B
  44797. >
  44798.       The <VAR
  44799. CLASS="literal"
  44800. >CGI SAPI</VAR
  44801. > supports this <VAR
  44802. CLASS="literal"
  44803. >CLI SAPI</VAR
  44804. >
  44805.       behaviour by means of the <VAR
  44806. CLASS="option"
  44807. >-C</VAR
  44808. > switch when run from the
  44809.       command line.
  44810.      </P
  44811. ></BLOCKQUOTE
  44812. ></DIV
  44813. ></LI
  44814. ></UL
  44815. >
  44816.  </P
  44817. ><P
  44818. >   The list of command line options provided by the PHP
  44819.   binary can be queried anytime by running PHP with the
  44820.   <VAR
  44821. CLASS="option"
  44822. >-h</VAR
  44823. > switch:
  44824.   <TABLE
  44825. BORDER="0"
  44826. BGCOLOR="#E0E0E0"
  44827. CELLPADDING="5"
  44828. ><TR
  44829. ><TD
  44830. ><PRE
  44831. CLASS="screen"
  44832. >Usage: php [options] [-f] <file> [args...]
  44833.        php [options] -r <code> [args...]
  44834.        php [options] [-- args...]
  44835.   -s               Display colour syntax highlighted source.
  44836.   -w               Display source with stripped comments and whitespace.
  44837.   -f <file>        Parse <file>.
  44838.   -v               Version number
  44839.   -c <path>|<file> Look for php.ini file in this directory
  44840.   -a               Run interactively
  44841.   -d foo[=bar]     Define INI entry foo with value 'bar'
  44842.   -e               Generate extended information for debugger/profiler
  44843.   -z <file>        Load Zend extension <file>.
  44844.   -l               Syntax check only (lint)
  44845.   -m               Show compiled in modules
  44846.   -i               PHP information
  44847.   -r <code>        Run PHP <code> without using script tags <?..?>
  44848.   -h               This help
  44849.  
  44850.   args...          Arguments passed to script. Use -- args when first argument 
  44851.                    starts with - or script is read from stdin</PRE
  44852. ></TD
  44853. ></TR
  44854. ></TABLE
  44855. >
  44856.  </P
  44857. ><P
  44858. >   The <VAR
  44859. CLASS="literal"
  44860. >CLI SAPI</VAR
  44861. > has three different ways of getting the
  44862.   PHP code you want to execute:
  44863.   <P
  44864. ></P
  44865. ><OL
  44866. TYPE="1"
  44867. ><LI
  44868. ><P
  44869. >      Telling PHP to execute a certain file.
  44870.     </P
  44871. ><P
  44872. >      <TABLE
  44873. BORDER="0"
  44874. BGCOLOR="#E0E0E0"
  44875. CELLPADDING="5"
  44876. ><TR
  44877. ><TD
  44878. ><PRE
  44879. CLASS="screen"
  44880. >php my_script.php
  44881.  
  44882. php -f my_script.php</PRE
  44883. ></TD
  44884. ></TR
  44885. ></TABLE
  44886. >
  44887.      Both ways (whether using the <VAR
  44888. CLASS="option"
  44889. >-f</VAR
  44890. > switch or not) execute
  44891.      the file <TT
  44892. CLASS="filename"
  44893. >my_script.php</TT
  44894. >. You can choose any file to
  44895.      execute - your PHP scripts do not have to end with the
  44896.      <VAR
  44897. CLASS="literal"
  44898. >.php</VAR
  44899. > extension but can have any name or extension
  44900.      you wish.
  44901.     </P
  44902. ></LI
  44903. ><LI
  44904. ><P
  44905. >      Pass the PHP code to execute directly on the command
  44906.      line.
  44907.     </P
  44908. ><P
  44909. >      <TABLE
  44910. BORDER="0"
  44911. BGCOLOR="#E0E0E0"
  44912. CELLPADDING="5"
  44913. ><TR
  44914. ><TD
  44915. ><PRE
  44916. CLASS="screen"
  44917. >php -r 'print_r(get_defined_constants());'</PRE
  44918. ></TD
  44919. ></TR
  44920. ></TABLE
  44921. >
  44922.      Special care has to be taken in regards of shell variable substitution and
  44923.      quoting usage.
  44924.     </P
  44925. ><DIV
  44926. CLASS="note"
  44927. ><BLOCKQUOTE
  44928. CLASS="note"
  44929. ><P
  44930. ><B
  44931. >Note: </B
  44932. >
  44933.       Read the example carefully, there are no beginning or ending tags! The
  44934.       <VAR
  44935. CLASS="option"
  44936. >-r</VAR
  44937. > switch simply does not need them. Using them will
  44938.       lead to a parser error.
  44939.      </P
  44940. ></BLOCKQUOTE
  44941. ></DIV
  44942. ></LI
  44943. ><LI
  44944. ><P
  44945. >      Provide the PHP code to execute via standard input
  44946.      (<VAR
  44947. CLASS="literal"
  44948. >stdin</VAR
  44949. >).
  44950.     </P
  44951. ><P
  44952. >      This gives the powerful ability to dynamically create
  44953.      PHP code and feed it to the binary, as shown in this
  44954.      (fictional) example:
  44955.      <TABLE
  44956. BORDER="0"
  44957. BGCOLOR="#E0E0E0"
  44958. CELLPADDING="5"
  44959. ><TR
  44960. ><TD
  44961. ><PRE
  44962. CLASS="screen"
  44963. >$ some_application | some_filter | php | sort -u >final_output.txt</PRE
  44964. ></TD
  44965. ></TR
  44966. ></TABLE
  44967. >
  44968.     </P
  44969. ></LI
  44970. ></OL
  44971. >
  44972.   You cannot combine any of the three ways to execute code.
  44973.  </P
  44974. ><P
  44975. >   Like every shell application, the PHP binary
  44976.   accepts a number of arguments but your PHP script can
  44977.   also receive arguments. The number of arguments which can be passed to your script
  44978.   is not limited by PHP (the shell has a certain size limit
  44979.   in the number of characters which can be passed; usually you won't hit this
  44980.   limit). The arguments passed to your script are available in the global
  44981.   array <VAR
  44982. CLASS="varname"
  44983. >$argv</VAR
  44984. >. The zero index always contains the script
  44985.   name (which is <VAR
  44986. CLASS="literal"
  44987. >-</VAR
  44988. > in case the PHP code
  44989.   is coming from either standard input or from the command line switch
  44990.   <VAR
  44991. CLASS="option"
  44992. >-r</VAR
  44993. >). The second registered global variable is
  44994.   <VAR
  44995. CLASS="varname"
  44996. >$argc</VAR
  44997. > which contains the number of elements in the
  44998.   <VAR
  44999. CLASS="varname"
  45000. >$argv</VAR
  45001. > array (<SPAN
  45002. CLASS="strong"
  45003. ><B
  45004. CLASS="emphasis"
  45005. >not</B
  45006. ></SPAN
  45007. > the
  45008.   number of arguments passed to the script).
  45009.  </P
  45010. ><P
  45011. >   As long as the arguments you want to pass to your script do not start with
  45012.   the <VAR
  45013. CLASS="literal"
  45014. >-</VAR
  45015. > character, there's nothing special to watch out
  45016.   for. Passing an argument to your script which starts with a
  45017.    <VAR
  45018. CLASS="literal"
  45019. >-</VAR
  45020. > will cause trouble because PHP
  45021.    itself thinks it has to handle it. To prevent this, use the argument list
  45022.    separator <VAR
  45023. CLASS="literal"
  45024. >--</VAR
  45025. >. After this separator has been parsed by
  45026.    PHP, every argument following it is passed
  45027.    untouched to your script.
  45028.  </P
  45029. ><P
  45030. >   <TABLE
  45031. BORDER="0"
  45032. BGCOLOR="#E0E0E0"
  45033. CELLPADDING="5"
  45034. ><TR
  45035. ><TD
  45036. ><PRE
  45037. CLASS="screen"
  45038. ># This will not execute the given code but will show the PHP usage
  45039. $ php -r 'var_dump($argv);' -h
  45040. Usage: php [options] [-f] <file> [args...]
  45041. [...]
  45042.  
  45043. # This will pass the '-h' argument to your script and prevent PHP from showing it's usage
  45044. $ php -r 'var_dump($argv);' -- -h
  45045. array(2) {
  45046.   [0]=>
  45047.   string(1) "-"
  45048.   [1]=>
  45049.   string(2) "-h"
  45050. }</PRE
  45051. ></TD
  45052. ></TR
  45053. ></TABLE
  45054. >
  45055.  </P
  45056. ><P
  45057. >   However, there's another way of using PHP for shell
  45058.   scripting. You can write a script where the first line starts with
  45059.   <VAR
  45060. CLASS="literal"
  45061. >#!/usr/bin/php</VAR
  45062. >. Following this you can place
  45063.   normal PHP code included within the PHP
  45064.   starting and end tags. Once you have set the execution attributes of the file
  45065.   appropriately (e.g. <B
  45066. CLASS="command"
  45067. >chmod +x test</B
  45068. >) your script can be
  45069.   executed like a normal shell or perl script:
  45070.   <TABLE
  45071. BORDER="0"
  45072. BGCOLOR="#E0E0E0"
  45073. CELLPADDING="5"
  45074. ><TR
  45075. ><TD
  45076. ><PRE
  45077. CLASS="php"
  45078. >#!/usr/bin/php
  45079. <?php
  45080. var_dump($argv);
  45081. ?></PRE
  45082. ></TD
  45083. ></TR
  45084. ></TABLE
  45085. >
  45086.   Assuming this file is named <TT
  45087. CLASS="filename"
  45088. >test</TT
  45089. > in the current
  45090.   directory, we can now do the following:
  45091.   <TABLE
  45092. BORDER="0"
  45093. BGCOLOR="#E0E0E0"
  45094. CELLPADDING="5"
  45095. ><TR
  45096. ><TD
  45097. ><PRE
  45098. CLASS="screen"
  45099. >$ chmod +x test
  45100. $ ./test -h -- foo
  45101. array(4) {
  45102.   [0]=>
  45103.   string(6) "./test"
  45104.   [1]=>
  45105.   string(2) "-h"
  45106.   [2]=>
  45107.   string(2) "--"
  45108.   [3]=>
  45109.   string(3) "foo"
  45110. }</PRE
  45111. ></TD
  45112. ></TR
  45113. ></TABLE
  45114. >
  45115.   As you see, in this case no care needs to be taken when passing parameters
  45116.   which start with <VAR
  45117. CLASS="literal"
  45118. >-</VAR
  45119. > to your script.
  45120.  </P
  45121. ><P
  45122. >   Long options are available since PHP 4.3.3.
  45123.   <DIV
  45124. CLASS="table"
  45125. ><A
  45126. NAME="AEN7558"
  45127. ></A
  45128. ><P
  45129. ><B
  45130. >Table 43-3. Command line options</B
  45131. ></P
  45132. ><TABLE
  45133. BORDER="1"
  45134. CLASS="CALSTABLE"
  45135. ><COL><COL><THEAD
  45136. ><TR
  45137. ><TH
  45138. >Option</TH
  45139. ><TH
  45140. >Long Option</TH
  45141. ><TH
  45142. >Description</TH
  45143. ></TR
  45144. ></THEAD
  45145. ><TBODY
  45146. ><TR
  45147. ><TD
  45148. >-s</TD
  45149. ><TD
  45150. >--syntax-highlight</TD
  45151. ><TD
  45152. >        <P
  45153. >         Display colour syntax highlighted source.
  45154.        </P
  45155. >
  45156.        <P
  45157. >         This option uses the internal mechanism to parse the file and produces
  45158.         a <VAR
  45159. CLASS="literal"
  45160. >HTML</VAR
  45161. > highlighted version of it and writes it to
  45162.         standard output. Note that all it does it to generate a block of
  45163.         <VAR
  45164. CLASS="literal"
  45165. ><code> [...] </code></VAR
  45166. >
  45167.         <VAR
  45168. CLASS="literal"
  45169. >HTML</VAR
  45170. > tags, no <VAR
  45171. CLASS="literal"
  45172. >HTML</VAR
  45173. > headers.
  45174.        </P
  45175. >
  45176.        <DIV
  45177. CLASS="note"
  45178. ><BLOCKQUOTE
  45179. CLASS="note"
  45180. ><P
  45181. ><B
  45182. >Note: </B
  45183. >
  45184.          This option does not work together with the <VAR
  45185. CLASS="option"
  45186. >-r</VAR
  45187. >
  45188.          option.
  45189.         </P
  45190. ></BLOCKQUOTE
  45191. ></DIV
  45192. >
  45193.       </TD
  45194. ></TR
  45195. ><TR
  45196. ><TD
  45197. >-s</TD
  45198. ><TD
  45199. >--syntax-highlighting</TD
  45200. ><TD
  45201. >        <P
  45202. >         Alias of <VAR
  45203. CLASS="option"
  45204. >--syntax-highlight</VAR
  45205. >.
  45206.        </P
  45207. >
  45208.       </TD
  45209. ></TR
  45210. ><TR
  45211. ><TD
  45212. >-w</TD
  45213. ><TD
  45214. >--strip</TD
  45215. ><TD
  45216. >        <P
  45217. >         Display source with stripped comments and whitespace.
  45218.        </P
  45219. >
  45220.        <DIV
  45221. CLASS="note"
  45222. ><BLOCKQUOTE
  45223. CLASS="note"
  45224. ><P
  45225. ><B
  45226. >Note: </B
  45227. >
  45228.          This option does not work together with the <VAR
  45229. CLASS="option"
  45230. >-r</VAR
  45231. >
  45232.          option.
  45233.         </P
  45234. ></BLOCKQUOTE
  45235. ></DIV
  45236. >
  45237.       </TD
  45238. ></TR
  45239. ><TR
  45240. ><TD
  45241. >-f</TD
  45242. ><TD
  45243. >--file</TD
  45244. ><TD
  45245. >        <P
  45246. >         Parses and executed the given filename to the <VAR
  45247. CLASS="option"
  45248. >-f</VAR
  45249. >
  45250.         option. This switch is optional and can be left out. Only providing
  45251.         the filename to execute is sufficient.
  45252.        </P
  45253. >
  45254.       </TD
  45255. ></TR
  45256. ><TR
  45257. ><TD
  45258. >-v</TD
  45259. ><TD
  45260. >--version</TD
  45261. ><TD
  45262. >        <P
  45263. >         Writes the PHP, PHP SAPI, and Zend version to standard output, e.g.
  45264.         <TABLE
  45265. BORDER="0"
  45266. BGCOLOR="#E0E0E0"
  45267. CELLPADDING="5"
  45268. ><TR
  45269. ><TD
  45270. ><PRE
  45271. CLASS="screen"
  45272. >$ php -v
  45273. PHP 4.3.0 (cli), Copyright (c) 1997-2002 The PHP Group
  45274. Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies</PRE
  45275. ></TD
  45276. ></TR
  45277. ></TABLE
  45278. >
  45279.        </P
  45280. >
  45281.       </TD
  45282. ></TR
  45283. ><TR
  45284. ><TD
  45285. >-c</TD
  45286. ><TD
  45287. >--php-ini</TD
  45288. ><TD
  45289. >        <P
  45290. >         With this option one can either specify a directory where to look for
  45291.         <TT
  45292. CLASS="filename"
  45293. >php.ini</TT
  45294. > or you can specify a custom <VAR
  45295. CLASS="literal"
  45296. >INI</VAR
  45297. > file
  45298.         directly (which does not need to be named <TT
  45299. CLASS="filename"
  45300. >php.ini</TT
  45301. >), e.g.:
  45302.         <TABLE
  45303. BORDER="0"
  45304. BGCOLOR="#E0E0E0"
  45305. CELLPADDING="5"
  45306. ><TR
  45307. ><TD
  45308. ><PRE
  45309. CLASS="screen"
  45310. >$ php -c /custom/directory/ my_script.php
  45311.  
  45312. $ php -c /custom/directory/custom-file.ini my_script.php</PRE
  45313. ></TD
  45314. ></TR
  45315. ></TABLE
  45316. >
  45317.         If you don't specify this option, file is searched in
  45318.         <A
  45319. HREF="#configuration.file"
  45320. >default locations</A
  45321. >.
  45322.        </P
  45323. >
  45324.       </TD
  45325. ></TR
  45326. ><TR
  45327. ><TD
  45328. >-n</TD
  45329. ><TD
  45330. >--no-php-ini</TD
  45331. ><TD
  45332. >        <P
  45333. >         Ignore <TT
  45334. CLASS="filename"
  45335. >php.ini</TT
  45336. > at all. This switch is available since PHP 4.3.0.
  45337.        </P
  45338. >
  45339.       </TD
  45340. ></TR
  45341. ><TR
  45342. ><TD
  45343. >-d</TD
  45344. ><TD
  45345. >--define</TD
  45346. ><TD
  45347. >        <P
  45348. >         This option allows you to set a custom value for any of the configuration
  45349.         directives allowed in <TT
  45350. CLASS="filename"
  45351. >php.ini</TT
  45352. >. The syntax is:
  45353.         <TABLE
  45354. BORDER="0"
  45355. BGCOLOR="#E0E0E0"
  45356. CELLPADDING="5"
  45357. ><TR
  45358. ><TD
  45359. ><PRE
  45360. CLASS="screen"
  45361. >-d configuration_directive[=value]</PRE
  45362. ></TD
  45363. ></TR
  45364. ></TABLE
  45365. >
  45366.        </P
  45367. >
  45368.        <P
  45369. >         Examples (lines are wrapped for layout reasons):
  45370.         <TABLE
  45371. BORDER="0"
  45372. BGCOLOR="#E0E0E0"
  45373. CELLPADDING="5"
  45374. ><TR
  45375. ><TD
  45376. ><PRE
  45377. CLASS="screen"
  45378. ># Omitting the value part will set the given configuration directive to "1"
  45379. $ php -d max_execution_time
  45380.         -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
  45381. string(1) "1"
  45382.  
  45383. # Passing an empty value part will set the configuration directive to ""
  45384. php -d max_execution_time=
  45385.         -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
  45386. string(0) ""
  45387.  
  45388. # The configuration directive will be set to anything passed after the '=' character
  45389. $  php -d max_execution_time=20
  45390.         -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
  45391. string(2) "20"
  45392. $  php
  45393.         -d max_execution_time=doesntmakesense
  45394.         -r '$foo = ini_get("max_execution_time"); var_dump($foo);'
  45395. string(15) "doesntmakesense"</PRE
  45396. ></TD
  45397. ></TR
  45398. ></TABLE
  45399. >
  45400.        </P
  45401. >
  45402.       </TD
  45403. ></TR
  45404. ><TR
  45405. ><TD
  45406. >-a</TD
  45407. ><TD
  45408. >--interactive</TD
  45409. ><TD
  45410. >        <P
  45411. >         Runs PHP interactively.
  45412.         
  45413.        </P
  45414. >
  45415.       </TD
  45416. ></TR
  45417. ><TR
  45418. ><TD
  45419. >-e</TD
  45420. ><TD
  45421. >--profile-info</TD
  45422. ><TD
  45423. >        <P
  45424. >         Generate extended information for debugger/profiler.
  45425.         
  45426.        </P
  45427. >
  45428.       </TD
  45429. ></TR
  45430. ><TR
  45431. ><TD
  45432. >-z</TD
  45433. ><TD
  45434. >--zend-extension</TD
  45435. ><TD
  45436. >        <P
  45437. >         Load Zend extension. If only a filename is given, PHP tries to load
  45438.         this extension from the current default library path on your system
  45439.         (usually specified <TT
  45440. CLASS="filename"
  45441. >/etc/ld.so.conf</TT
  45442. > on Linux
  45443.         systems).  Passing a filename with an absolute path information will
  45444.         not use the systems library search path. A relative filename with a
  45445.         directory information will tell PHP only to try to
  45446.         load the extension relative to the current directory.
  45447.        </P
  45448. >
  45449.       </TD
  45450. ></TR
  45451. ><TR
  45452. ><TD
  45453. >-l</TD
  45454. ><TD
  45455. >--syntax-check</TD
  45456. ><TD
  45457. >        <P
  45458. >         This option provides a convenient way to only perform a syntax check
  45459.         on the given PHP code. On success, the text
  45460.         <VAR
  45461. CLASS="literal"
  45462. >No syntax errors detected in <filename></VAR
  45463. > is
  45464.         written to standard output and the shell return code is
  45465.         <VAR
  45466. CLASS="literal"
  45467. >0</VAR
  45468. >. On failure, the text <VAR
  45469. CLASS="literal"
  45470. >Errors parsing
  45471.         <filename></VAR
  45472. > in addition to the internal parser error
  45473.         message is written to standard output and the shell return code is set
  45474.         to <VAR
  45475. CLASS="literal"
  45476. >255</VAR
  45477. >.
  45478.        </P
  45479. >
  45480.        <P
  45481. >         This option won't find fatal errors (like undefined functions). Use
  45482.         <VAR
  45483. CLASS="option"
  45484. >-f</VAR
  45485. > if you would like to test for fatal errors too.
  45486.        </P
  45487. >
  45488.        <DIV
  45489. CLASS="note"
  45490. ><BLOCKQUOTE
  45491. CLASS="note"
  45492. ><P
  45493. ><B
  45494. >Note: </B
  45495. >
  45496.          This option does not work together with the <VAR
  45497. CLASS="option"
  45498. >-r</VAR
  45499. >
  45500.          option.
  45501.         </P
  45502. ></BLOCKQUOTE
  45503. ></DIV
  45504. >
  45505.       </TD
  45506. ></TR
  45507. ><TR
  45508. ><TD
  45509. >-m</TD
  45510. ><TD
  45511. >--modules</TD
  45512. ><TD
  45513. >        <P
  45514. >         Using this option, PHP prints out the built in (and loaded) PHP and
  45515.         Zend modules:
  45516.         <TABLE
  45517. BORDER="0"
  45518. BGCOLOR="#E0E0E0"
  45519. CELLPADDING="5"
  45520. ><TR
  45521. ><TD
  45522. ><PRE
  45523. CLASS="screen"
  45524. >$ php -m
  45525. [PHP Modules]
  45526. xml
  45527. tokenizer
  45528. standard
  45529. session
  45530. posix
  45531. pcre
  45532. overload
  45533. mysql
  45534. mbstring
  45535. ctype
  45536.  
  45537. [Zend Modules]</PRE
  45538. ></TD
  45539. ></TR
  45540. ></TABLE
  45541. >
  45542.        </P
  45543. >
  45544.       </TD
  45545. ></TR
  45546. ><TR
  45547. ><TD
  45548. >-i</TD
  45549. ><TD
  45550. >--info</TD
  45551. ><TD
  45552. >        This command line option calls <A
  45553. HREF="#function.phpinfo"
  45554. ><B
  45555. CLASS="function"
  45556. >phpinfo()</B
  45557. ></A
  45558. >, and prints
  45559.        out the results. If PHP is not working correctly, it is
  45560.        advisable to use <B
  45561. CLASS="command"
  45562. >php -i</B
  45563. > and see whether any error
  45564.        messages are printed out before or in place of the information tables.
  45565.        Beware that when using the CGI mode the output is in <VAR
  45566. CLASS="literal"
  45567. >HTML</VAR
  45568.        and therefore quite huge.
  45569.       </TD
  45570. ></TR
  45571. ><TR
  45572. ><TD
  45573. >-r</TD
  45574. ><TD
  45575. >--run</TD
  45576. ><TD
  45577. >        <P
  45578. >         This option allows execution of PHP right from
  45579.         within the command line. The PHP start and end tags
  45580.         (<VAR
  45581. CLASS="literal"
  45582. ><?php</VAR
  45583. > and <VAR
  45584. CLASS="literal"
  45585. >?></VAR
  45586. >) are
  45587.         <SPAN
  45588. CLASS="strong"
  45589. ><B
  45590. CLASS="emphasis"
  45591. >not needed</B
  45592. ></SPAN
  45593. > and will cause a parser
  45594.         error if present.
  45595.        </P
  45596. >
  45597.        <DIV
  45598. CLASS="note"
  45599. ><BLOCKQUOTE
  45600. CLASS="note"
  45601. ><P
  45602. ><B
  45603. >Note: </B
  45604. >
  45605.          Care has to be taken when using this form of PHP
  45606.          to not collide with command line variable substitution done by the
  45607.          shell.
  45608.         </P
  45609. ><P
  45610. >          Example showing a parser error
  45611.          <TABLE
  45612. BORDER="0"
  45613. BGCOLOR="#E0E0E0"
  45614. CELLPADDING="5"
  45615. ><TR
  45616. ><TD
  45617. ><PRE
  45618. CLASS="screen"
  45619. >$ php -r "$foo = get_defined_constants();"
  45620. Command line code(1) : Parse error - parse error, unexpected '='</PRE
  45621. ></TD
  45622. ></TR
  45623. ></TABLE
  45624. >
  45625.          The problem here is that the sh/bash performs variable substitution
  45626.          even when using double quotes <VAR
  45627. CLASS="literal"
  45628. >"</VAR
  45629. >. Since the
  45630.          variable <VAR
  45631. CLASS="varname"
  45632. >$foo</VAR
  45633. > is unlikely to be defined, it
  45634.          expands to nothing which results in the code passed to
  45635.          PHP for execution actually reading:
  45636.          <TABLE
  45637. BORDER="0"
  45638. BGCOLOR="#E0E0E0"
  45639. CELLPADDING="5"
  45640. ><TR
  45641. ><TD
  45642. ><PRE
  45643. CLASS="screen"
  45644. >$ php -r " = get_defined_constants();"</PRE
  45645. ></TD
  45646. ></TR
  45647. ></TABLE
  45648. >
  45649.          The correct way would be to use single quotes <VAR
  45650. CLASS="literal"
  45651. >'</VAR
  45652. >.
  45653.          Variables in single-quoted strings are not expanded
  45654.          by sh/bash.
  45655.          <TABLE
  45656. BORDER="0"
  45657. BGCOLOR="#E0E0E0"
  45658. CELLPADDING="5"
  45659. ><TR
  45660. ><TD
  45661. ><PRE
  45662. CLASS="screen"
  45663. >$ php -r '$foo = get_defined_constants(); var_dump($foo);'
  45664. array(370) {
  45665.   ["E_ERROR"]=>
  45666.   int(1)
  45667.   ["E_WARNING"]=>
  45668.   int(2)
  45669.   ["E_PARSE"]=>
  45670.   int(4)
  45671.   ["E_NOTICE"]=>
  45672.   int(8)
  45673.   ["E_CORE_ERROR"]=>
  45674.   [...]</PRE
  45675. ></TD
  45676. ></TR
  45677. ></TABLE
  45678. >
  45679.          If you are using a shell different from sh/bash, you might experience
  45680.          further issues. Feel free to open a bug report at
  45681.          <A
  45682. HREF="http://bugs.php.net/"
  45683. TARGET="_top"
  45684. >http://bugs.php.net/</A
  45685. > or send a mail to
  45686.          phpdoc@lists.php.net.
  45687.  
  45688.          One can still easily run into troubles when trying to get shell
  45689.          variables into the code or using backslashes for escaping. You've
  45690.          been warned. 
  45691.         </P
  45692. ></BLOCKQUOTE
  45693. ></DIV
  45694. >
  45695.        <DIV
  45696. CLASS="note"
  45697. ><BLOCKQUOTE
  45698. CLASS="note"
  45699. ><P
  45700. ><B
  45701. >Note: </B
  45702. >
  45703.          <VAR
  45704. CLASS="option"
  45705. >-r</VAR
  45706. > is available in the <SPAN
  45707. CLASS="emphasis"
  45708. ><I
  45709. CLASS="emphasis"
  45710. >CLI</I
  45711. ></SPAN
  45712. >
  45713.          SAPI and not in the <SPAN
  45714. CLASS="emphasis"
  45715. ><I
  45716. CLASS="emphasis"
  45717. >CGI</I
  45718. ></SPAN
  45719. > SAPI.
  45720.         </P
  45721. ></BLOCKQUOTE
  45722. ></DIV
  45723. >
  45724.       </TD
  45725. ></TR
  45726. ><TR
  45727. ><TD
  45728. >-h</TD
  45729. ><TD
  45730. >--help</TD
  45731. ><TD
  45732. >        With this option, you can get information about the actual list of
  45733.        command line options and some one line descriptions about what they do.
  45734.       </TD
  45735. ></TR
  45736. ><TR
  45737. ><TD
  45738. >-?</TD
  45739. ><TD
  45740. >--usage</TD
  45741. ><TD
  45742. >        Alias of <VAR
  45743. CLASS="option"
  45744. >--help</VAR
  45745. >.
  45746.       </TD
  45747. ></TR
  45748. ></TBODY
  45749. ></TABLE
  45750. ></DIV
  45751. >
  45752.  </P
  45753. ><P
  45754. >   The PHP executable can be used to run PHP scripts absolutely independent
  45755.   from the web server. If you are on a Unix system, you should add a special
  45756.   first line to your PHP script, and make it executable, so the system will
  45757.   know, what program should run the script. On a Windows platform you can
  45758.   associate <TT
  45759. CLASS="filename"
  45760. >php.exe</TT
  45761. > with the double click option of the
  45762.   <VAR
  45763. CLASS="literal"
  45764. >.php</VAR
  45765. > files, or you can make a batch
  45766.   file to run the script through PHP. The first line added to the script to
  45767.   work on Unix won't hurt on Windows, so you can write cross platform programs
  45768.   this way. A simple example of writing a command line PHP program can be
  45769.   found below.
  45770.  </P
  45771. ><P
  45772. >   <TABLE
  45773. WIDTH="100%"
  45774. BORDER="0"
  45775. CELLPADDING="0"
  45776. CELLSPACING="0"
  45777. CLASS="EXAMPLE"
  45778. ><TR
  45779. ><TD
  45780. ><DIV
  45781. CLASS="example"
  45782. ><A
  45783. NAME="AEN7710"
  45784. ></A
  45785. ><P
  45786. ><B
  45787. >Example 43-1. Script intended to be run from command line (script.php)</B
  45788. ></P
  45789. ><TABLE
  45790. BORDER="0"
  45791. BGCOLOR="#E0E0E0"
  45792. CELLPADDING="5"
  45793. ><TR
  45794. ><TD
  45795. ><PRE
  45796. CLASS="php"
  45797. >#!/usr/bin/php
  45798. <?php
  45799.  
  45800. if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
  45801. ?>
  45802.  
  45803. This is a command line PHP script with one option.
  45804.  
  45805.   Usage:
  45806.   <?php echo $argv[0]; ?> <option>
  45807.  
  45808.   <option> can be some word you would like
  45809.   to print out. With the --help, -help, -h,
  45810.   or -? options, you can get this help.
  45811.  
  45812. <?php
  45813. } else {
  45814.     echo $argv[1];
  45815. }
  45816. ?></PRE
  45817. ></TD
  45818. ></TR
  45819. ></TABLE
  45820. ></DIV
  45821. ></TD
  45822. ></TR
  45823. ></TABLE
  45824. >
  45825.  </P
  45826. ><P
  45827. >   In the script above, we used the special first line to indicate
  45828.   that this file should be run by PHP. We work with a CLI version
  45829.   here, so there will be no HTTP header printouts. There are two
  45830.   variables you can use while writing command line applications with
  45831.   PHP: <VAR
  45832. CLASS="varname"
  45833. >$argc</VAR
  45834. > and <VAR
  45835. CLASS="varname"
  45836. >$argv</VAR
  45837. >. The
  45838.   first is the number of arguments plus one (the name of the script
  45839.   running). The second is an array containing the arguments, starting
  45840.   with the script name as number zero (<VAR
  45841. CLASS="varname"
  45842. >$argv[0]</VAR
  45843. >).
  45844.  </P
  45845. ><P
  45846. >   In the program above we checked if there are less or more than one
  45847.   arguments. Also if the argument was <VAR
  45848. CLASS="option"
  45849. >--help</VAR
  45850. >,
  45851.   <VAR
  45852. CLASS="option"
  45853. >-help</VAR
  45854. >, <VAR
  45855. CLASS="option"
  45856. >-h</VAR
  45857. > or <VAR
  45858. CLASS="option"
  45859. >-?</VAR
  45860. >,
  45861.   we printed out the help message, printing the script name dynamically.
  45862.   If we received some other argument we echoed that out.
  45863.  </P
  45864. ><P
  45865. >   If you would like to run the above script on Unix, you need to
  45866.   make it executable, and simply call it as
  45867.   <B
  45868. CLASS="command"
  45869. >script.php echothis</B
  45870. > or
  45871.   <B
  45872. CLASS="command"
  45873. >script.php -h</B
  45874. >. On Windows, you can make a
  45875.   batch file for this task:
  45876.  </P
  45877. ><P
  45878. >   <TABLE
  45879. WIDTH="100%"
  45880. BORDER="0"
  45881. CELLPADDING="0"
  45882. CELLSPACING="0"
  45883. CLASS="EXAMPLE"
  45884. ><TR
  45885. ><TD
  45886. ><DIV
  45887. CLASS="example"
  45888. ><A
  45889. NAME="AEN7726"
  45890. ></A
  45891. ><P
  45892. ><B
  45893. >Example 43-2. Batch file to run a command line PHP script (script.bat)</B
  45894. ></P
  45895. ><TABLE
  45896. BORDER="0"
  45897. BGCOLOR="#E0E0E0"
  45898. CELLPADDING="5"
  45899. ><TR
  45900. ><TD
  45901. ><PRE
  45902. CLASS="shell"
  45903. >@c:\php\cli\php.exe script.php %1 %2 %3 %4</PRE
  45904. ></TD
  45905. ></TR
  45906. ></TABLE
  45907. ></DIV
  45908. ></TD
  45909. ></TR
  45910. ></TABLE
  45911. >
  45912.  </P
  45913. ><P
  45914. >   Assuming you named the above program
  45915.   <TT
  45916. CLASS="filename"
  45917. >script.php</TT
  45918. >, and you have your
  45919.   CLI <TT
  45920. CLASS="filename"
  45921. >php.exe</TT
  45922. > in
  45923.   <TT
  45924. CLASS="filename"
  45925. >c:\php\cli\php.exe</TT
  45926. > this batch file
  45927.   will run it for you with your added options:
  45928.   <B
  45929. CLASS="command"
  45930. >script.bat echothis</B
  45931. > or
  45932.   <B
  45933. CLASS="command"
  45934. >script.bat -h</B
  45935. >.
  45936.  </P
  45937. ><P
  45938. >   See also the <A
  45939. HREF="#ref.readline"
  45940. >Readline</A
  45941. >
  45942.   extension documentation for more functions you can use
  45943.   to enhance your command line applications in PHP.
  45944.  </P
  45945. ></DIV
  45946. ></DIV
  45947. ><DIV
  45948. CLASS="PART"
  45949. ><A
  45950. NAME="funcref"
  45951. ></A
  45952. ><DIV
  45953. CLASS="TITLEPAGE"
  45954. ><H1
  45955. CLASS="title"
  45956. >VI. Function Reference</H1
  45957. ><DIV
  45958. CLASS="TOC"
  45959. ><DL
  45960. ><DT
  45961. ><B
  45962. >Table of Contents</B
  45963. ></DT
  45964. ><DT
  45965. >I. <A
  45966. HREF="#ref.apache"
  45967. >Apache-specific Functions</A
  45968. ></DT
  45969. ><DT
  45970. >II. <A
  45971. HREF="#ref.apd"
  45972. >Advanced PHP debugger</A
  45973. ></DT
  45974. ><DT
  45975. >III. <A
  45976. HREF="#ref.array"
  45977. >Array Functions</A
  45978. ></DT
  45979. ><DT
  45980. >IV. <A
  45981. HREF="#ref.aspell"
  45982. >Aspell functions [deprecated]</A
  45983. ></DT
  45984. ><DT
  45985. >V. <A
  45986. HREF="#ref.bc"
  45987. >BCMath Arbitrary Precision Mathematics Functions</A
  45988. ></DT
  45989. ><DT
  45990. >VI. <A
  45991. HREF="#ref.bcompiler"
  45992. >PHP bytecode Compiler</A
  45993. ></DT
  45994. ><DT
  45995. >VII. <A
  45996. HREF="#ref.bzip2"
  45997. >Bzip2 Compression Functions</A
  45998. ></DT
  45999. ><DT
  46000. >VIII. <A
  46001. HREF="#ref.calendar"
  46002. >Calendar Functions</A
  46003. ></DT
  46004. ><DT
  46005. >IX. <A
  46006. HREF="#ref.ccvs"
  46007. >CCVS API Functions [deprecated]</A
  46008. ></DT
  46009. ><DT
  46010. >X. <A
  46011. HREF="#ref.com"
  46012. >COM and .Net (Windows)</A
  46013. ></DT
  46014. ><DT
  46015. >XI. <A
  46016. HREF="#ref.classkit"
  46017. >Classkit Functions</A
  46018. ></DT
  46019. ><DT
  46020. >XII. <A
  46021. HREF="#ref.classobj"
  46022. >Class/Object Functions</A
  46023. ></DT
  46024. ><DT
  46025. >XIII. <A
  46026. HREF="#ref.cpdf"
  46027. >ClibPDF Functions</A
  46028. ></DT
  46029. ><DT
  46030. >XIV. <A
  46031. HREF="#ref.crack"
  46032. >Crack Functions</A
  46033. ></DT
  46034. ><DT
  46035. >XV. <A
  46036. HREF="#ref.curl"
  46037. >CURL, Client URL Library Functions</A
  46038. ></DT
  46039. ><DT
  46040. >XVI. <A
  46041. HREF="#ref.cybercash"
  46042. >Cybercash Payment Functions</A
  46043. ></DT
  46044. ><DT
  46045. >XVII. <A
  46046. HREF="#ref.cyrus"
  46047. >Cyrus IMAP administration Functions</A
  46048. ></DT
  46049. ><DT
  46050. >XVIII. <A
  46051. HREF="#ref.ctype"
  46052. >Character Type Functions</A
  46053. ></DT
  46054. ><DT
  46055. >XIX. <A
  46056. HREF="#ref.dba"
  46057. >Database (dbm-style) Abstraction Layer Functions</A
  46058. ></DT
  46059. ><DT
  46060. >XX. <A
  46061. HREF="#ref.datetime"
  46062. >Date and Time Functions</A
  46063. ></DT
  46064. ><DT
  46065. >XXI. <A
  46066. HREF="#ref.dbase"
  46067. >dBase Functions</A
  46068. ></DT
  46069. ><DT
  46070. >XXII. <A
  46071. HREF="#ref.dbm"
  46072. >DBM Functions [deprecated]</A
  46073. ></DT
  46074. ><DT
  46075. >XXIII. <A
  46076. HREF="#ref.dbx"
  46077. >dbx Functions</A
  46078. ></DT
  46079. ><DT
  46080. >XXIV. <A
  46081. HREF="#ref.dbplus"
  46082. >DB++ Functions</A
  46083. ></DT
  46084. ><DT
  46085. >XXV. <A
  46086. HREF="#ref.dio"
  46087. >Direct IO Functions</A
  46088. ></DT
  46089. ><DT
  46090. >XXVI. <A
  46091. HREF="#ref.dir"
  46092. >Directory Functions</A
  46093. ></DT
  46094. ><DT
  46095. >XXVII. <A
  46096. HREF="#ref.dom"
  46097. >DOM Functions</A
  46098. ></DT
  46099. ><DT
  46100. >XXVIII. <A
  46101. HREF="#ref.domxml"
  46102. >DOM XML Functions</A
  46103. ></DT
  46104. ><DT
  46105. >XXIX. <A
  46106. HREF="#ref.dotnet"
  46107. >.NET Functions</A
  46108. ></DT
  46109. ><DT
  46110. >XXX. <A
  46111. HREF="#ref.errorfunc"
  46112. >Error Handling and Logging Functions</A
  46113. ></DT
  46114. ><DT
  46115. >XXXI. <A
  46116. HREF="#ref.exif"
  46117. >Exif Functions</A
  46118. ></DT
  46119. ><DT
  46120. >XXXII. <A
  46121. HREF="#ref.fam"
  46122. >File Alteration Monitor Functions</A
  46123. ></DT
  46124. ><DT
  46125. >XXXIII. <A
  46126. HREF="#ref.fbsql"
  46127. >FrontBase Functions</A
  46128. ></DT
  46129. ><DT
  46130. >XXXIV. <A
  46131. HREF="#ref.filepro"
  46132. >filePro Functions</A
  46133. ></DT
  46134. ><DT
  46135. >XXXV. <A
  46136. HREF="#ref.filesystem"
  46137. >Filesystem Functions</A
  46138. ></DT
  46139. ><DT
  46140. >XXXVI. <A
  46141. HREF="#ref.fdf"
  46142. >Forms Data Format Functions</A
  46143. ></DT
  46144. ><DT
  46145. >XXXVII. <A
  46146. HREF="#ref.fribidi"
  46147. >FriBiDi Functions</A
  46148. ></DT
  46149. ><DT
  46150. >XXXVIII. <A
  46151. HREF="#ref.ftp"
  46152. >FTP Functions</A
  46153. ></DT
  46154. ><DT
  46155. >XXXIX. <A
  46156. HREF="#ref.funchand"
  46157. >Function Handling Functions</A
  46158. ></DT
  46159. ><DT
  46160. >XL. <A
  46161. HREF="#ref.gettext"
  46162. >Gettext</A
  46163. ></DT
  46164. ><DT
  46165. >XLI. <A
  46166. HREF="#ref.gmp"
  46167. >GMP Functions</A
  46168. ></DT
  46169. ><DT
  46170. >XLII. <A
  46171. HREF="#ref.http"
  46172. >HTTP Functions</A
  46173. ></DT
  46174. ><DT
  46175. >XLIII. <A
  46176. HREF="#ref.hw"
  46177. >Hyperwave Functions</A
  46178. ></DT
  46179. ><DT
  46180. >XLIV. <A
  46181. HREF="#ref.hwapi"
  46182. >Hyperwave API Functions</A
  46183. ></DT
  46184. ><DT
  46185. >XLV. <A
  46186. HREF="#ref.iconv"
  46187. >iconv Functions</A
  46188. ></DT
  46189. ><DT
  46190. >XLVI. <A
  46191. HREF="#ref.image"
  46192. >Image Functions</A
  46193. ></DT
  46194. ><DT
  46195. >XLVII. <A
  46196. HREF="#ref.imap"
  46197. >IMAP, POP3 and NNTP Functions</A
  46198. ></DT
  46199. ><DT
  46200. >XLVIII. <A
  46201. HREF="#ref.ifx"
  46202. >Informix Functions</A
  46203. ></DT
  46204. ><DT
  46205. >XLIX. <A
  46206. HREF="#ref.ibase"
  46207. >Firebird/InterBase Functions</A
  46208. ></DT
  46209. ><DT
  46210. >L. <A
  46211. HREF="#ref.id3"
  46212. >ID3 Functions</A
  46213. ></DT
  46214. ><DT
  46215. >LI. <A
  46216. HREF="#ref.ingres"
  46217. >Ingres II Functions</A
  46218. ></DT
  46219. ><DT
  46220. >LII. <A
  46221. HREF="#ref.ircg"
  46222. >IRC Gateway Functions</A
  46223. ></DT
  46224. ><DT
  46225. >LIII. <A
  46226. HREF="#ref.java"
  46227. >PHP / Java Integration</A
  46228. ></DT
  46229. ><DT
  46230. >LIV. <A
  46231. HREF="#ref.ldap"
  46232. >LDAP Functions</A
  46233. ></DT
  46234. ><DT
  46235. >LV. <A
  46236. HREF="#ref.lzf"
  46237. >LZF Functions</A
  46238. ></DT
  46239. ><DT
  46240. >LVI. <A
  46241. HREF="#ref.mail"
  46242. >Mail Functions</A
  46243. ></DT
  46244. ><DT
  46245. >LVII. <A
  46246. HREF="#ref.mailparse"
  46247. >mailparse Functions</A
  46248. ></DT
  46249. ><DT
  46250. >LVIII. <A
  46251. HREF="#ref.math"
  46252. >Mathematical Functions</A
  46253. ></DT
  46254. ><DT
  46255. >LIX. <A
  46256. HREF="#ref.mbstring"
  46257. >Multibyte String Functions</A
  46258. ></DT
  46259. ><DT
  46260. >LX. <A
  46261. HREF="#ref.mcal"
  46262. >MCAL Functions</A
  46263. ></DT
  46264. ><DT
  46265. >LXI. <A
  46266. HREF="#ref.mcrypt"
  46267. >Mcrypt Encryption Functions</A
  46268. ></DT
  46269. ><DT
  46270. >LXII. <A
  46271. HREF="#ref.mcve"
  46272. >MCVE Payment Functions</A
  46273. ></DT
  46274. ><DT
  46275. >LXIII. <A
  46276. HREF="#ref.memcache"
  46277. >Memcache Functions</A
  46278. ></DT
  46279. ><DT
  46280. >LXIV. <A
  46281. HREF="#ref.mhash"
  46282. >Mhash Functions</A
  46283. ></DT
  46284. ><DT
  46285. >LXV. <A
  46286. HREF="#ref.mime-magic"
  46287. >Mimetype Functions</A
  46288. ></DT
  46289. ><DT
  46290. >LXVI. <A
  46291. HREF="#ref.mssql"
  46292. >Microsoft SQL Server Functions</A
  46293. ></DT
  46294. ><DT
  46295. >LXVII. <A
  46296. HREF="#ref.ming"
  46297. >Ming functions for Flash</A
  46298. ></DT
  46299. ><DT
  46300. >LXVIII. <A
  46301. HREF="#ref.misc"
  46302. >Miscellaneous Functions</A
  46303. ></DT
  46304. ><DT
  46305. >LXIX. <A
  46306. HREF="#ref.mnogosearch"
  46307. >mnoGoSearch Functions</A
  46308. ></DT
  46309. ><DT
  46310. >LXX. <A
  46311. HREF="#ref.msql"
  46312. >mSQL Functions</A
  46313. ></DT
  46314. ><DT
  46315. >LXXI. <A
  46316. HREF="#ref.mysql"
  46317. >MySQL Functions</A
  46318. ></DT
  46319. ><DT
  46320. >LXXII. <A
  46321. HREF="#ref.mysqli"
  46322. >Improved MySQL Extension</A
  46323. ></DT
  46324. ><DT
  46325. >LXXIII. <A
  46326. HREF="#ref.msession"
  46327. >Mohawk Software Session Handler Functions</A
  46328. ></DT
  46329. ><DT
  46330. >LXXIV. <A
  46331. HREF="#ref.muscat"
  46332. >muscat Functions</A
  46333. ></DT
  46334. ><DT
  46335. >LXXV. <A
  46336. HREF="#ref.network"
  46337. >Network Functions</A
  46338. ></DT
  46339. ><DT
  46340. >LXXVI. <A
  46341. HREF="#ref.ncurses"
  46342. >Ncurses Terminal Screen Control Functions</A
  46343. ></DT
  46344. ><DT
  46345. >LXXVII. <A
  46346. HREF="#ref.notes"
  46347. >Lotus Notes Functions</A
  46348. ></DT
  46349. ><DT
  46350. >LXXVIII. <A
  46351. HREF="#ref.nsapi"
  46352. >NSAPI-specific Functions</A
  46353. ></DT
  46354. ><DT
  46355. >LXXIX. <A
  46356. HREF="#ref.uodbc"
  46357. >ODBC Functions (Unified)</A
  46358. ></DT
  46359. ><DT
  46360. >LXXX. <A
  46361. HREF="#ref.objaggregation"
  46362. >Object Aggregation/Composition Functions</A
  46363. ></DT
  46364. ><DT
  46365. >LXXXI. <A
  46366. HREF="#ref.oci8"
  46367. >Oracle 8 functions</A
  46368. ></DT
  46369. ><DT
  46370. >LXXXII. <A
  46371. HREF="#ref.openal"
  46372. >OpenAL Audio Bindings</A
  46373. ></DT
  46374. ><DT
  46375. >LXXXIII. <A
  46376. HREF="#ref.openssl"
  46377. >OpenSSL Functions</A
  46378. ></DT
  46379. ><DT
  46380. >LXXXIV. <A
  46381. HREF="#ref.oracle"
  46382. >Oracle Functions</A
  46383. ></DT
  46384. ><DT
  46385. >LXXXV. <A
  46386. HREF="#ref.ovrimos"
  46387. >Ovrimos SQL Functions</A
  46388. ></DT
  46389. ><DT
  46390. >LXXXVI. <A
  46391. HREF="#ref.outcontrol"
  46392. >Output Control Functions</A
  46393. ></DT
  46394. ><DT
  46395. >LXXXVII. <A
  46396. HREF="#ref.overload"
  46397. >Object property and method call overloading</A
  46398. ></DT
  46399. ><DT
  46400. >LXXXVIII. <A
  46401. HREF="#ref.parsekit"
  46402. >Parsekit Functions</A
  46403. ></DT
  46404. ><DT
  46405. >LXXXIX. <A
  46406. HREF="#ref.pdf"
  46407. >PDF functions</A
  46408. ></DT
  46409. ><DT
  46410. >XC. <A
  46411. HREF="#ref.pdo"
  46412. >PDO Functions</A
  46413. ></DT
  46414. ><DT
  46415. >XCI. <A
  46416. HREF="#ref.pfpro"
  46417. >Verisign Payflow Pro Functions</A
  46418. ></DT
  46419. ><DT
  46420. >XCII. <A
  46421. HREF="#ref.info"
  46422. >PHP Options&Information</A
  46423. ></DT
  46424. ><DT
  46425. >XCIII. <A
  46426. HREF="#ref.posix"
  46427. >POSIX Functions</A
  46428. ></DT
  46429. ><DT
  46430. >XCIV. <A
  46431. HREF="#ref.pgsql"
  46432. >PostgreSQL Functions</A
  46433. ></DT
  46434. ><DT
  46435. >XCV. <A
  46436. HREF="#ref.pcntl"
  46437. >Process Control Functions</A
  46438. ></DT
  46439. ><DT
  46440. >XCVI. <A
  46441. HREF="#ref.exec"
  46442. >Program Execution Functions</A
  46443. ></DT
  46444. ><DT
  46445. >XCVII. <A
  46446. HREF="#ref.printer"
  46447. >Printer Functions</A
  46448. ></DT
  46449. ><DT
  46450. >XCVIII. <A
  46451. HREF="#ref.pspell"
  46452. >Pspell Functions</A
  46453. ></DT
  46454. ><DT
  46455. >XCIX. <A
  46456. HREF="#ref.readline"
  46457. >GNU Readline</A
  46458. ></DT
  46459. ><DT
  46460. >C. <A
  46461. HREF="#ref.recode"
  46462. >GNU Recode Functions</A
  46463. ></DT
  46464. ><DT
  46465. >CI. <A
  46466. HREF="#ref.pcre"
  46467. >Regular Expression Functions (Perl-Compatible)</A
  46468. ></DT
  46469. ><DT
  46470. >CII. <A
  46471. HREF="#ref.qtdom"
  46472. >qtdom Functions</A
  46473. ></DT
  46474. ><DT
  46475. >CIII. <A
  46476. HREF="#ref.rar"
  46477. >Rar Functions</A
  46478. ></DT
  46479. ><DT
  46480. >CIV. <A
  46481. HREF="#ref.regex"
  46482. >Regular Expression Functions (POSIX Extended)</A
  46483. ></DT
  46484. ><DT
  46485. >CV. <A
  46486. HREF="#ref.ssh2"
  46487. >Secure Shell2 Functions</A
  46488. ></DT
  46489. ><DT
  46490. >CVI. <A
  46491. HREF="#ref.sem"
  46492. >Semaphore, Shared Memory and IPC Functions</A
  46493. ></DT
  46494. ><DT
  46495. >CVII. <A
  46496. HREF="#ref.sesam"
  46497. >SESAM Database Functions</A
  46498. ></DT
  46499. ><DT
  46500. >CVIII. <A
  46501. HREF="#ref.session"
  46502. >Session Handling Functions</A
  46503. ></DT
  46504. ><DT
  46505. >CIX. <A
  46506. HREF="#ref.shmop"
  46507. >Shared Memory Functions</A
  46508. ></DT
  46509. ><DT
  46510. >CX. <A
  46511. HREF="#ref.simplexml"
  46512. >SimpleXML functions</A
  46513. ></DT
  46514. ><DT
  46515. >CXI. <A
  46516. HREF="#ref.soap"
  46517. >SOAP Functions</A
  46518. ></DT
  46519. ><DT
  46520. >CXII. <A
  46521. HREF="#ref.sqlite"
  46522. >SQLite</A
  46523. ></DT
  46524. ><DT
  46525. >CXIII. <A
  46526. HREF="#ref.swf"
  46527. >Shockwave Flash Functions</A
  46528. ></DT
  46529. ><DT
  46530. >CXIV. <A
  46531. HREF="#ref.snmp"
  46532. >SNMP Functions</A
  46533. ></DT
  46534. ><DT
  46535. >CXV. <A
  46536. HREF="#ref.sockets"
  46537. >Socket Functions</A
  46538. ></DT
  46539. ><DT
  46540. >CXVI. <A
  46541. HREF="#ref.spl"
  46542. >Standard PHP Library (SPL) Functions</A
  46543. ></DT
  46544. ><DT
  46545. >CXVII. <A
  46546. HREF="#ref.stream"
  46547. >Stream Functions</A
  46548. ></DT
  46549. ><DT
  46550. >CXVIII. <A
  46551. HREF="#ref.strings"
  46552. >String Functions</A
  46553. ></DT
  46554. ><DT
  46555. >CXIX. <A
  46556. HREF="#ref.sybase"
  46557. >Sybase Functions</A
  46558. ></DT
  46559. ><DT
  46560. >CXX. <A
  46561. HREF="#ref.tcpwrap"
  46562. >TCP Wrappers Functions</A
  46563. ></DT
  46564. ><DT
  46565. >CXXI. <A
  46566. HREF="#ref.tidy"
  46567. >Tidy Functions</A
  46568. ></DT
  46569. ><DT
  46570. >CXXII. <A
  46571. HREF="#ref.tokenizer"
  46572. >Tokenizer Functions</A
  46573. ></DT
  46574. ><DT
  46575. >CXXIII. <A
  46576. HREF="#ref.url"
  46577. >URL Functions</A
  46578. ></DT
  46579. ><DT
  46580. >CXXIV. <A
  46581. HREF="#ref.var"
  46582. >Variable Functions</A
  46583. ></DT
  46584. ><DT
  46585. >CXXV. <A
  46586. HREF="#ref.vpopmail"
  46587. >vpopmail Functions</A
  46588. ></DT
  46589. ><DT
  46590. >CXXVI. <A
  46591. HREF="#ref.w32api"
  46592. >W32api Functions</A
  46593. ></DT
  46594. ><DT
  46595. >CXXVII. <A
  46596. HREF="#ref.wddx"
  46597. >WDDX Functions</A
  46598. ></DT
  46599. ><DT
  46600. >CXXVIII. <A
  46601. HREF="#ref.xattr"
  46602. >xattr Functions</A
  46603. ></DT
  46604. ><DT
  46605. >CXXIX. <A
  46606. HREF="#ref.xml"
  46607. >XML Parser Functions</A
  46608. ></DT
  46609. ><DT
  46610. >CXXX. <A
  46611. HREF="#ref.xmlrpc"
  46612. >XML-RPC Functions</A
  46613. ></DT
  46614. ><DT
  46615. >CXXXI. <A
  46616. HREF="#ref.xdiff"
  46617. >xdiff Functions</A
  46618. ></DT
  46619. ><DT
  46620. >CXXXII. <A
  46621. HREF="#ref.xsl"
  46622. >XSL functions</A
  46623. ></DT
  46624. ><DT
  46625. >CXXXIII. <A
  46626. HREF="#ref.xslt"
  46627. >XSLT Functions</A
  46628. ></DT
  46629. ><DT
  46630. >CXXXIV. <A
  46631. HREF="#ref.yaz"
  46632. >YAZ Functions</A
  46633. ></DT
  46634. ><DT
  46635. >CXXXV. <A
  46636. HREF="#ref.nis"
  46637. >YP/NIS Functions</A
  46638. ></DT
  46639. ><DT
  46640. >CXXXVI. <A
  46641. HREF="#ref.zip"
  46642. >Zip File Functions (Read Only Access)</A
  46643. ></DT
  46644. ><DT
  46645. >CXXXVII. <A
  46646. HREF="#ref.zlib"
  46647. >Zlib Compression Functions</A
  46648. ></DT
  46649. ></DL
  46650. ></DIV
  46651. ></DIV
  46652. ><DIV
  46653. CLASS="reference"
  46654. ><A
  46655. NAME="ref.apache"
  46656. ></A
  46657. ><DIV
  46658. CLASS="TITLEPAGE"
  46659. ><H1
  46660. CLASS="title"
  46661. >I. Apache-specific Functions</H1
  46662. ><DIV
  46663. CLASS="PARTINTRO"
  46664. ><A
  46665. NAME="AEN7742"
  46666. ></A
  46667. ><DIV
  46668. CLASS="section"
  46669. ><H2
  46670. CLASS="section"
  46671. ><A
  46672. NAME="apache.intro"
  46673. >Introduction</A
  46674. ></H2
  46675. ><P
  46676. >      These functions are only available when running PHP as an Apache module.
  46677.     </P
  46678. ><DIV
  46679. CLASS="note"
  46680. ><BLOCKQUOTE
  46681. CLASS="note"
  46682. ><P
  46683. ><B
  46684. >Note: </B
  46685. >
  46686.       As of PHP 4.3.2, <VAR
  46687. CLASS="envar"
  46688. >PATH_TRANSLATED</VAR
  46689. > is no longer set 
  46690.       implicitly under the Apache 2 <ACRONYM
  46691. CLASS="acronym"
  46692. >SAPI</ACRONYM
  46693. > in contrast 
  46694.       to the situation in Apache 1, where it's set to the same value as 
  46695.       the <VAR
  46696. CLASS="envar"
  46697. >SCRIPT_FILENAME</VAR
  46698. > server variable when it's not 
  46699.       populated by Apache.  This change was made to comply with the 
  46700.       <ACRONYM
  46701. CLASS="acronym"
  46702. >CGI</ACRONYM
  46703. > specification that 
  46704.       <VAR
  46705. CLASS="envar"
  46706. >PATH_TRANSLATED</VAR
  46707. > should only exist if 
  46708.       <VAR
  46709. CLASS="envar"
  46710. >PATH_INFO</VAR
  46711. > is defined.
  46712.      </P
  46713. ><P
  46714. >       Apache 2 users may use <VAR
  46715. CLASS="literal"
  46716. >AcceptPathInfo = On</VAR
  46717. > inside
  46718.       <TT
  46719. CLASS="filename"
  46720. >httpd.conf</TT
  46721. > to define <VAR
  46722. CLASS="envar"
  46723. >PATH_INFO</VAR
  46724. >.
  46725.      </P
  46726. ></BLOCKQUOTE
  46727. ></DIV
  46728. ></DIV
  46729. ><DIV
  46730. CLASS="section"
  46731. ><HR><H2
  46732. CLASS="section"
  46733. ><A
  46734. NAME="apache.installation"
  46735. >Installation</A
  46736. ></H2
  46737. ><P
  46738. >      For PHP installation on Apache see the <A
  46739. HREF="#install"
  46740. >installation chapter</A
  46741. >.
  46742.     </P
  46743. ></DIV
  46744. ><DIV
  46745. CLASS="section"
  46746. ><HR><H2
  46747. CLASS="section"
  46748. ><A
  46749. NAME="apache.configuration"
  46750. >Runtime Configuration</A
  46751. ></H2
  46752. ><P
  46753. >   The behaviour of the Apache PHP module is affected by settings in <TT
  46754. CLASS="filename"
  46755. >php.ini</TT
  46756. >.
  46757.   Configuration settings from <TT
  46758. CLASS="filename"
  46759. >php.ini</TT
  46760. > may be overridden by
  46761.   <A
  46762. HREF="#configuration.changes.apache"
  46763. >php_flag</A
  46764. > settings
  46765.   in the server configuration file or local <TT
  46766. CLASS="filename"
  46767. >.htaccess</TT
  46768. > files.
  46769.  </P
  46770. ><TABLE
  46771. WIDTH="100%"
  46772. BORDER="0"
  46773. CELLPADDING="0"
  46774. CELLSPACING="0"
  46775. CLASS="EXAMPLE"
  46776. ><TR
  46777. ><TD
  46778. ><DIV
  46779. CLASS="example"
  46780. ><A
  46781. NAME="AEN7769"
  46782. ></A
  46783. ><P
  46784. ><B
  46785. >Example 1. Turning off PHP parsing for a directory using <TT
  46786. CLASS="filename"
  46787. >.htaccess</TT
  46788. ></B
  46789. ></P
  46790. ><TABLE
  46791. BORDER="0"
  46792. BGCOLOR="#E0E0E0"
  46793. CELLPADDING="5"
  46794. ><TR
  46795. ><TD
  46796. ><PRE
  46797. CLASS="programlisting"
  46798. >php_flag engine off</PRE
  46799. ></TD
  46800. ></TR
  46801. ></TABLE
  46802. ></DIV
  46803. ></TD
  46804. ></TR
  46805. ></TABLE
  46806. ><P
  46807. >   <DIV
  46808. CLASS="table"
  46809. ><A
  46810. NAME="AEN7774"
  46811. ></A
  46812. ><P
  46813. ><B
  46814. >Table 1. Apache configuration options</B
  46815. ></P
  46816. ><TABLE
  46817. BORDER="1"
  46818. CLASS="CALSTABLE"
  46819. ><COL><COL><COL><THEAD
  46820. ><TR
  46821. ><TH
  46822. >Name</TH
  46823. ><TH
  46824. >Default</TH
  46825. ><TH
  46826. >Changeable</TH
  46827. ></TR
  46828. ></THEAD
  46829. ><TBODY
  46830. ><TR
  46831. ><TD
  46832. >engine</TD
  46833. ><TD
  46834. >On</TD
  46835. ><TD
  46836. >PHP_INI_ALL</TD
  46837. ></TR
  46838. ><TR
  46839. ><TD
  46840. >child_terminate</TD
  46841. ><TD
  46842. >Off</TD
  46843. ><TD
  46844. >PHP_INI_ALL</TD
  46845. ></TR
  46846. ><TR
  46847. ><TD
  46848. >last_modified</TD
  46849. ><TD
  46850. >Off</TD
  46851. ><TD
  46852. >PHP_INI_ALL</TD
  46853. ></TR
  46854. ><TR
  46855. ><TD
  46856. >xbithack</TD
  46857. ><TD
  46858. >Off</TD
  46859. ><TD
  46860. >PHP_INI_ALL</TD
  46861. ></TR
  46862. ></TBODY
  46863. ></TABLE
  46864. ></DIV
  46865. >
  46866.   For further details and definition of the PHP_INI_* constants see
  46867.   <A
  46868. HREF="#function.ini-set"
  46869. ><B
  46870. CLASS="function"
  46871. >ini_set()</B
  46872. ></A
  46873. >.
  46874.  </P
  46875. ><P
  46876. >Here's a short explanation of
  46877. the configuration directives.</P
  46878. ><P
  46879. >   <P
  46880. ></P
  46881. ><DIV
  46882. CLASS="variablelist"
  46883. ><DL
  46884. ><DT
  46885. ><A
  46886. NAME="ini.engine"
  46887. ></A
  46888. ><VAR
  46889. CLASS="parameter"
  46890. >engine</VAR
  46891. >
  46892.      <A
  46893. HREF="#language.types.boolean"
  46894. ><B
  46895. CLASS="type"
  46896. >boolean</B
  46897. ></A
  46898. ></DT
  46899. ><DD
  46900. ><P
  46901. >       Turns PHP parsing on or off.
  46902.       This directive is really only useful in the Apache module
  46903.       version of PHP. It is used by sites that would like to turn
  46904.       PHP parsing on and off on a per-directory or per-virtual
  46905.       server basis. By putting <KBD
  46906. CLASS="userinput"
  46907. >engine off</KBD
  46908. >
  46909.       in the appropriate places in the <TT
  46910. CLASS="filename"
  46911. >httpd.conf</TT
  46912. > file, PHP can
  46913.       be enabled or disabled.
  46914.      </P
  46915. ></DD
  46916. ><DT
  46917. ><A
  46918. NAME="ini.child-terminate"
  46919. ></A
  46920. ><VAR
  46921. CLASS="parameter"
  46922. >child_terminate</VAR
  46923. >
  46924.      <A
  46925. HREF="#language.types.boolean"
  46926. ><B
  46927. CLASS="type"
  46928. >boolean</B
  46929. ></A
  46930. ></DT
  46931. ><DD
  46932. ><P
  46933. >       Specify whether PHP scripts may request child process termination on end of request, 
  46934.       see also <A
  46935. HREF="#function.apache-child-terminate"
  46936. ><B
  46937. CLASS="function"
  46938. >apache_child_terminate()</B
  46939. ></A
  46940. >.
  46941.      </P
  46942. ></DD
  46943. ><DT
  46944. ><A
  46945. NAME="ini.last-modified"
  46946. ></A
  46947. ><VAR
  46948. CLASS="parameter"
  46949. >last_modified</VAR
  46950. >
  46951.      <A
  46952. HREF="#language.types.boolean"
  46953. ><B
  46954. CLASS="type"
  46955. >boolean</B
  46956. ></A
  46957. ></DT
  46958. ><DD
  46959. ><P
  46960. >       Send PHP scripts modification date as Last-Modified: header for this request.
  46961.      </P
  46962. ></DD
  46963. ><DT
  46964. ><A
  46965. NAME="ini.xbithack"
  46966. ></A
  46967. ><VAR
  46968. CLASS="parameter"
  46969. >xbithack</VAR
  46970. >
  46971.      <A
  46972. HREF="#language.types.boolean"
  46973. ><B
  46974. CLASS="type"
  46975. >boolean</B
  46976. ></A
  46977. ></DT
  46978. ><DD
  46979. ><P
  46980. >       Parse files with executable bit set as PHP regardless of their file ending.
  46981.      </P
  46982. ></DD
  46983. ></DL
  46984. ></DIV
  46985. >
  46986.  </P
  46987. ></DIV
  46988. ><DIV
  46989. CLASS="section"
  46990. ><HR><H2
  46991. CLASS="section"
  46992. ><A
  46993. NAME="apache.resources"
  46994. >Resource Types</A
  46995. ></H2
  46996. ><P
  46997. >This extension has no resource types defined.</P
  46998. ></DIV
  46999. ><DIV
  47000. CLASS="section"
  47001. ><HR><H2
  47002. CLASS="section"
  47003. ><A
  47004. NAME="apache.constants"
  47005. >Predefined Constants</A
  47006. ></H2
  47007. ><P
  47008. >This extension has no constants defined.</P
  47009. ></DIV
  47010. ></DIV
  47011. ><DIV
  47012. CLASS="TOC"
  47013. ><DL
  47014. ><DT
  47015. ><B
  47016. >Table of Contents</B
  47017. ></DT
  47018. ><DT
  47019. ><A
  47020. HREF="#function.apache-child-terminate"
  47021. >apache_child_terminate</A
  47022. > -- Terminate apache process after this request</DT
  47023. ><DT
  47024. ><A
  47025. HREF="#function.apache-get-modules"
  47026. >apache_get_modules</A
  47027. > -- 
  47028.      Get a list of loaded Apache modules
  47029.     </DT
  47030. ><DT
  47031. ><A
  47032. HREF="#function.apache-get-version"
  47033. >apache_get_version</A
  47034. > -- 
  47035.      Fetch Apache version
  47036.     </DT
  47037. ><DT
  47038. ><A
  47039. HREF="#function.apache-getenv"
  47040. >apache_getenv</A
  47041. > -- Get an Apache subprocess_env variable</DT
  47042. ><DT
  47043. ><A
  47044. HREF="#function.apache-lookup-uri"
  47045. >apache_lookup_uri</A
  47046. > -- 
  47047.      Perform a partial request for the specified URI and return all
  47048.      info about it
  47049.     </DT
  47050. ><DT
  47051. ><A
  47052. HREF="#function.apache-note"
  47053. >apache_note</A
  47054. > -- Get and set apache request notes</DT
  47055. ><DT
  47056. ><A
  47057. HREF="#function.apache-request-headers"
  47058. >apache_request_headers</A
  47059. > -- Fetch all HTTP request headers</DT
  47060. ><DT
  47061. ><A
  47062. HREF="#function.apache-reset-timeout"
  47063. >apache_reset_timeout</A
  47064. > -- 
  47065.      Reset the Apache write timer
  47066.     </DT
  47067. ><DT
  47068. ><A
  47069. HREF="#function.apache-response-headers"
  47070. >apache_response_headers</A
  47071. > -- 
  47072.      Fetch all HTTP response headers
  47073.     </DT
  47074. ><DT
  47075. ><A
  47076. HREF="#function.apache-setenv"
  47077. >apache_setenv</A
  47078. > -- Set an Apache subprocess_env variable</DT
  47079. ><DT
  47080. ><A
  47081. HREF="#function.ascii2ebcdic"
  47082. >ascii2ebcdic</A
  47083. > -- Translate string from ASCII to EBCDIC</DT
  47084. ><DT
  47085. ><A
  47086. HREF="#function.ebcdic2ascii"
  47087. >ebcdic2ascii</A
  47088. > -- Translate string from EBCDIC to ASCII</DT
  47089. ><DT
  47090. ><A
  47091. HREF="#function.getallheaders"
  47092. >getallheaders</A
  47093. > -- Fetch all HTTP request headers</DT
  47094. ><DT
  47095. ><A
  47096. HREF="#function.virtual"
  47097. >virtual</A
  47098. > -- Perform an Apache sub-request</DT
  47099. ></DL
  47100. ></DIV
  47101. ></DIV
  47102. ><H1
  47103. ><A
  47104. NAME="function.apache-child-terminate"
  47105. ></A
  47106. >apache_child_terminate</H1
  47107. ><DIV
  47108. CLASS="refnamediv"
  47109. ><A
  47110. NAME="AEN7837"
  47111. ></A
  47112. ><P
  47113. >    (PHP 4 >= 4.0.5, PHP 5)</P
  47114. >apache_child_terminate -- Terminate apache process after this request</DIV
  47115. ><DIV
  47116. CLASS="refsect1"
  47117. ><A
  47118. NAME="AEN7840"
  47119. ></A
  47120. ><H2
  47121. >Description</H2
  47122. >bool <B
  47123. CLASS="methodname"
  47124. >apache_child_terminate</B
  47125. > ( void )<BR
  47126. ></BR
  47127. ><P
  47128. >      <B
  47129. CLASS="function"
  47130. >apache_child_terminate()</B
  47131. > will register the
  47132.      Apache process executing the current PHP request for termination
  47133.      once execution of PHP code it is completed. It may be used to
  47134.      terminate a process after a script with high memory consumption has
  47135.      been run as memory will usually only be freed internally but not
  47136.      given back to the operating system. 
  47137.     </P
  47138. ><DIV
  47139. CLASS="note"
  47140. ><BLOCKQUOTE
  47141. CLASS="note"
  47142. ><P
  47143. ><B
  47144. >Note: </B
  47145. >
  47146.       The availability of this feature is controlled by the <TT
  47147. CLASS="filename"
  47148. >php.ini</TT
  47149. > directive
  47150.       <VAR
  47151. CLASS="option"
  47152. >child_terminate</VAR
  47153. >, which is set to <VAR
  47154. CLASS="literal"
  47155. >off</VAR
  47156. >
  47157.       by default.
  47158.      </P
  47159. ><P
  47160. >       This feature is also not available on multithreaded versions of apache 
  47161.       like the win32 version.
  47162.      </P
  47163. ></BLOCKQUOTE
  47164. ></DIV
  47165. ><P
  47166. >      See also <A
  47167. HREF="#function.exit"
  47168. ><B
  47169. CLASS="function"
  47170. >exit()</B
  47171. ></A
  47172. >.
  47173.     </P
  47174. ></DIV
  47175. ><H1
  47176. ><A
  47177. NAME="function.apache-get-modules"
  47178. ></A
  47179. >apache_get_modules</H1
  47180. ><DIV
  47181. CLASS="refnamediv"
  47182. ><A
  47183. NAME="AEN7857"
  47184. ></A
  47185. ><P
  47186. >    (PHP 4 >= 4.3.2, PHP 5)</P
  47187. >apache_get_modules -- 
  47188.      Get a list of loaded Apache modules
  47189.     </DIV
  47190. ><DIV
  47191. CLASS="refsect1"
  47192. ><A
  47193. NAME="AEN7860"
  47194. ></A
  47195. ><H2
  47196. >Description</H2
  47197. >array <B
  47198. CLASS="methodname"
  47199. >apache_get_modules</B
  47200. > ( void )<BR
  47201. ></BR
  47202. ><P
  47203. >      This function returns an array with the loaded Apache modules.
  47204.     </P
  47205. ><P
  47206. >      <TABLE
  47207. WIDTH="100%"
  47208. BORDER="0"
  47209. CELLPADDING="0"
  47210. CELLSPACING="0"
  47211. CLASS="EXAMPLE"
  47212. ><TR
  47213. ><TD
  47214. ><DIV
  47215. CLASS="example"
  47216. ><A
  47217. NAME="AEN7868"
  47218. ></A
  47219. ><P
  47220. ><B
  47221. >Example 1. <B
  47222. CLASS="function"
  47223. >apache_get_modules()</B
  47224. > example</B
  47225. ></P
  47226. ><TABLE
  47227. BORDER="0"
  47228. BGCOLOR="#E0E0E0"
  47229. CELLPADDING="5"
  47230. ><TR
  47231. ><TD
  47232. ><PRE
  47233. CLASS="php"
  47234. ><?php
  47235. print_r(apache_get_modules());
  47236. ?></PRE
  47237. ></TD
  47238. ></TR
  47239. ></TABLE
  47240. ><P
  47241. >The above example will output 
  47242. something similar to:</P
  47243. ><TABLE
  47244. BORDER="0"
  47245. BGCOLOR="#E0E0E0"
  47246. CELLPADDING="5"
  47247. ><TR
  47248. ><TD
  47249. ><PRE
  47250. CLASS="screen"
  47251. >Array
  47252. (
  47253.     [0] => core
  47254.     [1] => http_core
  47255.     [2] => mod_so
  47256.     [3] => sapi_apache2
  47257.     [4] => mod_mime
  47258.     [5] => mod_rewrite
  47259. )</PRE
  47260. ></TD
  47261. ></TR
  47262. ></TABLE
  47263. ></DIV
  47264. ></TD
  47265. ></TR
  47266. ></TABLE
  47267. >
  47268.     </P
  47269. ><DIV
  47270. CLASS="note"
  47271. ><BLOCKQUOTE
  47272. CLASS="note"
  47273. ><P
  47274. ><B
  47275. >Note: </B
  47276. >
  47277.       This function is available only in Apache 2 <SPAN
  47278. CLASS="emphasis"
  47279. ><I
  47280. CLASS="emphasis"
  47281. >handler</I
  47282. ></SPAN
  47283. >.
  47284.       As of PHP 5, it is available also in Apache 2 <SPAN
  47285. CLASS="emphasis"
  47286. ><I
  47287. CLASS="emphasis"
  47288. >filter</I
  47289. ></SPAN
  47290. >
  47291.       and Apache 1.
  47292.      </P
  47293. ></BLOCKQUOTE
  47294. ></DIV
  47295. ></DIV
  47296. ><H1
  47297. ><A
  47298. NAME="function.apache-get-version"
  47299. ></A
  47300. >apache_get_version</H1
  47301. ><DIV
  47302. CLASS="refnamediv"
  47303. ><A
  47304. NAME="AEN7879"
  47305. ></A
  47306. ><P
  47307. >    (PHP 4 >= 4.3.2, PHP 5)</P
  47308. >apache_get_version -- 
  47309.      Fetch Apache version
  47310.     </DIV
  47311. ><DIV
  47312. CLASS="refsect1"
  47313. ><A
  47314. NAME="AEN7882"
  47315. ></A
  47316. ><H2
  47317. >Description</H2
  47318. >string <B
  47319. CLASS="methodname"
  47320. >apache_get_version</B
  47321. > ( void )<BR
  47322. ></BR
  47323. ><P
  47324. >      <B
  47325. CLASS="function"
  47326. >apache_get_version()</B
  47327. > returns the version of Apache as
  47328.      string, or <TT
  47329. CLASS="constant"
  47330. ><B
  47331. >FALSE</B
  47332. ></TT
  47333. > on failure.
  47334.     </P
  47335. ><TABLE
  47336. WIDTH="100%"
  47337. BORDER="0"
  47338. CELLPADDING="0"
  47339. CELLSPACING="0"
  47340. CLASS="EXAMPLE"
  47341. ><TR
  47342. ><TD
  47343. ><DIV
  47344. CLASS="example"
  47345. ><A
  47346. NAME="AEN7891"
  47347. ></A
  47348. ><P
  47349. ><B
  47350. >Example 1. <B
  47351. CLASS="function"
  47352. >apache_get_version()</B
  47353. > example</B
  47354. ></P
  47355. ><TABLE
  47356. BORDER="0"
  47357. BGCOLOR="#E0E0E0"
  47358. CELLPADDING="5"
  47359. ><TR
  47360. ><TD
  47361. ><PRE
  47362. CLASS="php"
  47363. ><?php
  47364. $version = apache_get_version();
  47365. echo "$version\n";
  47366. ?></PRE
  47367. ></TD
  47368. ></TR
  47369. ></TABLE
  47370. ><P
  47371. >The above example will output 
  47372. something similar to:</P
  47373. ><TABLE
  47374. BORDER="0"
  47375. BGCOLOR="#E0E0E0"
  47376. CELLPADDING="5"
  47377. ><TR
  47378. ><TD
  47379. ><PRE
  47380. CLASS="screen"
  47381. >Apache/1.3.29 (Unix) PHP/4.3.4</PRE
  47382. ></TD
  47383. ></TR
  47384. ></TABLE
  47385. ></DIV
  47386. ></TD
  47387. ></TR
  47388. ></TABLE
  47389. ><DIV
  47390. CLASS="note"
  47391. ><BLOCKQUOTE
  47392. CLASS="note"
  47393. ><P
  47394. ><B
  47395. >Note: </B
  47396. >
  47397.       This function is available only in Apache 2 <SPAN
  47398. CLASS="emphasis"
  47399. ><I
  47400. CLASS="emphasis"
  47401. >handler</I
  47402. ></SPAN
  47403. >.
  47404.       As of PHP 4.3.4, it is available also in Apache 1 and as of PHP 5, it is
  47405.       available also in Apache 2 <SPAN
  47406. CLASS="emphasis"
  47407. ><I
  47408. CLASS="emphasis"
  47409. >filter</I
  47410. ></SPAN
  47411. >.
  47412.      </P
  47413. ></BLOCKQUOTE
  47414. ></DIV
  47415. ><P
  47416. >      See also <A
  47417. HREF="#function.phpinfo"
  47418. ><B
  47419. CLASS="function"
  47420. >phpinfo()</B
  47421. ></A
  47422. >.
  47423.     </P
  47424. ></DIV
  47425. ><H1
  47426. ><A
  47427. NAME="function.apache-getenv"
  47428. ></A
  47429. >apache_getenv</H1
  47430. ><DIV
  47431. CLASS="refnamediv"
  47432. ><A
  47433. NAME="AEN7904"
  47434. ></A
  47435. ><P
  47436. >    (PHP 4 >= 4.3.0, PHP 5)</P
  47437. >apache_getenv -- Get an Apache subprocess_env variable</DIV
  47438. ><DIV
  47439. CLASS="refsect1"
  47440. ><A
  47441. NAME="AEN7907"
  47442. ></A
  47443. ><H2
  47444. >Description</H2
  47445. >string <B
  47446. CLASS="methodname"
  47447. >apache_getenv</B
  47448. > ( string variable [, bool walk_to_top])<BR
  47449. ></BR
  47450. ><P
  47451. >      <B
  47452. CLASS="function"
  47453. >apache_getenv()</B
  47454. > returns the value of the Apache
  47455.      environment variable specified by <VAR
  47456. CLASS="parameter"
  47457. >variable</VAR
  47458. >, or
  47459.      <TT
  47460. CLASS="constant"
  47461. ><B
  47462. >FALSE</B
  47463. ></TT
  47464. > on failure.
  47465.     </P
  47466. ><TABLE
  47467. WIDTH="100%"
  47468. BORDER="0"
  47469. CELLPADDING="0"
  47470. CELLSPACING="0"
  47471. CLASS="EXAMPLE"
  47472. ><TR
  47473. ><TD
  47474. ><DIV
  47475. CLASS="example"
  47476. ><A
  47477. NAME="AEN7922"
  47478. ></A
  47479. ><P
  47480. ><B
  47481. >Example 1. <B
  47482. CLASS="function"
  47483. >apache_getenv()</B
  47484. > example</B
  47485. ></P
  47486. ><TABLE
  47487. BORDER="0"
  47488. BGCOLOR="#E0E0E0"
  47489. CELLPADDING="5"
  47490. ><TR
  47491. ><TD
  47492. ><PRE
  47493. CLASS="php"
  47494. ><?php
  47495. $ret = apache_getenv("SERVER_ADDR");
  47496. echo $ret;
  47497. ?></PRE
  47498. ></TD
  47499. ></TR
  47500. ></TABLE
  47501. ><P
  47502. >       The example above shows how to retrieve the value of the Apache
  47503.       environment variable "SERVER_ADDR".
  47504.      </P
  47505. ></DIV
  47506. ></TD
  47507. ></TR
  47508. ></TABLE
  47509. ><P
  47510. >      See also <A
  47511. HREF="#function.apache-setenv"
  47512. ><B
  47513. CLASS="function"
  47514. >apache_setenv()</B
  47515. ></A
  47516. >.
  47517.     </P
  47518. ></DIV
  47519. ><H1
  47520. ><A
  47521. NAME="function.apache-lookup-uri"
  47522. ></A
  47523. >apache_lookup_uri</H1
  47524. ><DIV
  47525. CLASS="refnamediv"
  47526. ><A
  47527. NAME="AEN7930"
  47528. ></A
  47529. ><P
  47530. >    (PHP 3>= 3.0.4, PHP 4 , PHP 5)</P
  47531. >apache_lookup_uri -- 
  47532.      Perform a partial request for the specified URI and return all
  47533.      info about it
  47534.     </DIV
  47535. ><DIV
  47536. CLASS="refsect1"
  47537. ><A
  47538. NAME="AEN7933"
  47539. ></A
  47540. ><H2
  47541. >Description</H2
  47542. >object <B
  47543. CLASS="methodname"
  47544. >apache_lookup_uri</B
  47545. > ( string filename)<BR
  47546. ></BR
  47547. ><P
  47548. >      This performs a partial request for a URI.  It goes just far
  47549.      enough to obtain all the important information about the given
  47550.      resource and returns this information in a class.  The properties
  47551.      of the returned class are:
  47552.      <P
  47553. ></P
  47554. ><TABLE
  47555. BORDER="0"
  47556. ><TBODY
  47557. ><TR
  47558. ><TD
  47559. >status</TD
  47560. ></TR
  47561. ><TR
  47562. ><TD
  47563. >the_request</TD
  47564. ></TR
  47565. ><TR
  47566. ><TD
  47567. >status_line</TD
  47568. ></TR
  47569. ><TR
  47570. ><TD
  47571. >method</TD
  47572. ></TR
  47573. ><TR
  47574. ><TD
  47575. >content_type</TD
  47576. ></TR
  47577. ><TR
  47578. ><TD
  47579. >handler</TD
  47580. ></TR
  47581. ><TR
  47582. ><TD
  47583. >uri</TD
  47584. ></TR
  47585. ><TR
  47586. ><TD
  47587. >filename</TD
  47588. ></TR
  47589. ><TR
  47590. ><TD
  47591. >path_info</TD
  47592. ></TR
  47593. ><TR
  47594. ><TD
  47595. >args</TD
  47596. ></TR
  47597. ><TR
  47598. ><TD
  47599. >boundary</TD
  47600. ></TR
  47601. ><TR
  47602. ><TD
  47603. >no_cache</TD
  47604. ></TR
  47605. ><TR
  47606. ><TD
  47607. >no_local_copy</TD
  47608. ></TR
  47609. ><TR
  47610. ><TD
  47611. >allowed</TD
  47612. ></TR
  47613. ><TR
  47614. ><TD
  47615. >send_bodyct</TD
  47616. ></TR
  47617. ><TR
  47618. ><TD
  47619. >bytes_sent</TD
  47620. ></TR
  47621. ><TR
  47622. ><TD
  47623. >byterange</TD
  47624. ></TR
  47625. ><TR
  47626. ><TD
  47627. >clength</TD
  47628. ></TR
  47629. ><TR
  47630. ><TD
  47631. >unparsed_uri</TD
  47632. ></TR
  47633. ><TR
  47634. ><TD
  47635. >mtime</TD
  47636. ></TR
  47637. ><TR
  47638. ><TD
  47639. >request_time</TD
  47640. ></TR
  47641. ></TBODY
  47642. ></TABLE
  47643. ><P
  47644. ></P
  47645. >
  47646.     </P
  47647. ><P
  47648. >      <TABLE
  47649. WIDTH="100%"
  47650. BORDER="0"
  47651. CELLPADDING="0"
  47652. CELLSPACING="0"
  47653. CLASS="EXAMPLE"
  47654. ><TR
  47655. ><TD
  47656. ><DIV
  47657. CLASS="example"
  47658. ><A
  47659. NAME="AEN7965"
  47660. ></A
  47661. ><P
  47662. ><B
  47663. >Example 1. <B
  47664. CLASS="function"
  47665. >apache_lookup_uri()</B
  47666. > example</B
  47667. ></P
  47668. ><TABLE
  47669. BORDER="0"
  47670. BGCOLOR="#E0E0E0"
  47671. CELLPADDING="5"
  47672. ><TR
  47673. ><TD
  47674. ><PRE
  47675. CLASS="php"
  47676. ><?php
  47677. $info = apache_lookup_uri('index.php?var=value');
  47678. print_r($info);
  47679.  
  47680. if (file_exists($info->filename)) {
  47681.     echo 'file exists!';
  47682. }
  47683. ?></PRE
  47684. ></TD
  47685. ></TR
  47686. ></TABLE
  47687. ><P
  47688. >The above example will output 
  47689. something similar to:</P
  47690. ><TABLE
  47691. BORDER="0"
  47692. BGCOLOR="#E0E0E0"
  47693. CELLPADDING="5"
  47694. ><TR
  47695. ><TD
  47696. ><PRE
  47697. CLASS="screen"
  47698. >stdClass Object
  47699. (
  47700.     [status] => 200
  47701.     [the_request] => GET /dir/file.php HTTP/1.1
  47702.     [method] => GET
  47703.     [mtime] => 0
  47704.     [clength] => 0
  47705.     [chunked] => 0
  47706.     [content_type] => application/x-httpd-php
  47707.     [no_cache] => 0
  47708.     [no_local_copy] => 1
  47709.     [unparsed_uri] => /dir/index.php?var=value
  47710.     [uri] => /dir/index.php
  47711.     [filename] => /home/htdocs/dir/index.php
  47712.     [args] => var=value
  47713.     [allowed] => 0
  47714.     [sent_bodyct] => 0
  47715.     [bytes_sent] => 0
  47716.     [request_time] => 1074282764
  47717. )
  47718. file exists!</PRE
  47719. ></TD
  47720. ></TR
  47721. ></TABLE
  47722. ></DIV
  47723. ></TD
  47724. ></TR
  47725. ></TABLE
  47726. >
  47727.     </P
  47728. ><DIV
  47729. CLASS="note"
  47730. ><BLOCKQUOTE
  47731. CLASS="note"
  47732. ><P
  47733. ><B
  47734. >Note: </B
  47735. >
  47736.       <B
  47737. CLASS="function"
  47738. >apache_lookup_uri()</B
  47739. > only works when PHP
  47740.       is installed as an Apache module.
  47741.      </P
  47742. ></BLOCKQUOTE
  47743. ></DIV
  47744. ></DIV
  47745. ><H1
  47746. ><A
  47747. NAME="function.apache-note"
  47748. ></A
  47749. >apache_note</H1
  47750. ><DIV
  47751. CLASS="refnamediv"
  47752. ><A
  47753. NAME="AEN7975"
  47754. ></A
  47755. ><P
  47756. >    (PHP 3>= 3.0.2, PHP 4 , PHP 5)</P
  47757. >apache_note -- Get and set apache request notes</DIV
  47758. ><DIV
  47759. CLASS="refsect1"
  47760. ><A
  47761. NAME="AEN7978"
  47762. ></A
  47763. ><H2
  47764. >Description</H2
  47765. >string <B
  47766. CLASS="methodname"
  47767. >apache_note</B
  47768. > ( string note_name [, string note_value])<BR
  47769. ></BR
  47770. ><P
  47771. >      <B
  47772. CLASS="function"
  47773. >apache_note()</B
  47774. > is an Apache-specific function
  47775.      which gets and sets values in a request's
  47776.      <VAR
  47777. CLASS="literal"
  47778. >notes</VAR
  47779. > table. If called with one argument, it
  47780.      returns the current value of note
  47781.      <VAR
  47782. CLASS="literal"
  47783. >note_name</VAR
  47784. >. If called with two arguments, it
  47785.      sets the value of note <VAR
  47786. CLASS="literal"
  47787. >note_name</VAR
  47788. > to
  47789.      <VAR
  47790. CLASS="literal"
  47791. >note_value</VAR
  47792. > and returns the previous value of
  47793.      note <VAR
  47794. CLASS="literal"
  47795. >note_name</VAR
  47796. >.
  47797.     </P
  47798. ></DIV
  47799. ><H1
  47800. ><A
  47801. NAME="function.apache-request-headers"
  47802. ></A
  47803. >apache_request_headers</H1
  47804. ><DIV
  47805. CLASS="refnamediv"
  47806. ><A
  47807. NAME="AEN7997"
  47808. ></A
  47809. ><P
  47810. >    (PHP 4 >= 4.3.0, PHP 5)</P
  47811. >apache_request_headers -- Fetch all HTTP request headers</DIV
  47812. ><DIV
  47813. CLASS="refsect1"
  47814. ><A
  47815. NAME="AEN8000"
  47816. ></A
  47817. ><H2
  47818. >Description</H2
  47819. >array <B
  47820. CLASS="methodname"
  47821. >apache_request_headers</B
  47822. > ( void )<BR
  47823. ></BR
  47824. ><P
  47825. >      <B
  47826. CLASS="function"
  47827. >apache_request_headers()</B
  47828. > returns an associative
  47829.      array of all the HTTP headers in the current request.  This is only
  47830.      supported when PHP runs as an <SPAN
  47831. CLASS="productname"
  47832. >Apache</SPAN
  47833. >
  47834.      module.
  47835.     </P
  47836. ><P
  47837. >      <TABLE
  47838. WIDTH="100%"
  47839. BORDER="0"
  47840. CELLPADDING="0"
  47841. CELLSPACING="0"
  47842. CLASS="EXAMPLE"
  47843. ><TR
  47844. ><TD
  47845. ><DIV
  47846. CLASS="example"
  47847. ><A
  47848. NAME="AEN8010"
  47849. ></A
  47850. ><P
  47851. ><B
  47852. >Example 1. <B
  47853. CLASS="function"
  47854. >apache_request_headers()</B
  47855. > example</B
  47856. ></P
  47857. ><TABLE
  47858. BORDER="0"
  47859. BGCOLOR="#E0E0E0"
  47860. CELLPADDING="5"
  47861. ><TR
  47862. ><TD
  47863. ><PRE
  47864. CLASS="php"
  47865. ><?php
  47866. $headers = apache_request_headers();
  47867.  
  47868. foreach ($headers as $header => $value) {
  47869.     echo "$header: $value <br />\n";
  47870. }
  47871. ?></PRE
  47872. ></TD
  47873. ></TR
  47874. ></TABLE
  47875. ><P
  47876. >The above example will output 
  47877. something similar to:</P
  47878. ><TABLE
  47879. BORDER="0"
  47880. BGCOLOR="#E0E0E0"
  47881. CELLPADDING="5"
  47882. ><TR
  47883. ><TD
  47884. ><PRE
  47885. CLASS="screen"
  47886. >Accept: */*
  47887. Accept-Language: en-us
  47888. Accept-Encoding: gzip, deflate
  47889. User-Agent: Mozilla/4.0
  47890. Host: www.example.com
  47891. Connection: Keep-Alive</PRE
  47892. ></TD
  47893. ></TR
  47894. ></TABLE
  47895. ></DIV
  47896. ></TD
  47897. ></TR
  47898. ></TABLE
  47899. >
  47900.     </P
  47901. ><DIV
  47902. CLASS="note"
  47903. ><BLOCKQUOTE
  47904. CLASS="note"
  47905. ><P
  47906. ><B
  47907. >Note: </B
  47908. >
  47909.       Prior to PHP 4.3.0, <B
  47910. CLASS="function"
  47911. >apache_request_headers()</B
  47912. > was
  47913.       called <A
  47914. HREF="#function.getallheaders"
  47915. ><B
  47916. CLASS="function"
  47917. >getallheaders()</B
  47918. ></A
  47919. >.  After PHP 4.3.0,
  47920.       <A
  47921. HREF="#function.getallheaders"
  47922. ><B
  47923. CLASS="function"
  47924. >getallheaders()</B
  47925. ></A
  47926. > is an alias for
  47927.       <B
  47928. CLASS="function"
  47929. >apache_request_headers()</B
  47930. >.
  47931.      </P
  47932. ></BLOCKQUOTE
  47933. ></DIV
  47934. ><DIV
  47935. CLASS="note"
  47936. ><BLOCKQUOTE
  47937. CLASS="note"
  47938. ><P
  47939. ><B
  47940. >Note: </B
  47941. >
  47942.       You can also get at the value of the common CGI variables by
  47943.       reading them from the environment, which works whether or not
  47944.       you are using PHP as an <SPAN
  47945. CLASS="productname"
  47946. >Apache</SPAN
  47947. > module. Use
  47948.       <A
  47949. HREF="#function.phpinfo"
  47950. ><B
  47951. CLASS="function"
  47952. >phpinfo()</B
  47953. ></A
  47954. > to see a list of all of the available
  47955.       <A
  47956. HREF="#language.variables.predefined"
  47957. >environment variables</A
  47958. >.
  47959.      </P
  47960. ></BLOCKQUOTE
  47961. ></DIV
  47962. ><DIV
  47963. CLASS="note"
  47964. ><BLOCKQUOTE
  47965. CLASS="note"
  47966. ><P
  47967. ><B
  47968. >Note: </B
  47969. >As of PHP 4.3.3 you can use this function with the
  47970. <A
  47971. HREF="#ref.nsapi"
  47972. >NSAPI server module</A
  47973. > in Netscape/iPlanet/SunONE
  47974. webservers, too.</P
  47975. ></BLOCKQUOTE
  47976. ></DIV
  47977. ><P
  47978. >      See also <A
  47979. HREF="#function.apache-response-headers"
  47980. ><B
  47981. CLASS="function"
  47982. >apache_response_headers()</B
  47983. ></A
  47984. >.
  47985.     </P
  47986. ></DIV
  47987. ><H1
  47988. ><A
  47989. NAME="function.apache-reset-timeout"
  47990. ></A
  47991. >apache_reset_timeout</H1
  47992. ><DIV
  47993. CLASS="refnamediv"
  47994. ><A
  47995. NAME="AEN8033"
  47996. ></A
  47997. ><P
  47998. >    (no version information, might be only in CVS)</P
  47999. >apache_reset_timeout -- 
  48000.      Reset the Apache write timer
  48001.     </DIV
  48002. ><DIV
  48003. CLASS="refsect1"
  48004. ><A
  48005. NAME="AEN8036"
  48006. ></A
  48007. ><H2
  48008. >Description</H2
  48009. >bool <B
  48010. CLASS="methodname"
  48011. >apache_reset_timeout</B
  48012. > ( void )<BR
  48013. ></BR
  48014. ><P
  48015. >      <B
  48016. CLASS="function"
  48017. >apache_reset_timeout()</B
  48018. > resets the Apache write timer,
  48019.      which defaults to 300 seconds. With <VAR
  48020. CLASS="literal"
  48021. >set_time_limit(0);
  48022.       ignore_user_abort(true)</VAR
  48023. > and periodic
  48024.      <B
  48025. CLASS="function"
  48026. >apache_reset_timeout()</B
  48027. > calls, Apache can theoretically
  48028.      run forever.
  48029.     </P
  48030. ><P
  48031. >      Returns <TT
  48032. CLASS="constant"
  48033. ><B
  48034. >TRUE</B
  48035. ></TT
  48036. > on success or <TT
  48037. CLASS="constant"
  48038. ><B
  48039. >FALSE</B
  48040. ></TT
  48041. > on failure.
  48042.     </P
  48043. ><DIV
  48044. CLASS="note"
  48045. ><BLOCKQUOTE
  48046. CLASS="note"
  48047. ><P
  48048. ><B
  48049. >Note: </B
  48050. >
  48051.       This functions is just available for Apache 1.
  48052.      </P
  48053. ></BLOCKQUOTE
  48054. ></DIV
  48055. ><DIV
  48056. CLASS="note"
  48057. ><BLOCKQUOTE
  48058. CLASS="note"
  48059. ><P
  48060. ><B
  48061. >Note: </B
  48062. >This function is disabled in <A
  48063. HREF="#features.safe-mode"
  48064. >safe mode</A
  48065. >.</P
  48066. ></BLOCKQUOTE
  48067. ></DIV
  48068. ></DIV
  48069. ><H1
  48070. ><A
  48071. NAME="function.apache-response-headers"
  48072. ></A
  48073. >apache_response_headers</H1
  48074. ><DIV
  48075. CLASS="refnamediv"
  48076. ><A
  48077. NAME="AEN8055"
  48078. ></A
  48079. ><P
  48080. >    (PHP 4 >= 4.3.0, PHP 5)</P
  48081. >apache_response_headers -- 
  48082.      Fetch all HTTP response headers
  48083.     </DIV
  48084. ><DIV
  48085. CLASS="refsect1"
  48086. ><A
  48087. NAME="AEN8058"
  48088. ></A
  48089. ><H2
  48090. >Description</H2
  48091. >array <B
  48092. CLASS="methodname"
  48093. >apache_response_headers</B
  48094. > ( void )<BR
  48095. ></BR
  48096. ><P
  48097. >      Returns an array of all Apache response headers.
  48098.     </P
  48099. ><P
  48100. >      <TABLE
  48101. WIDTH="100%"
  48102. BORDER="0"
  48103. CELLPADDING="0"
  48104. CELLSPACING="0"
  48105. CLASS="EXAMPLE"
  48106. ><TR
  48107. ><TD
  48108. ><DIV
  48109. CLASS="example"
  48110. ><A
  48111. NAME="AEN8066"
  48112. ></A
  48113. ><P
  48114. ><B
  48115. >Example 1. <B
  48116. CLASS="function"
  48117. >apache_response_headers()</B
  48118. > example</B
  48119. ></P
  48120. ><TABLE
  48121. BORDER="0"
  48122. BGCOLOR="#E0E0E0"
  48123. CELLPADDING="5"
  48124. ><TR
  48125. ><TD
  48126. ><PRE
  48127. CLASS="php"
  48128. ><?php
  48129. print_r(apache_response_headers());
  48130. ?></PRE
  48131. ></TD
  48132. ></TR
  48133. ></TABLE
  48134. ><P
  48135. >The above example will output 
  48136. something similar to:</P
  48137. ><TABLE
  48138. BORDER="0"
  48139. BGCOLOR="#E0E0E0"
  48140. CELLPADDING="5"
  48141. ><TR
  48142. ><TD
  48143. ><PRE
  48144. CLASS="screen"
  48145. >Array
  48146. (
  48147.     [Accept-Ranges] => bytes
  48148.     [X-Powered-By] => PHP/4.3.8
  48149. )</PRE
  48150. ></TD
  48151. ></TR
  48152. ></TABLE
  48153. ></DIV
  48154. ></TD
  48155. ></TR
  48156. ></TABLE
  48157. >
  48158.     </P
  48159. ><DIV
  48160. CLASS="note"
  48161. ><BLOCKQUOTE
  48162. CLASS="note"
  48163. ><P
  48164. ><B
  48165. >Note: </B
  48166. >As of PHP 4.3.3 you can use this function with the
  48167. <A
  48168. HREF="#ref.nsapi"
  48169. >NSAPI server module</A
  48170. > in Netscape/iPlanet/SunONE
  48171. webservers, too.</P
  48172. ></BLOCKQUOTE
  48173. ></DIV
  48174. ><P
  48175. >      See also <A
  48176. HREF="#function.apache-request-headers"
  48177. ><B
  48178. CLASS="function"
  48179. >apache_request_headers()</B
  48180. ></A
  48181. >, and
  48182.      <A
  48183. HREF="#function.headers-sent"
  48184. ><B
  48185. CLASS="function"
  48186. >headers_sent()</B
  48187. ></A
  48188. >.
  48189.     </P
  48190. ></DIV
  48191. ><H1
  48192. ><A
  48193. NAME="function.apache-setenv"
  48194. ></A
  48195. >apache_setenv</H1
  48196. ><DIV
  48197. CLASS="refnamediv"
  48198. ><A
  48199. NAME="AEN8079"
  48200. ></A
  48201. ><P
  48202. >    (PHP 4 >= 4.2.0, PHP 5)</P
  48203. >apache_setenv -- Set an Apache subprocess_env variable</DIV
  48204. ><DIV
  48205. CLASS="refsect1"
  48206. ><A
  48207. NAME="AEN8082"
  48208. ></A
  48209. ><H2
  48210. >Description</H2
  48211. >int <B
  48212. CLASS="methodname"
  48213. >apache_setenv</B
  48214. > ( string variable, string value [, bool walk_to_top])<BR
  48215. ></BR
  48216. ><P
  48217. >      <B
  48218. CLASS="function"
  48219. >apache_setenv()</B
  48220. > sets the value of the Apache
  48221.      environment variable specified by
  48222.      <VAR
  48223. CLASS="parameter"
  48224. >variable</VAR
  48225. >.
  48226.     </P
  48227. ><DIV
  48228. CLASS="note"
  48229. ><BLOCKQUOTE
  48230. CLASS="note"
  48231. ><P
  48232. ><B
  48233. >Note: </B
  48234. >
  48235.       When setting an Apache environment variable, the corresponding $_SERVER
  48236.       variable is not changed.
  48237.      </P
  48238. ></BLOCKQUOTE
  48239. ></DIV
  48240. ><TABLE
  48241. WIDTH="100%"
  48242. BORDER="0"
  48243. CELLPADDING="0"
  48244. CELLSPACING="0"
  48245. CLASS="EXAMPLE"
  48246. ><TR
  48247. ><TD
  48248. ><DIV
  48249. CLASS="example"
  48250. ><A
  48251. NAME="AEN8101"
  48252. ></A
  48253. ><P
  48254. ><B
  48255. >Example 1. Setting an Apache environment variable using <B
  48256. CLASS="function"
  48257. >apache_setenv()</B
  48258. ></B
  48259. ></P
  48260. ><TABLE
  48261. BORDER="0"
  48262. BGCOLOR="#E0E0E0"
  48263. CELLPADDING="5"
  48264. ><TR
  48265. ><TD
  48266. ><PRE
  48267. CLASS="php"
  48268. ><?php
  48269. apache_setenv("EXAMPLE_VAR", "Example Value");
  48270. ?></PRE
  48271. ></TD
  48272. ></TR
  48273. ></TABLE
  48274. ></DIV
  48275. ></TD
  48276. ></TR
  48277. ></TABLE
  48278. ><P
  48279. >      <B
  48280. CLASS="function"
  48281. >apache_setenv()</B
  48282. > can be paired up with
  48283.      <A
  48284. HREF="#function.apache-getenv"
  48285. ><B
  48286. CLASS="function"
  48287. >apache_getenv()</B
  48288. ></A
  48289. > across separate pages or for setting
  48290.      variables to pass to Server Side Includes (.shtml) that have been
  48291.      included in PHP scripts.
  48292.     </P
  48293. ><P
  48294. >      See also <A
  48295. HREF="#function.apache-getenv"
  48296. ><B
  48297. CLASS="function"
  48298. >apache_getenv()</B
  48299. ></A
  48300. >.
  48301.     </P
  48302. ></DIV
  48303. ><H1
  48304. ><A
  48305. NAME="function.ascii2ebcdic"
  48306. ></A
  48307. >ascii2ebcdic</H1
  48308. ><DIV
  48309. CLASS="refnamediv"
  48310. ><A
  48311. NAME="AEN8111"
  48312. ></A
  48313. ><P
  48314. >    (PHP 3>= 3.0.17)</P
  48315. >ascii2ebcdic -- Translate string from ASCII to EBCDIC</DIV
  48316. ><DIV
  48317. CLASS="refsect1"
  48318. ><A
  48319. NAME="AEN8114"
  48320. ></A
  48321. ><H2
  48322. >Description</H2
  48323. >int <B
  48324. CLASS="methodname"
  48325. >ascii2ebcdic</B
  48326. > ( string ascii_str)<BR
  48327. ></BR
  48328. ><P
  48329. >      <B
  48330. CLASS="function"
  48331. >ascii2ebcdic()</B
  48332. > is an Apache-specific function which
  48333.      is available only on EBCDIC based operating systems (OS/390, BS2000).
  48334.      It translates the ASCII encoded string <VAR
  48335. CLASS="parameter"
  48336. >ascii_str</VAR
  48337. >
  48338.      to its equivalent EBCDIC representation (binary safe), and returns
  48339.      the result.
  48340.     </P
  48341. ><P
  48342. >      See also the reverse function <A
  48343. HREF="#function.ebcdic2ascii"
  48344. ><B
  48345. CLASS="function"
  48346. >ebcdic2ascii()</B
  48347. ></A
  48348.     </P
  48349. ></DIV
  48350. ><H1
  48351. ><A
  48352. NAME="function.ebcdic2ascii"
  48353. ></A
  48354. >ebcdic2ascii</H1
  48355. ><DIV
  48356. CLASS="refnamediv"
  48357. ><A
  48358. NAME="AEN8128"
  48359. ></A
  48360. ><P
  48361. >    (PHP 3>= 3.0.17)</P
  48362. >ebcdic2ascii -- Translate string from EBCDIC to ASCII</DIV
  48363. ><DIV
  48364. CLASS="refsect1"
  48365. ><A
  48366. NAME="AEN8131"
  48367. ></A
  48368. ><H2
  48369. >Description</H2
  48370. >int <B
  48371. CLASS="methodname"
  48372. >ebcdic2ascii</B
  48373. > ( string ebcdic_str)<BR
  48374. ></BR
  48375. ><P
  48376. >      <B
  48377. CLASS="function"
  48378. >ebcdic2ascii()</B
  48379. > is an Apache-specific function which
  48380.      is available only on EBCDIC based operating systems (OS/390, BS2000).
  48381.      It translates the EBCDIC encoded string <VAR
  48382. CLASS="parameter"
  48383. >ebcdic_str</VAR
  48384. >
  48385.      to its equivalent ASCII representation (binary safe), and returns
  48386.      the result.
  48387.     </P
  48388. ><P
  48389. >      See also the reverse function <A
  48390. HREF="#function.ascii2ebcdic"
  48391. ><B
  48392. CLASS="function"
  48393. >ascii2ebcdic()</B
  48394. ></A
  48395.     </P
  48396. ></DIV
  48397. ><H1
  48398. ><A
  48399. NAME="function.getallheaders"
  48400. ></A
  48401. >getallheaders</H1
  48402. ><DIV
  48403. CLASS="refnamediv"
  48404. ><A
  48405. NAME="AEN8145"
  48406. ></A
  48407. ><P
  48408. >    (PHP 3, PHP 4 , PHP 5)</P
  48409. >getallheaders -- Fetch all HTTP request headers</DIV
  48410. ><DIV
  48411. CLASS="refsect1"
  48412. ><A
  48413. NAME="AEN8148"
  48414. ></A
  48415. ><H2
  48416. >Description</H2
  48417. >array <B
  48418. CLASS="methodname"
  48419. >getallheaders</B
  48420. > ( void )<BR
  48421. ></BR
  48422. ><P
  48423. >      <B
  48424. CLASS="function"
  48425. >getallheaders()</B
  48426. > is an alias for
  48427.      <A
  48428. HREF="#function.apache-request-headers"
  48429. ><B
  48430. CLASS="function"
  48431. >apache_request_headers()</B
  48432. ></A
  48433. >.  It will return an
  48434.      associative array of all the HTTP headers in the current request.
  48435.      Please read the <A
  48436. HREF="#function.apache-request-headers"
  48437. ><B
  48438. CLASS="function"
  48439. >apache_request_headers()</B
  48440. ></A
  48441. >
  48442.      documentation for more information on how this function works.
  48443.     </P
  48444. ><DIV
  48445. CLASS="note"
  48446. ><BLOCKQUOTE
  48447. CLASS="note"
  48448. ><P
  48449. ><B
  48450. >Note: </B
  48451. >
  48452.       In PHP 4.3.0, <B
  48453. CLASS="function"
  48454. >getallheaders()</B
  48455. > became an
  48456.       alias for <A
  48457. HREF="#function.apache-request-headers"
  48458. ><B
  48459. CLASS="function"
  48460. >apache_request_headers()</B
  48461. ></A
  48462. >.
  48463.       Essentially, it was renamed.  This is because this function
  48464.       only works when PHP is compiled as an
  48465.       <SPAN
  48466. CLASS="productname"
  48467. >Apache</SPAN
  48468. > Module.
  48469.      </P
  48470. ></BLOCKQUOTE
  48471. ></DIV
  48472. ><DIV
  48473. CLASS="note"
  48474. ><BLOCKQUOTE
  48475. CLASS="note"
  48476. ><P
  48477. ><B
  48478. >Note: </B
  48479. >As of PHP 4.3.3 you can use this function with the
  48480. <A
  48481. HREF="#ref.nsapi"
  48482. >NSAPI server module</A
  48483. > in Netscape/iPlanet/SunONE
  48484. webservers, too.</P
  48485. ></BLOCKQUOTE
  48486. ></DIV
  48487. ><P
  48488. >      See also <A
  48489. HREF="#function.apache-request-headers"
  48490. ><B
  48491. CLASS="function"
  48492. >apache_request_headers()</B
  48493. ></A
  48494. >.
  48495.     </P
  48496. ></DIV
  48497. ><H1
  48498. ><A
  48499. NAME="function.virtual"
  48500. ></A
  48501. >virtual</H1
  48502. ><DIV
  48503. CLASS="refnamediv"
  48504. ><A
  48505. NAME="AEN8169"
  48506. ></A
  48507. ><P
  48508. >    (PHP 3, PHP 4 , PHP 5)</P
  48509. >virtual -- Perform an Apache sub-request</DIV
  48510. ><DIV
  48511. CLASS="refsect1"
  48512. ><A
  48513. NAME="AEN8172"
  48514. ></A
  48515. ><H2
  48516. >Description</H2
  48517. >int <B
  48518. CLASS="methodname"
  48519. >virtual</B
  48520. > ( string filename)<BR
  48521. ></BR
  48522. ><P
  48523. >      <B
  48524. CLASS="function"
  48525. >virtual()</B
  48526. > is an Apache-specific function which
  48527.      is equivalent to <!--#include virtual...--> in mod_include.
  48528.      It performs an Apache sub-request.  It is useful for including
  48529.      CGI scripts or .shtml files, or anything else that you would
  48530.      parse through Apache.  Note that for a CGI script, the script
  48531.      must generate valid CGI headers.  At the minimum that means it
  48532.      must generate a Content-type header.
  48533.     </P
  48534. ><P
  48535. >      To run the sub-request, all buffers are terminated and flushed to the
  48536.      browser, pending headers are sent too.
  48537.     </P
  48538. ><DIV
  48539. CLASS="warning"
  48540. ><P
  48541. ></P
  48542. ><TABLE
  48543. CLASS="warning"
  48544. BORDER="1"
  48545. WIDTH="100%"
  48546. ><TR
  48547. ><TD
  48548. ALIGN="CENTER"
  48549. ><B
  48550. >Warning</B
  48551. ></TD
  48552. ></TR
  48553. ><TR
  48554. ><TD
  48555. ALIGN="LEFT"
  48556. ><P
  48557. >       This function works only when PHP is compiled as an Apache module,
  48558.       since it uses the Apache API for doing sub requests. Query string can be
  48559.       passed to the included file but <VAR
  48560. CLASS="varname"
  48561. >$_GET</VAR
  48562. > is copied from
  48563.       the parent script and only <VAR
  48564. CLASS="varname"
  48565. >$_SERVER['QUERY_STRING']</VAR
  48566. > is
  48567.       filled with the passed query string. The query string may only be passed
  48568.       when using Apache 2. The requested file will not be listed in the Apache access log.
  48569.      </P
  48570. ></TD
  48571. ></TR
  48572. ></TABLE
  48573. ></DIV
  48574. ><P
  48575. >      As of PHP 4.0.6, you can use <B
  48576. CLASS="function"
  48577. >virtual()</B
  48578. > on PHP files.
  48579.      However, it is typically better to use <A
  48580. HREF="#function.include"
  48581. ><B
  48582. CLASS="function"
  48583. >include()</B
  48584. ></A
  48585. > or
  48586.      <A
  48587. HREF="#function.require"
  48588. ><B
  48589. CLASS="function"
  48590. >require()</B
  48591. ></A
  48592. > if you need to include another PHP file.
  48593.     </P
  48594. ><DIV
  48595. CLASS="note"
  48596. ><BLOCKQUOTE
  48597. CLASS="note"
  48598. ><P
  48599. ><B
  48600. >Note: </B
  48601. >As of PHP 4.3.3 you can use this function with the
  48602. <A
  48603. HREF="#ref.nsapi"
  48604. >NSAPI server module</A
  48605. > in Netscape/iPlanet/SunONE
  48606. webservers, too.</P
  48607. ></BLOCKQUOTE
  48608. ></DIV
  48609. ></DIV
  48610. ></DIV
  48611. ><DIV
  48612. CLASS="reference"
  48613. ><A
  48614. NAME="ref.apd"
  48615. ></A
  48616. ><DIV
  48617. CLASS="TITLEPAGE"
  48618. ><H1
  48619. CLASS="title"
  48620. >II. Advanced PHP debugger</H1
  48621. ><DIV
  48622. CLASS="PARTINTRO"
  48623. ><A
  48624. NAME="AEN8197"
  48625. ></A
  48626. ><DIV
  48627. CLASS="section"
  48628. ><H2
  48629. CLASS="section"
  48630. ><A
  48631. NAME="apd.intro"
  48632. >Introduction</A
  48633. ></H2
  48634. ><P
  48635. >      APD is the Advanced PHP Debugger. It was written to provide profiling and
  48636.      debugging capabilities for PHP code, as well as to provide the ability to
  48637.      print out a full stack backtrace. APD supports interactive debugging, but
  48638.      by default it writes data to trace files. It also offers event based
  48639.      logging so that varying levels of information (including function calls,
  48640.      arguments passed, timings, etc.) can be turned on or off for individual
  48641.      scripts.
  48642.     <DIV
  48643. CLASS="caution"
  48644. ><P
  48645. ></P
  48646. ><TABLE
  48647. CLASS="caution"
  48648. BORDER="1"
  48649. WIDTH="100%"
  48650. ><TR
  48651. ><TD
  48652. ALIGN="CENTER"
  48653. ><B
  48654. >Caution</B
  48655. ></TD
  48656. ></TR
  48657. ><TR
  48658. ><TD
  48659. ALIGN="LEFT"
  48660. ><P
  48661. >      APD is a Zend Extension, modifying the way the internals of PHP handle 
  48662.      function calls, and thus may or may not be compatible with other Zend 
  48663.      Extensions (for example Zend Optimizer). 
  48664.     </P
  48665. ></TD
  48666. ></TR
  48667. ></TABLE
  48668. ></DIV
  48669. >
  48670.     </P
  48671. ></DIV
  48672. ><DIV
  48673. CLASS="section"
  48674. ><HR><H2
  48675. CLASS="section"
  48676. ><A
  48677. NAME="apd.installation"
  48678. >Installation</A
  48679. ></H2
  48680. ><P
  48681. >   APD is currently available as a PECL extension from 
  48682.   <A
  48683. HREF="http://pecl.php.net/package/apd"
  48684. TARGET="_top"
  48685. >http://pecl.php.net/package/apd</A
  48686. >.
  48687.   Make sure you have installed the CGI version of PHP and it is available
  48688.   in your current path along with the phpize script.
  48689.  </P
  48690. ><P
  48691. >   Run the following command to download, build, and install the latest stable
  48692.   version of APD:
  48693.  <TABLE
  48694. BORDER="0"
  48695. BGCOLOR="#E0E0E0"
  48696. CELLPADDING="5"
  48697. ><TR
  48698. ><TD
  48699. ><PRE
  48700. CLASS="screen"
  48701. >pear install apd</PRE
  48702. ></TD
  48703. ></TR
  48704. ></TABLE
  48705. >
  48706.  </P
  48707. ><P
  48708. >   This automatically installs the APD Zend module into your PHP
  48709.   extensions directory. It is not mandatory to keep it there; you can
  48710.   store the module in any directory PHP can read as long as you set
  48711.   the zend_extension parameter accordingly.
  48712.  </P
  48713. ><P
  48714. >   Windows users can download the extension dll <TT
  48715. CLASS="filename"
  48716. >php_apd.dll</TT
  48717. >
  48718.   from <A
  48719. HREF="http://snaps.php.net/win32/PECL_STABLE/"
  48720. TARGET="_top"
  48721. >http://snaps.php.net/win32/PECL_STABLE/</A
  48722. >.
  48723.  </P
  48724. ><P
  48725. >   In your INI file, add the following lines:
  48726.  </P
  48727. ><P
  48728. >   <TABLE
  48729. BORDER="0"
  48730. BGCOLOR="#E0E0E0"
  48731. CELLPADDING="5"
  48732. ><TR
  48733. ><TD
  48734. ><PRE
  48735. CLASS="php.ini"
  48736. >zend_extension = /absolute/path/to/apd.so
  48737. apd.dumpdir = /absolute/path/to/trace/directory
  48738. apd.statement_trace = 0</PRE
  48739. ></TD
  48740. ></TR
  48741. ></TABLE
  48742. >
  48743.  </P
  48744. ><P
  48745. >   Depending on your PHP build, the zend_extension directive can be one of the
  48746.   following:
  48747.  </P
  48748. ><P
  48749. >   <TABLE
  48750. BORDER="0"
  48751. BGCOLOR="#E0E0E0"
  48752. CELLPADDING="5"
  48753. ><TR
  48754. ><TD
  48755. ><PRE
  48756. CLASS="script"
  48757. >zend_extension              (non ZTS, non debug build)
  48758. zend_extension_ts           (    ZTS, non debug build)
  48759. zend_extension_debug        (non ZTS,     debug build)
  48760. zend_extension_debug_ts     (    ZTS,     debug build)</PRE
  48761. ></TD
  48762. ></TR
  48763. ></TABLE
  48764. >
  48765.  </P
  48766. ></DIV
  48767. ><DIV
  48768. CLASS="section"
  48769. ><HR><H2
  48770. CLASS="section"
  48771. ><A
  48772. NAME="apd.installwin32"
  48773. >Building on Win32</A
  48774. ></H2
  48775. ><P
  48776. >   To build APD under Windows you need a working PHP compilation
  48777.   environment as described on http://php.net/ -- basically, it requires
  48778.   you to have Microsoft Visual C++, win32build.zip, bison/flex, and some know how
  48779.   to get it to work. Also ensure that adp.dsp has DOS line endings; if it has unix
  48780.   line endings, Microsoft Visual C++ will complain about it.
  48781.  </P
  48782. ></DIV
  48783. ><DIV
  48784. CLASS="section"
  48785. ><HR><H2
  48786. CLASS="section"
  48787. ><A
  48788. NAME="apd.configuration"
  48789. >Runtime Configuration</A
  48790. ></H2
  48791. ><P
  48792. >Here's a short explanation of
  48793. the configuration directives.</P
  48794. ><P
  48795. >   <P
  48796. ></P
  48797. ><DIV
  48798. CLASS="variablelist"
  48799. ><DL
  48800. ><DT
  48801. ><A
  48802. NAME="ini.apd.dumpdir"
  48803. ></A
  48804. ><VAR
  48805. CLASS="parameter"
  48806. >apd.dumpdir</VAR
  48807. >
  48808.      <A
  48809. HREF="#language.types.string"
  48810. ><B
  48811. CLASS="type"
  48812. >string</B
  48813. ></A
  48814. ></DT
  48815. ><DD
  48816. ><P
  48817. >       Sets the directory in which APD writes profile dump files.
  48818.       You can specify an absolute path or a relative path.
  48819.      </P
  48820. ><P
  48821. >       You can specify a different directory as an argument
  48822.       to <A
  48823. HREF="#function.apd-set-pprof-trace"
  48824. ><B
  48825. CLASS="function"
  48826. >apd_set_pprof_trace()</B
  48827. ></A
  48828. >.
  48829.      </P
  48830. ></DD
  48831. ><DT
  48832. ><A
  48833. NAME="ini.apd.statement-trace"
  48834. ></A
  48835. ><VAR
  48836. CLASS="parameter"
  48837. >apd.statement_trace</VAR
  48838. >
  48839.      <A
  48840. HREF="#language.types.boolean"
  48841. ><B
  48842. CLASS="type"
  48843. >boolean</B
  48844. ></A
  48845. ></DT
  48846. ><DD
  48847. ><P
  48848. >      Specfies whether or not to do per-line tracings. Turning this on (1) will
  48849.      impact the performance of your application.
  48850.      </P
  48851. ></DD
  48852. ></DL
  48853. ></DIV
  48854. >
  48855.  </P
  48856. ></DIV
  48857. ><DIV
  48858. CLASS="section"
  48859. ><HR><H2
  48860. CLASS="section"
  48861. ><A
  48862. NAME="apd.resources"
  48863. >Resource Types</A
  48864. ></H2
  48865. ><P
  48866. >This extension has no resource types defined.</P
  48867. ></DIV
  48868. ><DIV
  48869. CLASS="section"
  48870. ><HR><H2
  48871. CLASS="section"
  48872. ><A
  48873. NAME="apd.constants"
  48874. >Predefined Constants</A
  48875. ></H2
  48876. ><P
  48877. >This extension has no constants defined.</P
  48878. ></DIV
  48879. ><DIV
  48880. CLASS="section"
  48881. ><HR><H2
  48882. CLASS="section"
  48883. ><A
  48884. NAME="apd.examples"
  48885. >How to use PHP-APD in your scripts</A
  48886. ></H2
  48887. ><DIV
  48888. CLASS="procedure"
  48889. ><OL
  48890. TYPE="1"
  48891. ><LI
  48892. ><P
  48893. >       As the first line of your PHP script, call the apd_set_pprof_trace() function
  48894.       to start the trace:
  48895.      </P
  48896. ><P
  48897. >       <TABLE
  48898. BORDER="0"
  48899. BGCOLOR="#E0E0E0"
  48900. CELLPADDING="5"
  48901. ><TR
  48902. ><TD
  48903. ><PRE
  48904. CLASS="php"
  48905. >apd_set_pprof_trace();</PRE
  48906. ></TD
  48907. ></TR
  48908. ></TABLE
  48909. >
  48910.      </P
  48911. ><P
  48912. >      You can insert the line anywhere in your script, but if you do not start
  48913.      tracing at the beginning of your script you discard profile data that might
  48914.      otherwise lead you to a performance bottleneck.
  48915.      </P
  48916. ></LI
  48917. ><LI
  48918. ><P
  48919. >       Now run your script. The dump output will be written to
  48920.       <TT
  48921. CLASS="filename"
  48922. >apd.dumpdir/pprof_pid.ext</TT
  48923. >.
  48924.       <DIV
  48925. CLASS="tip"
  48926. ><BLOCKQUOTE
  48927. CLASS="tip"
  48928. ><P
  48929. ><B
  48930. >Tip: </B
  48931. >
  48932.         If you're running the CGI version of PHP, you will need to add the '-e'
  48933.         flag to enable extended information for apd to work properly. For
  48934.         example: 
  48935.         <KBD
  48936. CLASS="userinput"
  48937. >php -e -f script.php</KBD
  48938. >
  48939.       </P
  48940. ></BLOCKQUOTE
  48941. ></DIV
  48942. >
  48943.      </P
  48944. ></LI
  48945. ><LI
  48946. ><P
  48947. >       To display formatted profile data, issue the <B
  48948. CLASS="command"
  48949. >pprofp</B
  48950. >
  48951.       command with the sort and display options of your choice. The formatted
  48952.       output will look something like:
  48953.       <TABLE
  48954. BORDER="0"
  48955. BGCOLOR="#E0E0E0"
  48956. CELLPADDING="5"
  48957. ><TR
  48958. ><TD
  48959. ><PRE
  48960. CLASS="screen"
  48961. >bash-2.05b$ pprofp -R /tmp/pprof.22141.0
  48962.  
  48963. Trace for /home/dan/testapd.php
  48964. Total Elapsed Time = 0.00
  48965. Total System Time  = 0.00
  48966. Total User Time    = 0.00
  48967.  
  48968.  
  48969. Real         User        System             secs/    cumm
  48970. %Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
  48971. --------------------------------------------------------------------------------------
  48972. 100.0 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0000   0.0009            0 main
  48973. 56.9 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0005   0.0005            0 apd_set_pprof_trace
  48974. 28.0 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 preg_replace
  48975. 14.3 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 str_replace</PRE
  48976. ></TD
  48977. ></TR
  48978. ></TABLE
  48979. >
  48980.      </P
  48981. ><P
  48982. >      The -R option used in this example sorts the profile table by the amount
  48983.      of real time the script spent executing a given function. The "cumm call"
  48984.      column reveals how many times each function was called, and the "s/call"
  48985.      column reveals how many seconds each call to the function required, on
  48986.      average.
  48987.      </P
  48988. ></LI
  48989. ><LI
  48990. ><P
  48991. >      To generate a calltree file that you can import into the KCacheGrind
  48992.      profile analysis application, issue the <B
  48993. CLASS="command"
  48994. >pprof2calltree</B
  48995. >
  48996.      comand.
  48997.      </P
  48998. ></LI
  48999. ></OL
  49000. ></DIV
  49001. ></DIV
  49002. ><DIV
  49003. CLASS="section"
  49004. ><HR><H2
  49005. CLASS="section"
  49006. ><A
  49007. NAME="apd.contact"
  49008. >Contact information</A
  49009. ></H2
  49010. ><P
  49011. >       If you have comments, bugfixes, enhancements or want to help developing 
  49012.       this beast, you can send an mail to 
  49013.       <A
  49014. HREF="mailto:apd@mail.communityconnect.com"
  49015. TARGET="_top"
  49016. >apd@mail.communityconnect.com</A
  49017. >. Any help is very 
  49018.       welcome.
  49019.      </P
  49020. ></DIV
  49021. ></DIV
  49022. ><DIV
  49023. CLASS="TOC"
  49024. ><DL
  49025. ><DT
  49026. ><B
  49027. >Table of Contents</B
  49028. ></DT
  49029. ><DT
  49030. ><A
  49031. HREF="#function.apd-breakpoint"
  49032. >apd_breakpoint</A
  49033. > -- Stops the interpreter and waits on a CR from the socket</DT
  49034. ><DT
  49035. ><A
  49036. HREF="#function.apd-callstack"
  49037. >apd_callstack</A
  49038. > -- Returns the current call stack as an array</DT
  49039. ><DT
  49040. ><A
  49041. HREF="#function.apd-clunk"
  49042. >apd_clunk</A
  49043. > -- Throw a warning and a callstack</DT
  49044. ><DT
  49045. ><A
  49046. HREF="#function.apd-continue"
  49047. >apd_continue</A
  49048. > -- Restarts the interpreter</DT
  49049. ><DT
  49050. ><A
  49051. HREF="#function.apd-croak"
  49052. >apd_croak</A
  49053. > -- Throw an error, a callstack and then exit</DT
  49054. ><DT
  49055. ><A
  49056. HREF="#function.apd-dump-function-table"
  49057. >apd_dump_function_table</A
  49058. > -- Outputs the current function table</DT
  49059. ><DT
  49060. ><A
  49061. HREF="#function.apd-dump-persistent-resources"
  49062. >apd_dump_persistent_resources</A
  49063. > -- Return all persistent resources as an array</DT
  49064. ><DT
  49065. ><A
  49066. HREF="#function.apd-dump-regular-resources"
  49067. >apd_dump_regular_resources</A
  49068. > -- Return all current regular resources as an array</DT
  49069. ><DT
  49070. ><A
  49071. HREF="#function.apd-echo"
  49072. >apd_echo</A
  49073. > -- Echo to the debugging socket</DT
  49074. ><DT
  49075. ><A
  49076. HREF="#function.apd-get-active-symbols"
  49077. >apd_get_active_symbols</A
  49078. > -- Get an array of the current variables names in the local scope</DT
  49079. ><DT
  49080. ><A
  49081. HREF="#function.apd-set-pprof-trace"
  49082. >apd_set_pprof_trace</A
  49083. > -- Starts the session debugging</DT
  49084. ><DT
  49085. ><A
  49086. HREF="#function.apd-set-session-trace"
  49087. >apd_set_session_trace</A
  49088. > -- Starts the session debugging</DT
  49089. ><DT
  49090. ><A
  49091. HREF="#function.apd-set-session"
  49092. >apd_set_session</A
  49093. > -- Changes or sets the current debugging level</DT
  49094. ><DT
  49095. ><A
  49096. HREF="#function.apd-set-socket-session-trace"
  49097. >apd_set_socket_session_trace</A
  49098. > -- Starts the remote session debugging</DT
  49099. ><DT
  49100. ><A
  49101. HREF="#function.override-function"
  49102. >override_function</A
  49103. > -- Overrides built-in functions</DT
  49104. ><DT
  49105. ><A
  49106. HREF="#function.rename-function"
  49107. >rename_function</A
  49108. > -- Renames orig_name to new_name in the global function_table</DT
  49109. ></DL
  49110. ></DIV
  49111. ></DIV
  49112. ><H1
  49113. ><A
  49114. NAME="function.apd-breakpoint"
  49115. ></A
  49116. >apd_breakpoint</H1
  49117. ><DIV
  49118. CLASS="refnamediv"
  49119. ><A
  49120. NAME="AEN8274"
  49121. ></A
  49122. ><P
  49123. >    (no version information, might be only in CVS)</P
  49124. >apd_breakpoint -- Stops the interpreter and waits on a CR from the socket</DIV
  49125. ><DIV
  49126. CLASS="refsect1"
  49127. ><A
  49128. NAME="AEN8277"
  49129. ></A
  49130. ><H2
  49131. >Description</H2
  49132. >void <B
  49133. CLASS="methodname"
  49134. >apd_breakpoint</B
  49135. > ( int debug_level)<BR
  49136. ></BR
  49137. ><P
  49138. >      This can be used to stop the running of your script, and await responses 
  49139.      on the connected socket.  To step the program, just send enter (a blank 
  49140.      line), or enter a php command to be executed.  A typical session using 
  49141.      tcplisten would look like this.
  49142.     </P
  49143. ><P
  49144. >      <TABLE
  49145. BORDER="0"
  49146. BGCOLOR="#E0E0E0"
  49147. CELLPADDING="5"
  49148. ><TR
  49149. ><TD
  49150. ><PRE
  49151. CLASS="shell"
  49152. >bash#tcplisten localhost 7777
  49153.  
  49154. APD - Advanced PHP Debugger Trace File
  49155. ---------------------------------------------------------------------------
  49156. Process Pid (6118)
  49157. Trace Begun at Sun Mar 10 23:13:12 2002
  49158. ---------------------------------------------------------------------------
  49159. (  0.000000): apd_set_session_trace called at /home/alan/Projects/project2/test. 
  49160. php:5
  49161. (  0.074824): apd_set_session_trace_socket() at /home/alan/Projects/project2/tes 
  49162. t.php:5 returned.  Elapsed (0.074824)
  49163. (  0.074918): apd_breakpoint() /home/alan/Projects/project2/test.php:7
  49164.               ++ argv[0] $(??) = 9
  49165. apd_breakpoint() at /home/alan/Projects/project2/test.php:7 returned.  Elapsed ( 
  49166. -2089521468.1073275368)
  49167. >\n 
  49168. statement: /home/alan/Projects/project2/test.php:8
  49169. >\n 
  49170. statement: /home/alan/Projects/project2/test.php:8
  49171. >\n 
  49172. statement: /home/alan/Projects/project2/test.php:10
  49173. >apd_echo($i);
  49174. EXEC: apd_echo($i);
  49175. 0
  49176. >apd_echo(serialize(apd_get_active_symbols()));
  49177. EXEC:  apd_echo(serialize(apd_get_active_symbols()));
  49178. a:47:{i:0;s:4:"PWD";i:1;s:10:"COLORFGBG";i:2;s:11:"XAUTHORITY";i:3;s:14:"
  49179. COLORTERM_BCE";i:4;s:9:"WINDOWID";i:5;s:14:"ETERM_VERSION";i:6;s:16:"SE
  49180. SSION_MANAGER";i:7;s:4:"PS1";i:8;s:11:"GDMSESSION";i:9;s:5:"USER";i:10;s:5:"
  49181. MAIL";i:11;s:7:"OLDPWD";i:12;s:5:"LANG";i:13;s:10:"COLORTERM";i:14;s:8:"DISP
  49182. LAY";i:15;s:8:"LOGNAME";i:16;s:6:"
  49183. >apd_echo(system('ls /home/mydir'));
  49184. ........
  49185. >apd_continue(0);</PRE
  49186. ></TD
  49187. ></TR
  49188. ></TABLE
  49189. >
  49190.     </P
  49191. ></DIV
  49192. ><H1
  49193. ><A
  49194. NAME="function.apd-callstack"
  49195. ></A
  49196. >apd_callstack</H1
  49197. ><DIV
  49198. CLASS="refnamediv"
  49199. ><A
  49200. NAME="AEN8289"
  49201. ></A
  49202. ><P
  49203. >    (no version information, might be only in CVS)</P
  49204. >apd_callstack -- Returns the current call stack as an array</DIV
  49205. ><DIV
  49206. CLASS="refsect1"
  49207. ><A
  49208. NAME="AEN8292"
  49209. ></A
  49210. ><H2
  49211. >Description</H2
  49212. >array <B
  49213. CLASS="methodname"
  49214. >apd_callstack</B
  49215. > ( void )<BR
  49216. ></BR
  49217. ><P
  49218. >      Returns the current call stack as an array
  49219.     </P
  49220. ><P
  49221. >      <TABLE
  49222. WIDTH="100%"
  49223. BORDER="0"
  49224. CELLPADDING="0"
  49225. CELLSPACING="0"
  49226. CLASS="EXAMPLE"
  49227. ><TR
  49228. ><TD
  49229. ><DIV
  49230. CLASS="example"
  49231. ><A
  49232. NAME="AEN8300"
  49233. ></A
  49234. ><P
  49235. ><B
  49236. >Example 1. <B
  49237. CLASS="function"
  49238. >apd_callstack()</B
  49239. > example</B
  49240. ></P
  49241. ><TABLE
  49242. BORDER="0"
  49243. BGCOLOR="#E0E0E0"
  49244. CELLPADDING="5"
  49245. ><TR
  49246. ><TD
  49247. ><PRE
  49248. CLASS="php"
  49249. ><?php
  49250. print_r(apd_callstack());
  49251. ?></PRE
  49252. ></TD
  49253. ></TR
  49254. ></TABLE
  49255. ></DIV
  49256. ></TD
  49257. ></TR
  49258. ></TABLE
  49259. >
  49260.     </P
  49261. ></DIV
  49262. ><H1
  49263. ><A
  49264. NAME="function.apd-clunk"
  49265. ></A
  49266. >apd_clunk</H1
  49267. ><DIV
  49268. CLASS="refnamediv"
  49269. ><A
  49270. NAME="AEN8305"
  49271. ></A
  49272. ><P
  49273. >    (no version information, might be only in CVS)</P
  49274. >apd_clunk -- Throw a warning and a callstack</DIV
  49275. ><DIV
  49276. CLASS="refsect1"
  49277. ><A
  49278. NAME="AEN8308"
  49279. ></A
  49280. ><H2
  49281. >Description</H2
  49282. >void <B
  49283. CLASS="methodname"
  49284. >apd_clunk</B
  49285. > ( string warning [, string delimiter])<BR
  49286. ></BR
  49287. ><P
  49288. >      Behaves like perl's Carp::cluck. Throw a warning and a callstack.
  49289.      The default line delimiter is "<BR />\n".
  49290.     </P
  49291. ><P
  49292. >      <TABLE
  49293. WIDTH="100%"
  49294. BORDER="0"
  49295. CELLPADDING="0"
  49296. CELLSPACING="0"
  49297. CLASS="EXAMPLE"
  49298. ><TR
  49299. ><TD
  49300. ><DIV
  49301. CLASS="example"
  49302. ><A
  49303. NAME="AEN8321"
  49304. ></A
  49305. ><P
  49306. ><B
  49307. >Example 1. <B
  49308. CLASS="function"
  49309. >apd_clunk()</B
  49310. > example</B
  49311. ></P
  49312. ><TABLE
  49313. BORDER="0"
  49314. BGCOLOR="#E0E0E0"
  49315. CELLPADDING="5"
  49316. ><TR
  49317. ><TD
  49318. ><PRE
  49319. CLASS="php"
  49320. ><?php
  49321. apd_clunk("Some Warning","<P>");
  49322. ?></PRE
  49323. ></TD
  49324. ></TR
  49325. ></TABLE
  49326. ></DIV
  49327. ></TD
  49328. ></TR
  49329. ></TABLE
  49330. >
  49331.     </P
  49332. ></DIV
  49333. ><H1
  49334. ><A
  49335. NAME="function.apd-continue"
  49336. ></A
  49337. >apd_continue</H1
  49338. ><DIV
  49339. CLASS="refnamediv"
  49340. ><A
  49341. NAME="AEN8326"
  49342. ></A
  49343. ><P
  49344. >    (no version information, might be only in CVS)</P
  49345. >apd_continue -- Restarts the interpreter</DIV
  49346. ><DIV
  49347. CLASS="refsect1"
  49348. ><A
  49349. NAME="AEN8329"
  49350. ></A
  49351. ><H2
  49352. >Description</H2
  49353. >void <B
  49354. CLASS="methodname"
  49355. >apd_continue</B
  49356. > ( int debug_level)<BR
  49357. ></BR
  49358. ><P
  49359. >      Usually sent via the socket to restart the interpreter.
  49360.     </P
  49361. ><P
  49362. >      <TABLE
  49363. WIDTH="100%"
  49364. BORDER="0"
  49365. CELLPADDING="0"
  49366. CELLSPACING="0"
  49367. CLASS="EXAMPLE"
  49368. ><TR
  49369. ><TD
  49370. ><DIV
  49371. CLASS="example"
  49372. ><A
  49373. NAME="AEN8339"
  49374. ></A
  49375. ><P
  49376. ><B
  49377. >Example 1. <B
  49378. CLASS="function"
  49379. >apd_continue()</B
  49380. > example</B
  49381. ></P
  49382. ><TABLE
  49383. BORDER="0"
  49384. BGCOLOR="#E0E0E0"
  49385. CELLPADDING="5"
  49386. ><TR
  49387. ><TD
  49388. ><PRE
  49389. CLASS="php"
  49390. ><?php
  49391. apd_continue(0);
  49392. ?></PRE
  49393. ></TD
  49394. ></TR
  49395. ></TABLE
  49396. ></DIV
  49397. ></TD
  49398. ></TR
  49399. ></TABLE
  49400. >
  49401.     </P
  49402. ></DIV
  49403. ><H1
  49404. ><A
  49405. NAME="function.apd-croak"
  49406. ></A
  49407. >apd_croak</H1
  49408. ><DIV
  49409. CLASS="refnamediv"
  49410. ><A
  49411. NAME="AEN8344"
  49412. ></A
  49413. ><P
  49414. >    (no version information, might be only in CVS)</P
  49415. >apd_croak -- Throw an error, a callstack and then exit</DIV
  49416. ><DIV
  49417. CLASS="refsect1"
  49418. ><A
  49419. NAME="AEN8347"
  49420. ></A
  49421. ><H2
  49422. >Description</H2
  49423. >void <B
  49424. CLASS="methodname"
  49425. >apd_croak</B
  49426. > ( string warning [, string delimiter])<BR
  49427. ></BR
  49428. ><P
  49429. >      Behaves like perl's Carp::croak. Throw an error, a callstack and then
  49430.      exit.  The default line delimiter is "<BR />\n".
  49431.     </P
  49432. ><P
  49433. >      <TABLE
  49434. WIDTH="100%"
  49435. BORDER="0"
  49436. CELLPADDING="0"
  49437. CELLSPACING="0"
  49438. CLASS="EXAMPLE"
  49439. ><TR
  49440. ><TD
  49441. ><DIV
  49442. CLASS="example"
  49443. ><A
  49444. NAME="AEN8360"
  49445. ></A
  49446. ><P
  49447. ><B
  49448. >Example 1. <B
  49449. CLASS="function"
  49450. >apd_croak()</B
  49451. > example</B
  49452. ></P
  49453. ><TABLE
  49454. BORDER="0"
  49455. BGCOLOR="#E0E0E0"
  49456. CELLPADDING="5"
  49457. ><TR
  49458. ><TD
  49459. ><PRE
  49460. CLASS="php"
  49461. ><?php
  49462. apd_croak("Some Warning","<P>");
  49463. ?></PRE
  49464. ></TD
  49465. ></TR
  49466. ></TABLE
  49467. ></DIV
  49468. ></TD
  49469. ></TR
  49470. ></TABLE
  49471. >
  49472.     </P
  49473. ></DIV
  49474. ><H1
  49475. ><A
  49476. NAME="function.apd-dump-function-table"
  49477. ></A
  49478. >apd_dump_function_table</H1
  49479. ><DIV
  49480. CLASS="refnamediv"
  49481. ><A
  49482. NAME="AEN8365"
  49483. ></A
  49484. ><P
  49485. >    (no version information, might be only in CVS)</P
  49486. >apd_dump_function_table -- Outputs the current function table</DIV
  49487. ><DIV
  49488. CLASS="refsect1"
  49489. ><A
  49490. NAME="AEN8368"
  49491. ></A
  49492. ><H2
  49493. >Description</H2
  49494. >void <B
  49495. CLASS="methodname"
  49496. >apd_dump_function_table</B
  49497. > ( void )<BR
  49498. ></BR
  49499. ><P
  49500. >      Outputs the current function table.
  49501.     </P
  49502. ><P
  49503. >      <TABLE
  49504. WIDTH="100%"
  49505. BORDER="0"
  49506. CELLPADDING="0"
  49507. CELLSPACING="0"
  49508. CLASS="EXAMPLE"
  49509. ><TR
  49510. ><TD
  49511. ><DIV
  49512. CLASS="example"
  49513. ><A
  49514. NAME="AEN8376"
  49515. ></A
  49516. ><P
  49517. ><B
  49518. >Example 1. <B
  49519. CLASS="function"
  49520. >apd_dump_function_table()</B
  49521. > example</B
  49522. ></P
  49523. ><TABLE
  49524. BORDER="0"
  49525. BGCOLOR="#E0E0E0"
  49526. CELLPADDING="5"
  49527. ><TR
  49528. ><TD
  49529. ><PRE
  49530. CLASS="php"
  49531. ><?php
  49532. apd_dump_function_table();
  49533. ?></PRE
  49534. ></TD
  49535. ></TR
  49536. ></TABLE
  49537. ></DIV
  49538. ></TD
  49539. ></TR
  49540. ></TABLE
  49541. >
  49542.     </P
  49543. ></DIV
  49544. ><H1
  49545. ><A
  49546. NAME="function.apd-dump-persistent-resources"
  49547. ></A
  49548. >apd_dump_persistent_resources</H1
  49549. ><DIV
  49550. CLASS="refnamediv"
  49551. ><A
  49552. NAME="AEN8381"
  49553. ></A
  49554. ><P
  49555. >    (no version information, might be only in CVS)</P
  49556. >apd_dump_persistent_resources -- Return all persistent resources as an array</DIV
  49557. ><DIV
  49558. CLASS="refsect1"
  49559. ><A
  49560. NAME="AEN8384"
  49561. ></A
  49562. ><H2
  49563. >Description</H2
  49564. >array <B
  49565. CLASS="methodname"
  49566. >apd_dump_persistent_resources</B
  49567. > ( void )<BR
  49568. ></BR
  49569. ><P
  49570. >      Return all persistent resources as an array.
  49571.     </P
  49572. ><P
  49573. >      <TABLE
  49574. WIDTH="100%"
  49575. BORDER="0"
  49576. CELLPADDING="0"
  49577. CELLSPACING="0"
  49578. CLASS="EXAMPLE"
  49579. ><TR
  49580. ><TD
  49581. ><DIV
  49582. CLASS="example"
  49583. ><A
  49584. NAME="AEN8392"
  49585. ></A
  49586. ><P
  49587. ><B
  49588. >Example 1. <B
  49589. CLASS="function"
  49590. >apd_dump_persistent_resources()</B
  49591. > example</B
  49592. ></P
  49593. ><TABLE
  49594. BORDER="0"
  49595. BGCOLOR="#E0E0E0"
  49596. CELLPADDING="5"
  49597. ><TR
  49598. ><TD
  49599. ><PRE
  49600. CLASS="php"
  49601. ><?php
  49602. print_r(apd_dump_persistent_resources());
  49603. ?></PRE
  49604. ></TD
  49605. ></TR
  49606. ></TABLE
  49607. ></DIV
  49608. ></TD
  49609. ></TR
  49610. ></TABLE
  49611. >
  49612.     </P
  49613. ></DIV
  49614. ><H1
  49615. ><A
  49616. NAME="function.apd-dump-regular-resources"
  49617. ></A
  49618. >apd_dump_regular_resources</H1
  49619. ><DIV
  49620. CLASS="refnamediv"
  49621. ><A
  49622. NAME="AEN8397"
  49623. ></A
  49624. ><P
  49625. >    (no version information, might be only in CVS)</P
  49626. >apd_dump_regular_resources -- Return all current regular resources as an array</DIV
  49627. ><DIV
  49628. CLASS="refsect1"
  49629. ><A
  49630. NAME="AEN8400"
  49631. ></A
  49632. ><H2
  49633. >Description</H2
  49634. >array <B
  49635. CLASS="methodname"
  49636. >apd_dump_regular_resources</B
  49637. > ( void )<BR
  49638. ></BR
  49639. ><P
  49640. >      Return all current regular resources as an array.
  49641.     </P
  49642. ><P
  49643. >      <TABLE
  49644. WIDTH="100%"
  49645. BORDER="0"
  49646. CELLPADDING="0"
  49647. CELLSPACING="0"
  49648. CLASS="EXAMPLE"
  49649. ><TR
  49650. ><TD
  49651. ><DIV
  49652. CLASS="example"
  49653. ><A
  49654. NAME="AEN8408"
  49655. ></A
  49656. ><P
  49657. ><B
  49658. >Example 1. <B
  49659. CLASS="function"
  49660. >apd_dump_regular_resources()</B
  49661. > example</B
  49662. ></P
  49663. ><TABLE
  49664. BORDER="0"
  49665. BGCOLOR="#E0E0E0"
  49666. CELLPADDING="5"
  49667. ><TR
  49668. ><TD
  49669. ><PRE
  49670. CLASS="php"
  49671. ><?php
  49672. print_r(apd_dump_regular_resources());
  49673. ?></PRE
  49674. ></TD
  49675. ></TR
  49676. ></TABLE
  49677. ></DIV
  49678. ></TD
  49679. ></TR
  49680. ></TABLE
  49681. >
  49682.     </P
  49683. ></DIV
  49684. ><H1
  49685. ><A
  49686. NAME="function.apd-echo"
  49687. ></A
  49688. >apd_echo</H1
  49689. ><DIV
  49690. CLASS="refnamediv"
  49691. ><A
  49692. NAME="AEN8413"
  49693. ></A
  49694. ><P
  49695. >    (no version information, might be only in CVS)</P
  49696. >apd_echo -- Echo to the debugging socket</DIV
  49697. ><DIV
  49698. CLASS="refsect1"
  49699. ><A
  49700. NAME="AEN8416"
  49701. ></A
  49702. ><H2
  49703. >Description</H2
  49704. >void <B
  49705. CLASS="methodname"
  49706. >apd_echo</B
  49707. > ( string output)<BR
  49708. ></BR
  49709. ><P
  49710. >      Usually sent via the socket to request information about the running 
  49711.      script.
  49712.     </P
  49713. ><P
  49714. >      <TABLE
  49715. WIDTH="100%"
  49716. BORDER="0"
  49717. CELLPADDING="0"
  49718. CELLSPACING="0"
  49719. CLASS="EXAMPLE"
  49720. ><TR
  49721. ><TD
  49722. ><DIV
  49723. CLASS="example"
  49724. ><A
  49725. NAME="AEN8426"
  49726. ></A
  49727. ><P
  49728. ><B
  49729. >Example 1. <B
  49730. CLASS="function"
  49731. >apd_echo()</B
  49732. > example</B
  49733. ></P
  49734. ><TABLE
  49735. BORDER="0"
  49736. BGCOLOR="#E0E0E0"
  49737. CELLPADDING="5"
  49738. ><TR
  49739. ><TD
  49740. ><PRE
  49741. CLASS="php"
  49742. ><?php
  49743. apd_echo($i);
  49744. ?></PRE
  49745. ></TD
  49746. ></TR
  49747. ></TABLE
  49748. ></DIV
  49749. ></TD
  49750. ></TR
  49751. ></TABLE
  49752. >
  49753.     </P
  49754. ></DIV
  49755. ><H1
  49756. ><A
  49757. NAME="function.apd-get-active-symbols"
  49758. ></A
  49759. >apd_get_active_symbols</H1
  49760. ><DIV
  49761. CLASS="refnamediv"
  49762. ><A
  49763. NAME="AEN8431"
  49764. ></A
  49765. ><P
  49766. >    (no version information, might be only in CVS)</P
  49767. >apd_get_active_symbols -- Get an array of the current variables names in the local scope</DIV
  49768. ><DIV
  49769. CLASS="refsect1"
  49770. ><A
  49771. NAME="AEN8434"
  49772. ></A
  49773. ><H2
  49774. >Description</H2
  49775. >array <B
  49776. CLASS="methodname"
  49777. >apd_get_active_symbols</B
  49778. > ( )<BR
  49779. ></BR
  49780. ><P
  49781. >      Returns the names of all the variables defined in the active scope, (not 
  49782.      their values)
  49783.     </P
  49784. ><P
  49785. >      <TABLE
  49786. WIDTH="100%"
  49787. BORDER="0"
  49788. CELLPADDING="0"
  49789. CELLSPACING="0"
  49790. CLASS="EXAMPLE"
  49791. ><TR
  49792. ><TD
  49793. ><DIV
  49794. CLASS="example"
  49795. ><A
  49796. NAME="AEN8443"
  49797. ></A
  49798. ><P
  49799. ><B
  49800. >Example 1. <B
  49801. CLASS="function"
  49802. >apd_get_active_symbols()</B
  49803. > example</B
  49804. ></P
  49805. ><TABLE
  49806. BORDER="0"
  49807. BGCOLOR="#E0E0E0"
  49808. CELLPADDING="5"
  49809. ><TR
  49810. ><TD
  49811. ><PRE
  49812. CLASS="php"
  49813. ><?php
  49814. apd_echo(apd_get_active_symbols());
  49815. ?></PRE
  49816. ></TD
  49817. ></TR
  49818. ></TABLE
  49819. ></DIV
  49820. ></TD
  49821. ></TR
  49822. ></TABLE
  49823. >
  49824.     </P
  49825. ></DIV
  49826. ><H1
  49827. ><A
  49828. NAME="function.apd-set-pprof-trace"
  49829. ></A
  49830. >apd_set_pprof_trace</H1
  49831. ><DIV
  49832. CLASS="refnamediv"
  49833. ><A
  49834. NAME="AEN8448"
  49835. ></A
  49836. ><P
  49837. >    (no version information, might be only in CVS)</P
  49838. >apd_set_pprof_trace -- Starts the session debugging</DIV
  49839. ><DIV
  49840. CLASS="refsect1"
  49841. ><A
  49842. NAME="AEN8451"
  49843. ></A
  49844. ><H2
  49845. >Description</H2
  49846. >void <B
  49847. CLASS="methodname"
  49848. >apd_set_pprof_trace</B
  49849. > ( [string dump_directory])<BR
  49850. ></BR
  49851. ><P
  49852. >      Starts debugging to {dump_directory}/pprof_{process_id}, if 
  49853.      dump_directory is not set, then the apd.dumpdir setting from the 
  49854.      <TT
  49855. CLASS="filename"
  49856. >php.ini</TT
  49857. > file is used.
  49858.     </P
  49859. ><P
  49860. >      <TABLE
  49861. WIDTH="100%"
  49862. BORDER="0"
  49863. CELLPADDING="0"
  49864. CELLSPACING="0"
  49865. CLASS="EXAMPLE"
  49866. ><TR
  49867. ><TD
  49868. ><DIV
  49869. CLASS="example"
  49870. ><A
  49871. NAME="AEN8462"
  49872. ></A
  49873. ><P
  49874. ><B
  49875. >Example 1. <B
  49876. CLASS="function"
  49877. >apd_set_pprof_trace()</B
  49878. > example</B
  49879. ></P
  49880. ><TABLE
  49881. BORDER="0"
  49882. BGCOLOR="#E0E0E0"
  49883. CELLPADDING="5"
  49884. ><TR
  49885. ><TD
  49886. ><PRE
  49887. CLASS="php"
  49888. ><?php
  49889. apd_set_pprof_trace();
  49890. ?></PRE
  49891. ></TD
  49892. ></TR
  49893. ></TABLE
  49894. ></DIV
  49895. ></TD
  49896. ></TR
  49897. ></TABLE
  49898. >
  49899.     </P
  49900. ></DIV
  49901. ><H1
  49902. ><A
  49903. NAME="function.apd-set-session-trace"
  49904. ></A
  49905. >apd_set_session_trace</H1
  49906. ><DIV
  49907. CLASS="refnamediv"
  49908. ><A
  49909. NAME="AEN8467"
  49910. ></A
  49911. ><P
  49912. >    (no version information, might be only in CVS)</P
  49913. >apd_set_session_trace -- Starts the session debugging</DIV
  49914. ><DIV
  49915. CLASS="refsect1"
  49916. ><A
  49917. NAME="AEN8470"
  49918. ></A
  49919. ><H2
  49920. >Description</H2
  49921. >void <B
  49922. CLASS="methodname"
  49923. >apd_set_session_trace</B
  49924. > ( int debug_level [, string dump_directory])<BR
  49925. ></BR
  49926. ><P
  49927. >      Starts debugging to {dump_directory}/apd_dump_{process_id}, if 
  49928.      dump_directory is not set, then the apd.dumpdir setting from the 
  49929.      <TT
  49930. CLASS="filename"
  49931. >php.ini</TT
  49932. > file is used.
  49933.     </P
  49934. ><P
  49935. >      debug_level is an integer which is formed by adding together the following 
  49936.      values:
  49937.     </P
  49938. ><P
  49939. >      <TABLE
  49940. BORDER="0"
  49941. BGCOLOR="#E0E0E0"
  49942. CELLPADDING="5"
  49943. ><TR
  49944. ><TD
  49945. ><PRE
  49946. CLASS="script"
  49947. >FUNCTION_TRACE      1
  49948.         ARGS_TRACE          2
  49949.         ASSIGNMENT_TRACE    4
  49950.         STATEMENT_TRACE     8
  49951.         MEMORY_TRACE        16
  49952.         TIMING_TRACE        32
  49953.         SUMMARY_TRACE       64</PRE
  49954. ></TD
  49955. ></TR
  49956. ></TABLE
  49957. >
  49958.     </P
  49959. ><P
  49960. >      I would seriously not recommend using MEMORY_TRACE.  It is very slow and
  49961.      does not appear to be accurate (great, huh?)  also ASSIGNMENT_TRACE is not
  49962.      implemented. So, to turn on all functional traces (TIMING, FUNCTIONS, ARGS
  49963.      SUMMARY (like strace -c)) use the value 99
  49964.     </P
  49965. ><P
  49966. >      <TABLE
  49967. WIDTH="100%"
  49968. BORDER="0"
  49969. CELLPADDING="0"
  49970. CELLSPACING="0"
  49971. CLASS="EXAMPLE"
  49972. ><TR
  49973. ><TD
  49974. ><DIV
  49975. CLASS="example"
  49976. ><A
  49977. NAME="AEN8488"
  49978. ></A
  49979. ><P
  49980. ><B
  49981. >Example 1. <B
  49982. CLASS="function"
  49983. >apd_set_session_trace()</B
  49984. > example</B
  49985. ></P
  49986. ><TABLE
  49987. BORDER="0"
  49988. BGCOLOR="#E0E0E0"
  49989. CELLPADDING="5"
  49990. ><TR
  49991. ><TD
  49992. ><PRE
  49993. CLASS="php"
  49994. ><?php
  49995. apd_set_session_trace(99);
  49996. ?></PRE
  49997. ></TD
  49998. ></TR
  49999. ></TABLE
  50000. ></DIV
  50001. ></TD
  50002. ></TR
  50003. ></TABLE
  50004. >
  50005.     </P
  50006. ></DIV
  50007. ><H1
  50008. ><A
  50009. NAME="function.apd-set-session"
  50010. ></A
  50011. >apd_set_session</H1
  50012. ><DIV
  50013. CLASS="refnamediv"
  50014. ><A
  50015. NAME="AEN8493"
  50016. ></A
  50017. ><P
  50018. >    (no version information, might be only in CVS)</P
  50019. >apd_set_session -- Changes or sets the current debugging level</DIV
  50020. ><DIV
  50021. CLASS="refsect1"
  50022. ><A
  50023. NAME="AEN8496"
  50024. ></A
  50025. ><H2
  50026. >Description</H2
  50027. >void <B
  50028. CLASS="methodname"
  50029. >apd_set_session</B
  50030. > ( int debug_level)<BR
  50031. ></BR
  50032. ><P
  50033. >      This can be used to increase or decrease debugging in a different area of 
  50034.      your application,.debug_level is an integer which is formed by adding 
  50035.      together the following values:
  50036.     </P
  50037. ><P
  50038. >      <TABLE
  50039. BORDER="0"
  50040. BGCOLOR="#E0E0E0"
  50041. CELLPADDING="5"
  50042. ><TR
  50043. ><TD
  50044. ><PRE
  50045. CLASS="script"
  50046. >FUNCTION_TRACE      1
  50047.         ARGS_TRACE          2
  50048.         ASSIGNMENT_TRACE    4
  50049.         STATEMENT_TRACE     8
  50050.         MEMORY_TRACE        16
  50051.         TIMING_TRACE        32
  50052.         SUMMARY_TRACE       64</PRE
  50053. ></TD
  50054. ></TR
  50055. ></TABLE
  50056. >
  50057.     </P
  50058. ><P
  50059. >      <TABLE
  50060. WIDTH="100%"
  50061. BORDER="0"
  50062. CELLPADDING="0"
  50063. CELLSPACING="0"
  50064. CLASS="EXAMPLE"
  50065. ><TR
  50066. ><TD
  50067. ><DIV
  50068. CLASS="example"
  50069. ><A
  50070. NAME="AEN8508"
  50071. ></A
  50072. ><P
  50073. ><B
  50074. >Example 1. <B
  50075. CLASS="function"
  50076. >apd_set_session()</B
  50077. > example</B
  50078. ></P
  50079. ><TABLE
  50080. BORDER="0"
  50081. BGCOLOR="#E0E0E0"
  50082. CELLPADDING="5"
  50083. ><TR
  50084. ><TD
  50085. ><PRE
  50086. CLASS="php"
  50087. ><?php
  50088. apd_set_session(9);
  50089. ?></PRE
  50090. ></TD
  50091. ></TR
  50092. ></TABLE
  50093. ></DIV
  50094. ></TD
  50095. ></TR
  50096. ></TABLE
  50097. >
  50098.     </P
  50099. ></DIV
  50100. ><H1
  50101. ><A
  50102. NAME="function.apd-set-socket-session-trace"
  50103. ></A
  50104. >apd_set_socket_session_trace</H1
  50105. ><DIV
  50106. CLASS="refnamediv"
  50107. ><A
  50108. NAME="AEN8513"
  50109. ></A
  50110. ><P
  50111. >    (no version information, might be only in CVS)</P
  50112. >apd_set_socket_session_trace -- Starts the remote session debugging</DIV
  50113. ><DIV
  50114. CLASS="refsect1"
  50115. ><A
  50116. NAME="AEN8516"
  50117. ></A
  50118. ><H2
  50119. >Description</H2
  50120. >bool <B
  50121. CLASS="methodname"
  50122. >apd_set_socket_session_trace</B
  50123. > ( string ip_address_or_unix_socket_file, int socket_type, int port, int debug_level)<BR
  50124. ></BR
  50125. ><P
  50126. >      Connects to the tcp server (eg. tcplisten) specified IP or Unix Domain 
  50127.      socket (like a file), and sends debugging data to the socket. You can 
  50128.      use any port, but higher numbers are better as most of the lower numbers 
  50129.      may be used by other system services.
  50130.     </P
  50131. ><P
  50132. >      the socket_type can be APD_AF_UNIX (for file based sockets) or APD_AF_INET 
  50133.      (for standard tcp/ip)
  50134.     </P
  50135. ><P
  50136. >      debug_level is an integer which is formed by adding together the following 
  50137.      values:
  50138.     </P
  50139. ><P
  50140. >      <TABLE
  50141. BORDER="0"
  50142. BGCOLOR="#E0E0E0"
  50143. CELLPADDING="5"
  50144. ><TR
  50145. ><TD
  50146. ><PRE
  50147. CLASS="script"
  50148. >FUNCTION_TRACE      1
  50149.         ARGS_TRACE          2
  50150.         ASSIGNMENT_TRACE    4
  50151.         STATEMENT_TRACE     8
  50152.         MEMORY_TRACE        16
  50153.         TIMING_TRACE        32
  50154.         SUMMARY_TRACE       64</PRE
  50155. ></TD
  50156. ></TR
  50157. ></TABLE
  50158. >
  50159.     </P
  50160. ><P
  50161. >      I would seriously not recommend setting the value to 'zero' to start with, 
  50162.      and use the breakpoint methods to start debugging at a specific place in 
  50163.      the file.
  50164.     </P
  50165. ><P
  50166. >      <TABLE
  50167. WIDTH="100%"
  50168. BORDER="0"
  50169. CELLPADDING="0"
  50170. CELLSPACING="0"
  50171. CLASS="EXAMPLE"
  50172. ><TR
  50173. ><TD
  50174. ><DIV
  50175. CLASS="example"
  50176. ><A
  50177. NAME="AEN8540"
  50178. ></A
  50179. ><P
  50180. ><B
  50181. >Example 1. <B
  50182. CLASS="function"
  50183. >apd_set_socket_session_trace()</B
  50184. > example</B
  50185. ></P
  50186. ><TABLE
  50187. BORDER="0"
  50188. BGCOLOR="#E0E0E0"
  50189. CELLPADDING="5"
  50190. ><TR
  50191. ><TD
  50192. ><PRE
  50193. CLASS="php"
  50194. ><?php
  50195.   apd_set_socket_session_trace("127.0.0.1",APD_AF_INET,7112,0);
  50196. ?></PRE
  50197. ></TD
  50198. ></TR
  50199. ></TABLE
  50200. ></DIV
  50201. ></TD
  50202. ></TR
  50203. ></TABLE
  50204. >
  50205.     </P
  50206. ></DIV
  50207. ><H1
  50208. ><A
  50209. NAME="function.override-function"
  50210. ></A
  50211. >override_function</H1
  50212. ><DIV
  50213. CLASS="refnamediv"
  50214. ><A
  50215. NAME="AEN8545"
  50216. ></A
  50217. ><P
  50218. >    (no version information, might be only in CVS)</P
  50219. >override_function -- Overrides built-in functions</DIV
  50220. ><DIV
  50221. CLASS="refsect1"
  50222. ><A
  50223. NAME="AEN8548"
  50224. ></A
  50225. ><H2
  50226. >Description</H2
  50227. >bool <B
  50228. CLASS="methodname"
  50229. >override_function</B
  50230. > ( string function_name, string function_args, string function_code)<BR
  50231. ></BR
  50232. ><P
  50233. >      Syntax similar to create_function(). Overrides built-in functions
  50234.      (replaces them in the symbol table).
  50235.     </P
  50236. ><P
  50237. >      <TABLE
  50238. WIDTH="100%"
  50239. BORDER="0"
  50240. CELLPADDING="0"
  50241. CELLSPACING="0"
  50242. CLASS="EXAMPLE"
  50243. ><TR
  50244. ><TD
  50245. ><DIV
  50246. CLASS="example"
  50247. ><A
  50248. NAME="AEN8564"
  50249. ></A
  50250. ><P
  50251. ><B
  50252. >Example 1. <B
  50253. CLASS="function"
  50254. >override_function()</B
  50255. > example</B
  50256. ></P
  50257. ><TABLE
  50258. BORDER="0"
  50259. BGCOLOR="#E0E0E0"
  50260. CELLPADDING="5"
  50261. ><TR
  50262. ><TD
  50263. ><PRE
  50264. CLASS="php"
  50265. ><?php
  50266. override_function('test', '$a,$b', 'echo "DOING TEST"; return $a * $b;');
  50267. ?></PRE
  50268. ></TD
  50269. ></TR
  50270. ></TABLE
  50271. ></DIV
  50272. ></TD
  50273. ></TR
  50274. ></TABLE
  50275. >
  50276.     </P
  50277. ></DIV
  50278. ><H1
  50279. ><A
  50280. NAME="function.rename-function"
  50281. ></A
  50282. >rename_function</H1
  50283. ><DIV
  50284. CLASS="refnamediv"
  50285. ><A
  50286. NAME="AEN8569"
  50287. ></A
  50288. ><P
  50289. >    (no version information, might be only in CVS)</P
  50290. >rename_function -- Renames orig_name to new_name in the global function_table</DIV
  50291. ><DIV
  50292. CLASS="refsect1"
  50293. ><A
  50294. NAME="AEN8572"
  50295. ></A
  50296. ><H2
  50297. >Description</H2
  50298. >bool <B
  50299. CLASS="methodname"
  50300. >rename_function</B
  50301. > ( string original_name, string new_name)<BR
  50302. ></BR
  50303. ><P
  50304. >      Renames orig_name to new_name in the global function_table.  Useful
  50305.      for temporarily overriding builtin functions.
  50306.     </P
  50307. ><P
  50308. >      <TABLE
  50309. WIDTH="100%"
  50310. BORDER="0"
  50311. CELLPADDING="0"
  50312. CELLSPACING="0"
  50313. CLASS="EXAMPLE"
  50314. ><TR
  50315. ><TD
  50316. ><DIV
  50317. CLASS="example"
  50318. ><A
  50319. NAME="AEN8585"
  50320. ></A
  50321. ><P
  50322. ><B
  50323. >Example 1. <B
  50324. CLASS="function"
  50325. >rename_function()</B
  50326. > example</B
  50327. ></P
  50328. ><TABLE
  50329. BORDER="0"
  50330. BGCOLOR="#E0E0E0"
  50331. CELLPADDING="5"
  50332. ><TR
  50333. ><TD
  50334. ><PRE
  50335. CLASS="php"
  50336. ><?php
  50337. rename_function('mysql_connect', 'debug_mysql_connect' );
  50338. ?></PRE
  50339. ></TD
  50340. ></TR
  50341. ></TABLE
  50342. ></DIV
  50343. ></TD
  50344. ></TR
  50345. ></TABLE
  50346. >
  50347.     </P
  50348. ></DIV
  50349. ></DIV
  50350. ><DIV
  50351. CLASS="reference"
  50352. ><A
  50353. NAME="ref.array"
  50354. ></A
  50355. ><DIV
  50356. CLASS="TITLEPAGE"
  50357. ><H1
  50358. CLASS="title"
  50359. >III. Array Functions</H1
  50360. ><DIV
  50361. CLASS="PARTINTRO"
  50362. ><A
  50363. NAME="AEN8592"
  50364. ></A
  50365. ><DIV
  50366. CLASS="section"
  50367. ><H2
  50368. CLASS="section"
  50369. ><A
  50370. NAME="array.intro"
  50371. >Introduction</A
  50372. ></H2
  50373. ><P
  50374. >      These functions allow you to interact with and manipulate
  50375.      arrays in various ways. Arrays are essential for storing,
  50376.      managing, and operating on sets of variables.
  50377.     </P
  50378. ><P
  50379. >      Simple and multi-dimensional arrays are supported, and may be
  50380.      either user created or created by another function.
  50381.      There are specific database handling functions for populating
  50382.      arrays from database queries, and several functions return arrays.
  50383.     </P
  50384. ><P
  50385. >      Please see the <A
  50386. HREF="#language.types.array"
  50387. >Arrays</A
  50388. >
  50389.      section of the manual for a detailed explanation of how arrays are
  50390.      implemented and used in PHP.
  50391.      See also <A
  50392. HREF="#language.operators.array"
  50393. >Array operators</A
  50394. >
  50395.      for other ways how to manipulate the arrays.
  50396.     </P
  50397. ></DIV
  50398. ><DIV
  50399. CLASS="section"
  50400. ><HR><H2
  50401. CLASS="section"
  50402. ><A
  50403. NAME="array.requirements"
  50404. >Requirements</A
  50405. ></H2
  50406. ><P
  50407. >No external libraries are needed to build this extension.</P
  50408. ></DIV
  50409. ><DIV
  50410. CLASS="section"
  50411. ><HR><H2
  50412. CLASS="section"
  50413. ><A
  50414. NAME="array.installation"
  50415. >Installation</A
  50416. ></H2
  50417. ><P
  50418. >There is no installation needed to use these
  50419. functions; they are part of the PHP core.</P
  50420. ></DIV
  50421. ><DIV
  50422. CLASS="section"
  50423. ><HR><H2
  50424. CLASS="section"
  50425. ><A
  50426. NAME="array.configuration"
  50427. >Runtime Configuration</A
  50428. ></H2
  50429. ><P
  50430. >This extension has no configuration directives defined in <TT
  50431. CLASS="filename"
  50432. >php.ini</TT
  50433. >.</P
  50434. ></DIV
  50435. ><DIV
  50436. CLASS="section"
  50437. ><HR><H2
  50438. CLASS="section"
  50439. ><A
  50440. NAME="array.resources"
  50441. >Resource Types</A
  50442. ></H2
  50443. ><P
  50444. >This extension has no resource types defined.</P
  50445. ></DIV
  50446. ><DIV
  50447. CLASS="section"
  50448. ><HR><H2
  50449. CLASS="section"
  50450. ><A
  50451. NAME="array.constants"
  50452. >Predefined Constants</A
  50453. ></H2
  50454. ><P
  50455. > The constants below are always available as part of the PHP core.
  50456. </P
  50457. ><P
  50458. >  <P
  50459. ></P
  50460. ><DIV
  50461. CLASS="variablelist"
  50462. ><DL
  50463. ><DT
  50464. ><TT
  50465. CLASS="constant"
  50466. ><B
  50467. >CASE_LOWER</B
  50468. ></TT
  50469. >
  50470.     (<A
  50471. HREF="#language.types.integer"
  50472. ><B
  50473. CLASS="type"
  50474. >integer</B
  50475. ></A
  50476. >)</DT
  50477. ><DD
  50478. ><P
  50479. >      <TT
  50480. CLASS="constant"
  50481. ><B
  50482. >CASE_LOWER</B
  50483. ></TT
  50484. > is used with
  50485.      <A
  50486. HREF="#function.array-change-key-case"
  50487. ><B
  50488. CLASS="function"
  50489. >array_change_key_case()</B
  50490. ></A
  50491. > and is used to convert array
  50492.      keys to lower case. This is also the default case for
  50493.      <A
  50494. HREF="#function.array-change-key-case"
  50495. ><B
  50496. CLASS="function"
  50497. >array_change_key_case()</B
  50498. ></A
  50499. >.
  50500.     </P
  50501. ></DD
  50502. ><DT
  50503. ><TT
  50504. CLASS="constant"
  50505. ><B
  50506. >CASE_UPPER</B
  50507. ></TT
  50508. >
  50509.     (<A
  50510. HREF="#language.types.integer"
  50511. ><B
  50512. CLASS="type"
  50513. >integer</B
  50514. ></A
  50515. >)</DT
  50516. ><DD
  50517. ><P
  50518. >      <TT
  50519. CLASS="constant"
  50520. ><B
  50521. >CASE_UPPER</B
  50522. ></TT
  50523. > is used with
  50524.      <A
  50525. HREF="#function.array-change-key-case"
  50526. ><B
  50527. CLASS="function"
  50528. >array_change_key_case()</B
  50529. ></A
  50530. > and is used to convert array
  50531.      keys to upper case.
  50532.     </P
  50533. ></DD
  50534. ></DL
  50535. ></DIV
  50536. >
  50537.  </P
  50538. ><P
  50539. > Sorting order flags:
  50540.  <P
  50541. ></P
  50542. ><DIV
  50543. CLASS="variablelist"
  50544. ><DL
  50545. ><DT
  50546. ><TT
  50547. CLASS="constant"
  50548. ><B
  50549. >SORT_ASC</B
  50550. ></TT
  50551.     (<A
  50552. HREF="#language.types.integer"
  50553. ><B
  50554. CLASS="type"
  50555. >integer</B
  50556. ></A
  50557. >)</DT
  50558. ><DD
  50559. ><P
  50560. >      <TT
  50561. CLASS="constant"
  50562. ><B
  50563. >SORT_ASC</B
  50564. ></TT
  50565. > is used with
  50566.      <A
  50567. HREF="#function.array-multisort"
  50568. ><B
  50569. CLASS="function"
  50570. >array_multisort()</B
  50571. ></A
  50572. > to sort in ascending order.
  50573.     </P
  50574. ></DD
  50575. ><DT
  50576. ><TT
  50577. CLASS="constant"
  50578. ><B
  50579. >SORT_DESC</B
  50580. ></TT
  50581.     (<A
  50582. HREF="#language.types.integer"
  50583. ><B
  50584. CLASS="type"
  50585. >integer</B
  50586. ></A
  50587. >)</DT
  50588. ><DD
  50589. ><P
  50590. >      <TT
  50591. CLASS="constant"
  50592. ><B
  50593. >SORT_DESC</B
  50594. ></TT
  50595. > is used with
  50596.      <A
  50597. HREF="#function.array-multisort"
  50598. ><B
  50599. CLASS="function"
  50600. >array_multisort()</B
  50601. ></A
  50602. > to sort in descending order.
  50603.     </P
  50604. ></DD
  50605. ></DL
  50606. ></DIV
  50607. >
  50608.  </P
  50609. ><P
  50610. > Sorting type flags: used by various sort functions
  50611.  <P
  50612. ></P
  50613. ><DIV
  50614. CLASS="variablelist"
  50615. ><DL
  50616. ><DT
  50617. ><TT
  50618. CLASS="constant"
  50619. ><B
  50620. >SORT_REGULAR</B
  50621. ></TT
  50622.     (<A
  50623. HREF="#language.types.integer"
  50624. ><B
  50625. CLASS="type"
  50626. >integer</B
  50627. ></A
  50628. >)</DT
  50629. ><DD
  50630. ><P
  50631. >      <TT
  50632. CLASS="constant"
  50633. ><B
  50634. >SORT_REGULAR</B
  50635. ></TT
  50636. > is used to compare items normally.
  50637.     </P
  50638. ></DD
  50639. ><DT
  50640. ><TT
  50641. CLASS="constant"
  50642. ><B
  50643. >SORT_NUMERIC</B
  50644. ></TT
  50645.     (<A
  50646. HREF="#language.types.integer"
  50647. ><B
  50648. CLASS="type"
  50649. >integer</B
  50650. ></A
  50651. >)</DT
  50652. ><DD
  50653. ><P
  50654. >      <TT
  50655. CLASS="constant"
  50656. ><B
  50657. >SORT_NUMERIC</B
  50658. ></TT
  50659. > is used to compare items numerically.
  50660.     </P
  50661. ></DD
  50662. ><DT
  50663. ><TT
  50664. CLASS="constant"
  50665. ><B
  50666. >SORT_STRING</B
  50667. ></TT
  50668.     (<A
  50669. HREF="#language.types.integer"
  50670. ><B
  50671. CLASS="type"
  50672. >integer</B
  50673. ></A
  50674. >)</DT
  50675. ><DD
  50676. ><P
  50677. >     <TT
  50678. CLASS="constant"
  50679. ><B
  50680. >SORT_STRING</B
  50681. ></TT
  50682. > is used to compare items as strings.
  50683.     </P
  50684. ></DD
  50685. ></DL
  50686. ></DIV
  50687. >
  50688.  </P
  50689. ><P
  50690. ></P
  50691. ><DIV
  50692. CLASS="variablelist"
  50693. ><DL
  50694. ><DT
  50695. ><TT
  50696. CLASS="constant"
  50697. ><B
  50698. >COUNT_NORMAL</B
  50699. ></TT
  50700. >
  50701.      (<A
  50702. HREF="#language.types.integer"
  50703. ><B
  50704. CLASS="type"
  50705. >integer</B
  50706. ></A
  50707. >)</DT
  50708. ><DD
  50709. ><P
  50710. >
  50711.     </P
  50712. ></DD
  50713. ><DT
  50714. ><TT
  50715. CLASS="constant"
  50716. ><B
  50717. >COUNT_RECURSIVE</B
  50718. ></TT
  50719. >
  50720.      (<A
  50721. HREF="#language.types.integer"
  50722. ><B
  50723. CLASS="type"
  50724. >integer</B
  50725. ></A
  50726. >)</DT
  50727. ><DD
  50728. ><P
  50729. >
  50730.     </P
  50731. ></DD
  50732. ><DT
  50733. ><TT
  50734. CLASS="constant"
  50735. ><B
  50736. >EXTR_OVERWRITE</B
  50737. ></TT
  50738. >
  50739.      (<A
  50740. HREF="#language.types.integer"
  50741. ><B
  50742. CLASS="type"
  50743. >integer</B
  50744. ></A
  50745. >)</DT
  50746. ><DD
  50747. ><P
  50748. >
  50749.     </P
  50750. ></DD
  50751. ><DT
  50752. ><TT
  50753. CLASS="constant"
  50754. ><B
  50755. >EXTR_SKIP</B
  50756. ></TT
  50757. >
  50758.      (<A
  50759. HREF="#language.types.integer"
  50760. ><B
  50761. CLASS="type"
  50762. >integer</B
  50763. ></A
  50764. >)</DT
  50765. ><DD
  50766. ><P
  50767. >
  50768.     </P
  50769. ></DD
  50770. ><DT
  50771. ><TT
  50772. CLASS="constant"
  50773. ><B
  50774. >EXTR_PREFIX_SAME</B
  50775. ></TT
  50776. >
  50777.      (<A
  50778. HREF="#language.types.integer"
  50779. ><B
  50780. CLASS="type"
  50781. >integer</B
  50782. ></A
  50783. >)</DT
  50784. ><DD
  50785. ><P
  50786. >
  50787.     </P
  50788. ></DD
  50789. ><DT
  50790. ><TT
  50791. CLASS="constant"
  50792. ><B
  50793. >EXTR_PREFIX_ALL</B
  50794. ></TT
  50795. >
  50796.      (<A
  50797. HREF="#language.types.integer"
  50798. ><B
  50799. CLASS="type"
  50800. >integer</B
  50801. ></A
  50802. >)</DT
  50803. ><DD
  50804. ><P
  50805. >
  50806.     </P
  50807. ></DD
  50808. ><DT
  50809. ><TT
  50810. CLASS="constant"
  50811. ><B
  50812. >EXTR_PREFIX_INVALID</B
  50813. ></TT
  50814. >
  50815.      (<A
  50816. HREF="#language.types.integer"
  50817. ><B
  50818. CLASS="type"
  50819. >integer</B
  50820. ></A
  50821. >)</DT
  50822. ><DD
  50823. ><P
  50824. >
  50825.     </P
  50826. ></DD
  50827. ><DT
  50828. ><TT
  50829. CLASS="constant"
  50830. ><B
  50831. >EXTR_PREFIX_IF_EXISTS</B
  50832. ></TT
  50833. >
  50834.      (<A
  50835. HREF="#language.types.integer"
  50836. ><B
  50837. CLASS="type"
  50838. >integer</B
  50839. ></A
  50840. >)</DT
  50841. ><DD
  50842. ><P
  50843. >
  50844.     </P
  50845. ></DD
  50846. ><DT
  50847. ><TT
  50848. CLASS="constant"
  50849. ><B
  50850. >EXTR_IF_EXISTS</B
  50851. ></TT
  50852. >
  50853.      (<A
  50854. HREF="#language.types.integer"
  50855. ><B
  50856. CLASS="type"
  50857. >integer</B
  50858. ></A
  50859. >)</DT
  50860. ><DD
  50861. ><P
  50862. >
  50863.     </P
  50864. ></DD
  50865. ><DT
  50866. ><TT
  50867. CLASS="constant"
  50868. ><B
  50869. >EXTR_REFS</B
  50870. ></TT
  50871. >
  50872.      (<A
  50873. HREF="#language.types.integer"
  50874. ><B
  50875. CLASS="type"
  50876. >integer</B
  50877. ></A
  50878. >)</DT
  50879. ><DD
  50880. ><P
  50881. >
  50882.     </P
  50883. ></DD
  50884. ></DL
  50885. ></DIV
  50886. ></DIV
  50887. ><DIV
  50888. CLASS="section"
  50889. ><HR><H2
  50890. CLASS="section"
  50891. ><A
  50892. NAME="array.seealso"
  50893. >See Also</A
  50894. ></H2
  50895. ><P
  50896. >      See also <A
  50897. HREF="#function.is-array"
  50898. ><B
  50899. CLASS="function"
  50900. >is_array()</B
  50901. ></A
  50902. >, <A
  50903. HREF="#function.explode"
  50904. ><B
  50905. CLASS="function"
  50906. >explode()</B
  50907. ></A
  50908. >,
  50909.      <A
  50910. HREF="#function.implode"
  50911. ><B
  50912. CLASS="function"
  50913. >implode()</B
  50914. ></A
  50915. >, <A
  50916. HREF="#function.split"
  50917. ><B
  50918. CLASS="function"
  50919. >split()</B
  50920. ></A
  50921. >,
  50922.      <A
  50923. HREF="#function.preg-split"
  50924. ><B
  50925. CLASS="function"
  50926. >preg_split()</B
  50927. ></A
  50928. >, and <A
  50929. HREF="#function.unset"
  50930. ><B
  50931. CLASS="function"
  50932. >unset()</B
  50933. ></A
  50934. >.
  50935.     </P
  50936. ></DIV
  50937. ></DIV
  50938. ><DIV
  50939. CLASS="TOC"
  50940. ><DL
  50941. ><DT
  50942. ><B
  50943. >Table of Contents</B
  50944. ></DT
  50945. ><DT
  50946. ><A
  50947. HREF="#function.array-change-key-case"
  50948. >array_change_key_case</A
  50949. > -- Returns an array with all string keys lowercased or uppercased</DT
  50950. ><DT
  50951. ><A
  50952. HREF="#function.array-chunk"
  50953. >array_chunk</A
  50954. > -- Split an array into chunks</DT
  50955. ><DT
  50956. ><A
  50957. HREF="#function.array-combine"
  50958. >array_combine</A
  50959. > -- 
  50960.      Creates an array by using one array for keys and another for its values
  50961.     </DT
  50962. ><DT
  50963. ><A
  50964. HREF="#function.array-count-values"
  50965. >array_count_values</A
  50966. > -- Counts all the values of an array</DT
  50967. ><DT
  50968. ><A
  50969. HREF="#function.array-diff-assoc"
  50970. >array_diff_assoc</A
  50971. > -- Computes the difference of arrays with additional index check</DT
  50972. ><DT
  50973. ><A
  50974. HREF="#function.array-diff-key"
  50975. >array_diff_key</A
  50976. > -- Computes the difference of arrays using keys for comparison</DT
  50977. ><DT
  50978. ><A
  50979. HREF="#function.array-diff-uassoc"
  50980. >array_diff_uassoc</A
  50981. > -- 
  50982.      Computes the difference of arrays with additional index check
  50983.      which is performed by a user supplied callback function
  50984.     </DT
  50985. ><DT
  50986. ><A
  50987. HREF="#function.array-diff-ukey"
  50988. >array_diff_ukey</A
  50989. > -- Computes the difference of arrays using a callback function on the keys for comparison</DT
  50990. ><DT
  50991. ><A
  50992. HREF="#function.array-diff"
  50993. >array_diff</A
  50994. > -- Computes the difference of arrays</DT
  50995. ><DT
  50996. ><A
  50997. HREF="#function.array-fill"
  50998. >array_fill</A
  50999. > -- Fill an array with values</DT
  51000. ><DT
  51001. ><A
  51002. HREF="#function.array-filter"
  51003. >array_filter</A
  51004. > -- 
  51005.      Filters elements of an array using a callback function
  51006.     </DT
  51007. ><DT
  51008. ><A
  51009. HREF="#function.array-flip"
  51010. >array_flip</A
  51011. > -- Exchanges all keys with their associated values in an array</DT
  51012. ><DT
  51013. ><A
  51014. HREF="#function.array-intersect-assoc"
  51015. >array_intersect_assoc</A
  51016. > -- Computes the intersection of arrays with additional index check</DT
  51017. ><DT
  51018. ><A
  51019. HREF="#function.array-intersect-key"
  51020. >array_intersect_key</A
  51021. > -- Computes the intersection of arrays using keys for comparison</DT
  51022. ><DT
  51023. ><A
  51024. HREF="#function.array-intersect-uassoc"
  51025. >array_intersect_uassoc</A
  51026. > -- Computes the intersection of arrays with additional index check, compares indexes by a callback function</DT
  51027. ><DT
  51028. ><A
  51029. HREF="#function.array-intersect-ukey"
  51030. >array_intersect_ukey</A
  51031. > -- Computes the intersection of arrays using a callback function on the keys for comparison</DT
  51032. ><DT
  51033. ><A
  51034. HREF="#function.array-intersect"
  51035. >array_intersect</A
  51036. > -- Computes the intersection of arrays</DT
  51037. ><DT
  51038. ><A
  51039. HREF="#function.array-key-exists"
  51040. >array_key_exists</A
  51041. > -- Checks if the given key or index exists in the array</DT
  51042. ><DT
  51043. ><A
  51044. HREF="#function.array-keys"
  51045. >array_keys</A
  51046. > -- Return all the keys of an array</DT
  51047. ><DT
  51048. ><A
  51049. HREF="#function.array-map"
  51050. >array_map</A
  51051. > -- 
  51052.      Applies the callback to the elements of the given arrays
  51053.     </DT
  51054. ><DT
  51055. ><A
  51056. HREF="#function.array-merge-recursive"
  51057. >array_merge_recursive</A
  51058. > -- Merge two or more arrays recursively</DT
  51059. ><DT
  51060. ><A
  51061. HREF="#function.array-merge"
  51062. >array_merge</A
  51063. > -- Merge one or more arrays</DT
  51064. ><DT
  51065. ><A
  51066. HREF="#function.array-multisort"
  51067. >array_multisort</A
  51068. > -- Sort multiple or multi-dimensional arrays</DT
  51069. ><DT
  51070. ><A
  51071. HREF="#function.array-pad"
  51072. >array_pad</A
  51073. > -- 
  51074.      Pad array to the specified length with a value
  51075.     </DT
  51076. ><DT
  51077. ><A
  51078. HREF="#function.array-pop"
  51079. >array_pop</A
  51080. > -- Pop the element off the end of array</DT
  51081. ><DT
  51082. ><A
  51083. HREF="#function.array-push"
  51084. >array_push</A
  51085. > -- 
  51086.      Push one or more elements onto the end of array
  51087.     </DT
  51088. ><DT
  51089. ><A
  51090. HREF="#function.array-rand"
  51091. >array_rand</A
  51092. > -- 
  51093.      Pick one or more random entries out of an array
  51094.     </DT
  51095. ><DT
  51096. ><A
  51097. HREF="#function.array-reduce"
  51098. >array_reduce</A
  51099. > -- 
  51100.      Iteratively reduce the array to a single value using a callback
  51101.      function
  51102.     </DT
  51103. ><DT
  51104. ><A
  51105. HREF="#function.array-reverse"
  51106. >array_reverse</A
  51107. > -- 
  51108.      Return an array with elements in reverse order
  51109.     </DT
  51110. ><DT
  51111. ><A
  51112. HREF="#function.array-search"
  51113. >array_search</A
  51114. > -- 
  51115.      Searches the array for a given value and returns the
  51116.      corresponding key if successful
  51117.     </DT
  51118. ><DT
  51119. ><A
  51120. HREF="#function.array-shift"
  51121. >array_shift</A
  51122. > -- 
  51123.      Shift an element off the beginning of array
  51124.     </DT
  51125. ><DT
  51126. ><A
  51127. HREF="#function.array-slice"
  51128. >array_slice</A
  51129. > -- Extract a slice of the array</DT
  51130. ><DT
  51131. ><A
  51132. HREF="#function.array-splice"
  51133. >array_splice</A
  51134. > -- 
  51135.      Remove a portion of the array and replace it with something
  51136.      else
  51137.     </DT
  51138. ><DT
  51139. ><A
  51140. HREF="#function.array-sum"
  51141. >array_sum</A
  51142. > -- 
  51143.      Calculate the sum of values in an array
  51144.     </DT
  51145. ><DT
  51146. ><A
  51147. HREF="#function.array-udiff-assoc"
  51148. >array_udiff_assoc</A
  51149. > -- Computes the difference of arrays with additional index check, compares data by a callback function</DT
  51150. ><DT
  51151. ><A
  51152. HREF="#function.array-udiff-uassoc"
  51153. >array_udiff_uassoc</A
  51154. > -- Computes the difference of arrays with additional index check, compares data and indexes by a callback function</DT
  51155. ><DT
  51156. ><A
  51157. HREF="#function.array-udiff"
  51158. >array_udiff</A
  51159. > -- Computes the difference of arrays by using a callback function for data comparison</DT
  51160. ><DT
  51161. ><A
  51162. HREF="#function.array-uintersect-assoc"
  51163. >array_uintersect_assoc</A
  51164. > -- Computes the intersection of arrays with additional index check, compares data by a callback function</DT
  51165. ><DT
  51166. ><A
  51167. HREF="#function.array-uintersect-uassoc"
  51168. >array_uintersect_uassoc</A
  51169. > -- Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions</DT
  51170. ><DT
  51171. ><A
  51172. HREF="#function.array-uintersect"
  51173. >array_uintersect</A
  51174. > -- Computes the intersection of arrays, compares data by a callback function</DT
  51175. ><DT
  51176. ><A
  51177. HREF="#function.array-unique"
  51178. >array_unique</A
  51179. > -- Removes duplicate values from an array</DT
  51180. ><DT
  51181. ><A
  51182. HREF="#function.array-unshift"
  51183. >array_unshift</A
  51184. > -- 
  51185.      Prepend one or more elements to the beginning of an array
  51186.     </DT
  51187. ><DT
  51188. ><A
  51189. HREF="#function.array-values"
  51190. >array_values</A
  51191. > -- Return all the values of an array</DT
  51192. ><DT
  51193. ><A
  51194. HREF="#function.array-walk-recursive"
  51195. >array_walk_recursive</A
  51196. > -- 
  51197.    Apply a user function recursively to every member of an array
  51198.   </DT
  51199. ><DT
  51200. ><A
  51201. HREF="#function.array-walk"
  51202. >array_walk</A
  51203. > -- 
  51204.      Apply a user function to every member of an array
  51205.     </DT
  51206. ><DT
  51207. ><A
  51208. HREF="#function.array"
  51209. >array</A
  51210. > -- 
  51211.      Create an array
  51212.     </DT
  51213. ><DT
  51214. ><A
  51215. HREF="#function.arsort"
  51216. >arsort</A
  51217. > -- 
  51218.      Sort an array in reverse order and maintain index association
  51219.     </DT
  51220. ><DT
  51221. ><A
  51222. HREF="#function.asort"
  51223. >asort</A
  51224. > -- Sort an array and maintain index association</DT
  51225. ><DT
  51226. ><A
  51227. HREF="#function.compact"
  51228. >compact</A
  51229. > -- 
  51230.      Create array containing variables and their values
  51231.     </DT
  51232. ><DT
  51233. ><A
  51234. HREF="#function.count"
  51235. >count</A
  51236. > -- Count elements in an array, or properties in an object</DT
  51237. ><DT
  51238. ><A
  51239. HREF="#function.current"
  51240. >current</A
  51241. > -- Return the current element in an array</DT
  51242. ><DT
  51243. ><A
  51244. HREF="#function.each"
  51245. >each</A
  51246. > -- 
  51247.      Return the current key and value pair from an array and advance
  51248.      the array cursor
  51249.     </DT
  51250. ><DT
  51251. ><A
  51252. HREF="#function.end"
  51253. >end</A
  51254. > -- 
  51255.      Set the internal pointer of an array to its last element
  51256.     </DT
  51257. ><DT
  51258. ><A
  51259. HREF="#function.extract"
  51260. >extract</A
  51261. > -- 
  51262.      Import variables into the current symbol table from an array
  51263.     </DT
  51264. ><DT
  51265. ><A
  51266. HREF="#function.in-array"
  51267. >in_array</A
  51268. > -- Checks if a value exists in an array</DT
  51269. ><DT
  51270. ><A
  51271. HREF="#function.key"
  51272. >key</A
  51273. > -- Fetch a key from an associative array</DT
  51274. ><DT
  51275. ><A
  51276. HREF="#function.krsort"
  51277. >krsort</A
  51278. > -- Sort an array by key in reverse order</DT
  51279. ><DT
  51280. ><A
  51281. HREF="#function.ksort"
  51282. >ksort</A
  51283. > -- Sort an array by key</DT
  51284. ><DT
  51285. ><A
  51286. HREF="#function.list"
  51287. >list</A
  51288. > -- 
  51289.      Assign variables as if they were an array
  51290.     </DT
  51291. ><DT
  51292. ><A
  51293. HREF="#function.natcasesort"
  51294. >natcasesort</A
  51295. > -- 
  51296.      Sort an array using a case insensitive "natural order" algorithm
  51297.     </DT
  51298. ><DT
  51299. ><A
  51300. HREF="#function.natsort"
  51301. >natsort</A
  51302. > -- 
  51303.      Sort an array using a "natural order" algorithm
  51304.     </DT
  51305. ><DT
  51306. ><A
  51307. HREF="#function.next"
  51308. >next</A
  51309. > -- 
  51310.      Advance the internal array pointer of an array
  51311.     </DT
  51312. ><DT
  51313. ><A
  51314. HREF="#function.pos"
  51315. >pos</A
  51316. > -- Alias of <A
  51317. HREF="#function.current"
  51318. ><B
  51319. CLASS="function"
  51320. >current()</B
  51321. ></A
  51322. ></DT
  51323. ><DT
  51324. ><A
  51325. HREF="#function.prev"
  51326. >prev</A
  51327. > -- Rewind the internal array pointer</DT
  51328. ><DT
  51329. ><A
  51330. HREF="#function.range"
  51331. >range</A
  51332. > -- 
  51333.      Create an array containing a range of elements
  51334.     </DT
  51335. ><DT
  51336. ><A
  51337. HREF="#function.reset"
  51338. >reset</A
  51339. > -- 
  51340.      Set the internal pointer of an array to its first element
  51341.     </DT
  51342. ><DT
  51343. ><A
  51344. HREF="#function.rsort"
  51345. >rsort</A
  51346. > -- Sort an array in reverse order</DT
  51347. ><DT
  51348. ><A
  51349. HREF="#function.shuffle"
  51350. >shuffle</A
  51351. > -- Shuffle an array</DT
  51352. ><DT
  51353. ><A
  51354. HREF="#function.sizeof"
  51355. >sizeof</A
  51356. > -- Alias of <A
  51357. HREF="#function.count"
  51358. ><B
  51359. CLASS="function"
  51360. >count()</B
  51361. ></A
  51362. ></DT
  51363. ><DT
  51364. ><A
  51365. HREF="#function.sort"
  51366. >sort</A
  51367. > -- Sort an array</DT
  51368. ><DT
  51369. ><A
  51370. HREF="#function.uasort"
  51371. >uasort</A
  51372. > -- 
  51373.      Sort an array with a user-defined comparison function and
  51374.      maintain index association
  51375.     </DT
  51376. ><DT
  51377. ><A
  51378. HREF="#function.uksort"
  51379. >uksort</A
  51380. > -- 
  51381.      Sort an array by keys using a user-defined comparison function
  51382.     </DT
  51383. ><DT
  51384. ><A
  51385. HREF="#function.usort"
  51386. >usort</A
  51387. > -- 
  51388.      Sort an array by values using a user-defined comparison function
  51389.     </DT
  51390. ></DL
  51391. ></DIV
  51392. ></DIV
  51393. ><H1
  51394. ><A
  51395. NAME="function.array-change-key-case"
  51396. ></A
  51397. >array_change_key_case</H1
  51398. ><DIV
  51399. CLASS="refnamediv"
  51400. ><A
  51401. NAME="AEN8747"
  51402. ></A
  51403. ><P
  51404. >    (PHP 4 >= 4.2.0, PHP 5)</P
  51405. >array_change_key_case -- Returns an array with all string keys lowercased or uppercased</DIV
  51406. ><DIV
  51407. CLASS="refsect1"
  51408. ><A
  51409. NAME="AEN8750"
  51410. ></A
  51411. ><H2
  51412. >Description</H2
  51413. >array <B
  51414. CLASS="methodname"
  51415. >array_change_key_case</B
  51416. > ( array input [, int case])<BR
  51417. ></BR
  51418. ><P
  51419. >      <B
  51420. CLASS="function"
  51421. >array_change_key_case()</B
  51422. > changes the
  51423.      keys in the <VAR
  51424. CLASS="parameter"
  51425. >input</VAR
  51426. > array to
  51427.      be all lowercase or uppercase. The change depends
  51428.      on the last optional <VAR
  51429. CLASS="parameter"
  51430. >case</VAR
  51431. >
  51432.      parameter. You can pass two constants there,
  51433.      <TT
  51434. CLASS="constant"
  51435. ><B
  51436. >CASE_UPPER</B
  51437. ></TT
  51438. > and
  51439.      <TT
  51440. CLASS="constant"
  51441. ><B
  51442. >CASE_LOWER</B
  51443. ></TT
  51444. >. The default is
  51445.      <TT
  51446. CLASS="constant"
  51447. ><B
  51448. >CASE_LOWER</B
  51449. ></TT
  51450. >. The function will leave
  51451.      number indices as is.
  51452.     </P
  51453. ><TABLE
  51454. WIDTH="100%"
  51455. BORDER="0"
  51456. CELLPADDING="0"
  51457. CELLSPACING="0"
  51458. CLASS="EXAMPLE"
  51459. ><TR
  51460. ><TD
  51461. ><DIV
  51462. CLASS="example"
  51463. ><A
  51464. NAME="AEN8768"
  51465. ></A
  51466. ><P
  51467. ><B
  51468. >Example 1. <B
  51469. CLASS="function"
  51470. >array_change_key_case()</B
  51471. > example</B
  51472. ></P
  51473. ><TABLE
  51474. BORDER="0"
  51475. BGCOLOR="#E0E0E0"
  51476. CELLPADDING="5"
  51477. ><TR
  51478. ><TD
  51479. ><PRE
  51480. CLASS="php"
  51481. ><?php
  51482. $input_array = array("FirSt" => 1, "SecOnd" => 4);
  51483. print_r(array_change_key_case($input_array, CASE_UPPER));
  51484. ?></PRE
  51485. ></TD
  51486. ></TR
  51487. ></TABLE
  51488. ><P
  51489. >The above example will output:</P
  51490. ><TABLE
  51491. BORDER="0"
  51492. BGCOLOR="#E0E0E0"
  51493. CELLPADDING="5"
  51494. ><TR
  51495. ><TD
  51496. ><PRE
  51497. CLASS="screen"
  51498. >Array
  51499. (
  51500.     [FIRST] => 1
  51501.     [SECOND] => 4
  51502. )</PRE
  51503. ></TD
  51504. ></TR
  51505. ></TABLE
  51506. ></DIV
  51507. ></TD
  51508. ></TR
  51509. ></TABLE
  51510. ><P
  51511. >      If an array has indices that will be the same once run through this
  51512.      function (e.g. "keY" and "kEY"), the value that is later in the array
  51513.      will override other indices.
  51514.     </P
  51515. ></DIV
  51516. ><H1
  51517. ><A
  51518. NAME="function.array-chunk"
  51519. ></A
  51520. >array_chunk</H1
  51521. ><DIV
  51522. CLASS="refnamediv"
  51523. ><A
  51524. NAME="AEN8776"
  51525. ></A
  51526. ><P
  51527. >    (PHP 4 >= 4.2.0, PHP 5)</P
  51528. >array_chunk -- Split an array into chunks</DIV
  51529. ><DIV
  51530. CLASS="refsect1"
  51531. ><A
  51532. NAME="AEN8779"
  51533. ></A
  51534. ><H2
  51535. >Description</H2
  51536. >array <B
  51537. CLASS="methodname"
  51538. >array_chunk</B
  51539. > ( array input, int size [, bool preserve_keys])<BR
  51540. ></BR
  51541. ><P
  51542. >      <B
  51543. CLASS="function"
  51544. >array_chunk()</B
  51545. > splits the array into
  51546.      several arrays with <VAR
  51547. CLASS="parameter"
  51548. >size</VAR
  51549. > values
  51550.      in them. You may also have an array with less values
  51551.      at the end. You get the arrays as members of a
  51552.      multidimensional array indexed with numbers starting
  51553.      from zero.
  51554.     </P
  51555. ><P
  51556. >      By setting the optional <VAR
  51557. CLASS="parameter"
  51558. >preserve_keys</VAR
  51559. >
  51560.      parameter to <TT
  51561. CLASS="constant"
  51562. ><B
  51563. >TRUE</B
  51564. ></TT
  51565. >, you can force PHP to preserve the original
  51566.      keys from the input array. If you specify <TT
  51567. CLASS="constant"
  51568. ><B
  51569. >FALSE</B
  51570. ></TT
  51571. > new number
  51572.      indices will be used in each resulting array with
  51573.      indices starting from zero. The default is <TT
  51574. CLASS="constant"
  51575. ><B
  51576. >FALSE</B
  51577. ></TT
  51578. >.
  51579.     </P
  51580. ><TABLE
  51581. WIDTH="100%"
  51582. BORDER="0"
  51583. CELLPADDING="0"
  51584. CELLSPACING="0"
  51585. CLASS="EXAMPLE"
  51586. ><TR
  51587. ><TD
  51588. ><DIV
  51589. CLASS="example"
  51590. ><A
  51591. NAME="AEN8801"
  51592. ></A
  51593. ><P
  51594. ><B
  51595. >Example 1. <B
  51596. CLASS="function"
  51597. >array_chunk()</B
  51598. > example</B
  51599. ></P
  51600. ><TABLE
  51601. BORDER="0"
  51602. BGCOLOR="#E0E0E0"
  51603. CELLPADDING="5"
  51604. ><TR
  51605. ><TD
  51606. ><PRE
  51607. CLASS="php"
  51608. ><?php
  51609. $input_array = array('a', 'b', 'c', 'd', 'e');
  51610. print_r(array_chunk($input_array, 2));
  51611. print_r(array_chunk($input_array, 2, true));
  51612. ?></PRE
  51613. ></TD
  51614. ></TR
  51615. ></TABLE
  51616. ><P
  51617. >The above example will output:</P
  51618. ><TABLE
  51619. BORDER="0"
  51620. BGCOLOR="#E0E0E0"
  51621. CELLPADDING="5"
  51622. ><TR
  51623. ><TD
  51624. ><PRE
  51625. CLASS="screen"
  51626. >Array
  51627. (
  51628.     [0] => Array
  51629.         (
  51630.             [0] => a
  51631.             [1] => b
  51632.         )
  51633.  
  51634.     [1] => Array
  51635.         (
  51636.             [0] => c
  51637.             [1] => d
  51638.         )
  51639.  
  51640.     [2] => Array
  51641.         (
  51642.             [0] => e
  51643.         )
  51644.  
  51645. )
  51646. Array
  51647. (
  51648.     [0] => Array
  51649.         (
  51650.             [0] => a
  51651.             [1] => b
  51652.         )
  51653.  
  51654.     [1] => Array
  51655.         (
  51656.             [2] => c
  51657.             [3] => d
  51658.         )
  51659.  
  51660.     [2] => Array
  51661.         (
  51662.             [4] => e
  51663.         )
  51664.  
  51665. )</PRE
  51666. ></TD
  51667. ></TR
  51668. ></TABLE
  51669. ></DIV
  51670. ></TD
  51671. ></TR
  51672. ></TABLE
  51673. ></DIV
  51674. ><H1
  51675. ><A
  51676. NAME="function.array-combine"
  51677. ></A
  51678. >array_combine</H1
  51679. ><DIV
  51680. CLASS="refnamediv"
  51681. ><A
  51682. NAME="AEN8808"
  51683. ></A
  51684. ><P
  51685. >    (PHP 5)</P
  51686. >array_combine -- 
  51687.      Creates an array by using one array for keys and another for its values
  51688.     </DIV
  51689. ><DIV
  51690. CLASS="refsect1"
  51691. ><A
  51692. NAME="AEN8811"
  51693. ></A
  51694. ><H2
  51695. >Description</H2
  51696. >array <B
  51697. CLASS="methodname"
  51698. >array_combine</B
  51699. > ( array keys, array values)<BR
  51700. ></BR
  51701. ><P
  51702. >      Returns an <A
  51703. HREF="#language.types.array"
  51704. ><B
  51705. CLASS="type"
  51706. >array</B
  51707. ></A
  51708. > by using the values from the
  51709.      <VAR
  51710. CLASS="parameter"
  51711. >keys</VAR
  51712. > array as keys and the values from the
  51713.      <VAR
  51714. CLASS="parameter"
  51715. >values</VAR
  51716. > array as the corresponding values.
  51717.     </P
  51718. ><P
  51719. >      Returns <TT
  51720. CLASS="constant"
  51721. ><B
  51722. >FALSE</B
  51723. ></TT
  51724. > if the number of elements for each array isn't equal or
  51725.      if the arrays are empty.
  51726.     </P
  51727. ><P
  51728. >      <TABLE
  51729. WIDTH="100%"
  51730. BORDER="0"
  51731. CELLPADDING="0"
  51732. CELLSPACING="0"
  51733. CLASS="EXAMPLE"
  51734. ><TR
  51735. ><TD
  51736. ><DIV
  51737. CLASS="example"
  51738. ><A
  51739. NAME="AEN8829"
  51740. ></A
  51741. ><P
  51742. ><B
  51743. >Example 1. A simple <B
  51744. CLASS="function"
  51745. >array_combine()</B
  51746. > example</B
  51747. ></P
  51748. ><TABLE
  51749. BORDER="0"
  51750. BGCOLOR="#E0E0E0"
  51751. CELLPADDING="5"
  51752. ><TR
  51753. ><TD
  51754. ><PRE
  51755. CLASS="php"
  51756. ><?php
  51757. $a = array('green', 'red', 'yellow');
  51758. $b = array('avocado', 'apple', 'banana');
  51759. $c = array_combine($a, $b);
  51760.  
  51761. print_r($c);
  51762. ?></PRE
  51763. ></TD
  51764. ></TR
  51765. ></TABLE
  51766. ><P
  51767. >The above example will output:</P
  51768. ><TABLE
  51769. BORDER="0"
  51770. BGCOLOR="#E0E0E0"
  51771. CELLPADDING="5"
  51772. ><TR
  51773. ><TD
  51774. ><PRE
  51775. CLASS="screen"
  51776. >Array
  51777. (
  51778.     [green]  => avocado
  51779.     [red]    => apple
  51780.     [yellow] => banana
  51781. )</PRE
  51782. ></TD
  51783. ></TR
  51784. ></TABLE
  51785. ></DIV
  51786. ></TD
  51787. ></TR
  51788. ></TABLE
  51789. >
  51790.     </P
  51791. ><P
  51792. >      See also <A
  51793. HREF="#function.array-merge"
  51794. ><B
  51795. CLASS="function"
  51796. >array_merge()</B
  51797. ></A
  51798. >,
  51799.      <A
  51800. HREF="#function.array-walk"
  51801. ><B
  51802. CLASS="function"
  51803. >array_walk()</B
  51804. ></A
  51805. >, and
  51806.      <A
  51807. HREF="#function.array-values"
  51808. ><B
  51809. CLASS="function"
  51810. >array_values()</B
  51811. ></A
  51812. >.
  51813.     </P
  51814. ></DIV
  51815. ><H1
  51816. ><A
  51817. NAME="function.array-count-values"
  51818. ></A
  51819. >array_count_values</H1
  51820. ><DIV
  51821. CLASS="refnamediv"
  51822. ><A
  51823. NAME="AEN8840"
  51824. ></A
  51825. ><P
  51826. >    (PHP 4 , PHP 5)</P
  51827. >array_count_values -- Counts all the values of an array</DIV
  51828. ><DIV
  51829. CLASS="refsect1"
  51830. ><A
  51831. NAME="AEN8843"
  51832. ></A
  51833. ><H2
  51834. >Description</H2
  51835. >array <B
  51836. CLASS="methodname"
  51837. >array_count_values</B
  51838. > ( array input)<BR
  51839. ></BR
  51840. ><P
  51841. >      <B
  51842. CLASS="function"
  51843. >array_count_values()</B
  51844. > returns an array using
  51845.      the values of the <VAR
  51846. CLASS="parameter"
  51847. >input</VAR
  51848. > array as keys and
  51849.      their frequency in <VAR
  51850. CLASS="parameter"
  51851. >input</VAR
  51852. > as values.
  51853.     </P
  51854. ><P
  51855. >      <TABLE
  51856. WIDTH="100%"
  51857. BORDER="0"
  51858. CELLPADDING="0"
  51859. CELLSPACING="0"
  51860. CLASS="EXAMPLE"
  51861. ><TR
  51862. ><TD
  51863. ><DIV
  51864. CLASS="example"
  51865. ><A
  51866. NAME="AEN8856"
  51867. ></A
  51868. ><P
  51869. ><B
  51870. >Example 1. <B
  51871. CLASS="function"
  51872. >array_count_values()</B
  51873. > example</B
  51874. ></P
  51875. ><TABLE
  51876. BORDER="0"
  51877. BGCOLOR="#E0E0E0"
  51878. CELLPADDING="5"
  51879. ><TR
  51880. ><TD
  51881. ><PRE
  51882. CLASS="php"
  51883. ><?php
  51884. $array = array(1, "hello", 1, "world", "hello");
  51885. print_r(array_count_values($array));
  51886. ?></PRE
  51887. ></TD
  51888. ></TR
  51889. ></TABLE
  51890. ><P
  51891. >The above example will output:</P
  51892. ><TABLE
  51893. BORDER="0"
  51894. BGCOLOR="#E0E0E0"
  51895. CELLPADDING="5"
  51896. ><TR
  51897. ><TD
  51898. ><PRE
  51899. CLASS="screen"
  51900. >Array
  51901. (
  51902.     [1] => 2
  51903.     [hello] => 2
  51904.     [world] => 1
  51905. )</PRE
  51906. ></TD
  51907. ></TR
  51908. ></TABLE
  51909. ></DIV
  51910. ></TD
  51911. ></TR
  51912. ></TABLE
  51913. >
  51914.     </P
  51915. ><P
  51916. >      See also <A
  51917. HREF="#function.count"
  51918. ><B
  51919. CLASS="function"
  51920. >count()</B
  51921. ></A
  51922. >,
  51923.      <A
  51924. HREF="#function.array-unique"
  51925. ><B
  51926. CLASS="function"
  51927. >array_unique()</B
  51928. ></A
  51929. >,
  51930.      <A
  51931. HREF="#function.array-values"
  51932. ><B
  51933. CLASS="function"
  51934. >array_values()</B
  51935. ></A
  51936. >, and
  51937.      <A
  51938. HREF="#function.count-chars"
  51939. ><B
  51940. CLASS="function"
  51941. >count_chars()</B
  51942. ></A
  51943. >.
  51944.     </P
  51945. ></DIV
  51946. ><H1
  51947. ><A
  51948. NAME="function.array-diff-assoc"
  51949. ></A
  51950. >array_diff_assoc</H1
  51951. ><DIV
  51952. CLASS="refnamediv"
  51953. ><A
  51954. NAME="AEN8868"
  51955. ></A
  51956. ><P
  51957. >    (PHP 4 >= 4.3.0, PHP 5)</P
  51958. >array_diff_assoc -- Computes the difference of arrays with additional index check</DIV
  51959. ><DIV
  51960. CLASS="refsect1"
  51961. ><A
  51962. NAME="AEN8871"
  51963. ></A
  51964. ><H2
  51965. >Description</H2
  51966. >array <B
  51967. CLASS="methodname"
  51968. >array_diff_assoc</B
  51969. > ( array array1, array array2 [, array ...])<BR
  51970. ></BR
  51971. ><P
  51972. >      <B
  51973. CLASS="function"
  51974. >array_diff_assoc()</B
  51975. > returns an <A
  51976. HREF="#language.types.array"
  51977. ><B
  51978. CLASS="type"
  51979. >array</B
  51980. ></A
  51981. >
  51982.      containing all the values from <VAR
  51983. CLASS="parameter"
  51984. >array1</VAR
  51985. >
  51986.      that are not present in any of the other arguments.
  51987.      Note that the keys are used in the comparison unlike
  51988.      <A
  51989. HREF="#function.array-diff"
  51990. ><B
  51991. CLASS="function"
  51992. >array_diff()</B
  51993. ></A
  51994. >.
  51995.     </P
  51996. ><P
  51997. >      <TABLE
  51998. WIDTH="100%"
  51999. BORDER="0"
  52000. CELLPADDING="0"
  52001. CELLSPACING="0"
  52002. CLASS="EXAMPLE"
  52003. ><TR
  52004. ><TD
  52005. ><DIV
  52006. CLASS="example"
  52007. ><A
  52008. NAME="AEN8891"
  52009. ></A
  52010. ><P
  52011. ><B
  52012. >Example 1. <B
  52013. CLASS="function"
  52014. >array_diff_assoc()</B
  52015. > example</B
  52016. ></P
  52017. ><TABLE
  52018. BORDER="0"
  52019. BGCOLOR="#E0E0E0"
  52020. CELLPADDING="5"
  52021. ><TR
  52022. ><TD
  52023. ><PRE
  52024. CLASS="php"
  52025. ><?php
  52026. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
  52027. $array2 = array("a" => "green", "yellow", "red");
  52028. $result = array_diff_assoc($array1, $array2);
  52029. print_r($result);
  52030. ?></PRE
  52031. ></TD
  52032. ></TR
  52033. ></TABLE
  52034. ><P
  52035. >The above example will output:</P
  52036. ><TABLE
  52037. BORDER="0"
  52038. BGCOLOR="#E0E0E0"
  52039. CELLPADDING="5"
  52040. ><TR
  52041. ><TD
  52042. ><PRE
  52043. CLASS="screen"
  52044. >Array
  52045. (
  52046.     [b] => brown
  52047.     [c] => blue
  52048.     [0] => red
  52049. )</PRE
  52050. ></TD
  52051. ></TR
  52052. ></TABLE
  52053. ></DIV
  52054. ></TD
  52055. ></TR
  52056. ></TABLE
  52057. >
  52058.     </P
  52059. ><P
  52060. >      In our example above you see the <VAR
  52061. CLASS="literal"
  52062. >"a" => "green"</VAR
  52063. >
  52064.      pair is present in both arrays and thus it is not in the ouput from the
  52065.      function. Unlike this, the pair <VAR
  52066. CLASS="literal"
  52067. >0 => "red"</VAR
  52068.      is in the ouput because in the second argument <VAR
  52069. CLASS="literal"
  52070. >"red"</VAR
  52071. >
  52072.      has key which is <VAR
  52073. CLASS="literal"
  52074. >1</VAR
  52075. >. 
  52076.     </P
  52077. ><P
  52078. >      Two values from <SPAN
  52079. CLASS="emphasis"
  52080. ><I
  52081. CLASS="emphasis"
  52082. >key => value</I
  52083. ></SPAN
  52084. > pairs are
  52085.      considered equal only if <VAR
  52086. CLASS="literal"
  52087. >(string) $elem1 === (string)
  52088.      $elem2 </VAR
  52089. >. In other words a strict check takes place so 
  52090.      the string representations must be the same.
  52091.      
  52092.     </P
  52093. ><DIV
  52094. CLASS="note"
  52095. ><BLOCKQUOTE
  52096. CLASS="note"
  52097. ><P
  52098. ><B
  52099. >Note: </B
  52100. >
  52101.       Please note that this function only checks one dimension of a n-dimensional
  52102.       array. Of course you can check deeper dimensions by using, for example, 
  52103.       <VAR
  52104. CLASS="literal"
  52105. >array_diff_assoc($array1[0], $array2[0]);</VAR
  52106. >.
  52107.      </P
  52108. ></BLOCKQUOTE
  52109. ></DIV
  52110. ><P
  52111. >      See also <A
  52112. HREF="#function.array-diff"
  52113. ><B
  52114. CLASS="function"
  52115. >array_diff()</B
  52116. ></A
  52117. >,
  52118.      <A
  52119. HREF="#function.array-intersect"
  52120. ><B
  52121. CLASS="function"
  52122. >array_intersect()</B
  52123. ></A
  52124. >,
  52125.      and <A
  52126. HREF="#function.array-intersect-assoc"
  52127. ><B
  52128. CLASS="function"
  52129. >array_intersect_assoc()</B
  52130. ></A
  52131. >.
  52132.     </P
  52133. ></DIV
  52134. ><H1
  52135. ><A
  52136. NAME="function.array-diff-key"
  52137. ></A
  52138. >array_diff_key</H1
  52139. ><DIV
  52140. CLASS="refnamediv"
  52141. ><A
  52142. NAME="AEN8913"
  52143. ></A
  52144. ><P
  52145. >    (no version information, might be only in CVS)</P
  52146. >array_diff_key -- Computes the difference of arrays using keys for comparison</DIV
  52147. ><DIV
  52148. CLASS="refsect1"
  52149. ><A
  52150. NAME="AEN8916"
  52151. ></A
  52152. ><H2
  52153. >Description</H2
  52154. >array <B
  52155. CLASS="methodname"
  52156. >array_diff_key</B
  52157. > ( array array1, array array2 [, array ...])<BR
  52158. ></BR
  52159. ><P
  52160. >      <B
  52161. CLASS="function"
  52162. >array_diff_key()</B
  52163. > returns an array
  52164.      containing all the values of <VAR
  52165. CLASS="parameter"
  52166. >array1</VAR
  52167. >
  52168.      that have keys that are not present in any of the other arguments.
  52169.      Note that the associativity is preserved. This function is like
  52170.      <A
  52171. HREF="#function.array-diff"
  52172. ><B
  52173. CLASS="function"
  52174. >array_diff()</B
  52175. ></A
  52176. > except the comparison is done on the
  52177.      keys instead of the values.
  52178.     </P
  52179. ><P
  52180. >      <TABLE
  52181. WIDTH="100%"
  52182. BORDER="0"
  52183. CELLPADDING="0"
  52184. CELLSPACING="0"
  52185. CLASS="EXAMPLE"
  52186. ><TR
  52187. ><TD
  52188. ><DIV
  52189. CLASS="example"
  52190. ><A
  52191. NAME="AEN8935"
  52192. ></A
  52193. ><P
  52194. ><B
  52195. >Example 1. <B
  52196. CLASS="function"
  52197. >array_diff_key()</B
  52198. > example</B
  52199. ></P
  52200. ><TABLE
  52201. BORDER="0"
  52202. BGCOLOR="#E0E0E0"
  52203. CELLPADDING="5"
  52204. ><TR
  52205. ><TD
  52206. ><PRE
  52207. CLASS="php"
  52208. ><?php
  52209. $array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
  52210. $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
  52211.  
  52212. var_dump(array_diff_key($array1, $array2));
  52213. ?></PRE
  52214. ></TD
  52215. ></TR
  52216. ></TABLE
  52217. ><P
  52218. >The above example will output:</P
  52219. ><TABLE
  52220. BORDER="0"
  52221. BGCOLOR="#E0E0E0"
  52222. CELLPADDING="5"
  52223. ><TR
  52224. ><TD
  52225. ><PRE
  52226. CLASS="screen"
  52227. >array(2) {
  52228.   ["red"]=>
  52229.   int(2)
  52230.   ["purple"]=>
  52231.   int(4)
  52232. }</PRE
  52233. ></TD
  52234. ></TR
  52235. ></TABLE
  52236. ></DIV
  52237. ></TD
  52238. ></TR
  52239. ></TABLE
  52240. >
  52241.     </P
  52242. ><P
  52243. >      The two keys from the <VAR
  52244. CLASS="literal"
  52245. >key => value</VAR
  52246. > pairs are
  52247.      considered equal only if 
  52248.      <VAR
  52249. CLASS="literal"
  52250. >(string) $key1 === (string) $key2 </VAR
  52251. >. In other words
  52252.      a strict type check is executed so the string representation must be
  52253.      the same.
  52254.     </P
  52255. ><DIV
  52256. CLASS="note"
  52257. ><BLOCKQUOTE
  52258. CLASS="note"
  52259. ><P
  52260. ><B
  52261. >Note: </B
  52262. >
  52263.       Please note that this function only checks one dimension of a n-dimensional
  52264.       array. Of course you can check deeper dimensions by using
  52265.       <VAR
  52266. CLASS="literal"
  52267. >array_diff_key($array1[0], $array2[0]);</VAR
  52268. >.
  52269.      </P
  52270. ></BLOCKQUOTE
  52271. ></DIV
  52272. ><P
  52273. >      See also <A
  52274. HREF="#function.array-diff"
  52275. ><B
  52276. CLASS="function"
  52277. >array_diff()</B
  52278. ></A
  52279. >,
  52280.      <A
  52281. HREF="#function.array-udiff"
  52282. ><B
  52283. CLASS="function"
  52284. >array_udiff()</B
  52285. ></A
  52286. >
  52287.      <A
  52288. HREF="#function.array-diff-assoc"
  52289. ><B
  52290. CLASS="function"
  52291. >array_diff_assoc()</B
  52292. ></A
  52293. >,
  52294.      <A
  52295. HREF="#function.array-diff-uassoc"
  52296. ><B
  52297. CLASS="function"
  52298. >array_diff_uassoc()</B
  52299. ></A
  52300. >,
  52301.      <A
  52302. HREF="#function.array-udiff-assoc"
  52303. ><B
  52304. CLASS="function"
  52305. >array_udiff_assoc()</B
  52306. ></A
  52307. >,
  52308.      <A
  52309. HREF="#function.array-udiff-uassoc"
  52310. ><B
  52311. CLASS="function"
  52312. >array_udiff_uassoc()</B
  52313. ></A
  52314. >,
  52315.      <A
  52316. HREF="#function.array-diff-ukey"
  52317. ><B
  52318. CLASS="function"
  52319. >array_diff_ukey()</B
  52320. ></A
  52321. >,
  52322.      <A
  52323. HREF="#function.array-intersect"
  52324. ><B
  52325. CLASS="function"
  52326. >array_intersect()</B
  52327. ></A
  52328. >,
  52329.      <A
  52330. HREF="#function.array-intersect-assoc"
  52331. ><B
  52332. CLASS="function"
  52333. >array_intersect_assoc()</B
  52334. ></A
  52335. >,
  52336.      <A
  52337. HREF="#function.array-intersect-uassoc"
  52338. ><B
  52339. CLASS="function"
  52340. >array_intersect_uassoc()</B
  52341. ></A
  52342. >,
  52343.      <A
  52344. HREF="#function.array-intersect-key"
  52345. ><B
  52346. CLASS="function"
  52347. >array_intersect_key()</B
  52348. ></A
  52349. > and
  52350.      <A
  52351. HREF="#function.array-intersect-ukey"
  52352. ><B
  52353. CLASS="function"
  52354. >array_intersect_ukey()</B
  52355. ></A
  52356. >.
  52357.     </P
  52358. ></DIV
  52359. ><H1
  52360. ><A
  52361. NAME="function.array-diff-uassoc"
  52362. ></A
  52363. >array_diff_uassoc</H1
  52364. ><DIV
  52365. CLASS="refnamediv"
  52366. ><A
  52367. NAME="AEN8961"
  52368. ></A
  52369. ><P
  52370. >    (PHP 5)</P
  52371. >array_diff_uassoc -- 
  52372.      Computes the difference of arrays with additional index check
  52373.      which is performed by a user supplied callback function
  52374.     </DIV
  52375. ><DIV
  52376. CLASS="refsect1"
  52377. ><A
  52378. NAME="AEN8964"
  52379. ></A
  52380. ><H2
  52381. >Description</H2
  52382. >array <B
  52383. CLASS="methodname"
  52384. >array_diff_uassoc</B
  52385. > ( array array1, array array2 [, array ..., callback key_compare_func])<BR
  52386. ></BR
  52387. ><P
  52388. >      <B
  52389. CLASS="function"
  52390. >array_diff_uassoc()</B
  52391. > returns an <A
  52392. HREF="#language.types.array"
  52393. ><B
  52394. CLASS="type"
  52395. >array</B
  52396. ></A
  52397. >
  52398.      containing all the values from <VAR
  52399. CLASS="parameter"
  52400. >array1</VAR
  52401. >
  52402.      that are not present in any of the other arguments.
  52403.      Note that the keys are used in the comparison unlike
  52404.      <A
  52405. HREF="#function.array-diff"
  52406. ><B
  52407. CLASS="function"
  52408. >array_diff()</B
  52409. ></A
  52410. >. This comparison is done by a user supplied callback function.
  52411.      It must return an integer less than, equal
  52412.      to, or greater than zero if the first argument is considered to
  52413.      be respectively less than, equal to, or greater than the
  52414.      second. This is unlike <A
  52415. HREF="#function.array-diff-assoc"
  52416. ><B
  52417. CLASS="function"
  52418. >array_diff_assoc()</B
  52419. ></A
  52420. > where an
  52421.      internal function for comparing the indices is used.
  52422.     </P
  52423. ><P
  52424. >      <TABLE
  52425. WIDTH="100%"
  52426. BORDER="0"
  52427. CELLPADDING="0"
  52428. CELLSPACING="0"
  52429. CLASS="EXAMPLE"
  52430. ><TR
  52431. ><TD
  52432. ><DIV
  52433. CLASS="example"
  52434. ><A
  52435. NAME="AEN8988"
  52436. ></A
  52437. ><P
  52438. ><B
  52439. >Example 1. <B
  52440. CLASS="function"
  52441. >array_diff_uassoc()</B
  52442. > example</B
  52443. ></P
  52444. ><TABLE
  52445. BORDER="0"
  52446. BGCOLOR="#E0E0E0"
  52447. CELLPADDING="5"
  52448. ><TR
  52449. ><TD
  52450. ><PRE
  52451. CLASS="php"
  52452. ><?php
  52453. function key_compare_func($a, $b) 
  52454. {
  52455.     if ($a === $b) {
  52456.         return 0;
  52457.     }
  52458.     return ($a > $b)? 1:-1;
  52459. }
  52460.  
  52461. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
  52462. $array2 = array("a" => "green", "yellow", "red");
  52463. $result = array_diff_uassoc($array1, $array2, "key_compare_func");
  52464. ?></PRE
  52465. ></TD
  52466. ></TR
  52467. ></TABLE
  52468. ><P
  52469. >The above example will output:</P
  52470. ><TABLE
  52471. BORDER="0"
  52472. BGCOLOR="#E0E0E0"
  52473. CELLPADDING="5"
  52474. ><TR
  52475. ><TD
  52476. ><PRE
  52477. CLASS="screen"
  52478. >Array
  52479. (
  52480.     [b] => brown
  52481.     [c] => blue
  52482.     [0] => red
  52483. )</PRE
  52484. ></TD
  52485. ></TR
  52486. ></TABLE
  52487. ></DIV
  52488. ></TD
  52489. ></TR
  52490. ></TABLE
  52491. >
  52492.     </P
  52493. ><P
  52494. >      In our example above you see the <VAR
  52495. CLASS="literal"
  52496. >"a" => "green"</VAR
  52497. >
  52498.      pair is present in both arrays and thus it is not in the ouput from the
  52499.      function. Unlike this, the pair <VAR
  52500. CLASS="literal"
  52501. >0 => "red"</VAR
  52502. >
  52503.      is in the ouput because in the second argument <VAR
  52504. CLASS="literal"
  52505. >"red"</VAR
  52506. >
  52507.      has key which is <VAR
  52508. CLASS="literal"
  52509. >1</VAR
  52510. >.
  52511.     </P
  52512. ><P
  52513. >      The equality of 2 indices is checked by the user supplied callback function.
  52514.     </P
  52515. ><DIV
  52516. CLASS="note"
  52517. ><BLOCKQUOTE
  52518. CLASS="note"
  52519. ><P
  52520. ><B
  52521. >Note: </B
  52522. >
  52523.       Please note that this function only checks one dimension of a n-dimensional
  52524.       array. Of course you can check deeper dimensions by using, for example,
  52525.       <VAR
  52526. CLASS="literal"
  52527. >array_diff_uassoc($array1[0], $array2[0], "key_compare_func");</VAR
  52528. >.
  52529.      </P
  52530. ></BLOCKQUOTE
  52531. ></DIV
  52532. ><P
  52533. >      See also
  52534.      <A
  52535. HREF="#function.array-diff"
  52536. ><B
  52537. CLASS="function"
  52538. >array_diff()</B
  52539. ></A
  52540. >,
  52541.      <A
  52542. HREF="#function.array-diff-assoc"
  52543. ><B
  52544. CLASS="function"
  52545. >array_diff_assoc()</B
  52546. ></A
  52547. >,
  52548.      <A
  52549. HREF="#function.array-udiff"
  52550. ><B
  52551. CLASS="function"
  52552. >array_udiff()</B
  52553. ></A
  52554. >,
  52555.      <A
  52556. HREF="#function.array-udiff-assoc"
  52557. ><B
  52558. CLASS="function"
  52559. >array_udiff_assoc()</B
  52560. ></A
  52561. >, 
  52562.      <A
  52563. HREF="#function.array-udiff-uassoc"
  52564. ><B
  52565. CLASS="function"
  52566. >array_udiff_uassoc()</B
  52567. ></A
  52568. >, 
  52569.      <A
  52570. HREF="#function.array-intersect"
  52571. ><B
  52572. CLASS="function"
  52573. >array_intersect()</B
  52574. ></A
  52575. >,  
  52576.      <A
  52577. HREF="#function.array-intersect-assoc"
  52578. ><B
  52579. CLASS="function"
  52580. >array_intersect_assoc()</B
  52581. ></A
  52582. >,
  52583.      <A
  52584. HREF="#function.array-uintersect"
  52585. ><B
  52586. CLASS="function"
  52587. >array_uintersect()</B
  52588. ></A
  52589. >,  
  52590.      <A
  52591. HREF="#function.array-uintersect-assoc"
  52592. ><B
  52593. CLASS="function"
  52594. >array_uintersect_assoc()</B
  52595. ></A
  52596. > and 
  52597.      <A
  52598. HREF="#function.array-uintersect-uassoc"
  52599. ><B
  52600. CLASS="function"
  52601. >array_uintersect_uassoc()</B
  52602. ></A
  52603. >.
  52604.     </P
  52605. ></DIV
  52606. ><H1
  52607. ><A
  52608. NAME="function.array-diff-ukey"
  52609. ></A
  52610. >array_diff_ukey</H1
  52611. ><DIV
  52612. CLASS="refnamediv"
  52613. ><A
  52614. NAME="AEN9015"
  52615. ></A
  52616. ><P
  52617. >    (no version information, might be only in CVS)</P
  52618. >array_diff_ukey -- Computes the difference of arrays using a callback function on the keys for comparison</DIV
  52619. ><DIV
  52620. CLASS="refsect1"
  52621. ><A
  52622. NAME="AEN9018"
  52623. ></A
  52624. ><H2
  52625. >Description</H2
  52626. >array <B
  52627. CLASS="methodname"
  52628. >array_diff_ukey</B
  52629. > ( array array1, array array2 [, array ..., callback key_compare_func])<BR
  52630. ></BR
  52631. ><P
  52632. >      <B
  52633. CLASS="function"
  52634. >array_diff_ukey()</B
  52635. > returns an array
  52636.      containing all the values of <VAR
  52637. CLASS="parameter"
  52638. >array1</VAR
  52639. >
  52640.      that have keys that are not present in any of the other arguments.
  52641.      Note that the associativity is preserved. This function is like
  52642.      <A
  52643. HREF="#function.array-diff"
  52644. ><B
  52645. CLASS="function"
  52646. >array_diff()</B
  52647. ></A
  52648. > except the comparison is done on the
  52649.      keys instead of the values.
  52650.     </P
  52651. ><P
  52652. >      This comparison is done by a user supplied callback function.
  52653.      It must return an integer less than, equal to, or greater than zero if the
  52654.      first key is considered to be respectively less than, equal to, or
  52655.      greater than the second.
  52656.     </P
  52657. ><P
  52658. >      <TABLE
  52659. WIDTH="100%"
  52660. BORDER="0"
  52661. CELLPADDING="0"
  52662. CELLSPACING="0"
  52663. CLASS="EXAMPLE"
  52664. ><TR
  52665. ><TD
  52666. ><DIV
  52667. CLASS="example"
  52668. ><A
  52669. NAME="AEN9041"
  52670. ></A
  52671. ><P
  52672. ><B
  52673. >Example 1. <B
  52674. CLASS="function"
  52675. >array_diff_ukey()</B
  52676. > example</B
  52677. ></P
  52678. ><TABLE
  52679. BORDER="0"
  52680. BGCOLOR="#E0E0E0"
  52681. CELLPADDING="5"
  52682. ><TR
  52683. ><TD
  52684. ><PRE
  52685. CLASS="php"
  52686. ><?php
  52687. function key_compare_func($key1, $key2)
  52688. {
  52689.     if ($key1 == $key2)
  52690.         return 0;
  52691.     else if ($key1 > $key2)
  52692.         return 1;
  52693.     else
  52694.         return -1;
  52695. }
  52696.  
  52697. $array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
  52698. $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
  52699.  
  52700. var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));
  52701. ?></PRE
  52702. ></TD
  52703. ></TR
  52704. ></TABLE
  52705. ><P
  52706. >The above example will output:</P
  52707. ><TABLE
  52708. BORDER="0"
  52709. BGCOLOR="#E0E0E0"
  52710. CELLPADDING="5"
  52711. ><TR
  52712. ><TD
  52713. ><PRE
  52714. CLASS="screen"
  52715. >array(2) {
  52716.   ["red"]=>
  52717.   int(2)
  52718.   ["purple"]=>
  52719.   int(4)
  52720. }</PRE
  52721. ></TD
  52722. ></TR
  52723. ></TABLE
  52724. ></DIV
  52725. ></TD
  52726. ></TR
  52727. ></TABLE
  52728. >
  52729.     </P
  52730. ><P
  52731. >      The two keys from the <VAR
  52732. CLASS="literal"
  52733. >key => value</VAR
  52734. > pairs are
  52735.      considered equal only if 
  52736.      <VAR
  52737. CLASS="literal"
  52738. >(string) $key1 === (string) $key2 </VAR
  52739. >. In other words
  52740.      a strict type check is executed so the string representation must be
  52741.      the same.
  52742.     </P
  52743. ><DIV
  52744. CLASS="note"
  52745. ><BLOCKQUOTE
  52746. CLASS="note"
  52747. ><P
  52748. ><B
  52749. >Note: </B
  52750. >
  52751.       Please note that this function only checks one dimension of a n-dimensional
  52752.       array. Of course you can check deeper dimensions by using
  52753.       <VAR
  52754. CLASS="literal"
  52755. >array_diff_ukey($array1[0], $array2[0], 'callback_func');</VAR
  52756. >.
  52757.      </P
  52758. ></BLOCKQUOTE
  52759. ></DIV
  52760. ><P
  52761. >      See also <A
  52762. HREF="#function.array-diff"
  52763. ><B
  52764. CLASS="function"
  52765. >array_diff()</B
  52766. ></A
  52767. >,
  52768.      <A
  52769. HREF="#function.array-udiff"
  52770. ><B
  52771. CLASS="function"
  52772. >array_udiff()</B
  52773. ></A
  52774. >
  52775.      <A
  52776. HREF="#function.array-diff-assoc"
  52777. ><B
  52778. CLASS="function"
  52779. >array_diff_assoc()</B
  52780. ></A
  52781. >,
  52782.      <A
  52783. HREF="#function.array-diff-uassoc"
  52784. ><B
  52785. CLASS="function"
  52786. >array_diff_uassoc()</B
  52787. ></A
  52788. >,
  52789.      <A
  52790. HREF="#function.array-udiff-assoc"
  52791. ><B
  52792. CLASS="function"
  52793. >array_udiff_assoc()</B
  52794. ></A
  52795. >,
  52796.      <A
  52797. HREF="#function.array-udiff-uassoc"
  52798. ><B
  52799. CLASS="function"
  52800. >array_udiff_uassoc()</B
  52801. ></A
  52802. >,
  52803.      <A
  52804. HREF="#function.array-diff-key"
  52805. ><B
  52806. CLASS="function"
  52807. >array_diff_key()</B
  52808. ></A
  52809. >,
  52810.      <A
  52811. HREF="#function.array-intersect"
  52812. ><B
  52813. CLASS="function"
  52814. >array_intersect()</B
  52815. ></A
  52816. >,
  52817.      <A
  52818. HREF="#function.array-intersect-assoc"
  52819. ><B
  52820. CLASS="function"
  52821. >array_intersect_assoc()</B
  52822. ></A
  52823. >,
  52824.      <A
  52825. HREF="#function.array-intersect-uassoc"
  52826. ><B
  52827. CLASS="function"
  52828. >array_intersect_uassoc()</B
  52829. ></A
  52830. >,
  52831.      <A
  52832. HREF="#function.array-intersect-key"
  52833. ><B
  52834. CLASS="function"
  52835. >array_intersect_key()</B
  52836. ></A
  52837. > and
  52838.      <A
  52839. HREF="#function.array-intersect-ukey"
  52840. ><B
  52841. CLASS="function"
  52842. >array_intersect_ukey()</B
  52843. ></A
  52844. >.
  52845.     </P
  52846. ></DIV
  52847. ><H1
  52848. ><A
  52849. NAME="function.array-diff"
  52850. ></A
  52851. >array_diff</H1
  52852. ><DIV
  52853. CLASS="refnamediv"
  52854. ><A
  52855. NAME="AEN9067"
  52856. ></A
  52857. ><P
  52858. >    (PHP 4 >= 4.0.1, PHP 5)</P
  52859. >array_diff -- Computes the difference of arrays</DIV
  52860. ><DIV
  52861. CLASS="refsect1"
  52862. ><A
  52863. NAME="AEN9070"
  52864. ></A
  52865. ><H2
  52866. >Description</H2
  52867. >array <B
  52868. CLASS="methodname"
  52869. >array_diff</B
  52870. > ( array array1, array array2 [, array ...])<BR
  52871. ></BR
  52872. ><P
  52873. >      <B
  52874. CLASS="function"
  52875. >array_diff()</B
  52876. > returns an array
  52877.      containing all the values of <VAR
  52878. CLASS="parameter"
  52879. >array1</VAR
  52880. >
  52881.      that are not present in any of the other arguments.
  52882.      Note that keys are preserved.
  52883.     </P
  52884. ><P
  52885. >      <TABLE
  52886. WIDTH="100%"
  52887. BORDER="0"
  52888. CELLPADDING="0"
  52889. CELLSPACING="0"
  52890. CLASS="EXAMPLE"
  52891. ><TR
  52892. ><TD
  52893. ><DIV
  52894. CLASS="example"
  52895. ><A
  52896. NAME="AEN9088"
  52897. ></A
  52898. ><P
  52899. ><B
  52900. >Example 1. <B
  52901. CLASS="function"
  52902. >array_diff()</B
  52903. > example</B
  52904. ></P
  52905. ><TABLE
  52906. BORDER="0"
  52907. BGCOLOR="#E0E0E0"
  52908. CELLPADDING="5"
  52909. ><TR
  52910. ><TD
  52911. ><PRE
  52912. CLASS="php"
  52913. ><?php
  52914. $array1 = array("a" => "green", "red", "blue", "red");
  52915. $array2 = array("b" => "green", "yellow", "red");
  52916. $result = array_diff($array1, $array2);
  52917.  
  52918. print_r($result);
  52919. ?></PRE
  52920. ></TD
  52921. ></TR
  52922. ></TABLE
  52923. ><P
  52924. >        Multiple occurrences in $array1 are all treated the same way.
  52925.        This will output :
  52926.       </P
  52927. ><TABLE
  52928. BORDER="0"
  52929. BGCOLOR="#E0E0E0"
  52930. CELLPADDING="5"
  52931. ><TR
  52932. ><TD
  52933. ><PRE
  52934. CLASS="screen"
  52935. >Array
  52936. (
  52937.     [1] => blue
  52938. )</PRE
  52939. ></TD
  52940. ></TR
  52941. ></TABLE
  52942. ></DIV
  52943. ></TD
  52944. ></TR
  52945. ></TABLE
  52946. >
  52947.     </P
  52948. ><DIV
  52949. CLASS="note"
  52950. ><BLOCKQUOTE
  52951. CLASS="note"
  52952. ><P
  52953. ><B
  52954. >Note: </B
  52955. >
  52956.       Two elements are considered equal if and only if
  52957.       <VAR
  52958. CLASS="literal"
  52959. >(string) $elem1 === (string) $elem2</VAR
  52960. >. In words:
  52961.       when the string representation is the same.
  52962.       
  52963.      </P
  52964. ></BLOCKQUOTE
  52965. ></DIV
  52966. ><DIV
  52967. CLASS="note"
  52968. ><BLOCKQUOTE
  52969. CLASS="note"
  52970. ><P
  52971. ><B
  52972. >Note: </B
  52973. >
  52974.       Please note that this function only checks one dimension of a n-dimensional
  52975.       array. Of course you can check deeper dimensions by using
  52976.       <VAR
  52977. CLASS="literal"
  52978. >array_diff($array1[0], $array2[0]);</VAR
  52979. >.
  52980.      </P
  52981. ></BLOCKQUOTE
  52982. ></DIV
  52983. ><DIV
  52984. CLASS="warning"
  52985. ><P
  52986. ></P
  52987. ><TABLE
  52988. CLASS="warning"
  52989. BORDER="1"
  52990. WIDTH="100%"
  52991. ><TR
  52992. ><TD
  52993. ALIGN="CENTER"
  52994. ><B
  52995. >Warning</B
  52996. ></TD
  52997. ></TR
  52998. ><TR
  52999. ><TD
  53000. ALIGN="LEFT"
  53001. ><P
  53002. >       This was broken in PHP 4.0.4!
  53003.       
  53004.      </P
  53005. ></TD
  53006. ></TR
  53007. ></TABLE
  53008. ></DIV
  53009. ><P
  53010. >      See also <A
  53011. HREF="#function.array-diff-assoc"
  53012. ><B
  53013. CLASS="function"
  53014. >array_diff_assoc()</B
  53015. ></A
  53016. >,
  53017.      <A
  53018. HREF="#function.array-intersect"
  53019. ><B
  53020. CLASS="function"
  53021. >array_intersect()</B
  53022. ></A
  53023. >, and
  53024.      <A
  53025. HREF="#function.array-intersect-assoc"
  53026. ><B
  53027. CLASS="function"
  53028. >array_intersect_assoc()</B
  53029. ></A
  53030. >.
  53031.     </P
  53032. ></DIV
  53033. ><H1
  53034. ><A
  53035. NAME="function.array-fill"
  53036. ></A
  53037. >array_fill</H1
  53038. ><DIV
  53039. CLASS="refnamediv"
  53040. ><A
  53041. NAME="AEN9107"
  53042. ></A
  53043. ><P
  53044. >    (PHP 4 >= 4.2.0, PHP 5)</P
  53045. >array_fill -- Fill an array with values</DIV
  53046. ><DIV
  53047. CLASS="refsect1"
  53048. ><A
  53049. NAME="AEN9110"
  53050. ></A
  53051. ><H2
  53052. >Description</H2
  53053. >array <B
  53054. CLASS="methodname"
  53055. >array_fill</B
  53056. > ( int start_index, int num, mixed value)<BR
  53057. ></BR
  53058. ><P
  53059. >      <B
  53060. CLASS="function"
  53061. >array_fill()</B
  53062. > fills an array with
  53063.      <VAR
  53064. CLASS="parameter"
  53065. >num</VAR
  53066. > entries of the value of the
  53067.      <VAR
  53068. CLASS="parameter"
  53069. >value</VAR
  53070. > parameter, keys starting at the
  53071.      <VAR
  53072. CLASS="parameter"
  53073. >start_index</VAR
  53074. > parameter.  Note that <VAR
  53075. CLASS="parameter"
  53076. >      num</VAR
  53077. > must be a number greater than zero, or PHP will throw
  53078.      a warning.
  53079.     </P
  53080. ><P
  53081. >      <TABLE
  53082. WIDTH="100%"
  53083. BORDER="0"
  53084. CELLPADDING="0"
  53085. CELLSPACING="0"
  53086. CLASS="EXAMPLE"
  53087. ><TR
  53088. ><TD
  53089. ><DIV
  53090. CLASS="example"
  53091. ><A
  53092. NAME="AEN9131"
  53093. ></A
  53094. ><P
  53095. ><B
  53096. >Example 1. <B
  53097. CLASS="function"
  53098. >array_fill()</B
  53099. > example</B
  53100. ></P
  53101. ><TABLE
  53102. BORDER="0"
  53103. BGCOLOR="#E0E0E0"
  53104. CELLPADDING="5"
  53105. ><TR
  53106. ><TD
  53107. ><PRE
  53108. CLASS="php"
  53109. ><?php
  53110. $a = array_fill(5, 6, 'banana');
  53111. print_r($a);
  53112. ?></PRE
  53113. ></TD
  53114. ></TR
  53115. ></TABLE
  53116. ><P
  53117. >        <VAR
  53118. CLASS="varname"
  53119. >$a</VAR
  53120. > now is:
  53121.       </P
  53122. ><TABLE
  53123. BORDER="0"
  53124. BGCOLOR="#E0E0E0"
  53125. CELLPADDING="5"
  53126. ><TR
  53127. ><TD
  53128. ><PRE
  53129. CLASS="screen"
  53130. >Array
  53131. (
  53132.     [5]  => banana
  53133.     [6]  => banana
  53134.     [7]  => banana
  53135.     [8]  => banana
  53136.     [9]  => banana
  53137.     [10] => banana
  53138. )</PRE
  53139. ></TD
  53140. ></TR
  53141. ></TABLE
  53142. ></DIV
  53143. ></TD
  53144. ></TR
  53145. ></TABLE
  53146. >
  53147.     </P
  53148. ><P
  53149. >      See also <A
  53150. HREF="#function.str-repeat"
  53151. ><B
  53152. CLASS="function"
  53153. >str_repeat()</B
  53154. ></A
  53155. > and
  53156.      <A
  53157. HREF="#function.range"
  53158. ><B
  53159. CLASS="function"
  53160. >range()</B
  53161. ></A
  53162. >.
  53163.     </P
  53164. ></DIV
  53165. ><H1
  53166. ><A
  53167. NAME="function.array-filter"
  53168. ></A
  53169. >array_filter</H1
  53170. ><DIV
  53171. CLASS="refnamediv"
  53172. ><A
  53173. NAME="AEN9142"
  53174. ></A
  53175. ><P
  53176. >    (PHP 4 >= 4.0.6, PHP 5)</P
  53177. >array_filter -- 
  53178.      Filters elements of an array using a callback function
  53179.     </DIV
  53180. ><DIV
  53181. CLASS="refsect1"
  53182. ><A
  53183. NAME="AEN9145"
  53184. ></A
  53185. ><H2
  53186. >Description</H2
  53187. >array <B
  53188. CLASS="methodname"
  53189. >array_filter</B
  53190. > ( array input [, callback callback])<BR
  53191. ></BR
  53192. ><P
  53193. >      <B
  53194. CLASS="function"
  53195. >array_filter()</B
  53196. > iterates over each value in
  53197.      the <VAR
  53198. CLASS="parameter"
  53199. >input</VAR
  53200. > array passing them to the
  53201.      <VAR
  53202. CLASS="parameter"
  53203. >callback</VAR
  53204. > function.  If the <VAR
  53205. CLASS="parameter"
  53206. >      callback</VAR
  53207. > function returns true, the current
  53208.      value from <VAR
  53209. CLASS="parameter"
  53210. >input</VAR
  53211. > is returned into the
  53212.      result array.  Array keys are preserved.
  53213.     </P
  53214. ><P
  53215. >      <TABLE
  53216. WIDTH="100%"
  53217. BORDER="0"
  53218. CELLPADDING="0"
  53219. CELLSPACING="0"
  53220. CLASS="EXAMPLE"
  53221. ><TR
  53222. ><TD
  53223. ><DIV
  53224. CLASS="example"
  53225. ><A
  53226. NAME="AEN9163"
  53227. ></A
  53228. ><P
  53229. ><B
  53230. >Example 1. <B
  53231. CLASS="function"
  53232. >array_filter()</B
  53233. > example</B
  53234. ></P
  53235. ><TABLE
  53236. BORDER="0"
  53237. BGCOLOR="#E0E0E0"
  53238. CELLPADDING="5"
  53239. ><TR
  53240. ><TD
  53241. ><PRE
  53242. CLASS="php"
  53243. ><?php
  53244. function odd($var) 
  53245. {
  53246.     return($var % 2 == 1);
  53247. }
  53248.  
  53249. function even($var) 
  53250. {
  53251.     return($var % 2 == 0);
  53252. }
  53253.  
  53254. $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
  53255. $array2 = array(6, 7, 8, 9, 10, 11, 12);
  53256.  
  53257. echo "Odd :\n";
  53258. print_r(array_filter($array1, "odd"));
  53259. echo "Even:\n";
  53260. print_r(array_filter($array2, "even"));
  53261. ?></PRE
  53262. ></TD
  53263. ></TR
  53264. ></TABLE
  53265. ><P
  53266. >The above example will output:</P
  53267. ><TABLE
  53268. BORDER="0"
  53269. BGCOLOR="#E0E0E0"
  53270. CELLPADDING="5"
  53271. ><TR
  53272. ><TD
  53273. ><PRE
  53274. CLASS="php"
  53275. >Odd :
  53276. Array
  53277. (
  53278.     [a] => 1
  53279.     [c] => 3
  53280.     [e] => 5
  53281. )
  53282. Even:
  53283. Array
  53284. (
  53285.     [0] => 6
  53286.     [2] => 8
  53287.     [4] => 10
  53288.     [6] => 12
  53289. )</PRE
  53290. ></TD
  53291. ></TR
  53292. ></TABLE
  53293. ></DIV
  53294. ></TD
  53295. ></TR
  53296. ></TABLE
  53297. >
  53298.     </P
  53299. ><P
  53300. >      Users may not change the array itself from the callback
  53301.      function. e.g. Add/delete an element, unset the array that
  53302.      <B
  53303. CLASS="function"
  53304. >array_filter()</B
  53305. > is applied to. If the array
  53306.      is changed, the behavior of this function is undefined.
  53307.     </P
  53308. ><P
  53309. >      If the <VAR
  53310. CLASS="parameter"
  53311. >callback</VAR
  53312. > function is not supplied,
  53313.      <B
  53314. CLASS="function"
  53315. >array_filter()</B
  53316. > will remove all the entries of
  53317.      <VAR
  53318. CLASS="parameter"
  53319. >input</VAR
  53320. > that are equal to <TT
  53321. CLASS="constant"
  53322. ><B
  53323. >FALSE</B
  53324. ></TT
  53325. >. See <A
  53326. HREF="#language.types.boolean.casting"
  53327. >converting to boolean</A
  53328.      for more information.
  53329.     </P
  53330. ><P
  53331. >      <TABLE
  53332. WIDTH="100%"
  53333. BORDER="0"
  53334. CELLPADDING="0"
  53335. CELLSPACING="0"
  53336. CLASS="EXAMPLE"
  53337. ><TR
  53338. ><TD
  53339. ><DIV
  53340. CLASS="example"
  53341. ><A
  53342. NAME="AEN9178"
  53343. ></A
  53344. ><P
  53345. ><B
  53346. >Example 2. <B
  53347. CLASS="function"
  53348. >array_filter()</B
  53349. > without
  53350.       <VAR
  53351. CLASS="parameter"
  53352. >callback</VAR
  53353. ></B
  53354. ></P
  53355. ><TABLE
  53356. BORDER="0"
  53357. BGCOLOR="#E0E0E0"
  53358. CELLPADDING="5"
  53359. ><TR
  53360. ><TD
  53361. ><PRE
  53362. CLASS="php"
  53363. ><?php
  53364.  
  53365. $entry = array(
  53366.              0 => 'foo',
  53367.              1 => false,
  53368.              2 => -1,
  53369.              3 => null,
  53370.              4 => ''
  53371.           );
  53372.  
  53373. print_r(array_filter($entry));
  53374. ?></PRE
  53375. ></TD
  53376. ></TR
  53377. ></TABLE
  53378. ><P
  53379. >The above example will output:</P
  53380. ><TABLE
  53381. BORDER="0"
  53382. BGCOLOR="#E0E0E0"
  53383. CELLPADDING="5"
  53384. ><TR
  53385. ><TD
  53386. ><PRE
  53387. CLASS="screen"
  53388. >Array
  53389. (
  53390.     [0] => foo
  53391.     [2] => -1
  53392. )</PRE
  53393. ></TD
  53394. ></TR
  53395. ></TABLE
  53396. ></DIV
  53397. ></TD
  53398. ></TR
  53399. ></TABLE
  53400. >
  53401.     </P
  53402. ><P
  53403. >      See also <A
  53404. HREF="#function.array-map"
  53405. ><B
  53406. CLASS="function"
  53407. >array_map()</B
  53408. ></A
  53409. >,
  53410.      <A
  53411. HREF="#function.array-reduce"
  53412. ><B
  53413. CLASS="function"
  53414. >array_reduce()</B
  53415. ></A
  53416. >, and <A
  53417. HREF="#function.array-walk"
  53418. ><B
  53419. CLASS="function"
  53420. >array_walk()</B
  53421. ></A
  53422. >.
  53423.     </P
  53424. ></DIV
  53425. ><H1
  53426. ><A
  53427. NAME="function.array-flip"
  53428. ></A
  53429. >array_flip</H1
  53430. ><DIV
  53431. CLASS="refnamediv"
  53432. ><A
  53433. NAME="AEN9190"
  53434. ></A
  53435. ><P
  53436. >    (PHP 4 , PHP 5)</P
  53437. >array_flip -- Exchanges all keys with their associated values in an array</DIV
  53438. ><DIV
  53439. CLASS="refsect1"
  53440. ><A
  53441. NAME="AEN9193"
  53442. ></A
  53443. ><H2
  53444. >Description</H2
  53445. >array <B
  53446. CLASS="methodname"
  53447. >array_flip</B
  53448. > ( array trans)<BR
  53449. ></BR
  53450. ><P
  53451. >      <B
  53452. CLASS="function"
  53453. >array_flip()</B
  53454. > returns an <A
  53455. HREF="#language.types.array"
  53456. ><B
  53457. CLASS="type"
  53458. >array</B
  53459. ></A
  53460. > in flip
  53461.      order, i.e. keys from <VAR
  53462. CLASS="parameter"
  53463. >trans</VAR
  53464. > become values and values
  53465.      from <VAR
  53466. CLASS="parameter"
  53467. >trans</VAR
  53468. > become keys.
  53469.     </P
  53470. ><P
  53471. >      Note that the values of <VAR
  53472. CLASS="parameter"
  53473. >trans</VAR
  53474. > need to be valid
  53475.      keys, i.e. they need to be either <A
  53476. HREF="#language.types.integer"
  53477. ><B
  53478. CLASS="type"
  53479. >integer</B
  53480. ></A
  53481. > or
  53482.      <A
  53483. HREF="#language.types.string"
  53484. ><B
  53485. CLASS="type"
  53486. >string</B
  53487. ></A
  53488. >. A warning will be emitted if a value has the wrong
  53489.      type, and the key/value pair in question <SPAN
  53490. CLASS="emphasis"
  53491. ><I
  53492. CLASS="emphasis"
  53493. >will not be
  53494.      flipped</I
  53495. ></SPAN
  53496. >.
  53497.     </P
  53498. ><P
  53499. >      If a value has several occurrences, the latest key will be
  53500.      used as its values, and all others will be lost.
  53501.     </P
  53502. ><P
  53503. >      <B
  53504. CLASS="function"
  53505. >array_flip()</B
  53506. > returns <TT
  53507. CLASS="constant"
  53508. ><B
  53509. >FALSE</B
  53510. ></TT
  53511. >
  53512.      if it fails.
  53513.     </P
  53514. ><P
  53515. >      <TABLE
  53516. WIDTH="100%"
  53517. BORDER="0"
  53518. CELLPADDING="0"
  53519. CELLSPACING="0"
  53520. CLASS="EXAMPLE"
  53521. ><TR
  53522. ><TD
  53523. ><DIV
  53524. CLASS="example"
  53525. ><A
  53526. NAME="AEN9216"
  53527. ></A
  53528. ><P
  53529. ><B
  53530. >Example 1. <B
  53531. CLASS="function"
  53532. >array_flip()</B
  53533. > example</B
  53534. ></P
  53535. ><TABLE
  53536. BORDER="0"
  53537. BGCOLOR="#E0E0E0"
  53538. CELLPADDING="5"
  53539. ><TR
  53540. ><TD
  53541. ><PRE
  53542. CLASS="php"
  53543. ><?php
  53544. $trans = array_flip($trans);
  53545. $original = strtr($str, $trans);
  53546. ?></PRE
  53547. ></TD
  53548. ></TR
  53549. ></TABLE
  53550. ></DIV
  53551. ></TD
  53552. ></TR
  53553. ></TABLE
  53554. >
  53555.     </P
  53556. ><P
  53557. >      <TABLE
  53558. WIDTH="100%"
  53559. BORDER="0"
  53560. CELLPADDING="0"
  53561. CELLSPACING="0"
  53562. CLASS="EXAMPLE"
  53563. ><TR
  53564. ><TD
  53565. ><DIV
  53566. CLASS="example"
  53567. ><A
  53568. NAME="AEN9221"
  53569. ></A
  53570. ><P
  53571. ><B
  53572. >Example 2. <B
  53573. CLASS="function"
  53574. >array_flip()</B
  53575. > example : collision</B
  53576. ></P
  53577. ><TABLE
  53578. BORDER="0"
  53579. BGCOLOR="#E0E0E0"
  53580. CELLPADDING="5"
  53581. ><TR
  53582. ><TD
  53583. ><PRE
  53584. CLASS="php"
  53585. ><?php
  53586. $trans = array("a" => 1, "b" => 1, "c" => 2);
  53587. $trans = array_flip($trans);
  53588. print_r($trans);
  53589. ?></PRE
  53590. ></TD
  53591. ></TR
  53592. ></TABLE
  53593. ><P
  53594. >        now <VAR
  53595. CLASS="varname"
  53596. >$trans</VAR
  53597. > is:
  53598.       </P
  53599. ><TABLE
  53600. BORDER="0"
  53601. BGCOLOR="#E0E0E0"
  53602. CELLPADDING="5"
  53603. ><TR
  53604. ><TD
  53605. ><PRE
  53606. CLASS="screen"
  53607. >Array
  53608. (
  53609.     [1] => b
  53610.     [2] => c
  53611. )</PRE
  53612. ></TD
  53613. ></TR
  53614. ></TABLE
  53615. ></DIV
  53616. ></TD
  53617. ></TR
  53618. ></TABLE
  53619. >
  53620.     </P
  53621. ><P
  53622. >      See also <A
  53623. HREF="#function.array-values"
  53624. ><B
  53625. CLASS="function"
  53626. >array_values()</B
  53627. ></A
  53628. >,
  53629.      <A
  53630. HREF="#function.array-keys"
  53631. ><B
  53632. CLASS="function"
  53633. >array_keys()</B
  53634. ></A
  53635. >, and
  53636.      <A
  53637. HREF="#function.array-reverse"
  53638. ><B
  53639. CLASS="function"
  53640. >array_reverse()</B
  53641. ></A
  53642. >.
  53643.     </P
  53644. ></DIV
  53645. ><H1
  53646. ><A
  53647. NAME="function.array-intersect-assoc"
  53648. ></A
  53649. >array_intersect_assoc</H1
  53650. ><DIV
  53651. CLASS="refnamediv"
  53652. ><A
  53653. NAME="AEN9233"
  53654. ></A
  53655. ><P
  53656. >    (PHP 4 >= 4.3.0, PHP 5)</P
  53657. >array_intersect_assoc -- Computes the intersection of arrays with additional index check</DIV
  53658. ><DIV
  53659. CLASS="refsect1"
  53660. ><A
  53661. NAME="AEN9236"
  53662. ></A
  53663. ><H2
  53664. >Description</H2
  53665. >array <B
  53666. CLASS="methodname"
  53667. >array_intersect_assoc</B
  53668. > ( array array1, array array2 [, array ...])<BR
  53669. ></BR
  53670. ><P
  53671. >      <B
  53672. CLASS="function"
  53673. >array_intersect_assoc()</B
  53674. > returns an array
  53675.      containing all the values of <VAR
  53676. CLASS="parameter"
  53677. >array1</VAR
  53678. >
  53679.      that are present in all the arguments. Note that the keys are used in
  53680.      the comparison unlike in <A
  53681. HREF="#function.array-intersect"
  53682. ><B
  53683. CLASS="function"
  53684. >array_intersect()</B
  53685. ></A
  53686. >.
  53687.     </P
  53688. ><P
  53689. >      <TABLE
  53690. WIDTH="100%"
  53691. BORDER="0"
  53692. CELLPADDING="0"
  53693. CELLSPACING="0"
  53694. CLASS="EXAMPLE"
  53695. ><TR
  53696. ><TD
  53697. ><DIV
  53698. CLASS="example"
  53699. ><A
  53700. NAME="AEN9255"
  53701. ></A
  53702. ><P
  53703. ><B
  53704. >Example 1. <B
  53705. CLASS="function"
  53706. >array_intersect_assoc()</B
  53707. > example</B
  53708. ></P
  53709. ><TABLE
  53710. BORDER="0"
  53711. BGCOLOR="#E0E0E0"
  53712. CELLPADDING="5"
  53713. ><TR
  53714. ><TD
  53715. ><PRE
  53716. CLASS="php"
  53717. ><?php
  53718. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
  53719. $array2 = array("a" => "green", "yellow", "red");
  53720. $result_array = array_intersect_assoc($array1, $array2);
  53721. ?></PRE
  53722. ></TD
  53723. ></TR
  53724. ></TABLE
  53725. ><P
  53726. >        $result_array will look like:
  53727.       </P
  53728. ><TABLE
  53729. BORDER="0"
  53730. BGCOLOR="#E0E0E0"
  53731. CELLPADDING="5"
  53732. ><TR
  53733. ><TD
  53734. ><PRE
  53735. CLASS="screen"
  53736. >Array
  53737. (
  53738.     [a] => green
  53739. )</PRE
  53740. ></TD
  53741. ></TR
  53742. ></TABLE
  53743. ></DIV
  53744. ></TD
  53745. ></TR
  53746. ></TABLE
  53747. >
  53748.     </P
  53749. ><P
  53750. >      In our example you see that only the pair <VAR
  53751. CLASS="literal"
  53752. >"a" =>
  53753.      "green"</VAR
  53754. > is present in both arrays and thus is returned. 
  53755.      The value <VAR
  53756. CLASS="literal"
  53757. >"red"</VAR
  53758. > is not returned because in 
  53759.      <VAR
  53760. CLASS="varname"
  53761. >$array1</VAR
  53762. > its key is <VAR
  53763. CLASS="literal"
  53764. >0</VAR
  53765. > while
  53766.      the key of "red" in <VAR
  53767. CLASS="varname"
  53768. >$array2</VAR
  53769. > is 
  53770.      <VAR
  53771. CLASS="literal"
  53772. >1</VAR
  53773. >.
  53774.     </P
  53775. ><P
  53776. >      The two values from the <VAR
  53777. CLASS="literal"
  53778. >key => value</VAR
  53779. > pairs are
  53780.      considered equal only if 
  53781.      <VAR
  53782. CLASS="literal"
  53783. >(string) $elem1 === (string) $elem2 </VAR
  53784. >. In other words
  53785.      a strict type check is executed so the string representation must be
  53786.      the same. 
  53787.      
  53788.     </P
  53789. ><P
  53790. >      See also <A
  53791. HREF="#function.array-intersect"
  53792. ><B
  53793. CLASS="function"
  53794. >array_intersect()</B
  53795. ></A
  53796. >,
  53797.      <A
  53798. HREF="#function.array-uintersect-assoc"
  53799. ><B
  53800. CLASS="function"
  53801. >array_uintersect_assoc()</B
  53802. ></A
  53803. >,
  53804.      <A
  53805. HREF="#function.array-intersect-uassoc"
  53806. ><B
  53807. CLASS="function"
  53808. >array_intersect_uassoc()</B
  53809. ></A
  53810. >,
  53811.      <A
  53812. HREF="#function.array-uintersect-uassoc"
  53813. ><B
  53814. CLASS="function"
  53815. >array_uintersect_uassoc()</B
  53816. ></A
  53817. >,
  53818.      <A
  53819. HREF="#function.array-diff"
  53820. ><B
  53821. CLASS="function"
  53822. >array_diff()</B
  53823. ></A
  53824. > and
  53825.      <A
  53826. HREF="#function.array-diff-assoc"
  53827. ><B
  53828. CLASS="function"
  53829. >array_diff_assoc()</B
  53830. ></A
  53831. >.
  53832.     </P
  53833. ></DIV
  53834. ><H1
  53835. ><A
  53836. NAME="function.array-intersect-key"
  53837. ></A
  53838. >array_intersect_key</H1
  53839. ><DIV
  53840. CLASS="refnamediv"
  53841. ><A
  53842. NAME="AEN9279"
  53843. ></A
  53844. ><P
  53845. >    (no version information, might be only in CVS)</P
  53846. >array_intersect_key -- Computes the intersection of arrays using keys for comparison</DIV
  53847. ><DIV
  53848. CLASS="refsect1"
  53849. ><A
  53850. NAME="AEN9282"
  53851. ></A
  53852. ><H2
  53853. >Description</H2
  53854. >array <B
  53855. CLASS="methodname"
  53856. >array_intersect_key</B
  53857. > ( array array1, array array2 [, array ...])<BR
  53858. ></BR
  53859. ><P
  53860. >      <B
  53861. CLASS="function"
  53862. >array_intersect_key()</B
  53863. > returns an array
  53864.      containing all the values of <VAR
  53865. CLASS="parameter"
  53866. >array1</VAR
  53867. >
  53868.      which have matching keys that are present in all the arguments.
  53869.     </P
  53870. ><P
  53871. >      <TABLE
  53872. WIDTH="100%"
  53873. BORDER="0"
  53874. CELLPADDING="0"
  53875. CELLSPACING="0"
  53876. CLASS="EXAMPLE"
  53877. ><TR
  53878. ><TD
  53879. ><DIV
  53880. CLASS="example"
  53881. ><A
  53882. NAME="AEN9300"
  53883. ></A
  53884. ><P
  53885. ><B
  53886. >Example 1. <B
  53887. CLASS="function"
  53888. >array_intersect_key()</B
  53889. > example</B
  53890. ></P
  53891. ><TABLE
  53892. BORDER="0"
  53893. BGCOLOR="#E0E0E0"
  53894. CELLPADDING="5"
  53895. ><TR
  53896. ><TD
  53897. ><PRE
  53898. CLASS="php"
  53899. ><?php
  53900. $array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
  53901. $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
  53902.  
  53903. var_dump(array_intersect_key($array1, $array2));
  53904. ?></PRE
  53905. ></TD
  53906. ></TR
  53907. ></TABLE
  53908. ><P
  53909. >The above example will output:</P
  53910. ><TABLE
  53911. BORDER="0"
  53912. BGCOLOR="#E0E0E0"
  53913. CELLPADDING="5"
  53914. ><TR
  53915. ><TD
  53916. ><PRE
  53917. CLASS="screen"
  53918. >array(2) {
  53919.   ["blue"]=>
  53920.   int(1)
  53921.   ["green"]=>
  53922.   int(3)
  53923. })</PRE
  53924. ></TD
  53925. ></TR
  53926. ></TABLE
  53927. ></DIV
  53928. ></TD
  53929. ></TR
  53930. ></TABLE
  53931. >
  53932.     </P
  53933. ><P
  53934. >      In our example you see that only the keys <VAR
  53935. CLASS="literal"
  53936. >'blue'</VAR
  53937. >
  53938.      and <VAR
  53939. CLASS="literal"
  53940. >'green'</VAR
  53941. > are present in both arrays and thus
  53942.      returned. Also notice that the values for the keys
  53943.      <VAR
  53944. CLASS="literal"
  53945. >'blue'</VAR
  53946. > and <VAR
  53947. CLASS="literal"
  53948. >'green'</VAR
  53949. > differ between
  53950.      the two arrays. A match still occurs because only the keys are checked.
  53951.      The values returned are those of <VAR
  53952. CLASS="parameter"
  53953. >array1</VAR
  53954. >.
  53955.     </P
  53956. ><P
  53957. >      The two keys from the <VAR
  53958. CLASS="literal"
  53959. >key => value</VAR
  53960. > pairs are
  53961.      considered equal only if 
  53962.      <VAR
  53963. CLASS="literal"
  53964. >(string) $key1 === (string) $key2 </VAR
  53965. >. In other words
  53966.      a strict type check is executed so the string representation must be
  53967.      the same.
  53968.     </P
  53969. ><P
  53970. >      See also <A
  53971. HREF="#function.array-diff"
  53972. ><B
  53973. CLASS="function"
  53974. >array_diff()</B
  53975. ></A
  53976. >,
  53977.      <A
  53978. HREF="#function.array-udiff"
  53979. ><B
  53980. CLASS="function"
  53981. >array_udiff()</B
  53982. ></A
  53983. >
  53984.      <A
  53985. HREF="#function.array-diff-assoc"
  53986. ><B
  53987. CLASS="function"
  53988. >array_diff_assoc()</B
  53989. ></A
  53990. >,
  53991.      <A
  53992. HREF="#function.array-diff-uassoc"
  53993. ><B
  53994. CLASS="function"
  53995. >array_diff_uassoc()</B
  53996. ></A
  53997. >,
  53998.      <A
  53999. HREF="#function.array-udiff-assoc"
  54000. ><B
  54001. CLASS="function"
  54002. >array_udiff_assoc()</B
  54003. ></A
  54004. >,
  54005.      <A
  54006. HREF="#function.array-udiff-uassoc"
  54007. ><B
  54008. CLASS="function"
  54009. >array_udiff_uassoc()</B
  54010. ></A
  54011. >,
  54012.      <A
  54013. HREF="#function.array-diff-key"
  54014. ><B
  54015. CLASS="function"
  54016. >array_diff_key()</B
  54017. ></A
  54018. >,
  54019.      <A
  54020. HREF="#function.array-diff-ukey"
  54021. ><B
  54022. CLASS="function"
  54023. >array_diff_ukey()</B
  54024. ></A
  54025. >,
  54026.      <A
  54027. HREF="#function.array-intersect"
  54028. ><B
  54029. CLASS="function"
  54030. >array_intersect()</B
  54031. ></A
  54032. >,
  54033.      <A
  54034. HREF="#function.array-intersect-assoc"
  54035. ><B
  54036. CLASS="function"
  54037. >array_intersect_assoc()</B
  54038. ></A
  54039. >,
  54040.      <A
  54041. HREF="#function.array-intersect-uassoc"
  54042. ><B
  54043. CLASS="function"
  54044. >array_intersect_uassoc()</B
  54045. ></A
  54046. > and
  54047.      <A
  54048. HREF="#function.array-intersect-ukey"
  54049. ><B
  54050. CLASS="function"
  54051. >array_intersect_ukey()</B
  54052. ></A
  54053. >.
  54054.     </P
  54055. ></DIV
  54056. ><H1
  54057. ><A
  54058. NAME="function.array-intersect-uassoc"
  54059. ></A
  54060. >array_intersect_uassoc</H1
  54061. ><DIV
  54062. CLASS="refnamediv"
  54063. ><A
  54064. NAME="AEN9329"
  54065. ></A
  54066. ><P
  54067. >    (PHP 5)</P
  54068. >array_intersect_uassoc -- Computes the intersection of arrays with additional index check, compares indexes by a callback function</DIV
  54069. ><DIV
  54070. CLASS="refsect1"
  54071. ><A
  54072. NAME="AEN9332"
  54073. ></A
  54074. ><H2
  54075. >Description</H2
  54076. >array <B
  54077. CLASS="methodname"
  54078. >array_intersect_uassoc</B
  54079. > ( array array1, array array2 [, array ..., callback key_compare_func])<BR
  54080. ></BR
  54081. ><P
  54082. >      <B
  54083. CLASS="function"
  54084. >array_intersect_uassoc()</B
  54085. > returns an array
  54086.      containing all the values of <VAR
  54087. CLASS="parameter"
  54088. >array1</VAR
  54089. >
  54090.      that are present in all the arguments. Note that the keys are used in
  54091.      the comparison unlike in <A
  54092. HREF="#function.array-intersect"
  54093. ><B
  54094. CLASS="function"
  54095. >array_intersect()</B
  54096. ></A
  54097. >.
  54098.     </P
  54099. ><P
  54100. >      The index comparison is done by a user supplied callback function.
  54101.      It must return an integer less than, equal to, or greater than zero
  54102.      if the first argument is considered to be respectively less than,
  54103.      equal to, or greater than the second.
  54104.     </P
  54105. ><P
  54106. >      <TABLE
  54107. WIDTH="100%"
  54108. BORDER="0"
  54109. CELLPADDING="0"
  54110. CELLSPACING="0"
  54111. CLASS="EXAMPLE"
  54112. ><TR
  54113. ><TD
  54114. ><DIV
  54115. CLASS="example"
  54116. ><A
  54117. NAME="AEN9355"
  54118. ></A
  54119. ><P
  54120. ><B
  54121. >Example 1. <B
  54122. CLASS="function"
  54123. >array_intersect_uassoc()</B
  54124. > example</B
  54125. ></P
  54126. ><TABLE
  54127. BORDER="0"
  54128. BGCOLOR="#E0E0E0"
  54129. CELLPADDING="5"
  54130. ><TR
  54131. ><TD
  54132. ><PRE
  54133. CLASS="php"
  54134. ><?php
  54135. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
  54136. $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
  54137.  
  54138. print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));
  54139. ?></PRE
  54140. ></TD
  54141. ></TR
  54142. ></TABLE
  54143. ><P
  54144. >The above example will output:</P
  54145. ><TABLE
  54146. BORDER="0"
  54147. BGCOLOR="#E0E0E0"
  54148. CELLPADDING="5"
  54149. ><TR
  54150. ><TD
  54151. ><PRE
  54152. CLASS="screen"
  54153. >Array
  54154. (
  54155.     [b] => brown
  54156. )</PRE
  54157. ></TD
  54158. ></TR
  54159. ></TABLE
  54160. ></DIV
  54161. ></TD
  54162. ></TR
  54163. ></TABLE
  54164. >
  54165.     </P
  54166. ><P
  54167. >      See also <A
  54168. HREF="#function.array-intersect"
  54169. ><B
  54170. CLASS="function"
  54171. >array_intersect()</B
  54172. ></A
  54173. >,
  54174.      <A
  54175. HREF="#function.array-intersect-assoc"
  54176. ><B
  54177. CLASS="function"
  54178. >array_intersect_assoc()</B
  54179. ></A
  54180. >,
  54181.      <A
  54182. HREF="#function.array-uintersect-assoc"
  54183. ><B
  54184. CLASS="function"
  54185. >array_uintersect_assoc()</B
  54186. ></A
  54187. >,
  54188.      <A
  54189. HREF="#function.array-uintersect-uassoc"
  54190. ><B
  54191. CLASS="function"
  54192. >array_uintersect_uassoc()</B
  54193. ></A
  54194. >,
  54195.      <A
  54196. HREF="#function.array-intersect-key"
  54197. ><B
  54198. CLASS="function"
  54199. >array_intersect_key()</B
  54200. ></A
  54201. > and
  54202.      <A
  54203. HREF="#function.array-intersect-ukey"
  54204. ><B
  54205. CLASS="function"
  54206. >array_intersect_ukey()</B
  54207. ></A
  54208. >.
  54209.     </P
  54210. ></DIV
  54211. ><H1
  54212. ><A
  54213. NAME="function.array-intersect-ukey"
  54214. ></A
  54215. >array_intersect_ukey</H1
  54216. ><DIV
  54217. CLASS="refnamediv"
  54218. ><A
  54219. NAME="AEN9369"
  54220. ></A
  54221. ><P
  54222. >    (no version information, might be only in CVS)</P
  54223. >array_intersect_ukey -- Computes the intersection of arrays using a callback function on the keys for comparison</DIV
  54224. ><DIV
  54225. CLASS="refsect1"
  54226. ><A
  54227. NAME="AEN9372"
  54228. ></A
  54229. ><H2
  54230. >Description</H2
  54231. >array <B
  54232. CLASS="methodname"
  54233. >array_intersect_ukey</B
  54234. > ( array array1, array array2 [, array ..., callback key_compare_func])<BR
  54235. ></BR
  54236. ><P
  54237. >      <B
  54238. CLASS="function"
  54239. >array_intersect_ukey()</B
  54240. > returns an array
  54241.      containing all the values of <VAR
  54242. CLASS="parameter"
  54243. >array1</VAR
  54244. >
  54245.      which have matching keys that are present in all the arguments.
  54246.     </P
  54247. ><P
  54248. >      This comparison is done by a user supplied callback function.
  54249.      It must return an integer less than, equal to, or greater than zero if the
  54250.      first key is considered to be respectively less than, equal to, or
  54251.      greater than the second.
  54252.     </P
  54253. ><P
  54254. >      <TABLE
  54255. WIDTH="100%"
  54256. BORDER="0"
  54257. CELLPADDING="0"
  54258. CELLSPACING="0"
  54259. CLASS="EXAMPLE"
  54260. ><TR
  54261. ><TD
  54262. ><DIV
  54263. CLASS="example"
  54264. ><A
  54265. NAME="AEN9394"
  54266. ></A
  54267. ><P
  54268. ><B
  54269. >Example 1. <B
  54270. CLASS="function"
  54271. >array_intersect_ukey()</B
  54272. > example</B
  54273. ></P
  54274. ><TABLE
  54275. BORDER="0"
  54276. BGCOLOR="#E0E0E0"
  54277. CELLPADDING="5"
  54278. ><TR
  54279. ><TD
  54280. ><PRE
  54281. CLASS="php"
  54282. ><?php
  54283. function key_compare_func($key1, $key2)
  54284. {
  54285.     if ($key1 == $key2)
  54286.         return 0;
  54287.     else if ($key1 > $key2)
  54288.         return 1;
  54289.     else
  54290.         return -1;
  54291. }
  54292.  
  54293. $array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
  54294. $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
  54295.  
  54296. var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
  54297. ?></PRE
  54298. ></TD
  54299. ></TR
  54300. ></TABLE
  54301. ><P
  54302. >The above example will output:</P
  54303. ><TABLE
  54304. BORDER="0"
  54305. BGCOLOR="#E0E0E0"
  54306. CELLPADDING="5"
  54307. ><TR
  54308. ><TD
  54309. ><PRE
  54310. CLASS="screen"
  54311. >array(2) {
  54312.   ["blue"]=>
  54313.   int(1)
  54314.   ["green"]=>
  54315.   int(3)
  54316. })</PRE
  54317. ></TD
  54318. ></TR
  54319. ></TABLE
  54320. ></DIV
  54321. ></TD
  54322. ></TR
  54323. ></TABLE
  54324. >
  54325.     </P
  54326. ><P
  54327. >      In our example you see that only the keys <VAR
  54328. CLASS="literal"
  54329. >'blue'</VAR
  54330. >
  54331.      and <VAR
  54332. CLASS="literal"
  54333. >'green'</VAR
  54334. > are present in both arrays and thus
  54335.      returned. Also notice that the values for the keys
  54336.      <VAR
  54337. CLASS="literal"
  54338. >'blue'</VAR
  54339. > and <VAR
  54340. CLASS="literal"
  54341. >'green'</VAR
  54342. > differ between
  54343.      the two arrays. A match still occurs because only the keys are checked.
  54344.      The values returned are those of <VAR
  54345. CLASS="parameter"
  54346. >array1</VAR
  54347. >.
  54348.     </P
  54349. ><P
  54350. >      The two keys from the <VAR
  54351. CLASS="literal"
  54352. >key => value</VAR
  54353. > pairs are
  54354.      considered equal only if 
  54355.      <VAR
  54356. CLASS="literal"
  54357. >(string) $key1 === (string) $key2 </VAR
  54358. >. In other words
  54359.      a strict type check is executed so the string representation must be
  54360.      the same.
  54361.     </P
  54362. ><P
  54363. >      See also <A
  54364. HREF="#function.array-diff"
  54365. ><B
  54366. CLASS="function"
  54367. >array_diff()</B
  54368. ></A
  54369. >,
  54370.      <A
  54371. HREF="#function.array-udiff"
  54372. ><B
  54373. CLASS="function"
  54374. >array_udiff()</B
  54375. ></A
  54376. >
  54377.      <A
  54378. HREF="#function.array-diff-assoc"
  54379. ><B
  54380. CLASS="function"
  54381. >array_diff_assoc()</B
  54382. ></A
  54383. >,
  54384.      <A
  54385. HREF="#function.array-diff-uassoc"
  54386. ><B
  54387. CLASS="function"
  54388. >array_diff_uassoc()</B
  54389. ></A
  54390. >,
  54391.      <A
  54392. HREF="#function.array-udiff-assoc"
  54393. ><B
  54394. CLASS="function"
  54395. >array_udiff_assoc()</B
  54396. ></A
  54397. >,
  54398.      <A
  54399. HREF="#function.array-udiff-uassoc"
  54400. ><B
  54401. CLASS="function"
  54402. >array_udiff_uassoc()</B
  54403. ></A
  54404. >,
  54405.      <A
  54406. HREF="#function.array-diff-key"
  54407. ><B
  54408. CLASS="function"
  54409. >array_diff_key()</B
  54410. ></A
  54411. >,
  54412.      <A
  54413. HREF="#function.array-diff-ukey"
  54414. ><B
  54415. CLASS="function"
  54416. >array_diff_ukey()</B
  54417. ></A
  54418. >,
  54419.      <A
  54420. HREF="#function.array-intersect"
  54421. ><B
  54422. CLASS="function"
  54423. >array_intersect()</B
  54424. ></A
  54425. >,
  54426.      <A
  54427. HREF="#function.array-intersect-assoc"
  54428. ><B
  54429. CLASS="function"
  54430. >array_intersect_assoc()</B
  54431. ></A
  54432. >,
  54433.      <A
  54434. HREF="#function.array-intersect-uassoc"
  54435. ><B
  54436. CLASS="function"
  54437. >array_intersect_uassoc()</B
  54438. ></A
  54439. > and
  54440.      <A
  54441. HREF="#function.array-intersect-key"
  54442. ><B
  54443. CLASS="function"
  54444. >array_intersect_key()</B
  54445. ></A
  54446. >.
  54447.     </P
  54448. ></DIV
  54449. ><H1
  54450. ><A
  54451. NAME="function.array-intersect"
  54452. ></A
  54453. >array_intersect</H1
  54454. ><DIV
  54455. CLASS="refnamediv"
  54456. ><A
  54457. NAME="AEN9423"
  54458. ></A
  54459. ><P
  54460. >    (PHP 4 >= 4.0.1, PHP 5)</P
  54461. >array_intersect -- Computes the intersection of arrays</DIV
  54462. ><DIV
  54463. CLASS="refsect1"
  54464. ><A
  54465. NAME="AEN9426"
  54466. ></A
  54467. ><H2
  54468. >Description</H2
  54469. >array <B
  54470. CLASS="methodname"
  54471. >array_intersect</B
  54472. > ( array array1, array array2 [, array ...])<BR
  54473. ></BR
  54474. ><P
  54475. >      <B
  54476. CLASS="function"
  54477. >array_intersect()</B
  54478. > returns an array
  54479.      containing all the values of <VAR
  54480. CLASS="parameter"
  54481. >array1</VAR
  54482. >
  54483.      that are present in all the arguments.
  54484.      Note that keys are preserved.
  54485.     </P
  54486. ><P
  54487. >      <TABLE
  54488. WIDTH="100%"
  54489. BORDER="0"
  54490. CELLPADDING="0"
  54491. CELLSPACING="0"
  54492. CLASS="EXAMPLE"
  54493. ><TR
  54494. ><TD
  54495. ><DIV
  54496. CLASS="example"
  54497. ><A
  54498. NAME="AEN9444"
  54499. ></A
  54500. ><P
  54501. ><B
  54502. >Example 1. <B
  54503. CLASS="function"
  54504. >array_intersect()</B
  54505. > example</B
  54506. ></P
  54507. ><TABLE
  54508. BORDER="0"
  54509. BGCOLOR="#E0E0E0"
  54510. CELLPADDING="5"
  54511. ><TR
  54512. ><TD
  54513. ><PRE
  54514. CLASS="php"
  54515. ><?php
  54516. $array1 = array("a" => "green", "red", "blue");
  54517. $array2 = array("b" => "green", "yellow", "red");
  54518. $result = array_intersect($array1, $array2);
  54519. ?></PRE
  54520. ></TD
  54521. ></TR
  54522. ></TABLE
  54523. ><P
  54524. >        This makes <VAR
  54525. CLASS="varname"
  54526. >$result</VAR
  54527. > have
  54528.       </P
  54529. ><TABLE
  54530. BORDER="0"
  54531. BGCOLOR="#E0E0E0"
  54532. CELLPADDING="5"
  54533. ><TR
  54534. ><TD
  54535. ><PRE
  54536. CLASS="php"
  54537. >Array
  54538. (
  54539.     [a] => green
  54540.     [0] => red
  54541. )</PRE
  54542. ></TD
  54543. ></TR
  54544. ></TABLE
  54545. ></DIV
  54546. ></TD
  54547. ></TR
  54548. ></TABLE
  54549. >
  54550.     </P
  54551. ><DIV
  54552. CLASS="note"
  54553. ><BLOCKQUOTE
  54554. CLASS="note"
  54555. ><P
  54556. ><B
  54557. >Note: </B
  54558. >
  54559.       Two elements are considered equal if and only if
  54560.       <VAR
  54561. CLASS="literal"
  54562. >(string) $elem1 === (string) $elem2</VAR
  54563. >. In words:
  54564.       when the string representation is the same.
  54565.       
  54566.      </P
  54567. ></BLOCKQUOTE
  54568. ></DIV
  54569. ><P
  54570. >      See also <A
  54571. HREF="#function.array-intersect-assoc"
  54572. ><B
  54573. CLASS="function"
  54574. >array_intersect_assoc()</B
  54575. ></A
  54576. >,
  54577.      <A
  54578. HREF="#function.array-diff"
  54579. ><B
  54580. CLASS="function"
  54581. >array_diff()</B
  54582. ></A
  54583. >, and
  54584.      <A
  54585. HREF="#function.array-diff-assoc"
  54586. ><B
  54587. CLASS="function"
  54588. >array_diff_assoc()</B
  54589. ></A
  54590. >.
  54591.     </P
  54592. ></DIV
  54593. ><H1
  54594. ><A
  54595. NAME="function.array-key-exists"
  54596. ></A
  54597. >array_key_exists</H1
  54598. ><DIV
  54599. CLASS="refnamediv"
  54600. ><A
  54601. NAME="AEN9459"
  54602. ></A
  54603. ><P
  54604. >    (PHP 4 >= 4.1.0, PHP 5)</P
  54605. >array_key_exists -- Checks if the given key or index exists in the array</DIV
  54606. ><DIV
  54607. CLASS="refsect1"
  54608. ><A
  54609. NAME="AEN9462"
  54610. ></A
  54611. ><H2
  54612. >Description</H2
  54613. >bool <B
  54614. CLASS="methodname"
  54615. >array_key_exists</B
  54616. > ( mixed key, array search)<BR
  54617. ></BR
  54618. ><P
  54619. >      <B
  54620. CLASS="function"
  54621. >array_key_exists()</B
  54622. > returns <TT
  54623. CLASS="constant"
  54624. ><B
  54625. >TRUE</B
  54626. ></TT
  54627. > if the
  54628.      given <VAR
  54629. CLASS="parameter"
  54630. >key</VAR
  54631. > is set in the array.
  54632.      <VAR
  54633. CLASS="parameter"
  54634. >key</VAR
  54635. > can be any value possible
  54636.      for an array index. <B
  54637. CLASS="function"
  54638. >array_key_exists()</B
  54639. > also works
  54640.      on objects.
  54641.     </P
  54642. ><P
  54643. >      <TABLE
  54644. WIDTH="100%"
  54645. BORDER="0"
  54646. CELLPADDING="0"
  54647. CELLSPACING="0"
  54648. CLASS="EXAMPLE"
  54649. ><TR
  54650. ><TD
  54651. ><DIV
  54652. CLASS="example"
  54653. ><A
  54654. NAME="AEN9480"
  54655. ></A
  54656. ><P
  54657. ><B
  54658. >Example 1. <B
  54659. CLASS="function"
  54660. >array_key_exists()</B
  54661. > example</B
  54662. ></P
  54663. ><TABLE
  54664. BORDER="0"
  54665. BGCOLOR="#E0E0E0"
  54666. CELLPADDING="5"
  54667. ><TR
  54668. ><TD
  54669. ><PRE
  54670. CLASS="php"
  54671. ><?php
  54672. $search_array = array('first' => 1, 'second' => 4);
  54673. if (array_key_exists('first', $search_array)) {
  54674.     echo "The 'first' element is in the array";
  54675. }
  54676. ?></PRE
  54677. ></TD
  54678. ></TR
  54679. ></TABLE
  54680. ></DIV
  54681. ></TD
  54682. ></TR
  54683. ></TABLE
  54684. >
  54685.     </P
  54686. ><DIV
  54687. CLASS="note"
  54688. ><BLOCKQUOTE
  54689. CLASS="note"
  54690. ><P
  54691. ><B
  54692. >Note: </B
  54693. >
  54694.       The name of this function is <B
  54695. CLASS="function"
  54696. >key_exists()</B
  54697. >
  54698.       in PHP 4.0.6.
  54699.      </P
  54700. ></BLOCKQUOTE
  54701. ></DIV
  54702. ><TABLE
  54703. WIDTH="100%"
  54704. BORDER="0"
  54705. CELLPADDING="0"
  54706. CELLSPACING="0"
  54707. CLASS="EXAMPLE"
  54708. ><TR
  54709. ><TD
  54710. ><DIV
  54711. CLASS="example"
  54712. ><A
  54713. NAME="AEN9487"
  54714. ></A
  54715. ><P
  54716. ><B
  54717. >Example 2. <B
  54718. CLASS="function"
  54719. >array_key_exists()</B
  54720. > vs <A
  54721. HREF="#function.isset"
  54722. ><B
  54723. CLASS="function"
  54724. >isset()</B
  54725. ></A
  54726. ></B
  54727. ></P
  54728. ><P
  54729. >       <A
  54730. HREF="#function.isset"
  54731. ><B
  54732. CLASS="function"
  54733. >isset()</B
  54734. ></A
  54735. > does not return <TT
  54736. CLASS="constant"
  54737. ><B
  54738. >TRUE</B
  54739. ></TT
  54740. > for array keys
  54741.       that correspond to a <TT
  54742. CLASS="constant"
  54743. ><B
  54744. >NULL</B
  54745. ></TT
  54746. > value, while
  54747.       <B
  54748. CLASS="function"
  54749. >array_key_exists()</B
  54750. > does.
  54751.      </P
  54752. ><TABLE
  54753. BORDER="0"
  54754. BGCOLOR="#E0E0E0"
  54755. CELLPADDING="5"
  54756. ><TR
  54757. ><TD
  54758. ><PRE
  54759. CLASS="php"
  54760. ><?php
  54761. $search_array = array('first' => null, 'second' => 4);
  54762.  
  54763. // returns false
  54764. isset($search_array['first']);
  54765.  
  54766. // returns true
  54767. array_key_exists('first', $search_array);
  54768. ?></PRE
  54769. ></TD
  54770. ></TR
  54771. ></TABLE
  54772. ></DIV
  54773. ></TD
  54774. ></TR
  54775. ></TABLE
  54776. ><P
  54777. >      See also <A
  54778. HREF="#function.isset"
  54779. ><B
  54780. CLASS="function"
  54781. >isset()</B
  54782. ></A
  54783. >, 
  54784.      <A
  54785. HREF="#function.array-keys"
  54786. ><B
  54787. CLASS="function"
  54788. >array_keys()</B
  54789. ></A
  54790. >, and
  54791.      <A
  54792. HREF="#function.in-array"
  54793. ><B
  54794. CLASS="function"
  54795. >in_array()</B
  54796. ></A
  54797. >.
  54798.     </P
  54799. ></DIV
  54800. ><H1
  54801. ><A
  54802. NAME="function.array-keys"
  54803. ></A
  54804. >array_keys</H1
  54805. ><DIV
  54806. CLASS="refnamediv"
  54807. ><A
  54808. NAME="AEN9502"
  54809. ></A
  54810. ><P
  54811. >    (PHP 4 , PHP 5)</P
  54812. >array_keys -- Return all the keys of an array</DIV
  54813. ><DIV
  54814. CLASS="refsect1"
  54815. ><A
  54816. NAME="AEN9505"
  54817. ></A
  54818. ><H2
  54819. >Description</H2
  54820. >array <B
  54821. CLASS="methodname"
  54822. >array_keys</B
  54823. > ( array input [, mixed search_value [, bool strict]])<BR
  54824. ></BR
  54825. ><P
  54826. >      <B
  54827. CLASS="function"
  54828. >array_keys()</B
  54829. > returns the keys, numeric and
  54830.      string, from the <VAR
  54831. CLASS="parameter"
  54832. >input</VAR
  54833. > array.
  54834.     </P
  54835. ><P
  54836. >      If the optional <VAR
  54837. CLASS="parameter"
  54838. >search_value</VAR
  54839. > is specified,
  54840.      then only the keys for that value are returned. Otherwise, all
  54841.      the keys from the <VAR
  54842. CLASS="parameter"
  54843. >input</VAR
  54844. > are returned.
  54845.      As of PHP 5, you can use <VAR
  54846. CLASS="parameter"
  54847. >strict</VAR
  54848. > parameter for
  54849.      comparison including type (===).
  54850.     </P
  54851. ><P
  54852. >      <TABLE
  54853. WIDTH="100%"
  54854. BORDER="0"
  54855. CELLPADDING="0"
  54856. CELLSPACING="0"
  54857. CLASS="EXAMPLE"
  54858. ><TR
  54859. ><TD
  54860. ><DIV
  54861. CLASS="example"
  54862. ><A
  54863. NAME="AEN9527"
  54864. ></A
  54865. ><P
  54866. ><B
  54867. >Example 1. <B
  54868. CLASS="function"
  54869. >array_keys()</B
  54870. > example</B
  54871. ></P
  54872. ><TABLE
  54873. BORDER="0"
  54874. BGCOLOR="#E0E0E0"
  54875. CELLPADDING="5"
  54876. ><TR
  54877. ><TD
  54878. ><PRE
  54879. CLASS="php"
  54880. ><?php
  54881. $array = array(0 => 100, "color" => "red");
  54882. print_r(array_keys($array));
  54883.  
  54884. $array = array("blue", "red", "green", "blue", "blue");
  54885. print_r(array_keys($array, "blue"));
  54886.  
  54887. $array = array("color" => array("blue", "red", "green"),
  54888.                "size"  => array("small", "medium", "large"));
  54889. print_r(array_keys($array));
  54890. ?></PRE
  54891. ></TD
  54892. ></TR
  54893. ></TABLE
  54894. ><P
  54895. >The above example will output:</P
  54896. ><TABLE
  54897. BORDER="0"
  54898. BGCOLOR="#E0E0E0"
  54899. CELLPADDING="5"
  54900. ><TR
  54901. ><TD
  54902. ><PRE
  54903. CLASS="screen"
  54904. >Array
  54905. (
  54906.     [0] => 0
  54907.     [1] => color
  54908. )
  54909. Array
  54910. (
  54911.     [0] => 0
  54912.     [1] => 3
  54913.     [2] => 4
  54914. )
  54915. Array
  54916. (
  54917.     [0] => color
  54918.     [1] => size
  54919. )</PRE
  54920. ></TD
  54921. ></TR
  54922. ></TABLE
  54923. ></DIV
  54924. ></TD
  54925. ></TR
  54926. ></TABLE
  54927. >
  54928.     </P
  54929. ><P
  54930. >      See also <A
  54931. HREF="#function.array-values"
  54932. ><B
  54933. CLASS="function"
  54934. >array_values()</B
  54935. ></A
  54936. > and
  54937.      <A
  54938. HREF="#function.array-key-exists"
  54939. ><B
  54940. CLASS="function"
  54941. >array_key_exists()</B
  54942. ></A
  54943. >.
  54944.     </P
  54945. ></DIV
  54946. ><H1
  54947. ><A
  54948. NAME="function.array-map"
  54949. ></A
  54950. >array_map</H1
  54951. ><DIV
  54952. CLASS="refnamediv"
  54953. ><A
  54954. NAME="AEN9537"
  54955. ></A
  54956. ><P
  54957. >    (PHP 4 >= 4.0.6, PHP 5)</P
  54958. >array_map -- 
  54959.      Applies the callback to the elements of the given arrays
  54960.     </DIV
  54961. ><DIV
  54962. CLASS="refsect1"
  54963. ><A
  54964. NAME="AEN9540"
  54965. ></A
  54966. ><H2
  54967. >Description</H2
  54968. >array <B
  54969. CLASS="methodname"
  54970. >array_map</B
  54971. > ( callback callback, array arr1 [, array ...])<BR
  54972. ></BR
  54973. ><P
  54974. >      <B
  54975. CLASS="function"
  54976. >array_map()</B
  54977. > returns an array containing all
  54978.      the elements of <VAR
  54979. CLASS="parameter"
  54980. >arr1</VAR
  54981. > after applying the
  54982.      <VAR
  54983. CLASS="parameter"
  54984. >callback</VAR
  54985. > function to each one.
  54986.      The number of parameters that the <VAR
  54987. CLASS="parameter"
  54988. >callback</VAR
  54989. >
  54990.      function accepts
  54991.      should match the number of arrays
  54992.      passed to the <B
  54993. CLASS="function"
  54994. >array_map()</B
  54995. >
  54996.     </P
  54997. ><P
  54998. >      <TABLE
  54999. WIDTH="100%"
  55000. BORDER="0"
  55001. CELLPADDING="0"
  55002. CELLSPACING="0"
  55003. CLASS="EXAMPLE"
  55004. ><TR
  55005. ><TD
  55006. ><DIV
  55007. CLASS="example"
  55008. ><A
  55009. NAME="AEN9561"
  55010. ></A
  55011. ><P
  55012. ><B
  55013. >Example 1. <B
  55014. CLASS="function"
  55015. >array_map()</B
  55016. > example</B
  55017. ></P
  55018. ><TABLE
  55019. BORDER="0"
  55020. BGCOLOR="#E0E0E0"
  55021. CELLPADDING="5"
  55022. ><TR
  55023. ><TD
  55024. ><PRE
  55025. CLASS="php"
  55026. ><?php
  55027. function cube($n) 
  55028. {
  55029.     return($n * $n * $n);
  55030. }
  55031.  
  55032. $a = array(1, 2, 3, 4, 5);
  55033. $b = array_map("cube", $a);
  55034. print_r($b);
  55035. ?></PRE
  55036. ></TD
  55037. ></TR
  55038. ></TABLE
  55039. ><P
  55040. >        This makes <VAR
  55041. CLASS="varname"
  55042. >$b</VAR
  55043. > have:
  55044.       </P
  55045. ><TABLE
  55046. BORDER="0"
  55047. BGCOLOR="#E0E0E0"
  55048. CELLPADDING="5"
  55049. ><TR
  55050. ><TD
  55051. ><PRE
  55052. CLASS="screen"
  55053. >Array
  55054. (
  55055.     [0] => 1
  55056.     [1] => 8
  55057.     [2] => 27
  55058.     [3] => 64
  55059.     [4] => 125
  55060. )</PRE
  55061. ></TD
  55062. ></TR
  55063. ></TABLE
  55064. ></DIV
  55065. ></TD
  55066. ></TR
  55067. ></TABLE
  55068. >
  55069.     </P
  55070. ><P
  55071. >      <TABLE
  55072. WIDTH="100%"
  55073. BORDER="0"
  55074. CELLPADDING="0"
  55075. CELLSPACING="0"
  55076. CLASS="EXAMPLE"
  55077. ><TR
  55078. ><TD
  55079. ><DIV
  55080. CLASS="example"
  55081. ><A
  55082. NAME="AEN9569"
  55083. ></A
  55084. ><P
  55085. ><B
  55086. >Example 2. <B
  55087. CLASS="function"
  55088. >array_map()</B
  55089. > - using more arrays</B
  55090. ></P
  55091. ><TABLE
  55092. BORDER="0"
  55093. BGCOLOR="#E0E0E0"
  55094. CELLPADDING="5"
  55095. ><TR
  55096. ><TD
  55097. ><PRE
  55098. CLASS="php"
  55099. ><?php
  55100. function show_Spanish($n, $m) 
  55101. {
  55102.     return("The number $n is called $m in Spanish");
  55103. }
  55104.  
  55105. function map_Spanish($n, $m) 
  55106. {
  55107.     return(array($n => $m));
  55108. }
  55109.  
  55110. $a = array(1, 2, 3, 4, 5);
  55111. $b = array("uno", "dos", "tres", "cuatro", "cinco");
  55112.  
  55113. $c = array_map("show_Spanish", $a, $b);
  55114. print_r($c);
  55115.  
  55116. $d = array_map("map_Spanish", $a , $b);
  55117. print_r($d);
  55118. ?></PRE
  55119. ></TD
  55120. ></TR
  55121. ></TABLE
  55122. ><P
  55123. >The above example will output:</P
  55124. ><TABLE
  55125. BORDER="0"
  55126. BGCOLOR="#E0E0E0"
  55127. CELLPADDING="5"
  55128. ><TR
  55129. ><TD
  55130. ><PRE
  55131. CLASS="screen"
  55132. >// printout of $c
  55133. Array
  55134. (
  55135.     [0] => The number 1 is called uno in Spanish
  55136.     [1] => The number 2 is called dos in Spanish
  55137.     [2] => The number 3 is called tres in Spanish
  55138.     [3] => The number 4 is called cuatro in Spanish
  55139.     [4] => The number 5 is called cinco in Spanish
  55140. )
  55141.  
  55142. // printout of $d
  55143. Array
  55144. (
  55145.     [0] => Array
  55146.         (
  55147.             [1] => uno
  55148.         )
  55149.  
  55150.     [1] => Array
  55151.         (
  55152.             [2] => dos
  55153.         )
  55154.  
  55155.     [2] => Array
  55156.         (
  55157.             [3] => tres
  55158.         )
  55159.  
  55160.     [3] => Array
  55161.         (
  55162.             [4] => cuatro
  55163.         )
  55164.  
  55165.     [4] => Array
  55166.         (
  55167.             [5] => cinco
  55168.         )
  55169.  
  55170. )</PRE
  55171. ></TD
  55172. ></TR
  55173. ></TABLE
  55174. ></DIV
  55175. ></TD
  55176. ></TR
  55177. ></TABLE
  55178. >
  55179.     </P
  55180. ><P
  55181. >      Usually when using two or more arrays, they should be of equal length
  55182.      because the callback function is applied in parallel to the corresponding
  55183.      elements.
  55184.      If the arrays are of unequal length, the shortest one will be extended
  55185.      with empty elements.
  55186.     </P
  55187. ><P
  55188. >      An interesting use of this function is to construct an array of arrays,
  55189.      which can be easily performed by using <TT
  55190. CLASS="constant"
  55191. ><B
  55192. >NULL</B
  55193. ></TT
  55194. >
  55195.      as the name of the callback function
  55196.     </P
  55197. ><P
  55198. >      <TABLE
  55199. WIDTH="100%"
  55200. BORDER="0"
  55201. CELLPADDING="0"
  55202. CELLSPACING="0"
  55203. CLASS="EXAMPLE"
  55204. ><TR
  55205. ><TD
  55206. ><DIV
  55207. CLASS="example"
  55208. ><A
  55209. NAME="AEN9579"
  55210. ></A
  55211. ><P
  55212. ><B
  55213. >Example 3. Creating an array of arrays</B
  55214. ></P
  55215. ><TABLE
  55216. BORDER="0"
  55217. BGCOLOR="#E0E0E0"
  55218. CELLPADDING="5"
  55219. ><TR
  55220. ><TD
  55221. ><PRE
  55222. CLASS="php"
  55223. ><?php
  55224. $a = array(1, 2, 3, 4, 5);
  55225. $b = array("one", "two", "three", "four", "five");
  55226. $c = array("uno", "dos", "tres", "cuatro", "cinco");
  55227.  
  55228. $d = array_map(null, $a, $b, $c);
  55229. print_r($d);
  55230. ?></PRE
  55231. ></TD
  55232. ></TR
  55233. ></TABLE
  55234. ><P
  55235. >The above example will output:</P
  55236. ><TABLE
  55237. BORDER="0"
  55238. BGCOLOR="#E0E0E0"
  55239. CELLPADDING="5"
  55240. ><TR
  55241. ><TD
  55242. ><PRE
  55243. CLASS="screen"
  55244. >Array
  55245. (
  55246.     [0] => Array
  55247.         (
  55248.             [0] => 1
  55249.             [1] => one
  55250.             [2] => uno
  55251.         )
  55252.  
  55253.     [1] => Array
  55254.         (
  55255.             [0] => 2
  55256.             [1] => two
  55257.             [2] => dos
  55258.         )
  55259.  
  55260.     [2] => Array
  55261.         (
  55262.             [0] => 3
  55263.             [1] => three
  55264.             [2] => tres
  55265.         )
  55266.  
  55267.     [3] => Array
  55268.         (
  55269.             [0] => 4
  55270.             [1] => four
  55271.             [2] => cuatro
  55272.         )
  55273.  
  55274.     [4] => Array
  55275.         (
  55276.             [0] => 5
  55277.             [1] => five
  55278.             [2] => cinco
  55279.         )
  55280.  
  55281. )</PRE
  55282. ></TD
  55283. ></TR
  55284. ></TABLE
  55285. ></DIV
  55286. ></TD
  55287. ></TR
  55288. ></TABLE
  55289. >
  55290.     </P
  55291. ><P
  55292. >      See also <A
  55293. HREF="#function.array-filter"
  55294. ><B
  55295. CLASS="function"
  55296. >array_filter()</B
  55297. ></A
  55298. >, 
  55299.      <A
  55300. HREF="#function.array-reduce"
  55301. ><B
  55302. CLASS="function"
  55303. >array_reduce()</B
  55304. ></A
  55305. >, and
  55306.      <A
  55307. HREF="#function.array-walk"
  55308. ><B
  55309. CLASS="function"
  55310. >array_walk()</B
  55311. ></A
  55312. >.
  55313.     </P
  55314. ></DIV
  55315. ><H1
  55316. ><A
  55317. NAME="function.array-merge-recursive"
  55318. ></A
  55319. >array_merge_recursive</H1
  55320. ><DIV
  55321. CLASS="refnamediv"
  55322. ><A
  55323. NAME="AEN9589"
  55324. ></A
  55325. ><P
  55326. >    (PHP 4 >= 4.0.1, PHP 5)</P
  55327. >array_merge_recursive -- Merge two or more arrays recursively</DIV
  55328. ><DIV
  55329. CLASS="refsect1"
  55330. ><A
  55331. NAME="AEN9592"
  55332. ></A
  55333. ><H2
  55334. >Description</H2
  55335. >array <B
  55336. CLASS="methodname"
  55337. >array_merge_recursive</B
  55338. > ( array array1, array array2 [, array ...])<BR
  55339. ></BR
  55340. ><P
  55341. >      <B
  55342. CLASS="function"
  55343. >array_merge_recursive()</B
  55344. > merges the elements of
  55345.      two or more arrays together so that the values of one are appended
  55346.      to the end of the previous one.  It returns the resulting array.
  55347.     </P
  55348. ><P
  55349. >      If the input arrays have the same string keys, then the values for
  55350.      these keys are merged together into an array, and this is done
  55351.      recursively, so that if one of the values is an array itself, the
  55352.      function will merge it with a corresponding entry in another array
  55353.      too. If, however, the arrays have the same numeric key, the later
  55354.      value will not overwrite the original value, but will be appended.
  55355.     </P
  55356. ><P
  55357. >      <TABLE
  55358. WIDTH="100%"
  55359. BORDER="0"
  55360. CELLPADDING="0"
  55361. CELLSPACING="0"
  55362. CLASS="EXAMPLE"
  55363. ><TR
  55364. ><TD
  55365. ><DIV
  55366. CLASS="example"
  55367. ><A
  55368. NAME="AEN9610"
  55369. ></A
  55370. ><P
  55371. ><B
  55372. >Example 1. <B
  55373. CLASS="function"
  55374. >array_merge_recursive()</B
  55375. > example</B
  55376. ></P
  55377. ><TABLE
  55378. BORDER="0"
  55379. BGCOLOR="#E0E0E0"
  55380. CELLPADDING="5"
  55381. ><TR
  55382. ><TD
  55383. ><PRE
  55384. CLASS="php"
  55385. ><?php
  55386. $ar1 = array("color" => array("favorite" => "red"), 5);
  55387. $ar2 = array(10, "color" => array("favorite" => "green", "blue"));
  55388. $result = array_merge_recursive($ar1, $ar2);
  55389. ?></PRE
  55390. ></TD
  55391. ></TR
  55392. ></TABLE
  55393. ><P
  55394. >        The <VAR
  55395. CLASS="literal"
  55396. >$result</VAR
  55397. > will be:
  55398.       </P
  55399. ><TABLE
  55400. BORDER="0"
  55401. BGCOLOR="#E0E0E0"
  55402. CELLPADDING="5"
  55403. ><TR
  55404. ><TD
  55405. ><PRE
  55406. CLASS="php"
  55407. >Array
  55408. (
  55409.     [color] => Array
  55410.         (
  55411.             [favorite] => Array
  55412.                 (
  55413.                     [0] => red
  55414.                     [1] => green
  55415.                 )
  55416.  
  55417.             [0] => blue
  55418.         )
  55419.  
  55420.     [0] => 5
  55421.     [1] => 10
  55422. )</PRE
  55423. ></TD
  55424. ></TR
  55425. ></TABLE
  55426. ></DIV
  55427. ></TD
  55428. ></TR
  55429. ></TABLE
  55430. >
  55431.     </P
  55432. ><P
  55433. >      See also <A
  55434. HREF="#function.array-merge"
  55435. ><B
  55436. CLASS="function"
  55437. >array_merge()</B
  55438. ></A
  55439. >.
  55440.     </P
  55441. ></DIV
  55442. ><H1
  55443. ><A
  55444. NAME="function.array-merge"
  55445. ></A
  55446. >array_merge</H1
  55447. ><DIV
  55448. CLASS="refnamediv"
  55449. ><A
  55450. NAME="AEN9620"
  55451. ></A
  55452. ><P
  55453. >    (PHP 4 , PHP 5)</P
  55454. >array_merge -- Merge one or more arrays</DIV
  55455. ><DIV
  55456. CLASS="refsect1"
  55457. ><A
  55458. NAME="AEN9623"
  55459. ></A
  55460. ><H2
  55461. >Description</H2
  55462. >array <B
  55463. CLASS="methodname"
  55464. >array_merge</B
  55465. > ( array array1 [, array array2 [, array ...]])<BR
  55466. ></BR
  55467. ><P
  55468. >      <B
  55469. CLASS="function"
  55470. >array_merge()</B
  55471. > merges the elements of one or
  55472.      more arrays together so that the values of one are appended to
  55473.      the end of the previous one.  It returns the resulting array.
  55474.     </P
  55475. ><P
  55476. >      If the input arrays have the same string keys, then the later value for
  55477.      that key will overwrite the previous one.  If, however, the arrays
  55478.      contain numeric keys, the later value will <SPAN
  55479. CLASS="strong"
  55480. ><B
  55481. CLASS="emphasis"
  55482. >not</B
  55483. ></SPAN
  55484. > overwrite the original value, but will be
  55485.      appended.
  55486.     </P
  55487. ><P
  55488. >      If only one array is given and the array is numerically indexed, the
  55489.      keys get reindexed in a continuous way. For associative arrays, duplicate
  55490.      entries will be merged into the last one. See example three for details.
  55491.     </P
  55492. ><P
  55493. >      <TABLE
  55494. WIDTH="100%"
  55495. BORDER="0"
  55496. CELLPADDING="0"
  55497. CELLSPACING="0"
  55498. CLASS="EXAMPLE"
  55499. ><TR
  55500. ><TD
  55501. ><DIV
  55502. CLASS="example"
  55503. ><A
  55504. NAME="AEN9643"
  55505. ></A
  55506. ><P
  55507. ><B
  55508. >Example 1. <B
  55509. CLASS="function"
  55510. >array_merge()</B
  55511. > example</B
  55512. ></P
  55513. ><TABLE
  55514. BORDER="0"
  55515. BGCOLOR="#E0E0E0"
  55516. CELLPADDING="5"
  55517. ><TR
  55518. ><TD
  55519. ><PRE
  55520. CLASS="php"
  55521. ><?php
  55522. $array1 = array("color" => "red", 2, 4);
  55523. $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
  55524. $result = array_merge($array1, $array2);
  55525. print_r($result);
  55526. ?></PRE
  55527. ></TD
  55528. ></TR
  55529. ></TABLE
  55530. ><P
  55531. >        The <VAR
  55532. CLASS="varname"
  55533. >$result</VAR
  55534. > is:
  55535.       </P
  55536. ><TABLE
  55537. BORDER="0"
  55538. BGCOLOR="#E0E0E0"
  55539. CELLPADDING="5"
  55540. ><TR
  55541. ><TD
  55542. ><PRE
  55543. CLASS="php"
  55544. >Array
  55545. (
  55546.     [color] => green
  55547.     [0] => 2
  55548.     [1] => 4
  55549.     [2] => a
  55550.     [3] => b
  55551.     [shape] => trapezoid
  55552.     [4] => 4
  55553. )</PRE
  55554. ></TD
  55555. ></TR
  55556. ></TABLE
  55557. ></DIV
  55558. ></TD
  55559. ></TR
  55560. ></TABLE
  55561. >
  55562.     </P
  55563. ><P
  55564. >      <TABLE
  55565. WIDTH="100%"
  55566. BORDER="0"
  55567. CELLPADDING="0"
  55568. CELLSPACING="0"
  55569. CLASS="EXAMPLE"
  55570. ><TR
  55571. ><TD
  55572. ><DIV
  55573. CLASS="example"
  55574. ><A
  55575. NAME="AEN9651"
  55576. ></A
  55577. ><P
  55578. ><B
  55579. >Example 2. Simple <B
  55580. CLASS="function"
  55581. >array_merge()</B
  55582. > example</B
  55583. ></P
  55584. ><TABLE
  55585. BORDER="0"
  55586. BGCOLOR="#E0E0E0"
  55587. CELLPADDING="5"
  55588. ><TR
  55589. ><TD
  55590. ><PRE
  55591. CLASS="php"
  55592. ><?php
  55593. $array1 = array();
  55594. $array2 = array(1 => "data");
  55595. $result = array_merge($array1, $array2);
  55596. ?></PRE
  55597. ></TD
  55598. ></TR
  55599. ></TABLE
  55600. ><P
  55601. >        Don't forget that numeric keys will be renumbered!
  55602.       </P
  55603. ><TABLE
  55604. BORDER="0"
  55605. BGCOLOR="#E0E0E0"
  55606. CELLPADDING="5"
  55607. ><TR
  55608. ><TD
  55609. ><PRE
  55610. CLASS="php"
  55611. >Array
  55612. (
  55613.     [0] => data
  55614. )</PRE
  55615. ></TD
  55616. ></TR
  55617. ></TABLE
  55618. ><P
  55619. >        If you want to completely preserve the arrays and just want to append
  55620.        them to each other, use the <VAR
  55621. CLASS="literal"
  55622. >+</VAR
  55623. > operator:
  55624.       </P
  55625. ><TABLE
  55626. BORDER="0"
  55627. BGCOLOR="#E0E0E0"
  55628. CELLPADDING="5"
  55629. ><TR
  55630. ><TD
  55631. ><PRE
  55632. CLASS="php"
  55633. ><?php
  55634. $array1 = array();
  55635. $array2 = array(1 => "data");
  55636. $result = $array1 + $array2;
  55637. ?></PRE
  55638. ></TD
  55639. ></TR
  55640. ></TABLE
  55641. ><P
  55642. >        The numeric key will be preserved and thus the association remains.
  55643.       </P
  55644. ><TABLE
  55645. BORDER="0"
  55646. BGCOLOR="#E0E0E0"
  55647. CELLPADDING="5"
  55648. ><TR
  55649. ><TD
  55650. ><PRE
  55651. CLASS="php"
  55652. >Array
  55653. (
  55654.     [1] => data
  55655. )</PRE
  55656. ></TD
  55657. ></TR
  55658. ></TABLE
  55659. ></DIV
  55660. ></TD
  55661. ></TR
  55662. ></TABLE
  55663. >
  55664.      <TABLE
  55665. WIDTH="100%"
  55666. BORDER="0"
  55667. CELLPADDING="0"
  55668. CELLSPACING="0"
  55669. CLASS="EXAMPLE"
  55670. ><TR
  55671. ><TD
  55672. ><DIV
  55673. CLASS="example"
  55674. ><A
  55675. NAME="AEN9662"
  55676. ></A
  55677. ><P
  55678. ><B
  55679. >Example 3. <B
  55680. CLASS="function"
  55681. >array_merge()</B
  55682. > example</B
  55683. ></P
  55684. ><TABLE
  55685. BORDER="0"
  55686. BGCOLOR="#E0E0E0"
  55687. CELLPADDING="5"
  55688. ><TR
  55689. ><TD
  55690. ><PRE
  55691. CLASS="php"
  55692. ><?php
  55693. $array_one = array(0 => "jay", 1 => "bob", 2 => "randal", 3 => "dante");
  55694. $array_two = array("jay" => "bob", "randal" => "dante", "jay" => "jason");
  55695.  
  55696. unset($array_one[2]);
  55697.  
  55698. $result_one = array_merge($array_one);
  55699. $result_two = array_merge($array_two);
  55700.  
  55701. print_r($result_one);
  55702. print_r($result_two);
  55703. ?></PRE
  55704. ></TD
  55705. ></TR
  55706. ></TABLE
  55707. ><P
  55708. >The above example will output:</P
  55709. ><TABLE
  55710. BORDER="0"
  55711. BGCOLOR="#E0E0E0"
  55712. CELLPADDING="5"
  55713. ><TR
  55714. ><TD
  55715. ><PRE
  55716. CLASS="php"
  55717. >Array
  55718. (
  55719.     [0] => jay
  55720.     [1] => bob
  55721.     [2] => dante
  55722. )
  55723. Array
  55724. (
  55725.     [jay] => jason
  55726.     [randal] => dante
  55727. )</PRE
  55728. ></TD
  55729. ></TR
  55730. ></TABLE
  55731. ></DIV
  55732. ></TD
  55733. ></TR
  55734. ></TABLE
  55735. >
  55736.     </P
  55737. ><DIV
  55738. CLASS="note"
  55739. ><BLOCKQUOTE
  55740. CLASS="note"
  55741. ><P
  55742. ><B
  55743. >Note: </B
  55744. >
  55745.       Shared keys will be overwritten on a first-come first-served basis.
  55746.      </P
  55747. ></BLOCKQUOTE
  55748. ></DIV
  55749. ><DIV
  55750. CLASS="warning"
  55751. ><P
  55752. ></P
  55753. ><TABLE
  55754. CLASS="warning"
  55755. BORDER="1"
  55756. WIDTH="100%"
  55757. ><TR
  55758. ><TD
  55759. ALIGN="CENTER"
  55760. ><B
  55761. >Warning</B
  55762. ></TD
  55763. ></TR
  55764. ><TR
  55765. ><TD
  55766. ALIGN="LEFT"
  55767. ><P
  55768. >       The behavior of <B
  55769. CLASS="function"
  55770. >array_merge()</B
  55771. > was modified in PHP 5. Unlike PHP 4, <B
  55772. CLASS="function"
  55773. >array_merge()</B
  55774.       now only accepts parameters of type <A
  55775. HREF="#language.types.array"
  55776. ><B
  55777. CLASS="type"
  55778. >array</B
  55779. ></A
  55780. >. However, you can use typecasting 
  55781.       to merge other types. See the example below for details.
  55782.      </P
  55783. ><P
  55784. >       <TABLE
  55785. WIDTH="100%"
  55786. BORDER="0"
  55787. CELLPADDING="0"
  55788. CELLSPACING="0"
  55789. CLASS="EXAMPLE"
  55790. ><TR
  55791. ><TD
  55792. ><DIV
  55793. CLASS="example"
  55794. ><A
  55795. NAME="AEN9676"
  55796. ></A
  55797. ><P
  55798. ><B
  55799. >Example 4. <B
  55800. CLASS="function"
  55801. >array_merge()</B
  55802. > PHP 5 example</B
  55803. ></P
  55804. ><TABLE
  55805. BORDER="0"
  55806. BGCOLOR="#E0E0E0"
  55807. CELLPADDING="5"
  55808. ><TR
  55809. ><TD
  55810. ><PRE
  55811. CLASS="php"
  55812. ><?php
  55813. $beginning = 'foo';
  55814. $end = array(1 => 'bar');
  55815. $result = array_merge((array)$beginning, (array)$end);
  55816. ?></PRE
  55817. ></TD
  55818. ></TR
  55819. ></TABLE
  55820. ><P
  55821. >The above example will output:</P
  55822. ><TABLE
  55823. BORDER="0"
  55824. BGCOLOR="#E0E0E0"
  55825. CELLPADDING="5"
  55826. ><TR
  55827. ><TD
  55828. ><PRE
  55829. CLASS="php"
  55830. >Array
  55831. (
  55832.     [0] => foo
  55833.     [1] => bar
  55834. )</PRE
  55835. ></TD
  55836. ></TR
  55837. ></TABLE
  55838. ></DIV
  55839. ></TD
  55840. ></TR
  55841. ></TABLE
  55842. >
  55843.      </P
  55844. ></TD
  55845. ></TR
  55846. ></TABLE
  55847. ></DIV
  55848. ><P
  55849. >      See also <A
  55850. HREF="#function.array-merge-recursive"
  55851. ><B
  55852. CLASS="function"
  55853. >array_merge_recursive()</B
  55854. ></A
  55855. >,
  55856.      <A
  55857. HREF="#function.array-combine"
  55858. ><B
  55859. CLASS="function"
  55860. >array_combine()</B
  55861. ></A
  55862. > and
  55863.      <A
  55864. HREF="#language.operators.array"
  55865. >array operators</A
  55866. >.
  55867.     </P
  55868. ></DIV
  55869. ><H1
  55870. ><A
  55871. NAME="function.array-multisort"
  55872. ></A
  55873. >array_multisort</H1
  55874. ><DIV
  55875. CLASS="refnamediv"
  55876. ><A
  55877. NAME="AEN9687"
  55878. ></A
  55879. ><P
  55880. >    (PHP 4 , PHP 5)</P
  55881. >array_multisort -- Sort multiple or multi-dimensional arrays</DIV
  55882. ><DIV
  55883. CLASS="refsect1"
  55884. ><A
  55885. NAME="AEN9690"
  55886. ></A
  55887. ><H2
  55888. >Description</H2
  55889. >bool <B
  55890. CLASS="methodname"
  55891. >array_multisort</B
  55892. > ( array ar1 [, mixed arg [, mixed ... [, array ...]]])<BR
  55893. ></BR
  55894. ><P
  55895. >      Returns <TT
  55896. CLASS="constant"
  55897. ><B
  55898. >TRUE</B
  55899. ></TT
  55900. > on success or <TT
  55901. CLASS="constant"
  55902. ><B
  55903. >FALSE</B
  55904. ></TT
  55905. > on failure.
  55906.     </P
  55907. ><P
  55908. >      <B
  55909. CLASS="function"
  55910. >array_multisort()</B
  55911. > can be used to sort several
  55912.      arrays at once, or a multi-dimensional array by one or more
  55913.      dimensions.
  55914.     </P
  55915. ><P
  55916. >      Associative (<A
  55917. HREF="#language.types.string"
  55918. ><B
  55919. CLASS="type"
  55920. >string</B
  55921. ></A
  55922. >) keys will be maintained, but numeric
  55923.      keys will be re-indexed.
  55924.     </P
  55925. ><P
  55926. >      The input arrays are treated as columns of a table to be sorted
  55927.      by rows - this resembles the functionality of SQL ORDER BY
  55928.      clause. The first array is the primary one to sort by. The rows
  55929.      (values) in that array that compare the same are sorted by the
  55930.      next input array, and so on.
  55931.     </P
  55932. ><P
  55933. >      The argument structure of this function is a bit unusual, but
  55934.      flexible. The first argument has to be an array. Subsequently,
  55935.      each argument can be either an array or a sorting flag from the
  55936.      following lists.
  55937.     </P
  55938. ><P
  55939. >      Sorting order flags:
  55940.      <P
  55941. ></P
  55942. ><UL
  55943. ><LI
  55944. ><P
  55945. ><TT
  55946. CLASS="constant"
  55947. ><B
  55948. >SORT_ASC</B
  55949. ></TT
  55950. > - Sort in ascending order</P
  55951. ></LI
  55952. ><LI
  55953. ><P
  55954. ><TT
  55955. CLASS="constant"
  55956. ><B
  55957. >SORT_DESC</B
  55958. ></TT
  55959. > - Sort in descending order</P
  55960. ></LI
  55961. ></UL
  55962. >
  55963.     </P
  55964. ><P
  55965. >      Sorting type flags:
  55966.      <P
  55967. ></P
  55968. ><UL
  55969. ><LI
  55970. ><P
  55971. ><TT
  55972. CLASS="constant"
  55973. ><B
  55974. >SORT_REGULAR</B
  55975. ></TT
  55976. > - Compare items normally</P
  55977. ></LI
  55978. ><LI
  55979. ><P
  55980. ><TT
  55981. CLASS="constant"
  55982. ><B
  55983. >SORT_NUMERIC</B
  55984. ></TT
  55985. > - Compare items numerically</P
  55986. ></LI
  55987. ><LI
  55988. ><P
  55989. ><TT
  55990. CLASS="constant"
  55991. ><B
  55992. >SORT_STRING</B
  55993. ></TT
  55994. > - Compare items as strings</P
  55995. ></LI
  55996. ></UL
  55997. >
  55998.     </P
  55999. ><P
  56000. >      No two sorting flags of the same type can be specified after each
  56001.      array. The sorting flags specified after an array argument apply
  56002.      only to that array - they are reset to default <TT
  56003. CLASS="constant"
  56004. ><B
  56005. >SORT_ASC</B
  56006. ></TT
  56007. > and
  56008.      <TT
  56009. CLASS="constant"
  56010. ><B
  56011. >SORT_REGULAR</B
  56012. ></TT
  56013. > before each new array argument.
  56014.     </P
  56015. ><P
  56016. >      <TABLE
  56017. WIDTH="100%"
  56018. BORDER="0"
  56019. CELLPADDING="0"
  56020. CELLSPACING="0"
  56021. CLASS="EXAMPLE"
  56022. ><TR
  56023. ><TD
  56024. ><DIV
  56025. CLASS="example"
  56026. ><A
  56027. NAME="AEN9739"
  56028. ></A
  56029. ><P
  56030. ><B
  56031. >Example 1. Sorting multiple arrays</B
  56032. ></P
  56033. ><TABLE
  56034. BORDER="0"
  56035. BGCOLOR="#E0E0E0"
  56036. CELLPADDING="5"
  56037. ><TR
  56038. ><TD
  56039. ><PRE
  56040. CLASS="php"
  56041. ><?php
  56042. $ar1 = array("10", 100, 100, "a");
  56043. $ar2 = array(1, 3, "2", 1);
  56044. array_multisort($ar1, $ar2);
  56045.  
  56046. var_dump($ar1);
  56047. var_dump($ar2);
  56048. ?></PRE
  56049. ></TD
  56050. ></TR
  56051. ></TABLE
  56052. ><P
  56053. >        In this example, after sorting, the first array will contain "10",
  56054.        "a", 100, 100. The second array will contain 1, 1, "2", 3. The
  56055.        entries in the second array corresponding to the identical
  56056.        entries in the first array (100 and 100) were sorted as well.
  56057.       </P
  56058. ><TABLE
  56059. BORDER="0"
  56060. BGCOLOR="#E0E0E0"
  56061. CELLPADDING="5"
  56062. ><TR
  56063. ><TD
  56064. ><PRE
  56065. CLASS="screen"
  56066. >array(4) {
  56067.   [0]=> string(2) "10"
  56068.   [1]=> string(1) "a"
  56069.   [2]=> int(100)
  56070.   [3]=> int(100)
  56071. }
  56072. array(4) {
  56073.   [0]=> int(1)
  56074.   [1]=> int(1)
  56075.   [2]=> string(1) "2"
  56076.   [3]=> int(3)
  56077. }</PRE
  56078. ></TD
  56079. ></TR
  56080. ></TABLE
  56081. ></DIV
  56082. ></TD
  56083. ></TR
  56084. ></TABLE
  56085. >
  56086.     </P
  56087. ><P
  56088. >      <TABLE
  56089. WIDTH="100%"
  56090. BORDER="0"
  56091. CELLPADDING="0"
  56092. CELLSPACING="0"
  56093. CLASS="EXAMPLE"
  56094. ><TR
  56095. ><TD
  56096. ><DIV
  56097. CLASS="example"
  56098. ><A
  56099. NAME="AEN9745"
  56100. ></A
  56101. ><P
  56102. ><B
  56103. >Example 2. Sorting multi-dimensional array</B
  56104. ></P
  56105. ><TABLE
  56106. BORDER="0"
  56107. BGCOLOR="#E0E0E0"
  56108. CELLPADDING="5"
  56109. ><TR
  56110. ><TD
  56111. ><PRE
  56112. CLASS="php"
  56113. ><?php
  56114. $ar = array(array("10", 100, 100, "a"), array(1, 3, "2", 1));
  56115. array_multisort($ar[0], SORT_ASC, SORT_STRING,
  56116.                 $ar[1], SORT_NUMERIC, SORT_DESC);
  56117. ?></PRE
  56118. ></TD
  56119. ></TR
  56120. ></TABLE
  56121. ><P
  56122. >        In this example, after sorting, the first array will contain "10",
  56123.        100, 100, "a" (it was sorted as strings in ascending order). The
  56124.        second will contain 1, 3, "2", 1 (sorted as numbers, in
  56125.        descending order).
  56126.       </P
  56127. ><TABLE
  56128. BORDER="0"
  56129. BGCOLOR="#E0E0E0"
  56130. CELLPADDING="5"
  56131. ><TR
  56132. ><TD
  56133. ><PRE
  56134. CLASS="screen"
  56135. >array(2) {
  56136.   [0]=> array(4) {
  56137.     [0]=> string(2) "10"
  56138.     [1]=> int(100)
  56139.     [2]=> int(100)
  56140.     [3]=> string(1) "a"
  56141.   }
  56142.   [1]=> array(4) {
  56143.     [0]=> int(1)
  56144.     [1]=> int(3)
  56145.     [2]=> string(1) "2"
  56146.     [3]=> int(1)
  56147.   }
  56148. }</PRE
  56149. ></TD
  56150. ></TR
  56151. ></TABLE
  56152. ></DIV
  56153. ></TD
  56154. ></TR
  56155. ></TABLE
  56156. >
  56157.     </P
  56158. ><P
  56159. >      <TABLE
  56160. WIDTH="100%"
  56161. BORDER="0"
  56162. CELLPADDING="0"
  56163. CELLSPACING="0"
  56164. CLASS="EXAMPLE"
  56165. ><TR
  56166. ><TD
  56167. ><DIV
  56168. CLASS="example"
  56169. ><A
  56170. NAME="AEN9751"
  56171. ></A
  56172. ><P
  56173. ><B
  56174. >Example 3. Sorting database results</B
  56175. ></P
  56176. ><P
  56177. >        For this example, each element in the <VAR
  56178. CLASS="varname"
  56179. >data</VAR
  56180. >
  56181.        array represents one row in a table. This type of dataset is typical
  56182.        of database records.
  56183.       </P
  56184. ><P
  56185. >        Example data:
  56186.       </P
  56187. ><TABLE
  56188. BORDER="0"
  56189. BGCOLOR="#E0E0E0"
  56190. CELLPADDING="5"
  56191. ><TR
  56192. ><TD
  56193. ><PRE
  56194. CLASS="screen"
  56195. >volume | edition
  56196. -------+--------
  56197.     67 |       2
  56198.     86 |       1
  56199.     85 |       6
  56200.     98 |       2
  56201.     86 |       6
  56202.     67 |       7</PRE
  56203. ></TD
  56204. ></TR
  56205. ></TABLE
  56206. ><P
  56207. >        The data as an array, called <VAR
  56208. CLASS="varname"
  56209. >data</VAR
  56210. >. This would usually,
  56211.        for example, be obtained by looping with <A
  56212. HREF="#function.mysql-fetch-assoc"
  56213. ><B
  56214. CLASS="function"
  56215. >mysql_fetch_assoc()</B
  56216. ></A
  56217. >.
  56218.       </P
  56219. ><TABLE
  56220. BORDER="0"
  56221. BGCOLOR="#E0E0E0"
  56222. CELLPADDING="5"
  56223. ><TR
  56224. ><TD
  56225. ><PRE
  56226. CLASS="php"
  56227. ><?php
  56228. $data[] = array('volume' => 67, 'edition' => 2);
  56229. $data[] = array('volume' => 86, 'edition' => 1);
  56230. $data[] = array('volume' => 85, 'edition' => 6);
  56231. $data[] = array('volume' => 98, 'edition' => 2);
  56232. $data[] = array('volume' => 86, 'edition' => 6);
  56233. $data[] = array('volume' => 67, 'edition' => 7);
  56234. ?></PRE
  56235. ></TD
  56236. ></TR
  56237. ></TABLE
  56238. ><P
  56239. >        In this example, we will order by <VAR
  56240. CLASS="varname"
  56241. >volume</VAR
  56242. > descending,
  56243.        <VAR
  56244. CLASS="varname"
  56245. >edition</VAR
  56246. > ascending.
  56247.       </P
  56248. ><P
  56249. >        We have an array of rows, but <B
  56250. CLASS="function"
  56251. >array_multisort()</B
  56252. >
  56253.        requires an array of columns, so we use the the below code to obtain the
  56254.        columns, then perform the sorting.
  56255.       </P
  56256. ><TABLE
  56257. BORDER="0"
  56258. BGCOLOR="#E0E0E0"
  56259. CELLPADDING="5"
  56260. ><TR
  56261. ><TD
  56262. ><PRE
  56263. CLASS="php"
  56264. ><?php
  56265. // Obtain a list of columns
  56266. foreach ($data as $key => $row) {
  56267.     $volume[$key]  = $row['volume'];
  56268.     $edition[$key] = $row['edition'];
  56269. }
  56270.  
  56271. // Sort the data with volume descending, edition ascending
  56272. // Add $data as the last parameter, to sort by the common key
  56273. array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
  56274. ?></PRE
  56275. ></TD
  56276. ></TR
  56277. ></TABLE
  56278. ><P
  56279. >        The dataset is now sorted, and will look like this:
  56280.       </P
  56281. ><TABLE
  56282. BORDER="0"
  56283. BGCOLOR="#E0E0E0"
  56284. CELLPADDING="5"
  56285. ><TR
  56286. ><TD
  56287. ><PRE
  56288. CLASS="screen"
  56289. >volume | edition
  56290. -------+--------
  56291.     98 |       2
  56292.     86 |       1
  56293.     86 |       6
  56294.     85 |       6
  56295.     67 |       2
  56296.     67 |       7</PRE
  56297. ></TD
  56298. ></TR
  56299. ></TABLE
  56300. ></DIV
  56301. ></TD
  56302. ></TR
  56303. ></TABLE
  56304. >
  56305.     </P
  56306. ><P
  56307. >      <TABLE
  56308. WIDTH="100%"
  56309. BORDER="0"
  56310. CELLPADDING="0"
  56311. CELLSPACING="0"
  56312. CLASS="EXAMPLE"
  56313. ><TR
  56314. ><TD
  56315. ><DIV
  56316. CLASS="example"
  56317. ><A
  56318. NAME="AEN9770"
  56319. ></A
  56320. ><P
  56321. ><B
  56322. >Example 4. Case insensitive sorting</B
  56323. ></P
  56324. ><P
  56325. >        Both <TT
  56326. CLASS="constant"
  56327. ><B
  56328. >SORT_STRING</B
  56329. ></TT
  56330. > and
  56331.        <TT
  56332. CLASS="constant"
  56333. ><B
  56334. >SORT_REGULAR</B
  56335. ></TT
  56336. > are case sensitive, strings
  56337.        starting with a capital letter will come before strings starting
  56338.        with a lowercase letter.
  56339.       </P
  56340. ><P
  56341. >        To perform a case insensitve search, force the sorting order to be
  56342.        determined by a lowercase copy of the original array.
  56343.       </P
  56344. ><TABLE
  56345. BORDER="0"
  56346. BGCOLOR="#E0E0E0"
  56347. CELLPADDING="5"
  56348. ><TR
  56349. ><TD
  56350. ><PRE
  56351. CLASS="php"
  56352. ><?php
  56353. $array = array('Alpha', 'atomic', 'Beta', 'bank');
  56354. $array_lowercase = array_map('strtolower', $array);
  56355.  
  56356. array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);
  56357.  
  56358. print_r($array);
  56359. ?></PRE
  56360. ></TD
  56361. ></TR
  56362. ></TABLE
  56363. ><P
  56364. >The above example will output:</P
  56365. ><TABLE
  56366. BORDER="0"
  56367. BGCOLOR="#E0E0E0"
  56368. CELLPADDING="5"
  56369. ><TR
  56370. ><TD
  56371. ><PRE
  56372. CLASS="screen"
  56373. >Array
  56374. (
  56375.     [0] => Alpha
  56376.     [1] => atomic
  56377.     [2] => bank
  56378.     [3] => Beta
  56379. )</PRE
  56380. ></TD
  56381. ></TR
  56382. ></TABLE
  56383. ></DIV
  56384. ></TD
  56385. ></TR
  56386. ></TABLE
  56387. >
  56388.     </P
  56389. ></DIV
  56390. ><H1
  56391. ><A
  56392. NAME="function.array-pad"
  56393. ></A
  56394. >array_pad</H1
  56395. ><DIV
  56396. CLASS="refnamediv"
  56397. ><A
  56398. NAME="AEN9780"
  56399. ></A
  56400. ><P
  56401. >    (PHP 4 , PHP 5)</P
  56402. >array_pad -- 
  56403.      Pad array to the specified length with a value
  56404.     </DIV
  56405. ><DIV
  56406. CLASS="refsect1"
  56407. ><A
  56408. NAME="AEN9783"
  56409. ></A
  56410. ><H2
  56411. >Description</H2
  56412. >array <B
  56413. CLASS="methodname"
  56414. >array_pad</B
  56415. > ( array input, int pad_size, mixed pad_value)<BR
  56416. ></BR
  56417. ><P
  56418. >      <B
  56419. CLASS="function"
  56420. >array_pad()</B
  56421. > returns a copy of the
  56422.      <VAR
  56423. CLASS="parameter"
  56424. >input</VAR
  56425. > padded to size specified by
  56426.      <VAR
  56427. CLASS="parameter"
  56428. >pad_size</VAR
  56429. > with value
  56430.      <VAR
  56431. CLASS="parameter"
  56432. >pad_value</VAR
  56433. >. If
  56434.      <VAR
  56435. CLASS="parameter"
  56436. >pad_size</VAR
  56437. > is positive then the array is
  56438.      padded on the right, if it's negative then on the left. If the
  56439.      absolute value of <VAR
  56440. CLASS="parameter"
  56441. >pad_size</VAR
  56442. > is less than or
  56443.      equal to the length of the <VAR
  56444. CLASS="parameter"
  56445. >input</VAR
  56446. > then no
  56447.      padding takes place.
  56448.     </P
  56449. ><P
  56450. >      <TABLE
  56451. WIDTH="100%"
  56452. BORDER="0"
  56453. CELLPADDING="0"
  56454. CELLSPACING="0"
  56455. CLASS="EXAMPLE"
  56456. ><TR
  56457. ><TD
  56458. ><DIV
  56459. CLASS="example"
  56460. ><A
  56461. NAME="AEN9806"
  56462. ></A
  56463. ><P
  56464. ><B
  56465. >Example 1. <B
  56466. CLASS="function"
  56467. >array_pad()</B
  56468. > example</B
  56469. ></P
  56470. ><TABLE
  56471. BORDER="0"
  56472. BGCOLOR="#E0E0E0"
  56473. CELLPADDING="5"
  56474. ><TR
  56475. ><TD
  56476. ><PRE
  56477. CLASS="php"
  56478. ><?php
  56479. $input = array(12, 10, 9);
  56480.  
  56481. $result = array_pad($input, 5, 0);
  56482. // result is array(12, 10, 9, 0, 0)
  56483.  
  56484. $result = array_pad($input, -7, -1);
  56485. // result is array(-1, -1, -1, -1, 12, 10, 9)
  56486.  
  56487. $result = array_pad($input, 2, "noop");
  56488. // not padded
  56489. ?></PRE
  56490. ></TD
  56491. ></TR
  56492. ></TABLE
  56493. ></DIV
  56494. ></TD
  56495. ></TR
  56496. ></TABLE
  56497. >
  56498.     </P
  56499. ><P
  56500. >      See also <A
  56501. HREF="#function.array-fill"
  56502. ><B
  56503. CLASS="function"
  56504. >array_fill()</B
  56505. ></A
  56506. > and
  56507.      <A
  56508. HREF="#function.range"
  56509. ><B
  56510. CLASS="function"
  56511. >range()</B
  56512. ></A
  56513. >.
  56514.     </P
  56515. ></DIV
  56516. ><H1
  56517. ><A
  56518. NAME="function.array-pop"
  56519. ></A
  56520. >array_pop</H1
  56521. ><DIV
  56522. CLASS="refnamediv"
  56523. ><A
  56524. NAME="AEN9814"
  56525. ></A
  56526. ><P
  56527. >    (PHP 4 , PHP 5)</P
  56528. >array_pop -- Pop the element off the end of array</DIV
  56529. ><DIV
  56530. CLASS="refsect1"
  56531. ><A
  56532. NAME="AEN9817"
  56533. ></A
  56534. ><H2
  56535. >Description</H2
  56536. >mixed <B
  56537. CLASS="methodname"
  56538. >array_pop</B
  56539. > ( array &array)<BR
  56540. ></BR
  56541. ><P
  56542. >      <B
  56543. CLASS="function"
  56544. >array_pop()</B
  56545. > pops and returns the last value of
  56546.      the <VAR
  56547. CLASS="parameter"
  56548. >array</VAR
  56549. >, shortening the
  56550.      <VAR
  56551. CLASS="parameter"
  56552. >array</VAR
  56553. > by one element.
  56554.      If <VAR
  56555. CLASS="parameter"
  56556. >array</VAR
  56557. > is empty (or is not an array),
  56558.      <TT
  56559. CLASS="constant"
  56560. ><B
  56561. >NULL</B
  56562. ></TT
  56563. > will be returned.
  56564.     </P
  56565. ><DIV
  56566. CLASS="note"
  56567. ><BLOCKQUOTE
  56568. CLASS="note"
  56569. ><P
  56570. ><B
  56571. >Note: </B
  56572. >This function will 
  56573. <A
  56574. HREF="#function.reset"
  56575. ><B
  56576. CLASS="function"
  56577. >reset()</B
  56578. ></A
  56579. > the <A
  56580. HREF="#language.types.array"
  56581. ><B
  56582. CLASS="type"
  56583. >array</B
  56584. ></A
  56585. > pointer after 
  56586. use.</P
  56587. ></BLOCKQUOTE
  56588. ></DIV
  56589. ><P
  56590. >      <TABLE
  56591. WIDTH="100%"
  56592. BORDER="0"
  56593. CELLPADDING="0"
  56594. CELLSPACING="0"
  56595. CLASS="EXAMPLE"
  56596. ><TR
  56597. ><TD
  56598. ><DIV
  56599. CLASS="example"
  56600. ><A
  56601. NAME="AEN9836"
  56602. ></A
  56603. ><P
  56604. ><B
  56605. >Example 1. <B
  56606. CLASS="function"
  56607. >array_pop()</B
  56608. > example</B
  56609. ></P
  56610. ><TABLE
  56611. BORDER="0"
  56612. BGCOLOR="#E0E0E0"
  56613. CELLPADDING="5"
  56614. ><TR
  56615. ><TD
  56616. ><PRE
  56617. CLASS="php"
  56618. ><?php
  56619. $stack = array("orange", "banana", "apple", "raspberry");
  56620. $fruit = array_pop($stack);
  56621. print_r($stack);
  56622. ?></PRE
  56623. ></TD
  56624. ></TR
  56625. ></TABLE
  56626. ><P
  56627. >        After this, <VAR
  56628. CLASS="varname"
  56629. >$stack</VAR
  56630. > will have only 3 elements:
  56631.       </P
  56632. ><TABLE
  56633. BORDER="0"
  56634. BGCOLOR="#E0E0E0"
  56635. CELLPADDING="5"
  56636. ><TR
  56637. ><TD
  56638. ><PRE
  56639. CLASS="php"
  56640. >Array
  56641. (
  56642.     [0] => orange
  56643.     [1] => banana
  56644.     [2] => apple
  56645. )</PRE
  56646. ></TD
  56647. ></TR
  56648. ></TABLE
  56649. ><P
  56650. >        and <VAR
  56651. CLASS="literal"
  56652. >raspberry</VAR
  56653. > will be assigned to
  56654.        <VAR
  56655. CLASS="varname"
  56656. >$fruit</VAR
  56657. >.
  56658.       </P
  56659. ></DIV
  56660. ></TD
  56661. ></TR
  56662. ></TABLE
  56663. >
  56664.     </P
  56665. ><P
  56666. >      See also <A
  56667. HREF="#function.array-push"
  56668. ><B
  56669. CLASS="function"
  56670. >array_push()</B
  56671. ></A
  56672. >,
  56673.      <A
  56674. HREF="#function.array-shift"
  56675. ><B
  56676. CLASS="function"
  56677. >array_shift()</B
  56678. ></A
  56679. >, and
  56680.      <A
  56681. HREF="#function.array-unshift"
  56682. ><B
  56683. CLASS="function"
  56684. >array_unshift()</B
  56685. ></A
  56686. >.
  56687.     </P
  56688. ></DIV
  56689. ><H1
  56690. ><A
  56691. NAME="function.array-push"
  56692. ></A
  56693. >array_push</H1
  56694. ><DIV
  56695. CLASS="refnamediv"
  56696. ><A
  56697. NAME="AEN9851"
  56698. ></A
  56699. ><P
  56700. >    (PHP 4 , PHP 5)</P
  56701. >array_push -- 
  56702.      Push one or more elements onto the end of array
  56703.     </DIV
  56704. ><DIV
  56705. CLASS="refsect1"
  56706. ><A
  56707. NAME="AEN9854"
  56708. ></A
  56709. ><H2
  56710. >Description</H2
  56711. >int <B
  56712. CLASS="methodname"
  56713. >array_push</B
  56714. > ( array &array, mixed var [, mixed ...])<BR
  56715. ></BR
  56716. ><P
  56717. >      <B
  56718. CLASS="function"
  56719. >array_push()</B
  56720. > treats
  56721.      <VAR
  56722. CLASS="parameter"
  56723. >array</VAR
  56724. > as a stack, and pushes the passed
  56725.      variables onto the end of <VAR
  56726. CLASS="parameter"
  56727. >array</VAR
  56728. >. The
  56729.      length of <VAR
  56730. CLASS="parameter"
  56731. >array</VAR
  56732. > increases by the number of
  56733.      variables pushed. Has the same effect as:
  56734.      <TABLE
  56735. BORDER="0"
  56736. BGCOLOR="#E0E0E0"
  56737. CELLPADDING="5"
  56738. ><TR
  56739. ><TD
  56740. ><PRE
  56741. CLASS="php"
  56742. ><?php
  56743. $array[] = $var;
  56744. ?></PRE
  56745. ></TD
  56746. ></TR
  56747. ></TABLE
  56748. >
  56749.      repeated for each <VAR
  56750. CLASS="parameter"
  56751. >var</VAR
  56752. >.
  56753.     </P
  56754. ><P
  56755. >      Returns the new number of elements in the array.
  56756.     </P
  56757. ><P
  56758. >      <TABLE
  56759. WIDTH="100%"
  56760. BORDER="0"
  56761. CELLPADDING="0"
  56762. CELLSPACING="0"
  56763. CLASS="EXAMPLE"
  56764. ><TR
  56765. ><TD
  56766. ><DIV
  56767. CLASS="example"
  56768. ><A
  56769. NAME="AEN9877"
  56770. ></A
  56771. ><P
  56772. ><B
  56773. >Example 1. <B
  56774. CLASS="function"
  56775. >array_push()</B
  56776. > example</B
  56777. ></P
  56778. ><TABLE
  56779. BORDER="0"
  56780. BGCOLOR="#E0E0E0"
  56781. CELLPADDING="5"
  56782. ><TR
  56783. ><TD
  56784. ><PRE
  56785. CLASS="php"
  56786. ><?php
  56787. $stack = array("orange", "banana");
  56788. array_push($stack, "apple", "raspberry");
  56789. print_r($stack);
  56790. ?></PRE
  56791. ></TD
  56792. ></TR
  56793. ></TABLE
  56794. ><P
  56795. >        This example would result in <VAR
  56796. CLASS="varname"
  56797. >$stack</VAR
  56798. > having
  56799.        the following elements:
  56800.       </P
  56801. ><TABLE
  56802. BORDER="0"
  56803. BGCOLOR="#E0E0E0"
  56804. CELLPADDING="5"
  56805. ><TR
  56806. ><TD
  56807. ><PRE
  56808. CLASS="php"
  56809. >Array
  56810. (
  56811.     [0] => orange
  56812.     [1] => banana
  56813.     [2] => apple
  56814.     [3] => raspberry
  56815. )</PRE
  56816. ></TD
  56817. ></TR
  56818. ></TABLE
  56819. ></DIV
  56820. ></TD
  56821. ></TR
  56822. ></TABLE
  56823. >
  56824.     </P
  56825. ><DIV
  56826. CLASS="note"
  56827. ><BLOCKQUOTE
  56828. CLASS="note"
  56829. ><P
  56830. ><B
  56831. >Note: </B
  56832. >
  56833.       If you use <B
  56834. CLASS="function"
  56835. >array_push()</B
  56836. > to add one element to the
  56837.       array it's better to use <VAR
  56838. CLASS="literal"
  56839. >$array[] = </VAR
  56840. > because in that
  56841.       way there is no overhead of calling a function.
  56842.      </P
  56843. ></BLOCKQUOTE
  56844. ></DIV
  56845. ><P
  56846. >      See also <A
  56847. HREF="#function.array-pop"
  56848. ><B
  56849. CLASS="function"
  56850. >array_pop()</B
  56851. ></A
  56852. >,
  56853.      <A
  56854. HREF="#function.array-shift"
  56855. ><B
  56856. CLASS="function"
  56857. >array_shift()</B
  56858. ></A
  56859. >, and
  56860.      <A
  56861. HREF="#function.array-unshift"
  56862. ><B
  56863. CLASS="function"
  56864. >array_unshift()</B
  56865. ></A
  56866. >.
  56867.     </P
  56868. ></DIV
  56869. ><H1
  56870. ><A
  56871. NAME="function.array-rand"
  56872. ></A
  56873. >array_rand</H1
  56874. ><DIV
  56875. CLASS="refnamediv"
  56876. ><A
  56877. NAME="AEN9893"
  56878. ></A
  56879. ><P
  56880. >    (PHP 4 , PHP 5)</P
  56881. >array_rand -- 
  56882.      Pick one or more random entries out of an array
  56883.     </DIV
  56884. ><DIV
  56885. CLASS="refsect1"
  56886. ><A
  56887. NAME="AEN9896"
  56888. ></A
  56889. ><H2
  56890. >Description</H2
  56891. >mixed <B
  56892. CLASS="methodname"
  56893. >array_rand</B
  56894. > ( array input [, int num_req])<BR
  56895. ></BR
  56896. ><P
  56897. >      <B
  56898. CLASS="function"
  56899. >array_rand()</B
  56900. > is rather useful when you want to
  56901.      pick one or more random entries out of an array. It takes an
  56902.      <VAR
  56903. CLASS="parameter"
  56904. >input</VAR
  56905. > array and an optional argument
  56906.      <VAR
  56907. CLASS="parameter"
  56908. >num_req</VAR
  56909. > which specifies how many entries you
  56910.      want to pick - if not specified, it defaults to 1.
  56911.     </P
  56912. ><P
  56913. >      If you are picking only one entry, <B
  56914. CLASS="function"
  56915. >array_rand()</B
  56916. >
  56917.      returns the key for a random entry. Otherwise, it returns an array
  56918.      of keys for the random entries. This is done so that you can pick
  56919.      random keys as well as values out of the array.
  56920.     </P
  56921. ><DIV
  56922. CLASS="note"
  56923. ><BLOCKQUOTE
  56924. CLASS="note"
  56925. ><P
  56926. ><B
  56927. >Note: </B
  56928. >As of PHP 4.2.0, there is no need 
  56929. to seed the random number generator with <A
  56930. HREF="#function.srand"
  56931. ><B
  56932. CLASS="function"
  56933. >srand()</B
  56934. ></A
  56935. > or 
  56936. <A
  56937. HREF="#function.mt-srand"
  56938. ><B
  56939. CLASS="function"
  56940. >mt_srand()</B
  56941. ></A
  56942. > as this is now done automatically.
  56943. </P
  56944. ></BLOCKQUOTE
  56945. ></DIV
  56946. ><P
  56947. >      <TABLE
  56948. WIDTH="100%"
  56949. BORDER="0"
  56950. CELLPADDING="0"
  56951. CELLSPACING="0"
  56952. CLASS="EXAMPLE"
  56953. ><TR
  56954. ><TD
  56955. ><DIV
  56956. CLASS="example"
  56957. ><A
  56958. NAME="AEN9918"
  56959. ></A
  56960. ><P
  56961. ><B
  56962. >Example 1. <B
  56963. CLASS="function"
  56964. >array_rand()</B
  56965. > example</B
  56966. ></P
  56967. ><TABLE
  56968. BORDER="0"
  56969. BGCOLOR="#E0E0E0"
  56970. CELLPADDING="5"
  56971. ><TR
  56972. ><TD
  56973. ><PRE
  56974. CLASS="php"
  56975. ><?php
  56976. srand((float) microtime() * 10000000);
  56977. $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
  56978. $rand_keys = array_rand($input, 2);
  56979. echo $input[$rand_keys[0]] . "\n";
  56980. echo $input[$rand_keys[1]] . "\n";
  56981. ?></PRE
  56982. ></TD
  56983. ></TR
  56984. ></TABLE
  56985. ></DIV
  56986. ></TD
  56987. ></TR
  56988. ></TABLE
  56989. >
  56990.     </P
  56991. ><P
  56992. >      See also <A
  56993. HREF="#function.shuffle"
  56994. ><B
  56995. CLASS="function"
  56996. >shuffle()</B
  56997. ></A
  56998. >.
  56999.     </P
  57000. ></DIV
  57001. ><H1
  57002. ><A
  57003. NAME="function.array-reduce"
  57004. ></A
  57005. >array_reduce</H1
  57006. ><DIV
  57007. CLASS="refnamediv"
  57008. ><A
  57009. NAME="AEN9925"
  57010. ></A
  57011. ><P
  57012. >    (PHP 4 >= 4.0.5, PHP 5)</P
  57013. >array_reduce -- 
  57014.      Iteratively reduce the array to a single value using a callback
  57015.      function
  57016.     </DIV
  57017. ><DIV
  57018. CLASS="refsect1"
  57019. ><A
  57020. NAME="AEN9928"
  57021. ></A
  57022. ><H2
  57023. >Description</H2
  57024. >mixed <B
  57025. CLASS="methodname"
  57026. >array_reduce</B
  57027. > ( array input, callback function [, int initial])<BR
  57028. ></BR
  57029. ><P
  57030. >      <B
  57031. CLASS="function"
  57032. >array_reduce()</B
  57033. > applies iteratively the
  57034.      <VAR
  57035. CLASS="parameter"
  57036. >function</VAR
  57037. > function to the elements of the
  57038.      array <VAR
  57039. CLASS="parameter"
  57040. >input</VAR
  57041. >, so as to reduce the array to
  57042.      a single value. If the optional <VAR
  57043. CLASS="parameter"
  57044. >initial</VAR
  57045. > is
  57046.      available, it will be used at the beginning of the process, or as
  57047.      a final result in case the array is empty.
  57048.     </P
  57049. ><P
  57050. >      <TABLE
  57051. WIDTH="100%"
  57052. BORDER="0"
  57053. CELLPADDING="0"
  57054. CELLSPACING="0"
  57055. CLASS="EXAMPLE"
  57056. ><TR
  57057. ><TD
  57058. ><DIV
  57059. CLASS="example"
  57060. ><A
  57061. NAME="AEN9948"
  57062. ></A
  57063. ><P
  57064. ><B
  57065. >Example 1. <B
  57066. CLASS="function"
  57067. >array_reduce()</B
  57068. > example</B
  57069. ></P
  57070. ><TABLE
  57071. BORDER="0"
  57072. BGCOLOR="#E0E0E0"
  57073. CELLPADDING="5"
  57074. ><TR
  57075. ><TD
  57076. ><PRE
  57077. CLASS="php"
  57078. ><?php
  57079. function rsum($v, $w) 
  57080. {
  57081.     $v += $w;
  57082.     return $v;
  57083. }
  57084.  
  57085. function rmul($v, $w) 
  57086. {
  57087.     $v *= $w;
  57088.     return $v;
  57089. }
  57090.  
  57091. $a = array(1, 2, 3, 4, 5);
  57092. $x = array();
  57093. $b = array_reduce($a, "rsum");
  57094. $c = array_reduce($a, "rmul", 10);
  57095. $d = array_reduce($x, "rsum", 1);
  57096. ?></PRE
  57097. ></TD
  57098. ></TR
  57099. ></TABLE
  57100. ></DIV
  57101. ></TD
  57102. ></TR
  57103. ></TABLE
  57104. >
  57105.     </P
  57106. ><P
  57107. >      This will result in <VAR
  57108. CLASS="varname"
  57109. >$b</VAR
  57110. > containing
  57111.      <VAR
  57112. CLASS="literal"
  57113. >15</VAR
  57114. >, <VAR
  57115. CLASS="varname"
  57116. >$c</VAR
  57117. > containing
  57118.      <VAR
  57119. CLASS="literal"
  57120. >1200</VAR
  57121. > (= 1*2*3*4*5*10), and
  57122.      <VAR
  57123. CLASS="varname"
  57124. >$d</VAR
  57125. > containing <VAR
  57126. CLASS="literal"
  57127. >1</VAR
  57128. >.
  57129.     </P
  57130. ><P
  57131. >      See also <A
  57132. HREF="#function.array-filter"
  57133. ><B
  57134. CLASS="function"
  57135. >array_filter()</B
  57136. ></A
  57137. >,
  57138.      <A
  57139. HREF="#function.array-map"
  57140. ><B
  57141. CLASS="function"
  57142. >array_map()</B
  57143. ></A
  57144. >,
  57145.      <A
  57146. HREF="#function.array-unique"
  57147. ><B
  57148. CLASS="function"
  57149. >array_unique()</B
  57150. ></A
  57151. >, and
  57152.      <A
  57153. HREF="#function.array-count-values"
  57154. ><B
  57155. CLASS="function"
  57156. >array_count_values()</B
  57157. ></A
  57158. >.
  57159.     </P
  57160. ></DIV
  57161. ><H1
  57162. ><A
  57163. NAME="function.array-reverse"
  57164. ></A
  57165. >array_reverse</H1
  57166. ><DIV
  57167. CLASS="refnamediv"
  57168. ><A
  57169. NAME="AEN9965"
  57170. ></A
  57171. ><P
  57172. >    (PHP 4 , PHP 5)</P
  57173. >array_reverse -- 
  57174.      Return an array with elements in reverse order
  57175.     </DIV
  57176. ><DIV
  57177. CLASS="refsect1"
  57178. ><A
  57179. NAME="AEN9968"
  57180. ></A
  57181. ><H2
  57182. >Description</H2
  57183. >array <B
  57184. CLASS="methodname"
  57185. >array_reverse</B
  57186. > ( array array [, bool preserve_keys])<BR
  57187. ></BR
  57188. ><P
  57189. >      <B
  57190. CLASS="function"
  57191. >array_reverse()</B
  57192. > takes input
  57193.      <VAR
  57194. CLASS="parameter"
  57195. >array</VAR
  57196. > and returns a new array with the
  57197.      order of the elements reversed, preserving the keys if
  57198.      <VAR
  57199. CLASS="parameter"
  57200. >preserve_keys</VAR
  57201. > is <TT
  57202. CLASS="constant"
  57203. ><B
  57204. >TRUE</B
  57205. ></TT
  57206. >.
  57207.     </P
  57208. ><P
  57209. >      <TABLE
  57210. WIDTH="100%"
  57211. BORDER="0"
  57212. CELLPADDING="0"
  57213. CELLSPACING="0"
  57214. CLASS="EXAMPLE"
  57215. ><TR
  57216. ><TD
  57217. ><DIV
  57218. CLASS="example"
  57219. ><A
  57220. NAME="AEN9985"
  57221. ></A
  57222. ><P
  57223. ><B
  57224. >Example 1. <B
  57225. CLASS="function"
  57226. >array_reverse()</B
  57227. > example</B
  57228. ></P
  57229. ><TABLE
  57230. BORDER="0"
  57231. BGCOLOR="#E0E0E0"
  57232. CELLPADDING="5"
  57233. ><TR
  57234. ><TD
  57235. ><PRE
  57236. CLASS="php"
  57237. ><?php
  57238. $input  = array("php", 4.0, array("green", "red"));
  57239. $result = array_reverse($input);
  57240. $result_keyed = array_reverse($input, true);
  57241. ?></PRE
  57242. ></TD
  57243. ></TR
  57244. ></TABLE
  57245. ><P
  57246. >        This makes both <VAR
  57247. CLASS="varname"
  57248. >$result</VAR
  57249. > and
  57250.        <VAR
  57251. CLASS="varname"
  57252. >$result_keyed</VAR
  57253. > have the same elements, but
  57254.        note the difference between the keys. The printout of
  57255.        <VAR
  57256. CLASS="varname"
  57257. >$result</VAR
  57258. > and
  57259.        <VAR
  57260. CLASS="varname"
  57261. >$result_keyed</VAR
  57262. > will be:
  57263.       </P
  57264. ><TABLE
  57265. BORDER="0"
  57266. BGCOLOR="#E0E0E0"
  57267. CELLPADDING="5"
  57268. ><TR
  57269. ><TD
  57270. ><PRE
  57271. CLASS="php"
  57272. >Array
  57273. (
  57274.     [0] => Array
  57275.         (
  57276.             [0] => green
  57277.             [1] => red
  57278.         )
  57279.  
  57280.     [1] => 4
  57281.     [2] => php
  57282. )
  57283. Array
  57284. (
  57285.     [2] => Array
  57286.         (
  57287.             [0] => green
  57288.             [1] => red
  57289.         )
  57290.  
  57291.     [1] => 4
  57292.     [0] => php
  57293. )</PRE
  57294. ></TD
  57295. ></TR
  57296. ></TABLE
  57297. ></DIV
  57298. ></TD
  57299. ></TR
  57300. ></TABLE
  57301. >
  57302.     </P
  57303. ><DIV
  57304. CLASS="note"
  57305. ><BLOCKQUOTE
  57306. CLASS="note"
  57307. ><P
  57308. ><B
  57309. >Note: </B
  57310. >
  57311.       The second parameter was added in PHP 4.0.3.
  57312.      </P
  57313. ></BLOCKQUOTE
  57314. ></DIV
  57315. ><P
  57316. >      See also <A
  57317. HREF="#function.array-flip"
  57318. ><B
  57319. CLASS="function"
  57320. >array_flip()</B
  57321. ></A
  57322. >.
  57323.     </P
  57324. ></DIV
  57325. ><H1
  57326. ><A
  57327. NAME="function.array-search"
  57328. ></A
  57329. >array_search</H1
  57330. ><DIV
  57331. CLASS="refnamediv"
  57332. ><A
  57333. NAME="AEN10000"
  57334. ></A
  57335. ><P
  57336. >    (PHP 4 >= 4.0.5, PHP 5)</P
  57337. >array_search -- 
  57338.      Searches the array for a given value and returns the
  57339.      corresponding key if successful
  57340.     </DIV
  57341. ><DIV
  57342. CLASS="refsect1"
  57343. ><A
  57344. NAME="AEN10003"
  57345. ></A
  57346. ><H2
  57347. >Description</H2
  57348. >mixed <B
  57349. CLASS="methodname"
  57350. >array_search</B
  57351. > ( mixed needle, array haystack [, bool strict])<BR
  57352. ></BR
  57353. ><P
  57354. >      Searches <VAR
  57355. CLASS="parameter"
  57356. >haystack</VAR
  57357. > for
  57358.      <VAR
  57359. CLASS="parameter"
  57360. >needle</VAR
  57361. > and returns the key if it is found in
  57362.      the array, <TT
  57363. CLASS="constant"
  57364. ><B
  57365. >FALSE</B
  57366. ></TT
  57367. > otherwise.
  57368.     </P
  57369. ><DIV
  57370. CLASS="note"
  57371. ><BLOCKQUOTE
  57372. CLASS="note"
  57373. ><P
  57374. ><B
  57375. >Note: </B
  57376. >
  57377.       If <VAR
  57378. CLASS="parameter"
  57379. >needle</VAR
  57380. > is a string, the comparison is done
  57381.       in a case-sensitive manner.
  57382.      </P
  57383. ></BLOCKQUOTE
  57384. ></DIV
  57385. ><DIV
  57386. CLASS="note"
  57387. ><BLOCKQUOTE
  57388. CLASS="note"
  57389. ><P
  57390. ><B
  57391. >Note: </B
  57392. >
  57393.       Prior to PHP 4.2.0, <B
  57394. CLASS="function"
  57395. >array_search()</B
  57396. > returns
  57397.       <TT
  57398. CLASS="constant"
  57399. ><B
  57400. >NULL</B
  57401. ></TT
  57402. > on failure instead of <TT
  57403. CLASS="constant"
  57404. ><B
  57405. >FALSE</B
  57406. ></TT
  57407. >.
  57408.      </P
  57409. ></BLOCKQUOTE
  57410. ></DIV
  57411. ><P
  57412. >      If the optional third parameter <VAR
  57413. CLASS="parameter"
  57414. >strict</VAR
  57415. > is set to
  57416.      <TT
  57417. CLASS="constant"
  57418. ><B
  57419. >TRUE</B
  57420. ></TT
  57421. > then the <B
  57422. CLASS="function"
  57423. >array_search()</B
  57424. >
  57425.      will also check the types of the <VAR
  57426. CLASS="parameter"
  57427. >needle</VAR
  57428. >
  57429.      in the <VAR
  57430. CLASS="parameter"
  57431. >haystack</VAR
  57432. >.
  57433.     </P
  57434. ><P
  57435. >      If <VAR
  57436. CLASS="parameter"
  57437. >needle</VAR
  57438. > is found in
  57439.      <VAR
  57440. CLASS="parameter"
  57441. >haystack</VAR
  57442. > more than once, the first matching key
  57443.      is returned. To return the keys for all matching values, use
  57444.      <A
  57445. HREF="#function.array-keys"
  57446. ><B
  57447. CLASS="function"
  57448. >array_keys()</B
  57449. ></A
  57450. > with the optional
  57451.      <VAR
  57452. CLASS="parameter"
  57453. >search_value</VAR
  57454. > parameter instead.
  57455.     </P
  57456. ><P
  57457. >      <TABLE
  57458. WIDTH="100%"
  57459. BORDER="0"
  57460. CELLPADDING="0"
  57461. CELLSPACING="0"
  57462. CLASS="EXAMPLE"
  57463. ><TR
  57464. ><TD
  57465. ><DIV
  57466. CLASS="example"
  57467. ><A
  57468. NAME="AEN10041"
  57469. ></A
  57470. ><P
  57471. ><B
  57472. >Example 1. <B
  57473. CLASS="function"
  57474. >array_search()</B
  57475. > example</B
  57476. ></P
  57477. ><TABLE
  57478. BORDER="0"
  57479. BGCOLOR="#E0E0E0"
  57480. CELLPADDING="5"
  57481. ><TR
  57482. ><TD
  57483. ><PRE
  57484. CLASS="php"
  57485. ><?php
  57486. $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
  57487.  
  57488. $key = array_search('green', $array); // $key = 2;
  57489. $key = array_search('red', $array);   // $key = 1;
  57490. ?></PRE
  57491. ></TD
  57492. ></TR
  57493. ></TABLE
  57494. ></DIV
  57495. ></TD
  57496. ></TR
  57497. ></TABLE
  57498. >
  57499.     </P
  57500. ><DIV
  57501. CLASS="warning"
  57502. ><P
  57503. ></P
  57504. ><TABLE
  57505. CLASS="warning"
  57506. BORDER="1"
  57507. WIDTH="100%"
  57508. ><TR
  57509. ><TD
  57510. ALIGN="CENTER"
  57511. ><B
  57512. >Warning</B
  57513. ></TD
  57514. ></TR
  57515. ><TR
  57516. ><TD
  57517. ALIGN="LEFT"
  57518. ><P
  57519. >This function may
  57520. return Boolean <TT
  57521. CLASS="constant"
  57522. ><B
  57523. >FALSE</B
  57524. ></TT
  57525. >, but may also return a non-Boolean value which
  57526. evaluates to <TT
  57527. CLASS="constant"
  57528. ><B
  57529. >FALSE</B
  57530. ></TT
  57531. >, such as <VAR
  57532. CLASS="literal"
  57533. >0</VAR
  57534. > or
  57535. "". Please read the section on <A
  57536. HREF="#language.types.boolean"
  57537. >Booleans</A
  57538. > for more
  57539. information. Use <A
  57540. HREF="#language.operators.comparison"
  57541. >the ===
  57542. operator</A
  57543. > for testing the return value of this
  57544. function.</P
  57545. ></TD
  57546. ></TR
  57547. ></TABLE
  57548. ></DIV
  57549. ><P
  57550. >      See also <A
  57551. HREF="#function.array-keys"
  57552. ><B
  57553. CLASS="function"
  57554. >array_keys()</B
  57555. ></A
  57556. >,
  57557.      <A
  57558. HREF="#function.array-values"
  57559. ><B
  57560. CLASS="function"
  57561. >array_values()</B
  57562. ></A
  57563. >,
  57564.      <A
  57565. HREF="#function.array-key-exists"
  57566. ><B
  57567. CLASS="function"
  57568. >array_key_exists()</B
  57569. ></A
  57570. >, and
  57571.      <A
  57572. HREF="#function.in-array"
  57573. ><B
  57574. CLASS="function"
  57575. >in_array()</B
  57576. ></A
  57577. >.
  57578.     </P
  57579. ></DIV
  57580. ><H1
  57581. ><A
  57582. NAME="function.array-shift"
  57583. ></A
  57584. >array_shift</H1
  57585. ><DIV
  57586. CLASS="refnamediv"
  57587. ><A
  57588. NAME="AEN10058"
  57589. ></A
  57590. ><P
  57591. >    (PHP 4 , PHP 5)</P
  57592. >array_shift -- 
  57593.      Shift an element off the beginning of array
  57594.     </DIV
  57595. ><DIV
  57596. CLASS="refsect1"
  57597. ><A
  57598. NAME="AEN10061"
  57599. ></A
  57600. ><H2
  57601. >Description</H2
  57602. >mixed <B
  57603. CLASS="methodname"
  57604. >array_shift</B
  57605. > ( array &array)<BR
  57606. ></BR
  57607. ><P
  57608. >      <B
  57609. CLASS="function"
  57610. >array_shift()</B
  57611. > shifts the first value of the
  57612.      <VAR
  57613. CLASS="parameter"
  57614. >array</VAR
  57615. > off and returns it, shortening the
  57616.      <VAR
  57617. CLASS="parameter"
  57618. >array</VAR
  57619. > by one element and moving everything
  57620.      down. All numerical array keys will be modified to start counting from zero
  57621.      while literal keys won't be touched. If <VAR
  57622. CLASS="parameter"
  57623. >array</VAR
  57624. > is empty 
  57625.      (or is not an array), <TT
  57626. CLASS="constant"
  57627. ><B
  57628. >NULL</B
  57629. ></TT
  57630. > will be returned.
  57631.     </P
  57632. ><DIV
  57633. CLASS="note"
  57634. ><BLOCKQUOTE
  57635. CLASS="note"
  57636. ><P
  57637. ><B
  57638. >Note: </B
  57639. >This function will 
  57640. <A
  57641. HREF="#function.reset"
  57642. ><B
  57643. CLASS="function"
  57644. >reset()</B
  57645. ></A
  57646. > the <A
  57647. HREF="#language.types.array"
  57648. ><B
  57649. CLASS="type"
  57650. >array</B
  57651. ></A
  57652. > pointer after 
  57653. use.</P
  57654. ></BLOCKQUOTE
  57655. ></DIV
  57656. ><TABLE
  57657. WIDTH="100%"
  57658. BORDER="0"
  57659. CELLPADDING="0"
  57660. CELLSPACING="0"
  57661. CLASS="EXAMPLE"
  57662. ><TR
  57663. ><TD
  57664. ><DIV
  57665. CLASS="example"
  57666. ><A
  57667. NAME="AEN10079"
  57668. ></A
  57669. ><P
  57670. ><B
  57671. >Example 1. <B
  57672. CLASS="function"
  57673. >array_shift()</B
  57674. > example</B
  57675. ></P
  57676. ><TABLE
  57677. BORDER="0"
  57678. BGCOLOR="#E0E0E0"
  57679. CELLPADDING="5"
  57680. ><TR
  57681. ><TD
  57682. ><PRE
  57683. CLASS="php"
  57684. ><?php
  57685. $stack = array("orange", "banana", "apple", "raspberry");
  57686. $fruit = array_shift($stack);
  57687. print_r($stack);
  57688. ?></PRE
  57689. ></TD
  57690. ></TR
  57691. ></TABLE
  57692. ><P
  57693. >       This would result in <VAR
  57694. CLASS="varname"
  57695. >$stack</VAR
  57696. > having 3 elements left:
  57697.      </P
  57698. ><TABLE
  57699. BORDER="0"
  57700. BGCOLOR="#E0E0E0"
  57701. CELLPADDING="5"
  57702. ><TR
  57703. ><TD
  57704. ><PRE
  57705. CLASS="php"
  57706. >Array
  57707. (
  57708.     [0] => banana
  57709.     [1] => apple
  57710.     [2] => raspberry
  57711. )</PRE
  57712. ></TD
  57713. ></TR
  57714. ></TABLE
  57715. ><P
  57716. >       and <VAR
  57717. CLASS="literal"
  57718. >orange</VAR
  57719. > will be assigned to
  57720.       <VAR
  57721. CLASS="varname"
  57722. >$fruit</VAR
  57723. >.
  57724.      </P
  57725. ></DIV
  57726. ></TD
  57727. ></TR
  57728. ></TABLE
  57729. ><P
  57730. >      See also <A
  57731. HREF="#function.array-unshift"
  57732. ><B
  57733. CLASS="function"
  57734. >array_unshift()</B
  57735. ></A
  57736. >,
  57737.      <A
  57738. HREF="#function.array-push"
  57739. ><B
  57740. CLASS="function"
  57741. >array_push()</B
  57742. ></A
  57743. >, and
  57744.      <A
  57745. HREF="#function.array-pop"
  57746. ><B
  57747. CLASS="function"
  57748. >array_pop()</B
  57749. ></A
  57750. >.
  57751.     </P
  57752. ></DIV
  57753. ><H1
  57754. ><A
  57755. NAME="function.array-slice"
  57756. ></A
  57757. >array_slice</H1
  57758. ><DIV
  57759. CLASS="refnamediv"
  57760. ><A
  57761. NAME="AEN10094"
  57762. ></A
  57763. ><P
  57764. >    (PHP 4 , PHP 5)</P
  57765. >array_slice -- Extract a slice of the array</DIV
  57766. ><DIV
  57767. CLASS="refsect1"
  57768. ><A
  57769. NAME="AEN10097"
  57770. ></A
  57771. ><H2
  57772. >Description</H2
  57773. >array <B
  57774. CLASS="methodname"
  57775. >array_slice</B
  57776. > ( array array, int offset [, int length [, bool preserve_keys]])<BR
  57777. ></BR
  57778. ><P
  57779. >      <B
  57780. CLASS="function"
  57781. >array_slice()</B
  57782. > returns the sequence of elements
  57783.      from the array <VAR
  57784. CLASS="parameter"
  57785. >array</VAR
  57786. > as specified by the
  57787.      <VAR
  57788. CLASS="parameter"
  57789. >offset</VAR
  57790. > and <VAR
  57791. CLASS="parameter"
  57792. >length</VAR
  57793. >
  57794.      parameters.
  57795.     </P
  57796. ><P
  57797. >      If <VAR
  57798. CLASS="parameter"
  57799. >offset</VAR
  57800. > is positive, the sequence will
  57801.      start at that offset in the <VAR
  57802. CLASS="parameter"
  57803. >array</VAR
  57804. >.  If
  57805.      <VAR
  57806. CLASS="parameter"
  57807. >offset</VAR
  57808. > is negative, the sequence will
  57809.      start that far from the end of the <VAR
  57810. CLASS="parameter"
  57811. >array</VAR
  57812. >.
  57813.     </P
  57814. ><P
  57815. >      If <VAR
  57816. CLASS="parameter"
  57817. >length</VAR
  57818. > is given and is positive, then
  57819.      the sequence will have that many elements in it. If
  57820.      <VAR
  57821. CLASS="parameter"
  57822. >length</VAR
  57823. > is given and is negative then the
  57824.      sequence will stop that many elements from the end of the
  57825.      array. If it is omitted, then the sequence will have everything
  57826.      from <VAR
  57827. CLASS="parameter"
  57828. >offset</VAR
  57829. > up until the end of the
  57830.      <VAR
  57831. CLASS="parameter"
  57832. >array</VAR
  57833. >.
  57834.     </P
  57835. ><P
  57836. >      Note that <B
  57837. CLASS="function"
  57838. >array_slice()</B
  57839. > will reset the array keys by
  57840.      default. Since PHP 5.0.2, you can change this behaviour by setting
  57841.      <VAR
  57842. CLASS="parameter"
  57843. >preserve_keys</VAR
  57844. > to <TT
  57845. CLASS="constant"
  57846. ><B
  57847. >TRUE</B
  57848. ></TT
  57849. >.
  57850.     </P
  57851. ><P
  57852. >      <TABLE
  57853. WIDTH="100%"
  57854. BORDER="0"
  57855. CELLPADDING="0"
  57856. CELLSPACING="0"
  57857. CLASS="EXAMPLE"
  57858. ><TR
  57859. ><TD
  57860. ><DIV
  57861. CLASS="example"
  57862. ><A
  57863. NAME="AEN10134"
  57864. ></A
  57865. ><P
  57866. ><B
  57867. >Example 1. <B
  57868. CLASS="function"
  57869. >array_slice()</B
  57870. > examples</B
  57871. ></P
  57872. ><TABLE
  57873. BORDER="0"
  57874. BGCOLOR="#E0E0E0"
  57875. CELLPADDING="5"
  57876. ><TR
  57877. ><TD
  57878. ><PRE
  57879. CLASS="php"
  57880. ><?php
  57881. $input = array("a", "b", "c", "d", "e");
  57882.  
  57883. $output = array_slice($input, 2);      // returns "c", "d", and "e"
  57884. $output = array_slice($input, -2, 1);  // returns "d"
  57885. $output = array_slice($input, 0, 3);   // returns "a", "b", and "c"
  57886.  
  57887. // note the differences in the array keys
  57888. print_r(array_slice($input, 2, -1));
  57889. print_r(array_slice($input, 2, -1, true));
  57890. ?></PRE
  57891. ></TD
  57892. ></TR
  57893. ></TABLE
  57894. ><P
  57895. >The above example will output:</P
  57896. ><TABLE
  57897. BORDER="0"
  57898. BGCOLOR="#E0E0E0"
  57899. CELLPADDING="5"
  57900. ><TR
  57901. ><TD
  57902. ><PRE
  57903. CLASS="screen"
  57904. >Array
  57905. (
  57906.     [0] => c
  57907.     [1] => d
  57908. )
  57909. Array
  57910. (
  57911.     [2] => c
  57912.     [3] => d
  57913. )</PRE
  57914. ></TD
  57915. ></TR
  57916. ></TABLE
  57917. ></DIV
  57918. ></TD
  57919. ></TR
  57920. ></TABLE
  57921. >
  57922.     </P
  57923. ><P
  57924. >      See also <A
  57925. HREF="#function.array-splice"
  57926. ><B
  57927. CLASS="function"
  57928. >array_splice()</B
  57929. ></A
  57930. > and
  57931.      <A
  57932. HREF="#function.unset"
  57933. ><B
  57934. CLASS="function"
  57935. >unset()</B
  57936. ></A
  57937. >.
  57938.     </P
  57939. ></DIV
  57940. ><H1
  57941. ><A
  57942. NAME="function.array-splice"
  57943. ></A
  57944. >array_splice</H1
  57945. ><DIV
  57946. CLASS="refnamediv"
  57947. ><A
  57948. NAME="AEN10144"
  57949. ></A
  57950. ><P
  57951. >    (PHP 4 , PHP 5)</P
  57952. >array_splice -- 
  57953.      Remove a portion of the array and replace it with something
  57954.      else
  57955.     </DIV
  57956. ><DIV
  57957. CLASS="refsect1"
  57958. ><A
  57959. NAME="AEN10147"
  57960. ></A
  57961. ><H2
  57962. >Description</H2
  57963. >array <B
  57964. CLASS="methodname"
  57965. >array_splice</B
  57966. > ( array &input, int offset [, int length [, array replacement]])<BR
  57967. ></BR
  57968. ><P
  57969. >      <B
  57970. CLASS="function"
  57971. >array_splice()</B
  57972. > removes the elements designated
  57973.      by <VAR
  57974. CLASS="parameter"
  57975. >offset</VAR
  57976. > and
  57977.      <VAR
  57978. CLASS="parameter"
  57979. >length</VAR
  57980. > from the
  57981.      <VAR
  57982. CLASS="parameter"
  57983. >input</VAR
  57984. > array, and replaces them with the
  57985.      elements of the <VAR
  57986. CLASS="parameter"
  57987. >replacement</VAR
  57988. > array, if
  57989.      supplied. It returns an array containing the extracted elements.
  57990.     </P
  57991. ><P
  57992. >      If <VAR
  57993. CLASS="parameter"
  57994. >offset</VAR
  57995. > is positive then the start of
  57996.      removed portion is at that offset from the beginning of the
  57997.      <VAR
  57998. CLASS="parameter"
  57999. >input</VAR
  58000. > array.  If
  58001.      <VAR
  58002. CLASS="parameter"
  58003. >offset</VAR
  58004. > is negative then it starts that far
  58005.      from the end of the <VAR
  58006. CLASS="parameter"
  58007. >input</VAR
  58008. > array.
  58009.     </P
  58010. ><P
  58011. >      If <VAR
  58012. CLASS="parameter"
  58013. >length</VAR
  58014. > is omitted, removes everything
  58015.      from <VAR
  58016. CLASS="parameter"
  58017. >offset</VAR
  58018. > to the end of the array.  If
  58019.      <VAR
  58020. CLASS="parameter"
  58021. >length</VAR
  58022. > is specified and is positive, then
  58023.      that many elements will be removed. If
  58024.      <VAR
  58025. CLASS="parameter"
  58026. >length</VAR
  58027. > is specified and is negative then
  58028.      the end of the removed portion will be that many elements from
  58029.      the end of the array.  Tip: to remove everything from
  58030.      <VAR
  58031. CLASS="parameter"
  58032. >offset</VAR
  58033. > to the end of the array when
  58034.      <VAR
  58035. CLASS="parameter"
  58036. >replacement</VAR
  58037. > is also specified, use
  58038.      <VAR
  58039. CLASS="literal"
  58040. >count($input)</VAR
  58041. > for
  58042.      <VAR
  58043. CLASS="parameter"
  58044. >length</VAR
  58045. >.
  58046.     </P
  58047. ><P
  58048. >      If <VAR
  58049. CLASS="parameter"
  58050. >replacement</VAR
  58051. > array is specified, then
  58052.      the removed elements are replaced with elements from this array.
  58053.      If <VAR
  58054. CLASS="parameter"
  58055. >offset</VAR
  58056. > and
  58057.      <VAR
  58058. CLASS="parameter"
  58059. >length</VAR
  58060. > are such that nothing is removed,
  58061.      then the elements from the <VAR
  58062. CLASS="parameter"
  58063. >replacement</VAR
  58064. >
  58065.      array are inserted in the place specified by the
  58066.      <VAR
  58067. CLASS="parameter"
  58068. >offset</VAR
  58069. >. Tip: if the replacement is just
  58070.      one element it is not necessary to put <VAR
  58071. CLASS="literal"
  58072. >array()</VAR
  58073. >
  58074.      around it, unless the element is an array itself.
  58075.     </P
  58076. ><P
  58077. >      The following statements change the values of <VAR
  58078. CLASS="varname"
  58079. >$input</VAR
  58080. >
  58081.      the same way:
  58082.    <DIV
  58083. CLASS="table"
  58084. ><A
  58085. NAME="AEN10193"
  58086. ></A
  58087. ><P
  58088. ><B
  58089. >Table 1. <B
  58090. CLASS="function"
  58091. >array_splice()</B
  58092. > equivalents</B
  58093. ></P
  58094. ><TABLE
  58095. BORDER="1"
  58096. CLASS="CALSTABLE"
  58097. ><COL><COL><TBODY
  58098. ><TR
  58099. ><TD
  58100. >         array_push($input, $x, $y) 
  58101.        </TD
  58102. ><TD
  58103. >         array_splice($input, count($input), 0, array($x, $y))
  58104.        </TD
  58105. ></TR
  58106. ><TR
  58107. ><TD
  58108. >         array_pop($input) 
  58109.        </TD
  58110. ><TD
  58111. >         array_splice($input, -1)
  58112.        </TD
  58113. ></TR
  58114. ><TR
  58115. ><TD
  58116. >         array_shift($input)
  58117.        </TD
  58118. ><TD
  58119. >         array_splice($input, 0, 1)
  58120.        </TD
  58121. ></TR
  58122. ><TR
  58123. ><TD
  58124. >         array_unshift($input, $x, $y)
  58125.        </TD
  58126. ><TD
  58127. >         array_splice($input, 0, 0, array($x, $y))
  58128.        </TD
  58129. ></TR
  58130. ><TR
  58131. ><TD
  58132. >         $input[$x] = $y // for arrays where key equals offset
  58133.        </TD
  58134. ><TD
  58135. >         array_splice($input, $x, 1, $y)
  58136.        </TD
  58137. ></TR
  58138. ></TBODY
  58139. ></TABLE
  58140. ></DIV
  58141. >
  58142.     </P
  58143. ><P
  58144. >      Returns the array consisting of removed elements.
  58145.     </P
  58146. ><P
  58147. >      <TABLE
  58148. WIDTH="100%"
  58149. BORDER="0"
  58150. CELLPADDING="0"
  58151. CELLSPACING="0"
  58152. CLASS="EXAMPLE"
  58153. ><TR
  58154. ><TD
  58155. ><DIV
  58156. CLASS="example"
  58157. ><A
  58158. NAME="AEN10215"
  58159. ></A
  58160. ><P
  58161. ><B
  58162. >Example 1. <B
  58163. CLASS="function"
  58164. >array_splice()</B
  58165. > examples</B
  58166. ></P
  58167. ><TABLE
  58168. BORDER="0"
  58169. BGCOLOR="#E0E0E0"
  58170. CELLPADDING="5"
  58171. ><TR
  58172. ><TD
  58173. ><PRE
  58174. CLASS="php"
  58175. ><?php
  58176. $input = array("red", "green", "blue", "yellow");
  58177. array_splice($input, 2);
  58178. // $input is now array("red", "green")
  58179.  
  58180. $input = array("red", "green", "blue", "yellow");
  58181. array_splice($input, 1, -1);
  58182. // $input is now array("red", "yellow")
  58183.  
  58184. $input = array("red", "green", "blue", "yellow");
  58185. array_splice($input, 1, count($input), "orange");
  58186. // $input is now array("red", "orange")
  58187.  
  58188. $input = array("red", "green", "blue", "yellow");
  58189. array_splice($input, -1, 1, array("black", "maroon"));
  58190. // $input is now array("red", "green",
  58191. //          "blue", "black", "maroon")
  58192.  
  58193. $input = array("red", "green", "blue", "yellow");
  58194. array_splice($input, 3, 0, "purple");
  58195. // $input is now array("red", "green",
  58196. //          "blue", "purple", "yellow");
  58197. ?></PRE
  58198. ></TD
  58199. ></TR
  58200. ></TABLE
  58201. ></DIV
  58202. ></TD
  58203. ></TR
  58204. ></TABLE
  58205. >
  58206.     </P
  58207. ><P
  58208. >      See also <A
  58209. HREF="#function.array-slice"
  58210. ><B
  58211. CLASS="function"
  58212. >array_slice()</B
  58213. ></A
  58214. >, 
  58215.      <A
  58216. HREF="#function.unset"
  58217. ><B
  58218. CLASS="function"
  58219. >unset()</B
  58220. ></A
  58221. >, and
  58222.      <A
  58223. HREF="#function.array-merge"
  58224. ><B
  58225. CLASS="function"
  58226. >array_merge()</B
  58227. ></A
  58228. >.
  58229.     </P
  58230. ></DIV
  58231. ><H1
  58232. ><A
  58233. NAME="function.array-sum"
  58234. ></A
  58235. >array_sum</H1
  58236. ><DIV
  58237. CLASS="refnamediv"
  58238. ><A
  58239. NAME="AEN10224"
  58240. ></A
  58241. ><P
  58242. >    (PHP 4 >= 4.0.4, PHP 5)</P
  58243. >array_sum -- 
  58244.      Calculate the sum of values in an array
  58245.     </DIV
  58246. ><DIV
  58247. CLASS="refsect1"
  58248. ><A
  58249. NAME="AEN10227"
  58250. ></A
  58251. ><H2
  58252. >Description</H2
  58253. >number <B
  58254. CLASS="methodname"
  58255. >array_sum</B
  58256. > ( array array)<BR
  58257. ></BR
  58258. ><P
  58259. >      <B
  58260. CLASS="function"
  58261. >array_sum()</B
  58262. > returns the sum of values
  58263.      in an array as an integer or float.
  58264.     </P
  58265. ><P
  58266. >      <TABLE
  58267. WIDTH="100%"
  58268. BORDER="0"
  58269. CELLPADDING="0"
  58270. CELLSPACING="0"
  58271. CLASS="EXAMPLE"
  58272. ><TR
  58273. ><TD
  58274. ><DIV
  58275. CLASS="example"
  58276. ><A
  58277. NAME="AEN10238"
  58278. ></A
  58279. ><P
  58280. ><B
  58281. >Example 1. <B
  58282. CLASS="function"
  58283. >array_sum()</B
  58284. > examples</B
  58285. ></P
  58286. ><TABLE
  58287. BORDER="0"
  58288. BGCOLOR="#E0E0E0"
  58289. CELLPADDING="5"
  58290. ><TR
  58291. ><TD
  58292. ><PRE
  58293. CLASS="php"
  58294. ><?php
  58295. $a = array(2, 4, 6, 8);
  58296. echo "sum(a) = " . array_sum($a) . "\n";
  58297.  
  58298. $b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
  58299. echo "sum(b) = " . array_sum($b) . "\n";
  58300. ?></PRE
  58301. ></TD
  58302. ></TR
  58303. ></TABLE
  58304. ><P
  58305. >The above example will output:</P
  58306. ><TABLE
  58307. BORDER="0"
  58308. BGCOLOR="#E0E0E0"
  58309. CELLPADDING="5"
  58310. ><TR
  58311. ><TD
  58312. ><PRE
  58313. CLASS="screen"
  58314. >sum(a) = 20
  58315. sum(b) = 6.9</PRE
  58316. ></TD
  58317. ></TR
  58318. ></TABLE
  58319. ></DIV
  58320. ></TD
  58321. ></TR
  58322. ></TABLE
  58323. >
  58324.     </P
  58325. ><DIV
  58326. CLASS="note"
  58327. ><BLOCKQUOTE
  58328. CLASS="note"
  58329. ><P
  58330. ><B
  58331. >Note: </B
  58332. >
  58333.       PHP versions prior to 4.2.1 modified the passed array
  58334.       itself and converted strings to numbers (which most
  58335.       of the time converted them to zero, depending on
  58336.       their value).
  58337.      </P
  58338. ></BLOCKQUOTE
  58339. ></DIV
  58340. ></DIV
  58341. ><H1
  58342. ><A
  58343. NAME="function.array-udiff-assoc"
  58344. ></A
  58345. >array_udiff_assoc</H1
  58346. ><DIV
  58347. CLASS="refnamediv"
  58348. ><A
  58349. NAME="AEN10247"
  58350. ></A
  58351. ><P
  58352. >    (PHP 5)</P
  58353. >array_udiff_assoc -- Computes the difference of arrays with additional index check, compares data by a callback function</DIV
  58354. ><DIV
  58355. CLASS="refsect1"
  58356. ><A
  58357. NAME="AEN10250"
  58358. ></A
  58359. ><H2
  58360. >Description</H2
  58361. >array <B
  58362. CLASS="methodname"
  58363. >array_udiff_assoc</B
  58364. > ( array array1, array array2 [, array ..., callback data_compare_func])<BR
  58365. ></BR
  58366. ><P
  58367. >      <B
  58368. CLASS="function"
  58369. >array_udiff_assoc()</B
  58370. > returns an <A
  58371. HREF="#language.types.array"
  58372. ><B
  58373. CLASS="type"
  58374. >array</B
  58375. ></A
  58376. >
  58377.      containing all the values from <VAR
  58378. CLASS="parameter"
  58379. >array1</VAR
  58380. >
  58381.      that are not present in any of the other arguments.
  58382.      Note that the keys are used in the comparison unlike 
  58383.      <A
  58384. HREF="#function.array-diff"
  58385. ><B
  58386. CLASS="function"
  58387. >array_diff()</B
  58388. ></A
  58389. > and <A
  58390. HREF="#function.array-udiff"
  58391. ><B
  58392. CLASS="function"
  58393. >array_udiff()</B
  58394. ></A
  58395. >.
  58396.      The comparison of arrays' data is performed by using an user-supplied
  58397.      callback. In this aspect the behaviour is opposite to the behaviour of
  58398.      <A
  58399. HREF="#function.array-diff-assoc"
  58400. ><B
  58401. CLASS="function"
  58402. >array_diff_assoc()</B
  58403. ></A
  58404. > which uses internal function for
  58405.      comparison.
  58406.     </P
  58407. ><P
  58408. >      <TABLE
  58409. WIDTH="100%"
  58410. BORDER="0"
  58411. CELLPADDING="0"
  58412. CELLSPACING="0"
  58413. CLASS="EXAMPLE"
  58414. ><TR
  58415. ><TD
  58416. ><DIV
  58417. CLASS="example"
  58418. ><A
  58419. NAME="AEN10275"
  58420. ></A
  58421. ><P
  58422. ><B
  58423. >Example 1. <B
  58424. CLASS="function"
  58425. >array_udiff_assoc()</B
  58426. > example</B
  58427. ></P
  58428. ><TABLE
  58429. BORDER="0"
  58430. BGCOLOR="#E0E0E0"
  58431. CELLPADDING="5"
  58432. ><TR
  58433. ><TD
  58434. ><PRE
  58435. CLASS="php"
  58436. ><?php
  58437. class cr {
  58438.     private $priv_member;
  58439.     function cr($val) 
  58440.     {
  58441.         $this->priv_member = $val;
  58442.     }
  58443.     
  58444.     function comp_func_cr($a, $b) 
  58445.     {
  58446.         if ($a->priv_member === $b->priv_member) return 0;
  58447.         return ($a->priv_member > $b->priv_member)? 1:-1;
  58448.     }
  58449. }
  58450.  
  58451. $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
  58452. $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
  58453.  
  58454. $result = array_udiff_assoc($a, $b, array("cr", "comp_func_cr"));
  58455. print_r($result);
  58456. ?></PRE
  58457. ></TD
  58458. ></TR
  58459. ></TABLE
  58460. ><P
  58461. >The above example will output:</P
  58462. ><TABLE
  58463. BORDER="0"
  58464. BGCOLOR="#E0E0E0"
  58465. CELLPADDING="5"
  58466. ><TR
  58467. ><TD
  58468. ><PRE
  58469. CLASS="screen"
  58470. >Array
  58471. (
  58472.     [0.1] => cr Object
  58473.         (
  58474.             [priv_member:private] => 9
  58475.         )
  58476.  
  58477.     [0.5] => cr Object
  58478.         (
  58479.             [priv_member:private] => 12
  58480.         )
  58481.  
  58482.     [0] => cr Object
  58483.         (
  58484.             [priv_member:private] => 23
  58485.         )
  58486. )</PRE
  58487. ></TD
  58488. ></TR
  58489. ></TABLE
  58490. ></DIV
  58491. ></TD
  58492. ></TR
  58493. ></TABLE
  58494. >
  58495.     </P
  58496. ><P
  58497. >      In our example above you see the <VAR
  58498. CLASS="literal"
  58499. >"1" => new cr(4)</VAR
  58500. >
  58501.      pair is present in both arrays and thus it is not in the ouput from the
  58502.      function.
  58503.     </P
  58504. ><P
  58505. >      For comparison is used the user supplied callback function.
  58506.      It must return an integer less than, equal
  58507.      to, or greater than zero if the first argument is considered to
  58508.      be respectively less than, equal to, or greater than the
  58509.      second.
  58510.     </P
  58511. ><DIV
  58512. CLASS="note"
  58513. ><BLOCKQUOTE
  58514. CLASS="note"
  58515. ><P
  58516. ><B
  58517. >Note: </B
  58518. >
  58519.       Please note that this function only checks one dimension of a n-dimensional
  58520.       array. Of course you can check deeper dimensions by using, for example, 
  58521.       <VAR
  58522. CLASS="literal"
  58523. >array_udiff_assoc($array1[0], $array2[0], "some_comparison_func");</VAR
  58524. >.
  58525.      </P
  58526. ></BLOCKQUOTE
  58527. ></DIV
  58528. ><P
  58529. >      See also
  58530.      <A
  58531. HREF="#function.array-diff"
  58532. ><B
  58533. CLASS="function"
  58534. >array_diff()</B
  58535. ></A
  58536. >,
  58537.      <A
  58538. HREF="#function.array-diff-assoc"
  58539. ><B
  58540. CLASS="function"
  58541. >array_diff_assoc()</B
  58542. ></A
  58543. >,
  58544.      <A
  58545. HREF="#function.array-diff-uassoc"
  58546. ><B
  58547. CLASS="function"
  58548. >array_diff_uassoc()</B
  58549. ></A
  58550. >,
  58551.      <A
  58552. HREF="#function.array-udiff"
  58553. ><B
  58554. CLASS="function"
  58555. >array_udiff()</B
  58556. ></A
  58557. >,
  58558.      <A
  58559. HREF="#function.array-udiff-uassoc"
  58560. ><B
  58561. CLASS="function"
  58562. >array_udiff_uassoc()</B
  58563. ></A
  58564. >, 
  58565.      <A
  58566. HREF="#function.array-intersect"
  58567. ><B
  58568. CLASS="function"
  58569. >array_intersect()</B
  58570. ></A
  58571. >,  
  58572.      <A
  58573. HREF="#function.array-intersect-assoc"
  58574. ><B
  58575. CLASS="function"
  58576. >array_intersect_assoc()</B
  58577. ></A
  58578. >,
  58579.      <A
  58580. HREF="#function.array-uintersect"
  58581. ><B
  58582. CLASS="function"
  58583. >array_uintersect()</B
  58584. ></A
  58585. >,  
  58586.      <A
  58587. HREF="#function.array-uintersect-assoc"
  58588. ><B
  58589. CLASS="function"
  58590. >array_uintersect_assoc()</B
  58591. ></A
  58592. > and 
  58593.      <A
  58594. HREF="#function.array-uintersect-uassoc"
  58595. ><B
  58596. CLASS="function"
  58597. >array_uintersect_uassoc()</B
  58598. ></A
  58599. >.
  58600.     </P
  58601. ></DIV
  58602. ><H1
  58603. ><A
  58604. NAME="function.array-udiff-uassoc"
  58605. ></A
  58606. >array_udiff_uassoc</H1
  58607. ><DIV
  58608. CLASS="refnamediv"
  58609. ><A
  58610. NAME="AEN10299"
  58611. ></A
  58612. ><P
  58613. >    (PHP 5)</P
  58614. >array_udiff_uassoc -- Computes the difference of arrays with additional index check, compares data and indexes by a callback function</DIV
  58615. ><DIV
  58616. CLASS="refsect1"
  58617. ><A
  58618. NAME="AEN10302"
  58619. ></A
  58620. ><H2
  58621. >Description</H2
  58622. >array <B
  58623. CLASS="methodname"
  58624. >array_udiff_uassoc</B
  58625. > ( array array1, array array2 [, array ..., callback data_compare_func, callback key_compare_func])<BR
  58626. ></BR
  58627. ><P
  58628. >      <B
  58629. CLASS="function"
  58630. >array_udiff_uassoc()</B
  58631. > returns an <A
  58632. HREF="#language.types.array"
  58633. ><B
  58634. CLASS="type"
  58635. >array</B
  58636. ></A
  58637. >
  58638.      containing all the values from <VAR
  58639. CLASS="parameter"
  58640. >array1</VAR
  58641. >
  58642.      that are not present in any of the other arguments.
  58643.      Note that the keys are used in the comparison unlike 
  58644.      <A
  58645. HREF="#function.array-diff"
  58646. ><B
  58647. CLASS="function"
  58648. >array_diff()</B
  58649. ></A
  58650. > and <A
  58651. HREF="#function.array-udiff"
  58652. ><B
  58653. CLASS="function"
  58654. >array_udiff()</B
  58655. ></A
  58656. >.
  58657.      The comparison of arrays' data is performed by using an user-supplied
  58658.      callback : <VAR
  58659. CLASS="parameter"
  58660. >data_compare_func</VAR
  58661. >. In this aspect
  58662.      the behaviour is opposite to the behaviour of
  58663.      <A
  58664. HREF="#function.array-diff-assoc"
  58665. ><B
  58666. CLASS="function"
  58667. >array_diff_assoc()</B
  58668. ></A
  58669. > which uses internal function for
  58670.      comparison. The comparison of keys (indices) is done also by the 
  58671.      callback function <VAR
  58672. CLASS="parameter"
  58673. >key_compare_func</VAR
  58674. >. This
  58675.      behaviour is unlike what <A
  58676. HREF="#function.array-udiff-assoc"
  58677. ><B
  58678. CLASS="function"
  58679. >array_udiff_assoc()</B
  58680. ></A
  58681. > does, since
  58682.      the latter compares the indices by using an internal function.
  58683.     </P
  58684. ><P
  58685. >      <TABLE
  58686. WIDTH="100%"
  58687. BORDER="0"
  58688. CELLPADDING="0"
  58689. CELLSPACING="0"
  58690. CLASS="EXAMPLE"
  58691. ><TR
  58692. ><TD
  58693. ><DIV
  58694. CLASS="example"
  58695. ><A
  58696. NAME="AEN10333"
  58697. ></A
  58698. ><P
  58699. ><B
  58700. >Example 1. <B
  58701. CLASS="function"
  58702. >array_udiff_uassoc()</B
  58703. > example</B
  58704. ></P
  58705. ><TABLE
  58706. BORDER="0"
  58707. BGCOLOR="#E0E0E0"
  58708. CELLPADDING="5"
  58709. ><TR
  58710. ><TD
  58711. ><PRE
  58712. CLASS="php"
  58713. ><?php
  58714. class cr {
  58715.     private $priv_member;
  58716.     function cr($val) 
  58717.     {
  58718.         $this->priv_member = $val;
  58719.     }
  58720.  
  58721.     function comp_func_cr($a, $b) 
  58722.     {
  58723.         if ($a->priv_member === $b->priv_member) return 0;
  58724.         return ($a->priv_member > $b->priv_member)? 1:-1;
  58725.     }
  58726.     
  58727.     function comp_func_key($a, $b) 
  58728.     {
  58729.         if ($a === $b) return 0;
  58730.         return ($a > $b)? 1:-1;
  58731.     }
  58732. }
  58733. $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
  58734. $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
  58735.  
  58736. $result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
  58737. print_r($result);
  58738. ?></PRE
  58739. ></TD
  58740. ></TR
  58741. ></TABLE
  58742. ><P
  58743. >The above example will output:</P
  58744. ><TABLE
  58745. BORDER="0"
  58746. BGCOLOR="#E0E0E0"
  58747. CELLPADDING="5"
  58748. ><TR
  58749. ><TD
  58750. ><PRE
  58751. CLASS="screen"
  58752. >Array
  58753. (
  58754.     [0.1] => cr Object
  58755.         (
  58756.             [priv_member:private] => 9
  58757.         )
  58758.  
  58759.     [0.5] => cr Object
  58760.         (
  58761.             [priv_member:private] => 12
  58762.         )
  58763.  
  58764.     [0] => cr Object
  58765.         (
  58766.             [priv_member:private] => 23
  58767.         )
  58768. )</PRE
  58769. ></TD
  58770. ></TR
  58771. ></TABLE
  58772. ></DIV
  58773. ></TD
  58774. ></TR
  58775. ></TABLE
  58776. >
  58777.     </P
  58778. ><P
  58779. >      In our example above you see the <VAR
  58780. CLASS="literal"
  58781. >"1" => new cr(4)</VAR
  58782. >
  58783.      pair is present in both arrays and thus it is not in the ouput from the
  58784.      function. Keep in mind that you have to supply 2 callback functions.
  58785.     </P
  58786. ><P
  58787. >      For comparison is used the user supplied callback function.
  58788.      It must return an integer less than, equal
  58789.      to, or greater than zero if the first argument is considered to
  58790.      be respectively less than, equal to, or greater than the
  58791.      second.
  58792.     </P
  58793. ><DIV
  58794. CLASS="note"
  58795. ><BLOCKQUOTE
  58796. CLASS="note"
  58797. ><P
  58798. ><B
  58799. >Note: </B
  58800. >
  58801.       Please note that this function only checks one dimension of a n-dimensional
  58802.       array. Of course you can check deeper dimensions by using, for example, 
  58803.       <VAR
  58804. CLASS="literal"
  58805. >array_udiff_uassoc($array1[0], $array2[0], "data_compare_func",
  58806.       "key_compare_func");</VAR
  58807. >.
  58808.      </P
  58809. ></BLOCKQUOTE
  58810. ></DIV
  58811. ><P
  58812. >      See also
  58813.      <A
  58814. HREF="#function.array-diff"
  58815. ><B
  58816. CLASS="function"
  58817. >array_diff()</B
  58818. ></A
  58819. >,
  58820.      <A
  58821. HREF="#function.array-diff-assoc"
  58822. ><B
  58823. CLASS="function"
  58824. >array_diff_assoc()</B
  58825. ></A
  58826. >,
  58827.      <A
  58828. HREF="#function.array-diff-uassoc"
  58829. ><B
  58830. CLASS="function"
  58831. >array_diff_uassoc()</B
  58832. ></A
  58833. >,
  58834.      <A
  58835. HREF="#function.array-udiff"
  58836. ><B
  58837. CLASS="function"
  58838. >array_udiff()</B
  58839. ></A
  58840. >,
  58841.      <A
  58842. HREF="#function.array-udiff-assoc"
  58843. ><B
  58844. CLASS="function"
  58845. >array_udiff_assoc()</B
  58846. ></A
  58847. >, 
  58848.      <A
  58849. HREF="#function.array-intersect"
  58850. ><B
  58851. CLASS="function"
  58852. >array_intersect()</B
  58853. ></A
  58854. >,  
  58855.      <A
  58856. HREF="#function.array-intersect-assoc"
  58857. ><B
  58858. CLASS="function"
  58859. >array_intersect_assoc()</B
  58860. ></A
  58861. >,
  58862.      <A
  58863. HREF="#function.array-uintersect"
  58864. ><B
  58865. CLASS="function"
  58866. >array_uintersect()</B
  58867. ></A
  58868. >,  
  58869.      <A
  58870. HREF="#function.array-uintersect-assoc"
  58871. ><B
  58872. CLASS="function"
  58873. >array_uintersect_assoc()</B
  58874. ></A
  58875. > and 
  58876.      <A
  58877. HREF="#function.array-uintersect-uassoc"
  58878. ><B
  58879. CLASS="function"
  58880. >array_uintersect_uassoc()</B
  58881. ></A
  58882. >.
  58883.     </P
  58884. ></DIV
  58885. ><H1
  58886. ><A
  58887. NAME="function.array-udiff"
  58888. ></A
  58889. >array_udiff</H1
  58890. ><DIV
  58891. CLASS="refnamediv"
  58892. ><A
  58893. NAME="AEN10357"
  58894. ></A
  58895. ><P
  58896. >    (PHP 5)</P
  58897. >array_udiff -- Computes the difference of arrays by using a callback function for data comparison</DIV
  58898. ><DIV
  58899. CLASS="refsect1"
  58900. ><A
  58901. NAME="AEN10360"
  58902. ></A
  58903. ><H2
  58904. >Description</H2
  58905. >array <B
  58906. CLASS="methodname"
  58907. >array_udiff</B
  58908. > ( array array1, array array2 [, array ..., callback data_compare_func])<BR
  58909. ></BR
  58910. ><P
  58911. >      <B
  58912. CLASS="function"
  58913. >array_udiff()</B
  58914. > returns an array
  58915.      containing all the values of <VAR
  58916. CLASS="parameter"
  58917. >array1</VAR
  58918. >
  58919.      that are not present in any of the other arguments.
  58920.      Note that keys are preserved. For the comparison of the data
  58921.      <VAR
  58922. CLASS="parameter"
  58923. >data_compare_func</VAR
  58924. > is used.
  58925.      It must return an integer less than, equal
  58926.      to, or greater than zero if the first argument is considered to
  58927.      be respectively less than, equal to, or greater than the
  58928.      second. This is unlike <A
  58929. HREF="#function.array-diff"
  58930. ><B
  58931. CLASS="function"
  58932. >array_diff()</B
  58933. ></A
  58934. > which uses an
  58935.      internal function for comparing the data.
  58936.     </P
  58937. ><P
  58938. >      <TABLE
  58939. WIDTH="100%"
  58940. BORDER="0"
  58941. CELLPADDING="0"
  58942. CELLSPACING="0"
  58943. CLASS="EXAMPLE"
  58944. ><TR
  58945. ><TD
  58946. ><DIV
  58947. CLASS="example"
  58948. ><A
  58949. NAME="AEN10383"
  58950. ></A
  58951. ><P
  58952. ><B
  58953. >Example 1. <B
  58954. CLASS="function"
  58955. >array_udiff()</B
  58956. > example</B
  58957. ></P
  58958. ><TABLE
  58959. BORDER="0"
  58960. BGCOLOR="#E0E0E0"
  58961. CELLPADDING="5"
  58962. ><TR
  58963. ><TD
  58964. ><PRE
  58965. CLASS="php"
  58966. ><?php
  58967. class cr {
  58968.     private $priv_member;
  58969.     function cr($val) 
  58970.     {
  58971.         $this->priv_member = $val;
  58972.     }
  58973.     
  58974.     function comp_func_cr($a, $b) 
  58975.     {
  58976.         if ($a->priv_member === $b->priv_member) return 0;
  58977.         return ($a->priv_member > $b->priv_member)? 1:-1;
  58978.     }
  58979. }
  58980. $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
  58981. $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);
  58982.  
  58983. $result = array_udiff($a, $b, array("cr", "comp_func_cr"));
  58984. print_r($result);
  58985. ?></PRE
  58986. ></TD
  58987. ></TR
  58988. ></TABLE
  58989. ><P
  58990. >The above example will output:</P
  58991. ><TABLE
  58992. BORDER="0"
  58993. BGCOLOR="#E0E0E0"
  58994. CELLPADDING="5"
  58995. ><TR
  58996. ><TD
  58997. ><PRE
  58998. CLASS="screen"
  58999. >Array
  59000. (
  59001.     [0.5] => cr Object
  59002.         (
  59003.             [priv_member:private] => 12
  59004.         )
  59005.  
  59006.     [0] => cr Object
  59007.         (
  59008.             [priv_member:private] => 23
  59009.         )
  59010.  
  59011. )</PRE
  59012. ></TD
  59013. ></TR
  59014. ></TABLE
  59015. ></DIV
  59016. ></TD
  59017. ></TR
  59018. ></TABLE
  59019. >
  59020.     </P
  59021. ><DIV
  59022. CLASS="note"
  59023. ><BLOCKQUOTE
  59024. CLASS="note"
  59025. ><P
  59026. ><B
  59027. >Note: </B
  59028. >
  59029.       Two elements are considered equal if and only if
  59030.       <VAR
  59031. CLASS="literal"
  59032. >(string) $elem1 === (string) $elem2</VAR
  59033. >. In words:
  59034.       when the string representation is the same.
  59035.      </P
  59036. ></BLOCKQUOTE
  59037. ></DIV
  59038. ><DIV
  59039. CLASS="note"
  59040. ><BLOCKQUOTE
  59041. CLASS="note"
  59042. ><P
  59043. ><B
  59044. >Note: </B
  59045. >
  59046.       Please note that this function only checks one dimension of a n-dimensional
  59047.       array. Of course you can check deeper dimensions by using
  59048.       <VAR
  59049. CLASS="literal"
  59050. >array_udiff($array1[0], $array2[0], "data_compare_func");</VAR
  59051. >.
  59052.      </P
  59053. ></BLOCKQUOTE
  59054. ></DIV
  59055. ><P
  59056. >      See also
  59057.      <A
  59058. HREF="#function.array-diff"
  59059. ><B
  59060. CLASS="function"
  59061. >array_diff()</B
  59062. ></A
  59063. >,
  59064.      <A
  59065. HREF="#function.array-diff-assoc"
  59066. ><B
  59067. CLASS="function"
  59068. >array_diff_assoc()</B
  59069. ></A
  59070. >,
  59071.      <A
  59072. HREF="#function.array-diff-uassoc"
  59073. ><B
  59074. CLASS="function"
  59075. >array_diff_uassoc()</B
  59076. ></A
  59077. >,
  59078.      <A
  59079. HREF="#function.array-udiff-assoc"
  59080. ><B
  59081. CLASS="function"
  59082. >array_udiff_assoc()</B
  59083. ></A
  59084. >, 
  59085.      <A
  59086. HREF="#function.array-udiff-uassoc"
  59087. ><B
  59088. CLASS="function"
  59089. >array_udiff_uassoc()</B
  59090. ></A
  59091. >, 
  59092.      <A
  59093. HREF="#function.array-intersect"
  59094. ><B
  59095. CLASS="function"
  59096. >array_intersect()</B
  59097. ></A
  59098. >,  
  59099.      <A
  59100. HREF="#function.array-intersect-assoc"
  59101. ><B
  59102. CLASS="function"
  59103. >array_intersect_assoc()</B
  59104. ></A
  59105. >,
  59106.      <A
  59107. HREF="#function.array-uintersect"
  59108. ><B
  59109. CLASS="function"
  59110. >array_uintersect()</B
  59111. ></A
  59112. >,  
  59113.      <A
  59114. HREF="#function.array-uintersect-assoc"
  59115. ><B
  59116. CLASS="function"
  59117. >array_uintersect_assoc()</B
  59118. ></A
  59119. > and 
  59120.      <A
  59121. HREF="#function.array-uintersect-uassoc"
  59122. ><B
  59123. CLASS="function"
  59124. >array_uintersect_uassoc()</B
  59125. ></A
  59126. >.
  59127.     </P
  59128. ></DIV
  59129. ><H1
  59130. ><A
  59131. NAME="function.array-uintersect-assoc"
  59132. ></A
  59133. >array_uintersect_assoc</H1
  59134. ><DIV
  59135. CLASS="refnamediv"
  59136. ><A
  59137. NAME="AEN10407"
  59138. ></A
  59139. ><P
  59140. >    (PHP 5)</P
  59141. >array_uintersect_assoc -- Computes the intersection of arrays with additional index check, compares data by a callback function</DIV
  59142. ><DIV
  59143. CLASS="refsect1"
  59144. ><A
  59145. NAME="AEN10410"
  59146. ></A
  59147. ><H2
  59148. >Description</H2
  59149. >array <B
  59150. CLASS="methodname"
  59151. >array_uintersect_assoc</B
  59152. > ( array array1, array array2 [, array ..., callback data_compare_func])<BR
  59153. ></BR
  59154. ><P
  59155. >      <B
  59156. CLASS="function"
  59157. >array_uintersect_assoc()</B
  59158. > returns an array
  59159.      containing all the values of <VAR
  59160. CLASS="parameter"
  59161. >array1</VAR
  59162. >
  59163.      that are present in all the arguments. Note that the keys are used in
  59164.      the comparison unlike in <A
  59165. HREF="#function.array-uintersect"
  59166. ><B
  59167. CLASS="function"
  59168. >array_uintersect()</B
  59169. ></A
  59170. >.
  59171.      The data is compared by using a callback function.
  59172.     </P
  59173. ><P
  59174. >      <TABLE
  59175. WIDTH="100%"
  59176. BORDER="0"
  59177. CELLPADDING="0"
  59178. CELLSPACING="0"
  59179. CLASS="EXAMPLE"
  59180. ><TR
  59181. ><TD
  59182. ><DIV
  59183. CLASS="example"
  59184. ><A
  59185. NAME="AEN10432"
  59186. ></A
  59187. ><P
  59188. ><B
  59189. >Example 1. <B
  59190. CLASS="function"
  59191. >array_uintersect_assoc()</B
  59192. > example</B
  59193. ></P
  59194. ><TABLE
  59195. BORDER="0"
  59196. BGCOLOR="#E0E0E0"
  59197. CELLPADDING="5"
  59198. ><TR
  59199. ><TD
  59200. ><PRE
  59201. CLASS="php"
  59202. ><?php
  59203. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
  59204. $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
  59205.  
  59206. print_r(array_uintersect_assoc($array1, $array2, "strcasecmp"));
  59207. ?></PRE
  59208. ></TD
  59209. ></TR
  59210. ></TABLE
  59211. ><P
  59212. >The above example will output:</P
  59213. ><TABLE
  59214. BORDER="0"
  59215. BGCOLOR="#E0E0E0"
  59216. CELLPADDING="5"
  59217. ><TR
  59218. ><TD
  59219. ><PRE
  59220. CLASS="screen"
  59221. >Array
  59222. (
  59223.     [a] => green
  59224. )</PRE
  59225. ></TD
  59226. ></TR
  59227. ></TABLE
  59228. ></DIV
  59229. ></TD
  59230. ></TR
  59231. ></TABLE
  59232. >
  59233.     </P
  59234. ><P
  59235. >      For comparison is used the user supplied callback function.
  59236.      It must return an integer less than, equal
  59237.      to, or greater than zero if the first argument is considered to
  59238.      be respectively less than, equal to, or greater than the
  59239.      second.
  59240.     </P
  59241. ><P
  59242. >      See also <A
  59243. HREF="#function.array-uintersect"
  59244. ><B
  59245. CLASS="function"
  59246. >array_uintersect()</B
  59247. ></A
  59248. >,
  59249.      <A
  59250. HREF="#function.array-intersect-assoc"
  59251. ><B
  59252. CLASS="function"
  59253. >array_intersect_assoc()</B
  59254. ></A
  59255. >,
  59256.      <A
  59257. HREF="#function.array-intersect-uassoc"
  59258. ><B
  59259. CLASS="function"
  59260. >array_intersect_uassoc()</B
  59261. ></A
  59262. > and
  59263.      <A
  59264. HREF="#function.array-uintersect-uassoc"
  59265. ><B
  59266. CLASS="function"
  59267. >array_uintersect_uassoc()</B
  59268. ></A
  59269. >.
  59270.     </P
  59271. ></DIV
  59272. ><H1
  59273. ><A
  59274. NAME="function.array-uintersect-uassoc"
  59275. ></A
  59276. >array_uintersect_uassoc</H1
  59277. ><DIV
  59278. CLASS="refnamediv"
  59279. ><A
  59280. NAME="AEN10445"
  59281. ></A
  59282. ><P
  59283. >    (PHP 5)</P
  59284. >array_uintersect_uassoc -- Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions</DIV
  59285. ><DIV
  59286. CLASS="refsect1"
  59287. ><A
  59288. NAME="AEN10448"
  59289. ></A
  59290. ><H2
  59291. >Description</H2
  59292. >array <B
  59293. CLASS="methodname"
  59294. >array_uintersect_uassoc</B
  59295. > ( array array1, array array2 [, array ..., callback data_compare_func, callback key_compare_func])<BR
  59296. ></BR
  59297. ><P
  59298. >      <B
  59299. CLASS="function"
  59300. >array_uintersect_uassoc()</B
  59301. > returns an array
  59302.      containing all the values of <VAR
  59303. CLASS="parameter"
  59304. >array1</VAR
  59305. >
  59306.      that are present in all the arguments. Note that the keys are used in
  59307.      the comparison unlike in <A
  59308. HREF="#function.array-uintersect"
  59309. ><B
  59310. CLASS="function"
  59311. >array_uintersect()</B
  59312. ></A
  59313. >.
  59314.      Both the data and the indexes are compared by using a callback functions.
  59315.     </P
  59316. ><P
  59317. >      <TABLE
  59318. WIDTH="100%"
  59319. BORDER="0"
  59320. CELLPADDING="0"
  59321. CELLSPACING="0"
  59322. CLASS="EXAMPLE"
  59323. ><TR
  59324. ><TD
  59325. ><DIV
  59326. CLASS="example"
  59327. ><A
  59328. NAME="AEN10473"
  59329. ></A
  59330. ><P
  59331. ><B
  59332. >Example 1. <B
  59333. CLASS="function"
  59334. >array_uintersect_uassoc()</B
  59335. > example</B
  59336. ></P
  59337. ><TABLE
  59338. BORDER="0"
  59339. BGCOLOR="#E0E0E0"
  59340. CELLPADDING="5"
  59341. ><TR
  59342. ><TD
  59343. ><PRE
  59344. CLASS="php"
  59345. ><?php
  59346. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
  59347. $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
  59348.  
  59349. print_r(array_uintersect_uassoc($array1, $array2, "strcasecmp", "strcasecmp"));
  59350. ?></PRE
  59351. ></TD
  59352. ></TR
  59353. ></TABLE
  59354. ><P
  59355. >The above example will output:</P
  59356. ><TABLE
  59357. BORDER="0"
  59358. BGCOLOR="#E0E0E0"
  59359. CELLPADDING="5"
  59360. ><TR
  59361. ><TD
  59362. ><PRE
  59363. CLASS="screen"
  59364. >Array
  59365. (
  59366.     [a] => green
  59367.     [b] => brown
  59368. )</PRE
  59369. ></TD
  59370. ></TR
  59371. ></TABLE
  59372. ></DIV
  59373. ></TD
  59374. ></TR
  59375. ></TABLE
  59376. >
  59377.     </P
  59378. ><P
  59379. >      For comparison is used the user supplied callback function.
  59380.      It must return an integer less than, equal
  59381.      to, or greater than zero if the first argument is considered to
  59382.      be respectively less than, equal to, or greater than the
  59383.      second.
  59384.     </P
  59385. ><P
  59386. >      See also <A
  59387. HREF="#function.array-uintersect"
  59388. ><B
  59389. CLASS="function"
  59390. >array_uintersect()</B
  59391. ></A
  59392. >,
  59393.      <A
  59394. HREF="#function.array-intersect-assoc"
  59395. ><B
  59396. CLASS="function"
  59397. >array_intersect_assoc()</B
  59398. ></A
  59399. >,
  59400.      <A
  59401. HREF="#function.array-intersect-uassoc"
  59402. ><B
  59403. CLASS="function"
  59404. >array_intersect_uassoc()</B
  59405. ></A
  59406. > and
  59407.      <A
  59408. HREF="#function.array-uintersect-assoc"
  59409. ><B
  59410. CLASS="function"
  59411. >array_uintersect_assoc()</B
  59412. ></A
  59413. >.
  59414.     </P
  59415. ></DIV
  59416. ><H1
  59417. ><A
  59418. NAME="function.array-uintersect"
  59419. ></A
  59420. >array_uintersect</H1
  59421. ><DIV
  59422. CLASS="refnamediv"
  59423. ><A
  59424. NAME="AEN10486"
  59425. ></A
  59426. ><P
  59427. >    (PHP 5)</P
  59428. >array_uintersect -- Computes the intersection of arrays, compares data by a callback function</DIV
  59429. ><DIV
  59430. CLASS="refsect1"
  59431. ><A
  59432. NAME="AEN10489"
  59433. ></A
  59434. ><H2
  59435. >Description</H2
  59436. >array <B
  59437. CLASS="methodname"
  59438. >array_uintersect</B
  59439. > ( array array1, array array2 [, array ..., callback data_compare_func])<BR
  59440. ></BR
  59441. ><P
  59442. >      <B
  59443. CLASS="function"
  59444. >array_uintersect()</B
  59445. > returns an array
  59446.      containing all the values of <VAR
  59447. CLASS="parameter"
  59448. >array1</VAR
  59449. >
  59450.      that are present in all the arguments.
  59451.      The data is compared by using a callback function.
  59452.     </P
  59453. ><P
  59454. >      <TABLE
  59455. WIDTH="100%"
  59456. BORDER="0"
  59457. CELLPADDING="0"
  59458. CELLSPACING="0"
  59459. CLASS="EXAMPLE"
  59460. ><TR
  59461. ><TD
  59462. ><DIV
  59463. CLASS="example"
  59464. ><A
  59465. NAME="AEN10510"
  59466. ></A
  59467. ><P
  59468. ><B
  59469. >Example 1. <B
  59470. CLASS="function"
  59471. >array_uintersect()</B
  59472. > example</B
  59473. ></P
  59474. ><TABLE
  59475. BORDER="0"
  59476. BGCOLOR="#E0E0E0"
  59477. CELLPADDING="5"
  59478. ><TR
  59479. ><TD
  59480. ><PRE
  59481. CLASS="php"
  59482. ><?php
  59483. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
  59484. $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
  59485.  
  59486. print_r(array_uintersect($array1, $array2, "strcasecmp"));
  59487. ?></PRE
  59488. ></TD
  59489. ></TR
  59490. ></TABLE
  59491. ><P
  59492. >The above example will output:</P
  59493. ><TABLE
  59494. BORDER="0"
  59495. BGCOLOR="#E0E0E0"
  59496. CELLPADDING="5"
  59497. ><TR
  59498. ><TD
  59499. ><PRE
  59500. CLASS="screen"
  59501. >Array
  59502. (
  59503.     [a] => green
  59504.     [b] => brown
  59505.     [0] => red
  59506. )</PRE
  59507. ></TD
  59508. ></TR
  59509. ></TABLE
  59510. ></DIV
  59511. ></TD
  59512. ></TR
  59513. ></TABLE
  59514. >
  59515.     </P
  59516. ><P
  59517. >      For comparison is used the user supplied callback function.
  59518.      It must return an integer less than, equal
  59519.      to, or greater than zero if the first argument is considered to
  59520.      be respectively less than, equal to, or greater than the
  59521.      second.
  59522.     </P
  59523. ><P
  59524. >      See also <A
  59525. HREF="#function.array-intersect"
  59526. ><B
  59527. CLASS="function"
  59528. >array_intersect()</B
  59529. ></A
  59530. >,
  59531.      <A
  59532. HREF="#function.array-uintersect-assoc"
  59533. ><B
  59534. CLASS="function"
  59535. >array_uintersect_assoc()</B
  59536. ></A
  59537. >,
  59538.      <A
  59539. HREF="#function.array-intersect-uassoc"
  59540. ><B
  59541. CLASS="function"
  59542. >array_intersect_uassoc()</B
  59543. ></A
  59544. > and
  59545.      <A
  59546. HREF="#function.array-uintersect-uassoc"
  59547. ><B
  59548. CLASS="function"
  59549. >array_uintersect_uassoc()</B
  59550. ></A
  59551. >.
  59552.     </P
  59553. ></DIV
  59554. ><H1
  59555. ><A
  59556. NAME="function.array-unique"
  59557. ></A
  59558. >array_unique</H1
  59559. ><DIV
  59560. CLASS="refnamediv"
  59561. ><A
  59562. NAME="AEN10523"
  59563. ></A
  59564. ><P
  59565. >    (PHP 4 >= 4.0.1, PHP 5)</P
  59566. >array_unique -- Removes duplicate values from an array</DIV
  59567. ><DIV
  59568. CLASS="refsect1"
  59569. ><A
  59570. NAME="AEN10526"
  59571. ></A
  59572. ><H2
  59573. >Description</H2
  59574. >array <B
  59575. CLASS="methodname"
  59576. >array_unique</B
  59577. > ( array array)<BR
  59578. ></BR
  59579. ><P
  59580. >      <B
  59581. CLASS="function"
  59582. >array_unique()</B
  59583. > takes input
  59584.      <VAR
  59585. CLASS="parameter"
  59586. >array</VAR
  59587. > and returns a new array
  59588.      without duplicate values.
  59589.     </P
  59590. ><P
  59591. >      Note that keys are preserved. <B
  59592. CLASS="function"
  59593. >array_unique()</B
  59594. > sorts
  59595.      the values treated as string at first, then will keep the first key
  59596.      encountered for every value, and ignore all following keys. It does not
  59597.      mean that the key of the first related value from the unsorted
  59598.      <VAR
  59599. CLASS="parameter"
  59600. >array</VAR
  59601. > will be kept.
  59602.     </P
  59603. ><DIV
  59604. CLASS="note"
  59605. ><BLOCKQUOTE
  59606. CLASS="note"
  59607. ><P
  59608. ><B
  59609. >Note: </B
  59610. >
  59611.       Two elements are considered equal if and only if
  59612.       <VAR
  59613. CLASS="literal"
  59614. >(string) $elem1 === (string) $elem2</VAR
  59615. >. In words:
  59616.       when the string representation is the same.
  59617.      </P
  59618. ><P
  59619. >       The first element will be used.
  59620.      </P
  59621. ></BLOCKQUOTE
  59622. ></DIV
  59623. ><P
  59624. >      <TABLE
  59625. WIDTH="100%"
  59626. BORDER="0"
  59627. CELLPADDING="0"
  59628. CELLSPACING="0"
  59629. CLASS="EXAMPLE"
  59630. ><TR
  59631. ><TD
  59632. ><DIV
  59633. CLASS="example"
  59634. ><A
  59635. NAME="AEN10545"
  59636. ></A
  59637. ><P
  59638. ><B
  59639. >Example 1. <B
  59640. CLASS="function"
  59641. >array_unique()</B
  59642. > example</B
  59643. ></P
  59644. ><TABLE
  59645. BORDER="0"
  59646. BGCOLOR="#E0E0E0"
  59647. CELLPADDING="5"
  59648. ><TR
  59649. ><TD
  59650. ><PRE
  59651. CLASS="php"
  59652. ><?php
  59653. $input = array("a" => "green", "red", "b" => "green", "blue", "red");
  59654. $result = array_unique($input);
  59655. print_r($result);
  59656. ?></PRE
  59657. ></TD
  59658. ></TR
  59659. ></TABLE
  59660. ><P
  59661. >The above example will output:</P
  59662. ><TABLE
  59663. BORDER="0"
  59664. BGCOLOR="#E0E0E0"
  59665. CELLPADDING="5"
  59666. ><TR
  59667. ><TD
  59668. ><PRE
  59669. CLASS="php"
  59670. >Array
  59671. (
  59672.     [a] => green
  59673.     [0] => red
  59674.     [1] => blue
  59675. )</PRE
  59676. ></TD
  59677. ></TR
  59678. ></TABLE
  59679. ></DIV
  59680. ></TD
  59681. ></TR
  59682. ></TABLE
  59683. >
  59684.     </P
  59685. ><P
  59686. >      <TABLE
  59687. WIDTH="100%"
  59688. BORDER="0"
  59689. CELLPADDING="0"
  59690. CELLSPACING="0"
  59691. CLASS="EXAMPLE"
  59692. ><TR
  59693. ><TD
  59694. ><DIV
  59695. CLASS="example"
  59696. ><A
  59697. NAME="AEN10552"
  59698. ></A
  59699. ><P
  59700. ><B
  59701. >Example 2. <B
  59702. CLASS="function"
  59703. >array_unique()</B
  59704. > and types</B
  59705. ></P
  59706. ><TABLE
  59707. BORDER="0"
  59708. BGCOLOR="#E0E0E0"
  59709. CELLPADDING="5"
  59710. ><TR
  59711. ><TD
  59712. ><PRE
  59713. CLASS="php"
  59714. ><?php
  59715. $input = array(4, "4", "3", 4, 3, "3");
  59716. $result = array_unique($input);
  59717. var_dump($result);
  59718. ?></PRE
  59719. ></TD
  59720. ></TR
  59721. ></TABLE
  59722. ><P
  59723. >The above example will output:</P
  59724. ><TABLE
  59725. BORDER="0"
  59726. BGCOLOR="#E0E0E0"
  59727. CELLPADDING="5"
  59728. ><TR
  59729. ><TD
  59730. ><PRE
  59731. CLASS="php"
  59732. >array(2) {
  59733.   [0] => int(4)
  59734.   [2] => string(1) "3"
  59735. }</PRE
  59736. ></TD
  59737. ></TR
  59738. ></TABLE
  59739. ></DIV
  59740. ></TD
  59741. ></TR
  59742. ></TABLE
  59743. >
  59744.     </P
  59745. ></DIV
  59746. ><H1
  59747. ><A
  59748. NAME="function.array-unshift"
  59749. ></A
  59750. >array_unshift</H1
  59751. ><DIV
  59752. CLASS="refnamediv"
  59753. ><A
  59754. NAME="AEN10559"
  59755. ></A
  59756. ><P
  59757. >    (PHP 4 , PHP 5)</P
  59758. >array_unshift -- 
  59759.      Prepend one or more elements to the beginning of an array
  59760.     </DIV
  59761. ><DIV
  59762. CLASS="refsect1"
  59763. ><A
  59764. NAME="AEN10562"
  59765. ></A
  59766. ><H2
  59767. >Description</H2
  59768. >int <B
  59769. CLASS="methodname"
  59770. >array_unshift</B
  59771. > ( array &array, mixed var [, mixed ...])<BR
  59772. ></BR
  59773. ><P
  59774. >      <B
  59775. CLASS="function"
  59776. >array_unshift()</B
  59777. > prepends passed elements to
  59778.      the front of the <VAR
  59779. CLASS="parameter"
  59780. >array</VAR
  59781. >. Note that the list
  59782.      of elements is prepended as a whole, so that the prepended
  59783.      elements stay in the same order.  All numerical array keys will be 
  59784.      modified to start counting from zero while literal keys won't be touched. 
  59785.     </P
  59786. ><P
  59787. >      Returns the new number of elements in the
  59788.      <VAR
  59789. CLASS="parameter"
  59790. >array</VAR
  59791. >.
  59792.     </P
  59793. ><P
  59794. >      <TABLE
  59795. WIDTH="100%"
  59796. BORDER="0"
  59797. CELLPADDING="0"
  59798. CELLSPACING="0"
  59799. CLASS="EXAMPLE"
  59800. ><TR
  59801. ><TD
  59802. ><DIV
  59803. CLASS="example"
  59804. ><A
  59805. NAME="AEN10582"
  59806. ></A
  59807. ><P
  59808. ><B
  59809. >Example 1. <B
  59810. CLASS="function"
  59811. >array_unshift()</B
  59812. > example</B
  59813. ></P
  59814. ><TABLE
  59815. BORDER="0"
  59816. BGCOLOR="#E0E0E0"
  59817. CELLPADDING="5"
  59818. ><TR
  59819. ><TD
  59820. ><PRE
  59821. CLASS="php"
  59822. ><?php
  59823. $queue = array("orange", "banana");
  59824. array_unshift($queue, "apple", "raspberry");
  59825. ?></PRE
  59826. ></TD
  59827. ></TR
  59828. ></TABLE
  59829. ><P
  59830. >        This would result in <VAR
  59831. CLASS="varname"
  59832. >$queue</VAR
  59833. > having the
  59834.        following elements:
  59835.       </P
  59836. ><TABLE
  59837. BORDER="0"
  59838. BGCOLOR="#E0E0E0"
  59839. CELLPADDING="5"
  59840. ><TR
  59841. ><TD
  59842. ><PRE
  59843. CLASS="php"
  59844. >Array
  59845. (
  59846.     [0] => apple
  59847.     [1] => raspberry
  59848.     [2] => orange
  59849.     [3] => banana
  59850. )</PRE
  59851. ></TD
  59852. ></TR
  59853. ></TABLE
  59854. ></DIV
  59855. ></TD
  59856. ></TR
  59857. ></TABLE
  59858. >
  59859.     </P
  59860. ><P
  59861. >      See also <A
  59862. HREF="#function.array-shift"
  59863. ><B
  59864. CLASS="function"
  59865. >array_shift()</B
  59866. ></A
  59867. >,
  59868.      <A
  59869. HREF="#function.array