PHP FAQ (unoffical)

At one point there were no—or at least very few, FAQs for PHP. This document came about as a volunteer effort to fill this gap and has been hosted by Kaomso from the beginning. It started out as a compliment to the Usenet group comp.lang.php but the author rarely frequents this group anymore and so it is no longer a good medium for gathering the questions. The FAQ in it's new form has become a stand-alone resource for PHP developers who may find some value in what it holds.

NOTE:  New or updated items are indicated with a "less than" sign '<' acting as a "pointer" next to the item number.

How many forum members does it takes to change a light bulb?

Updated December 12, 2008

© 2008, Kaomso



General   Top
1   Where can I find PHP documentation?
2   What are the system requirements to run PHP?
3   What can PHP do?
4   Is PHP object oriented?
5   How can I suggest additions or changes to this FAQ?
6   What is LAMP?
7   How do I install PHP?
8  < What does the term "framework" mean?
9  < What is PHP on TRAX?
10  < What is Fusebox?
Language   Top
1   What's the difference between file() and fopen()?
2   What's the concatenation operator?
3   How can I access the page that referred me to my script?
4   Why won't the file() function work as advertised on my files?
5   What does T_SOMECODE mean?
6   What are the logical operators in PHP? (Or operators in general, like modulus, and concatenation.)
7   Is PHP case-sensitive?
8   What are some of the style conventions used in the code?
9   What values are used with the boolean data type?
10   Can I launch a command-line script without typing "php" at the prompt?
11   What is the syntax for a function?
12   Do I need to use a semicolon at the end of each line?
13   What are the bit-wise operators in PHP? (AND, OR, XOR, et cetera)
Errors   Top
1   I am getting a "parse error." What does this mean?
2   Why do I get an error saying "headers already sent?"
HTML/HTTP   Top
1   How do I upload a file from a form?
2   POST method versus GET
3   How can I find the page a page was referred from?
Variables   Top
1   How can I access a global variable from a function?
2   How do I output a variable as part of a string?
3   What is an associative array?
4   What does "register globals" mean?
PEAR   Top
1   What is PEAR?






General:  1 Where can I find PHP documentation?
The language is very well documented in a variety of languages and can be found here: http://www.php.net/docs.php. The docs are also available in a few formats for download as well. (Back)
General:  2 What are the system requirements to run PHP?
A major reason for the massive adoption of PHP is that it runs on such a wide variety of platforms. PHP runs on Mac OS X, most major distributions of Unix and Linux-based systems, as well as later Windows systems. (Back)
General:  3 What can PHP do?
PHP has enjoyed tremendous success as the back-end language for a countless number of websites. It has allowed developers the means to produce low cost and powerful sites and applications. It can also serve as a very powerful scripting language on the command-line "side" of your computer system as well.

The PHP documentation has more information if you'd like. You can find it here: http://www.php.net/manual/en/intro-whatcando.php. This document is part of the language documentation which also available for download. See FAQ item General:  1. (Back)

General:  4 Is PHP object oriented?
PHP introduced object oriented concepts in version 3.0 but took criticism for not truly being an object oriented language. Later versions tried to address this issue and the object oriented paradigm was completely re-written for version 5.0. (Back)
General:  5 How can I suggest additions or changes to this FAQ?
If this FAQ does not address your specific question and you would like it to be considered as part of the FAQ, or you would like to suggest changes, send an email to phpfaq AT kaomso.com using one of the applicable lines below as the subject for the message.

To add an item to the FAQ:

    [FAQ Request: add]

If modifications are necessary:

    [FAQ Request: change]

If you feel a FAQ item is no longer relevant you can suggest it be removed:

    [FAQ Request: remove]

(Back)

