PHP

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Introduction

I don't like PHP. Here's why.

This section covers some PHP basics.

What is it?

PHP is a language for writing HTML documents. It uses a simple method of embedding commands in pages with the magical "<?php ... ?>" syntax.

The language, however, is atrocious. It shows no sign of any serious thought of language design. It is clearly a hack designed by C programmers for C programmers, an afterthought, really.

This makes the language very fast, however, at the cost of being very difficult to write correct code in it.

If you don't understand what I am talking about, then you haven't spent much time with Python or Lisp, two languages that truly show what you can do if you simply consider functions as first-class data types and give at least a moment's thought to what the programmer will have to do to get common tasks done.

Pitfalls

  • php.ini is the global config file. Find it, love it, read it, and use it.
  • pear installs files to a special directory. If that directory is not on the path, then you won't find them.

Documentation

See http://us3.php.net/manual

The Language

Ordinary Text

In PHP, like most templating languages, ordinary text is interpreted to output verbatim. For instance:

This is a php file.

Evaluates to:

This is a php file.

PHP Escapes

To execute php code, you embed the PHP escape sequence:

This is <?php echo "not" ?> ordinary text

Evaluates to:

This is not ordinary text.

Braces for flow control statements (such as if, for, foreach, and while) may be broken between separate escape sequences.

If an escape sequence isn't closed, then everything until the end of the file is interpreted as a single escape sequence without anything after it. This is useful for modules.

Types

The basic scalars are:

  • null:
  • booleans: TRUE / True / true) or FALSE / False / false. They are case insensitive.
  • integers: Whole numbers, positive and negative, and zero. Literals can be decimal, octal (starting with '0') or hex (starting with '0x'). The max/min are determined by the platform (32-bits or 64-bits.)
  • floats: Arbitrary precision, base-2 floats, platform dependent. Usually a double (64 bits.) You can use decimals and e scientific notation.
  • strings: Specified by single-quotes (no expansion), double-quotes (full expansion), heredocs (<<TAG...TAG, interpreted as double-quotes) and nowdocs (<<'TAG'...TAG, interpreted as single-quoted). Of note, you can put expressions in a double-quoted strings by surrounding them with curly braces.

The basic complex types are:

Variables

Names

Variables can be named pretty much anything. They can't start with a number but they can contain a number. They can have any letter or number, and even any extended ASCII character in their name.

You can't use any of the supervariables or $this, either.

References

Of note, only variables can be referenced---not functions or supervariables.

$foo = &$bar; # Now $foo will always equal $bar.

Note that the above implies that assigning to $foo is the same as assigning to $bar. Weird, huh?

Supervariables

Supervariables are not variables, but they really, really look like them.

Scope

All variables refer to the local scope. Use a global statement to see variables outside the local scope. Note that supervariables are available everywhere.

Static Variables

Static variables remember their value from the last time the function was called. Use 'static' to declare them.

Reference Variables By Name

$$a means "lookup the variable with the name that is the value of $a.' This doesn't work for supervariables or $this.