The Ternary Operator

The Ternary Operator

Conditional (Ternary) Operator (?:)

A ternary operator is an operator that requires three terms. Alternatively, this operator could be described as "trinary".

This is the basic syntax:

(expression1) ? (expression2) : (expression3);
(BOOLEAN EXPR ? VALUE-IF-TRUE : VALUE-IF-FALSE)

Returns one of two expressions depending on a condition.

If expression1 is true, expression2 is returned, else expression3 is returned

true ? exp2 : exp3 // exp2 returned
false ? exp2 : exp3 // exp3 returned

Arguments

test
Any Boolean expression.
- a boolean type

      Subscribe in a reader

Fixing Warning: main(): stream does not support seeking

Fixing "Warning: main(): stream does not support seeking"

PHP 4.3.? include/require bug

How do I bypass/get rid of the warning: main(): stream does not support seeking?

It looks like the 4.3 versions of PHP have reintroduced an old bug with include/require in regards to including files with URLs. I have sites with two different ISPs and both have upgraded their servers in the past few weeks (without any warning) and this has caused me problems on both sites.

You will see the following warning message:

Warning: main(): stream does not support seeking

      Subscribe in a reader

Intelligent/Smart Favorites in IE

Intelligent/Smart Favorites in IE

Turning on the Smart/Intelligent Favorites Menu in Internet Explorer 5.x/6

      Subscribe in a reader

PHP Sessions

Session allows you to preserve data across page requests (going from one page to another on a website) by saving the data to variables that you register as being part of a session. Sessions are tracked through
cookies or through the URL. I prefer to track sessions through the URL because cookies may not be enabled on the client computer.

If PHP is compiled with --enable-trans-sid then URLs will contain the session id automatically via the constant PHPSESSID.

Function session_start(void):

      Subscribe in a reader

Downloading files with PHP

Downloading Files

Using the header function to force file downloads

If you have files that you would like users to be able to download, it can be done simply by providing a link to the file. When the link is clicked, the browser will prompt the user to download the file or it will display it if the file type is known to the browser i.e. text files and gif/jpeg images. If you do not want the files displayed in the browser you can use the header() in PHP with the appropriate content type to force a file download.

      Subscribe in a reader

PHP Quiz Script

PHP Quiz Script

Hard coding a graded quiz script in PHP

Quizzes are generally used to test knowledge of a subject.
This is a simple script that will allow you to create a quiz and grade the answers given.

PHP Code:

<?php
$let=array(
"a","b","c","d","e","f","g","h","i",
"j","k","l","m","n","o","p","q","r",
"s","t","u","v","w","x","y","z"
);

//Set a counter to 1
$count = 1;

/* What is a perfect score? Usually 100. */

$psco=100;

/* Deduction for each wrong answer. May be the perfect score divided by the number of questions

      Subscribe in a reader

Simple MySQL Guestbook

Creating the guestbook has two parts, the table to hold the guestbook records and the script to insert and select records from the database
table.

The structure of the table should look similar to this:

CREATE TABLE guests (
id int(10) unsigned NOT NULL auto_increment,
guest varchar(50) NOT NULL default '',
message varchar(200) NOT NULL default '',
date datetime default NULL,
PRIMARY KEY (id)
) ;

The guestbook script has three sections - the code for the form, the code for saving the form data, and the code for displaying the data.

      Subscribe in a reader

A Closer Look: Five MySQL functions that handle query results

A Closer Look: Five MySQL functions that handle query results

Assigning variable names to a MySQL result set

After executing a query to select data from a MySQL table there are several functions you can use to perform operations on the retrieved data.

mysql_result() - fetches the contents of the specified cell

mysql_data_seek() - moves to the specified row of a result set

mysql_fetch_array() - fetches the next row in the result set as an array

mysql_fetch_object() - fetches the next row in the result set as an object

      Subscribe in a reader

The Image Creation Capabilities of PHP

The Image Creation Capabilities of PHP

PHP image functions will allow you to create images dynamically

"The format of images you are able to manipulate depend on the version of GD you install, and any other libraries GD might need to access those image formats. Versions of GD older than gd-1.6 support gif format images, and do not support png, where versions greater than gd-1.6 support png, not gif." -- From the PHP Manual: Image Functions

      Subscribe in a reader

Count the number of MySQL Queries

Count the number of MySQL Queries

How to count the number of MySQL queries used by your script

One way of optimizing your scripts that connect to MySQL databases is to determine how many queries to the database your script makes. If you would like to count the number of queries, you can write a script that will do so. You use this function in place of the regular mysql_query() function.

PHP Code:

<?php

function cnt_mysql_query($sql=FALSE)
{
static $queries = 0;
if (!$sql)
return $queries;

$queries ++;

return mysql_query($sql);
}

$sql= "SELECT 8+9";

      Subscribe in a reader
Syndicate content