a The extension of php file is .php
a This extension signifies to the server that it needs to parse the php code before sending the resulting html code to the viewer's web browser.
a PHP is denoted in the page with opening and closing tags, as follows,

..... ..... ....
..... ..... ....
?>

The lines end with a semicolon.

echo "What is you name?";
echo $name;
?>

a You can add comments frequently in you code.

// This is a comment statement that is not compiled by web server;
/* You can use this kind of comment also when;
you need to write multi-lined text as comment */;
?>

a Some few PHP coding are given below;

echo "

Hi $_POST['fname']

";
echo "

Your name's not Jeo, so you can't enter the web site.

";
?>

Or

echo "

";
echo "Hi";
echo "$_POST['fname']";
echo "

";
?>

Considerations with HTML Inside PHP

a You have to check for double quotes. Ex: echo "";a Remember that you still have to follow PHP rules, even though you're coding in HTML.
a Don't try cram too much HTML into your PHP sections.

Using Constants and Variables

Constant:

Syntax:
define("CONSTANT NAME","VALUES")

define ("FAVMOVIE","The Life of Brian");
?>

Overview of variables

a Variables are denoted with a dollar sign ($).
a They are case-sensitive.
a The first letter of the variable name must be an underscore or letter and cannot be a number.

echo "The average movie rating for this movie is : ";
echo $variable5;
?>

Function

Description

rand([min],[max])

Generates a random integer.

ceil(number)

Rounds a decimal up to the next highest integer.

floor(number)

Rounds a decimal down to the next lowest integer.

max(argument1,argument2, ...)

Returns the maximum value of the supplied arguments.

min(argument1,argument2, ...)

Returns the minimum value of the supplied arguments.

Passing Variables Between Pages

There are basically four ways to accomplish this task;

1) Pass the variables in the URL.
2) Through a session.
3) Via a cookie
4) With an HTML form.

A Word about register_global

a This is a configuration setting in your php.inia Coding with the assumption that register_globals has been turned off is the safest way to code because your program will work regardless of the server's setting.
a We can pass the value of variables from one page to another by using the following functions without based on whether register_globals is turned on or off.

Syntax

When to use it

$_GET['varname']

When the method of passing the variable is the "GET" method in HTML forms.

$_POST['varname']

When the method of passing the variable is the "POST" method in HTML forms.

$_SESSION['varname']

When the variable has been assigned the value from a particular session.

$_COOKIE['varname']

When the variable has been assigned a value from a cookie.

$_REQUEST['varname']

When it doesn't matter ($_REQUEST includes variables passed from any of the above methods)

$_SERVER['varname']

When the variable has been assigned a value from the server.

$_FILES['varname']

When the variable has been assigned a value from a file upload.

$_ENV['varname']

When the variable has been assigned a value from the operating environment.

Passing Variables Through a URL (Uniform Resource Allocate)

Example:
First File name is index.php which will send value of variables to second file.

a A session is basically a temporary set of variables that exists only until the browser has shut down.
a Unless you can set up differently in your php.ini file.
a Every session is assigned a unique session ID, which keeps all the current information together.
a Your session ID either can be passed through the URL or through the use of cookies.
a Although it is preferable for security reasons to pass the session ID through a cookie so that it is hidden from the human eye.
a If cookies are not enabled, the backup method is through the URL.
a If you would like to force the user to pass variables through cookies, you would set the following line in your php.ini. Such as, session.use_only_cookies=1a To begin a session, use the function session_start().
a Because we assume you have register_globals set to "off", you should not use the session_register() function you may have seen in other PHP scripts.
a Make sure before using sessions that your php.ini file has been modified to show a valid path in the session.save_path variable.
a All the session information is at the top of the page, before any HTML code. This is very important. If there is even a leading space before the php code at the top of the page, you will get the error.

The Second file: page1.php session_start();
// check to see if user has logged in with a valid password
if ($_SESSION['authuser'] != 1) {
echo "Sorry, but you don't have permission to view this page.";
exit();
}
?>

a Cookies are tiny bits of information stored on your Web site visitor's computer.
a Your visitors may either have cookies turned off or may physically delete cookies from their computers.
a The advantage to storing information in a cookie versus a session is longevity. Session can't store information for more than the length of time the browser window is opened.
a Cookies can live on a person's computer until the developer has decided it's been long enough and they automatically die.
a If your sessions are passing variables using cookies, you can set the life of these cookies to longer than the life of the browser using the session.cookie_lifetime configuration in your php.ini file.
a Like session, cookie must be placed at the very top of he page, before your first line.

