March 11, 2008

PHP Quick Reference

PHP Syntax

The basic PHP syntax is as follows:


<?php

// enter lines of code, make sure they end with a semicolon;

?>


Displaying to Browser

To display text in a browser, use the following syntax:


<?php

echo “Enter text here”; //echo text

echo $variable; //echo values

echo “<br>”; //echo HTML text

?>


Assigning a Value to a Variable

To set a value to a variable, use the following syntax:


<?php

$varname = value; //for numeric

$varname = “value”; //for text

?>


Passing Variables

We can pass variables among pages in our Web site in three ways: through a URL, through sessions,

and through a form.

Through a URL

To pass a variable through a URL, use the following HTML code:


<a href=”http://www.localhost.com/index.php?varname=value”>


Through Sessions

To pass a variable through a session, use the following PHP code:


<?php //this must be the first line of the script, before HTML code

session_start(); //starts the session

$_SESSION[‘varname’] = value; //sets values for the entire session

$_SESSION[‘varname2’] = value;

?>

<?php //this must be the first few lines of every

//page accessing session variables

session_start();

?>


Through a Form

A form must reference the PHP script that will parse the variables:


<?php

$value = $_POST[‘varname’]; //this is how we will access the

//values from the form

?>


if Statements

To use if statements, type the following syntax:


<?php
if (this is true) //execute this command
?>

or

<?php

if (this is true) {

//execute command 1;

//execute command 2;

//execute command 3;


}

?>


else Statements

To use else statements, type the following syntax:


<?php

if (this is true) //execute this command;

else //execute this command

?>

or

else {

//execute command 1;

//execute command 2;

//execute command 3;


}

?>


Nested if Statements

We can use nested if statements by using the following syntax:

<?php

if (this is true) { //remember to use == for equals

if (this is true) //execute this command;

if (this is true) //execute this command;

else //execute this command;

}

?>


Including a File

To include a file, use the following syntax:


<?php include “header.php”; ?>, or

<?php require “header.php”; ?>



Using Functions

We can create and call functions using the following syntax:


<?php

function funcname()
{ //defines the function

//line of php code;

//line of php code;

//line of php code;


}

funcname(); //calls the function to execute

?>


Arrays

We can set the values for an array in one of two ways:


<?php

$name = array(“firstname”=>”ridho”, “lastname”=>”fitra”, “age”=”124”);

echo $name[“firstname”];

?>

or

<?php

$name[“firstname”] = “ridho”;

$name[“lastname”] = “fitra”;

$name[“age”] = 21;

?>


If no keys are required, we can set the values for an array like this:


<?php

$flavor[] = “infocomnet”;

$flavor[] = “ictworld2u”;

$flavor[] = “eurokickoff”;

?>


for

We can execute a block of code a specified number of times with the following for statement:


<?php

for ($n = 0; $n <= 10; $n=$n+1) {

//these lines will execute while the value ‘n’ is

//less than or equal to 10


echo $n;

echo “<br>”;

}


foreach

We can apply the same block of code to each value in a specified array with the foreach statement:


foreach ($arrayvalue as $currentvalue) {

//these lines will execute as long as there is a value in $arrayvalue

echo $currentvalue;

echo “<br>\n”;

}


Share

0 comments:

Blogger template 'PlainFish' by Ourblogtemplates.com 2008