Danny's Weblog
Some basic setup info for PHP
I haven't looked at PHP for years and needed to read through Sams' "Teach yourself PHP in 24 hours" recently.
Here are some basic tips for setting things up:
1. Assuming your Apache server allows you to use .htaccess files, you can set a bunch of useful stuff here, eg:
AddType application/x-httpd-php .php
For instance, if most of your site is real .html files but you want to put .php files in a certain folder, you can put an .htaccess file in that folder that tells Apache to run the php interpreter on .html files in that folder:
AddType application/x-httpd-php .html
(You don't want to do that in your root folder because it would waste time running the php interpreter on all the ordinary html files.)
2. The phpinfo() command inside php of course produces a nicely-formatted html file with all the setup information for php, and you need to look at it. In particular, you want to check which php version you have and what modules are loaded (irritatingly Panix has a rather old version with few modules compiled in).
Also, it's good to check the "config-file-path". This (assuming you have shell access) allows you to find the actual php.ini file to check any items not shown by the phpinfo() command.
3. If you don't control the server, you can still set php setup variables inside .htaccess. You use the "php_flag" directive to set a php variable and the php_value directive to set a php.ini variable to a value, eg:
php_flag short_open_tag On
php_value include_path ".:/home/corrdev"
4. Even if you can't use .htaccess files, as of php 4.0.5 you can set some .ini values within php pre itself, eg:
ini_set( "include_path", ".:/home/corrdev" );
5. Don't forget the main php website, eg:
[http://www.php.net/manual]
6. Here's the entire contents of a file you can save as test.php:
<?php phpinfo(); ?>
(I know putting that here makes me look like not the greatest programmer in the world, but every time I start looking at php again I find myself having to work out the basics of the syntax all over again.)
7. Here's some code which looks simple – ok, it is simple – but I find myself wondering whether it will work every time I start playing with php again (this is basically from the book's Listing 5.14 Returning to HTML Mode within a pre block):
<?php
$display_prices= true;
if ( $display_prices ) {
?>
<p>Interesting stuff to show in this case</p>
<?php
}
?>
I don't know about you, but if I had to figure out that syntax from scratch it would take a while.
Copyright © 2003-2010 Alternate Worlds Publishing, Boston MA USA
The Little Trouble Girl font is copyright Blue Vinyl Fonts vendorurl: www.bvfonts.com