Articles in: PHP Functions

Home » Archive » PHP Functions

Check if Field Exists in Mysql Table

Sometimes it is useful to know if a field exists in a mysql table before running a query using that field name, especially when the field name is coming from some kind of user input.
So to do this we use the function mysql_list_fields to grab the fields out of a table and run through them with a for loop until …

PHP fopen() Function

The fopen() function will open any valid file or url.
If the function fails it will return false along with an error generated. You can hide the error message by adding an “@” in front of the function name.
Syntax

fopen(filename, mode, include_path, context);

Parameters
filename (String | Required)
This specifies the URL or file to open. Example: “./files/myfile.zip“.
mode (String | Required)
The type of access you …

PHP Date() Function

The date() function converts a given timestamp into a readable date format.
Here is the syntax for this function:
Syntax

date(format,timestamp);

Parameters:
format
This parameter determines the format and how the date will read.
For example: “Saturday, Nov. 16th 2009” would be the following code:

$timestamp = time();
date(“l, M. jS Y”,$timestamp);

timestamp
This parameter is the timestamp you want to use to format a date from. This timestamp is a …

PHP Date Formats Reference

Here is a table for formatting the PHP function date() and the letters used to represent different parts of a timestamp.

a
‘am’ or ‘pm’

A
‘AM’ or ‘PM’

B
Swatch Internet time

d
day of the month, 2 digits with leading zeros; i.e. ’01′ to
’31′

D
day of the week, textual, 3 letters; i.e. ‘Fri’

F
month, textual, long; i.e. ‘January’

g
hour, 12-hour format without leading zeros; i.e. ’1′ to ’12′

G
hour, 24-hour …

PHP Ordinalize Numbers – Add Suffix

Here is a very simple function to use to ordinalize numbers in PHP. This adds the place value suffix to numbers. So you can turn numbers like 1, 2, 3 into 1st, 2nd, 3rd.
Here is the code:

function ordinalize($int){
if(in_array(($int % 100),range(11,13))){
return $int . “th”;
} else {
switch(($int % 10)){
case 1:
return $int . “st”;
break;
case 2:
return $int . “nd”;
break;
case 3:
return $int . “rd”;
break;
default:
return $int …

Mysql Rows in HTML Option Tag

Let’s say you want to have a simple HTML <select> form as a drop down for rows in a Mysql table. This could be for things like categories, pages, games, anything you want to have in a drop down to navigate to another page or submit a form. What ever the case is, I’m going to make a simple layout …

PHP error_reporting() Function

The error_reporting() function determines what errors are reported from the current script.
Here is the syntax for this function:
Syntax

error_reporting(report_level);

The report_level parameter is optional and specifies what report level to report for the current script. This can be set by its numeric value or its constant name, however for future versions of PHP it is recommended you use the constant name rather …

PHP Global Variables with Functions

Here we’re going to take a look at variables inside and outside of PHP functions. It is very easy to make one error with a function and have it return the wrong value. We’re going to make a simple function named “func1” which we’ll tweak a bit to show how variables are used inside and out of functions in PHP.
Here …

PHP Timestamps time()

One of the most common ways to capture the current time in PHP scripting is by using the time() function. This returns the current timestamp which is the number of seconds after a certain date and time in the past. You can use this when entering a mysql query to note the current time of whatever action you …

PHP Switch Function

The function switch() in PHP is used to execute different codes based on a variable’s value. This is used in place of the IF/ELSE IF statements. A default value is optional and, if specified, is used when no other option is matched. You must include a break; after each case or the following cases will all return true.

<?php

switch($var){

case "blue":
echo "You …

Sponsors