PHP - Bits & Pieces

PHP programming codes and scripting are discussed in the manner of use and relevance. Examples of PHP syntax and scripting are presented in concise writups for better understanding.

Wednesday, May 28, 2008

Using PHP to solve the session_start() Error

This is a commonly occurred error in PHP programming. It is usually caused by the session start script. When a separate header file is included at the top of the page, the error is caused by duplicate HTML tag or white space before the session start script. This happen when the web page try to resend headers information

Error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent


Solution:

Place the following script at the top of the page:
<?php
ob_start();
session_start();
?>


and this at the end of the page:
<?php // Flush the buffered output to the Web browser.
ob_flush();
?>

Tuesday, May 27, 2008

Using PHP to read posted form data

The first line below shows how form data is read by PHP. 'StsToAnInch' is the the input type name. In this case the input type is a text box. The second line is how the text box is written in html

$_POST['StsToAnInch']

Note: The code works with an input text field named "StsToAnInch".

Monday, May 26, 2008

Using PHP to add to strings or join string with string variables

In PHP programming, one constantly has to be reminded of how to add a string of text to an existing one dynamically. We first create a line of string text and assign it to a string variable. We add onto the string in the following manner:

$instn = $instn . "No shaping is needed from waist to underarm as their widths are the same.\n\n";

Remember to add the new string to the variable with '.' or else the new text will replace all previous text in the variable.

Friday, May 23, 2008

Using PHP to check if a form has been submitted

The 'if' condition in this PHP script is place in the same page as the html form and is located at the top of the page, above the form. The purpose is to verify the inputs of the form. Example of uses include the Login Page.

if (isset($_POST['submit'])) {

//run this code when the submit button is activated, name the button submit

}

Thursday, May 22, 2008

Using PHP to find Out if a Nummber is Odd or Even

This little bit of scripts was most useful in the coding of Waist Shaping Knitting Calculator . It evens out the number of knitting stitches so that both sides of the waist line get to be adjusted evenly.

The 'if' condition tests if variable '$Sts' is an odd number:

if( $Sts & 1 ){
$Sts = $Sts + 1;} //true when $Sts is odd, so add 1 to $Sts to make it even.


This bit of php will be useful for further development of the Knitting Calculator series.

My Blog, My PHP Journal

I started designing my website using ASP.net five years ago. Simply because I was introduced first to ASP.net. It has its advantages and problems. So, I took up php which was a lot faster and easier to code. It was easy in the beginning but gets a bit more tedious as I am not a professional programmer and only use php to build and maintain my site every now and then.

Bits and pieces of coding got lost once you stop using them. The best solution then is to store the scripts in a place when you need them and they never get lost. Store the useful PHP scripts in a Blog!