PHP 4.x Archives

How to PHP scripting course begins Friday 9/23/2005

For those who would like to learn how to write PHP code, starting next Friday at Webmaster Cookbook, our family friendly webmaster how-to site/blog, we’ll be going through a 32 week course on PHP programming. Details will be announced during the radio show and podcast next Friday September 23, 2005 at 3:30pm PST. For those […]

How to join an ID list using IN MySQL function

How to create a query list of ID numbers using join from an $array and put into a string that can be used in a MySQL query:

$id_numbers = array(1,6,14,32,84,27,49,77);
$complist = join(”,”, $id_numbers);
$query = “SELECT id from tablename WHERE id IN($complist)”;

How to strip off last two characters from string using substr

substr comes in handy if you need to strip characters off the end of a string.

<?php
$test_string = ‘item 1, item 2, item 3, ‘;
$test_string = substr($test_string,0,-2);
echo($test_string); // returns “item 1, item 2, item 3″
?>

How to check the user’s browser type and platform

Sooner or later it might become necessary to write an application that tracks site visitor browser types. Here’s some code to use:

<?php
echo($_SERVER[‘HTTP_USER_AGENT’]);
?>

This will output something that looks like one of the following:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) 
Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC)
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
Mozilla/5.0 (Windows; U; Windows NT 5.1; […]

Google Adwords API

It’s called the APIlity Library and it allows interaction with the Google Adwords program:
The APIlity PHP Library provides an object-oriented way to easily access and manage the Google AdWords API from within PHP. This comes along with an abstraction from the SOAP and WSDL details.
Requirements (also from the sourceforge page):

APIlity works both with PHP4 and […]

How to tell if the cURL extension is enabled

To determine if the cURL extension is enabled, run the following code on your server:

<?php
var_dump(curl_version());
?>

Success! - If cURL exists and is enabled it should return something that looks like this:
PHP version 4 example successful result:

string(54) "libcurl/7.13.2 OpenSSL/0.9.7e zlib/1.2.2 libidn/0.5.13"

PHP version 5.1 example successful result (returns an array)

Array
(
   [version_number] => 461570
   [age] => 1
   [features] […]

Colorized pretty source code htaccess method

Tired of black and whit coding that looks like what’s above? Want to colorize it to look like this instead:

Here is how to make colorized pretty source code using the .htacess method for .phps files:
Step 1. create a text file and save it as .htaccess. Put the following code in the file:

AddType application/x-httpd-php-source .phps

Step 2. […]

How to find the location of php.ini on the server

Using the phpinfo() function you can easily find the location of the php.ini file on the server. Here’s the code to generate the phpinfo information pictured above:

<?php
phpinfo();
?>

Shown with the red arrows in the screenshot above:
#1 - this is the version of PHP
#2 - these are the configured library. For example ‘–with-mcrypt=/usr/lib’ means the mcrypt library […]

PHP Hello world!

Welcome to the PHP-scripts blog. Though I’m not a huge fan of ‘Hello World’ stuff, it seems that is where most things in the programming world start out. So here we go with examples using print and echo
Hello World using print statement

<?php
print ‘Hello World!’;
?>

Hello World using echo

<?php
echo ‘Hello World!’;
?>

What is the difference between […]