PHP 3.x Archives

Using header to redirect the browser

The PHP header function allows redirection of the browser, among other cool things.
Example redirecting the browser to php-scripts.com homepage:

<?php
header(”Location: http://www.php-scripts.com/”);
?>

Using floor and time to calculate number of days elapsed from UNIX timestamp

Finding out the number of days elapsed from a UNIX timestamp is actually pretty straightforward. If you only know two dates then first you’ll need to convert the date to a UNIX timestamps and then subtract the most recent timestamp from the older timestamp like this:

<?php
$old_timestamp = 1102971600;
$elapsed_seconds = time() - $old_timestamp;
print ‘Seconds elapsed since […]

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″
?>

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 […]