Using static variables

Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

<?php
function Test()
{
    static $a = 0;
    echo $a;
    $a++;
}
?>
	$GLOBALS['choice'] = array(0 => $opt1[0], 1 => $opt2[0], 2 => $opt3[0], 3 => $ans[0]);
	$GLOBALS['num'] = range(0,3);
	srand((float)microtime() * 1000000);
	shuffle($GLOBALS['num']);

	echo "<p style='color:rgb(1,255,1);'>以下那一個是錯別字:</p>";
	for ($i = 0; $i <= 3; $i++) {
		echo $i+1 . ": " . $GLOBALS['choice'][$GLOBALS['num'][$i]] . "  ";

		// to capture the answer number (for MC of four options)
		if ($GLOBALS['num'][$i] == 3) $GLOBALS['ansnum'] = $i;
	}

<?php
$a = 1; /* global scope */ 

function Test()
{
    echo $a; /* reference to local scope variable */
} 

Test();
?>

This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function.

A little technical problem:
PHP part:

<select id="mc">
  <option value="1"><?php echo $choice[0];?></option>
  <option value="2"><?php echo $choice[1];?></option>
  <option value="3"><?php echo $choice[2];?></option>
  <option value="4"><?php echo $choice[3];?></option>
</select>

<input type="button" id="answer_btn" style="float: left;" value="選擇答案" onclick="answerbox(<?php echo $ans[0];?>)"/>

jScript part:

function answerbox(a) {

obja = document.getElementById("mc");
if (a == obja.value)
	alert ("correct !");
else
	alert ("wrong !");

}

CAN’T GET IT OUT !!!!!!!!!!!!!!!!!!!!!

$query = "SELECT cword FROM ww_tbl ORDER by rand() LIMIT 1";
$result = mysql_query($query);
$opt1 = mysql_fetch_row($result);
$query = "SELECT cword FROM ww_tbl ORDER by rand() LIMIT 1";
$result = mysql_query($query);
$opt2 = mysql_fetch_row($result);
$query = "SELECT cword FROM ww_tbl ORDER by rand() LIMIT 1";
$result = mysql_query($query);
$opt3 = mysql_fetch_row($result);
$query = "SELECT wword FROM ww_tbl ORDER by rand() LIMIT 1";
$result = mysql_query($query);
$ans = mysql_fetch_row($result);

$choice = array(0 => $opt1[0], 1 => $opt2[0], 2 => $opt3[0], 3 => $ans[0]);
$num = range(0,3);
srand((float)microtime() * 1000000);
shuffle($num);

echo "<p style='color:rgb(1,255,1);'>以下那一個是錯別字:</p>";
foreach ($num as $n) {
    echo $choice[$n] . "  ";
}

$query = "SELECT wword ,cword FROM ww_tbl ORDER by rand() LIMIT 1";
$result = mysql_query($query);

$row = mysql_fetch_row($result);

echo $row[0] . "的正寫應是" . $row[1] ;
CREATE TABLE `tbl_user`
(
	`userid` integer (11) NOT NULL AUTO_INCREMENT ,
	`username` varchar (50) NOT NULL,
	`useremail` varchar (100) NOT NULL,
	`userpassword` varchar (50) NOT NULL,
	PRIMARY KEY (`userid`)
) TYPE=MyISAM CHARACTER SET utf8 COLLATE utf8_general_ci;

BEGIN;
INSERT INTO `tbl_user` (`userid`, `username`, `useremail`, `userpassword`) VALUES(1, 'admin', 'admin@chazzuka.com', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `tbl_user` (`userid`, `username`, `useremail`, `userpassword`) VALUES(2, 'user', 'user@chazzuka.com', 'e10adc3949ba59abbe56e057f20f883e');
COMMIT;

Date Function by Java Script:

function showdate() {
var t = new Date;
var day = t.getDate();
var month = t.getMonth() + 1;
var year = t.getFullYear();
var weekday = t.getDay();
var daynames = new Array(
      '日','月','火','水','木','金','土');
//if zeros are needed to keep date format correct!
if (day < 10) day = "0" + day;
if (month < 10) month = "0" + month;
var stoday = month + '/' + day + '/' + year;
document.getElementById('showdate').innerHTML = stoday + '(' + daynames[weekday] + ')';

// set the date field of today
$("input#date").val(stoday);
}

jquery thick box example:

<script type="text/javascript" src="thickbox.js"></script>
<link rel="stylesheet" href="thickbox.css" type="text/css" media="screen" />

<a href="#TB_inline?height=100&amp;width=280&amp;inlineId=input2" class="thickbox" style="font-size: 0.6em;" title="事件">?</a>
<div id="input2" style="display: none;">
<p>發生的事件,應盡量包括有關的金額、單據、支票號碼。時間和地點和有關的項目亦應註明。</p>
</div>

There are two methods to propagate a session id:

* Cookies
* URL parameter

The session module supports both methods. Cookies are optimal, but because they are not always available, we also provide an alternative way. The second method embeds the session id directly into URLs.

PHP is capable of transforming links transparently.

Note: The arg_separator.output php.ini directive allows to customize the argument seperator. For full XHTML conformance, specify & there.

Alternatively, you can use the constant SID which is defined if the session started. If the client did not send an appropriate session cookie, it has the form session_name=session_id. Otherwise, it expands to an empty string. Thus, you can embed it unconditionally into URLs.

The following example demonstrates how to register a variable, and how to link correctly to another page using SID.

<?php
session_start();
if (empty($_SESSION['count'])) {
 $_SESSION['count'] = 1;
} else {
 $_SESSION['count']++;
}
?>
<p>
Hello visitor, you have seen this page <?php echo $_SESSION['count']; ?> times.
</p>
<p>
To continue, <a href="nextpage.php?<?php echo htmlspecialchars(SID); ?>">click
here</a>.
</p>

The htmlspecialchars() may be used when printing the SID in order to prevent XSS related attacks.

Printing the SID, like shown above, is not necessary if –enable-trans-sid was used to compile PHP.

Note: Non-relative URLs are assumed to point to external sites and hence don’t append the SID, as it would be a security risk to leak the SID to a different server.