General:  6 What is LAMP?
It stands for Linux, Apache, MySQL, PHP. It's an acronym for the popular combination of the four technologies on the World-wide Web to publish an ever-growing number of dynamic sites. Linux is the server operating system, but any Unix-like system could be substituted—for instance Mac OS X Server. Apache refers to the organization that develops the httpd web server daemon. MySQL is the database used to store the content for the sites and applications. And PHP is—of course, the language used to code the dynamic sites and applications. (Back)
General:  7 How do I install PHP?
PHP is automatically installed as part of Mac OS X and is generaly an optional package that can be installed with the rest of the system on Linux-based systems. There is a typical installer for installing on Windows systems. (Back)
General:  8 What does the term "framework" mean?
A framework is a library of routines that can be employed to aid in the rapid development of an application. The goal of a framework is provide common functionality for tasks such as rendering page elements and access to data sources so that the developer can focus his or her time on the logic specific to the application. Common frameworks are: Ruby on Rails (Ruby), PHP on TRAX (PHP), .NET (VB/C#), and Fusebox (ColdFusion/PHP). (Back)
General:  9 What is PHP on TRAX?
Ruby on Rails is a popular framework for developing Ruby web applications. PHP on TRAX is a framework specific to PHP to deliver a similar rapid development advantage that Ruby on Rails developers enjoy. (Back)
General:  10 What is Fusebox?
While originally written for ColdFusion, Fusebox is now a framework for both ColdFusion and PHP. (Back)
Language:  1 What's the difference between file() and fopen()?
The most significant difference is that file() handles all of the file handling operations automatically as well as returns an array containing the file broken on each line of the file. fopen() actually requires at least two other functions to be useful. You will need to use fread() to access the data in the file and fclose() to close the file when you're done. The advantage here is that you may access the file as you see fit and nothing but a file handle is returned. One important thing to note with regard to file() is that the file it's acting on needs to be line terminated with the "newline" character (\n). Otherwise, the array returned by the function will have only one element. If the desired result is to read the contents of a file into a string rather than an array, use file_get_contents() instead. Also, both file() and file_get_contents() are read-only functions. (Back)
Language:  2 What's the concatenation operator?
The period. Like: NewString = StringOne . StringTwo; (Back)
Language:  3 How can I access the page that referred me to my script?
The global array $_SERVER has an element called "HTTP_REFERER." This is intended to identify what page the current page was loaded from. It should be noted however, that the information in this element cannot be guaranteed, and in fact may not even be present. A far more reliable method would be to set a "referrer" variable on the preceding script (if authored by you) and set it equal to $_SERVER["PHP_SELF"]. Then pass this to the next page. Unfortunately, this solution does not help if your script is loaded from a page you did not write. (Back)
Language:  4 Why won't the file() function work as advertised on my files?
The file() function loads the contents of the named file into an array where each element of the array corresponds to a line in the file broken on the newline '\n' character. Historically, lines in Unix files are terminated with a "newline" character (\n) or (LF). Mac OS files are terminated with a "carriage return" (\r) or (CR), while Windows systems use both a carriage return and newline (\r\n) or (CRLF). Note that, since Mac OS X files may be terminated with either a \r or a \n. Be sure that the files your PHP scripts use are terminated with a \n character. (Back)
Language:  5 What does T_SOMECODE mean?
PHP uses these codes to represent parts of itself. The codes are cryptic and are generally included as part of an error message. Some become recognized purely from exposure, but for those that are not seen regularly, they are defined in the documentation Appendix as Appendix P. List of Parser Tokens. (Back)
Language:  6 What are the logical operators in PHP? (Or operators in general, like modulus, and concatenation.)
Like much of the rest of PHP it uses mostly the same syntax as other languages like C, C++, Java, and Objective C. The concatenation operator (.) may be different however. See FAQ item Language:  2 for more on that. The logical operators are:
	&&		AND
	||		OR
	!		NOT
	=		assigment
	==		comparison
	===		comparison and of the same type
	!=		not equal
	<		less than
	>		greater than
	!<		not less than
	!>		not greater than
You can read the PHP documentation Comparison Operators for more. (Back)
Language:  7 Is PHP case-sensitive?
PHP is case-sensitive. Which means that PHP recognizes a difference between $VariableName and $variablename. This behavior is consistent with all Unix and Unix-derived systems. (Back)
Language:  7 What are some of the style conventions used in the code?
The "parts" of the names in system-wide functions that would—in normal language, be considered separate words, are separated by an underscore (_) character. Each "word" is also used in lowercase. Such as system_function_call().

The use of camel case is also common, such as customFunction(). The Kaomso preferred is to use all lower case in instances where there is only one word and to use initial capitals for cases where more than one word is used. For example, CustomFunction(). The distinction between this and camel case is subtle, but preferred due to consistency.

There are also conventions concerning the style around opening and closing curly brackets ({ and }) for code blocks. The PHP norm is to put each on its own line, with the opening bracket on the line immediately following the beginning of the code block. For example:

if(condition)
{
	code
	.
	.
	.
}
The JavaScript and Java code block style is also common. Whereby the opening bracket is on the same line as the beginning of the block. And the closing bracket in on the same line as the last line of the code block. Like this:
if(condition){
	code
	.
	.
	.}
Though it doesn't cause any difficulty for the interpretter, putting all lines of a block in a single line is discouraged for the sake of readability. Generally, Kaomso falls into this line of thinking. However, the feeling is that in some cases—such as simple blocks (perhaps ones with one or two commands or function calls), that it's acceptable to put a block into a single line. The expectation is that with long scripts, the savings of several lines may actually improve readability. For example:
if(condition)   {code}
else            {AlternateCode}
versus
if(condition)
{
	code
}
else
{
	AlternateCode
}
The difference is a 75% savings in line space (two lines instead of eight). (Back)
Language:  9 What values are used with the boolean data type?
Since PHP dynamically re-types data values as necessary, any value that can be interpretted—or "evaluated" as the PHP manual puts it, as a boolean value can be used. So that, a boolean TRUE can be set to TRUE, true, "true", yes, "yes", 1, and "1". Conversely, a boolean FALSE can be set to FALSE, false, "false", no, "no", 0, and "0".

There is one gotcha to be aware of however. In some instances an attempt to compare strings that may be interpretted as boolean may create undesirable behavior. For cases like these, use the === operator (three equals signs) which compares both the value and type. (Back)

Language:  10 Can I launch a command-line script without typing "php" at the prompt?
Yes. There are two things you'll need to do to your script. You will need to add the following code to your script on the first line:
#! [PATH_TO_PHP]
for example, on Mac OS X, it's:
#! /usr/bin/php
Then you need to make sure the executable bit is set on the script's permissions—[r-x]. (Back)
Language:  11 What is the syntax for a function?

To call:

FUNCTIONNAME(ARGUMENT, ARGUMENT)

To declare:
function FUNCTIONNAME(ARGUMENT, ARGUMENT)
{
	.
	.
	.
	code
	.
	.
	.
}
The syntax to call [use] a function is simply the function name followed by a pair of parentheses. Some functions may require a set of comma-separated arguments to be passed into the function as well. These arguments would be placed inside the parentheses.

To declare a function use the function PHP reserved word followed by the name of the function name and a pair of parentheses. If the function will accept arguments, then put a comma-separated list of variables the argument values will be assigned to in the parentheses. Note that the order the variables in the parentheses are supplied in the order they will need to be supplied in when the function is called in the script. There's no need to define the return type of the function in the declaration.

The function notation for PHP will likely be very familiar to anyone who has worked with some other languages such as C, C++, Java, .NET, or JavaScript. (Back)

Language:  12 Do I need to use a semicolon at the end of each line?
Yes. With the exception of some language contructs like if/else, switch/case, and function declarations. And of course the { and } that go along with these. (Back)
Language:  13 What are the bit-wise operators in PHP? (AND, OR, XOR, et cetera)
The bit-wise operators are:
	&       AND         ($variable_A & $variable_B)
	|       OR          ($variable_A | $variable_B)
	^       XOR         ($variable_A ^ $variable_B)
	~       NOT         (~ $variable)
	<<      shift left  ($variable_A << $variable_B)
	>>      shift right ($variable_A >> $variable_B)
You can read the PHP documentation Bitwise Operators for more. (Back)
Errors:  1 I am getting a "parse error." What does this mean?
PHP generally likes to have semi-colons ';' terminate each line. Errors indicating a parsing problem can usually be linked to a lack of one in a line somewhere around where the error was encountered. FAQ item Language:5 may also be of some interest. (Back)
Errors:  2 Why do I get an error saying "headers already sent?"
PHP has the ability to send HTTP header data to the browser in addition to what's already sent between the browser and the server. This is mostly commonly used to re-direct the user to another page. The trouble is that the header communication has to take place before any content of the page can be output. Any attempt to send a header after the content of the page has been sent will throw this error. Header function calls need to be done at the very beginning of the script before outputting even a single character. (Back)
HTML/HTTP:  1 How do I upload a file from a form?
The enctype (encryption type) form tag parameter must be set to "multipart/form-data." This tells the browser to upload the a file on submit. An example taken from the PHP documentation looks like this:

<form enctype="multipart/form-data" action="__URL__" method="POST">

You may also read the PHP documentation on the subject here:
Handling file uploads
PUT method support

(Back)

HTML/HTTP:  2 POST method versus GET
There are two ways to pass data from one page to the next using HTTP. They are termed "post" and "get". Typically, data collected in a form is transmitted to the form handling page by way of the POST method while data sent a la the GET method is done so by supplying it directly in the URL's query string. Specifically, GET data is that part of the URL after the question mark character. It consists of name/value pairs where each pair is separated by an ampersand (&) character. The technique used when creating an HTML form is set using the method attribute of the FORM tag. PHP can access GET and POST data using the special associative arrays $_GET and $_POST respectively. The syntax is $_GET["VariableName"] or $_POST["VariableName"] and will return the associated value for that variable name. (Back)
HTML/HTTP:  3 How can I find the page a page was referred from?
PHP has a special array called $_SERVER that holds special information about the HTTP connection. The referring URL--if it exists, can be accessed using this array. The syntax is $_SERVER["HTTP_REFERER"]. It should also be known that for what ever reason the referrer header was mispelled at some point, most likely during the development of HTTP. When referencing the referring URL it's common, if not likely for it to be spelled with one 'r' as "referer URL." (Back)
Variables:  1 How can I access a global variable from a function?
A variable declared outside a function definition is considered global. In order to access that variable from a function you will need to "declare" it in that function with the keyword global. For example:

gGlobalVar = "The global value is a string.";

function CustomFunc()
{
	global gGlobalVar;
	
	.
	.
	.
}

(Back)
Variables:  2 How do I output a variable as part of a string?
At first one might suspect that to output a variable as part of a string a series of concatenations might be necessary. On the contrary, PHP allows a variable to be output as part of a string when the string is output enclosed in double quotation marks. So that, the code echo "My variable is: $TheVariable."; is perfectly understood. (Back)
Variables:  3 What is an associative array?
An associative array is one whose elements are accessed using words rather than a number. In other words the index of the array is a string rather than an integer. So that rather than getting the value at $ANormalArray[2] it's done with $AnAssociativeArray["TrumpSuit"]. (Back)
Variables:  4 What does "register globals" mean?
Due to the nature of how the Internet works concerning the communication between a web browser and a web server, there is information available that is largely unused but can have a great deal of value. Information like the user agent (browser), client's IP address, server name, and the GET and POST data are all examples. These are script-wide global variables and have historically been allowed by by default by the "register globals" directive being set to "on" in the php.ini file. After much debate, as a security measure, this directive has been set to "off" by default. There is plenty more information on this listed below: (Back)
PEAR:  1 What is PEAR?
PEAR is a community focused on a repository of open-source PHP code, it defines a standard style of coding, and among other things hosts websites and mailing lists specific to PHP developers. You can read more in the PEAR documentation Chapter 1. Introduction. (Back)