Time in seconds when the cookie will expire. This time is based on a Unix timestamp, but you can set it using the syntax time()+60*60*28*365, which keeps the cookie alive for a year. This is optional, but if it is not set, the cookie will expire when the browser is closed.

path

The directory where the cookie will be saved - optional.

domain

Domains that may access this cookie - optional.

secure connection

Whether a cookie must have a secure connection to be set (defaults to 0, to enable this feature set this to 1)

Second file: page1.php session_start();
// check to see if user has logged in with a valid password
if ($_SESSION['authuser'] != 1) {
echo "Sorry, but you don't have permission to view this page.";
exit();
}
?>

A form is made up of four parts -
1. Opening tag line, indicated by the tag: This tag must include an action and a method. A method may be either GET or POST (however POST is preferred method because it is more secure).

2. Content of the form, including input fields: Input areas where user types in the information (or selects it in the case of a checkbox or radio button). An input field must include a type and a name. The type of input field can be one of many different selections, the most common beings:
a) Text: Used for collecting from 2 characters up to 2,000 characters.
b) Checkbox: Used to allow user to make a selection from a list of choices; also permits user to make more than one choice.
c) Radio: Used for allowing users to choose from a list, but permits only one choice.
d) Select: Used for allowing users to choose from a list.
e) Password: Hides what user is typing behind asterisks, but does not compromise the value of the variable.

3. Action button(s) or images, typically submit/clear or a user-defined button, typically considered input types as well: These are indicated with the input types submit, reset and image for user-created buttons.

Syntax:
if (condition1 operator condition2) action to be taken if true;

Example:
if ($stockmarket >= 10000) echo "Hooray! Time to Party!";

Example: If the action to take is longer than a simple statement that will easily fit on one line.
if ($stockmarket >=10000) {
echo "Hooray! Time to Party!";
$mood = "happy";
$retirement = "Potentially obtainable";
}

Example: Here, date("n") returns a value equal to the numerical equivalent of the month as set in your server, such as 1 for January, 2 for February and so no.

equal to, AND data types match (both are integers, or both are strings)

===

not equal to, OR the data types are not the same

!==

Using Includes for effective code

Example:
First File: included_file.php - which will be added to the main page.

Welcome to my movie review site

echo "Today is ";
echo date("F d");
echo ", ";
echo date("Y");
?>

Second file: main.php -

Example of file INCLUDING

included_file.php"; ?>

My Favorite Movie is : Matrix Part I, II and III

Using Functions for Efficient Code

a Functions are mini-programs within themselves.

a They don't know about any other variables around them unless you let the other variables outside the function in through a door called "global".

a Your functions can be located anywhere within your script and can be called from anywhere within your script.

a You can list all your commonly used functions at the top of your program and they can all be kept together for easier debugging. Better yet, you can put all your functions in a file and include them in your programs.

foreach ($flavor as $currentvalue) {
// These lines will execute as long as there is a value in $flavor
echo $currentvalue . " \n";
}
?>

While Statement

Example: At the top of the loop, the while checks to see that the value of $num is less than or equal to 5. After five times through the loop, the value of $num is 6, so the loop ends.
$num = 1;
while ($num <= 5) {
echo $num;
echo "";
$num = $num + 1;
}

Example: The following code works exactly the the same way, except that the condition is checked at the end of the loop. This guarantees that the conditions inside the loop will always be executed at least once.
$num = 1;
do {
echo $num;
echo "";
$num = $num + 1;
} while ($num <=5);

echo

Example:
echo "My top ". $_POST['num'] ." movies are : ";

Alternates to the Tags

a : This must be turned on in your php.ini file with the short open tags configuration.

a : This must be turned on in your php.ini file with the ASP tags configuration.

a : These are available without changing your php.ini file.

Alternates to the echo command

a print() command to display text or variable values in your page.

a The difference between echo() and print() is that when you use print(), a value of 1 or 0 will also be returned upon the success or failure of the print command.

Altering to Logical Operators

a && can be used in place of and, only difference being the order in which the operators is evaluated during a mathematical function.

a || can be used in place of or, only difference being the order in which the operators is evaluated during a mathematical function.