Quick tutorial: php if and else

Filed under: PHP

The if construct is an important feature of many programming languages, allowing for particular code to be excuted depending on certain conditions.

The basic way of using the if construct is:


if (condition) {
   do this if the condition is satisfied or is true;
}

You can include an else construct to execute different code if the condition is not met. (Read more…)

Check if external image exists

Filed under: PHP

Quick and easy PHP script to determine if image exists. The image you are checking for can be hosted on your site or externally.

This script is useful to display an alternate image if the original one does not exist. (Read more…)

Regular expressions 101

Filed under: htaccess, PHP, Misc. Tips

What are regular expressions?

A regular expression is a special string used to describe or match a search pattern or set of strings. Regular expressions use certain syntax rules as outlined in the section below, “Basic regular expressions syntax”.

Regular expression can be abbreviated as:

  • regexp
  • regex
  • regxp

Plural forms of regular expressions are:

  • regexps
  • regexes
  • regexen

(Read more…)

Parsing PHP on .html pages

Filed under: htaccess, PHP

There are 2 ways of getting your .html pages to parse PHP, this is via:

  1. .htaccess
  2. httpd.conf

In both methods above, you need to make sure that your server supports PHP.

Which method to choose?

For most webmasters who uses shared hosting and do not have access to their server’s configuration files, you would use method one, via .htaccess. If you have access to your server’s configuration files, that is, you have a dedicated server and can access httpd.conf, then use method two, via httpd.conf.

(Read more…)

Different text based on date (php)

Filed under: PHP

The following PHP code snipplet is useful for displaying different text depending on the date. This is particularly useful for time limited specials and promotions where you can set up your special or promotion and its expiration date, then forget about it.

The PHP code

Add the following code at the top of your web page. This will set the variable $buffer, which contains the text string you wish to print out depending on the date.

<?php
$buffer = '';
$expiry_date = mktime(0,0,0,12,31,2005);
if ($expiry_date > time()) {
$buffer = 'This string will appear before the date December 31, 2005.';
} else {
$buffer = 'This string will appear after the date December 31, 2005.';
}
?>

Explanation

Line 1: Start PHP.
Line 2: Set $buffer variable as empty (creates the variable $buffer).
Line 3: Set the expiry date variable, $expiry_date, this is in the format of mktime(hour, minute, second, month, day, year) where each time is a integer.
Line 4: If the the date (time()) is before the expiry date ($expiry_date), then…
Line 5: Set the variable, $buffer, with the text string “This string will appear before the date December 31, 2005.”
Line 6: Else, if the date is after the expiry date, then…
Line 7: Set the variable, $buffer, with the text string “This string will appear after the date December 31, 2005.”
Line 8: Closes the If
Line 9: End of PHP

Calling the variable

In the HTML or XHTML section of your web page, use the following code where you would like to display the variable’s, that is, $buffer’s text string.

<?=$buffer?>

Add the current date on a web page

Filed under: PHP

Using PHP, you can add the current date to your webpage. Add the following line of PHP code to your web page in the place where you wish the current date to be displayed.

<?=date('F d, Y',time())?>

The above will display the date in the format of “month day, year”. The month is displayed in words and the day and year in numbers.

The date function is in the format of date(string format [, int timestamp] ), where the string format determines they way the date is formatted, eg July 12, 2005 or 7/12/05. The parameter “int timestamp” or integer timestamp is optional, you can leave the timestamp blank and the current local time will be used, that is, time().

See the PHP manual on date function for more information on the date function and for a list of the format characters.