Header Section:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $page['title'];?></title>
</head>
<body>
<!-- top menu bar -->
<table width="90%" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><a href="#">Home</a></td>
<td><a href="#">Site Map</a></td>
<td><a href="#">Search</a></td>
<td><a href="#">Help</a></td>
</tr>
</table>
<!-- header ends -->
Main Section:
<?php
// create an array to set page-level variables
$page = array();
$page['title'] = 'Product Catalog';
/* once the file is imported,
the variables set above will become available to it */
// include the page header
include('header.php');
?>
<!-- HTML content here -->
<?php
// include the page footer
include('footer.php');
?>
Footer Section:
<!-- footer begins -->
<br />
<center>
Your usage of this site is subject to its published
<a href="tac.html">terms and conditions</a>
. Data is copyright Big Company Inc, 1995-<?php
echo date("Y", mktime()); ?></center
</body>
</html>
Sunday, January 2, 2011
php - working with files
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* fopen() * returns "file handle"
*
* fread() *
*/
/*
// this does NOT work!
$file =
'http://localhost/testFolder/tetsuro/tetsuroP03_php_file/RandomSentences.txt'
or die('Could not open file!');
*/
// this works
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
/*
// this works too
// note: the file is in the same folder as this php file.
$file = 'RandomSentences.txt' or die('Could not open file!');
*/
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');
//
// some debug stuff
//
echo "<br /><br />";
$size=filesize($file);
echo "total bytes in the file is $size";
echo "<br />";
echo "this is what's in the file handle: $fh";
echo "<br /><br />";
// close file
fclose($fh);
// print file contents
echo $data;
echo "<br /><br />";
/*
* file() * opens the file, reads it into an array and closes the file - all in one
*
* foreach() * using loop, use this command to read array
*
*/
echo 'using file()';
echo "<br /><br />";
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
echo $line;
}
echo "<br /><br />";
/*
* file_get_contents() * reads the entire file into a string - all in one
*
*/
echo 'using file_get_contents()';
echo "<br /><br />";
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
// read file into string
$data = file_get_contents($file) or die('Could not read file!');
// print contents
echo $data;
echo "<br /><br />";
?>
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* fopen() * returns "file handle"
*
* fread() *
*/
/*
// this does NOT work!
$file =
'http://localhost/testFolder/tetsuro/tetsuroP03_php_file/RandomSentences.txt'
or die('Could not open file!');
*/
// this works
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
/*
// this works too
// note: the file is in the same folder as this php file.
$file = 'RandomSentences.txt' or die('Could not open file!');
*/
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');
//
// some debug stuff
//
echo "<br /><br />";
$size=filesize($file);
echo "total bytes in the file is $size";
echo "<br />";
echo "this is what's in the file handle: $fh";
echo "<br /><br />";
// close file
fclose($fh);
// print file contents
echo $data;
echo "<br /><br />";
/*
* file() * opens the file, reads it into an array and closes the file - all in one
*
* foreach() * using loop, use this command to read array
*
*/
echo 'using file()';
echo "<br /><br />";
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
echo $line;
}
echo "<br /><br />";
/*
* file_get_contents() * reads the entire file into a string - all in one
*
*/
echo 'using file_get_contents()';
echo "<br /><br />";
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
// read file into string
$data = file_get_contents($file) or die('Could not read file!');
// print contents
echo $data;
echo "<br /><br />";
?>
</body>
</html>
Saturday, January 1, 2011
php - working with array
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
//
// print_r()
// use it to debug array
//
echo '<i>using print_r() to debug anime list.</i><br />';
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore');
print_r($animeGroupList);
echo '<br />';
$animeList=array('naruto','high school of dead','bakuman','claymore');
print_r($animeList);
echo '<br />';
echo '<br />';
//
// array_push()
// add an element to the end
//
// note: does not work with associative arrays
//
$anime='bleach';
echo "<i>using array_push(), added $anime at the end.</i><br />";
array_push($animeList,'bleach');
print_r($animeList);
echo '<br />';
echo '<br />';
//
// array_pop()
// remove an element from the end,
// and returns it
//
$anime=array_pop($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_pop(), $anime was removed from the end of anime list.</i>";
echo '<br />';
echo '<br />';
//
// array_shift()
// take an element off the beginning
//
$anime=array_shift($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_shift(), removing $anime the beginning.</i>";
echo '<br />';
echo '<br />';
//
// array_unshift()
// add an element to the beginning
//
// note: does not work with associative arrays
// note: returns the shifted value - NOT the element!
//
$anime=array_unshift($animeList, 'naruto');
print_r($animeList);
echo '<br />';
echo "<i>using array_unshift(), added $anime to beginning.</i><br />";
echo '<br />';
echo '<br />';
//
// explode()
// using a deliminater, splits a string into array elements
//
echo "using explode()<br />";
$str = 'shinigami, zombie, ninja';
echo "$str <br />";
$charactorTypeList = explode(', ', $str);
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
//
// inplode()
// opposite of explode()
//
echo "using implode(), putting it back to a string<br />";
$str=implode(', ', $charactorTypeList);
echo $str;
echo '<br />';
echo '<br />';
//
// sort() and rsort()
//
echo 'original list';echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
sort($charactorTypeList);
echo 'after sort()';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
rsort($charactorTypeList);
echo 'after rsort() ';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
//
// getting elements out of array
//
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
for ($x = 0; $x < sizeof($artists); $x++){
echo "<li> $artists[$x]";
// echo '<li>'.$artists[$x]; //this works too
}
print "<br />";
print "<br />";
//
// array_key()
// array_values()
// for associative array
//
echo 'print keys and values of array of:<br />';
echo "'ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore'";
print "<br />";
print "<br />";
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore');
$result = array_keys($animeGroupList);
print_r($result);
print "<br />";
$result = array_values($animeGroupList);
print_r($result);
print "<br />";
print "<br />";
//
// foreach()
//
echo 'the list of artists: ';
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
foreach($artists as $a){
echo '<li>'.$a;
}
?>
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
//
// print_r()
// use it to debug array
//
echo '<i>using print_r() to debug anime list.</i><br />';
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore');
print_r($animeGroupList);
echo '<br />';
$animeList=array('naruto','high school of dead','bakuman','claymore');
print_r($animeList);
echo '<br />';
echo '<br />';
//
// array_push()
// add an element to the end
//
// note: does not work with associative arrays
//
$anime='bleach';
echo "<i>using array_push(), added $anime at the end.</i><br />";
array_push($animeList,'bleach');
print_r($animeList);
echo '<br />';
echo '<br />';
//
// array_pop()
// remove an element from the end,
// and returns it
//
$anime=array_pop($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_pop(), $anime was removed from the end of anime list.</i>";
echo '<br />';
echo '<br />';
//
// array_shift()
// take an element off the beginning
//
$anime=array_shift($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_shift(), removing $anime the beginning.</i>";
echo '<br />';
echo '<br />';
//
// array_unshift()
// add an element to the beginning
//
// note: does not work with associative arrays
// note: returns the shifted value - NOT the element!
//
$anime=array_unshift($animeList, 'naruto');
print_r($animeList);
echo '<br />';
echo "<i>using array_unshift(), added $anime to beginning.</i><br />";
echo '<br />';
echo '<br />';
//
// explode()
// using a deliminater, splits a string into array elements
//
echo "using explode()<br />";
$str = 'shinigami, zombie, ninja';
echo "$str <br />";
$charactorTypeList = explode(', ', $str);
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
//
// inplode()
// opposite of explode()
//
echo "using implode(), putting it back to a string<br />";
$str=implode(', ', $charactorTypeList);
echo $str;
echo '<br />';
echo '<br />';
//
// sort() and rsort()
//
echo 'original list';echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
sort($charactorTypeList);
echo 'after sort()';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
rsort($charactorTypeList);
echo 'after rsort() ';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
//
// getting elements out of array
//
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
for ($x = 0; $x < sizeof($artists); $x++){
echo "<li> $artists[$x]";
// echo '<li>'.$artists[$x]; //this works too
}
print "<br />";
print "<br />";
//
// array_key()
// array_values()
// for associative array
//
echo 'print keys and values of array of:<br />';
echo "'ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore'";
print "<br />";
print "<br />";
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore');
$result = array_keys($animeGroupList);
print_r($result);
print "<br />";
$result = array_values($animeGroupList);
print_r($result);
print "<br />";
print "<br />";
//
// foreach()
//
echo 'the list of artists: ';
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
foreach($artists as $a){
echo '<li>'.$a;
}
?>
</body>
</html>
php- form submit (another one)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* if the "submit" variable does not exist,
* the form has not been submitted - display initial page
*/
if(!isset($_POST['submit'])){
?>
<form action="
<?php echo $_SERVER['SCRIPT_NAME']; ?>"
method="post">Print all the squares between 1 and
<input type="text" name="limit" size="4" maxlength="4">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else{
/*
* if the "submit" variable exists,
* the form has been submitted - look for and process form data
*/
// set variables from form input
$upperLimit = $_POST['limit'];
$lowerLimit = 1;
// keep printing squares until lower limit = upper limit
while ($lowerLimit <= $upperLimit) {
echo ($lowerLimit * $lowerLimit).' ';
$lowerLimit++;
}
// print end marker
echo 'END';
}
?>
<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* if the "submit" variable does not exist,
* the form has not been submitted - display initial page
*/
if(!isset($_POST['submit'])){
?>
<form action="
<?php echo $_SERVER['SCRIPT_NAME']; ?>"
method="post">Print all the squares between 1 and
<input type="text" name="limit" size="4" maxlength="4">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else{
/*
* if the "submit" variable exists,
* the form has been submitted - look for and process form data
*/
// set variables from form input
$upperLimit = $_POST['limit'];
$lowerLimit = 1;
// keep printing squares until lower limit = upper limit
while ($lowerLimit <= $upperLimit) {
echo ($lowerLimit * $lowerLimit).' ';
$lowerLimit++;
}
// print end marker
echo 'END';
}
?>
<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->
</body>
</html>
php- form submit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/* if the "submit" variable does not exist,
* the form has not been submitted - display initial page */if(!isset($_POST['submit'])){
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
Enter your age: <input name="age" size="2">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else{
/* if the "submit" variable exists,
* the form has been submitted - look for and process form data
*/ // display result
$age = $_POST['age'];
if($age >= 21){
echo 'Come on in, we have alcohol and music awaiting you!';
}
else{
echo 'You\'re too young for this club,
come back when you\'re a little older';
}
}
?>
<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/* if the "submit" variable does not exist,
* the form has not been submitted - display initial page */if(!isset($_POST['submit'])){
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
Enter your age: <input name="age" size="2">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else{
/* if the "submit" variable exists,
* the form has been submitted - look for and process form data
*/ // display result
$age = $_POST['age'];
if($age >= 21){
echo 'Come on in, we have alcohol and music awaiting you!';
}
else{
echo 'You\'re too young for this club,
come back when you\'re a little older';
}
}
?>
<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->
</body>
</html>
php - "===" operator
<?php
$str = '7';
$int = 7;
//usual stuff
$result = ($str == $int);
echo "result is $result<br />";
//returns false because type is not the same
$result = ($str === $int);
echo "result is $result<br />";
?>
$str = '7';
$int = 7;
//usual stuff
$result = ($str == $int);
echo "result is $result<br />";
//returns false because type is not the same
$result = ($str === $int);
echo "result is $result<br />";
?>
php - String Concatenation Operator
The string concatenation operator, . is like + for numbers.
<?php
$str='the';
$str.='n';
echo $str; //prints out "then"
?>
<?php
$str='the';
$str.='n';
echo $str; //prints out "then"
?>
Subscribe to:
Posts (Atom)