Chapter 4. PHP s Built-in Arrays and Constants


Chapter 4. PHP's Built-in Arrays and Constants

Constants aren't.

”John Peers

PHP provides a number of built-in arrays and constants that make your job as a programmer much easier. For example: In Perl, you would have to read the buffer or parse the query string to collect data. But PHP not only immediately converts data that is sent to the script into variables, it also stores the variables in associative arrays (see recipe 4.6). So, the following Perl code:

 #!/usr/bin/perl -w use strict; use vars qw(%variables); if (lc $ENV{ REQUEST_METHOD} eq 'post') {     read STDIN, $_, filesize(STDIN); } else {      $_ = $ENV{ QUERY_STRING} ; } foreach (split /\ &/) {     my ($key, $val) = split /\ =/;     $key =~ s/\ +/ /g;     $val =~ s/\ +/ /g;     $key =~ s/%([A-Fa-f0-9]{ 2} )/pack("c", hex())/ge;     $val =~ s/%([A-Fa-f0-9]{ 2} )/pack("c", hex())/ge;     $variables{ $key} = $val; } 

becomes

 <?php $variables = strtolower($REQUEST_METHOD) == 'get' ?              $HTTP_GET_VARS : $HTTP_POST_VARS; ?> 

in PHP. (This is covered fully in recipe 4.6.)

In this chapter, we explain PHP's basic constants and built-in arrays. In particular, we discuss how to use them to create more secure, less error-prone programs.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net