Common PHP Tasks and Questions
Basics and String Literals
Answers to some frequently asked questions and solutions to some common PHP programming tasks. (With a few comments on how PHP syntax relates to Perl).
How Do I Tell if PHP is Working?
You can check to see if PHP is running and correctly configured by making a simple one line script:
<?php print phpinfo(); ?>
I usually call this phpinfo.php and upload it to the public web server directory, then point my browser to it. It displays the configuration of PHP in great detail.
How do I make a PHP page?
This may sound a little silly, but often people are confused about how a HTML page differs from a PHP page. A PHP page is just a regular plain text file like any other web page except that it has a "php file extension," usually "php" and meaning that this page will be run through PHP before going to the browser. Any PHP program code in the page will be executed. Any HTML will be passed on to the browser untouched. Making a PHP enabled web page is as easy as opening a new file in your favorite HTML or text editor. The editor can be as simple as Notepad or as complex as HomeSite. The choice is yours.
print "Hello World";
Save this file with the php file extension, such as "hello.php" (without the quotes) to your hard disk.
Upload the file to your PHP enabled web server. It must be in your web server's document root or a directory below it (this is usually a directory called public_html or www or something similar).
Load the page in your web browser, the URL should look something like:
Remember, that you may have to change the extension to suit how the web server is configured. If you're using PHP3, you may have to give your file the php3 extension instead of the standard php.
How do I quote strings?
PHP syntax takes some inspiration from the popular Perl programming language. In Perl, variable substitution is done in a double quotish context but not in a single quotish context. PHP follows this rule. To see how this works, run:
$test = "Test";
print "Test Double Quotish Context: $test";
print 'Test Single Quotish Context: $test';
By single quote context, I mean any characters enclosed in single quotes. By double quote context, I mean any string of characters enclosed by double quotes. Variable substitution refers to replacing the value of a variable where that variable name appears in the string.
The type of quotes you use to enclose a string literal can also affect the use of character sequences used to escape certain special characters:
print "This is a double quote context\n";
print 'This is a single Quote context\n';
When printed to the browser, the second line will contain "\n" instead of a newline.
This is a double quote context
This is a single Quote context\n
Not understanding how quotes work can lead to frustration and mysterious errors.
For example take a look at the code below:
The opendir() function gives mysterious errors until you stop looking at the path and notice the single quotes. This literally evaluates to:
which will give errors, instead of:
where the $dir variable's value gets properly substituted into the directory path string.
For this to work correctly use double quotes instead instead of single quotes:
PHP also allows string literals to be broken over multiple lines. Very useful for organizing or structuring HTML tags, such as for indenting tags used in TABLEs. Notice that spaces are significant in multi-line quoted material. (This is a little easier than Perl, but lacks that language's mechanism for specifying the start and end quote symbols that makes dropping HTML blocks into code a snap).
This is a multiple
|Pages: 1 2 3 4||Next: Printing to Web Pages and First Steps to Functions »|